Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
Add token duplcate support in style sources (#1043)
Bogdan Chadkinccbcba15f3f3
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.tsxindex 605aaa5d02..984516239f 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@@ -2,10 +2,7 @@ import { useState } from "react";import store from "immerhin";import { nanoid } from "nanoid";import { theme, DeprecatedText2 } from "@webstudio-is/design-system";-import type {- StyleSource,- StyleSourceSelection,-} from "@webstudio-is/project-build";+import { getStyleDeclKey, type StyleSource } from "@webstudio-is/project-build";import {type ItemSource,type ItemState,@@ -20,12 +17,14 @@ import {selectedStyleSourceStore,styleSourceSelectionsStore,styleSourcesStore,+ stylesStore,+ treeIdStore,} from "~/shared/nano-states";import { removeByMutable } from "~/shared/array-utils";+import { cloneStyles } from "~/shared/tree-utils";const createStyleSource = (name: string) => {const selectedInstanceId = selectedInstanceIdStore.get();- const selectedInstanceStyleSources = selectedInstanceStyleSourcesStore.get();if (selectedInstanceId === undefined) {return;}@@ -34,51 +33,46 @@ const createStyleSource = (name: string) => {id: nanoid(),name,};- // set style sources and selection along with generated local style source- const newStyleSources = [...selectedInstanceStyleSources, newStyleSource];- const newStyleSourceSelection: StyleSourceSelection = {- instanceId: selectedInstanceId,- values: [- ...selectedInstanceStyleSources.map((styleSource) => styleSource.id),- newStyleSource.id,- ],- };store.createTransaction([styleSourcesStore, styleSourceSelectionsStore],(styleSources, styleSourceSelections) => {- // set new style source and local if not set before- for (const newStyleSource of newStyleSources) {- styleSources.set(newStyleSource.id, newStyleSource);+ let styleSourceSelection = styleSourceSelections.get(selectedInstanceId);+ if (styleSourceSelection === undefined) {+ styleSourceSelection = {+ instanceId: selectedInstanceId,+ values: [],+ };+ styleSourceSelections.set(selectedInstanceId, styleSourceSelection);}- styleSourceSelections.set(selectedInstanceId, newStyleSourceSelection);+ styleSourceSelection.values.push(newStyleSource.id);+ styleSources.set(newStyleSource.id, newStyleSource);});+ selectedStyleSourceIdStore.set(newStyleSource.id);};-const addStyleSourceToInstace = (styleSourceId: StyleSource["id"]) => {+const addStyleSourceToInstace = (newStyleSourceId: StyleSource["id"]) => {const selectedInstanceId = selectedInstanceIdStore.get();- const selectedInstanceStyleSources = selectedInstanceStyleSourcesStore.get();if (selectedInstanceId === undefined) {return;}- // set style sources and selection along with generated local style source- const newStyleSourceSelection: StyleSourceSelection = {- instanceId: selectedInstanceId,- values: [- ...selectedInstanceStyleSources.map((styleSource) => styleSource.id),- styleSourceId,- ],- };store.createTransaction(- [styleSourcesStore, styleSourceSelectionsStore],- (styleSources, styleSourceSelections) => {- // set local style source if not set before- for (const newStyleSource of selectedInstanceStyleSources) {- styleSources.set(newStyleSource.id, newStyleSource);+ [styleSourceSelectionsStore],+ (styleSourceSelections) => {+ let styleSourceSelection = styleSourceSelections.get(selectedInstanceId);+ if (styleSourceSelection === undefined) {+ styleSourceSelection = {+ instanceId: selectedInstanceId,+ values: [],+ };+ styleSourceSelections.set(selectedInstanceId, styleSourceSelection);+ }+ if (styleSourceSelection.values.includes(newStyleSourceId) === false) {+ styleSourceSelection.values.push(newStyleSourceId);}- styleSourceSelections.set(selectedInstanceId, newStyleSourceSelection);});+ selectedStyleSourceIdStore.set(newStyleSourceId);};const removeStyleSourceFromInstance = (styleSourceId: StyleSource["id"]) => {@@ -102,20 +96,77 @@ const removeStyleSourceFromInstance = (styleSourceId: StyleSource["id"]) => {);};-const reorderStyleSources = (styleSourceIds: StyleSource["id"][]) => {+const duplicateStyleSource = (styleSourceId: StyleSource["id"]) => {const selectedInstanceId = selectedInstanceIdStore.get();+ if (selectedInstanceId === undefined) {+ return;+ }+ const styleSources = styleSourcesStore.get();+ // style source may not exist in store which means+ // temporary generated local stye source was not applied yet+ const styleSource = styleSources.get(styleSourceId);+ if (styleSource === undefined || styleSource.type === "local") {+ return;+ }++ const newStyleSource: StyleSource = {+ type: "token",+ id: nanoid(),+ name: `${styleSource.name} (copy)`,+ };+ const clonedStyleSourceIds = new Map();+ clonedStyleSourceIds.set(styleSourceId, newStyleSource.id);+ const clonedStyles = cloneStyles(stylesStore.get(), clonedStyleSourceIds);+store.createTransaction(- [styleSourceSelectionsStore],- (styleSourceSelections) => {- if (selectedInstanceId === undefined) {+ [styleSourcesStore, stylesStore, styleSourceSelectionsStore],+ (styleSources, styles, styleSourceSelections) => {+ const styleSourceSelection =+ styleSourceSelections.get(selectedInstanceId);+ if (styleSourceSelection === undefined) {return;}+ // put new style source after original one+ const position = styleSourceSelection.values.indexOf(styleSourceId);+ styleSourceSelection.values.splice(position + 1, 0, newStyleSource.id);+ styleSources.set(newStyleSource.id, newStyleSource);+ for (const styleDecl of clonedStyles) {+ styles.set(getStyleDeclKey(styleDecl), styleDecl);+ }+ }+ );++ selectedStyleSourceIdStore.set(newStyleSource.id);++ return newStyleSource.id;+};++const reorderStyleSources = (styleSourceIds: StyleSource["id"][]) => {+ const selectedInstanceId = selectedInstanceIdStore.get();+ const treeId = treeIdStore.get();+ if (selectedInstanceId === undefined || treeId === undefined) {+ return;+ }+ store.createTransaction(+ [styleSourcesStore, styleSourceSelectionsStore],+ (styleSources, styleSourceSelections) => {const styleSourceSelection =styleSourceSelections.get(selectedInstanceId);if (styleSourceSelection === undefined) {return;}styleSourceSelection.values = styleSourceIds;+ // reoder may affect temporary generated and not yet applied+ // local style source so add one when not found in style sources+ for (const styleSourceId of styleSourceIds) {+ if (styleSources.has(styleSourceId) === false) {+ styleSources.set(styleSourceId, {+ type: "local",+ id: styleSourceId,+ treeId,+ });+ }+ }});};@@ -194,6 +245,12 @@ export const StyleSourcesSection = () => {onRemoveItem={({ id }) => {removeStyleSourceFromInstance(id);}}+ onDuplicateItem={(id) => {+ const newId = duplicateStyleSource(id);+ if (newId !== undefined) {+ setEditingItemId(newId);+ }+ }}onSort={(items) => {reorderStyleSources(items.map((item) => item.id));}}@@ -204,7 +261,10 @@ export const StyleSourcesSection = () => {editingItemId={editingItemId}onEditItem={(id) => {setEditingItemId(id);- selectedStyleSourceIdStore.set(id);+ // prevent deselect after renaming+ if (id !== undefined) {+ selectedStyleSourceIdStore.set(id);+ }}}onChangeItem={(item) => {renameStyleSource(item.id, item.label);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.tsxindex 2d2e9dbf6a..3b315b6184 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@@ -68,6 +68,7 @@ type TextFieldBaseWrapperProps<Item extends IntermediateItem> = Omit<disabled?: boolean;containerRef?: RefObject<HTMLDivElement>;inputRef?: RefObject<HTMLInputElement>;+ onDuplicateItem: (id: Item["id"]) => void;onRemoveItem?: (item: Item) => void;onChangeItem?: (item: Item) => void;onDisableItem?: (item: Item) => void;@@ -96,6 +97,7 @@ const TextFieldBase: ForwardRefRenderFunction<onKeyDown,label,value,+ onDuplicateItem,onRemoveItem,onChangeItem,onDisableItem,@@ -155,6 +157,7 @@ const TextFieldBase: ForwardRefRenderFunction<onEditItem?.();onChangeItem?.({ ...item, label });}}+ onDuplicate={() => onDuplicateItem?.(item.id)}onRemove={() => {onRemoveItem?.(item);}}@@ -172,7 +175,6 @@ const TextFieldBase: ForwardRefRenderFunction<disabled={disabled}onClick={onClick}ref={mergeRefs(internalInputRef, inputRef ?? null)}- autoFocusaria-label="New Style Source Input"/>)}@@ -189,6 +191,7 @@ type StyleSourceInputProps<Item extends IntermediateItem> = {editingItemId?: Item["id"];onSelectAutocompleteItem?: (item: Item) => void;onRemoveItem?: (item: Item) => void;+ onDuplicateItem?: (id: Item["id"]) => void;onCreateItem?: (label: string) => void;onChangeItem?: (item: Item) => void;onSelectItem?: (item?: Item) => void;@@ -284,7 +287,10 @@ export const StyleSourceInput = <Item extends IntermediateItem>(label === "" &&props.editingItemId === undefined) {- props.onRemoveItem?.(value[value.length - 1]);+ const item = value[value.length - 1];+ if (item.source !== "local") {+ props.onRemoveItem?.(item);+ }}},});@@ -298,6 +304,7 @@ export const StyleSourceInput = <Item extends IntermediateItem>(<TextField{...inputProps}onRemoveItem={props.onRemoveItem}+ onDuplicateItem={props.onDuplicateItem}onChangeItem={props.onChangeItem}onSelectItem={props.onSelectItem}onEditItem={props.onEditItem}diff --git a/apps/builder/app/builder/features/style-panel/style-source/style-source.tsx b/apps/builder/app/builder/features/style-panel/style-source/style-source.tsxindex dabdac3e51..af85275ee5 100644--- a/apps/builder/app/builder/features/style-panel/style-source/style-source.tsx+++ b/apps/builder/app/builder/features/style-panel/style-source/style-source.tsx@@ -136,6 +136,7 @@ type MenuProps = {source: ItemSource;state: ItemState;onEdit: () => void;+ onDuplicate: () => void;onDisable: () => void;onEnable: () => void;onRemove: () => void;@@ -144,6 +145,7 @@ type MenuProps = {const Menu = (props: MenuProps) => {const scheduler = useCallScheduler();const canEdit = props.source !== "local";+ const canDuplicate = props.source !== "local";const canDisable = props.state !== "disabled";const canEnable = props.state === "disabled";const canRemove = props.source !== "local";@@ -162,6 +164,11 @@ const Menu = (props: MenuProps) => {Edit Name</DropdownMenuItem>)}+ {canDuplicate && (+ <DropdownMenuItem onSelect={scheduler.set(props.onDuplicate)}>+ Duplicate+ </DropdownMenuItem>+ )}{canEnable && (<DropdownMenuItem onSelect={scheduler.set(props.onEnable)}>Enable@@ -404,6 +411,7 @@ type StyleSourceProps = {source: ItemSource;onChangeState: (state: ItemState) => void;onSelect: () => void;+ onDuplicate: () => void;onRemove: () => void;onChangeValue: (value: string) => void;onChangeEditing: (isEditing: boolean) => void;@@ -421,6 +429,7 @@ export const StyleSource = ({onChangeEditing,onChangeState,onSelect,+ onDuplicate,onRemove,}: StyleSourceProps) => {const ref = useForceRecalcStyle<HTMLDivElement>("max-width", isEditing);@@ -444,6 +453,7 @@ export const StyleSource = ({<Menusource={source}state={state}+ onDuplicate={onDuplicate}onRemove={onRemove}onEnable={() => {onChangeState("unselected");diff --git a/apps/builder/app/shared/nano-states/nano-states.ts b/apps/builder/app/shared/nano-states/nano-states.tsindex 7c01770e32..4cbfae2471 100644--- a/apps/builder/app/shared/nano-states/nano-states.ts+++ b/apps/builder/app/shared/nano-states/nano-states.ts@@ -277,9 +277,9 @@ export const selectedInstanceStyleSourcesStore = computed(}}// generate style source when selection has not local style sources- // it is synchronized whenever instance style sources or styles are updated+ // it is synchronized whenever styles are updatedif (hasLocal === false && treeId !== undefined) {- selectedInstanceStyleSources.push({+ selectedInstanceStyleSources.unshift({type: "local",treeId,id: nanoid(),