From d0b7aea0b7ce08d8b379c4550b795b8ac68bc06f Mon Sep 17 00:00:00 2001 From: kotaindah55 Date: Tue, 4 Mar 2025 17:11:21 +0200 Subject: [PATCH] build: add some methods to refresh the editor, configure the colors, etc. --- main.ts | 92 +++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 73 insertions(+), 19 deletions(-) diff --git a/main.ts b/main.ts index 183c59b..279c214 100644 --- a/main.ts +++ b/main.ts @@ -1,44 +1,98 @@ -import { Plugin } from "obsidian"; -import { EditorView } from "@codemirror/view"; +import { MarkdownView, Plugin } from "obsidian"; // import { drawSelection } from "@codemirror/view"; -import { parserField } from "src/editor-mode/state-fields"; -import { editorExtendedSyntax } from "src/editor-mode/extensions"; import { PreviewExtendedSyntax } from "src/preview-mode/post-processor"; -import { PluginSettings } from "src/types"; +import { ColorConfig, PluginSettings } from "src/types"; import { DEFAULT_SETTINGS } from "src/settings"; -import { SettingTab } from "src/settings/interface"; -import { settingsFacet } from "src/editor-mode/facets"; -import { configureDelimLookup } from "src/utils"; -import { appFacet } from "src/editor-mode/facets/appFacet"; +import { ExtendedSettingTab } from "src/settings/interface"; +import { pluginFacet, settingsFacet } from "src/editor-mode/facets"; +import { configureDelimLookup, deepCopy, reconfigureDelimLookup } from "src/utils"; +import { appFacet } from "src/editor-mode/facets"; +import { editorExtendedSyntax } from "src/editor-mode/extensions"; +import { refresherAnnot } from "src/editor-mode/annotations"; +import { StyleSheetHandler } from "src/stylesheet-handler"; +import { createColorConfig, createCSSRuleFromColorConfig, getDefaultColorConfigs } from "src/color-management"; +import { Theme } from "src/enums"; export default class ExtendedMarkdownSyntax extends Plugin { settings: PluginSettings; - areSettingsChanged: boolean = false; + colorsHandler: StyleSheetHandler; + opacityHandler: StyleSheetHandler; async onload() { await this.loadSettings(); - this.addSettingTab(new SettingTab(this.app, this)); + this.addSettingTab(new ExtendedSettingTab(this.app, this)); configureDelimLookup(this.settings); this.registerEditorExtension([ appFacet.of(this.app), + pluginFacet.of(this), settingsFacet.of(this.settings), - parserField, - editorExtendedSyntax, - EditorView.outerDecorations.of(view => view.plugin(editorExtendedSyntax)!.outerDecoSet) + editorExtendedSyntax ]); this.registerMarkdownPostProcessor(new PreviewExtendedSyntax(this.settings).postProcess); + this.colorsHandler = new StyleSheetHandler(this); + this.opacityHandler = new StyleSheetHandler(this); + this.buildColorsStyleSheet(); + this.opacityHandler.insert(`body.theme-light{--hl-opacity:${this.settings.lightModeHlOpacity}}`); + this.opacityHandler.insert(`body.theme-dark{--hl-opacity:${this.settings.darkModeHlOpacity}}`); console.log("Load Extended Markdown Syntax"); } async loadSettings() { - this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); + this.settings = Object.assign({}, deepCopy(DEFAULT_SETTINGS), await this.loadData()); } async saveSettings() { - if (!this.areSettingsChanged) { - this.areSettingsChanged = true; - new Notice("You must restart the app to take the effect") - } await this.saveData(this.settings); } onunload(): void { + this.colorsHandler.destroy(); + this.opacityHandler.destroy(); console.log("Unload Extended Markdown Syntax"); } + reconfigureDelimLookup() { + reconfigureDelimLookup(this.settings); + } + /** Get the settings change effect without reload the whole app. */ + refreshMarkdownView() { + this.app.workspace.iterateAllLeaves(leaf => { + let view = leaf.view; + if (view instanceof MarkdownView) { + let cmView = view.editor.cm; + cmView.dispatch({ + annotations: refresherAnnot.of(true), + }); + view.previewMode.rerender(true); + } + }); + } + buildColorsStyleSheet(callback?: (colorConfig: ColorConfig, colorConfigs: ColorConfig[]) => unknown) { + let colorConfigs = this.settings.colorConfigs; + for (let i = 0; i < colorConfigs.length; i++) { + let ruleStr = createCSSRuleFromColorConfig(colorConfigs[i]); + this.colorsHandler.insert(ruleStr); + if (callback) { callback(colorConfigs[i], colorConfigs) } + } + } + setHlOpacity(value: number, theme: Theme) { + let selector = theme == Theme.LIGHT + ? "body.theme-light" + : "body.theme-dark", + property = "--hl-opacity", + // The index of light mode opacity is 0, and dark mode opacity is 1. + ruleIndex = theme; + this.opacityHandler.replace(`${selector}{${property}:${value}}`, ruleIndex); + } + rebuildColorsStyleSheet(callback?: (colorConfig: ColorConfig, colorConfigs: ColorConfig[]) => unknown) { + this.colorsHandler.removeAll(); + this.buildColorsStyleSheet(callback); + } + addNewColor() { + let index = this.settings.colorConfigs.length, + newConfig = createColorConfig("color-" + index, "#ffd000", "Color " + index), + ruleStr = createCSSRuleFromColorConfig(newConfig); + this.settings.colorConfigs.push(newConfig); + this.colorsHandler.insert(ruleStr, index); + } + revertColorConfigs(callback?: (colorConfig: ColorConfig, colorConfigs: ColorConfig[]) => unknown) { + let configs = this.settings.colorConfigs; + configs.splice(0, configs.length, ...getDefaultColorConfigs()); + this.rebuildColorsStyleSheet(callback); + } } \ No newline at end of file