From 2aa4b331aebdf4d057eab71dc2ebf78864c5aa2d Mon Sep 17 00:00:00 2001 From: quomat Date: Sun, 24 Dec 2023 22:56:30 +0100 Subject: [PATCH] feat: good undo behaviour (only one setCursor call) --- main.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) mode change 100755 => 100644 main.ts diff --git a/main.ts b/main.ts old mode 100755 new mode 100644 index 7a0c113..89dafcd --- a/main.ts +++ b/main.ts @@ -1,4 +1,4 @@ -import { App, Editor, Modal, Notice, Plugin, Setting, TextComponent, sanitizeHTMLToDom } from 'obsidian'; +import { App, Editor, Modal, EditorTransaction, Plugin, Setting, TextComponent, sanitizeHTMLToDom } from 'obsidian'; import { replacements, combiningmarks, subsuperscripts } from './data.ts'; export default class LaTeXtoUnicode extends Plugin { @@ -21,12 +21,11 @@ export default class LaTeXtoUnicode extends Plugin { function insertRange(editor : Editor, text : string) { var pos = editor.getCursor() - editor.setCursor(pos.line,pos.ch+1) // Advance the cursor forwards by 1 + var offset = editor.posToOffset(pos); editor.replaceRange(text, pos); // Replace (cursor will be pushed by the length of the text) - var newPos = editor.getCursor() - editor.setCursor(newPos.line,newPos.ch - 1) // Go back by one, resulting in the position after the replace + editor.setCursor(editor.offsetToPos(offset+text.length)) // Go back by one, resulting in the position after the replace } export class LaTeXToUnicodeModal extends Modal {