Skip to content

Commit

Permalink
Implement splitLevel of Tree.Edit (#705)
Browse files Browse the repository at this point in the history
When a user positions the cursor within a paragraph and hits the enter
key, the paragraph is divided into two separate paragraphs:

- Original: `<p>a|b</p>`
- Result: `<p>a</p><p>b</p>`

In this commit, `splitLevel` is introduced to support this scenario.

```ts
doc.update((root) => {
  root.tree.edit([0,0], {type:'p',children:[{type:'text', value: 'ab'}]});
  root.tree.edit([2,2], undefined, 1); // split the paragraph
});
```

This commit does not cover all test cases that may arise due to the
split operation. This is because we need to verify the interface
first. we need to add test cases in the simultaneous editing situation
to verify the algorithm later.
  • Loading branch information
hackerwins authored Dec 4, 2023
1 parent 12e86a2 commit 8ede955
Show file tree
Hide file tree
Showing 15 changed files with 1,104 additions and 732 deletions.
2 changes: 1 addition & 1 deletion api/converter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestConverter(t *testing.T) {
Type: "text",
Value: "Hello world",
}},
})
}, 0)

return nil
})
Expand Down
3 changes: 2 additions & 1 deletion api/converter/from_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,9 @@ func fromTreeEdit(pbTreeEdit *api.Operation_TreeEdit) (*operations.TreeEdit, err
parentCreatedAt,
from,
to,
createdAtMapByActor,
nodes,
int(pbTreeEdit.SplitLevel),
createdAtMapByActor,
executedAt,
), nil
}
Expand Down
2 changes: 1 addition & 1 deletion api/converter/to_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func ToTreeNodes(treeNode *crdt.TreeNode) []*api.TreeNode {
return pbTreeNodes
}

index.TraverseNode(treeNode.IndexTreeNode, func(node *index.Node[*crdt.TreeNode], depth int) {
index.TraverseNode(treeNode.Index, func(node *index.Node[*crdt.TreeNode], depth int) {
pbTreeNodes = append(pbTreeNodes, toTreeNode(node.Value, depth))
})
return pbTreeNodes
Expand Down
3 changes: 2 additions & 1 deletion api/converter/to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,9 @@ func toTreeEdit(e *operations.TreeEdit) (*api.Operation_TreeEdit_, error) {
ParentCreatedAt: ToTimeTicket(e.ParentCreatedAt()),
From: toTreePos(e.FromPos()),
To: toTreePos(e.ToPos()),
CreatedAtMapByActor: toCreatedAtMapByActor(e.CreatedAtMapByActor()),
Contents: ToTreeNodesWhenEdit(e.Contents()),
SplitLevel: int32(e.SplitLevel()),
CreatedAtMapByActor: toCreatedAtMapByActor(e.CreatedAtMapByActor()),
ExecutedAt: ToTimeTicket(e.ExecutedAt()),
},
}, nil
Expand Down
316 changes: 176 additions & 140 deletions api/yorkie/v1/resources.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions api/yorkie/v1/resources.proto
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ message Operation {
TreePos to = 3;
map<string, TimeTicket> created_at_map_by_actor = 4;
repeated TreeNodes contents = 5;
int32 split_level = 7;
TimeTicket executed_at = 6;
}
message TreeStyle {
Expand Down
Loading

0 comments on commit 8ede955

Please sign in to comment.