import { onMount } from "solid-js"; interface NoteEditorProps { initialContent: string; saving: boolean; onSave: (html: string) => Promise; onCancel: () => void; } function toHtml(raw: string): string { if (/<\/?[a-z]/i.test(raw)) return raw; return raw .replace(/&/g, "&") .replace(//g, ">") .replace(/\n/g, "
"); } export function NoteEditor(props: NoteEditorProps) { let editorRef: HTMLDivElement | undefined; onMount(() => { if (!editorRef) return; editorRef.innerHTML = toHtml(props.initialContent); editorRef.focus(); const sel = window.getSelection(); if (sel) { const range = document.createRange(); range.selectNodeContents(editorRef); range.collapse(false); sel.removeAllRanges(); sel.addRange(range); } }); function exec(cmd: string, value?: string) { document.execCommand(cmd, false, value); editorRef?.focus(); } function handleKeyDown(e: KeyboardEvent) { const mod = e.metaKey || e.ctrlKey; if (mod && e.key === "b") { e.preventDefault(); exec("bold"); return; } if (mod && e.key === "i") { e.preventDefault(); exec("italic"); return; } if (e.key === "Escape") { e.preventDefault(); props.onCancel(); } } return (
note
); }