From 2c05e6b4791883e2c40d285c9e7166fa78abcbc4 Mon Sep 17 00:00:00 2001 From: Tobias Frischmann Date: Mon, 7 Sep 2026 12:54:18 +0200 Subject: [PATCH] continue checkbox lists --- app/composables/useMarkdownEditor.ts | 90 ++++++++++++++++++++-------- flake.nix | 2 +- 2 files changed, 65 insertions(+), 27 deletions(-) diff --git a/app/composables/useMarkdownEditor.ts b/app/composables/useMarkdownEditor.ts index 15a9c9d..509d7b2 100644 --- a/app/composables/useMarkdownEditor.ts +++ b/app/composables/useMarkdownEditor.ts @@ -1,9 +1,56 @@ import type { ModelRef } from "vue"; +type State = { + textarea: HTMLTextAreaElement; + + selectionStart: number; + selectionEnd: number; + text: string; + + currentLine: string; +}; + +function continueList(state: State): string { + function insertAtCursor(insert: string): string { + const new_text = + state.text.slice(0, state.selectionStart) + + insert + + state.text.slice(state.selectionEnd); + + requestAnimationFrame(() => { + state.textarea.selectionStart = state.selectionStart + insert.length; + state.textarea.selectionEnd = state.selectionStart + insert.length; + }); + + return new_text; + } + + // Continue checkbox lists + const checkbox = state.currentLine.match(/^(\s*[-*+])\s+(\[[ xX]\]\s+)/); + if (checkbox) { + return insertAtCursor(`\n${checkbox[1]} [ ] `); + } + + // Continue unordered lists + const bullet = state.currentLine.match(/^(\s*[-*+])\s/); + if (bullet) { + return insertAtCursor(`\n${bullet[1]} `); + } + + // Continue numbered lists + const numbered = state.currentLine.match(/^(\s*)(\d+)\.\s/); + if (numbered) { + const next = Number(numbered[2]) + 1; + const insert = `\n${numbered[1]}${next}. `; + + return insertAtCursor(insert); + } + + return ""; +} + export function useMarkdownEditor(note: ModelRef) { function handleKeyDown(e: KeyboardEvent) { - if (e.key !== "Enter" || e.shiftKey) return; - const textarea = e.target as HTMLTextAreaElement; const start = textarea.selectionStart; const end = textarea.selectionEnd; @@ -12,32 +59,23 @@ export function useMarkdownEditor(note: ModelRef) { const lineStart = value.lastIndexOf("\n", start - 1) + 1; const currentLine = value.slice(lineStart, start); - function insertAtCursor(insert: string) { - e.preventDefault(); - - note.value = value.slice(0, start) + insert + value.slice(end); - - nextTick(() => { - textarea.selectionStart = textarea.selectionEnd = start + insert.length; - }); - } - - // Continue unordered lists - const bullet = currentLine.match(/^(\s*[-*+])\s/); - if (bullet) { - const insert = `\n${bullet[1]} `; - - insertAtCursor(insert); - return; - } + const state: State = { + textarea, + selectionStart: start, + selectionEnd: end, + text: value, + currentLine, + }; - // Continue numbered lists - const numbered = currentLine.match(/^(\s*)(\d+)\.\s/); - if (numbered) { - const next = Number(numbered[2]) + 1; - const insert = `\n${numbered[1]}${next}. `; + if (e.key === "Enter" && !e.shiftKey) { + const new_note = continueList(state); - insertAtCursor(insert); + if (new_note) { + e.preventDefault(); + note.value = new_note; + console.log("set text"); + } + } else if (e.key === "x" && e.ctrlKey) { } } diff --git a/flake.nix b/flake.nix index c6e6948..2d9c80f 100644 --- a/flake.nix +++ b/flake.nix @@ -87,7 +87,7 @@ inherit (finalAttrs) pname version src; inherit pnpm; fetcherVersion = 4; - hash = "sha256-G7iSujAXAhKyulb/RmMGzbqQ0OH3jFXHI8xxfKwAOPs="; + hash = "sha256-1lFD8PJjGFwTWdH0MpadZ+qVWaR7wS66KWrxhS6xe/w="; }; buildPhase = '' -- 2.51.2