| @@ -35,6 +35,17 @@ |
| 35 | 35 | * |
| 36 | 36 | * The engine's read-only view is readable as `element.editor` — { doc, |
| 37 | 37 | * selection } live getters, an escape hatch, not the API. |
| 38 | + * |
| 39 | + * Dialects (engine/dialect.ts): `defineDialect(name, dialect)` registers one |
| 40 | + * and `<pebble-tablet dialect="name">` uses it — its commands and chords, and, |
| 41 | + * when it decorates, a BACKDROP: an aria-hidden layer laid over the field's box, |
| 42 | + * spelling the same text in the same font and wrap, while |
| 43 | + * the field's own glyphs turn transparent. The field stays the surface (caret, |
| 44 | + * selection, IME, a11y); the backdrop only paints, and its task boxes are the |
| 45 | + * only thing in it that takes a click. A dialect defined after the element |
| 46 | + * upgraded (the usual order: importing this module upgrades the page at once) |
| 47 | + * applies from the moment it is defined. The registry is global, so a second |
| 48 | + * bundle carrying this module shares it. |
| 38 | 49 | */ |
| 39 | 50 | import { |
| 40 | 51 | afterRender, |
| @@ -53,12 +64,17 @@ import { |
| 53 | 64 | onCleanup, |
| 54 | 65 | pipe, |
| 55 | 66 | scan, |
| 67 | + Show, |
| 68 | + For, |
| 56 | 69 | subject, |
| 57 | 70 | subscribe, |
| 71 | + switchMap, |
| 58 | 72 | toSignal, |
| 73 | + untrack, |
| 59 | 74 | } from "@pebble/pebble"; |
| 60 | 75 | import { DEV } from "./dev"; |
| 61 | | −import { carriesText, handlesInputType, keymap, startModel, step, type Action, type Model } from "./engine/model"; |
| 76 | +import { plain, type Decorated, type Dialect } from "./engine/dialect"; |
| 77 | +import { carriesText, handlesInputType, keymap, startModel, stepWith, type Action, type Model } from "./engine/model"; |
| 62 | 78 | import { selected, type Selection } from "./engine/state"; |
| 63 | 79 | import styles from "./tablet.css"; |
| 64 | 80 | |
| @@ -84,7 +100,27 @@ function textOf(event: InputEvent): string | null { |
| 84 | 100 | return event.data; |
| 85 | 101 | } |
| 86 | 102 | |
| 87 | | −const Tablet = define("pebble-tablet", { shadow: false, styles }, (_, host) => { |
| 103 | +/** The dialect registry, global so every bundle carrying this module shares it. */ |
| 104 | +const DIALECTS = Symbol.for("pebble.tablet.dialects"); |
| 105 | +/** Fired on window when a dialect is defined; elements waiting for it pick it up. */ |
| 106 | +const DIALECT_DEFINED = "pebble-tablet:dialect"; |
| 107 | + |
| 108 | +function dialects(): Map<string, Dialect> { |
| 109 | + const scope = globalThis as { [DIALECTS]?: Map<string, Dialect> }; |
| 110 | + return (scope[DIALECTS] ??= new Map()); |
| 111 | +} |
| 112 | + |
| 113 | +/** Register a dialect under `name`, for `<pebble-tablet dialect="name">`. */ |
| 114 | +export function defineDialect(name: string, dialect: Dialect): void { |
| 115 | + if (DEV && dialects().has(name)) console.warn(`<pebble-tablet>: dialect "${name}" is defined twice; the later one wins.`); |
| 116 | + dialects().set(name, dialect); |
| 117 | + window.dispatchEvent(new CustomEvent(DIALECT_DEFINED, { detail: { name } })); |
| 118 | +} |
| 119 | + |
| 120 | +/** A line with no dialect decoration: one plain segment. */ |
| 121 | +const undecorated = (line: string): Decorated => ({ kind: "plain", segments: [{ text: line, classes: [] }] }); |
| 122 | + |
| 123 | +const Tablet = define("pebble-tablet", { shadow: false, styles, props: { dialect: String } }, (props, host) => { |
| 88 | 124 | // The server-rendered textarea is the field of record; absent, an empty |
| 89 | 125 | // one takes the role (and the host's name, so the form still has its key). |
| 90 | 126 | const server = host.querySelector("textarea"); |
| @@ -114,6 +150,15 @@ const Tablet = define("pebble-tablet", { shadow: false, styles }, (_, host) => { |
| 114 | 150 | // and the engine agrees — undo/redo are edits too. |
| 115 | 151 | const editable = () => !field.readOnly && !field.disabled; |
| 116 | 152 | |
| 153 | + // The dialect by name, picked up whenever one is defined after the upgrade. |
| 154 | + const defined = toSignal(on(window, DIALECT_DEFINED), null, { equals: () => false }); |
| 155 | + const dialect = computed((): Dialect => { |
| 156 | + defined(); |
| 157 | + const name = props.dialect(); |
| 158 | + return (name !== undefined && dialects().get(name)) || plain; |
| 159 | + }); |
| 160 | + const reduce = computed(() => stepWith(dialect())); |
| 161 | + |
| 117 | 162 | // The event streams, mapped to what they MEAN. beforeinput is canceled |
| 118 | 163 | // exactly when the engine handles it (synchronously, in the listener's |
| 119 | 164 | // task); the cancel and the action hang off the same filtered stream. An |
| @@ -126,7 +171,7 @@ const Tablet = define("pebble-tablet", { shadow: false, styles }, (_, host) => { |
| 126 | 171 | !event.isComposing && |
| 127 | 172 | event.inputType !== "insertCompositionText" && |
| 128 | 173 | editable() && |
| 129 | | − handlesInputType(event.inputType) && |
| 174 | + handlesInputType(event.inputType, untrack(dialect)) && |
| 130 | 175 | (!carriesText(event.inputType) || (textOf(event) ?? "") !== ""), |
| 131 | 176 | ), |
| 132 | 177 | ); |
| @@ -158,6 +203,16 @@ const Tablet = define("pebble-tablet", { shadow: false, styles }, (_, host) => { |
| 158 | 203 | }); |
| 159 | 204 | } |
| 160 | 205 | |
| 206 | + // Task boxes: the only part of the backdrop that takes the pointer. The |
| 207 | + // press must not move focus off the field; the click is the tick. |
| 208 | + const box = (event: Event) => (event.target instanceof Element ? event.target.closest("[part~=box]") : null); |
| 209 | + const lineOf = (event: Event) => Number(box(event)?.closest("[data-line]")?.getAttribute("data-line") ?? -1); |
| 210 | + subscribe(pipe(on(host, "mousedown"), filter(event => box(event) !== null)))(event => event.preventDefault()); |
| 211 | + const boxClicks = pipe( |
| 212 | + on(host, "click"), |
| 213 | + filter(event => box(event) !== null && editable()), |
| 214 | + ); |
| 215 | + |
| 161 | 216 | const actions = merge( |
| 162 | 217 | pipe( |
| 163 | 218 | edits, |
| @@ -178,7 +233,8 @@ const Tablet = define("pebble-tablet", { shadow: false, styles }, (_, host) => { |
| 178 | 233 | map((event): Action => ({ kind: "edit", inputType: "deleteByCut", text: "", selection: at(), at: event.timeStamp })), |
| 179 | 234 | ), |
| 180 | 235 | pipe( |
| 181 | | − keys(host, keymap), |
| 236 | + fromSignal(dialect), |
| 237 | + switchMap(current => keys(host, { ...keymap, ...current.keymap })), |
| 182 | 238 | filter(({ name }) => name === "all" || editable()), |
| 183 | 239 | map(({ name, event }): Action => ({ kind: "key", name, selection: at(), at: event.timeStamp })), |
| 184 | 240 | ), |
| @@ -202,13 +258,25 @@ const Tablet = define("pebble-tablet", { shadow: false, styles }, (_, host) => { |
| 202 | 258 | filter(event => !event.isComposing), |
| 203 | 259 | map((event): Action => ({ kind: "set", doc: field.value, selection: at(), at: event.timeStamp })), |
| 204 | 260 | ), |
| 261 | + // A task box in the backdrop was clicked: the dialect's tick decides. |
| 262 | + pipe( |
| 263 | + boxClicks, |
| 264 | + map((event): Action => ({ kind: "tick", line: lineOf(event), at: event.timeStamp })), |
| 265 | + ), |
| 205 | 266 | // The delayed form-reset read, pushed from the subscribe above. |
| 206 | 267 | poked, |
| 207 | 268 | ); |
| 208 | 269 | |
| 209 | 270 | // The one crossing: actions fold into the model; the model is a signal. |
| 210 | 271 | const initial = startModel(field.value); |
| 211 | | − const model = toSignal(pipe(actions, scan(step, initial)), initial, { equals: (a: Model, b: Model) => a === b }); |
| 272 | + const model = toSignal( |
| 273 | + pipe( |
| 274 | + actions, |
| 275 | + scan((current: Model, action: Action) => untrack(reduce)(current, action), initial), |
| 276 | + ), |
| 277 | + initial, |
| 278 | + { equals: (a: Model, b: Model) => a === b }, |
| 279 | + ); |
| 212 | 280 | const doc = computed(() => model().state.doc); |
| 213 | 281 | |
| 214 | 282 | // doc → field: the form field of record stays current, verbatim, even |
| @@ -246,6 +314,46 @@ const Tablet = define("pebble-tablet", { shadow: false, styles }, (_, host) => { |
| 246 | 314 | emit(host, "save", { value: doc() }); |
| 247 | 315 | }); |
| 248 | 316 | |
| 317 | + // The backdrop: drawn only when the dialect decorates. It spells the same |
| 318 | + // lines as the field, so it must scroll with it. |
| 319 | + const decorate = computed(() => dialect().decorate); |
| 320 | + const rows = computed(() => doc().split("\n")); |
| 321 | + effect(() => host.toggleAttribute("data-decorated", decorate() !== undefined)); |
| 322 | + const follow = () => { |
| 323 | + const backdrop = host.querySelector<HTMLElement>(":scope > [part~=backdrop]"); |
| 324 | + if (backdrop !== null) backdrop.scrollTop = field.scrollTop; |
| 325 | + }; |
| 326 | + subscribe(on(field, "scroll"))(follow); |
| 327 | + effect(() => { |
| 328 | + rows(); |
| 329 | + afterRender(field, follow); |
| 330 | + }); |
| 331 | + |
| 332 | + const line = (text: string, index: () => number) => { |
| 333 | + const decorated = computed(() => (decorate() ?? undecorated)(text)); |
| 334 | + return ( |
| 335 | + <div class={`line line-${decorated().kind}`} data-line={index()}> |
| 336 | + {decorated().segments.map(segment => |
| 337 | + segment.box === undefined ? ( |
| 338 | + <span class={segment.classes.join(" ")}>{segment.text}</span> |
| 339 | + ) : ( |
| 340 | + <span class={segment.classes.join(" ")} part="box" data-checked={segment.box.checked ? "" : undefined}> |
| 341 | + {segment.text} |
| 342 | + </span> |
| 343 | + ), |
| 344 | + )} |
| 345 | + </div> |
| 346 | + ); |
| 347 | + }; |
| 348 | + |
| 349 | + const drawn = ( |
| 350 | + <Show when={decorate()}> |
| 351 | + <div part="backdrop" aria-hidden="true"> |
| 352 | + <For each={rows()}>{line}</For> |
| 353 | + </div> |
| 354 | + </Show> |
| 355 | + ); |
| 356 | + |
| 249 | 357 | views.set(host, { |
| 250 | 358 | get doc() { |
| 251 | 359 | return model().state.doc; |
| @@ -261,7 +369,12 @@ const Tablet = define("pebble-tablet", { shadow: false, styles }, (_, host) => { |
| 261 | 369 | host.removeAttribute("data-enhanced"); |
| 262 | 370 | }); |
| 263 | 371 | |
| 264 | | − return server === null ? field : null; |
| 372 | + return ( |
| 373 | + <> |
| 374 | + {server === null ? field : null} |
| 375 | + {drawn} |
| 376 | + </> |
| 377 | + ); |
| 265 | 378 | }); |
| 266 | 379 | |
| 267 | 380 | /** A <pebble-tablet> element. `editor` is the engine's read-only view (escape hatch). */ |
| @@ -281,3 +394,5 @@ if (!("editor" in Tablet.prototype)) { |
| 281 | 394 | } |
| 282 | 395 | |
| 283 | 396 | export { Tablet }; |
| 397 | +export type { Command, Decorated, Dialect, Segment } from "./engine/dialect"; |
| 398 | +export type { Change, EditorState, Selection, Transaction } from "./engine/state"; |