From ef972ec7fe5c495cf473bbeeb5d3aea4abccffcd Mon Sep 17 00:00:00 2001 From: Mayuran Visakan Date: Wed, 16 Aug 2023 14:57:24 +0100 Subject: [PATCH] Add fold command structure --- README.md | 2 +- src/ReadingView.ts | 2 +- src/main.ts | 39 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3c9799e..e72eb3a 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This is a plugin for [Obsidian.md](https://obsidian.md) which lets you style codeblocks and inline code in both editing mode and reading mode. diff --git a/src/ReadingView.ts b/src/ReadingView.ts index eceec58..79f2745 100644 --- a/src/ReadingView.ts +++ b/src/ReadingView.ts @@ -203,7 +203,7 @@ async function getCodeblocksParameters(sourcePath: string, cache: CachedMetadata console.error(`Metadata cache not found for file: ${sourcePath}`); return codeblocksParameters; } -export async function documentFold(fold?: boolean) { +export async function documentFold(contentEl: HTMLElement, fold?: boolean) { const codeblockPreElements = document.querySelectorAll("pre.code-styler-pre"); //todo Change document if (typeof fold === "undefined") //Return all blocks to original state codeblockPreElements.forEach((codeblockPreElement: HTMLElement)=>toggleFold(codeblockPreElement,(codeblockPreElement.getAttribute("defaultFold")??"false")==="true")); diff --git a/src/main.ts b/src/main.ts index 6768961..43ae951 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,7 +4,7 @@ import { DEFAULT_SETTINGS, LANGUAGE_ICONS_DATA, CodeStylerSettings } from "./Set import { SettingsTab } from "./SettingsTab"; import { removeStylesAndClasses, updateStyling } from "./ApplyStyling"; import { createCodeblockCodeMirrorExtensions } from "./EditingView"; -import { destroyReadingModeElements, executeCodeMutationObserver, readingViewCodeblockDecoratingPostProcessor, readingViewInlineDecoratingPostProcessor } from "./ReadingView"; +import { destroyReadingModeElements, documentFold, executeCodeMutationObserver, readingViewCodeblockDecoratingPostProcessor, readingViewInlineDecoratingPostProcessor } from "./ReadingView"; export default class CodeStylerPlugin extends Plugin { settings: CodeStylerSettings; @@ -62,6 +62,43 @@ export default class CodeStylerPlugin extends Plugin { } },this)); + this.addCommand({id: "fold-all", name: "Fold all codeblocks", callback: ()=>{ + const activeView = this.app.workspace.getActiveViewOfType(MarkdownView); + if (activeView) { + if (activeView.getMode() === "preview") + documentFold(activeView.contentEl,true); + else if (activeView.getMode() === "source") + //TODO (@mayurankv) Add fold all editing view command here + ////@ts-expect-error Undocumented Obsidian API + //collapseCommand(activeView.editor.cm.docView.view,true); + console.debug("Command does not exist yet"); + } + }}); + this.addCommand({id: "unfold-all", name: "Unfold all codeblocks", callback: ()=>{ + const activeView = this.app.workspace.getActiveViewOfType(MarkdownView); + if (activeView) { + if (activeView.getMode() === "preview") + documentFold(activeView.contentEl,false); + else if (activeView.getMode() === "source") + //TODO (@mayurankv) Add unfold all editing view command here + ////@ts-expect-error Undocumented Obsidian API + //collapseCommand(activeView.editor.cm.docView.view,true); + console.debug("Command does not exist yet"); + } + }}); + this.addCommand({id: "reset-all", name: "Reset fold state for all codeblocks", callback: ()=>{ + const activeView = this.app.workspace.getActiveViewOfType(MarkdownView); + if (activeView) { + if (activeView.getMode() === "preview") + documentFold(activeView.contentEl); + else if (activeView.getMode() === "source") + //TODO (@mayurankv) Add reset fold state editing view command here + ////@ts-expect-error Undocumented Obsidian API + //collapseCommand(activeView.editor.cm.docView.view,true); + console.debug("Command does not exist yet"); + } + }}); + this.app.workspace.onLayoutReady(()=>{this.rerenderPreview();}); // Add decoration on enabling of plugin console.log("Loaded plugin: Code Styler");