From f73c55ed745b7f9083e5a590e5d690b40b943bca Mon Sep 17 00:00:00 2001 From: RyotaUshio Date: Sat, 19 Aug 2023 23:34:07 +0900 Subject: [PATCH] release: 0.1.2 (add clean up commands) --- manifest.json | 2 +- package-lock.json | 4 ++-- package.json | 2 +- src/cleaner.ts | 39 +++++++++++++++++++++++++++++++++++++++ src/main.ts | 23 ++++++++++++++++++++++- 5 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 src/cleaner.ts diff --git a/manifest.json b/manifest.json index 44b4163..d1c9911 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "inline-math", "name": "No more flickering inline math", - "version": "0.1.1", + "version": "0.1.2", "minAppVersion": "1.3.0", "description": "No longer disturbed by flickering inline math in Obsidian.", "author": "Ryota Ushio", diff --git a/package-lock.json b/package-lock.json index 2563781..2948cbd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "obsidian-inline-math", - "version": "0.1.1", + "version": "0.1.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "obsidian-inline-math", - "version": "0.1.1", + "version": "0.1.2", "license": "MIT", "dependencies": { "@codemirror/language": "^6.9.0", diff --git a/package.json b/package.json index 2a08114..419b5db 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-inline-math", - "version": "0.1.1", + "version": "0.1.2", "description": "No longer disturbed by flickering inline math in Obsidian.", "main": "main.js", "scripts": { diff --git a/src/cleaner.ts b/src/cleaner.ts new file mode 100644 index 0000000..9de0474 --- /dev/null +++ b/src/cleaner.ts @@ -0,0 +1,39 @@ +import { ChangeSpec } from '@codemirror/state'; +import { syntaxTree } from '@codemirror/language'; +import { EditorView } from '@codemirror/view'; + +import { isInlineMathBegin, isInlineMathEnd, printNode } from './utils'; +import { Editor } from 'obsidian'; + + +declare module "obsidian" { + interface Editor { + cm?: EditorView; + } +} + + +export function cleaner(view: EditorView) { + const changes: ChangeSpec[] = []; + syntaxTree(view.state).iterate({ + enter(node) { + if (isInlineMathBegin(node, view.state)) { + if (view.state.sliceDoc(node.to, node.to + 3) == "{} ") { + changes.push({ from: node.to, to: node.to + 3}); + } + } else if (isInlineMathEnd(node, view.state)) { + if (view.state.sliceDoc(node.from - 3, node.from) == " {}") { + changes.push({ from: node.from - 3, to: node.from}); + } + } + } + }); + view.dispatch({ changes }); +} + +export function cleanerCallback(editor: Editor) { + const view = editor.cm; + if (view) { + cleaner(view); + } +} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 37ef1e7..e245785 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,11 +1,12 @@ import { Prec } from '@codemirror/state'; -import { Plugin } from 'obsidian'; +import { MarkdownView, Plugin } from 'obsidian'; import { EditorView } from '@codemirror/view'; import { decorator } from './decoration_and_atomic-range'; import { DEFAULT_SETTINGS, NoMoreFlickerSettingTab, NoMoreFlickerSettings } from './settings'; import { deletionHandler, insertionHandler } from './handlers'; import { is } from './key'; +import { cleanerCallback } from 'cleaner'; export default class NoMoreFlicker extends Plugin { @@ -20,6 +21,18 @@ export default class NoMoreFlicker extends Plugin { this.registerEditorExtension(Prec.highest(EditorView.domEventHandlers({ "keydown": this.onKeydown.bind(this) }))); + + this.addCommand({ + id: "clean", + name: "Clean up brackets in this note", + editorCallback: cleanerCallback, + }); + + this.addCommand({ + id: "clean-all", + name: "Clean up brackets in all the opened notes", + editorCallback: this.cleanAllMarkdownViews.bind(this), + }); } async loadSettings() { @@ -41,4 +54,12 @@ export default class NoMoreFlicker extends Plugin { private isDeletion(event: KeyboardEvent): boolean { return this.settings.deletionKeys.some((key) => is(event, key)); } + + private cleanAllMarkdownViews() { + this.app.workspace.iterateAllLeaves((leaf) => { + if (leaf.view instanceof MarkdownView) { + cleanerCallback(leaf.view.editor); + } + }); + } }