mirror of
https://github.com/wenlzhang/obsidian-tag-index.git
synced 2026-07-22 05:44:03 +00:00
Prepare for submission
This commit is contained in:
parent
c2f47840d7
commit
b3c64f8135
2 changed files with 11 additions and 45 deletions
34
src/main.ts
34
src/main.ts
|
|
@ -23,6 +23,7 @@ import { TagIndexSettingTab } from "./settingsTab";
|
|||
export default class TagIndexPlugin extends Plugin {
|
||||
settings: TagIndexSettings;
|
||||
tagIndexView: TagIndexView | null = null;
|
||||
private contextMenuHandler: (event: MouseEvent) => void;
|
||||
|
||||
async onload() {
|
||||
await this.loadSettings();
|
||||
|
|
@ -103,6 +104,13 @@ export default class TagIndexPlugin extends Plugin {
|
|||
});
|
||||
}
|
||||
|
||||
onunload() {
|
||||
// Clean up resources when the plugin is disabled
|
||||
console.log('Unloading Tag Index plugin');
|
||||
this.app.workspace.detachLeavesOfType(TAG_INDEX_VIEW_TYPE);
|
||||
document.removeEventListener("contextmenu", this.contextMenuHandler, true);
|
||||
}
|
||||
|
||||
// Helper to find a tag at the cursor position
|
||||
findTagAtPosition(
|
||||
tags: TagCache[],
|
||||
|
|
@ -136,17 +144,10 @@ export default class TagIndexPlugin extends Plugin {
|
|||
|
||||
// Fallback: try to extract from text content if data-tag is not available
|
||||
if (!tagName) {
|
||||
// DEBUG: Log what we can find in the DOM element
|
||||
console.log("Tag pane element:", target);
|
||||
console.log("Tag pane attributes:", target.attributes);
|
||||
console.log("Tag pane innerText:", target.innerText);
|
||||
console.log("Tag pane textContent:", target.textContent);
|
||||
|
||||
// First see if the element has any parent with a data-tag attribute
|
||||
const parentWithDataTag = target.closest("[data-tag]");
|
||||
if (parentWithDataTag) {
|
||||
tagName = parentWithDataTag.getAttribute("data-tag");
|
||||
console.log("Found tag name from parent data-tag:", tagName);
|
||||
}
|
||||
|
||||
// If still no tag name, try text content
|
||||
|
|
@ -157,7 +158,6 @@ export default class TagIndexPlugin extends Plugin {
|
|||
// If there's a linebreak, the tag name is the part before it
|
||||
const parts = innerText.split('\n');
|
||||
tagName = parts[0].trim();
|
||||
console.log("Using tag name from innerText before linebreak:", tagName);
|
||||
}
|
||||
// Fallback to textContent if innerText doesn't work
|
||||
else {
|
||||
|
|
@ -167,9 +167,7 @@ export default class TagIndexPlugin extends Plugin {
|
|||
// Get all tags in the vault for reference
|
||||
const allTags = this.getAllTags().map(t =>
|
||||
t.startsWith('#') ? t.substring(1) : t);
|
||||
console.log("All tags in vault:", allTags);
|
||||
|
||||
// DIRECT APPROACH FOR THE SPECIFIC CASE:
|
||||
// Check if removing last character results in a valid tag
|
||||
const withoutLastChar = tagText.slice(0, -1);
|
||||
|
||||
|
|
@ -177,25 +175,20 @@ export default class TagIndexPlugin extends Plugin {
|
|||
// matches a tag we already know about
|
||||
if (allTags.includes(withoutLastChar)) {
|
||||
tagName = withoutLastChar;
|
||||
console.log("Found tag by removing last character:", tagName);
|
||||
}
|
||||
// Next try removing a space-number pattern
|
||||
else if (tagText.match(/^(.*?)\s+\d+$/)) {
|
||||
const match = tagText.match(/^(.*?)\s+\d+$/);
|
||||
if (match) {
|
||||
tagName = match[1].trim();
|
||||
console.log("Found tag by removing space-number:", tagName);
|
||||
}
|
||||
}
|
||||
// Use the exact text if nothing else matches
|
||||
else {
|
||||
tagName = tagText;
|
||||
console.log("Using original text as tag name:", tagName);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log("Found tag name from data-tag attribute:", tagName);
|
||||
}
|
||||
|
||||
if (!tagName) return;
|
||||
|
|
@ -239,7 +232,7 @@ export default class TagIndexPlugin extends Plugin {
|
|||
};
|
||||
|
||||
// Create a context menu handler function
|
||||
const contextMenuHandler = (event: MouseEvent) => {
|
||||
this.contextMenuHandler = (event: MouseEvent) => {
|
||||
const target = event.target as HTMLElement;
|
||||
if (target && target.closest && target.closest(".tag-pane-tag")) {
|
||||
handleContextMenu(
|
||||
|
|
@ -250,13 +243,13 @@ export default class TagIndexPlugin extends Plugin {
|
|||
};
|
||||
|
||||
// Add event listener with capture phase (true)
|
||||
document.addEventListener("contextmenu", contextMenuHandler, true);
|
||||
document.addEventListener("contextmenu", this.contextMenuHandler, true);
|
||||
|
||||
// Register a function to remove the event listener when the plugin is disabled
|
||||
this.register(() => {
|
||||
document.removeEventListener(
|
||||
"contextmenu",
|
||||
contextMenuHandler,
|
||||
this.contextMenuHandler,
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
|
@ -278,11 +271,6 @@ export default class TagIndexPlugin extends Plugin {
|
|||
await this.saveData(this.settings);
|
||||
}
|
||||
|
||||
onunload() {
|
||||
// Clean up when plugin is disabled
|
||||
this.app.workspace.detachLeavesOfType(TAG_INDEX_VIEW_TYPE);
|
||||
}
|
||||
|
||||
async activateView() {
|
||||
// Check if view is already open
|
||||
const leaves = this.app.workspace.getLeavesOfType(TAG_INDEX_VIEW_TYPE);
|
||||
|
|
|
|||
|
|
@ -260,9 +260,6 @@ export class TagIndexView extends ItemView {
|
|||
? tagName.substring(1)
|
||||
: tagName;
|
||||
|
||||
// Log for debugging
|
||||
console.log("Searching for notes with tag:", tagNameWithoutHash);
|
||||
|
||||
// Find files with the tag by searching the metadata cache
|
||||
const filesWithTag = this.app.vault
|
||||
.getMarkdownFiles()
|
||||
|
|
@ -306,11 +303,6 @@ export class TagIndexView extends ItemView {
|
|||
return false;
|
||||
});
|
||||
|
||||
// Debug log the results
|
||||
console.log(
|
||||
`Found ${filesWithTag.length} files with tag ${tagNameWithoutHash}`,
|
||||
);
|
||||
|
||||
if (filesWithTag.length === 0) {
|
||||
container
|
||||
.createDiv({ cls: "tag-index-empty-note" })
|
||||
|
|
@ -393,33 +385,26 @@ export class TagIndexView extends ItemView {
|
|||
if (cleanTagName.includes('\n')) {
|
||||
const parts = cleanTagName.split('\n');
|
||||
cleanTagName = parts[0].trim();
|
||||
console.log("Tag panel detected linebreak pattern, cleaned to:", cleanTagName);
|
||||
}
|
||||
// Check for the space-number pattern (also common in tag pane)
|
||||
else if (cleanTagName.match(/^(.*?)\s+\d+$/)) {
|
||||
const spaceNumberMatch = cleanTagName.match(/^(.*?)\s+\d+$/);
|
||||
if (spaceNumberMatch) {
|
||||
cleanTagName = spaceNumberMatch[1].trim();
|
||||
console.log("Tag panel detected space-number pattern, cleaned to:", cleanTagName);
|
||||
}
|
||||
}
|
||||
// Check for a numeric suffix that might be a count (least reliable)
|
||||
else if (cleanTagName.match(/^(.+?)(\d+)$/)) {
|
||||
// Only do this processing if we're confident it's from the tag pane
|
||||
// and not just a legitimate tag with numbers
|
||||
console.log("Potential tag with count detected, but leaving as is since we already processed in main.ts");
|
||||
}
|
||||
|
||||
console.log("Adding tag to index:", cleanTagName);
|
||||
console.log("Current addTagsToTop setting:", this.plugin.settings.addTagsToTop);
|
||||
|
||||
// Check if tag already exists
|
||||
if (
|
||||
this.plugin.settings.importantTags.some(
|
||||
(t: ImportantTag) => t.name === cleanTagName,
|
||||
)
|
||||
) {
|
||||
console.log("Tag already exists, not adding:", cleanTagName);
|
||||
// Return false to indicate the tag was not added (duplicate)
|
||||
return false;
|
||||
}
|
||||
|
|
@ -428,8 +413,6 @@ export class TagIndexView extends ItemView {
|
|||
const importantTags = [...this.plugin.settings.importantTags];
|
||||
|
||||
if (this.plugin.settings.addTagsToTop) {
|
||||
console.log("Adding tag to top:", cleanTagName);
|
||||
|
||||
// When adding to top, we need to increment all positions first
|
||||
for (let i = 0; i < importantTags.length; i++) {
|
||||
importantTags[i].position += 1;
|
||||
|
|
@ -444,8 +427,6 @@ export class TagIndexView extends ItemView {
|
|||
// Add to the beginning of the array
|
||||
importantTags.unshift(newTag);
|
||||
} else {
|
||||
console.log("Adding tag to bottom:", cleanTagName);
|
||||
|
||||
// When adding to bottom, position is the length of the current array
|
||||
const newPosition = importantTags.length;
|
||||
const newTag = {
|
||||
|
|
@ -460,9 +441,6 @@ export class TagIndexView extends ItemView {
|
|||
// Update the settings with the modified array
|
||||
this.plugin.settings.importantTags = importantTags;
|
||||
|
||||
// Log current tags after modification
|
||||
console.log("Tags after adding:", JSON.stringify(this.plugin.settings.importantTags));
|
||||
|
||||
// Save the updated settings
|
||||
await this.plugin.saveSettings();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue