1import { createSignal, onCleanup, onMount, Show } from "solid-js";2import type { VaultEntry } from "../../shared/types";3import { sendMessage } from "../../shared/bridge";4import { isNativePlatform } from "../../shared/native";5import { NoteRunes } from "../components/NoteRunes";6import { NoteToolbar, type ToolbarExec } from "../components/NoteToolbar";78const HEADER_HEIGHT = 52;9const AUTOSAVE_MS = 800;10const SAVED_DISPLAY_MS = 1500;1112interface NotesEditorProps {13 note: VaultEntry | null; 14 onBack: () => void;15 onSaved: (note: VaultEntry) => void;16 onDelete: (note: VaultEntry) => void;17 autoFocusTitle?: boolean;18}1920export function NotesEditor(props: NotesEditorProps) {21 let titleRef!: HTMLDivElement;22 let bodyRef!: HTMLDivElement;23 let contentScrollRef!: HTMLDivElement;2425 const [noteId, setNoteId] = createSignal<string | undefined>(props.note?.id);26 const [title, setTitle] = createSignal(props.note?.title ?? "");27 const [body, setBody] = createSignal(props.note?.notes ?? "");28 const [saved, setSaved] = createSignal(false);29 const [keyboardVisible, setKeyboardVisible] = createSignal(false);30 const [contentHeight, setContentHeight] = createSignal<number | null>(null);31 const [dirty, setDirty] = createSignal(false);3233 let saveTimer: number | null = null;34 let savedFadeTimer: number | null = null;3536 function scheduleAutosave() {37 setDirty(true);38 if (saveTimer) clearTimeout(saveTimer);39 saveTimer = window.setTimeout(save, AUTOSAVE_MS);40 }4142 async function save() {43 const t = title();44 const b = body();45 46 if (!t.trim() && !b.trim()) return;47 const res = await sendMessage({48 type: "SAVE_NOTE",49 payload: { id: noteId(), title: t || "Untitled", body: b },50 }) as { ok: boolean; data?: VaultEntry };51 if (res?.ok && res.data) {52 setNoteId(res.data.id);53 setDirty(false);54 showSaved();55 props.onSaved(res.data);56 }57 }5859 function showSaved() {60 setSaved(true);61 if (savedFadeTimer) clearTimeout(savedFadeTimer);62 savedFadeTimer = window.setTimeout(() => setSaved(false), SAVED_DISPLAY_MS);63 }6465 function exec(cmd: string, val?: string) {66 document.execCommand(cmd, false, val);67 bodyRef?.focus();68 scheduleAutosave();69 }7071 async function handleBack() {72 73 74 if (dirty() && (title().trim() || body().trim())) {75 if (saveTimer) { clearTimeout(saveTimer); saveTimer = null; }76 await save();77 }78 props.onBack();79 }8081 function handleKeyboardResize() {82 if (!isNativePlatform()) return;83 const vp = window.visualViewport;84 const viewportH = vp?.height ?? window.innerHeight;85 const kbH = window.innerHeight - viewportH;86 if (kbH > 50) {87 setKeyboardVisible(true);88 setContentHeight(viewportH - HEADER_HEIGHT);89 if (document.activeElement) {90 (document.activeElement as HTMLElement).scrollIntoView?.({ behavior: "smooth", block: "nearest" });91 }92 } else {93 setKeyboardVisible(false);94 setContentHeight(null);95 }96 }9798 onMount(() => {99 100 if (titleRef) titleRef.textContent = props.note?.title ?? "";101 if (bodyRef) bodyRef.innerHTML = props.note?.notes ?? "";102103 if (props.autoFocusTitle && titleRef) {104 setTimeout(() => {105 titleRef.focus();106 107 const range = document.createRange();108 const sel = window.getSelection();109 range.selectNodeContents(titleRef);110 range.collapse(false);111 sel?.removeAllRanges();112 sel?.addRange(range);113 }, 300);114 }115116 if (isNativePlatform()) {117 window.addEventListener("resize", handleKeyboardResize);118 }119120 onCleanup(() => {121 if (saveTimer) clearTimeout(saveTimer);122 if (savedFadeTimer) clearTimeout(savedFadeTimer);123 if (isNativePlatform()) window.removeEventListener("resize", handleKeyboardResize);124 });125 });126127 128 let swipeStartX = 0;129 let swipeStartY = 0;130 let swipeCaptured = false;131132 function handleSwipeStart(e: TouchEvent) {133 const t = e.touches[0];134 if (!t) return;135 swipeStartX = t.clientX;136 swipeStartY = t.clientY;137 swipeCaptured = false;138 }139140 function handleSwipeMove(e: TouchEvent) {141 const t = e.touches[0];142 if (!t) return;143 const dx = t.clientX - swipeStartX;144 const dy = Math.abs(t.clientY - swipeStartY);145 if (!swipeCaptured && swipeStartX < 30 && dx > 40 && dy < dx) {146 swipeCaptured = true;147 }148 }149150 function handleSwipeEnd() {151 if (swipeCaptured) {152 handleBack();153 }154 swipeCaptured = false;155 }156157 const execFn: ToolbarExec = exec;158159 return (160 <div161 class="peach-note-editor-surface"162 onTouchStart={handleSwipeStart}163 onTouchMove={handleSwipeMove}164 onTouchEnd={handleSwipeEnd}165 >166 <NoteRunes mode="editor" />167168 {}169 <div class="peach-notes-header">170 <button class="peach-notes-back" onClick={handleBack} aria-label="Back to notes list">‹</button>171 <span class="peach-notes-header-title is-breadcrumb" onClick={handleBack}>notes</span>172 <div class="peach-notes-header-actions">173 <Show when={noteId()}>174 <button175 class="peach-notes-header-btn"176 aria-label="Delete note"177 onClick={() => {178 const id = noteId();179 if (!id) return;180 181 props.onDelete({ id, title: title(), notes: body(), type: "note", createdAt: 0, modifiedAt: Date.now() } as VaultEntry);182 }}183 title="Delete note"184 >185 <svg width="14" height="14" viewBox="0 0 14 14" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round">186 <polyline points="1,3.5 13,3.5" />187 <path d="M5 3.5V2h4v1.5" />188 <rect x="2.5" y="3.5" width="9" height="9" rx="1.5" />189 <line x1="5.5" y1="6.5" x2="5.5" y2="10" />190 <line x1="8.5" y1="6.5" x2="8.5" y2="10" />191 </svg>192 </button>193 </Show>194 </div>195 </div>196197 {}198 <div199 ref={contentScrollRef}200 class="peach-note-editor-scroll"201 style={contentHeight() !== null ? { height: `${contentHeight()}px` } : undefined}202 >203 {}204 <Show when={!isNativePlatform()}>205 <NoteToolbar exec={execFn} />206 </Show>207208 <div209 ref={titleRef}210 class="peach-note-title-field"211 contenteditable="true"212 data-placeholder="Title"213 onInput={() => {214 setTitle(titleRef.textContent ?? "");215 scheduleAutosave();216 }}217 onKeyDown={(e) => {218 if (e.key === "Enter") {219 e.preventDefault();220 bodyRef?.focus();221 }222 }}223 aria-label="Note title"224 />225226 <div class="peach-note-hairline" />227228 <div229 ref={bodyRef}230 class="peach-note-body-field"231 contenteditable="true"232 data-placeholder="Begin writing…"233 onInput={() => {234 setBody(bodyRef.innerHTML ?? "");235 scheduleAutosave();236 }}237 onKeyDown={(e) => {238 const mod = e.metaKey || e.ctrlKey;239 if (mod && e.key === "b") { e.preventDefault(); exec("bold"); }240 if (mod && e.key === "i") { e.preventDefault(); exec("italic"); }241 if (mod && e.key === "u") { e.preventDefault(); exec("underline"); }242 }}243 aria-label="Note body"244 aria-multiline="true"245 />246 </div>247248 {}249 <div class="peach-note-saved-indicator" style={{ opacity: saved() ? "1" : "0" }}>250 saved.251 </div>252253 {}254 <Show when={isNativePlatform()}>255 <NoteToolbar256 exec={execFn}257 keyboardVisible={keyboardVisible()}258 onDismissKeyboard={() => {259 titleRef?.blur();260 bodyRef?.blur();261 }}262 />263 </Show>264 </div>265 );266}