From 72b23e45e5bef89dcd1aa754cd55aaba366608dd Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Sat, 1 Apr 2023 19:50:27 +0700 Subject: [PATCH] Support style source deletion (#1343) Ref https://github.com/webstudio-is/webstudio-builder/issues/807 Added "delete" item to style source menu and select last style source by default in input. --- .../style-panel/style-source-section.tsx | 31 +++++++++++++++++-- .../style-source/style-source-input.tsx | 13 +++++++- .../app/shared/nano-states/nano-states.ts | 4 +-- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/apps/builder/app/builder/features/style-panel/style-source-section.tsx b/apps/builder/app/builder/features/style-panel/style-source-section.tsx index ab53d98268..a814fedc80 100644 --- a/apps/builder/app/builder/features/style-panel/style-source-section.tsx +++ b/apps/builder/app/builder/features/style-panel/style-source-section.tsx @@ -110,6 +110,28 @@ const removeStyleSourceFromInstance = (styleSourceId: StyleSource["id"]) => { ); }; +const deleteStyleSource = (styleSourceId: StyleSource["id"]) => { + store.createTransaction( + [styleSourcesStore, styleSourceSelectionsStore, stylesStore], + (styleSources, styleSourceSelections, styles) => { + styleSources.delete(styleSourceId); + for (const styleSourceSelection of styleSourceSelections.values()) { + if (styleSourceSelection.values.includes(styleSourceId)) { + removeByMutable( + styleSourceSelection.values, + (item) => item === styleSourceId + ); + } + } + for (const [styleDeclKey, styleDecl] of styles) { + if (styleDecl.styleSourceId === styleSourceId) { + styles.delete(styleDeclKey); + } + } + } + ); +}; + const duplicateStyleSource = (styleSourceId: StyleSource["id"]) => { const selectedInstanceSelector = selectedInstanceSelectorStore.get(); if (selectedInstanceSelector === undefined) { @@ -284,9 +306,6 @@ export const StyleSourcesSection = () => { onSelectAutocompleteItem={({ id }) => { addStyleSourceToInstace(id); }} - onRemoveItem={(id) => { - removeStyleSourceFromInstance(id); - }} onDuplicateItem={(id) => { const newId = duplicateStyleSource(id); if (newId !== undefined) { @@ -297,6 +316,12 @@ export const StyleSourcesSection = () => { convertLocalStyleSourceToToken(id); setEditingItemId(id); }} + onRemoveItem={(id) => { + removeStyleSourceFromInstance(id); + }} + onDeleteItem={(id) => { + deleteStyleSource(id); + }} onSort={(items) => { reorderStyleSources(items.map((item) => item.id)); }} diff --git a/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx b/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx index ab010833a2..e3366311c3 100644 --- a/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx +++ b/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx @@ -173,6 +173,7 @@ type StyleSourceInputProps = { editingItemId?: Item["id"]; onSelectAutocompleteItem?: (item: Item) => void; onRemoveItem?: (id: Item["id"]) => void; + onDeleteItem?: (id: Item["id"]) => void; onDuplicateItem?: (id: Item["id"]) => void; onConvertToToken?: (id: Item["id"]) => void; onCreateItem?: (label: string) => void; @@ -234,6 +235,7 @@ const renderMenuItems = (props: { onDisable?: (itemId: IntermediateItem["id"]) => void; onEnable?: (itemId: IntermediateItem["id"]) => void; onRemove?: (itemId: IntermediateItem["id"]) => void; + onDelete?: (itemId: IntermediateItem["id"]) => void; }) => ( <> {props.source !== "local" && ( @@ -266,6 +268,14 @@ const renderMenuItems = (props: { Remove )} + {props.source !== "local" && ( + props.onDelete?.(props.itemId)} + > + Delete + + )} ); @@ -338,10 +348,11 @@ export const StyleSourceInput = ( state: item.state, onDuplicate: props.onDuplicateItem, onConvertToToken: props.onConvertToToken, - onRemove: props.onRemoveItem, onEnable: props.onEnableItem, onDisable: props.onDisableItem, onEdit: props.onEditItem, + onRemove: props.onRemoveItem, + onDelete: props.onDeleteItem, }) } onChangeItem={props.onChangeItem} diff --git a/apps/builder/app/shared/nano-states/nano-states.ts b/apps/builder/app/shared/nano-states/nano-states.ts index 40b36b8764..555d5a4477 100644 --- a/apps/builder/app/shared/nano-states/nano-states.ts +++ b/apps/builder/app/shared/nano-states/nano-states.ts @@ -375,14 +375,14 @@ export const selectedInstanceStyleSourcesStore = computed( /** * Provide selected style source with fallback - * to local style source of selected instance + * to the last style source of selected instance */ export const selectedStyleSourceStore = computed( [selectedInstanceStyleSourcesStore, selectedStyleSourceIdStore], (styleSources, selectedStyleSourceId) => { return ( styleSources.find((item) => item.id === selectedStyleSourceId) ?? - styleSources.find((item) => item.type === "local") + styleSources.at(-1) ); } );