svedit 0.4.0 → 0.6.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 (63) hide show
  1. package/README.md +762 -120
  2. package/dist/AnnotatedTextProperty.svelte +122 -75
  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/Session.svelte.d.ts +223 -0
  17. package/dist/Session.svelte.d.ts.map +1 -0
  18. package/dist/Session.svelte.js +594 -0
  19. package/dist/Svedit.svelte +1352 -1465
  20. package/dist/Svedit.svelte.d.ts.map +1 -1
  21. package/dist/Transaction.svelte.d.ts +122 -51
  22. package/dist/Transaction.svelte.d.ts.map +1 -1
  23. package/dist/Transaction.svelte.js +845 -551
  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} +23 -2
  36. package/dist/utils.d.ts.map +1 -0
  37. package/dist/utils.js +338 -0
  38. package/package.json +25 -23
  39. package/src/lib/AnnotatedTextProperty.svelte +122 -75
  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 +1352 -1465
  47. package/src/lib/Transaction.svelte.js +845 -551
  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 +0 -214
  55. package/dist/Document.svelte.d.ts.map +0 -1
  56. package/dist/Document.svelte.js +0 -796
  57. package/dist/commands.svelte.d.ts.map +0 -1
  58. package/dist/commands.svelte.js +0 -271
  59. package/dist/util.d.ts.map +0 -1
  60. package/dist/util.js +0 -235
  61. package/src/lib/Document.svelte.js +0 -796
  62. package/src/lib/commands.svelte.js +0 -271
  63. package/src/lib/util.js +0 -235
@@ -1 +1 @@
1
- {"version":3,"file":"Svedit.svelte.d.ts","sourceRoot":"","sources":["../src/lib/Svedit.svelte.js"],"names":[],"mappings":";;;;;;;AAo6CA;;eAAqD;iCAx5CgE,cAAc"}
1
+ {"version":3,"file":"Svedit.svelte.d.ts","sourceRoot":"","sources":["../src/lib/Svedit.svelte.js"],"names":[],"mappings":";;;;;;;AAszCA;;eAAqD;iCA9xC1C,cAAc"}
@@ -7,38 +7,106 @@
7
7
  *
8
8
  * @example
9
9
  * ```js
10
- * const tr = doc.tr;
10
+ * const tr = session.tr;
11
11
  * tr.set(['node_1', 'title'], 'New Title');
12
12
  * tr.create({id: 'node_2', type: 'paragraph', content: 'Hello'});
13
- * doc.apply(tr); // Applies all operations atomically
13
+ * session.apply(tr); // Applies all operations atomically
14
14
  * ```
15
15
  */
16
16
  export default class Transaction {
17
17
  /**
18
- * Creates a new Transaction for the given document.
18
+ * Creates a new Transaction with the given state.
19
19
  *
20
- * @param {any} doc - The document instance this transaction operates on
20
+ * @param {DocumentSchema} schema - The document schema
21
+ * @param {Document} doc - The document state {document_id, nodes}
22
+ * @param {Selection | null} selection - The current selection
23
+ * @param {object} config - The document config (including generate_id)
21
24
  */
22
- constructor(doc: any);
23
- doc: any;
25
+ constructor(schema: DocumentSchema, doc: Document, selection: Selection | null, config: object);
26
+ schema: DocumentSchema;
27
+ /** @type {object} */
28
+ doc: object;
29
+ /** @type {Selection} */
30
+ selection: Selection;
31
+ config: any;
24
32
  /** @type {Record<string, any>[]} */
25
33
  ops: Record<string, any>[];
26
34
  /** @type {Record<string, any>[]} */
27
35
  inverse_ops: Record<string, any>[];
28
- selection_before: any;
36
+ selection_before: Selection;
29
37
  /**
30
38
  * Gets a value from the document at the specified path.
31
39
  *
32
- * @param {DocumentPath} path - The path to the value in the document
40
+ * @param {DocumentPath|string} path - The path to the value in the document, or a string node ID
33
41
  * @returns {any} The value at the specified path
34
42
  */
35
- get(path: DocumentPath): any;
43
+ get(path: DocumentPath | string): any;
36
44
  /**
37
- * Gets the current selection state of the document.
45
+ * Gets the type of a property from the schema.
38
46
  *
39
- * @returns {Selection} The current selection object
47
+ * @param {string} type - The node type
48
+ * @param {string} property - The property name
49
+ * @returns {string} The property type
40
50
  */
41
- get selection(): Selection;
51
+ property_type(type: string, property: string): string;
52
+ /**
53
+ * Determines the kind of a node ('block', 'text', or 'annotation').
54
+ *
55
+ * @param {any} node - The node to check
56
+ * @returns {'block'|'text'|'annotation'} The node kind
57
+ */
58
+ kind(node: any): "block" | "text" | "annotation";
59
+ /**
60
+ * Inspects a path to get metadata about the value at that location.
61
+ *
62
+ * @param {DocumentPath} path - The path to inspect
63
+ * @returns {{kind: 'property'|'node', [key: string]: any}} Metadata about the path
64
+ */
65
+ inspect(path: DocumentPath): {
66
+ kind: "property" | "node";
67
+ [key: string]: any;
68
+ };
69
+ /**
70
+ * Generates a new unique ID using the config's generate_id function.
71
+ *
72
+ * @returns {string} A new unique ID
73
+ */
74
+ generate_id(): string;
75
+ /**
76
+ * Validates a node against the document schema.
77
+ *
78
+ * @param {any} node - The node to validate
79
+ * @throws {Error} Throws if the node is invalid
80
+ */
81
+ validate_node(node: any): void;
82
+ /**
83
+ * Gets all nodes referenced by a given node (recursively).
84
+ *
85
+ * @param {NodeId} node_id - The node ID to get references for
86
+ * @returns {NodeId[]} Array of referenced node IDs
87
+ */
88
+ get_referenced_nodes(node_id: NodeId): NodeId[];
89
+ /**
90
+ * Gets the available annotation types for the current selection.
91
+ *
92
+ * @returns {string[]} Array of available annotation type names
93
+ */
94
+ get available_annotation_types(): string[];
95
+ /**
96
+ * Returns the annotation object that is currently "under the cursor".
97
+ * NOTE: Annotations in Svedit are exclusive, so there can only be one active_annotation
98
+ *
99
+ * @param {string} [annotation_type] Optional annotation type to filter by
100
+ * @returns {Annotation|null}
101
+ */
102
+ active_annotation(annotation_type?: string): Annotation | null;
103
+ /**
104
+ * Applies an operation to the document (internal).
105
+ *
106
+ * @param {Array} op - The operation to apply
107
+ * @private
108
+ */
109
+ private _apply_op;
42
110
  /**
43
111
  * Sets a property of a node to a new value.
44
112
  *
@@ -47,6 +115,7 @@ export default class Transaction {
47
115
  *
48
116
  * @param {DocumentPath} path - Array path to the property (e.g., ["node_1", "title"])
49
117
  * @param {any} value - The new value to set
118
+ * @returns {Transaction} This transaction instance for method chaining
50
119
  *
51
120
  * @example
52
121
  * ```js
@@ -54,7 +123,8 @@ export default class Transaction {
54
123
  * tr.set(["page_1", "body", "0", "description"], {text: "Hello world", annotations: []});
55
124
  * ```
56
125
  */
57
- set(path: DocumentPath, value: any): void;
126
+ set(path: DocumentPath, value: any): Transaction;
127
+ build(node_id: any, nodes: any): any;
58
128
  /**
59
129
  * Creates a new node in the document.
60
130
  *
@@ -97,6 +167,14 @@ export default class Transaction {
97
167
  * @throws {Error} Throws if the selection is invalid or out of bounds
98
168
  */
99
169
  set_selection(selection: Selection): Transaction;
170
+ /**
171
+ * Validates a selection against the current document state.
172
+ *
173
+ * @param {Selection} selection - The selection to validate
174
+ * @throws {Error} Throws if the selection is invalid
175
+ * @private
176
+ */
177
+ private _validate_selection;
100
178
  /**
101
179
  * Adds, updates, or removes text annotations in the current selection.
102
180
  *
@@ -124,63 +202,53 @@ export default class Transaction {
124
202
  * Behavior depends on selection type:
125
203
  * - For node selections: Removes selected nodes and cascades deletion of unreferenced nodes
126
204
  * - For text selections: Removes selected text and adjusts annotations accordingly
127
- * - For collapsed selections: Deletes the previous character/node
205
+ * - For collapsed selections: Deletes the previous character/node (backward) or next character/node (forward)
206
+ * - Property selections are ignored: Those are best handled handled via commands + keyboard shortcuts.
128
207
  *
208
+ * @param {'backward' | 'forward'} [direction] - Direction of deletion for collapsed selections
129
209
  * @returns {Transaction} This transaction instance for method chaining
130
210
  */
131
- delete_selection(): Transaction;
211
+ delete_selection(direction?: "backward" | "forward"): Transaction;
132
212
  /**
133
- * Inserts an array of nodes at the current node selection.
213
+ * Inserts nodes at the current node selection position.
134
214
  *
135
- * If there's a current selection, it will be deleted first. New nodes are
136
- * created if they don't already exist (e.g., for cut+paste scenarios).
215
+ * If the selection is expanded (not collapsed), first deletes the selected nodes
216
+ * before inserting the new ones.
137
217
  *
138
- * @param {SerializedNode[]} nodes - Array of node objects to insert
218
+ * @param {NodeId[]} node_ids - Array of node IDs to insert
139
219
  * @returns {Transaction} This transaction instance for method chaining
140
- *
141
- * @note Assumes we only insert new nodes, not reference existing ones
142
- *
143
- * @example
144
- * ```js
145
- * tr.insert_nodes([
146
- * {id: 'para_1', type: 'paragraph', content: ['Hello', []]},
147
- * {id: 'para_2', type: 'paragraph', content: ['World', []]}
148
- * ]);
149
- * ```
150
220
  */
151
- insert_nodes(nodes: SerializedNode[]): Transaction;
221
+ insert_nodes(node_ids: NodeId[]): Transaction;
152
222
  /**
153
- * Inserts text at the current text selection, replacing any selected content.
223
+ * Inserts text at the current text selection position.
154
224
  *
155
- * Updates both the text content and adjusts annotation offsets to maintain
156
- * annotation integrity after the text insertion.
225
+ * Handles annotation adjustments when text is inserted, including:
226
+ * - Expanding annotations that contain the insertion point
227
+ * - Shifting annotations that come after the insertion point
228
+ * - Optionally applying new annotations to the inserted text
157
229
  *
158
230
  * @param {string} replaced_text - The text to insert
231
+ * @param {Array} [annotations] - Optional annotations to apply to the inserted text
232
+ * @param {object} [nodes] - Optional node definitions for annotation nodes
159
233
  * @returns {Transaction} This transaction instance for method chaining
160
- *
161
- * @todo Support annotations attached to replaced_text for full copy&paste support
162
- *
163
- * @example
164
- * ```js
165
- * tr.insert_text('Hello, world!');
166
- * ```
167
234
  */
168
- insert_text(replaced_text: string): Transaction;
235
+ insert_text(replaced_text: string, annotations?: any[], nodes?: object): Transaction;
169
236
  /**
170
- * Recursively deletes nodes that become unreferenced after the initial deletion.
237
+ * Recursively deletes nodes that are no longer referenced in the document.
171
238
  *
172
- * This method implements cascade deletion by checking reference counts and
173
- * removing nodes that are no longer referenced by any other nodes in the document.
239
+ * This handles the cascade deletion of child nodes when their parent
240
+ * references are removed. Uses reference counting to determine which
241
+ * nodes are safe to delete.
174
242
  *
175
- * @param {NodeId[]} removed_node_ids - Array of node IDs that were initially removed
243
+ * @param {NodeId[]} potentially_orphaned_nodes - Array of node IDs to check
176
244
  * @private
177
245
  */
178
246
  private _cascade_delete_unreferenced_nodes;
179
247
  /**
180
- * Counts references to a target node, excluding nodes that are marked for deletion.
248
+ * Counts references to a node, excluding nodes that have been marked for deletion.
181
249
  *
182
- * This is used during cascade deletion to determine if a node can be safely
183
- * deleted without breaking document integrity.
250
+ * This is used during cascade deletion to accurately count remaining references
251
+ * as nodes are being deleted.
184
252
  *
185
253
  * @param {NodeId} target_node_id - The node ID to count references for
186
254
  * @param {Record<NodeId, boolean>} nodes_to_delete - Nodes already marked for deletion
@@ -189,7 +257,10 @@ export default class Transaction {
189
257
  */
190
258
  private _count_references_excluding_deleted;
191
259
  }
192
- import type { DocumentPath } from './types.js';
193
- import type { Selection } from './types.js';
194
- import type { SerializedNode } from './types.js';
260
+ import type { DocumentSchema } from './types.d.js';
261
+ import type { Selection } from './types.d.js';
262
+ import type { DocumentPath } from './types.d.js';
263
+ import type { NodeId } from './types.d.js';
264
+ import type { Annotation } from './types.d.js';
265
+ import type { Document } from './types.d.js';
195
266
  //# sourceMappingURL=Transaction.svelte.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Transaction.svelte.d.ts","sourceRoot":"","sources":["../src/lib/Transaction.svelte.js"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;GAcG;AACH;IACE;;;;OAIG;IACH,iBAFW,GAAG,EAWb;IARC,SAAc;IAEd,oCAAoC;IACpC,KADW,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CACnB;IACb,oCAAoC;IACpC,aADW,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CACX;IAErB,sBAA0C;IAG5C;;;;;OAKG;IACH,UAHW,YAAY,GACV,GAAG,CAIf;IAED;;;;OAIG;IACH,iBAFa,SAAS,CAIrB;IAED;;;;;;;;;;;;;;OAcG;IACH,UATW,YAAY,SACZ,GAAG,QA2Bb;IAED;;;;;;;;;;;;;;;;;;OAkBG;IACH,aAbW,GAAG,GACD,WAAW,CAyBvB;IAED;;;;;;;;;;;;OAYG;IACH,WARW,GAAG,GACD,WAAW,CAcvB;IAED;;;;;;OAMG;IACH,yBAJW,SAAS,GACP,WAAW,CAOvB;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,+BAbW,GAAG,yBACH,GAAG,GACD,WAAW,CAuDvB;IAED;;;;;;;;;OASG;IACH,oBAFa,WAAW,CAyGvB;IAED;;;;;;;;;;;;;;;;;;OAkBG;IACH,oBAbW,cAAc,EAAE,GACd,WAAW,CAqDvB;IAED;;;;;;;;;;;;;;;OAeG;IACH,2BAVW,MAAM,GACJ,WAAW,CA8EvB;IAED;;;;;;;;OAQG;IACH,2CA+BC;IAED;;;;;;;;;;OAUG;IACH,4CAuBC;CACF;kCAxjBmE,YAAY;+BAAZ,YAAY;oCAAZ,YAAY"}
1
+ {"version":3,"file":"Transaction.svelte.d.ts","sourceRoot":"","sources":["../src/lib/Transaction.svelte.js"],"names":[],"mappings":"AAuBA;;;;;;;;;;;;;;GAcG;AACH;IACC;;;;;;;OAOG;IACH,oBALW,cAAc,OACd,QAAQ,aACR,SAAS,GAAG,IAAI,UAChB,MAAM,EAgBhB;IAbA,uBAAoB;IACpB,qBAAqB;IACrB,KADW,MAAM,CACH;IACd,wBAAwB;IACxB,WADW,SAAS,CACM;IAC1B,YAAoB;IAEpB,oCAAoC;IACpC,KADW,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CACnB;IACb,oCAAoC;IACpC,aADW,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CACX;IAErB,4BAAiC;IAGlC;;;;;OAKG;IACH,UAHW,YAAY,GAAC,MAAM,GACjB,GAAG,CAIf;IAED;;;;;;OAMG;IACH,oBAJW,MAAM,YACN,MAAM,GACJ,MAAM,CAIlB;IAED;;;;;OAKG;IACH,WAHW,GAAG,GACD,OAAO,GAAC,MAAM,GAAC,YAAY,CAIvC;IAED;;;;;OAKG;IACH,cAHW,YAAY,GACV;QAAC,IAAI,EAAE,UAAU,GAAC,MAAM,CAAC;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KAAC,CAIzD;IAED;;;;OAIG;IACH,eAFa,MAAM,CAIlB;IAED;;;;;OAKG;IACH,oBAHW,GAAG,QAKb;IAED;;;;;OAKG;IACH,8BAHW,MAAM,GACJ,MAAM,EAAE,CAKpB;IAED;;;;OAIG;IACH,kCAFa,MAAM,EAAE,CAOpB;IAED;;;;;;OAMG;IACH,oCAHW,MAAM,GACJ,UAAU,GAAC,IAAI,CAI3B;IAED;;;;;OAKG;IACH,kBAEC;IAED;;;;;;;;;;;;;;;OAeG;IACH,UAVW,YAAY,SACZ,GAAG,GACD,WAAW,CAmDvB;IAKD,qCAsDC;IAED;;;;;;;;;;;;;;;;;;OAkBG;IACH,aAbW,GAAG,GACD,WAAW,CAyBvB;IAED;;;;;;;;;;;;OAYG;IACH,WARW,GAAG,GACD,WAAW,CAuBvB;IAED;;;;;;OAMG;IACH,yBAJW,SAAS,GACP,WAAW,CAOvB;IAED;;;;;;OAMG;IACH,4BA6CC;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,+BAbW,GAAG,yBACH,GAAG,GACD,WAAW,CA+DvB;IAED;;;;;;;;;;;OAWG;IACH,6BAHW,UAAU,GAAG,SAAS,GACpB,WAAW,CA0IvB;IAED;;;;;;;;OAQG;IACH,uBAHW,MAAM,EAAE,GACN,WAAW,CAkCvB;IAED;;;;;;;;;;;;OAYG;IACH,2BALW,MAAM,+BAEN,MAAM,GACJ,WAAW,CAqGvB;IAED;;;;;;;;;OASG;IACH,2CAgCC;IAED;;;;;;;;;;OAUG;IACH,4CAOC;CACD;oCA91ByF,cAAc;+BAAd,cAAc;kCAAd,cAAc;4BAAd,cAAc;gCAAd,cAAc;8BAAd,cAAc"}