reptree 0.7.0 → 0.8.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/dist/index.cjs CHANGED
@@ -26,11 +26,17 @@ __export(index_exports, {
26
26
  Vertex: () => Vertex,
27
27
  VertexState: () => VertexState,
28
28
  bindVertex: () => bindVertex,
29
+ compareOpId: () => compareOpId,
30
+ createOpId: () => createOpId,
31
+ equalsOpId: () => equalsOpId,
29
32
  isAnyPropertyOp: () => isAnyPropertyOp,
30
33
  isMoveVertexOp: () => isMoveVertexOp,
34
+ isOpIdGreaterThan: () => isOpIdGreaterThan,
31
35
  newMoveVertexOp: () => newMoveVertexOp,
32
36
  newSetTransientVertexPropertyOp: () => newSetTransientVertexPropertyOp,
33
37
  newSetVertexPropertyOp: () => newSetVertexPropertyOp,
38
+ opIdToString: () => opIdToString,
39
+ tryParseOpIdStr: () => tryParseOpIdStr,
34
40
  uuid: () => uuid
35
41
  });
36
42
  module.exports = __toCommonJS(index_exports);
@@ -501,18 +507,29 @@ function bindVertex(tree, id, schemaOrOptions) {
501
507
  // src/Vertex.ts
502
508
  var Vertex = class _Vertex {
503
509
  constructor(tree, state) {
504
- this.tree = tree;
505
510
  this.state = state;
511
+ this._tree = tree;
506
512
  }
513
+ /** Returns the tree this vertex belongs to. */
514
+ get tree() {
515
+ return this._tree;
516
+ }
517
+ set tree(value) {
518
+ this._tree = value;
519
+ }
520
+ /** Returns the ID of this vertex. */
507
521
  get id() {
508
522
  return this.state.id;
509
523
  }
524
+ /** Returns the name of this vertex. The name is stored as a property with the key 'name'. */
510
525
  get name() {
511
526
  return this.getProperty("name");
512
527
  }
528
+ /** Sets the name of this vertex. The name is stored as a property with the key 'name'. */
513
529
  set name(name) {
514
530
  this.tree.setVertexProperty(this.id, "name", name);
515
531
  }
532
+ /** Returns the creation date of this vertex. The creation date is stored as a property with the key '_c'. */
516
533
  get createdAt() {
517
534
  const createdAt = this.getProperty("_c");
518
535
  if (!createdAt) {
@@ -520,30 +537,51 @@ var Vertex = class _Vertex {
520
537
  }
521
538
  return new Date(createdAt);
522
539
  }
523
- get path() {
524
- throw new Error("Not implemented");
525
- }
540
+ /** Returns the ID of the parent vertex of this vertex. */
526
541
  get parentId() {
527
542
  return this.state.parentId;
528
543
  }
544
+ /** Returns the parent vertex of this vertex. */
529
545
  get parent() {
530
546
  if (!this.parentId) {
531
547
  return void 0;
532
548
  }
533
549
  return this.tree.getVertex(this.parentId);
534
550
  }
551
+ /** Returns the children vertices of this vertex. */
535
552
  get children() {
536
553
  return this.tree.getChildren(this.id);
537
554
  }
555
+ /** Returns the IDs of the children vertices of this vertex. */
538
556
  get childrenIds() {
539
557
  return this.tree.getChildrenIds(this.id);
540
558
  }
559
+ /** Returns the ancestors of this vertex. The first element is the root vertex.
560
+ * E.g root -> grandparent -> parent.
561
+ * Doesn't include this vertex in the array.
562
+ */
563
+ get ancestors() {
564
+ return this.tree.getAncestors(this.id);
565
+ }
566
+ /** Returns the ID of the root vertex of the tree this vertex belongs to. */
567
+ get treeId() {
568
+ return this.root.id;
569
+ }
570
+ /** Returns the root vertex of the tree this vertex belongs to. */
571
+ get root() {
572
+ const root = this.tree.root;
573
+ if (!root) {
574
+ throw new Error("Root vertex of the tree is not set");
575
+ }
576
+ return root;
577
+ }
541
578
  getAsTypedObject() {
542
579
  return this.getProperties();
543
580
  }
544
581
  getChildrenAsTypedArray() {
545
582
  return this.children.map((v) => v.getAsTypedObject());
546
583
  }
584
+ /** Creates a new child vertex of this vertex. */
547
585
  newChild(props) {
548
586
  if (props && typeof props === "object" && "children" in props) {
549
587
  throw new Error("Passing children inside props is not supported at the moment");
@@ -551,6 +589,7 @@ var Vertex = class _Vertex {
551
589
  const normalized = _Vertex.normalizePropsForCreation(props);
552
590
  return this.tree.newVertex(this.id, normalized);
553
591
  }
592
+ /** Creates a new named child vertex of this vertex. */
554
593
  newNamedChild(name, props) {
555
594
  if (props && typeof props === "object" && "children" in props) {
556
595
  throw new Error("Passing children inside props is not supported at the moment");
@@ -558,6 +597,7 @@ var Vertex = class _Vertex {
558
597
  const normalized = _Vertex.normalizePropsForCreation(props);
559
598
  return this.tree.newNamedVertex(this.id, name, normalized);
560
599
  }
600
+ /** Sets a property on this vertex. */
561
601
  setProperty(key, value) {
562
602
  const existingValue = this.getProperty(key, false);
563
603
  if (existingValue === value) {
@@ -565,6 +605,7 @@ var Vertex = class _Vertex {
565
605
  }
566
606
  this.tree.setVertexProperty(this.id, key, value);
567
607
  }
608
+ /** Sets a transient property on this vertex. Transient properties are not persisted to the tree and are not included in the state vector. */
568
609
  setTransientProperty(key, value) {
569
610
  const existingValue = this.getProperty(key);
570
611
  if (existingValue === value) {
@@ -572,17 +613,21 @@ var Vertex = class _Vertex {
572
613
  }
573
614
  this.tree.setTransientVertexProperty(this.id, key, value);
574
615
  }
616
+ /** Promotes all transient (temporary) properties to persistent properties. */
575
617
  commitTransients() {
576
618
  this.tree.commitTransients(this.id);
577
619
  }
620
+ /** Sets multiple properties on this vertex. */
578
621
  setProperties(props) {
579
622
  for (const [key, value] of Object.entries(props)) {
580
623
  this.setProperty(key, value);
581
624
  }
582
625
  }
626
+ /** Returns the value of a property on this vertex. */
583
627
  getProperty(key, includingTransient = true) {
584
628
  return this.tree.getVertexProperty(this.id, key, includingTransient);
585
629
  }
630
+ /** Returns all properties on this vertex. */
586
631
  getProperties() {
587
632
  const props = {};
588
633
  this.tree.getVertexProperties(this.id).forEach((p) => {
@@ -602,10 +647,12 @@ var Vertex = class _Vertex {
602
647
  findAllTypedChildrenWithProperty(key, value) {
603
648
  return this.findAllChildrenWithProperty(key, value).map((c) => c.getAsTypedObject());
604
649
  }
650
+ /** Observes changes to this vertex. */
605
651
  observe(listener) {
606
652
  const unobserve = this.tree.observe(this.id, listener);
607
653
  return () => unobserve();
608
654
  }
655
+ /** Observes changes to the children of this vertex. */
609
656
  observeChildren(listener) {
610
657
  const unobserve = this.tree.observe(this.id, (events) => {
611
658
  if (events.some((e) => e.type === "children")) {
@@ -935,7 +982,6 @@ var _RepTree = class _RepTree {
935
982
  this.setPropertyOps = [];
936
983
  this.propertiesAndTheirOpIds = /* @__PURE__ */ new Map();
937
984
  this.transientPropertiesAndTheirOpIds = /* @__PURE__ */ new Map();
938
- // Observers for non-structural properties are not used
939
985
  this.localOps = [];
940
986
  this.pendingMovesWithMissingParent = /* @__PURE__ */ new Map();
941
987
  this.pendingPropertiesWithMissingVertex = /* @__PURE__ */ new Map();
@@ -1000,6 +1046,7 @@ var _RepTree = class _RepTree {
1000
1046
  getChildrenIds(vertexId) {
1001
1047
  return this.state.getChildrenIds(vertexId);
1002
1048
  }
1049
+ /** Returns the ancestors of the given vertex. The first element is the root vertex. */
1003
1050
  getAncestors(vertexId) {
1004
1051
  const ancestors = [];
1005
1052
  let currentVertex = this.state.getVertex(vertexId);
@@ -1555,11 +1602,17 @@ var RepTree = _RepTree;
1555
1602
  Vertex,
1556
1603
  VertexState,
1557
1604
  bindVertex,
1605
+ compareOpId,
1606
+ createOpId,
1607
+ equalsOpId,
1558
1608
  isAnyPropertyOp,
1559
1609
  isMoveVertexOp,
1610
+ isOpIdGreaterThan,
1560
1611
  newMoveVertexOp,
1561
1612
  newSetTransientVertexPropertyOp,
1562
1613
  newSetVertexPropertyOp,
1614
+ opIdToString,
1615
+ tryParseOpIdStr,
1563
1616
  uuid
1564
1617
  });
1565
1618
  //# sourceMappingURL=index.cjs.map