Add fold command structure

This commit is contained in:
Mayuran Visakan 2023-08-16 14:57:24 +01:00
parent 54e05dd710
commit ef972ec7fe
3 changed files with 40 additions and 3 deletions

View file

@ -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.
<!--
> **Warning**
> [!important]
> If you used version 1.1.9 or below, then you must delete the `data.json` file from `VaultFolder/.obsidian/plugins/code-styler/`. This only needs to happen once but is necessary as the file does not contain a few entries which are required by version x.y.z or above. After that, everything should work fine.
-->

View file

@ -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"));

View file

@ -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");