svedit 0.5.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 (62) hide show
  1. package/README.md +761 -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/{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 +1352 -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 +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 -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,66 +1,32 @@
1
1
  /**
2
- * @import {
3
- * NodeId,
4
- * DocumentPath,
5
- * Selection,
6
- * Annotation,
7
- * PrimitiveType,
8
- * NodeProperty,
9
- * NodeArrayProperty,
10
- * NodeKind,
11
- * NodeSchema,
12
- * DocumentSchema,
13
- * SerializedDocument
14
- * } from './types.d.ts';
2
+ * @typedef {Object} SessionOptions
3
+ * @property {Selection} [selection] - Initial selection state
15
4
  */
16
- /**
17
- * Identity function — keeps schema at runtime & makes IDE infer types.
18
- * Similar to your define_schema pattern but for document schemas.
19
- *
20
- * @template {Record<string, NodeSchema>} S
21
- * @param {S} schema - The document schema to validate
22
- * @returns {S} The same schema, but with type information preserved
23
- */
24
- export function define_document_schema<S extends Record<string, NodeSchema>>(schema: S): S;
25
- /**
26
- * Check if a string represents a valid primitive type.
27
- * @param {string} type - The type string to check
28
- * @returns {type is PrimitiveType} True if it's a valid primitive type
29
- */
30
- export function is_primitive_type(type: string): type is PrimitiveType;
31
- /**
32
- * Get the default node type for a property that references nodes.
33
- * @param {NodeProperty | NodeArrayProperty} property_definition - The property definition
34
- * @returns {string | null} The default node type, or null if none specified
35
- */
36
- export function get_default_node_type(property_definition: NodeProperty | NodeArrayProperty): string | null;
37
- /**
38
- * Validate a document schema to ensure all referenced node types exist.
39
- * @param {DocumentSchema} document_schema - The document schema to validate
40
- * @throws {Error} Throws if the document schema is invalid
41
- */
42
- export function validate_document_schema(document_schema: DocumentSchema): void;
43
- export default class Document {
5
+ export default class Session {
44
6
  /**
45
7
  * @param {DocumentSchema} schema - The document schema
46
- * @param {SerializedDocument} serialized_doc - The serialized document array
47
- * @param {object} [options] - Optional configuration
8
+ * @param {Document} doc - The document
9
+ * @param {object} config - configuration object
10
+ * @param {Object} [options]
48
11
  * @param {Selection} [options.selection] - Initial selection state
49
- * @param {any} [options.config] - Document configuration
50
12
  */
51
- constructor(schema: DocumentSchema, serialized_doc: SerializedDocument, { selection, config }?: {
13
+ constructor(schema: DocumentSchema, doc: Document, config: object, options?: {
52
14
  selection?: Selection;
53
- config?: any;
54
15
  });
16
+ /** @type {DocumentSchema} */
17
+ schema: DocumentSchema;
18
+ /** @type {Document} */
19
+ doc: Document;
20
+ /** @type {any} */
55
21
  config: any;
56
- document_id: any;
57
- nodes: any;
58
22
  history: any[];
59
23
  history_index: number;
24
+ last_batch_started: any;
25
+ commands: {};
26
+ keymap: {};
60
27
  can_undo: boolean;
61
28
  can_redo: boolean;
62
29
  selected_node: any;
63
- layout_node: any;
64
30
  available_annotation_types: any;
65
31
  /**
66
32
  * Sets the selection with validation
@@ -81,18 +47,26 @@ export default class Document {
81
47
  * @private
82
48
  */
83
49
  private _validate_selection;
84
- schema: DocumentSchema;
50
+ /**
51
+ * Gets the document_id from the doc
52
+ * @returns {string}
53
+ */
54
+ get document_id(): string;
55
+ validate_doc(): void;
85
56
  generate_id(): any;
86
- get_available_annotation_types(): any;
87
- get_selected_node(): any;
88
- get_layout_node(): any;
89
- _apply_op(op: any): void;
90
57
  /**
91
- * Validate a node against the document schema.
92
- * @param {any} node - The node to validate
93
- * @throws {Error} Throws if the node is invalid
58
+ * Initialize commands and keymap for this session.
59
+ * Called by Svedit component when it has the necessary context.
60
+ *
61
+ * NOTE: This assumes a single Svedit instance per session.
62
+ * For multiple editors on the same document, this architecture would need
63
+ * to be refactored to support multiple sessions per document.
64
+ *
65
+ * @param {object} context - The svedit context with session, editable, canvas, etc.
94
66
  */
95
- validate_node(node: any): void;
67
+ initialize_commands(context: object): void;
68
+ get_available_annotation_types(): any;
69
+ get_selected_node(): any;
96
70
  /**
97
71
  * Creates a new transaction for making atomic changes to the document.
98
72
  *
@@ -101,10 +75,15 @@ export default class Document {
101
75
  get tr(): Transaction;
102
76
  /**
103
77
  * Applies a transaction to the document.
78
+ * Auto-batches history entries with debounced behavior (max one entry per 2 seconds) when batch is true.
104
79
  *
105
80
  * @param {Transaction} transaction - The transaction to apply
81
+ * @param {object} [options] - Optional configuration
82
+ * @param {boolean} [options.batch=false] - Whether to allow batching with previous transaction
106
83
  */
107
- apply(transaction: Transaction): this;
84
+ apply(transaction: Transaction, { batch }?: {
85
+ batch?: boolean;
86
+ }): this;
108
87
  undo(): this;
109
88
  redo(): this;
110
89
  /**
@@ -113,19 +92,19 @@ export default class Document {
113
92
  * @returns {any} Either a node instance object or the value of a property
114
93
  * @example
115
94
  * // Get a node by ID
116
- * doc.get('list_1') // => { type: 'list', id: 'list_1', ... }
95
+ * session.get('list_1') // => { type: 'list', id: 'list_1', ... }
117
96
  *
118
97
  * @example
119
98
  * // Get a node array property
120
- * doc.get(['list_1', 'list_items']) // => [ 'list_item_1', 'list_item_2' ]
99
+ * session.get(['list_1', 'list_items']) // => [ 'list_item_1', 'list_item_2' ]
121
100
  *
122
101
  * @example
123
102
  * // Get a specific node from an array
124
- * doc.get(['page_1', 'body', 3, 'list_items', 0]) // => { type: 'list_item', id: 'list_item_1', ... }
103
+ * session.get(['page_1', 'body', 3, 'list_items', 0]) // => { type: 'list_item', id: 'list_item_1', ... }
125
104
  *
126
105
  * @example
127
106
  * // Get an annotated text property
128
- * doc.get(['page_1', 'cover', 'title']) // => {text: 'Hello world', annotations: []}
107
+ * session.get(['page_1', 'cover', 'title']) // => {text: 'Hello world', annotations: []}
129
108
  */
130
109
  get(path: DocumentPath | string): any;
131
110
  /**
@@ -135,7 +114,7 @@ export default class Document {
135
114
  * @todo The layout of these should be improved and more explictly typed
136
115
  *
137
116
  * @example
138
- * doc.inspect(['page_1', 'body']) => {
117
+ * session.inspect(['page_1', 'body']) => {
139
118
  * kind: 'property',
140
119
  * name: 'body',
141
120
  * type: 'node_array',
@@ -144,7 +123,7 @@ export default class Document {
144
123
  * }
145
124
  *
146
125
  * @example
147
- * doc.inspect(['page_1', 'body', 1]) => {
126
+ * session.inspect(['page_1', 'body', 1]) => {
148
127
  * kind: 'node',
149
128
  * id: 'paragraph_234',
150
129
  * type: 'paragraph',
@@ -183,10 +162,10 @@ export default class Document {
183
162
  * Returns the annotation object that is currently "under the cursor".
184
163
  * NOTE: Annotations in Svedit are exclusive, so there can only be one active_annotation
185
164
  *
186
- * @param {string} annotation_type
165
+ * @param {string} [annotation_type] Optional annotation type to filter by
187
166
  * @returns {Annotation|null}
188
167
  */
189
- active_annotation(annotation_type: string): Annotation | null;
168
+ active_annotation(annotation_type?: string): Annotation | null;
190
169
  get_selected_annotated_text(): {
191
170
  text: string;
192
171
  annotations: any;
@@ -195,18 +174,29 @@ export default class Document {
195
174
  get_selected_plain_text(): string;
196
175
  get_selected_nodes(): any;
197
176
  select_parent(): void;
198
- get_selection_range(): {
199
- start: number;
200
- end: number;
201
- length: number;
202
- };
203
- traverse(node_id: any): any[];
204
177
  /**
205
- * Convert the document to serialized format.
206
- * @returns {SerializedDocument} The serialized document array
178
+ * Traverses the document and returns a list of nodes in depth-first order.
179
+ *
180
+ * The traversal order is:
181
+ * 1. Leaf nodes first
182
+ * 2. Branch nodes second
183
+ * 3. Root node (entry point) last
184
+ *
185
+ * @param {string} node_id - The ID of the node to start traversing from
186
+ * @returns {Array<DocumentNode>} Array of nodes in depth-first order
187
+ * @note Nodes that are not reachable from the entry point node will not be included
188
+ */
189
+ traverse(node_id: string): Array<DocumentNode>;
190
+ /**
191
+ * Convert the document to a clean format for persistence.
192
+ *
193
+ * We make a traversal to ensure that orphaned nodes are not included,
194
+ * and that leaf nodes go first, followed by branches and the root node at last.
195
+ *
196
+ * @returns {Document} The document
207
197
  */
208
- to_json(): SerializedDocument;
209
- property_type(type: any, property: any): "string" | "number" | "boolean" | "node_array" | "node" | "annotated_text" | "integer" | "integer_array" | "number_array" | "string_array" | "datetime" | "boolean_array";
198
+ to_json(): Document;
199
+ property_type(type: any, property: any): string;
210
200
  count_references(node_id: any): number;
211
201
  /**
212
202
  * @param {NodeId} node_id
@@ -215,16 +205,19 @@ export default class Document {
215
205
  get_referenced_nodes(node_id: NodeId): NodeId[];
216
206
  #private;
217
207
  }
218
- import type { NodeSchema } from './types.d.ts';
219
- import type { PrimitiveType } from './types.d.ts';
220
- import type { NodeProperty } from './types.d.ts';
221
- import type { NodeArrayProperty } from './types.d.ts';
208
+ export type SessionOptions = {
209
+ /**
210
+ * - Initial selection state
211
+ */
212
+ selection?: Selection;
213
+ };
222
214
  import type { DocumentSchema } from './types.d.ts';
215
+ import type { Document } from './types.d.ts';
223
216
  import type { Selection } from './types.d.ts';
224
217
  import Transaction from './Transaction.svelte.js';
225
218
  import type { DocumentPath } from './types.d.ts';
226
219
  import type { NodeKind } from './types.d.ts';
227
220
  import type { Annotation } from './types.d.ts';
228
- import type { SerializedDocument } from './types.d.ts';
221
+ import type { DocumentNode } from './types.d.ts';
229
222
  import type { NodeId } from './types.d.ts';
230
- //# sourceMappingURL=Document.svelte.d.ts.map
223
+ //# sourceMappingURL=Session.svelte.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Session.svelte.d.ts","sourceRoot":"","sources":["../src/lib/Session.svelte.js"],"names":[],"mappings":"AA6BA;;;GAGG;AAEH;IA8BC;;;;;;OAMG;IACH,oBANW,cAAc,OACd,QAAQ,UACR,MAAM,YAEd;QAA4B,SAAS,GAA7B,SAAS;KACnB,EAWA;IA3CD,6BAA6B;IAC7B,QADW,cAAc,CACH;IAEtB,uBAAuB;IACvB,KADW,QAAQ,CACA;IAEnB,kBAAkB;IAClB,QADW,GAAG,CACQ;IAEtB,eAAyB;IACzB,sBAA+B;IAC/B,wBAA2C;IAI3C,aAA0B;IAC1B,WAAwB;IAGxB,kBAA6C;IAC7C,kBAAkE;IAGlE,mBAAmD;IACnD,gCAA6E;IA6B7E;;;;OAIG;IACH,qBAHW,SAAS,GAAG,SAAS,EAQ/B;IAlBD;;;OAGG;IACH,iBAFa,SAAS,GAAG,SAAS,CAIjC;IAcD;;;;;;OAMG;IACH,4BAyDC;IAED;;;OAGG;IACH,mBAFa,MAAM,CAIlB;IAED,qBAIC;IAED,mBAMC;IAED;;;;;;;;;OASG;IACH,6BAFW,MAAM,QAQhB;IAED,sCAKC;IAGD,yBAkBC;IAED;;;;OAIG;IACH,UAFa,WAAW,CAKvB;IAED;;;;;;;OAOG;IACH,mBAJW,WAAW,cAEnB;QAA0B,KAAK,GAAvB,OAAO;KACjB,QA4CA;IAED,aAgBC;IAED,aAaC;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,UAlBW,YAAY,GAAC,MAAM,GACjB,GAAG,CAmBf;IAED;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,cAHW,YAAY,GACV;QAAC,IAAI,EAAE,UAAU,GAAC,MAAM,CAAC;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KAAC,CAIzD;IAED;;;;;OAKG;IACH,WAHW,GAAG,GACD,QAAQ,CAIpB;IAED;;;;;OAKG;IACH,sBAJW,MAAM,cACN,SAAS,GACP,OAAO,CAcnB;IAED;;;;;OAKG;IACH,wCAHW,SAAS,GACP,SAAS,CAerB;IAED;;;;;;OAMG;IACH,oCAHW,MAAM,GACJ,UAAU,GAAC,IAAI,CAI3B;IAED;;;;MA6BC;IAGD,kCAOC;IAED,0BAOC;IAED,sBAmCC;IAED;;;;;;;;;;;OAWG;IACH,kBAJW,MAAM,GACJ,KAAK,CAAC,YAAY,CAAC,CAK/B;IAED;;;;;;;OAOG;IACH,WAFa,QAAQ,CAWpB;IAID,gDAEC;IAGD,uCAEC;IAGD;;;OAGG;IACH,8BAHW,MAAM,GACJ,MAAM,EAAE,CAOpB;;CACD;;;;;gBAljBa,SAAS;;oCAPb,cAAc;8BAAd,cAAc;+BAAd,cAAc;wBAxBA,yBAAyB;kCAwBvC,cAAc;8BAAd,cAAc;gCAAd,cAAc;kCAAd,cAAc;4BAAd,cAAc"}