svedit 0.8.0 → 0.10.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 (68) hide show
  1. package/README.md +137 -4
  2. package/dist/AnnotatedTextProperty.svelte +41 -8
  3. package/dist/AnnotatedTextProperty.svelte.d.ts.map +1 -1
  4. package/dist/Command.svelte.d.ts +0 -1
  5. package/dist/Command.svelte.d.ts.map +1 -1
  6. package/dist/Command.svelte.js +18 -3
  7. package/dist/CustomProperty.svelte +20 -9
  8. package/dist/CustomProperty.svelte.d.ts +2 -1
  9. package/dist/CustomProperty.svelte.d.ts.map +1 -1
  10. package/dist/Node.svelte +7 -5
  11. package/dist/Node.svelte.d.ts.map +1 -1
  12. package/dist/NodeArrayProperty.svelte +65 -42
  13. package/dist/NodeArrayProperty.svelte.d.ts.map +1 -1
  14. package/dist/NodeCaret.svelte +0 -7
  15. package/dist/NodeGap.svelte +179 -81
  16. package/dist/NodeGap.svelte.d.ts +0 -2
  17. package/dist/NodeGap.svelte.d.ts.map +1 -1
  18. package/dist/NodeGapMarkers.svelte +251 -84
  19. package/dist/NodeGapMarkers.svelte.d.ts +2 -0
  20. package/dist/NodeGapMarkers.svelte.d.ts.map +1 -1
  21. package/dist/NodeSelectionMarkers.svelte +4 -3
  22. package/dist/NodeSelectionMarkers.svelte.d.ts.map +1 -1
  23. package/dist/Session.svelte.d.ts +12 -1
  24. package/dist/Session.svelte.d.ts.map +1 -1
  25. package/dist/Session.svelte.js +40 -3
  26. package/dist/Svedit.svelte +340 -147
  27. package/dist/Svedit.svelte.d.ts.map +1 -1
  28. package/dist/Transaction.svelte.d.ts +24 -5
  29. package/dist/Transaction.svelte.d.ts.map +1 -1
  30. package/dist/Transaction.svelte.js +103 -61
  31. package/dist/doc_utils.d.ts +64 -3
  32. package/dist/doc_utils.d.ts.map +1 -1
  33. package/dist/doc_utils.js +276 -10
  34. package/dist/index.d.ts +1 -1
  35. package/dist/index.js +5 -1
  36. package/dist/node_visibility.svelte.d.ts +9 -0
  37. package/dist/node_visibility.svelte.d.ts.map +1 -0
  38. package/dist/node_visibility.svelte.js +718 -0
  39. package/dist/styles/svedit-animations.css +8 -0
  40. package/dist/styles/svedit-colors.css +1 -0
  41. package/dist/types.d.ts +46 -3
  42. package/dist/utils.d.ts +52 -0
  43. package/dist/utils.d.ts.map +1 -1
  44. package/dist/utils.js +109 -3
  45. package/package.json +3 -2
  46. package/src/lib/AnnotatedTextProperty.svelte +41 -8
  47. package/src/lib/Command.svelte.js +18 -3
  48. package/src/lib/CustomProperty.svelte +20 -9
  49. package/src/lib/Node.svelte +7 -5
  50. package/src/lib/NodeArrayProperty.svelte +65 -42
  51. package/src/lib/NodeCaret.svelte +0 -7
  52. package/src/lib/NodeGap.svelte +179 -81
  53. package/src/lib/NodeGapMarkers.svelte +251 -84
  54. package/src/lib/NodeSelectionMarkers.svelte +4 -3
  55. package/src/lib/Session.svelte.js +40 -3
  56. package/src/lib/Svedit.svelte +340 -147
  57. package/src/lib/Transaction.svelte.js +103 -61
  58. package/src/lib/doc_utils.js +276 -10
  59. package/src/lib/index.js +5 -1
  60. package/src/lib/node_visibility.svelte.js +718 -0
  61. package/src/lib/styles/svedit-animations.css +8 -0
  62. package/src/lib/styles/svedit-colors.css +1 -0
  63. package/src/lib/types.d.ts +46 -3
  64. package/src/lib/utils.js +109 -3
  65. package/dist/node_gap_computation.svelte.d.ts +0 -5
  66. package/dist/node_gap_computation.svelte.d.ts.map +0 -1
  67. package/dist/node_gap_computation.svelte.js +0 -621
  68. package/src/lib/node_gap_computation.svelte.js +0 -621
@@ -0,0 +1,8 @@
1
+ @layer svedit-defaults {
2
+ @keyframes node-caret-blink {
3
+ 0%, 60% { opacity: 1; }
4
+ 68% { opacity: 0; }
5
+ 88% { opacity: 0; }
6
+ 100% { opacity: 1; }
7
+ }
8
+ }
@@ -4,5 +4,6 @@
4
4
  --svedit-editing-stroke: var(--svedit-brand);
5
5
  --svedit-editing-fill: oklch(from var(--svedit-brand) l c h / 0.1);
6
6
  --svedit-canvas-stroke: color-mix(in oklch, currentColor 25%, transparent);
7
+ --svedit-caret-color: var(--svedit-brand);
7
8
  }
8
9
  }
@@ -11,12 +11,17 @@ import type { Component, Snippet } from 'svelte';
11
11
  // ===== SELECTION TYPE DEFINITIONS =====
12
12
 
13
13
  /**
14
- * A unique node identifier (ideally UUID)
14
+ * A unique node identifier.
15
+ *
16
+ * Node ids must be valid Svedit path string segments: they must start with
17
+ * a letter or underscore, contain only letters, numbers, underscores, or
18
+ * dashes, and must not contain `__`.
15
19
  */
16
20
  export type NodeId = string;
17
21
 
18
22
  /**
19
- * Array of IDs, property names (strings), or indexes (integers) that identify a node or property in the document
23
+ * Array of IDs, property names (strings), or indexes (integers) that identify a node or property in the document.
24
+ * String segments must follow the same path segment rules as NodeId.
20
25
  */
21
26
  export type DocumentPath = Array<string | number>;
22
27
 
@@ -269,7 +274,8 @@ export type PropertyDefinition =
269
274
  export type NodeKind = 'document' | 'block' | 'text' | 'annotation';
270
275
 
271
276
  /**
272
- * Schema for text nodes - must have a content property of type annotated_text
277
+ * Schema for text nodes - must have a content property of type annotated_text.
278
+ * Use define_document_schema to also check that content is the only annotated_text property.
273
279
  */
274
280
  export type TextNodeSchema = {
275
281
  kind: 'text';
@@ -278,6 +284,43 @@ export type TextNodeSchema = {
278
284
  } & Record<string, PropertyDefinition>;
279
285
  };
280
286
 
287
+ export type AnnotatedTextPropertyNames<Properties> = {
288
+ [PropertyName in keyof Properties]: Properties[PropertyName] extends { type: 'annotated_text' }
289
+ ? PropertyName
290
+ : never;
291
+ }[keyof Properties];
292
+
293
+ export type TextNodeSchemaError<Message extends string> = {
294
+ [SchemaError in `Svedit schema error: ${Message}`]: never;
295
+ };
296
+
297
+ export type TextNodeMissingContentError = TextNodeSchemaError<
298
+ 'Text node schemas must define a "content" property of type annotated_text.'
299
+ >;
300
+
301
+ export type TextNodeExtraAnnotatedTextError<ExtraProperty extends string> = TextNodeSchemaError<
302
+ `Text node schemas must not define annotated_text property "${ExtraProperty}". Use "content" as the only annotated_text property.`
303
+ >;
304
+
305
+ export type ValidateTextNodeSchema<Schema> = Schema extends {
306
+ kind: 'text';
307
+ properties: infer Properties;
308
+ }
309
+ ? string extends keyof Properties
310
+ ? Schema
311
+ : Properties extends { content: AnnotatedTextProperty }
312
+ ? Exclude<AnnotatedTextPropertyNames<Properties>, 'content'> extends infer ExtraProperties
313
+ ? [ExtraProperties] extends [never]
314
+ ? Schema
315
+ : TextNodeExtraAnnotatedTextError<Extract<ExtraProperties, string>>
316
+ : never
317
+ : TextNodeMissingContentError
318
+ : Schema;
319
+
320
+ export type ValidateDocumentSchema<Schema extends Record<string, NodeSchema>> = {
321
+ [NodeType in keyof Schema]: ValidateTextNodeSchema<Schema[NodeType]>;
322
+ };
323
+
281
324
  /**
282
325
  * Schema for non-text nodes
283
326
  */
package/src/lib/utils.js CHANGED
@@ -21,7 +21,7 @@ export function is_mobile_browser() {
21
21
  );
22
22
  }
23
23
 
24
- // ‼️‼️‼️‼️‼️‼️ UNUSED UTILITY BELOW ‼️‼️‼️‼️‼️‼️
24
+ // ‼️‼️‼️‼️‼️‼️ UNUSED UTILITY BELOW ‼️‼️‼️‼️‼️‼️
25
25
  /**
26
26
  * Detect if the current browser is Chrome on desktop
27
27
  * @returns {boolean} true if Chrome desktop browser, false otherwise
@@ -54,8 +54,7 @@ export function get_char_length(str) {
54
54
  return [...SEGMENTER.segment(str)].length;
55
55
  }
56
56
 
57
-
58
- // ‼️‼️‼️‼️‼️‼️ UNUSED UTILITY BELOW ‼️‼️‼️‼️‼️‼️
57
+ // ‼️‼️‼️‼️‼️‼️ UNUSED UTILITY BELOW ‼️‼️‼️‼️‼️‼️
59
58
  /**
60
59
  * Get a single character at the specified position (accounting for multi-byte characters)
61
60
  *
@@ -278,6 +277,113 @@ export function snake_to_pascal(str) {
278
277
  .join('');
279
278
  }
280
279
 
280
+ export const PATH_SEPARATOR = '__';
281
+ const PATH_STRING_SEGMENT_RE = /^[A-Za-z_][A-Za-z0-9_-]*$/;
282
+ const PATH_INDEX_SEGMENT_RE = /^(0|[1-9]\d*)$/;
283
+
284
+ /**
285
+ * Check whether a string can be used as a Svedit path segment.
286
+ *
287
+ * Path string segments include node ids and schema property names. Keeping
288
+ * them restricted makes serialized paths reversible, valid as HTML ids, and
289
+ * safe to use as CSS anchor-name suffixes.
290
+ *
291
+ * @param {string} segment
292
+ * @returns {boolean}
293
+ */
294
+ export function is_path_string_segment_valid(segment) {
295
+ return (
296
+ typeof segment === 'string' &&
297
+ PATH_STRING_SEGMENT_RE.test(segment) &&
298
+ !segment.includes(PATH_SEPARATOR)
299
+ );
300
+ }
301
+
302
+ /**
303
+ * Assert that a string can be used as a Svedit path segment.
304
+ *
305
+ * @param {string} segment
306
+ * @param {string} [label]
307
+ * @returns {void}
308
+ */
309
+ export function assert_path_string_segment(segment, label = 'Path segment') {
310
+ if (!is_path_string_segment_valid(segment)) {
311
+ throw new Error(
312
+ `${label} must start with a letter or underscore and contain only letters, numbers, underscores, or dashes. It must not contain "${PATH_SEPARATOR}".`
313
+ );
314
+ }
315
+ }
316
+
317
+ /**
318
+ * Serialize a document path while preserving whether each segment is a
319
+ * property/key string or an array index number.
320
+ *
321
+ * The result is reversible and safe to use as a data attribute value, HTML id,
322
+ * or CSS anchor-name suffix.
323
+ *
324
+ * @param {Array<string | number>} path
325
+ * @returns {string}
326
+ *
327
+ * @example
328
+ * serialize_path(['page_1', 'body', 0, 'items']) // Returns: 'page_1__body__0__items'
329
+ */
330
+ export function serialize_path(path) {
331
+ return path
332
+ .map((segment) => {
333
+ if (typeof segment === 'number') {
334
+ if (!Number.isInteger(segment) || segment < 0) {
335
+ throw new Error(`Path index must be a non-negative integer: ${segment}`);
336
+ }
337
+ return String(segment);
338
+ }
339
+
340
+ assert_path_string_segment(segment);
341
+ return segment;
342
+ })
343
+ .join(PATH_SEPARATOR);
344
+ }
345
+
346
+ /**
347
+ * Deserialize a path string produced by serialize_path.
348
+ *
349
+ * @param {string} serialized_path
350
+ * @returns {Array<string | number>}
351
+ *
352
+ * @example
353
+ * deserialize_path('page_1__body__0__items') // Returns: ['page_1', 'body', 0, 'items']
354
+ */
355
+ export function deserialize_path(serialized_path) {
356
+ if (serialized_path === '') return [];
357
+
358
+ return serialized_path.split(PATH_SEPARATOR).map((segment) => {
359
+ if (segment === '') {
360
+ throw new Error(`Invalid serialized path: ${serialized_path}`);
361
+ }
362
+
363
+ if (/^\d+$/.test(segment)) {
364
+ if (!PATH_INDEX_SEGMENT_RE.test(segment)) {
365
+ throw new Error(`Invalid serialized path index: ${segment}`);
366
+ }
367
+ return Number(segment);
368
+ }
369
+
370
+ assert_path_string_segment(segment, 'Serialized path segment');
371
+ return segment;
372
+ });
373
+ }
374
+
375
+ /**
376
+ * Compare two document paths segment-by-segment, preserving segment types.
377
+ *
378
+ * @param {Array<string | number>} a
379
+ * @param {Array<string | number>} b
380
+ * @returns {boolean}
381
+ */
382
+ export function paths_equal(a, b) {
383
+ if (a.length !== b.length) return false;
384
+ return a.every((segment, index) => segment === b[index]);
385
+ }
386
+
281
387
  export function traverse(node_id, schema, nodes) {
282
388
  const json = [];
283
389
  const visited = {};
@@ -1,5 +0,0 @@
1
- /**
2
- * @param {object} svedit - Svedit context (session, editable, doc, schema, etc.)
3
- */
4
- export function create_gap_computation(svedit: object): void;
5
- //# sourceMappingURL=node_gap_computation.svelte.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"node_gap_computation.svelte.d.ts","sourceRoot":"","sources":["../src/lib/node_gap_computation.svelte.js"],"names":[],"mappings":"AAkWA;;GAEG;AACH,+CAFW,MAAM,QAyQhB"}