// : a dialect's commands, chords, backdrop and task // boxes, driven through the element with synthetic events (the dialect paths // of the engine are proven in packages/tablet/engine/dialect.test.ts). // Whether the backdrop's glyphs really land on the field's is a layout // question happy-dom cannot answer; the headless-Chromium smoke owns it. import { afterEach, expect, test } from "bun:test"; import { defineDialect, type TabletElement } from "../packages/tablet/index"; import type { Dialect } from "../packages/tablet/engine/dialect"; import { caret, selected } from "../packages/tablet/engine/state"; const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); /** "- [ ] text" lines are tasks; Enter on "- " continues; Mod+B wraps in **. */ const tasks: Dialect = { commands: { insertLineBreak: state => { const { from } = selected(state.selection); const start = state.doc.lastIndexOf("\n", from - 1) + 1; if (!state.doc.startsWith("- ", start)) return null; return { change: { from, to: from, insert: "\n- " }, selection: caret(from + 3), group: "other" }; }, bold: state => { const { from, to } = selected(state.selection); return { change: { from, to, insert: `**${state.doc.slice(from, to)}**` }, selection: { anchor: from + 2, head: to + 2 }, group: "other", }; }, }, keymap: { "Mod+B": "bold" }, decorate: line => line.startsWith("- [ ] ") || line.startsWith("- [x] ") ? { kind: "task", segments: [ { text: "- ", classes: ["mark"] }, { text: line.slice(2, 5), classes: ["mark"], box: { checked: line[3] === "x" } }, { text: line.slice(5), classes: [] }, ], } : { kind: "plain", segments: [{ text: line, classes: [] }] }, tick: (state, line) => { const start = state.doc.split("\n").slice(0, line).join("\n").length + (line > 0 ? 1 : 0); const mark = state.doc.slice(start + 3, start + 4); if (!state.doc.startsWith("- [", start) || (mark !== " " && mark !== "x")) return null; return { change: { from: start + 3, to: start + 4, insert: mark === "x" ? " " : "x" }, selection: state.selection, group: "other" }; }, }; defineDialect("tasks", tasks); let named = 0; const mount = (text: string, dialect = "tasks") => { const el = document.createElement("pebble-tablet") as TabletElement; el.setAttribute("dialect", dialect); const field = document.createElement("textarea"); field.name = "body"; field.value = text; el.appendChild(field); document.body.appendChild(el); return { el, field }; }; const focusAt = (field: HTMLTextAreaElement, from: number, to = from) => { field.focus(); field.setSelectionRange(from, to); document.dispatchEvent(new Event("selectionchange")); }; const backdrop = (el: Element) => el.querySelector("[part~=backdrop]"); afterEach(async () => { document.body.textContent = ""; await sleep(0); }); test("a dialect's command runs for its inputType, and the native edit is canceled", () => { const { field } = mount("- milk"); focusAt(field, 6); const enter = new InputEvent("beforeinput", { inputType: "insertLineBreak", bubbles: true, cancelable: true }); field.dispatchEvent(enter); expect(enter.defaultPrevented).toBe(true); expect(field.value).toBe("- milk\n- "); }); test("a dialect's chord runs its command", async () => { const { field } = mount("milk"); focusAt(field, 0, 4); field.dispatchEvent(new KeyboardEvent("keydown", { key: "b", ctrlKey: true, bubbles: true, cancelable: true })); await sleep(0); expect(field.value).toBe("**milk**"); }); test("a decorating dialect draws a backdrop that spells every line", () => { const { el } = mount("- [ ] milk\nbread"); expect(el.hasAttribute("data-decorated")).toBe(true); const drawn = backdrop(el)!; expect(drawn.getAttribute("aria-hidden")).toBe("true"); const lines = [...drawn.querySelectorAll(".line")]; expect(lines.map(line => line.textContent)).toEqual(["- [ ] milk", "bread"]); expect(lines[0].classList.contains("line-task")).toBe(true); expect(lines[0].querySelector("[part~=box]")?.hasAttribute("data-checked")).toBe(false); }); test("the backdrop follows the text as it is edited", () => { const { el, field } = mount("bread"); focusAt(field, 0); field.dispatchEvent(new InputEvent("beforeinput", { inputType: "insertText", data: "- [x] ", bubbles: true, cancelable: true })); const lines = [...backdrop(el)!.querySelectorAll(".line")]; expect(lines.map(line => line.textContent)).toEqual(["- [x] bread"]); expect(lines[0].querySelector("[part~=box]")?.hasAttribute("data-checked")).toBe(true); }); test("clicking a task box ticks its line, keeps focus, and undoes as one step", async () => { const { el, field } = mount("note\n- [ ] milk"); focusAt(field, 0); const box = backdrop(el)!.querySelector("[part~=box]")!; const press = new MouseEvent("mousedown", { bubbles: true, cancelable: true }); box.dispatchEvent(press); expect(press.defaultPrevented).toBe(true); box.dispatchEvent(new MouseEvent("click", { bubbles: true })); expect(field.value).toBe("note\n- [x] milk"); field.dispatchEvent(new KeyboardEvent("keydown", { key: "z", ctrlKey: true, bubbles: true, cancelable: true })); await sleep(0); expect(field.value).toBe("note\n- [ ] milk"); }); test("a readonly field does not tick", () => { const { el, field } = mount("- [ ] milk"); field.readOnly = true; backdrop(el)!.querySelector("[part~=box]")!.dispatchEvent(new MouseEvent("click", { bubbles: true })); expect(field.value).toBe("- [ ] milk"); }); test("a dialect defined after the upgrade applies from then on", () => { const name = `late-${named++}`; const { el, field } = mount("- milk", name); expect(el.hasAttribute("data-decorated")).toBe(false); expect(backdrop(el)).toBeNull(); defineDialect(name, tasks); expect(el.hasAttribute("data-decorated")).toBe(true); expect(backdrop(el)).not.toBeNull(); focusAt(field, 6); field.dispatchEvent(new InputEvent("beforeinput", { inputType: "insertLineBreak", bubbles: true, cancelable: true })); expect(field.value).toBe("- milk\n- "); }); test("without a dialect the tablet stays plain: no backdrop, no dialect commands", () => { const el = document.createElement("pebble-tablet") as TabletElement; const field = document.createElement("textarea"); field.value = "- milk"; el.appendChild(field); document.body.appendChild(el); expect(backdrop(el)).toBeNull(); focusAt(field, 6); field.dispatchEvent(new InputEvent("beforeinput", { inputType: "insertLineBreak", bubbles: true, cancelable: true })); expect(field.value).toBe("- milk\n"); });