svedit 0.11.0 → 0.12.0

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.
Files changed (59) hide show
  1. package/README.md +230 -64
  2. package/dist/Command.svelte.d.ts +21 -5
  3. package/dist/Command.svelte.d.ts.map +1 -1
  4. package/dist/Command.svelte.js +86 -30
  5. package/dist/KeyMapper.svelte.d.ts.map +1 -1
  6. package/dist/KeyMapper.svelte.js +18 -6
  7. package/dist/Node.svelte +29 -1
  8. package/dist/Node.svelte.d.ts.map +1 -1
  9. package/dist/NodeArrayProperty.svelte +119 -12
  10. package/dist/NodeArrayProperty.svelte.d.ts.map +1 -1
  11. package/dist/NodeGap.svelte +48 -6
  12. package/dist/NodeGap.svelte.d.ts.map +1 -1
  13. package/dist/Session.svelte.d.ts +31 -14
  14. package/dist/Session.svelte.d.ts.map +1 -1
  15. package/dist/Session.svelte.js +155 -77
  16. package/dist/Svedit.svelte +110 -89
  17. package/dist/Svedit.svelte.d.ts.map +1 -1
  18. package/dist/{AnnotatedTextProperty.svelte → TextProperty.svelte} +45 -44
  19. package/dist/TextProperty.svelte.d.ts +7 -0
  20. package/dist/TextProperty.svelte.d.ts.map +1 -0
  21. package/dist/Transaction.svelte.d.ts +78 -40
  22. package/dist/Transaction.svelte.d.ts.map +1 -1
  23. package/dist/Transaction.svelte.js +412 -234
  24. package/dist/doc_utils.d.ts +96 -21
  25. package/dist/doc_utils.d.ts.map +1 -1
  26. package/dist/doc_utils.js +440 -168
  27. package/dist/index.d.ts +1 -1
  28. package/dist/index.js +1 -1
  29. package/dist/node_visibility.svelte.d.ts +30 -4
  30. package/dist/node_visibility.svelte.d.ts.map +1 -1
  31. package/dist/node_visibility.svelte.js +327 -363
  32. package/dist/paste_utils.d.ts +3 -2
  33. package/dist/paste_utils.d.ts.map +1 -1
  34. package/dist/paste_utils.js +16 -13
  35. package/dist/transforms.svelte.d.ts.map +1 -1
  36. package/dist/transforms.svelte.js +5 -6
  37. package/dist/types.d.ts +112 -42
  38. package/dist/utils.d.ts +89 -13
  39. package/dist/utils.d.ts.map +1 -1
  40. package/dist/utils.js +320 -85
  41. package/package.json +4 -1
  42. package/src/lib/Command.svelte.js +86 -30
  43. package/src/lib/KeyMapper.svelte.js +18 -6
  44. package/src/lib/Node.svelte +29 -1
  45. package/src/lib/NodeArrayProperty.svelte +119 -12
  46. package/src/lib/NodeGap.svelte +48 -6
  47. package/src/lib/Session.svelte.js +155 -77
  48. package/src/lib/Svedit.svelte +110 -89
  49. package/src/lib/{AnnotatedTextProperty.svelte → TextProperty.svelte} +45 -44
  50. package/src/lib/Transaction.svelte.js +412 -234
  51. package/src/lib/doc_utils.js +440 -168
  52. package/src/lib/index.js +1 -1
  53. package/src/lib/node_visibility.svelte.js +327 -363
  54. package/src/lib/paste_utils.js +16 -13
  55. package/src/lib/transforms.svelte.js +5 -6
  56. package/src/lib/types.d.ts +112 -42
  57. package/src/lib/utils.js +320 -85
  58. package/dist/AnnotatedTextProperty.svelte.d.ts +0 -7
  59. package/dist/AnnotatedTextProperty.svelte.d.ts.map +0 -1
@@ -1,3 +1,5 @@
1
+ import { is_virtual_keyboard_active } from './utils.js';
2
+
1
3
  const MODIFIER_KEYS = ['meta', 'ctrl', 'alt', 'shift'];
2
4
  const MODIFIER_EVENT_KEYS = {
3
5
  meta: 'metaKey',
@@ -34,7 +36,7 @@ export function define_keymap(keymap) {
34
36
  * Matches a keyboard event against a key combo string.
35
37
  * Example: 'meta+e,ctrl+e' matches either (metaKey && key==='e') OR (ctrlKey && key==='e')
36
38
  */
37
- function matches_key_combo(key_combo, event) {
39
+ function matches_key_combo(key_combo, event, virtual_keyboard_active = false) {
38
40
  const alternatives = key_combo.split(',');
39
41
 
40
42
  return alternatives.some((alternative) => {
@@ -42,14 +44,22 @@ function matches_key_combo(key_combo, event) {
42
44
  const modifiers = parts.filter((part) => MODIFIER_KEYS.includes(part));
43
45
  const non_modifier = parts.find((part) => !MODIFIER_KEYS.includes(part));
44
46
 
47
+ if (virtual_keyboard_active && modifiers.length > 0) {
48
+ return false;
49
+ }
50
+
45
51
  // Check if all specified modifiers are pressed
46
52
  const modifiers_match = modifiers.every((mod) => event[MODIFIER_EVENT_KEYS[mod]]);
47
53
 
48
54
  // Check if no unspecified modifiers are pressed
49
- const no_extra_modifiers = MODIFIER_KEYS.every((mod) => {
50
- if (modifiers.includes(mod)) return true; // This modifier is expected
51
- return !event[MODIFIER_EVENT_KEYS[mod]]; // This modifier should NOT be pressed
52
- });
55
+ // NOTE: On virtual keyboards (iOS / Anroid) modifier keys are just ignored, because
56
+ // they must be assumed accidental (e.g. Shift activated as a result of auto-capitalization)
57
+ const no_extra_modifiers = virtual_keyboard_active
58
+ ? true
59
+ : MODIFIER_KEYS.every((mod) => {
60
+ if (modifiers.includes(mod)) return true; // This modifier is expected
61
+ return !event[MODIFIER_EVENT_KEYS[mod]]; // This modifier should NOT be pressed
62
+ });
53
63
 
54
64
  // Check if the key matches
55
65
  const key_matches = event.key.toLowerCase() === non_modifier;
@@ -65,8 +75,10 @@ function matches_key_combo(key_combo, event) {
65
75
  * but don't crash the application.
66
76
  */
67
77
  function handle_key_map(key_map, event) {
78
+ const virtual_keyboard_active = is_virtual_keyboard_active();
79
+
68
80
  for (const [key_combo, commands] of Object.entries(key_map)) {
69
- if (matches_key_combo(key_combo, event)) {
81
+ if (matches_key_combo(key_combo, event, virtual_keyboard_active)) {
70
82
  // Find the first enabled command and execute it
71
83
  const enabled_command = commands.find((cmd) => cmd.is_enabled());
72
84
  if (enabled_command) {
@@ -16,17 +16,45 @@
16
16
  let child_index = $derived(node_array_meta ? parseInt(String(path.at(-1)), 10) : -1);
17
17
  let is_first = $derived(node_array_meta && child_index === 0);
18
18
  let is_last = $derived(node_array_meta && child_index === node_array_meta.length - 1);
19
+
20
+ // Marks and annotations covering this node become classes automatically:
21
+ // `mark-<type>`/`anno-<type>`, plus `-start`/`-end` variants on the run's
22
+ // first/last covered node. This allows styling ranges (including
23
+ // annotations, which never render wrapper components) with pure CSS. For
24
+ // richer rendering, node components still receive the `mark` and
25
+ // `annotations` props.
26
+ let range_classes = $derived.by(() => {
27
+ if (child_index < 0 || !node_array_meta?.annotations_for) return '';
28
+ const classes = [];
29
+
30
+ const add_classes = (range, prefix) => {
31
+ const range_type = range?.node?.type;
32
+ if (!range_type) return;
33
+ classes.push(`${prefix}-${range_type}`);
34
+ if (range.is_start) classes.push(`${prefix}-${range_type}-start`);
35
+ if (range.is_end) classes.push(`${prefix}-${range_type}-end`);
36
+ };
37
+
38
+ add_classes(node_array_meta.mark_for?.(child_index), 'mark');
39
+ for (const annotation of node_array_meta.annotations_for(child_index)) {
40
+ add_classes(annotation, 'anno');
41
+ }
42
+ return classes.join(' ');
43
+ });
19
44
  </script>
20
45
 
21
46
  <svelte:element
22
47
  this={tag}
23
48
  id={node.id}
24
- class="node-{node.type} {css_class}{is_first ? ' first' : ''}{is_last ? ' last' : ''}"
49
+ class="node-{node.type} {css_class}{range_classes
50
+ ? ` ${range_classes}`
51
+ : ''}{is_first ? ' first' : ''}{is_last ? ' last' : ''}"
25
52
  data-node-id={node.id}
26
53
  data-path={path_str}
27
54
  data-type="node"
28
55
  style="anchor-name: --{path_str};{style}"
29
56
  {...rest}
57
+ {@attach svedit.visibility_registry.track_node(path_str)}
30
58
  >
31
59
  {@render children()}
32
60
  </svelte:element>
@@ -1,11 +1,11 @@
1
1
  <script>
2
2
  import { getContext, setContext } from 'svelte';
3
3
  import UnknownNode from './UnknownNode.svelte';
4
- import { serialize_path } from './utils.js';
4
+ import { serialize_path, calculate_fragment_ranges } from './utils.js';
5
5
  import DefaultNodeGap from './NodeGap.svelte';
6
6
  import DefaultNodeGapMarkers from './NodeGapMarkers.svelte';
7
7
 
8
- /** @import { NodeArrayPropertyProps } from './types.d.ts'; */
8
+ /** @import { NodeArrayPropertyProps, NodeArrayAttachmentContext } from './types.d.ts'; */
9
9
 
10
10
  const svedit = getContext('svedit');
11
11
  let NodeGap = $derived(svedit.session.config.system_components?.node_gap ?? DefaultNodeGap);
@@ -20,10 +20,85 @@
20
20
  // NodeGap's should_position_gap call to avoid N+1 joins per render.
21
21
  let path_str = $derived(serialize_path(path));
22
22
 
23
- let node_ids = $derived(svedit.session.get(path));
24
- let nodes = $derived(node_ids.map((id) => svedit.session.get(id)));
23
+ let raw_value = $derived(svedit.session.get(path));
24
+ let node_ids = $derived(raw_value.nodes);
25
+ let marks = $derived(raw_value.marks);
26
+ let annotations = $derived(raw_value.annotations);
25
27
 
26
- // Mirrors the AnnotatedTextProperty pattern: a `focused` class follows
28
+ let fragments = $derived(get_fragments(node_ids, marks));
29
+
30
+ function get_fragments(node_ids, marks) {
31
+ const ranges = calculate_fragment_ranges(node_ids.length, marks);
32
+ let fragments = [];
33
+
34
+ for (const range of ranges) {
35
+ const nodes_slice = node_ids.slice(range.start_offset, range.end_offset);
36
+
37
+ if (range.type === 'mark') {
38
+ const node = svedit.session.get(range.node_id);
39
+ fragments.push({
40
+ type: 'mark',
41
+ node,
42
+ nodes: nodes_slice,
43
+ start_index: range.start_offset,
44
+ mark_index: range.mark_index
45
+ });
46
+ } else if (range.type === 'content') {
47
+ fragments.push({
48
+ type: 'nodes',
49
+ nodes: nodes_slice,
50
+ start_index: range.start_offset
51
+ });
52
+ }
53
+ }
54
+ return fragments;
55
+ }
56
+
57
+ function get_node_key(node_id, index) {
58
+ let occurrence = 0;
59
+ for (let current_index = 0; current_index < index; current_index++) {
60
+ if (node_ids[current_index] === node_id) occurrence += 1;
61
+ }
62
+ return `${node_id}:${occurrence}`;
63
+ }
64
+
65
+ /**
66
+ * @returns {NodeArrayAttachmentContext}
67
+ */
68
+ function get_range_context(range, index, node_index) {
69
+ const node = svedit.session.get(range.node_id);
70
+ const is_start = node_index === range.start_offset;
71
+ const is_end = node_index === range.end_offset - 1;
72
+
73
+ return {
74
+ ...range,
75
+ index,
76
+ node,
77
+ is_start,
78
+ is_middle: !is_start && !is_end,
79
+ is_end
80
+ };
81
+ }
82
+
83
+ function get_covering_ranges(ranges, node_index) {
84
+ return ranges
85
+ .map((range, index) => ({ range, index }))
86
+ .filter(({ range }) => range.start_offset <= node_index && node_index < range.end_offset)
87
+ .map(({ range, index }) => get_range_context(range, index, node_index));
88
+ }
89
+
90
+ function get_annotation_contexts(node_index) {
91
+ return get_covering_ranges(annotations, node_index);
92
+ }
93
+
94
+ function get_mark(node_index) {
95
+ // The single mark wrapping this node, or null. Mark exclusivity
96
+ // guarantees at most one. Annotations never wrap and are exposed via
97
+ // `annotations` instead.
98
+ return get_covering_ranges(marks, node_index)[0] ?? null;
99
+ }
100
+
101
+ // Mirrors the TextProperty pattern: a `focused` class follows
27
102
  // the model selection. Used by the CSS rule below to hide the empty-array
28
103
  // NodeGap while the caret is in the placeholder, so its <br> isn't a
29
104
  // second arrow-key stop (issue #260).
@@ -35,6 +110,15 @@
35
110
  setContext('node_array_meta', {
36
111
  get length() {
37
112
  return node_ids.length;
113
+ },
114
+ // Lets Node self-serve the marks and annotations covering a child, so
115
+ // node wrappers can carry range classes without every node component
116
+ // having to thread the `mark`/`annotations` props through.
117
+ mark_for(node_index) {
118
+ return get_mark(node_index);
119
+ },
120
+ annotations_for(node_index) {
121
+ return get_annotation_contexts(node_index);
38
122
  }
39
123
  });
40
124
  </script>
@@ -48,6 +132,7 @@
48
132
  data-path={path_str}
49
133
  style="anchor-name: --{path_str};{style ? ` ${style}` : ''}"
50
134
  {...rest}
135
+ {@attach svedit.visibility_registry.track_array(path_str)}
51
136
  >
52
137
  {#if node_ids.length === 0 && svedit.editable}
53
138
  <div
@@ -55,17 +140,39 @@
55
140
  data-path={serialize_path([...path, 0])}
56
141
  data-type="node"
57
142
  style="anchor-name: --{serialize_path([...path, 0])};"
143
+ {@attach svedit.visibility_registry.track_node(serialize_path([...path, 0]))}
58
144
  >
59
145
  <NodeGap array_path={path} offset={0} count={0} empty />
60
146
  </div>
61
147
  {/if}
62
- {#each nodes as node, index (index)}
63
- <NodeGap array_path={path} offset={index} count={nodes.length} />
64
- {@const Component = svedit.session.config.node_components[node.type]}
65
- {#if Component}
66
- <Component path={[...path, index]} />
67
- {:else}
68
- <UnknownNode path={[...path, index]} />
148
+ {#snippet render_nodes(nodes_slice, start_index)}
149
+ {#each nodes_slice as id, slice_index (get_node_key(id, start_index + slice_index))}
150
+ {@const index = start_index + slice_index}
151
+ {@const node = svedit.session.get(id)}
152
+ {@const mark = get_mark(index)}
153
+ {@const annotations = get_annotation_contexts(index)}
154
+ <NodeGap array_path={path} offset={index} count={node_ids.length} />
155
+ {@const Component = svedit.session.config.node_components[node.type]}
156
+ {#if Component}
157
+ <Component path={[...path, index]} {mark} {annotations} />
158
+ {:else}
159
+ <UnknownNode path={[...path, index]} />
160
+ {/if}
161
+ {/each}
162
+ {/snippet}
163
+
164
+ {#each fragments as fragment, fragment_index (fragment_index)}
165
+ {#if fragment.type === 'nodes'}
166
+ {@render render_nodes(fragment.nodes, fragment.start_index)}
167
+ {:else if fragment.type === 'mark'}
168
+ {@const MarkComponent = svedit.session.config.node_components[fragment.node.type]}
169
+ {#if MarkComponent}
170
+ <MarkComponent path={[...path, 'marks', fragment.mark_index, 'node_id']}>
171
+ {@render render_nodes(fragment.nodes, fragment.start_index)}
172
+ </MarkComponent>
173
+ {:else}
174
+ {@render render_nodes(fragment.nodes, fragment.start_index)}
175
+ {/if}
69
176
  {/if}
70
177
  {/each}
71
178
  {#if node_ids.length > 0}
@@ -13,6 +13,7 @@
13
13
  <script>
14
14
  import { serialize_path } from './utils.js';
15
15
  import { getContext } from 'svelte';
16
+ import { should_position_gap } from './node_visibility.svelte.js';
16
17
 
17
18
  /**
18
19
  * ┌─────────────────────────────────────────────────────────────────┐
@@ -37,10 +38,13 @@
37
38
  *
38
39
  * Always present in the DOM when editable (stable structure for
39
40
  * selection anchoring, scrollTo, etc.). Anchor positioning is
40
- * activated lazily via the `.positioned` class, toggled imperatively
41
- * by the IO callback / MO / bootstrap in node_visibility — keeps
42
- * NodeGap allocation-free of per-instance Svelte signals, which
43
- * matters at 1000+ NodeGaps per scroll frame.
41
+ * activated lazily via the `.positioned` class, derived reactively
42
+ * from the visibility registry's array_indices / edge_map. Svelte's
43
+ * reactive collections track reads at per-key granularity, so a
44
+ * registry write only
45
+ * re-evaluates the adjacent gaps. Applying the class declaratively
46
+ * (instead of imperative classList toggles) means it survives DOM
47
+ * recreation without a document change (e.g. dev-mode HMR).
44
48
  *
45
49
  * `position-visibility: anchors-visible` does NOT skip anchor
46
50
  * resolution — the browser resolves all anchor() then hides the
@@ -62,6 +66,22 @@
62
66
  let is_last = $derived(offset === count);
63
67
  let type = $derived(is_first ? 'gap-before' : 'gap-after');
64
68
 
69
+ let path_str = $derived(serialize_path(array_path));
70
+ // Two-derived split is load-bearing: get_array_indices lazily
71
+ // creates the set, and Svelte doesn't track deps on state created
72
+ // inside the reading derived — so acquire here, read in `positioned`.
73
+ let near_indices = $derived(svedit.visibility_registry.get_array_indices(path_str));
74
+ let positioned = $derived(
75
+ is_editable &&
76
+ should_position_gap(
77
+ near_indices,
78
+ svedit.visibility_registry.edge_map.get(path_str),
79
+ offset,
80
+ is_last,
81
+ empty
82
+ )
83
+ );
84
+
65
85
  let gap_style = $derived.by(() => {
66
86
  if (!is_editable) return '';
67
87
  const prev_idx = offset - 1;
@@ -87,8 +107,9 @@
87
107
  class:gap-after={!is_first}
88
108
  class:empty
89
109
  class:last={is_last}
110
+ class:positioned
90
111
  data-type={type}
91
- data-gap-array-path={serialize_path(array_path)}
112
+ data-gap-array-path={path_str}
92
113
  data-gap-offset={offset}
93
114
  style={gap_style}
94
115
  >
@@ -111,9 +132,30 @@
111
132
  }
112
133
 
113
134
  /* ------------------------------------------------------------------ */
114
- /* Un-positioned: minimal in-DOM presence, no anchor resolution */
135
+ /* Un-positioned: no layout box at all */
115
136
  /* ------------------------------------------------------------------ */
116
137
 
138
+ /*
139
+ * display: none is load-bearing for large documents. A zero-size
140
+ * absolutely-positioned selectable still belongs to the containing
141
+ * block's out-of-flow list, and the browser lays out EVERY such box
142
+ * on EVERY layout pass — measured ~430ms per pass at 2000 nodes
143
+ * (~3500 gaps) in Chrome vs ~23ms with the boxes removed. That cost
144
+ * hits every keystroke (the per-change reconcile reads a rect for
145
+ * every node, and selection rendering forces layout), every window
146
+ * resize frame, and every scroll-triggered layout.
147
+ *
148
+ * Off-screen gaps therefore contribute no layout box. DOM structure
149
+ * stays stable (the .node-gap wrapper and selectable elements remain),
150
+ * and DOM Ranges may still point into display:none elements, so
151
+ * programmatic node selections targeting off-screen gaps keep working.
152
+ * Empty-array gaps are excluded: they must stay clickable/visible even
153
+ * before reconcile positions them.
154
+ */
155
+ :global(.node-gap:not(.positioned):not(.empty) .svedit-selectable) {
156
+ display: none;
157
+ }
158
+
117
159
  :global(.node-gap:not(.positioned) .svedit-selectable) {
118
160
  position: absolute;
119
161
  pointer-events: none;