todo fixes

This commit is contained in:
th committed 2026-04-08 15:55:43 +02:00
1 parent fe93a0f80e
commit 940d9bbde4
8 files changed
+135 -47

No files matched your search

+64 -2
View File
@@ -2,7 +2,7 @@
import { $, api, escRe, getCsrf } from './util.js';
import { state, getChildren, CARD_COLORS } from './state.js';
import { renderMd, addCodeCopyButtons } from './markdown.js';
import { renderMd, addCodeCopyButtons, formatTodoItems } from './markdown.js';
import { pushCardEdit, snapshotCard } from './undo.js';
import { fuzzyMatch } from './util.js';
import { esc } from './util.js';
@@ -313,6 +313,16 @@ function updatePreview() {
const title = inpTitle.value.trim();
previewContent.innerHTML = (title ? `<h1>${esc(title)}</h1>` : '') + renderMd(body.value);
addCodeCopyButtons(previewContent);
formatTodoItems(previewContent);
// Interactive checkboxes in editor preview — toggle in textarea
previewContent.querySelectorAll('input[type="checkbox"]').forEach((cb, idx) => {
cb.disabled = false;
cb.addEventListener('change', () => {
let n = 0;
body.value = body.value.replace(/- \[([ x])\]/g, (m, s) => n++ === idx ? `- [${s === 'x' ? ' ' : 'x'}]` : m);
updatePreview();
});
});
}
async function doSave(close, { animate = true } = {}) {
@@ -381,7 +391,32 @@ $('md-toolbar')?.addEventListener('click', e => {
code: () => insertMd('`', '`', 'code'), codeblock: () => insertMd('```\n', '\n```', 'code'),
ul: () => insertMd('- ', '', 'item'), ol: () => insertMd('1. ', '', 'item'),
tree: () => insertMd('```tree\n- ', '\n - child\n', 'parent'),
quote: () => insertMd('> ', '', 'quote'), todo: () => insertMd('- [ ] ', '', 'task'),
quote: () => insertMd('> ', '', 'quote'),
todo: () => {
const s = body.selectionStart, e = body.selectionEnd;
const sel = body.value.substring(s, e);
if (sel && sel.includes('\n') || (sel && /^[\s]*[-*+]\s/m.test(sel))) {
// Multi-line or bullet list: convert each line
const lines = sel.split('\n');
const hasTodos = lines.some(l => /^\s*- \[[ x]\]/.test(l));
const converted = lines.map(l => {
const trimmed = l.trim();
if (!trimmed) return l;
if (hasTodos) {
// Toggle back: remove checkbox
return l.replace(/^(\s*)- \[[ x]\] /, '$1- ');
}
// Add checkbox: strip existing bullet if present
return l.replace(/^(\s*)(?:[-*+]|\d+\.)\s/, '$1').replace(/^(\s*)(.+)$/, '$1- [ ] $2');
}).join('\n');
body.setRangeText(converted, s, e, 'select');
body.selectionStart = s;
body.selectionEnd = s + converted.length;
updatePreview();
} else {
insertMd('- [ ] ', '', 'task');
}
},
link: () => insertMd('[', '](url)', 'text'),
cardlink: () => { insertMd('[[', ']]', ''); showLinkAc(''); },
extract: async () => {
@@ -828,6 +863,33 @@ body.addEventListener('keydown', e => {
return;
}
if (e.key === 'Tab') { e.preventDefault(); insertMd(' '); }
// Enter after todo/bullet/numbered list: continue the prefix
if (e.key === 'Enter' && !e.shiftKey) {
const pos = body.selectionStart;
const before = body.value.substring(0, pos);
const lineStart = before.lastIndexOf('\n') + 1;
const line = before.substring(lineStart);
const m = line.match(/^(\s*)(- \[[ x]\] |[-*+] |\d+\. )/);
if (m) {
e.preventDefault();
const indent = m[1];
let prefix = m[2];
// If current line is empty (just prefix), clear it instead
if (line.trim() === prefix.trim()) {
body.setRangeText('', lineStart, pos, 'end');
body.selectionStart = body.selectionEnd = lineStart;
updatePreview();
return;
}
// Increment number for ordered lists
const numMatch = prefix.match(/^(\d+)\. $/);
if (numMatch) prefix = (parseInt(numMatch[1]) + 1) + '. ';
body.setRangeText('\n' + indent + prefix, pos, pos, 'end');
updatePreview();
if (linesVisible) updateGutter();
return;
}
}
});
body.addEventListener('blur', () => { setTimeout(hideLinkAc, 150); setTimeout(hideTagAc, 150); setTimeout(hideSnippetAc, 150); });