-
Notifications
You must be signed in to change notification settings - Fork 577
refactor(tree): clarify move logic #27466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9957a39
55842a0
4b2409e
4043963
c79f175
7d26533
0addc49
71fcb1a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -732,6 +732,56 @@ describe("DefaultEditBuilder", () => { | |
| assert.deepEqual(treeView, [expected]); | ||
| }); | ||
|
|
||
| it("Can move nodes from field to a nested field under a sibling field of the same parent", () => { | ||
| const { builder, forest } = initializeEditableForest({ | ||
| type: brand(JsonAsTree.JsonObject.identifier), | ||
| fields: { | ||
| foo: [ | ||
| { type: brand(numberSchema.identifier), value: 0 }, | ||
| { type: brand(numberSchema.identifier), value: 1 }, | ||
| { type: brand(numberSchema.identifier), value: 2 }, | ||
| { type: brand(numberSchema.identifier), value: 3 }, | ||
| ], | ||
| bar: [ | ||
| { | ||
| type: brand(JsonAsTree.JsonObject.identifier), | ||
| fields: { | ||
| bar: [{ type: brand(numberSchema.identifier), value: 0 }], | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
| builder.move( | ||
| { parent: root, field: fooKey }, | ||
| 1, | ||
| 3, | ||
| { parent: root_bar0, field: barKey }, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: would be nice to name the path: |
||
| 1, | ||
| ); | ||
| const treeView = toJsonableTreeFromForest(forest); | ||
| const expected: JsonableTree = { | ||
| type: brand(JsonAsTree.JsonObject.identifier), | ||
| fields: { | ||
| foo: [{ type: brand(numberSchema.identifier), value: 0 }], | ||
| bar: [ | ||
| { | ||
| type: brand(JsonAsTree.JsonObject.identifier), | ||
| fields: { | ||
| bar: [ | ||
| { type: brand(numberSchema.identifier), value: 0 }, | ||
| { type: brand(numberSchema.identifier), value: 1 }, | ||
| { type: brand(numberSchema.identifier), value: 2 }, | ||
| { type: brand(numberSchema.identifier), value: 3 }, | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
| assert.deepEqual(treeView, [expected]); | ||
| }); | ||
|
|
||
| it("Can move nodes before an ancestor of the moved node", () => { | ||
| const { builder, forest } = initializeEditableForest({ | ||
| type: brand(JsonAsTree.JsonObject.identifier), | ||
|
|
@@ -824,6 +874,111 @@ describe("DefaultEditBuilder", () => { | |
| assert.deepEqual(treeView, [expected]); | ||
| }); | ||
|
|
||
| it("Can move nodes from before an ancestor of the destination field", () => { | ||
| const { builder, forest } = initializeEditableForest({ | ||
| type: brand(JsonAsTree.JsonObject.identifier), | ||
| fields: { | ||
| foo: [ | ||
| { type: brand(numberSchema.identifier), value: 0 }, | ||
| { type: brand(numberSchema.identifier), value: 1 }, | ||
| { type: brand(numberSchema.identifier), value: 2 }, | ||
| { type: brand(numberSchema.identifier), value: 3 }, | ||
| { | ||
| type: brand(JsonAsTree.JsonObject.identifier), | ||
| fields: { | ||
| foo: [{ type: brand(numberSchema.identifier), value: 0 }], | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
| builder.move( | ||
| // Path to root.foo. | ||
| { parent: root, field: fooKey }, | ||
| 1, | ||
| 3, | ||
| // Path to root.foo[4].foo, which is after the moved nodes in their original location. | ||
| { | ||
|
yann-achard-MS marked this conversation as resolved.
|
||
| parent: { | ||
| parent: root, | ||
| parentField: fooKey, | ||
| parentIndex: 4, | ||
| }, | ||
| field: fooKey, | ||
| }, | ||
| 1, | ||
| ); | ||
| const treeView = toJsonableTreeFromForest(forest); | ||
| const expected: JsonableTree = { | ||
| type: brand(JsonAsTree.JsonObject.identifier), | ||
| fields: { | ||
| foo: [ | ||
| { type: brand(numberSchema.identifier), value: 0 }, | ||
| { | ||
| type: brand(JsonAsTree.JsonObject.identifier), | ||
| fields: { | ||
| foo: [ | ||
| { type: brand(numberSchema.identifier), value: 0 }, | ||
| { type: brand(numberSchema.identifier), value: 1 }, | ||
| { type: brand(numberSchema.identifier), value: 2 }, | ||
| { type: brand(numberSchema.identifier), value: 3 }, | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
| assert.deepEqual(treeView, [expected]); | ||
| }); | ||
|
|
||
| it("Can move nodes from after an ancestor of the destination field", () => { | ||
| const { builder, forest } = initializeEditableForest({ | ||
| type: brand(JsonAsTree.JsonObject.identifier), | ||
| fields: { | ||
| foo: [ | ||
| { | ||
| type: brand(JsonAsTree.JsonObject.identifier), | ||
| fields: { | ||
| foo: [{ type: brand(numberSchema.identifier), value: 0 }], | ||
| }, | ||
| }, | ||
| { type: brand(numberSchema.identifier), value: 1 }, | ||
| { type: brand(numberSchema.identifier), value: 2 }, | ||
| { type: brand(numberSchema.identifier), value: 3 }, | ||
| ], | ||
| }, | ||
| }); | ||
| builder.move( | ||
| // Path to root.foo. | ||
| { parent: root, field: fooKey }, | ||
| 1, | ||
| 3, | ||
| // Path to root.foo[0].foo, which is before the moved nodes in their original location. | ||
| { parent: root_foo0, field: fooKey }, | ||
|
yann-achard-MS marked this conversation as resolved.
|
||
| 1, | ||
| ); | ||
| const treeView = toJsonableTreeFromForest(forest); | ||
| const expected: JsonableTree = { | ||
| type: brand(JsonAsTree.JsonObject.identifier), | ||
| fields: { | ||
| foo: [ | ||
| { | ||
| type: brand(JsonAsTree.JsonObject.identifier), | ||
| fields: { | ||
| foo: [ | ||
| { type: brand(numberSchema.identifier), value: 0 }, | ||
| { type: brand(numberSchema.identifier), value: 1 }, | ||
| { type: brand(numberSchema.identifier), value: 2 }, | ||
| { type: brand(numberSchema.identifier), value: 3 }, | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
| assert.deepEqual(treeView, [expected]); | ||
| }); | ||
|
|
||
| it("Errors when attempting to move a node under itself", () => { | ||
| const statingState: JsonableTree = { | ||
| type: brand(JsonAsTree.JsonObject.identifier), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1627,13 +1627,6 @@ describe("Editing", () => { | |
| expectJsonTree(tree, expectedState); | ||
| }); | ||
|
|
||
| it("can move a node out from a field and into a field under a sibling", () => { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This scenario is now covered in src/test/feature-libraries/default-field-kinds/defaultChangeFamily.spec.ts. |
||
| const tree = makeTreeFromJsonSequence(["A", {}]); | ||
| tree.editor.move(rootField, 0, 1, { parent: rootNode2, field: brand("foo") }, 0); | ||
| const expectedState: JsonCompatible = [{ foo: "A" }]; | ||
| expectJsonTree(tree, expectedState); | ||
| }); | ||
|
|
||
| it("can rebase a move over the deletion of the source parent", () => { | ||
| const tree = makeTreeFromJson({ src: ["A", "B"], dst: ["C", "D"] }); | ||
| const childBranch = tree.fork(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.