svedit 0.5.0 → 0.6.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.
Files changed (62) hide show
  1. package/README.md +761 -120
  2. package/dist/AnnotatedTextProperty.svelte +129 -76
  3. package/dist/AnnotatedTextProperty.svelte.d.ts.map +1 -1
  4. package/dist/Command.svelte.d.ts +118 -0
  5. package/dist/Command.svelte.d.ts.map +1 -0
  6. package/dist/Command.svelte.js +314 -0
  7. package/dist/CustomProperty.svelte +31 -42
  8. package/dist/CustomProperty.svelte.d.ts.map +1 -1
  9. package/dist/KeyMapper.svelte.d.ts +36 -0
  10. package/dist/KeyMapper.svelte.d.ts.map +1 -0
  11. package/dist/KeyMapper.svelte.js +141 -0
  12. package/dist/Node.svelte +33 -35
  13. package/dist/Node.svelte.d.ts.map +1 -1
  14. package/dist/NodeArrayProperty.svelte +40 -37
  15. package/dist/NodeArrayProperty.svelte.d.ts.map +1 -1
  16. package/dist/{Document.svelte.d.ts → Session.svelte.d.ts} +76 -83
  17. package/dist/Session.svelte.d.ts.map +1 -0
  18. package/dist/Session.svelte.js +594 -0
  19. package/dist/Svedit.svelte +1338 -1512
  20. package/dist/Svedit.svelte.d.ts.map +1 -1
  21. package/dist/Transaction.svelte.d.ts +114 -40
  22. package/dist/Transaction.svelte.d.ts.map +1 -1
  23. package/dist/Transaction.svelte.js +845 -661
  24. package/dist/UnknownNode.svelte +6 -6
  25. package/dist/UnknownNode.svelte.d.ts.map +1 -1
  26. package/dist/doc_utils.d.ts +127 -0
  27. package/dist/doc_utils.d.ts.map +1 -0
  28. package/dist/doc_utils.js +482 -0
  29. package/dist/index.d.ts +7 -3
  30. package/dist/index.js +20 -4
  31. package/dist/{commands.svelte.d.ts → transforms.svelte.d.ts} +1 -2
  32. package/dist/transforms.svelte.d.ts.map +1 -0
  33. package/dist/transforms.svelte.js +155 -0
  34. package/dist/types.d.ts +256 -110
  35. package/dist/{util.d.ts → utils.d.ts} +22 -2
  36. package/dist/utils.d.ts.map +1 -0
  37. package/dist/utils.js +338 -0
  38. package/package.json +20 -18
  39. package/src/lib/AnnotatedTextProperty.svelte +129 -76
  40. package/src/lib/Command.svelte.js +314 -0
  41. package/src/lib/CustomProperty.svelte +31 -42
  42. package/src/lib/KeyMapper.svelte.js +141 -0
  43. package/src/lib/Node.svelte +33 -35
  44. package/src/lib/NodeArrayProperty.svelte +40 -37
  45. package/src/lib/Session.svelte.js +594 -0
  46. package/src/lib/Svedit.svelte +1338 -1512
  47. package/src/lib/Transaction.svelte.js +845 -661
  48. package/src/lib/UnknownNode.svelte +6 -6
  49. package/src/lib/doc_utils.js +482 -0
  50. package/src/lib/index.js +20 -4
  51. package/src/lib/transforms.svelte.js +155 -0
  52. package/src/lib/types.d.ts +256 -110
  53. package/src/lib/utils.js +338 -0
  54. package/dist/Document.svelte.d.ts.map +0 -1
  55. package/dist/Document.svelte.js +0 -833
  56. package/dist/commands.svelte.d.ts.map +0 -1
  57. package/dist/commands.svelte.js +0 -266
  58. package/dist/util.d.ts.map +0 -1
  59. package/dist/util.js +0 -269
  60. package/src/lib/Document.svelte.js +0 -833
  61. package/src/lib/commands.svelte.js +0 -266
  62. package/src/lib/util.js +0 -269
@@ -1,51 +1,40 @@
1
1
  <script>
2
- import { getContext } from 'svelte';
2
+ import { getContext } from 'svelte';
3
3
 
4
- /** @import { CustomPropertyProps } from './types.d.ts'; */
4
+ /** @import { CustomPropertyProps } from './types.d.ts'; */
5
+ const svedit = getContext('svedit');
5
6
 
6
- const svedit = getContext('svedit');
7
-
8
- /** @type {CustomPropertyProps} */
9
- let {
10
- path,
11
- class: css_class,
12
- children,
13
- } = $props();
7
+ /** @type {CustomPropertyProps} */
8
+ let { path, tag = 'div', class: css_class, children } = $props();
14
9
  </script>
15
10
 
16
- {#if svedit.editable}
17
- <!-- NOTE: It's super crucial that there is no extra whitespace, to not introduce additional cursor positions in contenteditable -->
18
- <div
19
- class="property {css_class}"
20
- data-type="property"
21
- data-path={path.join('.')}
22
- style="anchor-name: --{path.join('-')};"
23
- >
24
- <div class="cursor-trap">
25
- <div class="svedit-selectable"><br/></div>
26
- </div>
11
+ <svelte:element
12
+ this={tag}
13
+ class={css_class}
14
+ data-type="property"
15
+ data-path={path.join('.')}
16
+ style="anchor-name: --{path.join('-')};"
17
+ >
18
+ {#if svedit.editable}
19
+ <div class="cursor-trap">
20
+ <div class="svedit-selectable"><br /></div>
21
+ </div>
22
+ {/if}
27
23
 
28
- <div class="property-content" contenteditable="false">{@render children()}</div>
29
- </div>
30
- {:else}
31
- {@render children()}
32
- {/if}
24
+ {@render children()}
25
+ </svelte:element>
33
26
 
34
27
  <style>
35
- .property {
36
- position: relative;
37
- }
38
- .cursor-trap {
39
- position: absolute;
40
- top: 0;
41
- left: 0;
42
- right: 0;
43
- bottom: 0;
44
- z-index: 1;
45
- outline: none;
46
- }
47
- .property-content {
48
- width: 100%;
49
- height: 100%;
50
- }
28
+ [data-type='property'] {
29
+ position: relative;
30
+ }
31
+ .cursor-trap {
32
+ position: absolute;
33
+ top: 0;
34
+ left: 0;
35
+ right: 0;
36
+ bottom: 0;
37
+ z-index: 1;
38
+ outline: none;
39
+ }
51
40
  </style>
@@ -1 +1 @@
1
- {"version":3,"file":"CustomProperty.svelte.d.ts","sourceRoot":"","sources":["../src/lib/CustomProperty.svelte.js"],"names":[],"mappings":";;;;;AAqCA,6GAA6D"}
1
+ {"version":3,"file":"CustomProperty.svelte.d.ts","sourceRoot":"","sources":["../src/lib/CustomProperty.svelte.js"],"names":[],"mappings":";;;;;AA6BA,6GAA6D"}
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Validates and defines a keymap.
3
+ * Throws an error if any key combo is invalid.
4
+ * Valid formats: 'meta+e,ctrl+e' or 'meta+shift+a'
5
+ * Invalid: 'meta+e+a' (only one non-modifier key allowed)
6
+ */
7
+ export function define_keymap(keymap: any): any;
8
+ /**
9
+ * KeyMapper manages keyboard shortcuts using a stack-based scope system.
10
+ *
11
+ * Scopes are tried from top to bottom (most specific to most general).
12
+ * This makes it completely general-purpose - no knowledge of specific contexts.
13
+ *
14
+ * Usage:
15
+ * const mapper = new KeyMapper();
16
+ * mapper.push_scope(app_keymap); // Base layer
17
+ * mapper.push_scope(editor_keymap); // When editor gains focus
18
+ * mapper.pop_scope(); // When editor loses focus
19
+ */
20
+ export class KeyMapper {
21
+ scope_stack: any[];
22
+ skip_onkeydown: boolean;
23
+ /**
24
+ * Push a new scope onto the stack (becomes highest priority)
25
+ */
26
+ push_scope(keymap: any): void;
27
+ /**
28
+ * Pop the most recent scope from the stack
29
+ */
30
+ pop_scope(): any;
31
+ /**
32
+ * Handle keyboard event by trying scopes from top to bottom
33
+ */
34
+ handle_keydown(event: any): void;
35
+ }
36
+ //# sourceMappingURL=KeyMapper.svelte.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KeyMapper.svelte.d.ts","sourceRoot":"","sources":["../src/lib/KeyMapper.svelte.js"],"names":[],"mappings":"AAQA;;;;;GAKG;AACH,gDAgBC;AA6DD;;;;;;;;;;;GAWG;AACH;IAEE,mBAAqB;IACrB,wBAA2B;IAG5B;;OAEG;IACH,8BAGC;IAED;;OAEG;IACH,iBAIC;IAED;;OAEG;IACH,iCAUC;CACD"}
@@ -0,0 +1,141 @@
1
+ const MODIFIER_KEYS = ['meta', 'ctrl', 'alt', 'shift'];
2
+ const MODIFIER_EVENT_KEYS = {
3
+ meta: 'metaKey',
4
+ ctrl: 'ctrlKey',
5
+ alt: 'altKey',
6
+ shift: 'shiftKey'
7
+ };
8
+
9
+ /**
10
+ * Validates and defines a keymap.
11
+ * Throws an error if any key combo is invalid.
12
+ * Valid formats: 'meta+e,ctrl+e' or 'meta+shift+a'
13
+ * Invalid: 'meta+e+a' (only one non-modifier key allowed)
14
+ */
15
+ export function define_keymap(keymap) {
16
+ for (const [key_combo] of Object.entries(keymap)) {
17
+ const alternatives = key_combo.split(',');
18
+
19
+ for (const alternative of alternatives) {
20
+ const parts = alternative.trim().toLowerCase().split('+');
21
+ const non_modifiers = parts.filter((part) => !MODIFIER_KEYS.includes(part));
22
+
23
+ if (non_modifiers.length !== 1) {
24
+ throw new Error(
25
+ `Invalid key combo: "${alternative}". Must have exactly one non-modifier key. Found: ${non_modifiers.length}`
26
+ );
27
+ }
28
+ }
29
+ }
30
+ return keymap;
31
+ }
32
+
33
+ /**
34
+ * Matches a keyboard event against a key combo string.
35
+ * Example: 'meta+e,ctrl+e' matches either (metaKey && key==='e') OR (ctrlKey && key==='e')
36
+ */
37
+ function matches_key_combo(key_combo, event) {
38
+ const alternatives = key_combo.split(',');
39
+
40
+ return alternatives.some((alternative) => {
41
+ const parts = alternative.trim().toLowerCase().split('+');
42
+ const modifiers = parts.filter((part) => MODIFIER_KEYS.includes(part));
43
+ const non_modifier = parts.find((part) => !MODIFIER_KEYS.includes(part));
44
+
45
+ // Check if all specified modifiers are pressed
46
+ const modifiers_match = modifiers.every((mod) => event[MODIFIER_EVENT_KEYS[mod]]);
47
+
48
+ // 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
+ });
53
+
54
+ // Check if the key matches
55
+ const key_matches = event.key.toLowerCase() === non_modifier;
56
+ return modifiers_match && no_extra_modifiers && key_matches;
57
+ });
58
+ }
59
+
60
+ /**
61
+ * Handles a key map by matching keyboard event against registered key combos
62
+ * and executing the first enabled command.
63
+ *
64
+ * Supports both sync and async commands. For async commands, errors are logged
65
+ * but don't crash the application.
66
+ */
67
+ function handle_key_map(key_map, event) {
68
+ for (const [key_combo, commands] of Object.entries(key_map)) {
69
+ if (matches_key_combo(key_combo, event)) {
70
+ // Find the first enabled command and execute it
71
+ const enabled_command = commands.find((cmd) => cmd.is_enabled());
72
+ if (enabled_command) {
73
+ event.preventDefault();
74
+
75
+ // Execute command (may be sync or async)
76
+ const result = enabled_command.execute();
77
+
78
+ // If it's a promise, handle errors (fire-and-forget)
79
+ if (result instanceof Promise) {
80
+ result.catch((err) => {
81
+ console.error('Command execution failed:', err);
82
+ });
83
+ }
84
+
85
+ return true;
86
+ }
87
+ }
88
+ }
89
+ return false;
90
+ }
91
+
92
+ /**
93
+ * KeyMapper manages keyboard shortcuts using a stack-based scope system.
94
+ *
95
+ * Scopes are tried from top to bottom (most specific to most general).
96
+ * This makes it completely general-purpose - no knowledge of specific contexts.
97
+ *
98
+ * Usage:
99
+ * const mapper = new KeyMapper();
100
+ * mapper.push_scope(app_keymap); // Base layer
101
+ * mapper.push_scope(editor_keymap); // When editor gains focus
102
+ * mapper.pop_scope(); // When editor loses focus
103
+ */
104
+ export class KeyMapper {
105
+ constructor() {
106
+ this.scope_stack = [];
107
+ this.skip_onkeydown = false;
108
+ }
109
+
110
+ /**
111
+ * Push a new scope onto the stack (becomes highest priority)
112
+ */
113
+ push_scope(keymap) {
114
+ // console.log('pushed keymap', keymap);
115
+ this.scope_stack.push(keymap);
116
+ }
117
+
118
+ /**
119
+ * Pop the most recent scope from the stack
120
+ */
121
+ pop_scope() {
122
+ const keymap = this.scope_stack.pop();
123
+ // console.log('popped keymap', keymap);
124
+ return keymap;
125
+ }
126
+
127
+ /**
128
+ * Handle keyboard event by trying scopes from top to bottom
129
+ */
130
+ handle_keydown(event) {
131
+ // Key handling temporarily disabled (e.g. while character composition takes place)
132
+ if (this.skip_onkeydown) return;
133
+ // console.log('KeyMapper.handle_keydown', event);
134
+ // Try from most specific (top of stack) to most general (bottom)
135
+ for (let i = this.scope_stack.length - 1; i >= 0; i--) {
136
+ if (handle_key_map(this.scope_stack[i], event)) {
137
+ return;
138
+ }
139
+ }
140
+ }
141
+ }
package/dist/Node.svelte CHANGED
@@ -1,45 +1,43 @@
1
1
  <script>
2
- import { getContext } from 'svelte';
2
+ import { getContext } from 'svelte';
3
3
 
4
- /** @import { NodeProps } from './types.d.ts'; */
4
+ /** @import { NodeProps } from './types.d.ts'; */
5
5
 
6
- const svedit = getContext('svedit');
7
- let NodeCursorTrap = $derived(svedit.doc.config.system_components.NodeCursorTrap);
6
+ const svedit = getContext('svedit');
7
+ let NodeCursorTrap = $derived(svedit.session.config.system_components.NodeCursorTrap);
8
8
 
9
- /** @type {NodeProps} */
10
- let {
11
- path,
12
- children
13
- } = $props();
9
+ /** @type {NodeProps} */
10
+ let { path, children, tag = 'div', class: css_class } = $props();
14
11
 
15
- // NOTE: When the next to last path segment is a node_array property, the node is wrapped in a node_array
16
- let is_inside_node_array = $derived(path.length > 1 && svedit.doc.inspect(path.slice(0, -1))?.type === 'node_array');
17
- let node = $derived(svedit.doc.get(path));
18
- let is_first_node_array_child = $derived(is_inside_node_array && parseInt(String(path.at(-1)), 10) === 0);
12
+ // NOTE: When the next to last path segment is a node_array property, the node is wrapped in a node_array
13
+ let is_inside_node_array = $derived(
14
+ path.length > 1 && svedit.session.inspect(path.slice(0, -1))?.type === 'node_array'
15
+ );
16
+ let node = $derived(svedit.session.get(path));
17
+ let is_first_node_array_child = $derived(
18
+ is_inside_node_array && parseInt(String(path.at(-1)), 10) === 0
19
+ );
19
20
  </script>
20
21
 
21
- {#if svedit.editable}
22
- <div
23
- class="node"
24
- data-node-id={node.id}
25
- data-path={path.join('.')}
26
- data-type="node"
27
- style="anchor-name: --{path.join('-')};"
28
- >
29
- {#if is_first_node_array_child}
30
- <NodeCursorTrap {path} type="position-zero-cursor-trap" />
31
- {/if}
32
- {@render children()}
33
- {#if is_inside_node_array}
34
- <NodeCursorTrap {path} type="after-node-cursor-trap" />
35
- {/if}
36
- </div>
37
- {:else}
38
- {@render children()}
39
- {/if}
22
+ <svelte:element
23
+ this={tag}
24
+ class={css_class}
25
+ data-node-id={node.id}
26
+ data-path={path.join('.')}
27
+ data-type="node"
28
+ style="anchor-name: --{path.join('-')};"
29
+ >
30
+ {#if svedit.editable && is_first_node_array_child}
31
+ <NodeCursorTrap {path} type="position-zero-cursor-trap" />
32
+ {/if}
33
+ {@render children()}
34
+ {#if svedit.editable && is_inside_node_array}
35
+ <NodeCursorTrap {path} type="after-node-cursor-trap" />
36
+ {/if}
37
+ </svelte:element>
40
38
 
41
39
  <style>
42
- div {
43
- position: relative;
44
- }
40
+ div {
41
+ position: relative;
42
+ }
45
43
  </style>
@@ -1 +1 @@
1
- {"version":3,"file":"Node.svelte.d.ts","sourceRoot":"","sources":["../src/lib/Node.svelte.js"],"names":[],"mappings":";;;;;AA2CA,yFAAmD"}
1
+ {"version":3,"file":"Node.svelte.d.ts","sourceRoot":"","sources":["../src/lib/Node.svelte.js"],"names":[],"mappings":";;;;;AAwCA,yFAAmD"}
@@ -1,45 +1,48 @@
1
1
  <script>
2
- import { getContext } from 'svelte';
3
- import UnknownNode from './UnknownNode.svelte';
4
- import { snake_to_pascal } from './util.js';
2
+ import { getContext } from 'svelte';
3
+ import UnknownNode from './UnknownNode.svelte';
4
+ import { snake_to_pascal } from './utils.js';
5
5
 
6
- /** @import { NodeArrayPropertyProps } from './types.d.ts'; */
6
+ /** @import { NodeArrayPropertyProps } from './types.d.ts'; */
7
7
 
8
- const svedit = getContext('svedit');
9
- let NodeCursorTrap = $derived(svedit.doc.config.system_components.NodeCursorTrap);
8
+ const svedit = getContext('svedit');
9
+ let NodeCursorTrap = $derived(svedit.session.config.system_components.NodeCursorTrap);
10
10
 
11
- /** @type {NodeArrayPropertyProps} */
12
- let {
13
- path,
14
- class: css_class,
15
- } = $props();
11
+ /** @type {NodeArrayPropertyProps} */
12
+ let { path, tag = 'div', class: css_class } = $props();
16
13
 
17
- let nodes = $derived(svedit.doc.get(path).map(/** @param {string} node_id */ (node_id) => svedit.doc.get(node_id)));
14
+ let nodes = $derived(
15
+ svedit.session
16
+ .get(path)
17
+ .map(/** @param {string} node_id */ (node_id) => svedit.session.get(node_id))
18
+ );
18
19
  </script>
19
20
 
20
- <div class={css_class} data-type="node_array" data-path={path.join('.')}>
21
- {#if nodes.length === 0 && svedit.editable}
22
- <!--
23
- Experimental: We'll let .empty-node-array act like a node, so the existing
24
- code paths for selection mapping will work as expected.
21
+ <svelte:element this={tag} class={css_class} data-type="node_array" data-path={path.join('.')}>
22
+ {#if nodes.length === 0 && svedit.editable}
23
+ <!--
24
+ Experimental: We'll let .empty-node-array act like a node, so the existing
25
+ code paths for selection mapping will work as expected.
25
26
 
26
- TODO: Need to figure out a way to make .empty-node-array customizable.
27
- -->
28
- <div
29
- class="node empty-node-array"
30
- data-path={[...path, 0].join('.')}
31
- data-type="node"
32
- style="anchor-name: --{[...path, 0].join('-')}; position: relative; min-height: 40px; min-width: 40px;"
33
- >
34
- <NodeCursorTrap path={[...path, 0]} type="position-zero-cursor-trap" />
35
- </div>
36
- {/if}
37
- {#each nodes as node, index (index) }
38
- {@const Component = svedit.doc.config.node_components[snake_to_pascal(node.type)]}
39
- {#if Component}
40
- <Component path={[...path, index]} />
41
- {:else}
42
- <UnknownNode path={[...path, index]} />
43
- {/if}
44
- {/each}
45
- </div>
27
+ TODO: Need to figure out a way to make .empty-node-array customizable.
28
+ -->
29
+ <div
30
+ class="node empty-node-array"
31
+ data-path={[...path, 0].join('.')}
32
+ data-type="node"
33
+ style="anchor-name: --{[...path, 0].join(
34
+ '-'
35
+ )}; position: relative; min-height: 40px; min-width: 40px;"
36
+ >
37
+ <NodeCursorTrap path={[...path, 0]} type="position-zero-cursor-trap" />
38
+ </div>
39
+ {/if}
40
+ {#each nodes as node, index (index)}
41
+ {@const Component = svedit.session.config.node_components[snake_to_pascal(node.type)]}
42
+ {#if Component}
43
+ <Component path={[...path, index]} />
44
+ {:else}
45
+ <UnknownNode path={[...path, index]} />
46
+ {/if}
47
+ {/each}
48
+ </svelte:element>
@@ -1 +1 @@
1
- {"version":3,"file":"NodeArrayProperty.svelte.d.ts","sourceRoot":"","sources":["../src/lib/NodeArrayProperty.svelte.js"],"names":[],"mappings":";;;;;AA6CA,mHAAgE"}
1
+ {"version":3,"file":"NodeArrayProperty.svelte.d.ts","sourceRoot":"","sources":["../src/lib/NodeArrayProperty.svelte.js"],"names":[],"mappings":";;;;;AAgDA,mHAAgE"}