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.
- package/README.md +761 -120
- package/dist/AnnotatedTextProperty.svelte +129 -76
- package/dist/AnnotatedTextProperty.svelte.d.ts.map +1 -1
- package/dist/Command.svelte.d.ts +118 -0
- package/dist/Command.svelte.d.ts.map +1 -0
- package/dist/Command.svelte.js +314 -0
- package/dist/CustomProperty.svelte +31 -42
- package/dist/CustomProperty.svelte.d.ts.map +1 -1
- package/dist/KeyMapper.svelte.d.ts +36 -0
- package/dist/KeyMapper.svelte.d.ts.map +1 -0
- package/dist/KeyMapper.svelte.js +141 -0
- package/dist/Node.svelte +33 -35
- package/dist/Node.svelte.d.ts.map +1 -1
- package/dist/NodeArrayProperty.svelte +40 -37
- package/dist/NodeArrayProperty.svelte.d.ts.map +1 -1
- package/dist/{Document.svelte.d.ts → Session.svelte.d.ts} +76 -83
- package/dist/Session.svelte.d.ts.map +1 -0
- package/dist/Session.svelte.js +594 -0
- package/dist/Svedit.svelte +1338 -1512
- package/dist/Svedit.svelte.d.ts.map +1 -1
- package/dist/Transaction.svelte.d.ts +114 -40
- package/dist/Transaction.svelte.d.ts.map +1 -1
- package/dist/Transaction.svelte.js +845 -661
- package/dist/UnknownNode.svelte +6 -6
- package/dist/UnknownNode.svelte.d.ts.map +1 -1
- package/dist/doc_utils.d.ts +127 -0
- package/dist/doc_utils.d.ts.map +1 -0
- package/dist/doc_utils.js +482 -0
- package/dist/index.d.ts +7 -3
- package/dist/index.js +20 -4
- package/dist/{commands.svelte.d.ts → transforms.svelte.d.ts} +1 -2
- package/dist/transforms.svelte.d.ts.map +1 -0
- package/dist/transforms.svelte.js +155 -0
- package/dist/types.d.ts +256 -110
- package/dist/{util.d.ts → utils.d.ts} +22 -2
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +338 -0
- package/package.json +20 -18
- package/src/lib/AnnotatedTextProperty.svelte +129 -76
- package/src/lib/Command.svelte.js +314 -0
- package/src/lib/CustomProperty.svelte +31 -42
- package/src/lib/KeyMapper.svelte.js +141 -0
- package/src/lib/Node.svelte +33 -35
- package/src/lib/NodeArrayProperty.svelte +40 -37
- package/src/lib/Session.svelte.js +594 -0
- package/src/lib/Svedit.svelte +1338 -1512
- package/src/lib/Transaction.svelte.js +845 -661
- package/src/lib/UnknownNode.svelte +6 -6
- package/src/lib/doc_utils.js +482 -0
- package/src/lib/index.js +20 -4
- package/src/lib/transforms.svelte.js +155 -0
- package/src/lib/types.d.ts +256 -110
- package/src/lib/utils.js +338 -0
- package/dist/Document.svelte.d.ts.map +0 -1
- package/dist/Document.svelte.js +0 -833
- package/dist/commands.svelte.d.ts.map +0 -1
- package/dist/commands.svelte.js +0 -266
- package/dist/util.d.ts.map +0 -1
- package/dist/util.js +0 -269
- package/src/lib/Document.svelte.js +0 -833
- package/src/lib/commands.svelte.js +0 -266
- package/src/lib/util.js +0 -269
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
<script>
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
import { getContext } from 'svelte';
|
|
3
|
+
import Node from './Node.svelte';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
let node = $derived(svedit.
|
|
5
|
+
const svedit = getContext('svedit');
|
|
6
|
+
let { path } = $props();
|
|
7
|
+
let node = $derived(svedit.session.get(path));
|
|
8
8
|
</script>
|
|
9
9
|
|
|
10
10
|
<Node {path}>
|
|
11
|
-
|
|
11
|
+
Unknown: {node.type}.
|
|
12
12
|
</Node>
|
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared document utilities used by both Session and Transaction.
|
|
3
|
+
*
|
|
4
|
+
* These functions operate on the core document state (schema, doc, selection, config)
|
|
5
|
+
* without any history management or transaction tracking.
|
|
6
|
+
*
|
|
7
|
+
* @import { NodeId, DocumentPath, PrimitiveType, NodeProperty, NodeArrayProperty, NodeSchema, DocumentSchema, Selection, Annotation, Document } from './types'
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { get_selection_range } from './utils.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Identity function — keeps schema at runtime & makes IDE infer types.
|
|
14
|
+
* Similar to your define_schema pattern but for document schemas.
|
|
15
|
+
*
|
|
16
|
+
* @template {Record<string, NodeSchema>} S
|
|
17
|
+
* @param {S} schema - The document schema to validate
|
|
18
|
+
* @returns {S} The same schema, but with type information preserved
|
|
19
|
+
*/
|
|
20
|
+
export function define_document_schema(schema) {
|
|
21
|
+
return schema;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Check if a string represents a valid primitive type.
|
|
26
|
+
* @param {string} type - The type string to check
|
|
27
|
+
* @returns {type is PrimitiveType} True if it's a valid primitive type
|
|
28
|
+
*/
|
|
29
|
+
export function is_primitive_type(type) {
|
|
30
|
+
return [
|
|
31
|
+
'string',
|
|
32
|
+
'number',
|
|
33
|
+
'boolean',
|
|
34
|
+
'integer',
|
|
35
|
+
'datetime',
|
|
36
|
+
'annotated_text',
|
|
37
|
+
'string_array',
|
|
38
|
+
'number_array',
|
|
39
|
+
'boolean_array',
|
|
40
|
+
'integer_array'
|
|
41
|
+
].includes(type);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Get the default node type for a property that references nodes.
|
|
46
|
+
* @param {NodeProperty | NodeArrayProperty} property_definition - The property definition
|
|
47
|
+
* @returns {string | null} The default node type, or null if none specified
|
|
48
|
+
*/
|
|
49
|
+
export function get_default_node_type(property_definition) {
|
|
50
|
+
if (!property_definition || !property_definition.node_types) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
property_definition.default_node_type ||
|
|
56
|
+
(property_definition.node_types.length === 1 ? property_definition.node_types[0] : null)
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Validate a document schema to ensure all referenced node types exist.
|
|
62
|
+
* @param {DocumentSchema} document_schema - The document schema to validate
|
|
63
|
+
* @throws {Error} Throws if the document schema is invalid
|
|
64
|
+
*/
|
|
65
|
+
export function validate_document_schema(document_schema) {
|
|
66
|
+
// Check that all referenced node types exist
|
|
67
|
+
for (const [node_type, node_schema] of Object.entries(document_schema)) {
|
|
68
|
+
for (const [prop_name, prop_def] of Object.entries(node_schema.properties)) {
|
|
69
|
+
if (prop_def.type === 'node' || prop_def.type === 'node_array') {
|
|
70
|
+
const missing_types = prop_def.node_types.filter(
|
|
71
|
+
(ref_type) => !(ref_type in document_schema)
|
|
72
|
+
);
|
|
73
|
+
if (missing_types.length > 0) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
`Node type "${node_type}" property "${prop_name}" references unknown node types: ${missing_types.join(', ')}. Available node types: ${Object.keys(document_schema).join(', ')}`
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Validate a primitive value against its schema type.
|
|
85
|
+
* @param {PrimitiveType} type - The expected type
|
|
86
|
+
* @param {any} value - The value to validate
|
|
87
|
+
* @returns {boolean} True if the value matches the type
|
|
88
|
+
*/
|
|
89
|
+
function validate_primitive_value(type, value) {
|
|
90
|
+
switch (type) {
|
|
91
|
+
case 'string':
|
|
92
|
+
return typeof value === 'string';
|
|
93
|
+
case 'number':
|
|
94
|
+
return typeof value === 'number' && !isNaN(value);
|
|
95
|
+
case 'boolean':
|
|
96
|
+
return typeof value === 'boolean';
|
|
97
|
+
case 'integer':
|
|
98
|
+
return Number.isInteger(value);
|
|
99
|
+
case 'datetime':
|
|
100
|
+
return typeof value === 'string' && !isNaN(Date.parse(value));
|
|
101
|
+
case 'annotated_text':
|
|
102
|
+
return (
|
|
103
|
+
typeof value === 'object' &&
|
|
104
|
+
value !== null &&
|
|
105
|
+
typeof value.text === 'string' &&
|
|
106
|
+
Array.isArray(value.annotations)
|
|
107
|
+
);
|
|
108
|
+
case 'string_array':
|
|
109
|
+
return Array.isArray(value) && value.every((v) => typeof v === 'string');
|
|
110
|
+
case 'number_array':
|
|
111
|
+
return Array.isArray(value) && value.every((v) => typeof v === 'number' && !isNaN(v));
|
|
112
|
+
case 'boolean_array':
|
|
113
|
+
return Array.isArray(value) && value.every((v) => typeof v === 'boolean');
|
|
114
|
+
case 'integer_array':
|
|
115
|
+
return Array.isArray(value) && value.every((v) => Number.isInteger(v));
|
|
116
|
+
default:
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @param {String} id
|
|
123
|
+
* @returns {boolean}
|
|
124
|
+
*/
|
|
125
|
+
function is_id_valid(id) {
|
|
126
|
+
return typeof id === 'string' && id.length > 0;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Validate a node against its schema.
|
|
131
|
+
* @param {any} node - The node to validate
|
|
132
|
+
* @param {DocumentSchema} schema - The document schema
|
|
133
|
+
* @param {Record<string, any>} all_nodes - All nodes in the document to check references
|
|
134
|
+
* @throws {Error} Throws if the node is invalid
|
|
135
|
+
*/
|
|
136
|
+
export function validate_node(node, schema, all_nodes = {}) {
|
|
137
|
+
if (!is_id_valid(node.id)) {
|
|
138
|
+
throw new Error(`Node ${node.id} has an invalid id.`);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (!node.type || !schema[node.type]) {
|
|
142
|
+
throw new Error(`Node ${node.id} has an invalid type: ${node.type}`);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const node_schema = schema[node.type];
|
|
146
|
+
|
|
147
|
+
for (const [prop_name, prop_def] of Object.entries(node_schema.properties)) {
|
|
148
|
+
const value = node[prop_name];
|
|
149
|
+
|
|
150
|
+
// Check primitive types
|
|
151
|
+
if (is_primitive_type(prop_def.type)) {
|
|
152
|
+
if (!validate_primitive_value(prop_def.type, value)) {
|
|
153
|
+
throw new Error(
|
|
154
|
+
`Node ${node.id} has an invalid property: ${prop_name} must be of type ${prop_def.type}.`
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
// Check node references
|
|
159
|
+
if (prop_def.type === 'node') {
|
|
160
|
+
if (!is_id_valid(value)) {
|
|
161
|
+
throw new Error(
|
|
162
|
+
`Node ${node.id} has an invalid property: ${prop_name} must be a valid node id.`
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
// Check if referenced node exists and is of allowed type
|
|
166
|
+
const referenced_node = all_nodes[value];
|
|
167
|
+
if (referenced_node && !prop_def.node_types.includes(referenced_node.type)) {
|
|
168
|
+
throw new Error(
|
|
169
|
+
`Node ${node.id} property ${prop_name} references node ${value} of type ${referenced_node.type}, but only types [${/** @type {NodeProperty} */ (prop_def).node_types.join(', ')}] are allowed.`
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
// Check node arrays
|
|
174
|
+
else if (prop_def.type === 'node_array') {
|
|
175
|
+
if (
|
|
176
|
+
!Array.isArray(value) ||
|
|
177
|
+
!value.every((id) => typeof id === 'string' && is_id_valid(id))
|
|
178
|
+
) {
|
|
179
|
+
throw new Error(
|
|
180
|
+
`Node ${node.id} has an invalid property: ${prop_name} must be an array of node ids.`
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
// Check if all referenced nodes are of allowed types
|
|
184
|
+
for (const ref_id of value) {
|
|
185
|
+
const referenced_node = all_nodes[ref_id];
|
|
186
|
+
if (referenced_node && !prop_def.node_types.includes(referenced_node.type)) {
|
|
187
|
+
throw new Error(
|
|
188
|
+
`Node ${node.id} property ${prop_name} references node ${ref_id} of type ${referenced_node.type}, but only types [${prop_def.node_types.join(', ')}] are allowed.`
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Gets a value from the document at the specified path.
|
|
198
|
+
*
|
|
199
|
+
* @param {DocumentSchema} schema - The document schema
|
|
200
|
+
* @param {Document} doc - The document containing nodes
|
|
201
|
+
* @param {DocumentPath|string} path - Array path to the value, or a string node ID
|
|
202
|
+
* @returns {any} The value at the specified path
|
|
203
|
+
*/
|
|
204
|
+
export function get(schema, doc, path) {
|
|
205
|
+
if (typeof path === 'string') {
|
|
206
|
+
path = [path];
|
|
207
|
+
}
|
|
208
|
+
if (!(Array.isArray(path) && path.length >= 1)) {
|
|
209
|
+
throw new Error(`Invalid path provided ${JSON.stringify(path)}`);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
let val = doc.nodes[path[0]];
|
|
213
|
+
let val_type = 'node';
|
|
214
|
+
|
|
215
|
+
for (let i = 1; i < path.length; i++) {
|
|
216
|
+
const path_segment = path[i];
|
|
217
|
+
const path_segment_str = String(path_segment);
|
|
218
|
+
if (val_type === 'node') {
|
|
219
|
+
if (property_type(schema, val.type, path_segment_str) === 'node_array') {
|
|
220
|
+
val = val[path_segment];
|
|
221
|
+
val_type = 'node_array';
|
|
222
|
+
} else if (property_type(schema, val.type, path_segment_str) === 'annotated_text') {
|
|
223
|
+
val = val[path_segment];
|
|
224
|
+
val_type = 'annotated_text';
|
|
225
|
+
} else if (property_type(schema, val.type, path_segment_str) === 'node') {
|
|
226
|
+
val = doc.nodes[val[path_segment]];
|
|
227
|
+
val_type = 'node';
|
|
228
|
+
} else if (
|
|
229
|
+
['string_array', 'integer_array'].includes(
|
|
230
|
+
property_type(schema, val.type, path_segment_str)
|
|
231
|
+
)
|
|
232
|
+
) {
|
|
233
|
+
val = val[path_segment];
|
|
234
|
+
val_type = 'value_array';
|
|
235
|
+
} else {
|
|
236
|
+
val = val[path_segment];
|
|
237
|
+
val_type = 'value';
|
|
238
|
+
}
|
|
239
|
+
} else if (val_type === 'node_array') {
|
|
240
|
+
val = doc.nodes[val[path_segment]];
|
|
241
|
+
val_type = 'node';
|
|
242
|
+
} else if (val_type === 'value_array') {
|
|
243
|
+
val = val[path_segment];
|
|
244
|
+
val_type = 'value';
|
|
245
|
+
} else if (val_type === 'annotated_text') {
|
|
246
|
+
if (path_segment === 'text') {
|
|
247
|
+
val = val.text;
|
|
248
|
+
val_type = 'value';
|
|
249
|
+
} else if (path_segment === 'annotations') {
|
|
250
|
+
val = val.annotations;
|
|
251
|
+
val_type = 'annotation_array';
|
|
252
|
+
} else {
|
|
253
|
+
throw new Error(
|
|
254
|
+
`Invalid path segment "${path_segment}" for annotated_text. Use "text" or "annotations".`
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
} else if (val_type === 'annotation_array') {
|
|
258
|
+
val = val[path_segment];
|
|
259
|
+
val_type = 'annotation';
|
|
260
|
+
} else if (val_type === 'annotation') {
|
|
261
|
+
if (path_segment === 'node_id') {
|
|
262
|
+
val = doc.nodes[val.node_id];
|
|
263
|
+
val_type = 'node';
|
|
264
|
+
} else if (path_segment === 'start_offset') {
|
|
265
|
+
val = val.start_offset;
|
|
266
|
+
val_type = 'value';
|
|
267
|
+
} else if (path_segment === 'end_offset') {
|
|
268
|
+
val = val.end_offset;
|
|
269
|
+
val_type = 'value';
|
|
270
|
+
} else {
|
|
271
|
+
throw new Error(
|
|
272
|
+
`Invalid path segment "${path_segment}" for annotation. Use "start_offset", "end_offset", or "node_id".`
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return val;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Gets the type of a property from the schema.
|
|
282
|
+
*
|
|
283
|
+
* @param {object} schema - The document schema
|
|
284
|
+
* @param {string} type - The node type
|
|
285
|
+
* @param {string} property - The property name
|
|
286
|
+
* @returns {string} The property type
|
|
287
|
+
*/
|
|
288
|
+
export function property_type(schema, type, property) {
|
|
289
|
+
if (typeof type !== 'string') throw new Error(`Invalid type ${type} provided`);
|
|
290
|
+
if (typeof property !== 'string') throw new Error(`Invalid property ${property} provided`);
|
|
291
|
+
|
|
292
|
+
if (property === 'type') return 'string';
|
|
293
|
+
if (property === 'id') return 'string';
|
|
294
|
+
|
|
295
|
+
if (!schema[type]) throw new Error(`Type ${type} not found in schema`);
|
|
296
|
+
if (!schema[type].properties[property])
|
|
297
|
+
throw new Error(`Property ${property} not found in type ${type}`);
|
|
298
|
+
|
|
299
|
+
return schema[type].properties[property].type;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Determines the kind of a node ('block', 'text', or 'annotation').
|
|
304
|
+
*
|
|
305
|
+
* @param {object} schema - The document schema
|
|
306
|
+
* @param {any} node - The node to check
|
|
307
|
+
* @returns {'block'|'text'|'annotation'} The node kind
|
|
308
|
+
*/
|
|
309
|
+
export function kind(schema, node) {
|
|
310
|
+
return schema[node.type].kind;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Inspects a path to get metadata about the value at that location.
|
|
315
|
+
*
|
|
316
|
+
* @param {DocumentSchema} schema - The document schema
|
|
317
|
+
* @param {Document} doc - The document containing nodes
|
|
318
|
+
* @param {DocumentPath} path - The path to inspect
|
|
319
|
+
* @returns {{kind: 'property'|'node', [key: string]: any}} Metadata about the path
|
|
320
|
+
*/
|
|
321
|
+
export function inspect(schema, doc, path) {
|
|
322
|
+
const parent = path.length > 1 ? get(schema, doc, path.slice(0, -1)) : undefined;
|
|
323
|
+
if (parent?.type) {
|
|
324
|
+
const property_name = path.at(-1);
|
|
325
|
+
return {
|
|
326
|
+
kind: 'property',
|
|
327
|
+
name: property_name,
|
|
328
|
+
...schema[parent.type].properties[property_name]
|
|
329
|
+
};
|
|
330
|
+
} else {
|
|
331
|
+
const node = get(schema, doc, path);
|
|
332
|
+
return {
|
|
333
|
+
kind: 'node',
|
|
334
|
+
id: node.id,
|
|
335
|
+
type: node.type,
|
|
336
|
+
properties: schema[node.type]
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Applies an operation to a document and returns the new document.
|
|
343
|
+
* Uses copy-on-write semantics.
|
|
344
|
+
*
|
|
345
|
+
* @param {Document} doc - The document to apply the operation to
|
|
346
|
+
* @param {Array} op - The operation to apply [type, ...args]
|
|
347
|
+
* @returns {Document} The new document with the operation applied
|
|
348
|
+
*/
|
|
349
|
+
export function apply_op(doc, op) {
|
|
350
|
+
const [type, ...args] = op;
|
|
351
|
+
if (type === 'set') {
|
|
352
|
+
const [node_id, property] = args[0];
|
|
353
|
+
const value = structuredClone(args[1]);
|
|
354
|
+
return {
|
|
355
|
+
...doc,
|
|
356
|
+
nodes: {
|
|
357
|
+
...doc.nodes,
|
|
358
|
+
[node_id]: {
|
|
359
|
+
...doc.nodes[node_id],
|
|
360
|
+
[property]: value
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
} else if (type === 'create') {
|
|
365
|
+
return {
|
|
366
|
+
...doc,
|
|
367
|
+
nodes: {
|
|
368
|
+
...doc.nodes,
|
|
369
|
+
[args[0].id]: structuredClone(args[0])
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
} else if (type === 'delete') {
|
|
373
|
+
// eslint-disable-next-line
|
|
374
|
+
const { [args[0]]: _removed, ...remaining_nodes } = doc.nodes;
|
|
375
|
+
return {
|
|
376
|
+
...doc,
|
|
377
|
+
nodes: remaining_nodes
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
return doc;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Counts how many times a node is referenced in the document.
|
|
385
|
+
*
|
|
386
|
+
* @param {DocumentSchema} schema - The document schema
|
|
387
|
+
* @param {Document} doc - The document containing nodes
|
|
388
|
+
* @param {NodeId} node_id - The node ID to count references for
|
|
389
|
+
* @returns {number} The number of references
|
|
390
|
+
*/
|
|
391
|
+
export function count_references(schema, doc, node_id) {
|
|
392
|
+
let count = 0;
|
|
393
|
+
|
|
394
|
+
for (const node of Object.values(doc.nodes)) {
|
|
395
|
+
for (const [property, value] of Object.entries(node)) {
|
|
396
|
+
if (property === 'id' || property === 'type') continue;
|
|
397
|
+
|
|
398
|
+
const prop_type = property_type(schema, node.type, property);
|
|
399
|
+
|
|
400
|
+
if (prop_type === 'node_array' && Array.isArray(value)) {
|
|
401
|
+
count += value.filter((id) => id === node_id).length;
|
|
402
|
+
} else if (prop_type === 'node' && value === node_id) {
|
|
403
|
+
count += 1;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
return count;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Gets the currently active annotation at the selection, optionally filtered by type.
|
|
413
|
+
*
|
|
414
|
+
* An annotation is considered "active" if it overlaps with the current selection in any way:
|
|
415
|
+
* - Annotation contains the selection start
|
|
416
|
+
* - Annotation contains the selection end
|
|
417
|
+
* - Selection fully contains the annotation
|
|
418
|
+
*
|
|
419
|
+
* @param {DocumentSchema} schema - The document schema
|
|
420
|
+
* @param {Document} doc - The document containing nodes
|
|
421
|
+
* @param {Selection} [selection] - The current selection
|
|
422
|
+
* @param {string} [annotation_type] - Optional annotation type to filter by
|
|
423
|
+
* @returns {Annotation | null} The active annotation, or null if none found
|
|
424
|
+
*/
|
|
425
|
+
export function get_active_annotation(schema, doc, selection, annotation_type) {
|
|
426
|
+
if (selection?.type !== 'text') return null;
|
|
427
|
+
const range = get_selection_range(selection);
|
|
428
|
+
if (!range) return null;
|
|
429
|
+
|
|
430
|
+
const annotated_text = get(schema, doc, selection.path);
|
|
431
|
+
const annotations = annotated_text.annotations;
|
|
432
|
+
|
|
433
|
+
const active_annotation =
|
|
434
|
+
annotations.find(
|
|
435
|
+
(/** @type {Annotation} */ { start_offset, end_offset }) =>
|
|
436
|
+
(start_offset <= range.start_offset && end_offset > range.start_offset) ||
|
|
437
|
+
(start_offset < range.end_offset && end_offset >= range.end_offset) ||
|
|
438
|
+
(start_offset >= range.start_offset && end_offset <= range.end_offset)
|
|
439
|
+
) || null;
|
|
440
|
+
|
|
441
|
+
if (annotation_type && active_annotation) {
|
|
442
|
+
const annotation_node = get(schema, doc, [active_annotation.node_id]);
|
|
443
|
+
return annotation_node?.type === annotation_type ? active_annotation : null;
|
|
444
|
+
} else {
|
|
445
|
+
return active_annotation;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Counts references to a node, excluding nodes marked for deletion.
|
|
451
|
+
*
|
|
452
|
+
* @param {DocumentSchema} schema - The document schema
|
|
453
|
+
* @param {Document} doc - The document containing nodes
|
|
454
|
+
* @param {NodeId} target_node_id - The node ID to count references for
|
|
455
|
+
* @param {Record<NodeId, boolean>} nodes_to_delete - Nodes marked for deletion
|
|
456
|
+
* @returns {number} The number of references excluding deleted nodes
|
|
457
|
+
*/
|
|
458
|
+
export function count_references_excluding_deleted(schema, doc, target_node_id, nodes_to_delete) {
|
|
459
|
+
let count = 0;
|
|
460
|
+
|
|
461
|
+
for (const node of Object.values(doc.nodes)) {
|
|
462
|
+
if (nodes_to_delete[node.id]) continue;
|
|
463
|
+
|
|
464
|
+
for (const [property, value] of Object.entries(node)) {
|
|
465
|
+
if (property === 'id' || property === 'type') continue;
|
|
466
|
+
|
|
467
|
+
const prop_type = property_type(schema, node.type, property);
|
|
468
|
+
|
|
469
|
+
if (prop_type === 'node_array' && Array.isArray(value)) {
|
|
470
|
+
count += value.filter((id) => id === target_node_id).length;
|
|
471
|
+
} else if (prop_type === 'node' && value === target_node_id) {
|
|
472
|
+
count += 1;
|
|
473
|
+
} else if (prop_type === 'annotated_text' && value && value.annotations) {
|
|
474
|
+
count += value.annotations.filter(
|
|
475
|
+
(annotation) => annotation.node_id === target_node_id
|
|
476
|
+
).length;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
return count;
|
|
482
|
+
}
|
package/src/lib/index.js
CHANGED
|
@@ -6,9 +6,25 @@ export { default as Node } from './Node.svelte';
|
|
|
6
6
|
export { default as NodeArrayProperty } from './NodeArrayProperty.svelte';
|
|
7
7
|
|
|
8
8
|
// Core classes and utilities
|
|
9
|
-
export { default as
|
|
9
|
+
export { default as Session } from './Session.svelte.js';
|
|
10
10
|
export { default as Transaction } from './Transaction.svelte.js';
|
|
11
11
|
|
|
12
|
-
//
|
|
13
|
-
export
|
|
14
|
-
|
|
12
|
+
// Document utilities
|
|
13
|
+
export {
|
|
14
|
+
define_document_schema,
|
|
15
|
+
is_primitive_type,
|
|
16
|
+
get_default_node_type,
|
|
17
|
+
validate_document_schema,
|
|
18
|
+
validate_node
|
|
19
|
+
} from './doc_utils.js';
|
|
20
|
+
|
|
21
|
+
// Command system
|
|
22
|
+
export { default as Command } from './Command.svelte.js';
|
|
23
|
+
export * from './Command.svelte.js';
|
|
24
|
+
|
|
25
|
+
// Keyboard handling
|
|
26
|
+
export { KeyMapper, define_keymap } from './KeyMapper.svelte.js';
|
|
27
|
+
|
|
28
|
+
// Transforms and utilities
|
|
29
|
+
export * from './transforms.svelte.js';
|
|
30
|
+
export * from './utils.js';
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { split_annotated_text, join_annotated_text, get_char_length } from './utils.js';
|
|
2
|
+
import { get_default_node_type } from './doc_utils.js';
|
|
3
|
+
|
|
4
|
+
export function break_text_node(tr) {
|
|
5
|
+
// Keep a reference of the original selection (before any transforms are applied)
|
|
6
|
+
const selection = tr.selection;
|
|
7
|
+
// First we need to ensure we have a text selection
|
|
8
|
+
if (selection.type !== 'text') return false;
|
|
9
|
+
|
|
10
|
+
// Next, we need to determine if the enclosing node is a pure text node (e.g. paragraph),
|
|
11
|
+
// which is wrapped inside a node_array (e.g. page.body)
|
|
12
|
+
|
|
13
|
+
// Owner of the text property (e.g. paragraph)
|
|
14
|
+
const node = tr.get(selection.path.slice(0, -1));
|
|
15
|
+
if (tr.kind(node) !== 'text') return false;
|
|
16
|
+
const is_inside_node_array = tr.inspect(selection.path.slice(0, -2))?.type === 'node_array';
|
|
17
|
+
// console.log('is_inside_node_array', is_inside_node_array);
|
|
18
|
+
if (!is_inside_node_array) return false; // Do nothing if we're not inside a node_array
|
|
19
|
+
const node_array_prop = selection.path.at(-3);
|
|
20
|
+
// console.log('node_array_prop', node_array_prop);
|
|
21
|
+
// Get the node that owns the node_array property (e.g. a page.body)
|
|
22
|
+
const node_array_node = tr.get(selection.path.slice(0, -3));
|
|
23
|
+
// console.log('node_array_node', $state.snapshot(node_array_node));
|
|
24
|
+
|
|
25
|
+
// Delete selection unless collapsed
|
|
26
|
+
if (selection.anchor_offset !== selection.focus_offset) {
|
|
27
|
+
tr.delete_selection();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const split_at_position = tr.selection.anchor_offset;
|
|
31
|
+
const content = tr.get(selection.path);
|
|
32
|
+
const [left_text, right_text] = split_annotated_text(content, split_at_position);
|
|
33
|
+
|
|
34
|
+
tr.set([node.id, 'content'], left_text);
|
|
35
|
+
|
|
36
|
+
const node_insert_position = {
|
|
37
|
+
type: 'node',
|
|
38
|
+
path: tr.selection.path.slice(0, -2),
|
|
39
|
+
anchor_offset: parseInt(tr.selection.path.at(-2), 10) + 1,
|
|
40
|
+
focus_offset: parseInt(tr.selection.path.at(-2), 10) + 1
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// TODO: Only use default_node_type when cursor is at the end of
|
|
44
|
+
const node_array_property_definition =
|
|
45
|
+
tr.schema[node_array_node.type].properties[node_array_prop];
|
|
46
|
+
const target_node_type = get_default_node_type(node_array_property_definition);
|
|
47
|
+
|
|
48
|
+
if (!target_node_type) {
|
|
49
|
+
console.warn(
|
|
50
|
+
'Cannot determine target node type for break_text_node - no default_ref_type and multiple node_types'
|
|
51
|
+
);
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
tr.set_selection(node_insert_position);
|
|
56
|
+
|
|
57
|
+
tr.config.inserters[target_node_type](tr, right_text);
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function join_text_node(tr) {
|
|
62
|
+
// Keep a reference of the original selection (before any transforms are applied)
|
|
63
|
+
const selection = tr.selection;
|
|
64
|
+
// First we need to ensure we have a text selection
|
|
65
|
+
if (selection.type !== 'text') return false;
|
|
66
|
+
|
|
67
|
+
const node = tr.get(selection.path.slice(0, -1));
|
|
68
|
+
if (tr.kind(node) !== 'text') return false;
|
|
69
|
+
const is_inside_node_array = tr.inspect(selection.path.slice(0, -2))?.type === 'node_array';
|
|
70
|
+
// console.log('is_inside_node_array', is_inside_node_array);
|
|
71
|
+
if (!is_inside_node_array) return false; // Do nothing if we're not inside a node_array
|
|
72
|
+
|
|
73
|
+
const node_index = parseInt(tr.selection.path.at(-2), 10);
|
|
74
|
+
|
|
75
|
+
// Determine if we can join with the previous node
|
|
76
|
+
let can_join = false;
|
|
77
|
+
let predecessor_node = null;
|
|
78
|
+
|
|
79
|
+
if (node_index > 0) {
|
|
80
|
+
const previous_text_path = [...tr.selection.path.slice(0, -2), node_index - 1];
|
|
81
|
+
predecessor_node = tr.get(previous_text_path);
|
|
82
|
+
can_join = tr.kind(predecessor_node) === 'text';
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Special behavior: if we can't join and current node is empty, delete it
|
|
86
|
+
if (!can_join && node.content.text === '') {
|
|
87
|
+
tr.set_selection({
|
|
88
|
+
type: 'node',
|
|
89
|
+
path: tr.selection.path.slice(0, -2),
|
|
90
|
+
anchor_offset: node_index,
|
|
91
|
+
focus_offset: node_index + 1
|
|
92
|
+
});
|
|
93
|
+
tr.delete_selection();
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// If we can't join for any reason, return false
|
|
98
|
+
if (!can_join) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Normal joining logic - both nodes are text nodes
|
|
103
|
+
const previous_text_path = [...tr.selection.path.slice(0, -2), node_index - 1];
|
|
104
|
+
const joined_text = join_annotated_text(predecessor_node.content, node.content);
|
|
105
|
+
|
|
106
|
+
// Calculate cursor position based on original predecessor content length
|
|
107
|
+
const cursor_position = get_char_length(predecessor_node.content.text);
|
|
108
|
+
|
|
109
|
+
// First set the joined content on the predecessor node (preserves annotations)
|
|
110
|
+
tr.set([predecessor_node.id, 'content'], joined_text);
|
|
111
|
+
|
|
112
|
+
// Then delete the current node
|
|
113
|
+
tr.set_selection({
|
|
114
|
+
type: 'node',
|
|
115
|
+
path: tr.selection.path.slice(0, -2),
|
|
116
|
+
anchor_offset: node_index,
|
|
117
|
+
focus_offset: node_index + 1
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
tr.delete_selection();
|
|
121
|
+
|
|
122
|
+
// Finally set the cursor position at the join point using the pre-calculated position
|
|
123
|
+
tr.set_selection({
|
|
124
|
+
type: 'text',
|
|
125
|
+
path: [...previous_text_path, 'content'],
|
|
126
|
+
anchor_offset: cursor_position,
|
|
127
|
+
focus_offset: cursor_position
|
|
128
|
+
});
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function insert_default_node(tr) {
|
|
133
|
+
const selection = tr.selection;
|
|
134
|
+
|
|
135
|
+
// Only work with collapsed node selections
|
|
136
|
+
if (selection?.type !== 'node' || selection.anchor_offset !== selection.focus_offset) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const path = selection.path;
|
|
141
|
+
const node_array_node = tr.get(path.slice(0, -1));
|
|
142
|
+
const property_name = path.at(-1);
|
|
143
|
+
|
|
144
|
+
// Get the definition for this property
|
|
145
|
+
const property_definition = tr.schema[node_array_node.type].properties[property_name];
|
|
146
|
+
const default_type = get_default_node_type(property_definition);
|
|
147
|
+
|
|
148
|
+
// Use the inserter function if available
|
|
149
|
+
if (tr.config?.inserters?.[default_type]) {
|
|
150
|
+
tr.config.inserters[default_type](tr);
|
|
151
|
+
return true;
|
|
152
|
+
} else {
|
|
153
|
+
throw new Error(`No inserter function available for default node type '${default_type}'`);
|
|
154
|
+
}
|
|
155
|
+
}
|