Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -538,44 +538,47 @@
}

// All sources should be from the same region
const sourceRegion = drop.sources[0].region;
const sources = drop.sources || [];
const sourceRegion = sources.length > 0 ? sources[0].region : null;
const payload = {
target: identity.metadata.id,
position,
};

// When the source region is the clipboard, we want to make sure we use
// `excluded_descendants` by accessing the copy trees through the clipboard node ID
if (sourceRegion && sourceRegion.id === DraggableRegions.CLIPBOARD) {
return Promise.all(
data.sources.map(source => {
// Using `getCopyTrees` we can access the `excluded_descendants` for the node, such
// that we make sure to skip copying nodes that aren't intended to be copied
const trees = this.getCopyTrees(source.metadata.clipboardNodeId, true);
if (payload.target) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In such scenarios where you have to nest a large block of code in a conditional, it can make more sense to have to shortcut the code early instead, which keeps the code flatter. For example:

if (!payload.target) return;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Blaine! I will most likely refactor this in my next studio PR

// When the source region is the clipboard, we want to make sure we use
// `excluded_descendants` by accessing the copy trees through the clipboard node ID
if (sourceRegion && sourceRegion.id === DraggableRegions.CLIPBOARD) {
return Promise.all(
data.sources.map(source => {
// Using `getCopyTrees` we can access the `excluded_descendants` for the node, such
// that we make sure to skip copying nodes that aren't intended to be copied
const trees = this.getCopyTrees(source.metadata.clipboardNodeId, true);

// Since we're using `ignoreSelection=true` for `getCopyTrees`, it should only
// return one tree at most
if (trees.length === 0) {
return Promise.resolve();
} else if (trees.length > 1) {
throw new Error(
'Multiple copy trees are unexpected for drag and drop copy operation'
);
}
// Since we're using `ignoreSelection=true` for `getCopyTrees`, it should only
// return one tree at most
if (trees.length === 0) {
return Promise.resolve();
} else if (trees.length > 1) {
throw new Error(
'Multiple copy trees are unexpected for drag and drop copy operation'
);
}

return this.copyContentNode({
...payload,
id: source.metadata.id,
excluded_descendants: get(trees, [0, 'extra_fields', 'excluded_descendants'], {}),
});
})
);
}
return this.copyContentNode({
...payload,
id: source.metadata.id,
excluded_descendants: get(trees, [0, 'extra_fields', 'excluded_descendants'], {}),
});
})
);
}

return this.moveContentNodes({
...payload,
id__in: data.sources.map(s => s.metadata.id),
});
return this.moveContentNodes({
...payload,
id__in: data.sources.map(s => s.metadata.id),
});
}
},
insertPosition(mask) {
return mask & DraggableFlags.TOP
Expand Down