Skip to content

Commit

Permalink
Add a care for IME to math-in-callout decoration
Browse files Browse the repository at this point in the history
  • Loading branch information
RyotaUshio committed Dec 2, 2023
1 parent 9b96bc7 commit 236c424
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
17 changes: 9 additions & 8 deletions src/decorations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Range, StateField, Transaction, Extension } from '@codemirror/state';
import { syntaxTree } from '@codemirror/language';
import { ensureSyntaxTree, syntaxTree } from '@codemirror/language';
import { EditorView, Decoration, DecorationSet, WidgetType } from '@codemirror/view';
import { editorEditorField, editorLivePreviewField } from 'obsidian';
import { getQuoteInfo, rangesHaveOverlap } from 'utils';
Expand All @@ -15,19 +15,21 @@ import { BuiltInMathWidgetConstructor } from 'patch-widget-type';
* The implementation of this function was deeply inspired by the state field
* defined in the "QF" function in Obsidian's app.js.
*/
export const createCalloutDecorator = (plugin: MathInCalloutPlugin, BuiltInMathWidget: BuiltInMathWidgetConstructor) => StateField.define<DecorationSet>({
export const createCalloutDecorator = (BuiltInMathWidget: BuiltInMathWidgetConstructor) => StateField.define<DecorationSet>({
create() {
return Decoration.none;
},

update(prev: DecorationSet, tr: Transaction): DecorationSet {
const { state } = tr;
const isSourceMode = !state.field(editorLivePreviewField);

const view = state.field(editorEditorField);
const tree = syntaxTree(state);

if (view.composing) return prev.map(tr.changes); // User is using IME

const isSourceMode = !state.field(editorLivePreviewField);
const doc = state.doc;
const ranges = view.hasFocus ? state.selection.ranges : [];
const tree = syntaxTree(state);
const decorations: Range<Decoration>[] = [];

const makeDeco = (decorationSpec: { widget: WidgetType, block?: boolean, inclusiveEnd?: boolean, side?: number }, from: number, to: number) => {
Expand Down Expand Up @@ -117,14 +119,13 @@ export const createCalloutDecorator = (plugin: MathInCalloutPlugin, BuiltInMathW
block: false,
side: 1
}).range(mathEnd, mathEnd)
);
);
}
} else {
decorations.push(
makeDeco({
widget,
block: false,
side: 1
block: false
}, mathBegin, mathEnd).range(mathBegin, mathEnd)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class MathInCalloutPlugin extends Plugin {
setTimeout(() => {
if (notReadyNotice) notReadyNotice.hide();
new Notice(`${this.manifest.name}: You're ready!`, 1500);
this.registerEditorExtension(createCalloutDecorator(this, builtInMathWidget));
this.registerEditorExtension(createCalloutDecorator(builtInMathWidget));
this.rerender()
}, 100);
});
Expand Down

0 comments on commit 236c424

Please sign in to comment.