Skip to content

Commit

Permalink
release: 0.1.2 (add clean up commands)
Browse files Browse the repository at this point in the history
  • Loading branch information
RyotaUshio committed Aug 19, 2023
1 parent d4747d7 commit f73c55e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
39 changes: 39 additions & 0 deletions src/cleaner.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
23 changes: 22 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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() {
Expand All @@ -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);
}
});
}
}

0 comments on commit f73c55e

Please sign in to comment.