svedit 0.6.3 → 0.7.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 (65) hide show
  1. package/README.md +117 -45
  2. package/dist/AnnotatedTextProperty.svelte +19 -16
  3. package/dist/AnnotatedTextProperty.svelte.d.ts.map +1 -1
  4. package/dist/Command.svelte.d.ts +3 -3
  5. package/dist/Command.svelte.js +5 -5
  6. package/dist/CustomProperty.svelte +5 -4
  7. package/dist/Node.svelte +29 -14
  8. package/dist/Node.svelte.d.ts.map +1 -1
  9. package/dist/NodeArrayProperty.svelte +29 -9
  10. package/dist/NodeArrayProperty.svelte.d.ts.map +1 -1
  11. package/dist/NodeCaret.svelte +71 -0
  12. package/dist/NodeCaret.svelte.d.ts +27 -0
  13. package/dist/NodeCaret.svelte.d.ts.map +1 -0
  14. package/dist/NodeGap.svelte +249 -0
  15. package/dist/NodeGap.svelte.d.ts +20 -0
  16. package/dist/NodeGap.svelte.d.ts.map +1 -0
  17. package/dist/NodeGapMarkers.svelte +456 -0
  18. package/dist/NodeGapMarkers.svelte.d.ts +12 -0
  19. package/dist/NodeGapMarkers.svelte.d.ts.map +1 -0
  20. package/dist/NodeSelectionMarkers.svelte +49 -0
  21. package/dist/NodeSelectionMarkers.svelte.d.ts +7 -0
  22. package/dist/NodeSelectionMarkers.svelte.d.ts.map +1 -0
  23. package/dist/Session.svelte.d.ts +6 -6
  24. package/dist/Session.svelte.d.ts.map +1 -1
  25. package/dist/Session.svelte.js +13 -69
  26. package/dist/Svedit.svelte +158 -90
  27. package/dist/Svedit.svelte.d.ts.map +1 -1
  28. package/dist/Transaction.svelte.d.ts +1 -1
  29. package/dist/Transaction.svelte.d.ts.map +1 -1
  30. package/dist/Transaction.svelte.js +10 -70
  31. package/dist/doc_utils.d.ts +12 -0
  32. package/dist/doc_utils.d.ts.map +1 -1
  33. package/dist/doc_utils.js +61 -2
  34. package/dist/index.d.ts +4 -0
  35. package/dist/index.js +4 -0
  36. package/dist/node_gap_computation.svelte.d.ts +5 -0
  37. package/dist/node_gap_computation.svelte.d.ts.map +1 -0
  38. package/dist/node_gap_computation.svelte.js +621 -0
  39. package/dist/styles/svedit-colors.css +8 -0
  40. package/dist/transforms.svelte.d.ts.map +1 -1
  41. package/dist/transforms.svelte.js +7 -10
  42. package/dist/types.d.ts +56 -68
  43. package/dist/utils.d.ts +0 -3
  44. package/dist/utils.d.ts.map +1 -1
  45. package/dist/utils.js +10 -10
  46. package/package.json +21 -21
  47. package/src/lib/AnnotatedTextProperty.svelte +19 -16
  48. package/src/lib/Command.svelte.js +5 -5
  49. package/src/lib/CustomProperty.svelte +5 -4
  50. package/src/lib/Node.svelte +29 -14
  51. package/src/lib/NodeArrayProperty.svelte +29 -9
  52. package/src/lib/NodeCaret.svelte +71 -0
  53. package/src/lib/NodeGap.svelte +249 -0
  54. package/src/lib/NodeGapMarkers.svelte +456 -0
  55. package/src/lib/NodeSelectionMarkers.svelte +49 -0
  56. package/src/lib/Session.svelte.js +13 -69
  57. package/src/lib/Svedit.svelte +158 -90
  58. package/src/lib/Transaction.svelte.js +10 -70
  59. package/src/lib/doc_utils.js +61 -2
  60. package/src/lib/index.js +4 -0
  61. package/src/lib/node_gap_computation.svelte.js +621 -0
  62. package/src/lib/styles/svedit-colors.css +8 -0
  63. package/src/lib/transforms.svelte.js +7 -10
  64. package/src/lib/types.d.ts +56 -68
  65. package/src/lib/utils.js +10 -10
@@ -7,7 +7,7 @@
7
7
  * @import { NodeId, DocumentPath, PrimitiveType, NodeProperty, NodeArrayProperty, NodeSchema, DocumentSchema, Selection, Annotation, Document } from './types'
8
8
  */
9
9
 
10
- import { get_selection_range } from './utils.js';
10
+ import { get_selection_range, get_char_length } from './utils.js';
11
11
 
12
12
  /**
13
13
  * Identity function — keeps schema at runtime & makes IDE infer types.
@@ -119,7 +119,7 @@ function validate_primitive_value(type, value) {
119
119
  }
120
120
 
121
121
  /**
122
- * @param {String} id
122
+ * @param {string} id
123
123
  * @returns {boolean}
124
124
  */
125
125
  function is_id_valid(id) {
@@ -480,3 +480,62 @@ export function count_references_excluding_deleted(schema, doc, target_node_id,
480
480
 
481
481
  return count;
482
482
  }
483
+
484
+ /**
485
+ * Validates a selection against the current document state.
486
+ * Works with any object that implements get() and inspect() (Session or Transaction).
487
+ *
488
+ * @param {Selection} selection - The selection to validate
489
+ * @param {{ get: Function, inspect: Function }} session_or_transaction - A Session or Transaction instance
490
+ * @throws {Error} Throws if the selection is invalid
491
+ */
492
+ export function validate_selection(selection, session_or_transaction) {
493
+ if (!selection) return;
494
+
495
+ const selection_type = selection.type;
496
+ if (!['node', 'text', 'property'].includes(selection_type)) {
497
+ throw new Error(`Invalid selection type: ${selection_type}`);
498
+ }
499
+
500
+ if (selection_type === 'node') {
501
+ const node_array = session_or_transaction.get(selection.path);
502
+
503
+ if (!Array.isArray(node_array)) {
504
+ throw new Error('Node selection path must point to a node_array');
505
+ }
506
+
507
+ const max_offset = node_array.length;
508
+ if (selection.anchor_offset < 0 || selection.anchor_offset > max_offset) {
509
+ throw new Error(
510
+ `Node selection anchor_offset (${selection.anchor_offset}) is out of bounds. Max is ${max_offset}.`
511
+ );
512
+ }
513
+ if (selection.focus_offset < 0 || selection.focus_offset > max_offset) {
514
+ throw new Error(
515
+ `Node selection focus_offset (${selection.focus_offset}) is out of bounds. Max is ${max_offset}.`
516
+ );
517
+ }
518
+ } else if (selection_type === 'text') {
519
+ const annotated_text = session_or_transaction.get(selection.path);
520
+
521
+ if (!annotated_text || typeof annotated_text.text !== 'string') {
522
+ throw new Error('Text selection path must point to annotated_text');
523
+ }
524
+
525
+ const char_length = get_char_length(annotated_text.text);
526
+ if (selection.anchor_offset < 0 || selection.anchor_offset > char_length) {
527
+ throw new Error(
528
+ `Text selection anchor_offset (${selection.anchor_offset}) is out of bounds. Max is ${char_length}.`
529
+ );
530
+ }
531
+ if (selection.focus_offset < 0 || selection.focus_offset > char_length) {
532
+ throw new Error(
533
+ `Text selection focus_offset (${selection.focus_offset}) is out of bounds. Max is ${char_length}.`
534
+ );
535
+ }
536
+ } else if (selection_type === 'property') {
537
+ if (!session_or_transaction.inspect(selection.path)) {
538
+ throw new Error(`Property selection path not found: ${selection.path.join('.')}`);
539
+ }
540
+ }
541
+ }
package/src/lib/index.js CHANGED
@@ -4,6 +4,10 @@ export { default as AnnotatedTextProperty } from './AnnotatedTextProperty.svelte
4
4
  export { default as CustomProperty } from './CustomProperty.svelte';
5
5
  export { default as Node } from './Node.svelte';
6
6
  export { default as NodeArrayProperty } from './NodeArrayProperty.svelte';
7
+ export { default as NodeGap } from './NodeGap.svelte';
8
+ export { default as NodeGapMarkers } from './NodeGapMarkers.svelte';
9
+ export { default as NodeCaret } from './NodeCaret.svelte';
10
+ export { default as NodeSelectionMarkers } from './NodeSelectionMarkers.svelte';
7
11
 
8
12
  // Core classes and utilities
9
13
  export { default as Session } from './Session.svelte.js';