From 13a9f737fcf8a9806c71f4ea53eba3ca3f3d08cd Mon Sep 17 00:00:00 2001 From: Grol Grol Date: Sat, 8 Feb 2025 05:16:38 +0300 Subject: [PATCH] fix cursor, sckroll --- main.ts | 34 +++++++++++++++++++++++----------- manifest.json | 2 +- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/main.ts b/main.ts index 1b3319e..70a146e 100644 --- a/main.ts +++ b/main.ts @@ -8,15 +8,17 @@ export default class CheckboxSyncPlugin extends Plugin { }) ); } + updateParentCheckboxes(editor: Editor) { const lines = editor.getValue().split("\n"); - let hasChanges = false; + const scrollInfo = editor.getScrollInfo(); const cursor = editor.getCursor(); + let updates: { line: number; ch: number; value: string }[] = []; for (let i = lines.length - 1; i >= 0; i--) { - const match = lines[i].match(/^(\s*)- \[(.)\] (.*)$/); + const match = lines[i].match(/^(\s*)- \[(.)\] /); if (!match) continue; - + const indent = match[1].length; const isChecked = match[2] === "x"; let allChildrenChecked = true; @@ -24,7 +26,7 @@ export default class CheckboxSyncPlugin extends Plugin { let j = i + 1; while (j < lines.length) { - const childMatch = lines[j].match(/^(\s*)- \[(.)\] (.*)$/); + const childMatch = lines[j].match(/^(\s*)- \[(.)\] /); if (!childMatch || childMatch[1].length <= indent) break; hasChildren = true; if (childMatch[2] !== "x") allChildrenChecked = false; @@ -32,19 +34,29 @@ export default class CheckboxSyncPlugin extends Plugin { } if (hasChildren) { + const checkboxPos = match[1].length + 3; // позиция "x" или " " в строке if (allChildrenChecked && !isChecked) { - lines[i] = `${match[1]}- [x] ${match[3]}`; - hasChanges = true; + updates.push({ line: i, ch: checkboxPos, value: "x" }); } else if (!allChildrenChecked && isChecked) { - lines[i] = `${match[1]}- [ ] ${match[3]}`; - hasChanges = true; + updates.push({ line: i, ch: checkboxPos, value: " " }); } } } - if (hasChanges) { - editor.replaceRange(lines.join("\n"), { line: 0, ch: 0 }, { line: editor.lastLine(), ch: editor.getLine(editor.lastLine()).length }); - editor.setCursor(cursor); + // Если есть изменения, используем transaction() + if (updates.length > 0) { + // Создаем объект транзакции для применения изменений + const transaction = { + changes: updates.map(({ line, ch, value }) => ({ + from: { line, ch }, + to: { line, ch: ch + 1 }, + text: value, // Используем 'text' вместо 'insert' + })), + }; + editor.blur(); + editor.setCursor(updates[0].line, updates[0].ch) + // Применяем изменения в транзакции + editor.transaction(transaction); } } } diff --git a/manifest.json b/manifest.json index 098c33b..9cad1dc 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "checkbox-sync", "name": "Checkbox Sync", - "version": "1.0.0", + "version": "1.0.1", "minAppVersion": "0.12.0", "description": "Automatically checks the parent checkbox if all child checkboxes are completed, and unchecks it otherwise.", "author": "Grol",