tobogganSe connecter

library

Prebuilt pebble components: drop-in custom elements, no compiler needed

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

Dernier runAucun
0Étoiles
0Forks
2Issues
160 lignes · 6826 octetsBrut · Historique
1// <pebble-tablet dialect="…">: a dialect's commands, chords, backdrop and task
2// boxes, driven through the element with synthetic events (the dialect paths
3// of the engine are proven in packages/tablet/engine/dialect.test.ts).
4// Whether the backdrop's glyphs really land on the field's is a layout
5// question happy-dom cannot answer; the headless-Chromium smoke owns it.
6import { afterEach, expect, test } from "bun:test";
7import { defineDialect, type TabletElement } from "../packages/tablet/index";
8import type { Dialect } from "../packages/tablet/engine/dialect";
9import { caret, selected } from "../packages/tablet/engine/state";
10
11const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
12
13/** "- [ ] text" lines are tasks; Enter on "- " continues; Mod+B wraps in **. */
14const tasks: Dialect = {
15 commands: {
16 insertLineBreak: state => {
17 const { from } = selected(state.selection);
18 const start = state.doc.lastIndexOf("\n", from - 1) + 1;
19 if (!state.doc.startsWith("- ", start)) return null;
20 return { change: { from, to: from, insert: "\n- " }, selection: caret(from + 3), group: "other" };
21 },
22 bold: state => {
23 const { from, to } = selected(state.selection);
24 return {
25 change: { from, to, insert: `**${state.doc.slice(from, to)}**` },
26 selection: { anchor: from + 2, head: to + 2 },
27 group: "other",
28 };
29 },
30 },
31 keymap: { "Mod+B": "bold" },
32 decorate: line =>
33 line.startsWith("- [ ] ") || line.startsWith("- [x] ")
34 ? {
35 kind: "task",
36 segments: [
37 { text: "- ", classes: ["mark"] },
38 { text: line.slice(2, 5), classes: ["mark"], box: { checked: line[3] === "x" } },
39 { text: line.slice(5), classes: [] },
40 ],
41 }
42 : { kind: "plain", segments: [{ text: line, classes: [] }] },
43 tick: (state, line) => {
44 const start = state.doc.split("\n").slice(0, line).join("\n").length + (line > 0 ? 1 : 0);
45 const mark = state.doc.slice(start + 3, start + 4);
46 if (!state.doc.startsWith("- [", start) || (mark !== " " && mark !== "x")) return null;
47 return { change: { from: start + 3, to: start + 4, insert: mark === "x" ? " " : "x" }, selection: state.selection, group: "other" };
48 },
49};
50
51defineDialect("tasks", tasks);
52
53let named = 0;
54
55const mount = (text: string, dialect = "tasks") => {
56 const el = document.createElement("pebble-tablet") as TabletElement;
57 el.setAttribute("dialect", dialect);
58 const field = document.createElement("textarea");
59 field.name = "body";
60 field.value = text;
61 el.appendChild(field);
62 document.body.appendChild(el);
63 return { el, field };
64};
65
66const focusAt = (field: HTMLTextAreaElement, from: number, to = from) => {
67 field.focus();
68 field.setSelectionRange(from, to);
69 document.dispatchEvent(new Event("selectionchange"));
70};
71
72const backdrop = (el: Element) => el.querySelector<HTMLElement>("[part~=backdrop]");
73
74afterEach(async () => {
75 document.body.textContent = "";
76 await sleep(0);
77});
78
79test("a dialect's command runs for its inputType, and the native edit is canceled", () => {
80 const { field } = mount("- milk");
81 focusAt(field, 6);
82 const enter = new InputEvent("beforeinput", { inputType: "insertLineBreak", bubbles: true, cancelable: true });
83 field.dispatchEvent(enter);
84 expect(enter.defaultPrevented).toBe(true);
85 expect(field.value).toBe("- milk\n- ");
86});
87
88test("a dialect's chord runs its command", async () => {
89 const { field } = mount("milk");
90 focusAt(field, 0, 4);
91 field.dispatchEvent(new KeyboardEvent("keydown", { key: "b", ctrlKey: true, bubbles: true, cancelable: true }));
92 await sleep(0);
93 expect(field.value).toBe("**milk**");
94});
95
96test("a decorating dialect draws a backdrop that spells every line", () => {
97 const { el } = mount("- [ ] milk\nbread");
98 expect(el.hasAttribute("data-decorated")).toBe(true);
99 const drawn = backdrop(el)!;
100 expect(drawn.getAttribute("aria-hidden")).toBe("true");
101 const lines = [...drawn.querySelectorAll<HTMLElement>(".line")];
102 expect(lines.map(line => line.textContent)).toEqual(["- [ ] milk", "bread"]);
103 expect(lines[0].classList.contains("line-task")).toBe(true);
104 expect(lines[0].querySelector("[part~=box]")?.hasAttribute("data-checked")).toBe(false);
105});
106
107test("the backdrop follows the text as it is edited", () => {
108 const { el, field } = mount("bread");
109 focusAt(field, 0);
110 field.dispatchEvent(new InputEvent("beforeinput", { inputType: "insertText", data: "- [x] ", bubbles: true, cancelable: true }));
111 const lines = [...backdrop(el)!.querySelectorAll(".line")];
112 expect(lines.map(line => line.textContent)).toEqual(["- [x] bread"]);
113 expect(lines[0].querySelector("[part~=box]")?.hasAttribute("data-checked")).toBe(true);
114});
115
116test("clicking a task box ticks its line, keeps focus, and undoes as one step", async () => {
117 const { el, field } = mount("note\n- [ ] milk");
118 focusAt(field, 0);
119 const box = backdrop(el)!.querySelector<HTMLElement>("[part~=box]")!;
120 const press = new MouseEvent("mousedown", { bubbles: true, cancelable: true });
121 box.dispatchEvent(press);
122 expect(press.defaultPrevented).toBe(true);
123 box.dispatchEvent(new MouseEvent("click", { bubbles: true }));
124 expect(field.value).toBe("note\n- [x] milk");
125 field.dispatchEvent(new KeyboardEvent("keydown", { key: "z", ctrlKey: true, bubbles: true, cancelable: true }));
126 await sleep(0);
127 expect(field.value).toBe("note\n- [ ] milk");
128});
129
130test("a readonly field does not tick", () => {
131 const { el, field } = mount("- [ ] milk");
132 field.readOnly = true;
133 backdrop(el)!.querySelector<HTMLElement>("[part~=box]")!.dispatchEvent(new MouseEvent("click", { bubbles: true }));
134 expect(field.value).toBe("- [ ] milk");
135});
136
137test("a dialect defined after the upgrade applies from then on", () => {
138 const name = `late-${named++}`;
139 const { el, field } = mount("- milk", name);
140 expect(el.hasAttribute("data-decorated")).toBe(false);
141 expect(backdrop(el)).toBeNull();
142 defineDialect(name, tasks);
143 expect(el.hasAttribute("data-decorated")).toBe(true);
144 expect(backdrop(el)).not.toBeNull();
145 focusAt(field, 6);
146 field.dispatchEvent(new InputEvent("beforeinput", { inputType: "insertLineBreak", bubbles: true, cancelable: true }));
147 expect(field.value).toBe("- milk\n- ");
148});
149
150test("without a dialect the tablet stays plain: no backdrop, no dialect commands", () => {
151 const el = document.createElement("pebble-tablet") as TabletElement;
152 const field = document.createElement("textarea");
153 field.value = "- milk";
154 el.appendChild(field);
155 document.body.appendChild(el);
156 expect(backdrop(el)).toBeNull();
157 focusAt(field, 6);
158 field.dispatchEvent(new InputEvent("beforeinput", { inputType: "insertLineBreak", bubbles: true, cancelable: true }));
159 expect(field.value).toBe("- milk\n");
160});