mirror of
https://github.com/groldsf/obsidian_check_plugin.git
synced 2026-07-22 05:37:48 +00:00
fix cursor, sckroll
This commit is contained in:
parent
dcc9fe1b91
commit
13a9f737fc
2 changed files with 24 additions and 12 deletions
34
main.ts
34
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
Loading…
Reference in a new issue