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
Enforce local style source at the end (#2229)
Bogdan Chadkin2ed1646dae04
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 a87318eb66..e75a0c5f63 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@@ -1,11 +1,23 @@+import { enableMapSet } from "immer";import { expect, test } from "@jest/globals";import type { Breakpoint } from "@webstudio-is/sdk";import type { WsComponentMeta } from "@webstudio-is/react-sdk";+import { registerContainers } from "~/shared/sync";import {breakpointsStore,registeredComponentMetasStore,+ selectedInstanceSelectorStore,+ selectedStyleSourceSelectorStore,+ styleSourceSelectionsStore,+ styleSourcesStore,} from "~/shared/nano-states";-import { $presetTokens } from "./style-source-section";+import {+ $presetTokens,+ addStyleSourceToInstance,+} from "./style-source-section";++enableMapSet();+registerContainers();test("generate Styles from preset tokens", () => {breakpointsStore.set(@@ -94,3 +106,33 @@ test("generate Styles from preset tokens", () => {]));});++test("add style source to instance", () => {+ selectedInstanceSelectorStore.set(["root"]);+ styleSourcesStore.set(new Map([["local1", { id: "local1", type: "local" }]]));+ styleSourceSelectionsStore.set(new Map());+ selectedStyleSourceSelectorStore.set(undefined);++ addStyleSourceToInstance("token1");+ expect(styleSourceSelectionsStore.get().get("root")).toEqual({+ instanceId: "root",+ values: ["token1"],+ });+ expect(selectedStyleSourceSelectorStore.get()).toEqual({+ styleSourceId: "token1",+ });++ // put new style source last+ addStyleSourceToInstance("local1");+ expect(styleSourceSelectionsStore.get().get("root")).toEqual({+ instanceId: "root",+ values: ["token1", "local1"],+ });++ // put new token before local+ addStyleSourceToInstance("token2");+ expect(styleSourceSelectionsStore.get().get("root")).toEqual({+ instanceId: "root",+ values: ["token1", "token2", "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 4b2868ea7b..04fa5cbf30 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@@ -9,6 +9,7 @@ import {type StyleSourceToken,type StyleSourceSelections,type StyleDecl,+ type StyleSources,getStyleDeclKey,} from "@webstudio-is/sdk";import {@@ -57,35 +58,6 @@ const getOrCreateStyleSourceSelectionMutable = (return styleSourceSelection;};-const createLocalStyleSourceIfNotExists = (- styleSourceId: StyleSource["id"]-) => {- const selectedInstanceSelector = selectedInstanceSelectorStore.get();- if (selectedInstanceSelector === undefined) {- return;- }- const [selectedInstanceId] = selectedInstanceSelector;- store.createTransaction(- [styleSourceSelectionsStore, styleSourcesStore],- (styleSourceSelections, styleSources) => {- const styleSourceSelection = getOrCreateStyleSourceSelectionMutable(- styleSourceSelections,- selectedInstanceId- );- if (styleSourceSelection.values.includes(styleSourceId) === false) {- // local should be put first by default- styleSourceSelection.values.unshift(styleSourceId);- }- if (styleSources.has(styleSourceId) === false) {- styleSources.set(styleSourceId, {- type: "local",- id: styleSourceId,- });- }- }- );-};-const $baseBreakpointId = computed(breakpointsStore, (breakpoints) => {const breakpointValues = Array.from(breakpoints.values());const baseBreakpoint = breakpointValues.find(isBaseBreakpoint);@@ -139,6 +111,31 @@ export const $presetTokens = computed(});+const addStyleSourceToInstaceMutable = (+ styleSourceSelections: StyleSourceSelections,+ styleSources: StyleSources,+ instanceId: Instance["id"],+ newStyleSourceId: StyleSource["id"]+) => {+ const styleSourceSelection = getOrCreateStyleSourceSelectionMutable(+ styleSourceSelections,+ instanceId+ );+ if (styleSourceSelection.values.includes(newStyleSourceId) === false) {+ const lastStyleSourceId = styleSourceSelection.values.at(-1);+ const lastStyleSource =+ lastStyleSourceId === undefined+ ? undefined+ : styleSources.get(lastStyleSourceId);+ // when local style source exists insert before it+ if (lastStyleSource?.type === "local") {+ styleSourceSelection.values.splice(-1, 0, newStyleSourceId);+ } else {+ styleSourceSelection.values.push(newStyleSourceId);+ }+ }+};+const createStyleSource = (id: StyleSource["id"], name: string) => {const selectedInstanceSelector = selectedInstanceSelectorStore.get();if (selectedInstanceSelector === undefined) {@@ -154,13 +151,13 @@ const createStyleSource = (id: StyleSource["id"], name: string) => {store.createTransaction([styleSourcesStore, stylesStore, styleSourceSelectionsStore],(styleSources, styles, styleSourceSelections) => {- const styleSourceSelection = getOrCreateStyleSourceSelectionMutable(+ styleSources.set(newStyleSource.id, newStyleSource);+ addStyleSourceToInstaceMutable(styleSourceSelections,- selectedInstanceId+ styleSources,+ selectedInstanceId,+ newStyleSource.id);- styleSourceSelection.values.push(newStyleSource.id);- styleSources.set(newStyleSource.id, newStyleSource);-// populate preset token stylesconst presetToken = presetTokens.get(id);if (presetToken) {@@ -173,22 +170,23 @@ const createStyleSource = (id: StyleSource["id"], name: string) => {selectedStyleSourceSelectorStore.set({ styleSourceId: newStyleSource.id });};-const addStyleSourceToInstace = (newStyleSourceId: StyleSource["id"]) => {+export const addStyleSourceToInstance = (+ newStyleSourceId: StyleSource["id"]+) => {const selectedInstanceSelector = selectedInstanceSelectorStore.get();if (selectedInstanceSelector === undefined) {return;}const [selectedInstanceId] = selectedInstanceSelector;store.createTransaction(- [styleSourceSelectionsStore],- (styleSourceSelections) => {- const styleSourceSelection = getOrCreateStyleSourceSelectionMutable(+ [styleSourceSelectionsStore, styleSourcesStore],+ (styleSourceSelections, styleSources) => {+ addStyleSourceToInstaceMutable(styleSourceSelections,- selectedInstanceId+ styleSources,+ selectedInstanceId,+ newStyleSourceId);- if (styleSourceSelection.values.includes(newStyleSourceId) === false) {- styleSourceSelection.values.push(newStyleSourceId);- }});selectedStyleSourceSelectorStore.set({ styleSourceId: newStyleSourceId });@@ -314,9 +312,9 @@ const convertLocalStyleSourceToToken = (styleSourceId: StyleSource["id"]) => {styleSourceSelections,selectedInstanceId);- // generated local style source was not applied so put first+ // generated local style source was not applied so put lastif (styleSourceSelection.values.includes(newStyleSource.id) === false) {- styleSourceSelection.values.unshift(newStyleSource.id);+ styleSourceSelection.values.push(newStyleSource.id);}styleSources.set(newStyleSource.id, newStyleSource);}@@ -331,24 +329,14 @@ const reorderStyleSources = (styleSourceIds: StyleSource["id"][]) => {}const [selectedInstanceId] = selectedInstanceSelector;store.createTransaction(- [styleSourcesStore, styleSourceSelectionsStore],- (styleSources, styleSourceSelections) => {+ [styleSourceSelectionsStore],+ (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,- });- }- }});};@@ -488,7 +476,7 @@ export const StyleSourcesSection = () => {componentStates={componentStates}onCreateItem={createStyleSource}onSelectAutocompleteItem={({ id }) => {- addStyleSourceToInstace(id);+ addStyleSourceToInstance(id);}}onDuplicateItem={(id) => {const newId = duplicateStyleSource(id);@@ -515,7 +503,6 @@ export const StyleSourcesSection = () => {reorderStyleSources(items.map((item) => item.id));}}onSelectItem={(styleSourceSelector) => {- createLocalStyleSourceIfNotExists(styleSourceSelector.styleSourceId);selectedStyleSourceSelectorStore.set(styleSourceSelector);}}// style source renamingdiff --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 154b02b2ab..93b4b8dac6 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@@ -429,20 +429,7 @@ export const StyleSourceInput = (},});- const inputProps = getInputProps({- onKeyDown(event) {- if (- event.key === "Backspace" &&- label === "" &&- props.editingItemId === undefined- ) {- const item = value[value.length - 1];- if (item.source !== "local") {- props.onRemoveItem?.(item.id);- }- }- },- });+ const inputProps = getInputProps();let hasNewTokenItem = false;let hasGlobalTokenItem = false;diff --git a/apps/builder/app/builder/features/style-panel/style-source/use-sortable.tsx b/apps/builder/app/builder/features/style-panel/style-source/use-sortable.tsxindex 1b6331fec8..719cc8ac61 100644--- a/apps/builder/app/builder/features/style-panel/style-source/use-sortable.tsx+++ b/apps/builder/app/builder/features/style-panel/style-source/use-sortable.tsx@@ -8,7 +8,9 @@ import {useDrop,type DropTarget,computeIndicatorPlacement,+ toast,} from "@webstudio-is/design-system";+import type { ItemSource } from "./style-source";type UseSortable<Item> = {items: Array<Item>;@@ -27,7 +29,7 @@ const sharedDropOptions = {childrenOrientation: { type: "horizontal", reverse: false },} as const;-export const useSortable = <Item extends { id: string }>({+export const useSortable = <Item extends { id: string; source: ItemSource }>({items,onSort,}: UseSortable<Item>) => {@@ -58,6 +60,17 @@ export const useSortable = <Item extends { id: string }>({if (dropTarget === undefined) {setPlacementIndicator(undefined);} else {+ // when local style source is last always drop before it+ if (items.at(-1)?.source === "local") {+ const lastIndex = items.length - 1;+ const { placement } = dropTarget;+ const { indexAdjustment, closestChildIndex } = placement;+ placement.indexAdjustment = Math.min(indexAdjustment, lastIndex - 1);+ placement.closestChildIndex = Math.min(+ closestChildIndex,+ lastIndex - 1+ );+ }setPlacementIndicator(computeIndicatorPlacement({...sharedDropOptions,@@ -81,10 +94,22 @@ export const useSortable = <Item extends { id: string }>({return false;}- return getItemId(closest) || false;+ const itemId = getItemId(closest);+ if (itemId === undefined) {+ return false;+ }++ return itemId;},- onStart({ data }) {- setDragItemId(data);+ onStart({ data: itemId }) {+ const item = items.find((item) => item.id === itemId);+ if (items.at(-1) === item && item?.source === "local") {+ toast.error("Local style source is always last and can not be moved");+ useDragHandlers.cancelCurrentDrag();+ return;+ }++ setDragItemId(itemId);useDropHandlers.handleStart();},onMove: (point) => {@@ -109,6 +134,11 @@ export const useSortable = <Item extends { id: string }>({if (oldIndex < newIndex) {newIndex = Math.max(0, newIndex - 1);}+ // when local style source is last always drop before it+ if (items.at(-1)?.source === "local") {+ const lastIndex = items.length - 1;+ newIndex = Math.min(newIndex, lastIndex - 1);+ }if (oldIndex !== newIndex) {const newItems = [...items];diff --git a/apps/builder/app/shared/nano-states/nano-states.ts b/apps/builder/app/shared/nano-states/nano-states.tsindex 64f114ca9d..28cefd1c3e 100644--- a/apps/builder/app/shared/nano-states/nano-states.ts+++ b/apps/builder/app/shared/nano-states/nano-states.ts@@ -328,7 +328,8 @@ export const selectedInstanceStyleSourcesStore = computed(// generate style source when selection has not local style sources// it is synchronized whenever styles are updatedif (hasLocal === false) {- selectedInstanceStyleSources.unshift({+ // always put local style source last+ selectedInstanceStyleSources.push({type: "local",id: nanoid(),});diff --git a/packages/design-system/src/components/primitives/dnd/use-drag.ts b/packages/design-system/src/components/primitives/dnd/use-drag.tsindex e75e97fb5c..57c603df27 100644--- a/packages/design-system/src/components/primitives/dnd/use-drag.ts+++ b/packages/design-system/src/components/primitives/dnd/use-drag.ts@@ -131,7 +131,10 @@ export const useDrag = <DragItemData>(state.current.dragItemData !== undefined) {onStart({ data: state.current.dragItemData });- state.current.status = "dragging";+ // onStart may call cancel and reset state+ if (state.current.status === "pending") {+ state.current.status = "dragging";+ }}if (state.current.status === "dragging") {diff --git a/packages/react-sdk/src/embed-template.ts b/packages/react-sdk/src/embed-template.tsindex 657bdcee89..f09e8405dd 100644--- a/packages/react-sdk/src/embed-template.ts+++ b/packages/react-sdk/src/embed-template.ts@@ -284,6 +284,7 @@ const createInstancesFromTemplate = (type: "local",id: styleSourceId,});+ // always put local style source laststyleSourceIds.push(styleSourceId);for (const styleDecl of item.styles) {styles.push({