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
feat: preserve last selected style source on instances (#2873)
Bogdan Chadkin39579c904b8a
diff --git a/apps/builder/app/builder/features/footer/breadcrumbs.tsx b/apps/builder/app/builder/features/footer/breadcrumbs.tsxindex 073180dc47..f5624e6a61 100644--- a/apps/builder/app/builder/features/footer/breadcrumbs.tsx+++ b/apps/builder/app/builder/features/footer/breadcrumbs.tsx@@ -10,7 +10,6 @@ import {$instances,$registeredComponentMetas,$selectedInstanceSelector,- $selectedStyleSourceSelector,} from "~/shared/nano-states";import { getAncestorInstanceSelector } from "~/shared/tree-utils";import { $textEditingInstanceSelector } from "~/shared/nano-states";@@ -65,7 +64,6 @@ export const Breadcrumbs = () => {));$textEditingInstanceSelector.set(undefined);- $selectedStyleSourceSelector.set(undefined);}}>{getInstanceLabel(instance, meta)}diff --git a/apps/builder/app/builder/features/style-panel/shared/style-info.test.ts b/apps/builder/app/builder/features/style-panel/shared/style-info.test.tsindex 168b5ad783..c83a24321f 100644--- a/apps/builder/app/builder/features/style-panel/shared/style-info.test.ts+++ b/apps/builder/app/builder/features/style-panel/shared/style-info.test.ts@@ -17,10 +17,11 @@ import {$registeredComponentMetas,$selectedInstanceIntanceToTag,$selectedInstanceSelector,- $selectedStyleSourceSelector,+ $selectedStyleSources,$styleSourceSelections,$styleSources,$styles,+ $selectedStyleState,} from "~/shared/nano-states";import {getCascadedBreakpointIds,@@ -403,7 +404,7 @@ const resetStores = () => {$breakpoints.set(new Map());$selectedInstanceSelector.set(undefined);$selectedInstanceIntanceToTag.set(new Map());- $selectedStyleSourceSelector.set(undefined);+ $selectedStyleSources.set(new Map());$selectedInstanceStates.set(new Set());};@@ -610,7 +611,7 @@ describe("active states", () => {]));$selectedInstanceSelector.set(["box"]);- $selectedStyleSourceSelector.set({ styleSourceId: "box.local" });+ $selectedStyleSources.set(new Map([["box", "box.local"]]));const { result } = renderHook(() => useStyleInfo());expect(getStyleSource(result.current.color)).toEqual("local");@@ -620,10 +621,8 @@ describe("active states", () => {});act(() => {- $selectedStyleSourceSelector.set({- styleSourceId: "box.local",- state: ":hover",- });+ $selectedStyleSources.set(new Map([["box", "box.local"]]));+ $selectedStyleState.set(":hover");});expect(getStyleSource(result.current.color)).toEqual("remote");expect(result.current.color?.value).toEqual({@@ -654,7 +653,7 @@ describe("active states", () => {]));$selectedInstanceSelector.set(["box"]);- $selectedStyleSourceSelector.set({ styleSourceId: "box.local" });+ $selectedStyleSources.set(new Map([["box", "box.local"]]));$selectedInstanceStates.set(new Set([":hover"]));const { result } = renderHook(() => useStyleInfo());@@ -665,10 +664,8 @@ describe("active states", () => {});act(() => {- $selectedStyleSourceSelector.set({- styleSourceId: "box.local",- state: ":hover",- });+ $selectedStyleSources.set(new Map([["box", "box.local"]]));+ $selectedStyleState.set(":hover");});expect(getStyleSource(result.current.color)).toEqual("local");expect(result.current.color?.value).toEqual({@@ -711,7 +708,7 @@ describe("active states", () => {]));$selectedInstanceSelector.set(["box"]);- $selectedStyleSourceSelector.set({ styleSourceId: "box.local" });+ $selectedStyleSources.set(new Map([["box", "box.local"]]));$selectedBreakpointId.set("small");const { result } = renderHook(() => useStyleInfo());@@ -824,7 +821,7 @@ describe("active states", () => {]));$selectedInstanceSelector.set(["box"]);- $selectedStyleSourceSelector.set({ styleSourceId: "box.local" });+ $selectedStyleSources.set(new Map([["box", "box.local"]]));const { result } = renderHook(() => useStyleInfo());expect(getStyleSource(result.current.color)).toEqual("remote");@@ -881,7 +878,7 @@ describe("active states", () => {]));$selectedInstanceSelector.set(["box"]);- $selectedStyleSourceSelector.set({ styleSourceId: "box.first" });+ $selectedStyleSources.set(new Map([["box", "box.first"]]));$selectedInstanceStates.set(new Set([":hover"]));const { result } = renderHook(() => useStyleInfo());@@ -892,7 +889,7 @@ describe("active states", () => {});act(() => {- $selectedStyleSourceSelector.set({ styleSourceId: "box.second" });+ $selectedStyleSources.set(new Map([["box", "box.second"]]));});expect(getStyleSource(result.current.color)).toEqual("overwritten");expect(result.current.color?.value).toEqual({@@ -938,7 +935,7 @@ describe("active states", () => {);$selectedInstanceSelector.set(["box"]);$selectedInstanceStates.set(new Set([":hover"]));- $selectedStyleSourceSelector.set({ styleSourceId: "box.local" });+ $selectedStyleSources.set(new Map([["box", "box.local"]]));$selectedInstanceIntanceToTag.set(new Map([["box", "div"]]));const { result } = renderHook(() => useStyleInfo());diff --git a/apps/builder/app/builder/features/style-panel/style-source-section.test.ts b/apps/builder/app/builder/features/style-panel/style-source-section.test.tsindex 695d197166..655fb94764 100644--- a/apps/builder/app/builder/features/style-panel/style-source-section.test.ts+++ b/apps/builder/app/builder/features/style-panel/style-source-section.test.ts@@ -7,7 +7,7 @@ import {$breakpoints,$registeredComponentMetas,$selectedInstanceSelector,- $selectedStyleSourceSelector,+ $selectedStyleSources,$styleSourceSelections,$styleSources,} from "~/shared/nano-states";@@ -19,7 +19,7 @@ import {enableMapSet();registerContainers();-test("generate Styles from preset tokens", () => {+test("generate styles from preset tokens", () => {$breakpoints.set(new Map<Breakpoint["id"], Breakpoint>([["base", { id: "base", label: "Base" }],@@ -111,16 +111,14 @@ test("add style source to instance", () => {$selectedInstanceSelector.set(["root"]);$styleSources.set(new Map([["local1", { id: "local1", type: "local" }]]));$styleSourceSelections.set(new Map());- $selectedStyleSourceSelector.set(undefined);+ $selectedStyleSources.set(new Map());addStyleSourceToInstance("token1");expect($styleSourceSelections.get().get("root")).toEqual({instanceId: "root",values: ["token1"],});- expect($selectedStyleSourceSelector.get()).toEqual({- styleSourceId: "token1",- });+ expect($selectedStyleSources.get().get("root")).toEqual("token1");// put new style source lastaddStyleSourceToInstance("local1");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 a2a2dba1c5..38354a4453 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@@ -31,7 +31,8 @@ import {$selectedInstance,$selectedInstanceStyleSources,$selectedOrLastStyleSourceSelector,- $selectedStyleSourceSelector,+ $selectedStyleSources,+ $selectedStyleState,$styleSourceSelections,$styleSources,$styles,@@ -43,6 +44,35 @@ import { isBaseBreakpoint } from "~/shared/breakpoints";import { shallowComputed } from "~/shared/store-utils";import { serverSyncStore } from "~/shared/sync";+const selectStyleSource = (+ styleSourceId: StyleSource["id"],+ state?: StyleDecl["state"]+) => {+ const selectedInstanceSelector = $selectedInstanceSelector.get();+ if (selectedInstanceSelector === undefined) {+ return;+ }+ const [instanceId] = selectedInstanceSelector;+ const selectedStyleSources = new Map($selectedStyleSources.get());+ selectedStyleSources.set(instanceId, styleSourceId);+ $selectedStyleSources.set(selectedStyleSources);+ $selectedStyleState.set(state);+};++const deselectMatchingStyleSource = (styleSourceId: StyleSource["id"]) => {+ const selectedInstanceSelector = $selectedInstanceSelector.get();+ if (selectedInstanceSelector === undefined) {+ return;+ }+ const [instanceId] = selectedInstanceSelector;+ const selectedStyleSources = new Map($selectedStyleSources.get());+ if (selectedStyleSources.get(instanceId) === styleSourceId) {+ selectedStyleSources.delete(instanceId);+ $selectedStyleSources.set(selectedStyleSources);+ $selectedStyleState.set(undefined);+ }+};+const getOrCreateStyleSourceSelectionMutable = (styleSourceSelections: StyleSourceSelections,selectedInstanceId: Instance["id"]@@ -167,7 +197,7 @@ const createStyleSource = (id: StyleSource["id"], name: string) => {}});- $selectedStyleSourceSelector.set({ styleSourceId: newStyleSource.id });+ selectStyleSource(newStyleSource.id);};export const addStyleSourceToInstance = (@@ -189,7 +219,7 @@ export const addStyleSourceToInstance = ();});- $selectedStyleSourceSelector.set({ styleSourceId: newStyleSourceId });+ selectStyleSource(newStyleSourceId);};const removeStyleSourceFromInstance = (styleSourceId: StyleSource["id"]) => {@@ -213,11 +243,7 @@ const removeStyleSourceFromInstance = (styleSourceId: StyleSource["id"]) => {});// reset selected style source if necessary- const selectedStyleSourceId =- $selectedStyleSourceSelector.get()?.styleSourceId;- if (selectedStyleSourceId === styleSourceId) {- $selectedStyleSourceSelector.set(undefined);- }+ deselectMatchingStyleSource(styleSourceId);};const deleteStyleSource = (styleSourceId: StyleSource["id"]) => {@@ -241,11 +267,7 @@ const deleteStyleSource = (styleSourceId: StyleSource["id"]) => {});// reset selected style source if necessary- const selectedStyleSourceId =- $selectedStyleSourceSelector.get()?.styleSourceId;- if (selectedStyleSourceId === styleSourceId) {- $selectedStyleSourceSelector.set(undefined);- }+ deselectMatchingStyleSource(styleSourceId);};const duplicateStyleSource = (styleSourceId: StyleSource["id"]) => {@@ -289,7 +311,7 @@ const duplicateStyleSource = (styleSourceId: StyleSource["id"]) => {});- $selectedStyleSourceSelector.set({ styleSourceId: newStyleSource.id });+ selectStyleSource(newStyleSource.id);return newStyleSource.id;};@@ -319,7 +341,7 @@ const convertLocalStyleSourceToToken = (styleSourceId: StyleSource["id"]) => {styleSources.set(newStyleSource.id, newStyleSource);});- $selectedStyleSourceSelector.set({ styleSourceId: newStyleSource.id });+ selectStyleSource(newStyleSource.id);};const reorderStyleSources = (styleSourceIds: StyleSource["id"][]) => {@@ -501,7 +523,10 @@ export const StyleSourcesSection = () => {reorderStyleSources(items.map((item) => item.id));}}onSelectItem={(styleSourceSelector) => {- $selectedStyleSourceSelector.set(styleSourceSelector);+ selectStyleSource(+ styleSourceSelector.styleSourceId,+ styleSourceSelector.state+ );}}// style source renamingeditingItemId={editingItemId}@@ -509,9 +534,7 @@ export const StyleSourcesSection = () => {setEditingItemId(id);// prevent deselect after renamingif (id !== undefined) {- $selectedStyleSourceSelector.set({- styleSourceId: id,- });+ selectStyleSource(id);}}}onChangeItem={(item) => {diff --git a/apps/builder/app/builder/features/workspace/workspace.tsx b/apps/builder/app/builder/features/workspace/workspace.tsxindex e2fcd8e94a..e2b6fcef52 100644--- a/apps/builder/app/builder/features/workspace/workspace.tsx+++ b/apps/builder/app/builder/features/workspace/workspace.tsx@@ -5,10 +5,7 @@ import {useCanvasWidth,$workspaceRect,} from "~/builder/shared/nano-states";-import {- $selectedInstanceSelector,- $selectedStyleSourceSelector,-} from "~/shared/nano-states";+import { $selectedInstanceSelector } from "~/shared/nano-states";import { $textEditingInstanceSelector } from "~/shared/nano-states";import { CanvasTools } from "./canvas-tools";import { useEffect, useRef } from "react";@@ -145,7 +142,6 @@ export const Workspace = ({const handleWorkspaceClick = () => {$selectedInstanceSelector.set(undefined);$textEditingInstanceSelector.set(undefined);- $selectedStyleSourceSelector.set(undefined);};return (diff --git a/apps/builder/app/builder/shared/commands.ts b/apps/builder/app/builder/shared/commands.tsindex 03c5faee8c..aded431259 100644--- a/apps/builder/app/builder/shared/commands.ts+++ b/apps/builder/app/builder/shared/commands.ts@@ -4,7 +4,6 @@ import {$editingItemId,$instances,$selectedInstanceSelector,- $selectedStyleSourceSelector,$textEditingInstanceSelector,} from "~/shared/nano-states";import {@@ -75,7 +74,6 @@ const deleteSelectedInstance = () => {updateWebstudioData((data) => {if (deleteInstanceMutable(data, selectedInstanceSelector)) {$selectedInstanceSelector.set(newSelectedInstanceSelector);- $selectedStyleSourceSelector.set(undefined);}});};diff --git a/apps/builder/app/builder/shared/navigator-tree.tsx b/apps/builder/app/builder/shared/navigator-tree.tsxindex 826ec79ada..e9b25280ed 100644--- a/apps/builder/app/builder/shared/navigator-tree.tsx+++ b/apps/builder/app/builder/shared/navigator-tree.tsx@@ -9,7 +9,6 @@ import {$rootInstance,$selectedInstanceSelector,$textEditingInstanceSelector,- $selectedStyleSourceSelector,$registeredComponentMetas,$dragAndDropState,} from "~/shared/nano-states";@@ -125,7 +124,6 @@ export const NavigatorTree = () => {}$selectedInstanceSelector.set(instanceSelector);$textEditingInstanceSelector.set(undefined);- $selectedStyleSourceSelector.set(undefined);}, []);if (rootInstance === undefined) {diff --git a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsxindex d981d9626e..5d6df2a4dd 100644--- a/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx+++ b/apps/builder/app/canvas/features/webstudio-component/webstudio-component.tsx@@ -36,7 +36,6 @@ import {$selectedInstanceRenderState,$selectedInstanceSelector,$selectedPage,- $selectedStyleSourceSelector,useInstanceStyles,} from "~/shared/nano-states";import { $textEditingInstanceSelector } from "~/shared/nano-states";@@ -399,7 +398,6 @@ export const WebstudioComponentCanvas = forwardRef<);$textEditingInstanceSelector.set(undefined);$selectedInstanceSelector.set(newSelectedSelector);- $selectedStyleSourceSelector.set(undefined);}}/></Suspense>diff --git a/apps/builder/app/canvas/instance-selection.ts b/apps/builder/app/canvas/instance-selection.tsindex 7853cc62db..7a41eb0545 100644--- a/apps/builder/app/canvas/instance-selection.ts+++ b/apps/builder/app/canvas/instance-selection.ts@@ -4,7 +4,6 @@ import {$instances,$registeredComponentMetas,$selectedInstanceSelector,- $selectedStyleSourceSelector,} from "~/shared/nano-states";import { $textEditingInstanceSelector } from "~/shared/nano-states";import { emitCommand } from "./shared/commands";@@ -61,7 +60,6 @@ export const subscribeInstanceSelection = () => {$selectedInstanceSelector.set(instanceSelector);// reset text editor when another instance is selected$textEditingInstanceSelector.set(undefined);- $selectedStyleSourceSelector.set(undefined);}// the second click in a double click.@@ -76,7 +74,6 @@ export const subscribeInstanceSelection = () => {if (editableInstanceSelector) {$selectedInstanceSelector.set(editableInstanceSelector);$textEditingInstanceSelector.set(editableInstanceSelector);- $selectedStyleSourceSelector.set(undefined);}}};diff --git a/apps/builder/app/canvas/shared/commands.ts b/apps/builder/app/canvas/shared/commands.tsindex 54dffb4011..9744be0c9b 100644--- a/apps/builder/app/canvas/shared/commands.ts+++ b/apps/builder/app/canvas/shared/commands.ts@@ -7,7 +7,6 @@ import {$instances,$registeredComponentMetas,$selectedInstanceSelector,- $selectedStyleSourceSelector,$textEditingInstanceSelector,} from "~/shared/nano-states";import {@@ -69,7 +68,6 @@ export const { emitCommand, subscribeCommands } = createCommandsEmitter({}// unselect both instance and style source$selectedInstanceSelector.set(undefined);- $selectedStyleSourceSelector.set(undefined);},},diff --git a/apps/builder/app/canvas/shared/styles.ts b/apps/builder/app/canvas/shared/styles.tsindex 2848e0373c..7cdc8c49ab 100644--- a/apps/builder/app/canvas/shared/styles.ts+++ b/apps/builder/app/canvas/shared/styles.ts@@ -23,7 +23,7 @@ import {$isPreviewMode,$registeredComponentMetas,$selectedInstanceSelector,- $selectedStyleSourceSelector,+ $selectedStyleState,} from "~/shared/nano-states";import {type StyleRule,@@ -265,12 +265,12 @@ const getOrCreateRule = ({const useSelectedState = (instanceId: Instance["id"]) => {const $selectedState = useMemo(() => {return computed(- [$selectedInstanceSelector, $selectedStyleSourceSelector],- (selectedInstanceSelector, selectedStyleSourceSelector) => {+ [$selectedInstanceSelector, $selectedStyleState],+ (selectedInstanceSelector, selectedStyleState) => {if (selectedInstanceSelector?.[0] !== instanceId) {return;}- return selectedStyleSourceSelector?.state;+ return selectedStyleState;});}, [instanceId]);diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.tsindex 797b001307..f240cddf01 100644--- a/apps/builder/app/shared/instance-utils.ts+++ b/apps/builder/app/shared/instance-utils.ts@@ -37,7 +37,6 @@ import {$styleSourceSelections,$styleSources,$instances,- $selectedStyleSourceSelector,$registeredComponentMetas,$dataSources,$assets,@@ -424,7 +423,6 @@ export const insertTemplateData = ();$selectedInstanceSelector.set([rootInstanceId, ...dropTarget.parentSelector]);- $selectedStyleSourceSelector.set(undefined);};export const getComponentTemplateData = (component: string) => {@@ -492,7 +490,6 @@ export const reparentInstance = (rootInstanceId,...reparentDropTarget.parentSelector,]);- $selectedStyleSourceSelector.set(undefined);});};diff --git a/apps/builder/app/shared/nano-states/nano-states.ts b/apps/builder/app/shared/nano-states/nano-states.tsindex f6949a5346..32dc70393e 100644--- a/apps/builder/app/shared/nano-states/nano-states.ts+++ b/apps/builder/app/shared/nano-states/nano-states.ts@@ -1,5 +1,5 @@import { useMemo } from "react";-import { atom, computed } from "nanostores";+import { atom, computed, onSet } from "nanostores";import { useStore } from "@nanostores/react";import { nanoid } from "nanoid";import type { AuthPermit } from "@webstudio-is/trpc-interface/index.server";@@ -91,9 +91,14 @@ export type StyleSourceSelector = {state?: string;};-export const $selectedStyleSourceSelector = atom<- undefined | StyleSourceSelector->(undefined);+export const $selectedStyleSources = atom(+ new Map<Instance["id"], StyleSource["id"]>()+);+export const $selectedStyleState = atom<StyleDecl["state"]>();+// reset style state whenever selected instance change+onSet($selectedInstanceSelector, () => {+ $selectedStyleState.set(undefined);+});/*** Indexed styles data is recomputed on every styles update@@ -230,16 +235,32 @@ export const $selectedInstanceStyleSources = computed();export const $selectedOrLastStyleSourceSelector = computed(- [$selectedInstanceStyleSources, $selectedStyleSourceSelector],- (styleSources, selectedStyleSourceSelector) => {- if (selectedStyleSourceSelector !== undefined) {- return selectedStyleSourceSelector;+ [+ $selectedInstanceStyleSources,+ $selectedStyleSources,+ $selectedInstanceSelector,+ $selectedStyleState,+ ],+ (+ styleSources,+ selectedStyleSources,+ selectedInstanceSelector,+ selectedStyleState+ ) => {+ if (selectedInstanceSelector === undefined) {+ return;}+ const [instanceId] = selectedInstanceSelector;+ const styleSourceId = selectedStyleSources.get(instanceId);+ // always fallback to local (the last one) style sourceconst lastStyleSource = styleSources.at(-1);- if (lastStyleSource !== undefined) {- return { styleSourceId: lastStyleSource.id };+ const matchedStyleSource = styleSources.find(+ (styleSource) => styleSource.id === styleSourceId+ );+ const styleSource = matchedStyleSource ?? lastStyleSource;+ if (styleSource) {+ return { styleSourceId: styleSource.id, state: selectedStyleState };}- return;});@@ -248,12 +269,20 @@ export const $selectedOrLastStyleSourceSelector = computed(* to the last style source of selected instance*/export const $selectedStyleSource = computed(- [$selectedInstanceStyleSources, $selectedStyleSourceSelector],- (styleSources, selectedStyleSourceSelector) => {+ [+ $selectedInstanceStyleSources,+ $selectedStyleSources,+ $selectedInstanceSelector,+ ],+ (styleSources, selectedStyleSources, selectedInstanceSelector) => {+ if (selectedInstanceSelector === undefined) {+ return;+ }+ const [instanceId] = selectedInstanceSelector;+ const selectedStyleSourceId = selectedStyleSources.get(instanceId);return (- styleSources.find(- (item) => item.id === selectedStyleSourceSelector?.styleSourceId- ) ?? styleSources.at(-1)+ styleSources.find((item) => item.id === selectedStyleSourceId) ??+ styleSources.at(-1));});diff --git a/apps/builder/app/shared/sync/sync-stores.ts b/apps/builder/app/shared/sync/sync-stores.tsindex e16413250e..186a12a0c7 100644--- a/apps/builder/app/shared/sync/sync-stores.ts+++ b/apps/builder/app/shared/sync/sync-stores.ts@@ -27,7 +27,8 @@ import {synchronizedCanvasStores,$synchronizedInstances,$synchronizedBreakpoints,- $selectedStyleSourceSelector,+ $selectedStyleSources,+ $selectedStyleState,synchronizedComponentsMetaStores,$dataSourceVariables,$dragAndDropState,@@ -105,7 +106,8 @@ export const registerContainers = () => {);clientStores.set("hoveredInstanceSelector", $hoveredInstanceSelector);clientStores.set("isPreviewMode", $isPreviewMode);- clientStores.set("selectedStyleSourceSelector", $selectedStyleSourceSelector);+ clientStores.set("selectedStyleSources", $selectedStyleSources);+ clientStores.set("selectedStyleState", $selectedStyleState);clientStores.set("dragAndDropState", $dragAndDropState);clientStores.set("ephemeralStyles", $ephemeralStyles);clientStores.set("selectedInstanceStates", $selectedInstanceStates);