Skip to content

Commit

Permalink
Add newRootNode diff
Browse files Browse the repository at this point in the history
  • Loading branch information
dabbott committed Dec 17, 2023
1 parent 0bd5d80 commit ffb58ff
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
24 changes: 23 additions & 1 deletion packages/noya-component/src/traversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ export function unresolve(
resolvedNode: NoyaResolvedNode,
diffParam: NoyaDiff = { items: [] },
): NoyaNode {
for (const item of diffParam.items) {
if (isDeepEqual(item.path, resolvedNode.path)) {
if (item.newRootNode) {
return item.newRootNode;
}
}
}

switch (resolvedNode.type) {
case 'noyaString': {
const { id, value, type, name } = resolvedNode;
Expand Down Expand Up @@ -126,7 +134,8 @@ function isEmptyDiff(diff: NoyaDiffItem) {
!diff.props &&
!diff.classNames &&
!diff.variantNames &&
!diff.children
!diff.children &&
!diff.newRootNode
);
}

Expand Down Expand Up @@ -237,6 +246,19 @@ export function applyResolvedDiff(
(node, transformedChildren) => {
const nodeKey = node.path.join('/');

for (const item of diff.items) {
const itemKey = [...path, ...item.path].join('/');
if (itemKey === nodeKey) {
if (item.newRootNode) {
return createResolvedNode(
findComponent,
item.newRootNode,
item.path,
);
}
}
}

switch (node.type) {
case 'noyaCompositeElement': {
const newNode: NoyaResolvedCompositeElement = {
Expand Down
1 change: 1 addition & 0 deletions packages/noya-component/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export type NoyaDiffItem = {
textValue?: string;
name?: string;
componentID?: string;
newRootNode?: NoyaNode;
};

export type NoyaDiff = {
Expand Down
25 changes: 18 additions & 7 deletions packages/site/src/dseditor/DSComponentInspector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,21 @@ export function DSComponentInspector({
componentID: selection.componentID,
variantID: selection.variantID,
});
const diff = diffResolvedTrees(instance, newResolvedNode);
setSelection({ ...selection, diff });

if (instance.id !== newResolvedNode.id) {
setSelection({
...selection,
diff: Model.diff([
Model.diffItem({
path: [instance.id],
newRootNode: newResolvedNode,
}),
]),
});
} else {
const diff = diffResolvedTrees(instance, newResolvedNode);
setSelection({ ...selection, diff });
}
},
[findComponent, selection, setSelection],
);
Expand Down Expand Up @@ -581,8 +594,6 @@ export function DSComponentInspector({
return;
}

// debugger;

const [primitivesDiff, compositesDiff] = partitionDiff(
resolvedNode,
selection.diff.items || [],
Expand Down Expand Up @@ -659,12 +670,12 @@ export function DSComponentInspector({
<Chip
key={id}
colorScheme={
metadataMap[id].type === 'noyaCompositeElement'
metadataMap[id]?.type === 'noyaCompositeElement'
? 'primary'
: undefined
}
>
{metadataMap[id].name}
{metadataMap[id]?.name ?? '?'}
</Chip>
)),
<CaretRightIcon />,
Expand Down Expand Up @@ -705,7 +716,7 @@ export function DSComponentInspector({
{item.variantNames.map((arrayDiffItem, j) => {
const elementId = item.path[item.path.length - 1];
const targetComponent = findComponent(
metadataMap[elementId].componentID ?? '',
metadataMap[elementId]?.componentID ?? '',
);
const variants = targetComponent?.variants ?? [];
const findVariantName = (id: string): string => {
Expand Down
6 changes: 5 additions & 1 deletion packages/site/src/dseditor/utils/partitionDiff.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ export function partitionDiff(
const indexPath = ResolvedHierarchy.findIndexPath(resolvedNode, (n) =>
isDeepEqual(n.path, item.path),
);

if (!indexPath) return false;

let nodePath = ResolvedHierarchy.accessPath(resolvedNode, indexPath);

// Remove the root node which is the component itself
nodePath = nodePath.slice(1);
// nodePath = nodePath.slice(1);

const indexOfFirstComposite = nodePath.findIndex(
(node) => node.type === 'noyaCompositeElement',
);
Expand Down

0 comments on commit ffb58ff

Please sign in to comment.