@yorkie-js/react 0.6.8 → 0.6.9

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.
@@ -5,7 +5,7 @@ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
5
5
  import { jsx } from "react/jsx-runtime";
6
6
  import { createContext, useMemo, useState, useEffect, useContext, useCallback } from "react";
7
7
  const name$1 = "@yorkie-js/react";
8
- const version$1 = "0.6.8";
8
+ const version$1 = "0.6.9";
9
9
  const pkg$1 = {
10
10
  name: name$1,
11
11
  version: version$1
@@ -11730,6 +11730,18 @@ class IndexTreeNode {
11730
11730
  hasTextChild() {
11731
11731
  return this.children.length > 0 && this.children.every((child) => child.isText);
11732
11732
  }
11733
+ /**
11734
+ * `getChildrenText` returns text value of all text type children.
11735
+ */
11736
+ getChildrenText() {
11737
+ if (this.isText) {
11738
+ return this.value;
11739
+ }
11740
+ if (this.hasTextChild()) {
11741
+ return this.children.map((child) => child.value).join("");
11742
+ }
11743
+ return "";
11744
+ }
11733
11745
  /**
11734
11746
  * `append` appends the given nodes to the children.
11735
11747
  */
@@ -12928,7 +12940,7 @@ class CRDTTreeNode extends IndexTreeNode {
12928
12940
  return pairs;
12929
12941
  }
12930
12942
  }
12931
- function toTreeNode(node) {
12943
+ function toTreeNode$1(node) {
12932
12944
  var _a2;
12933
12945
  if (node.isText) {
12934
12946
  const currentNode = node;
@@ -12939,7 +12951,7 @@ function toTreeNode(node) {
12939
12951
  }
12940
12952
  const treeNode = {
12941
12953
  type: node.type,
12942
- children: node.children.map(toTreeNode)
12954
+ children: node.children.map(toTreeNode$1)
12943
12955
  };
12944
12956
  if (node.attrs) {
12945
12957
  treeNode.attributes = parseObjectValues((_a2 = node.attrs) == null ? void 0 : _a2.toObject());
@@ -13283,7 +13295,7 @@ class CRDTTree extends CRDTElement {
13283
13295
  }
13284
13296
  }
13285
13297
  if (aliveContents.length) {
13286
- const value = aliveContents.map((content) => toTreeNode(content));
13298
+ const value = aliveContents.map((content) => toTreeNode$1(content));
13287
13299
  if (changes.length && changes[changes.length - 1].from === fromIdx) {
13288
13300
  changes[changes.length - 1].value = value;
13289
13301
  } else {
@@ -13325,6 +13337,12 @@ class CRDTTree extends CRDTElement {
13325
13337
  `not implemented: ${target}, ${source}, ${ticket}`
13326
13338
  );
13327
13339
  }
13340
+ /**
13341
+ * `pathToTreePos` converts the given path of the node to the TreePos.
13342
+ */
13343
+ pathToTreePos(path) {
13344
+ return this.indexTree.pathToTreePos(path);
13345
+ }
13328
13346
  /**
13329
13347
  * `purge` physically purges the given node.
13330
13348
  */
@@ -13494,7 +13512,7 @@ class CRDTTree extends CRDTElement {
13494
13512
  * `getRootTreeNode` returns the converted value of this tree to TreeNode.
13495
13513
  */
13496
13514
  getRootTreeNode() {
13497
- return toTreeNode(this.indexTree.getRoot());
13515
+ return toTreeNode$1(this.indexTree.getRoot());
13498
13516
  }
13499
13517
  /**
13500
13518
  * `toTestTreeNode` returns the JSON of this tree for debugging.
@@ -18369,6 +18387,81 @@ class Counter {
18369
18387
  return this.counter.toJSForTest();
18370
18388
  }
18371
18389
  }
18390
+ function toTreeNode(node) {
18391
+ if (node.isText) {
18392
+ return {
18393
+ type: node.type,
18394
+ value: node.value
18395
+ };
18396
+ }
18397
+ const newNode = {
18398
+ type: node.type,
18399
+ children: node.children.map((child) => toTreeNode(child))
18400
+ };
18401
+ if (node.attrs) {
18402
+ newNode.attributes = parseObjectValues(node.attrs.toObject());
18403
+ }
18404
+ return newNode;
18405
+ }
18406
+ function createSplitNode(node, offset) {
18407
+ const newNode = {
18408
+ type: node.isText ? node.parent.type : node.type,
18409
+ children: node.isText ? node.parent.getChildrenText().length === offset ? [] : [
18410
+ {
18411
+ type: DefaultTextType,
18412
+ value: node.parent.getChildrenText().slice(offset)
18413
+ }
18414
+ ] : node.children.slice(offset).map((child) => toTreeNode(child))
18415
+ };
18416
+ if (node.attrs) {
18417
+ newNode.attributes = parseObjectValues(node.attrs.toObject());
18418
+ } else if (node.isText && node.parent.attrs) {
18419
+ newNode.attributes = parseObjectValues(node.parent.attrs.toObject());
18420
+ }
18421
+ return newNode;
18422
+ }
18423
+ function separateSplit(treePos, path) {
18424
+ const { node } = treePos;
18425
+ const parentPath = [...path].slice(0, -1);
18426
+ const last = node.isText ? node.parent.getChildrenText().length : node.children.length;
18427
+ const toPath = [...parentPath, last];
18428
+ const insertPath = [...parentPath];
18429
+ const res = [];
18430
+ insertPath[insertPath.length - 1] += 1;
18431
+ if (!path.every((v, i) => v === toPath[i])) {
18432
+ res.push({ fromPath: [...path], toPath });
18433
+ }
18434
+ const newNode = createSplitNode(node, path[path.length - 1]);
18435
+ if (newNode) {
18436
+ res.push({ fromPath: insertPath, toPath: insertPath, content: newNode });
18437
+ }
18438
+ return res;
18439
+ }
18440
+ function separateMerge(treePos, path) {
18441
+ const { node: parentNode, offset } = treePos;
18442
+ const node = parentNode.children[offset];
18443
+ const leftSiblingNode = parentNode.children[offset - 1];
18444
+ const { children } = node;
18445
+ const parentPath = [...path].slice(0, -1);
18446
+ const res = [
18447
+ { fromPath: [...path], toPath: [...parentPath, offset + 1] }
18448
+ ];
18449
+ if (!node.children.length) {
18450
+ return res;
18451
+ }
18452
+ const insertPath = [
18453
+ ...parentPath,
18454
+ offset - 1,
18455
+ leftSiblingNode.hasTextChild() ? leftSiblingNode.getChildrenText().length : leftSiblingNode.children.length
18456
+ ];
18457
+ const nodes = children.map((child) => toTreeNode(child));
18458
+ res.push({
18459
+ fromPath: insertPath,
18460
+ toPath: insertPath,
18461
+ content: nodes
18462
+ });
18463
+ return res;
18464
+ }
18372
18465
  function buildDescendants(treeNode, parent, context) {
18373
18466
  const { type } = treeNode;
18374
18467
  const ticket = context.issueTimeTicket();
@@ -18549,6 +18642,62 @@ class Tree {
18549
18642
  }
18550
18643
  return this.tree.getIndexTree();
18551
18644
  }
18645
+ /**
18646
+ * `splitByPath` splits the tree by the given path.
18647
+ */
18648
+ splitByPath(path) {
18649
+ if (!this.context || !this.tree) {
18650
+ throw new YorkieError(
18651
+ Code.ErrNotInitialized,
18652
+ "Tree is not initialized yet"
18653
+ );
18654
+ }
18655
+ if (!path.length) {
18656
+ throw new YorkieError(
18657
+ Code.ErrInvalidArgument,
18658
+ "path should not be empty"
18659
+ );
18660
+ }
18661
+ const treePos = this.tree.pathToTreePos(path);
18662
+ const commands = separateSplit(treePos, path);
18663
+ for (const command of commands) {
18664
+ const { fromPath, toPath, content } = command;
18665
+ const fromPos = this.tree.pathToPos(fromPath);
18666
+ const toPos = this.tree.pathToPos(toPath);
18667
+ this.editInternal(fromPos, toPos, content ? [content] : [], 0);
18668
+ }
18669
+ }
18670
+ /**
18671
+ * `mergeByPath` merges the tree by the given path.
18672
+ */
18673
+ mergeByPath(path) {
18674
+ if (!this.context || !this.tree) {
18675
+ throw new YorkieError(
18676
+ Code.ErrNotInitialized,
18677
+ "Tree is not initialized yet"
18678
+ );
18679
+ }
18680
+ if (!path.length) {
18681
+ throw new YorkieError(
18682
+ Code.ErrInvalidArgument,
18683
+ "path should not be empty"
18684
+ );
18685
+ }
18686
+ const treePos = this.tree.pathToTreePos(path);
18687
+ if (treePos.node.isText) {
18688
+ throw new YorkieError(
18689
+ Code.ErrInvalidArgument,
18690
+ "text node cannot be merged"
18691
+ );
18692
+ }
18693
+ const commands = separateMerge(treePos, path);
18694
+ for (const command of commands) {
18695
+ const { fromPath, toPath, content } = command;
18696
+ const fromPos = this.tree.pathToPos(fromPath);
18697
+ const toPos = this.tree.pathToPos(toPath);
18698
+ this.editInternal(fromPos, toPos, content ?? [], 0);
18699
+ }
18700
+ }
18552
18701
  /**
18553
18702
  * `styleByPath` sets the attributes to the elements of the given path.
18554
18703
  */
@@ -20447,7 +20596,7 @@ function createAuthInterceptor(apiKey, token) {
20447
20596
  };
20448
20597
  }
20449
20598
  const name = "@yorkie-js/sdk";
20450
- const version = "0.6.8";
20599
+ const version = "0.6.9";
20451
20600
  const pkg = {
20452
20601
  name,
20453
20602
  version