onload fix

This commit is contained in:
2025-03-25 00:19:18 +11:00
parent 4f21edecc7
commit e64738d751
2 changed files with 81 additions and 38 deletions

View File

@@ -4,11 +4,23 @@ class TitleAppenderPlugin extends obsidian.Plugin {
async onload() { async onload() {
console.log('Loading TitleAppender plugin'); console.log('Loading TitleAppender plugin');
// Wait for layout to be ready // Initial update attempt
this.updateAllTitles();
// Wait for layout to be ready before registering events
this.app.workspace.onLayoutReady(() => { this.app.workspace.onLayoutReady(() => {
this.registerEvents(); this.registerEvents();
// Force another update after layout is ready
setTimeout(() => {
this.updateAllTitles(); this.updateAllTitles();
}, 500);
}); });
// Schedule periodic updates to catch any missed files
this.registerInterval(
window.setInterval(() => this.updateAllTitles(), 5000)
);
} }
registerEvents() { registerEvents() {
@@ -39,16 +51,8 @@ class TitleAppenderPlugin extends obsidian.Plugin {
// Get all markdown files // Get all markdown files
const files = this.app.vault.getMarkdownFiles(); const files = this.app.vault.getMarkdownFiles();
// First, reset all previously set classes and attributes // First, clean up any existing elements
document.querySelectorAll('.has-sort-value, .has-title').forEach(el => { this.cleanupExistingElements();
el.classList.remove('has-sort-value', 'has-title');
el.removeAttribute('data-sort-value');
el.removeAttribute('data-title');
// Also remove any added elements
const sortSuffix = el.querySelector('.sort-suffix');
if (sortSuffix) sortSuffix.remove();
});
// Apply classes and attributes for each file with frontmatter // Apply classes and attributes for each file with frontmatter
files.forEach(file => { files.forEach(file => {
@@ -60,8 +64,9 @@ class TitleAppenderPlugin extends obsidian.Plugin {
if (title || sortValue) { if (title || sortValue) {
const escapedPath = CSS.escape(file.path); const escapedPath = CSS.escape(file.path);
const fileElement = document.querySelector(`.nav-file-title[data-path="${escapedPath}"] .nav-file-title-content`); const fileElements = document.querySelectorAll(`.nav-file-title[data-path="${escapedPath}"] .nav-file-title-content`);
fileElements.forEach(fileElement => {
if (fileElement) { if (fileElement) {
// Add title if available // Add title if available
if (title) { if (title) {
@@ -86,6 +91,7 @@ class TitleAppenderPlugin extends obsidian.Plugin {
} }
} }
} }
});
} }
} catch (e) { } catch (e) {
console.error('Error processing file:', file?.path, e); console.error('Error processing file:', file?.path, e);
@@ -96,16 +102,23 @@ class TitleAppenderPlugin extends obsidian.Plugin {
} }
} }
onunload() { cleanupExistingElements() {
console.log('Unloading TitleAppender plugin'); // Remove all classes and attributes
// Clean up any added classes when unloading
document.querySelectorAll('.has-sort-value, .has-title').forEach(el => { document.querySelectorAll('.has-sort-value, .has-title').forEach(el => {
el.classList.remove('has-sort-value', 'has-title'); el.classList.remove('has-sort-value', 'has-title');
el.removeAttribute('data-sort-value'); el.removeAttribute('data-sort-value');
el.removeAttribute('data-title'); el.removeAttribute('data-title');
// Remove any added elements
const sortSuffix = el.querySelector('.sort-suffix');
if (sortSuffix) sortSuffix.remove();
}); });
} }
onunload() {
console.log('Unloading TitleAppender plugin');
this.cleanupExistingElements();
}
} }
module.exports = TitleAppenderPlugin; module.exports = TitleAppenderPlugin;

View File

@@ -75,3 +75,33 @@
color: var(--color-yellow); color: var(--color-yellow);
margin-left: 4px; margin-left: 4px;
} }
/* Fix conflicting rules for title and sort display */
.has-title::after {
content: attr(data-title);
color: var(--color-orange);
font-size: 0.85em;
opacity: 0.8;
}
/* Sort value styling when sort value is alone */
.has-sort-value:not(.has-title)::after {
content: attr(data-sort-value);
color: var(--color-yellow);
margin-left: 4px;
}
/* Handle case when both title and sort are present */
.has-title.has-sort-value::after {
content: attr(data-title);
color: var(--color-orange);
font-size: 0.85em;
opacity: 0.8;
}
/* Style for the sort suffix span */
.sort-suffix {
color: var(--color-yellow);
margin-left: 4px;
font-size: 0.85em;
}