Skip to content

Commit

Permalink
fixed some strange behavior observed when using IME input after escap…
Browse files Browse the repository at this point in the history
…ing from a math block using Latex Suite's tabout feature by adding a new option 'disableOnIME'
  • Loading branch information
RyotaUshio committed Apr 1, 2024
1 parent 8ef7c6e commit 1f2c147
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/cleaner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand Down
3 changes: 1 addition & 2 deletions src/latex-suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down
13 changes: 13 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import NoMoreFlicker from "./main";

export interface NoMoreFlickerSettings {
disableInTable: boolean;
disableOnIME: boolean;
disableDecorations: boolean;
disableAtomicRanges: boolean;
}


export const DEFAULT_SETTINGS: NoMoreFlickerSettings = {
disableInTable: false,
disableOnIME: true,
disableDecorations: false,
disableAtomicRanges: false,
};
Expand Down Expand Up @@ -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")
Expand Down
13 changes: 11 additions & 2 deletions src/transaction-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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) {
Expand Down Expand Up @@ -75,9 +84,9 @@ export function getChangesForInsertion(state: EditorState, changes: ChangeSet):
const doc = state.doc.toString();
const changesToAdd: ChangeSpec[] = [];

const beginningOfChanges: Map<number, boolean> = new Map();
const beginningOfChanges = new Set<number>();
changes.iterChangedRanges((fromA, toA, fromB, toB) => {
beginningOfChanges.set(fromA, true);
beginningOfChanges.add(fromA);
});

for (const range of state.selection.ranges) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1f2c147

Please sign in to comment.