-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
253 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { partitionDiff } from './partitionDiff'; | ||
import { | ||
FindComponent, | ||
instantiateResolvedComponent, | ||
unresolve, | ||
} from './traversal'; | ||
import { NoyaComponent, NoyaNode, SelectedComponent } from './types'; | ||
|
||
export function applyDiff({ | ||
selection, | ||
component, | ||
findComponent, | ||
enforceSchema, | ||
}: { | ||
selection: SelectedComponent; | ||
component: NoyaComponent; | ||
findComponent: FindComponent; | ||
enforceSchema: (node: NoyaNode) => NoyaNode; | ||
}): { | ||
selection: SelectedComponent; | ||
component: NoyaComponent; | ||
} { | ||
const resolvedNode = instantiateResolvedComponent(findComponent, selection); | ||
|
||
if (!selection.diff) return { selection, component }; | ||
|
||
if (selection.variantID) { | ||
// Merge the diff into the variant's diff | ||
const variant = component.variants?.find( | ||
(variant) => variant.id === selection.variantID, | ||
); | ||
|
||
if (!variant) return { selection, component }; | ||
|
||
const newVariant = { | ||
...variant, | ||
diff: { | ||
items: [...(variant.diff?.items ?? []), ...selection.diff.items], | ||
}, | ||
}; | ||
|
||
return { | ||
component: { | ||
...component, | ||
variants: component.variants?.map((variant) => | ||
variant.id === selection.variantID ? newVariant : variant, | ||
), | ||
}, | ||
selection: { | ||
...selection, | ||
diff: undefined, | ||
}, | ||
}; | ||
} | ||
|
||
const [primitivesDiff, compositesDiff] = partitionDiff( | ||
resolvedNode, | ||
selection.diff.items || [], | ||
); | ||
|
||
const instance = instantiateResolvedComponent(findComponent, { | ||
componentID: selection.componentID, | ||
variantID: selection.variantID, | ||
diff: { items: primitivesDiff }, | ||
}); | ||
|
||
const newRootElement = enforceSchema( | ||
unresolve(instance, { items: compositesDiff }), | ||
); | ||
|
||
return { | ||
component: { | ||
...component, | ||
rootElement: newRootElement, | ||
}, | ||
selection: { | ||
...selection, | ||
diff: undefined, | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 2 additions & 5 deletions
7
...site/src/dseditor/utils/partitionDiff.tsx → ...ages/noya-component/src/partitionDiff.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.