edytor 7.0.0 → 7.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -17,7 +17,7 @@
17
17
 
18
18
  Edytor aims to be for Svelte what Slate.js is for React: a heavily customizable editor with an API to build any kind of collaborative rich text editor.
19
19
 
20
- > **7.0.** Edytor `7.0.0` is on npm as `edytor`: its API follows semantic versioning, and every change is listed in the changelog. It has no production track record yet: read the [limitations](https://edytor.dev/docs/reference/limitations) before you build on it. Issues and PRs are welcome: when you report a bug, include the document's JSON value.
20
+ > **7.0.** Edytor `7.0.1` is on npm as `edytor`: its API follows semantic versioning, and every change is listed in the changelog. It has no production track record yet: read the [limitations](https://edytor.dev/docs/reference/limitations) before you build on it. Issues and PRs are welcome: when you report a bug, include the document's JSON value.
21
21
 
22
22
  ## Features
23
23
 
@@ -40,7 +40,7 @@ Edytor aims to be for Svelte what Slate.js is for React: a heavily customizable
40
40
 
41
41
  ## Install
42
42
 
43
- Edytor `7.0.0` is on npm under `latest`:
43
+ Edytor `7.0.1` is on npm under `latest`:
44
44
 
45
45
  ```bash
46
46
  pnpm add edytor
@@ -72,14 +72,12 @@
72
72
  )
73
73
  );
74
74
  /** Session content, rendered from JSON as declared view values: no handle, no caret. */
75
- const ghost = (segment: Segment) => ({
76
- deltas: segmentDeltas(
75
+ const ghost = (segment: Segment) =>
76
+ segmentDeltas(
77
77
  { id, type: type ?? '', data: undefined } as Cell,
78
78
  segment,
79
79
  preview ? undefined : transform
80
- ),
81
- empty: !segment.text
82
- });
80
+ );
83
81
 
84
82
  /** The parts this content renders: a preview's (session parts, no handle), else the cell's. */
85
83
  const own = $derived(preview ? partsOf(preview) : parts);
@@ -137,12 +135,9 @@
137
135
  )}<!--
138
136
  -->{#each parts as part, index (index)}<!--
139
137
  -->{#if part.kind === 'text'}<!--
140
- -->{@const shown =
141
- ghost(part)}<!--
142
138
  --><RenderText
143
139
  text={undefined}
144
- deltas={shown.deltas}
145
- empty={shown.empty}
140
+ deltas={ghost(part)}
146
141
  newline={part.text.endsWith('\n')}
147
142
  /><!--
148
143
  -->{:else if rendersAtom(part)}<!--
@@ -155,19 +150,16 @@
155
150
  -->{/snippet}<!--
156
151
  -->{#key epoch}<RenderText
157
152
  text={firstText}
158
- deltas={firstShown.deltas}
159
- empty={firstShown.empty}
153
+ deltas={firstShown}
154
+ line={!entries.length}
160
155
  newline={first.text.endsWith('\n')}
161
156
  placeholder={preview ? null : placeholder}
162
157
  />{/key}<!--
163
158
  -->{#each entries as entry (entry.key)}<!--
164
159
  -->{#if entry.kind === 'text'}<!--
165
- -->{@const shown =
166
- preview ? ghost(entry.part) : view(entry.part)}<!--
167
160
  --><RenderText
168
161
  text={preview ? undefined : edytor.textAt(id, entry.ordinal)}
169
- deltas={shown.deltas}
170
- empty={shown.empty}
162
+ deltas={preview ? ghost(entry.part) : view(entry.part)}
171
163
  newline={entry.part.text.endsWith('\n')}
172
164
  placeholder={preview ? null : placeholder}
173
165
  /><!--
@@ -180,7 +172,6 @@
180
172
  --><RenderText
181
173
  text={undefined}
182
174
  deltas={entry.deltas}
183
- empty={false}
184
175
  newline={false}
185
176
  rest
186
177
  /><!--
@@ -207,7 +207,7 @@
207
207
  spellcheck = true,
208
208
  autocorrect = 'off',
209
209
  autocomplete = 'off',
210
- autocapitalize = 'none',
210
+ autocapitalize,
211
211
  inputmode,
212
212
  enterkeyhint
213
213
  }: EdytorProps & {
@@ -327,6 +327,8 @@
327
327
 
328
328
  /** The chrome popup open on this view (`edytor.popups`): the root names it while it is. */
329
329
  const popup = $derived(edytor.popups.current);
330
+ /** A block selection: no native highlight, and no virtual keyboard over it. */
331
+ const blocks = $derived(edytor.selection.value.kind === 'blocks');
330
332
 
331
333
  // The display projector: the root `$effect.pre` notes the focused
332
334
  // element before the flush writes the DOM; the root `$effect` — the last
@@ -368,11 +370,13 @@
368
370
  autocorrect,
369
371
  autocomplete,
370
372
  autocapitalize,
371
- inputmode,
373
+ // Selected blocks (a tapped image) open no virtual keyboard; the
374
+ // focus stays, so a hardware keyboard's keys still act on them.
375
+ inputmode: blocks ? 'none' : inputmode,
372
376
  enterkeyhint
373
377
  }}
374
378
  data-edytor
375
- data-edytor-selection={edytor.selection.value.kind === 'blocks' ? 'blocks' : undefined}
379
+ data-edytor-selection={blocks ? 'blocks' : undefined}
376
380
  contenteditable={!readonly}
377
381
  role="textbox"
378
382
  {id}
@@ -443,11 +447,16 @@
443
447
 
444
448
  /*
445
449
  * Room below the last block, where a press puts the caret in a trailing
446
- * paragraph (`nav.trailing.press`): `--edytor-trailing-space`. No
447
- * specificity, so any rule of the app's sets the padding instead.
450
+ * paragraph (`nav.trailing.press`): `--edytor-trailing-space`. A word
451
+ * longer than the line (a pasted URL) breaks anywhere, and counts so in
452
+ * the editor's minimum width: in a flex or grid track the editor never
453
+ * grows past its container (`break-word`, an editing host's default,
454
+ * wraps it too but sizes the track to the whole word). No specificity,
455
+ * so any rule of the app's or a theme's wins.
448
456
  */
449
457
  :global(:where([data-edytor])) {
450
458
  padding-block-end: var(--edytor-trailing-space, 2em);
459
+ overflow-wrap: anywhere;
451
460
  }
452
461
  /*
453
462
  * Long pages, opt-in: the `edytor-long-page` class on the editor or an
@@ -1,29 +1,32 @@
1
1
  <script lang="ts">
2
2
  import { getContext } from 'svelte';
3
3
  import type { Edytor } from '../edytor.svelte.js';
4
- import { rendersMark, type RenderDelta } from '../surface/cells.js';
4
+ import { FILLER, rendersMark, type RenderDelta } from '../surface/cells.js';
5
5
  import { Text } from '../text/text.svelte.js';
6
6
  import Mark, { customAt } from './Mark.svelte';
7
7
  import CustomMark from './CustomMark.svelte';
8
8
 
9
9
  /**
10
- * One segment of a cell: its render deltas, the empty filler, the
11
- * trailing-newline marker and the placeholder attribute (shown
12
- * by the `::before` rule below). `text` is the handle the element maps to
13
- * for the operations and the selection; a segment re-keyed or
14
- * shifted rebinds it without remounting the element.
10
+ * One segment of a cell: its render deltas, the empty filler (`FILLER`;
11
+ * a `<br>` for a text alone on its `line`), the trailing-newline marker
12
+ * and the placeholder attribute (shown by the `::before` rule below).
13
+ * `text` is the handle the element maps to for the operations and the
14
+ * selection; a segment re-keyed or shifted rebinds it without remounting
15
+ * the element. The optional props are read as given (no default: a
16
+ * default costs a prop call per text).
15
17
  */
16
18
  let {
17
19
  text,
18
20
  deltas,
19
- empty,
21
+ line,
20
22
  newline,
21
- placeholder = null,
22
- rest = false
23
+ placeholder,
24
+ rest
23
25
  }: {
24
26
  text: Text | undefined;
25
27
  deltas: readonly RenderDelta[];
26
- empty: boolean;
28
+ /** Nothing follows it in its block's content: empty, it renders a `<br>`. */
29
+ line?: boolean;
27
30
  newline: boolean;
28
31
  placeholder?: string | null;
29
32
  /** The rest a live composition merged into its host, shown after it (`pin.rest`). */
@@ -32,17 +35,15 @@
32
35
 
33
36
  const edytor = getContext<Edytor>('edytor');
34
37
 
35
- const attachText = (node: HTMLElement, initialText: Text | undefined) => {
36
- let attachedText = initialText;
38
+ const attachText = (node: HTMLElement, attached: Text | undefined) => {
37
39
  // A suggestion's ghost text has no handle and attaches nothing.
38
- let attachment = attachedText?.attach(node);
40
+ let attachment = attached?.attach(node);
39
41
 
40
42
  return {
41
- update(nextText: Text | undefined) {
42
- if (!nextText || nextText === attachedText) return;
43
+ update(next: Text | undefined) {
44
+ if (!next || next === attached) return;
43
45
  attachment?.destroy();
44
- attachedText = nextText;
45
- attachment = attachedText.attach(node);
46
+ attachment = (attached = next).attach(node);
46
47
  },
47
48
  destroy() {
48
49
  attachment?.destroy();
@@ -52,19 +53,16 @@
52
53
 
53
54
  const getDeltaKey = (delta: RenderDelta, index: number) =>
54
55
  `${index}:${JSON.stringify(delta.marks)}`;
55
- /**
56
- * An empty text renders the filler as its first unmarked delta: the same
57
- * text node, so the first character typed or composed into an empty
58
- * block lands in the node the browser (and the IME) already holds.
59
- */
60
- const FILLER: readonly RenderDelta[] = [{ text: '\u200B', marks: [] }];
61
56
 
62
57
  /** A delta's marks this view renders (an unknown mark renders its text only). */
63
58
  const rendered = (delta: RenderDelta): RenderDelta => {
64
59
  const known = delta.marks.filter(([name]) => rendersMark(edytor.marks, name));
65
60
  return known.length === delta.marks.length ? delta : { text: delta.text, marks: known };
66
61
  };
67
- const shown = $derived(empty ? FILLER : deltas);
62
+ /** No deltas, or the IME pin's filler (`Pin.shown`): the text holds no character. */
63
+ const empty = $derived(!deltas.length || deltas === FILLER);
64
+ /** Empty, beside an atom or a suggestion: the zero-width filler; alone on its line, the `<br>`. */
65
+ const shown = $derived(deltas.length || line ? deltas : FILLER);
68
66
  /**
69
67
  * The first delta's text when it is unmarked: the element's own first
70
68
  * text node, never re-created while the text starts unmarked (a plain
@@ -87,18 +85,23 @@
87
85
  return items;
88
86
  });
89
87
 
90
- /** A plain click (no drag, no Shift, a single press) places the caret at the pointer. */
88
+ /**
89
+ * A plain click (no drag, no Shift, a single press) places the caret at
90
+ * the pointer. A tap's is the platform's (`sel.press.touch`): iOS puts it
91
+ * at the word's edge, and selects the word at a double tap's second tap,
92
+ * whose click still says `detail` 1.
93
+ */
91
94
  const restoreTextSelectionFromClick = (node: HTMLElement) => {
92
95
  let pointerStart: { clientX: number; clientY: number } | null = null;
93
- const pointerdown = ({ button, shiftKey, clientX, clientY }: PointerEvent) => {
94
- pointerStart = button !== 0 || shiftKey ? null : { clientX, clientY };
96
+ const pointerdown = ({ button, shiftKey, clientX, clientY, pointerType }: PointerEvent) => {
97
+ pointerStart = button || shiftKey || pointerType === 'touch' ? null : { clientX, clientY };
95
98
  };
96
99
  const click = (event: MouseEvent) => {
97
100
  const start = pointerStart;
98
101
  pointerStart = null;
99
- if (event.button !== 0 || event.detail !== 1 || event.shiftKey || !start) return;
100
- const movement = Math.hypot(event.clientX - start.clientX, event.clientY - start.clientY);
101
- if (movement < 4 && text)
102
+ if (event.button || event.detail !== 1 || event.shiftKey || !start || !text) return;
103
+ // Moved less than 4px: no drag.
104
+ if (Math.hypot(event.clientX - start.clientX, event.clientY - start.clientY) < 4)
102
105
  edytor.selection.pointer.placeAt(text, event.clientX, event.clientY);
103
106
  };
104
107
  node.addEventListener('pointerdown', pointerdown);
@@ -118,15 +121,15 @@
118
121
  <span
119
122
  use:attachText={text}
120
123
  use:restoreTextSelectionFromClick
121
- data-edytor-text-empty={empty ? 'true' : 'false'}
124
+ data-edytor-text-empty={String(empty)}
122
125
  data-placeholder={placeholder ?? undefined}
123
126
  data-edytor-composition-rest={rest || undefined}
124
127
  style:white-space="break-spaces"
125
- >{#if head !== null}{head}{/if}<!--
128
+ >{#if head !== null}{head}{:else if !shown.length}<br
129
+ />{/if}<!--
126
130
  -->{#each tail as item (item.key)}<!--
127
131
  -->{#if item.delta === null}<!--
128
132
  --><span
129
- class="newline"
130
133
  data-edytor-trailing-newline>&#8203;</span
131
134
  ><!--
132
135
  -->{:else if customAt(edytor, item.delta, 0)}<!--
@@ -3,7 +3,8 @@ import { Text } from '../text/text.svelte.js';
3
3
  type $$ComponentProps = {
4
4
  text: Text | undefined;
5
5
  deltas: readonly RenderDelta[];
6
- empty: boolean;
6
+ /** Nothing follows it in its block's content: empty, it renders a `<br>`. */
7
+ line?: boolean;
7
8
  newline: boolean;
8
9
  placeholder?: string | null;
9
10
  /** The rest a live composition merged into its host, shown after it (`pin.rest`). */
@@ -368,7 +368,7 @@ export const validRoomId = (id) => id.length > 0 && id.length <= 256 && id !== '
368
368
  export const assertRoomId = (id) => {
369
369
  if (validRoomId(id))
370
370
  return;
371
- throw new TypeError(`room id ${JSON.stringify(id)} cannot be dialed: it must be 1-256 characters, not "." or ".." (a URL collapses those path segments), with no lone surrogate`);
371
+ throw new TypeError(`room id ${JSON.stringify(id)} cannot be dialed: 1-256 characters, not "." or "..", no lone surrogate`);
372
372
  };
373
373
  /** A close code that refuses this client for good: policy (`1008`) or an application code (`4xxx`) but `expired`. */
374
374
  export const isRefusal = (code) => code === CLOSE.refused || (code >= 4000 && code < 5000 && code !== CLOSE.expired);
@@ -735,7 +735,7 @@ export class Edytor {
735
735
  if (!at)
736
736
  return [];
737
737
  const { cell, segment } = at;
738
- return this.pin.shown(cell, segment, this.definitionOf(cell.type).transformText).deltas;
738
+ return this.pin.shown(cell, segment, this.definitionOf(cell.type).transformText);
739
739
  };
740
740
  /** @internal The handle of inline atom `atom`, shown in block `id`. */
741
741
  atomAt = (id, atom) => this.idToBlock.atom(id, atom);
@@ -322,6 +322,7 @@ const runKeyAttempt = async (edytor, attempt) => {
322
322
  // A real `beforeinput` or a newer key may have taken over meanwhile.
323
323
  if (edytor.attempts.key !== attempt)
324
324
  return;
325
+ edytor.attempts.ran = attempt.inputType;
325
326
  await edytor.userInput(() => occur(edytor, { inputType: attempt.inputType, cancelable: false }, attempt, INTENTS[attempt.inputType]?.key ?? null, attempt));
326
327
  };
327
328
  export async function onBeforeInput(event) {
@@ -329,6 +330,12 @@ export async function onBeforeInput(event) {
329
330
  const [key, offered] = keyed(this);
330
331
  const { inputType } = event;
331
332
  const kind = kindOf(inputType);
333
+ // The key the deadline ran, announced late (iOS's software keyboard,
334
+ // `sel.key.late-announcement`): that occurrence again, never run twice.
335
+ const ran = this.attempts.ran;
336
+ this.attempts.ran = null;
337
+ if (ran === inputType)
338
+ return event.preventDefault();
332
339
  if (inputType === 'deleteByDrag' && event.isTrusted) {
333
340
  event.preventDefault();
334
341
  return this.selection.pointer.release();
@@ -73,8 +73,9 @@ const getStructuralFallbackInputType = (edytor, event) => {
73
73
  return !isCollapsed || atBlockEdge || atTextEdge || atMarker ? intent : null;
74
74
  };
75
75
  export function onKeyDown(e) {
76
- // A keydown the bindings are not offered offers no key to its `beforeinput`.
77
- this.keymap.offered = null;
76
+ // A keydown the bindings are not offered offers no key to its `beforeinput`,
77
+ // and a key's late announcement comes before the next key's keydown.
78
+ this.keymap.offered = this.attempts.ran = null;
78
79
  if (e.defaultPrevented) {
79
80
  return;
80
81
  }
@@ -48,16 +48,28 @@
48
48
  if (change.order.size || change.added.size || change.removed.size) structure++;
49
49
  })
50
50
  );
51
+ /**
52
+ * The blocks whose handle stays mounted beyond the near and hovered ones,
53
+ * and shows on a touch screen (no hover there): the selected blocks, the
54
+ * focused handle's, the dragged one. The blocks the selection's text is in
55
+ * show theirs too (`data-active`), mounted once near: a new block paints
56
+ * before its handle mounts (`handles.after-paint`).
57
+ */
58
+ const active = $derived.by(() => {
59
+ const out = new Set<string>();
60
+ for (const block of edytor.selection.selectedBlocks) out.add(block.id);
61
+ if (focused !== null) out.add(focused);
62
+ if (controller.dragging) out.add(controller.dragging);
63
+ return out;
64
+ });
65
+
51
66
  /**
52
67
  * The few blocks that show a handle, sorted into document order: never a
53
68
  * walk of the whole document (a structural commit on a long page).
54
69
  */
55
70
  const ids = $derived.by(() => {
56
71
  void structure;
57
- const shown = new Set([...near, ...hovered]);
58
- for (const block of edytor.selection.selectedBlocks) shown.add(block.id);
59
- if (focused !== null) shown.add(focused);
60
- if (controller.dragging) shown.add(controller.dragging);
72
+ const shown = new Set([...near, ...hovered, ...active]);
61
73
  // Document order by index paths (O(depth) each): no whole-document order is rebuilt.
62
74
  const paths = new Map<string, number[]>();
63
75
  for (const id of shown) {
@@ -161,6 +173,9 @@
161
173
  data-edytor-block-handle-host
162
174
  data-block-id={id}
163
175
  data-visible={hovered.has(id) ? 'true' : undefined}
176
+ data-active={active.has(id) || edytor.selection.projection.blocks.includes(id)
177
+ ? 'true'
178
+ : undefined}
164
179
  data-over-gap={overGaps.has(id) ? 'true' : undefined}
165
180
  data-dragging={controller.dragging && controller.dragging !== id ? 'true' : undefined}
166
181
  data-selecting={edytor.selection.dragging ? 'true' : undefined}
@@ -237,10 +252,22 @@
237
252
  opacity: 1;
238
253
  }
239
254
 
255
+ /* A touch screen has no hover: a handle shows for the block the selection
256
+ * is in (Notion's), never for a touched one (the hover a touch leaves
257
+ * behind), and only a shown one takes a tap, so a tap in the gutter never
258
+ * opens a hidden block's menu. A touch that showed a handle would also
259
+ * cost a tap: iOS Safari takes a touch whose hover reveals content as a
260
+ * hover only, and sends no click. */
240
261
  @media (hover: none) {
241
262
  [data-edytor-block-handle-host] {
242
- opacity: 1;
243
263
  padding-inline-end: 0;
244
264
  }
265
+ [data-edytor-block-handle-host]:not([data-active='true']) {
266
+ opacity: 0;
267
+ pointer-events: none;
268
+ }
269
+ [data-edytor-block-handle-host][data-active='true'] {
270
+ opacity: 1;
271
+ }
245
272
  }
246
273
  </style>
@@ -180,6 +180,12 @@ export declare class Attempts {
180
180
  private closed;
181
181
  /** The last attempt admitted. */
182
182
  last: Attempt | null;
183
+ /**
184
+ * The intent the missing-`beforeinput` deadline ran for the keydown: its
185
+ * `beforeinput` may still come, before the next keydown
186
+ * (`sel.key.late-announcement`).
187
+ */
188
+ ran?: string | null;
183
189
  /** `closed`: an attempt closed (its deadline, a newer one): the observer compares again. */
184
190
  constructor(closed?: () => void);
185
191
  /** Queue `attempt`; a model-owned one supersedes every live attempt, a browser-owned one the browser-owned ones. */
@@ -145,6 +145,12 @@ export class Attempts {
145
145
  #timers = new Map();
146
146
  /** The last attempt admitted. */
147
147
  last = null;
148
+ /**
149
+ * The intent the missing-`beforeinput` deadline ran for the keydown: its
150
+ * `beforeinput` may still come, before the next keydown
151
+ * (`sel.key.late-announcement`).
152
+ */
153
+ ran;
148
154
  /** `closed`: an attempt closed (its deadline, a newer one): the observer compares again. */
149
155
  constructor(closed = () => { }) {
150
156
  this.closed = closed;
@@ -117,9 +117,11 @@ export class Composition {
117
117
  // With no target, the caret the browser parked for the IME.
118
118
  const parked = targetless && dom?.anchorNode;
119
119
  const point = parked ? this.edytor.ports.surface.pointAt(parked, dom.anchorOffset) : null;
120
+ // With no target the value is `none`: nothing to replace.
121
+ const replaced = shown === 'blocks' || shown === 'atom' || blocks;
120
122
  const at = targetless
121
123
  ? point && { text: point.text, from: point.offset, to: point.offset }
122
- : shown === 'blocks' || shown === 'atom' || blocks
124
+ : replaced
123
125
  ? this.#rangeless()
124
126
  : startText && {
125
127
  text: startText,
@@ -127,7 +129,7 @@ export class Composition {
127
129
  to: endText === startText ? yEnd : startText.length
128
130
  };
129
131
  if (at)
130
- this.#pin(at.text, at.from, at.to);
132
+ this.#pin(at.text, at.from, at.to, replaced);
131
133
  this.host = at?.text ?? null;
132
134
  this.#where = this.#place();
133
135
  };
@@ -291,8 +293,8 @@ export class Composition {
291
293
  * and remove the render's anchors between them before the change renders
292
294
  * (Firefox, `ime.range.blocks`). Its start target is replaced now (blocks:
293
295
  * `flow.slot`, one empty block in their place; an atom: removed; a range:
294
- * its blocks merged around the caret), the change rendered and the DOM
295
- * caret parked where the preview goes before the IME writes
296
+ * its blocks merged around the caret) and the change rendered; `#pin`
297
+ * parks the DOM caret where the preview goes before the IME writes
296
298
  * (`projector.park`, the DOM-selection writer). When the replacement is
297
299
  * refused, the IME writes where Escape leaves for (`selectedBlocksExit`;
298
300
  * an atom or a range: before it) and the tail restores it: nothing is
@@ -310,7 +312,6 @@ export class Composition {
310
312
  flushSync();
311
313
  if (!at.text.node)
312
314
  return null;
313
- edytor.ports.surface.park(at.text, at.offset);
314
315
  return { text: at.text, from: at.offset, to: at.offset };
315
316
  }
316
317
  /**
@@ -402,14 +403,25 @@ export class Composition {
402
403
  this.edytor.facade.deleteText(blockId, offset, length);
403
404
  this.#own.clear();
404
405
  }
405
- /** Pin the host's cell and segment: the renderer never rewrites the IME's node (`surface/pin`). */
406
- #pin(host, from, to) {
406
+ /**
407
+ * Pin the host's cell and segment: the renderer never rewrites the IME's
408
+ * node (`surface/pin`). The DOM caret is parked where the preview goes
409
+ * when the start target was `replaced` (`#rangeless`), and in an empty
410
+ * host, which shows its filler text node from now on, not the `<br>` of
411
+ * an empty line: the IME composes into a node the render owns
412
+ * (`ime.empty.filler`).
413
+ */
414
+ #pin(host, from, to, replaced) {
407
415
  const { edytor } = this;
408
416
  const at = edytor.segmentOf(host);
409
417
  if (!at)
410
418
  return;
411
419
  const transform = edytor.definitionOf(at.cell.type).transformText;
412
420
  edytor.pin.acquire(at.cell, at.segment, transform, from, to, host.node ?? null);
421
+ if (!replaced && at.segment.text)
422
+ return;
423
+ flushSync();
424
+ edytor.ports.surface.park(host, from);
413
425
  }
414
426
  /** A mechanical tracked write: the region holds `value`. */
415
427
  #write(value) {
@@ -57,6 +57,13 @@ export type RenderDelta = {
57
57
  /** Mark entries in nesting order (the first run's order when runs merge). */
58
58
  readonly marks: readonly (readonly [string, unknown])[];
59
59
  };
60
+ /**
61
+ * The text node an empty text renders where a `<br>` would break its line
62
+ * (beside an atom or a suggestion), or where the IME composes (`Pin.shown`):
63
+ * a zero-width space. An empty text alone in its block renders a `<br>`, so
64
+ * a phone's keyboard finds nothing before the caret and capitalizes.
65
+ */
66
+ export declare const FILLER: readonly RenderDelta[];
60
67
  /** One delta per maximal stretch of equal marks; empty runs render nothing. */
61
68
  export declare const renderDeltas: (items: readonly {
62
69
  text: string;
@@ -145,6 +145,13 @@ export const rendersMark = (marks, name) => {
145
145
  const mark = marks.get(name);
146
146
  return Boolean(mark?.tag || mark?.snippet);
147
147
  };
148
+ /**
149
+ * The text node an empty text renders where a `<br>` would break its line
150
+ * (beside an atom or a suggestion), or where the IME composes (`Pin.shown`):
151
+ * a zero-width space. An empty text alone in its block renders a `<br>`, so
152
+ * a phone's keyboard finds nothing before the caret and capitalizes.
153
+ */
154
+ export const FILLER = [{ text: '\u200B', marks: [] }];
148
155
  /** Mark payloads compare by canonical value: key-sorted, `undefined` values dropped. */
149
156
  const marksKey = (marks) => marks
150
157
  ? JSON.stringify(Object.entries(marks)
@@ -46,7 +46,7 @@ import { getDomSelectionSnapshot } from '../selection/domSelection.js';
46
46
  import { domOffsetIn } from '../selection/selection.utils.js';
47
47
  import { diverges, heal, ownText, ownedOf } from './attributes.js';
48
48
  import { getTextContentOffsetAtPoint, textLeaves } from '../events/domTextOffset.js';
49
- import { rendersMark } from './cells.js';
49
+ import { FILLER, rendersMark } from './cells.js';
50
50
  /** The empty filler (and the trailing newline's marker). */
51
51
  const ZWSP = '\u200B';
52
52
  const ATOM = '\uFFFC';
@@ -682,11 +682,12 @@ export class SurfaceObserver {
682
682
  };
683
683
  /**
684
684
  * Equal text, compared structurally: each text element (a strict
685
- * container) holds one node per render delta (the empty filler's one) — a
686
- * text node, or a registered mark element whose registered marks follow
687
- * the delta's chain and that renders one text node. Svelte's anchors and
688
- * the newline marker stand for none. Answers whether it differs; `write`:
689
- * a content that does not gets each delta's text back in its node.
685
+ * container) holds one node per render delta — a text node, or a
686
+ * registered mark element whose registered marks follow the delta's
687
+ * chain and that renders one text node — and an empty one its filler's
688
+ * (a `<br>`, or the zero-width text node). Svelte's anchors and the newline
689
+ * marker stand for none. Answers whether it differs; `write`: a content
690
+ * that does not gets each delta's text back in its node.
690
691
  */
691
692
  #shape = (id, write) => {
692
693
  const { edytor } = this;
@@ -696,7 +697,7 @@ export class SurfaceObserver {
696
697
  if (!element)
697
698
  continue;
698
699
  const deltas = edytor.deltasOf(text);
699
- const shown = deltas.length ? deltas : [{ text: ZWSP, marks: [] }];
700
+ const shown = deltas.length ? deltas : FILLER;
700
701
  const nodes = [...element.childNodes].filter((node) => !isAnchor(node) &&
701
702
  !(node instanceof Element && node.hasAttribute('data-edytor-trailing-newline')));
702
703
  if (nodes.length !== shown.length)
@@ -719,6 +720,8 @@ export class SurfaceObserver {
719
720
  return true;
720
721
  leaf = leaves[0];
721
722
  }
723
+ else if (node.nodeName === 'BR' && shown === FILLER)
724
+ continue;
722
725
  else if (node.nodeType !== Node.TEXT_NODE)
723
726
  return true;
724
727
  if (leaf.data !== delta.text)
@@ -939,8 +942,13 @@ export const adopt = async (edytor, text, dom, domCaret, prefer) => {
939
942
  return;
940
943
  // The render patches only the runs the command changed: when the one
941
944
  // insertion rule (`marksForInsertion`) gave the text other marks than the
942
- // node the browser wrote, that node keeps its copy — re-render.
943
- if (!adopted || drifted || (insert && shown !== modelMarks(edytor, text, at)))
945
+ // node the browser wrote, or that node is not the render's (the text was
946
+ // empty, `grow` is all of it: the browser typed into its `<br>`), that
947
+ // node keeps its copy — re-render.
948
+ if (grow === value.length ||
949
+ !adopted ||
950
+ drifted ||
951
+ (insert && shown !== modelMarks(edytor, text, at)))
944
952
  text.refreshFromModel();
945
953
  // Deferred past the re-render (selecting in the same turn left the
946
954
  // native caret at 0), so it checks the gesture serial: a gesture since the
@@ -38,16 +38,13 @@ export declare class Pin {
38
38
  owns: (node: Node | null | undefined) => boolean;
39
39
  /** The segment list `cell` renders: frozen while it hosts the pin. */
40
40
  parts: (cell: Cell) => readonly Part[];
41
- /** The pinned render of segment `key` of `block`, or null when it is not the host. */
42
- render: (block: BlockId, key: string) => {
43
- deltas: RenderDelta[];
44
- empty: boolean;
45
- } | null;
46
- /** The render of `segment` of `cell`: pinned while it hosts the IME, else its kind's (`segmentDeltas`). */
47
- shown: (cell: Cell, segment: Segment, transform: TextTransform | undefined) => {
48
- deltas: RenderDelta[];
49
- empty: boolean;
50
- };
41
+ /**
42
+ * The render of `segment` of `cell`: its kind's (`segmentDeltas`), or the
43
+ * pinned one while it hosts the IME, an empty host's the filler text node
44
+ * (never the `<br>` of an empty line): the IME composes into a node the
45
+ * render owns.
46
+ */
47
+ shown: (cell: Cell, segment: Segment, transform: TextTransform | undefined) => readonly RenderDelta[];
51
48
  /** What follows the pinned segment `key` of `block` in its own element (`merge`). */
52
49
  rest: (block: BlockId, key: string) => RenderDelta[];
53
50
  /**
@@ -1,4 +1,4 @@
1
- import { partsOf, renderDeltas, segmentDeltas } from './cells.js';
1
+ import { FILLER, partsOf, renderDeltas, segmentDeltas } from './cells.js';
2
2
  const textOf = (items) => items.map((item) => item.text).join('');
3
3
  /** The items of `deltas` before and after unit `at`. */
4
4
  const cut = (deltas, at) => {
@@ -25,7 +25,6 @@ export class Pin {
25
25
  key: segment.key,
26
26
  parts: partsOf(cell.runs),
27
27
  frozen,
28
- empty: segment.text === '',
29
28
  head: cut(frozen, from)[0],
30
29
  tail: cut(frozen, Math.max(from, to))[1],
31
30
  merged: [],
@@ -62,21 +61,19 @@ export class Pin {
62
61
  owns = (node) => Boolean(node && this.#held?.element?.contains(node));
63
62
  /** The segment list `cell` renders: frozen while it hosts the pin. */
64
63
  parts = (cell) => this.#held?.block === cell.id ? this.#held.parts : partsOf(cell.runs);
65
- /** The pinned render of segment `key` of `block`, or null when it is not the host. */
66
- render = (block, key) => {
64
+ /**
65
+ * The render of `segment` of `cell`: its kind's (`segmentDeltas`), or the
66
+ * pinned one while it hosts the IME, an empty host's the filler text node
67
+ * (never the `<br>` of an empty line): the IME composes into a node the
68
+ * render owns.
69
+ */
70
+ shown = (cell, segment, transform) => {
67
71
  const held = this.#held;
68
- if (!held || held.block !== block || held.key !== key)
69
- return null;
72
+ if (held?.block !== cell.id || held.key !== segment.key)
73
+ return segmentDeltas(cell, segment, transform);
70
74
  const { preview, head, tail } = held;
71
- if (!preview || preview.native)
72
- return { deltas: [...held.frozen], empty: held.empty };
73
- const items = [...head, preview, ...tail];
74
- return { deltas: renderDeltas(items), empty: !textOf(items) };
75
- };
76
- /** The render of `segment` of `cell`: pinned while it hosts the IME, else its kind's (`segmentDeltas`). */
77
- shown = (cell, segment, transform) => this.render(cell.id, segment.key) ?? {
78
- deltas: segmentDeltas(cell, segment, transform),
79
- empty: segment.text === ''
75
+ const deltas = !preview || preview.native ? held.frozen : renderDeltas([...head, preview, ...tail]);
76
+ return deltas.length ? deltas : FILLER;
80
77
  };
81
78
  /** What follows the pinned segment `key` of `block` in its own element (`merge`). */
82
79
  rest = (block, key) => {
@@ -80,9 +80,12 @@ export declare class Projector {
80
80
  */
81
81
  pressed: () => void;
82
82
  /**
83
- * A press placed a DOM caret the model has not observed yet: its
84
- * `selectionchange` is still queued (`sel.key.before-adoption`; Chromium
85
- * runs input ahead of it on a busy main thread). A key reads the DOM first.
83
+ * The DOM selection moved since the last observation, its
84
+ * `selectionchange` still queued (`sel.key.before-adoption`; Chromium
85
+ * runs input ahead of it on a busy main thread): a press placed a caret,
86
+ * or the browser moved it for a key of its own (an arrow, Shift+arrow:
87
+ * Mod+B right after Shift+→ held down acts on the whole extension). A key
88
+ * reads the DOM first.
86
89
  */
87
90
  unobserved: () => boolean;
88
91
  /**
@@ -218,17 +218,22 @@ export class Projector {
218
218
  #unreported = () => {
219
219
  const { edytor } = this;
220
220
  const { selection } = edytor;
221
- const at = this.#at;
222
- const node = edytor.node;
223
- if (!at || !node || selection.value.kind !== 'text' || edytor.intentSerial !== this.#serial)
221
+ if (selection.value.kind !== 'text' || edytor.intentSerial !== this.#serial)
224
222
  return false;
225
223
  if (selection.request !== this.#request || selection.dragging || edytor.composition.live)
226
224
  return false;
227
- const dom = getDomSelection(node);
228
- if (!dom || !isOwnPoint(node, dom.anchorNode))
229
- return false;
230
- return !(samePoint(at.anchor, dom.anchorNode, dom.anchorOffset) &&
231
- samePoint(at.focus, dom.focusNode, dom.focusOffset));
225
+ return this.#moved();
226
+ };
227
+ /** The DOM selection in the host is no longer where the last observation found it. */
228
+ #moved = () => {
229
+ const at = this.#at;
230
+ const node = this.edytor.node;
231
+ const dom = node && getDomSelection(node);
232
+ return (!!at &&
233
+ !!dom &&
234
+ isOwnPoint(node, dom.anchorNode) &&
235
+ !(samePoint(at.anchor, dom.anchorNode, dom.anchorOffset) &&
236
+ samePoint(at.focus, dom.focusNode, dom.focusOffset)));
232
237
  };
233
238
  /**
234
239
  * Classify a `selectionchange` by comparing the DOM selection with the
@@ -302,11 +307,14 @@ export class Projector {
302
307
  this.#press = this.edytor.intentSerial;
303
308
  };
304
309
  /**
305
- * A press placed a DOM caret the model has not observed yet: its
306
- * `selectionchange` is still queued (`sel.key.before-adoption`; Chromium
307
- * runs input ahead of it on a busy main thread). A key reads the DOM first.
310
+ * The DOM selection moved since the last observation, its
311
+ * `selectionchange` still queued (`sel.key.before-adoption`; Chromium
312
+ * runs input ahead of it on a busy main thread): a press placed a caret,
313
+ * or the browser moved it for a key of its own (an arrow, Shift+arrow:
314
+ * Mod+B right after Shift+→ held down acts on the whole extension). A key
315
+ * reads the DOM first.
308
316
  */
309
- unobserved = () => this.#press > this.#serial;
317
+ unobserved = () => this.#press > this.#serial || this.#moved();
310
318
  /**
311
319
  * Whether the host holds a DOM caret a gesture may have placed: a range
312
320
  * inside it that is not the browser's parked one (`parked`; a key such as
@@ -369,6 +369,13 @@
369
369
  .edytor-notion button[data-edytor-callout-icon][data-empty]:focus-visible::before {
370
370
  opacity: 1;
371
371
  }
372
+ /* A touch screen shows it: a touch that revealed it would cost the tap (iOS
373
+ * Safari takes it for a hover and sends no click). */
374
+ @media (hover: none) {
375
+ .edytor-notion button[data-edytor-callout-icon][data-empty]::before {
376
+ opacity: 1;
377
+ }
378
+ }
372
379
  /* No icon in a readonly view: the text starts at the panel's edge. */
373
380
  .edytor-notion [data-edytor-type='callout']:not(:has(> [data-edytor-callout-icon])) {
374
381
  grid-template-columns: minmax(0, 1fr);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "edytor",
3
3
  "license": "MIT",
4
- "version": "7.0.0",
4
+ "version": "7.0.1",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/beynar/edytor.git"