* Inject relationships widget into reading mode view
( leaf: WorkspaceLeaf, plugin: TaskNotesPlugin, context?: ReadingModeInjectionContext )
| 672 | * Inject relationships widget into reading mode view |
| 673 | */ |
| 674 | async function injectReadingModeWidget( |
| 675 | leaf: WorkspaceLeaf, |
| 676 | plugin: TaskNotesPlugin, |
| 677 | context?: ReadingModeInjectionContext |
| 678 | ): Promise<void> { |
| 679 | const view = leaf.view; |
| 680 | if (!(view instanceof MarkdownView) || view.getMode() !== "preview") { |
| 681 | return; |
| 682 | } |
| 683 | |
| 684 | if (shouldSkipMarkdownWidgetLeaf(leaf)) { |
| 685 | return; |
| 686 | } |
| 687 | |
| 688 | const file = view.file; |
| 689 | if (!file) { |
| 690 | return; |
| 691 | } |
| 692 | |
| 693 | // Check if relationships widget is enabled |
| 694 | if (!plugin.settings.showRelationships) { |
| 695 | return; |
| 696 | } |
| 697 | |
| 698 | // Show widget in task notes OR project notes. |
| 699 | // Get the file's frontmatter to check if it's a task or project |
| 700 | let isTaskNote = false; |
| 701 | let isProjectNote = false; |
| 702 | const metadata = plugin.app.metadataCache.getFileCache(file); |
| 703 | if (!metadata?.frontmatter) { |
| 704 | isProjectNote = isProjectNoteForRelationships(plugin, file, metadata); |
| 705 | } else { |
| 706 | // Check if this is a task note |
| 707 | isTaskNote = plugin.cacheManager.isTaskFile(metadata.frontmatter); |
| 708 | isProjectNote = isProjectNoteForRelationships(plugin, file, metadata); |
| 709 | } |
| 710 | |
| 711 | if (!isTaskNote && !isProjectNote) { |
| 712 | // Remove any existing widgets if conditions no longer met |
| 713 | try { |
| 714 | const previewView = view.previewMode; |
| 715 | const containerEl = previewView.containerEl; |
| 716 | containerEl.querySelectorAll(`.${CSS_RELATIONSHIPS_WIDGET}`).forEach((el) => { |
| 717 | const holder = el as HTMLElementWithComponent; |
| 718 | holder.component?.unload(); |
| 719 | el.remove(); |
| 720 | }); |
| 721 | } catch (error) { |
| 722 | tasknotesLogger.debug( |
| 723 | "[TaskNotes] Error cleaning up relationships widget in reading mode:", |
| 724 | { |
| 725 | category: "persistence", |
| 726 | operation: "cleaning-up-relationships-widget-reading-mode", |
| 727 | error: error, |
| 728 | } |
| 729 | ); |
| 730 | } |
| 731 | return; |
no test coverage detected