diff --git a/src/cleaner.ts b/src/cleaner.ts index 9de0474..796aa0d 100644 --- a/src/cleaner.ts +++ b/src/cleaner.ts @@ -2,7 +2,7 @@ import { ChangeSpec } from '@codemirror/state'; import { syntaxTree } from '@codemirror/language'; import { EditorView } from '@codemirror/view'; -import { isInlineMathBegin, isInlineMathEnd, printNode } from './utils'; +import { isInlineMathBegin, isInlineMathEnd } from './utils'; import { Editor } from 'obsidian'; diff --git a/src/latex-suite.ts b/src/latex-suite.ts index b82ac78..03f51ad 100644 --- a/src/latex-suite.ts +++ b/src/latex-suite.ts @@ -48,8 +48,7 @@ export function handleLatexSuiteTabout(state: EditorState, newSelection: EditorS const doc = state.doc.toString(); const newRanges: SelectionRange[] = []; - for (let i = 0; i < newSelection.ranges.length; i++) { - const range = newSelection.ranges[i]; + for (const range of newSelection.ranges) { const indexNextDollar = doc.indexOf("$", range.to); if (indexNextDollar >= 0) { diff --git a/src/main.ts b/src/main.ts index 22f6ec3..b6f9a76 100644 --- a/src/main.ts +++ b/src/main.ts @@ -22,7 +22,7 @@ export default class NoMoreFlicker extends Plugin { * (1. text replacement & 2. cursor position change) that Latex Suite's "box current equation" * command produces or not. See the commend in the makeTransactionFilter() method for details. */ - _latexSuiteBoxing: boolean = false; + _latexSuiteBoxing = false; async onload() { diff --git a/src/settings.ts b/src/settings.ts index acca3cc..219d637 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -4,6 +4,7 @@ import NoMoreFlicker from "./main"; export interface NoMoreFlickerSettings { disableInTable: boolean; + disableOnIME: boolean; disableDecorations: boolean; disableAtomicRanges: boolean; } @@ -11,6 +12,7 @@ export interface NoMoreFlickerSettings { export const DEFAULT_SETTINGS: NoMoreFlickerSettings = { disableInTable: false, + disableOnIME: true, disableDecorations: false, disableAtomicRanges: false, }; @@ -39,6 +41,17 @@ export class NoMoreFlickerSettingTab extends PluginSettingTab { }) }); + new Setting(containerEl) + .setName("Disable when using IME input") + .setDesc("This option can be helpful for avoiding some strange behavior occurring when using IME inputs after escaping from a math block with the Latex Suite plugin's tabout feature.") + .addToggle((toggle) => { + toggle.setValue(this.plugin.settings.disableOnIME) + .onChange(async (disable) => { + this.plugin.settings.disableOnIME = disable; + await this.plugin.saveSettings(); + }) + }); + containerEl.createEl("h4", { text: "Debug mode" }) new Setting(containerEl) .setName("Disable decorations") diff --git a/src/transaction-filter.ts b/src/transaction-filter.ts index 0922faa..501ee01 100644 --- a/src/transaction-filter.ts +++ b/src/transaction-filter.ts @@ -3,6 +3,7 @@ import { syntaxTree } from '@codemirror/language'; import { isInlineMathBegin, isInlineMathEnd } from './utils'; import NoMoreFlicker from 'main'; import { handleLatexSuite } from 'latex-suite'; +import { editorEditorField } from 'obsidian'; export const makeTransactionFilter = (plugin: NoMoreFlicker): Extension => { @@ -12,6 +13,14 @@ export const makeTransactionFilter = (plugin: NoMoreFlicker): Extension => { const userEvent = tr.annotation(Transaction.userEvent)?.split('.')[0]; if (userEvent === 'input') { + if (plugin.settings.disableOnIME) { + // Do nothing when the user is using IME input to avoid troubles that happen + // when using Latex Suite's tabout feature to escape from a math and then typing + // CJK characters + const view = tr.startState.field(editorEditorField); + if (view.composing) return tr; + } + const changes = getChangesForInsertion(tr.startState, tr.changes); return [tr, { changes }]; } else if (userEvent === 'select' && tr.selection) { @@ -75,9 +84,9 @@ export function getChangesForInsertion(state: EditorState, changes: ChangeSet): const doc = state.doc.toString(); const changesToAdd: ChangeSpec[] = []; - const beginningOfChanges: Map = new Map(); + const beginningOfChanges = new Set(); changes.iterChangedRanges((fromA, toA, fromB, toB) => { - beginningOfChanges.set(fromA, true); + beginningOfChanges.add(fromA); }); for (const range of state.selection.ranges) { diff --git a/src/utils.ts b/src/utils.ts index 864aaba..c98d7d0 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -27,7 +27,7 @@ export function isInlineMathEnd(node: SyntaxNodeRef, state: EditorState): boolea export function selectionSatisfies(state: EditorState, predicate: (node: SyntaxNodeRef) => boolean): boolean { let ret = false; const tree = syntaxTree(state); - for (const { from, to } of state.selection.ranges) { + for (const { from } of state.selection.ranges) { const line = state.doc.lineAt(from); tree.iterate({ from: line.from,