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
Represent style source as map (#975)
Bogdan Chadkin8fb4c59c77af
diff --git a/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts b/apps/designer/app/designer/features/style-panel/shared/use-style-data.tsindex de92147ede..9dd8b77a74 100644--- a/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts+++ b/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts@@ -90,11 +90,7 @@ export const useStyleData = ({ selectedInstance, publish }: UseStyleData) => {const instanceId = selectedInstance.id;const breakpointId = selectedBreakpoint.id;const styleSourceId = selectedStyleSource.id;- replaceByOrAppendMutable(- styleSources,- selectedStyleSource,- (item) => item.id === styleSourceId- );+ styleSources.set(selectedStyleSource.id, selectedStyleSource);const selections = selectedInstanceStyleSources.map((styleSource) => styleSource.id);diff --git a/apps/designer/app/designer/features/style-panel/style-source-section.tsx b/apps/designer/app/designer/features/style-panel/style-source-section.tsxindex 00d2e40e3b..0c23c51744 100644--- a/apps/designer/app/designer/features/style-panel/style-source-section.tsx+++ b/apps/designer/app/designer/features/style-panel/style-source-section.tsx@@ -45,11 +45,7 @@ const createStyleSource = (name: string) => {(styleSources, styleSourceSelections) => {// set new style source and local if not set beforefor (const newStyleSource of newStyleSources) {- replaceByOrAppendMutable(- styleSources,- newStyleSource,- (styleSource) => styleSource.id === newStyleSource.id- );+ styleSources.set(newStyleSource.id, newStyleSource);}replaceByOrAppendMutable(styleSourceSelections,@@ -79,11 +75,7 @@ const addStyleSourceToInstace = (styleSourceId: StyleSource["id"]) => {(styleSources, styleSourceSelections) => {// set local style source if not set beforefor (const newStyleSource of selectedInstanceStyleSources) {- replaceByOrAppendMutable(- styleSources,- newStyleSource,- (styleSource) => styleSource.id === newStyleSource.id- );+ styleSources.set(newStyleSource.id, newStyleSource);}replaceByOrAppendMutable(styleSourceSelections,diff --git a/apps/designer/app/shared/copy-paste/copy-paste-instance.ts b/apps/designer/app/shared/copy-paste/copy-paste-instance.tsindex b676b3e3b8..a26f337e60 100644--- a/apps/designer/app/shared/copy-paste/copy-paste-instance.ts+++ b/apps/designer/app/shared/copy-paste/copy-paste-instance.ts@@ -5,7 +5,7 @@ import {Instance,Prop,Styles,- StyleSources,+ StyleSource,StyleSourceSelections,} from "@webstudio-is/project-build";import { utils } from "@webstudio-is/project";@@ -35,7 +35,7 @@ const InstanceData = z.object({instance: Instance,props: z.array(Prop),styleSourceSelections: StyleSourceSelections,- styleSources: StyleSources,+ styleSources: z.array(StyleSource),styles: Styles,});@@ -113,7 +113,9 @@ const pasteInstance = (data: InstanceData) => {props.set(prop.id, prop);}styleSourceSelections.push(...data.styleSourceSelections);- styleSources.push(...data.styleSources);+ for (const styleSource of data.styleSources) {+ styleSources.set(styleSource.id, styleSource);+ }styles.push(...data.styles);});diff --git a/apps/designer/app/shared/instance-utils.ts b/apps/designer/app/shared/instance-utils.tsindex 50d754d622..7369cca550 100644--- a/apps/designer/app/shared/instance-utils.ts+++ b/apps/designer/app/shared/instance-utils.ts@@ -54,9 +54,9 @@ export const deleteInstance = (targetInstanceId: Instance["id"]) => {removeByMutable(styleSourceSelections, (styleSourceSelection) =>subtreeIds.has(styleSourceSelection.instanceId));- removeByMutable(styleSources, (styleSource) =>- subtreeLocalStyleSourceIds.has(styleSource.id)- );+ for (const styleSourceId of subtreeLocalStyleSourceIds) {+ styleSources.delete(styleSourceId);+ }removeByMutable(styles, (styleDecl) =>subtreeLocalStyleSourceIds.has(styleDecl.styleSourceId));diff --git a/apps/designer/app/shared/nano-states/nano-states.ts b/apps/designer/app/shared/nano-states/nano-states.tsindex b19b1dda06..5494904d62 100644--- a/apps/designer/app/shared/nano-states/nano-states.ts+++ b/apps/designer/app/shared/nano-states/nano-states.ts@@ -105,19 +105,7 @@ export const useInstanceStyles = (instanceId: undefined | Instance["id"]) => {return instanceStyles;};-export const styleSourcesStore = atom<StyleSources>([]);-export const styleSourcesIndexStore = computed(- styleSourcesStore,- (styleSources) => {- const styleSourcesById = new Map<StyleSource["id"], StyleSource>();- for (const styleSource of styleSources) {- styleSourcesById.set(styleSource.id, styleSource);- }- return {- styleSourcesById,- };- }-);+export const styleSourcesStore = atom<StyleSources>(new Map());/*** find all non-local style sources* scoped to current tree or whole project@@ -128,17 +116,24 @@ export const availableStyleSourcesStore = shallowComputed(if (treeId === undefined) {return [];}- return styleSources.filter(- (styleSource) =>- styleSource.type !== "local" &&- (styleSource.treeId === treeId || styleSource.treeId === undefined)- );+ const availableStylesSources: StyleSource[] = [];+ for (const styleSource of styleSources.values()) {+ if (styleSource.type === "local") {+ continue;+ }+ if (styleSource.treeId === treeId || styleSource.treeId === undefined) {+ availableStylesSources.push(styleSource);+ }+ }+ return availableStylesSources;});-export const useSetStyleSources = (styleSources: StyleSources) => {+export const useSetStyleSources = (+ styleSources: [StyleSource["id"], StyleSource][]+) => {useSyncInitializeOnce(() => {- styleSourcesStore.set(styleSources);+ styleSourcesStore.set(new Map(styleSources));});};@@ -250,21 +245,20 @@ export const selectedInstanceBrowserStyleStore = atom<undefined | Style>();export const selectedInstanceStyleSourcesStore = computed([styleSourceSelectionsStore,- styleSourcesIndexStore,+ styleSourcesStore,selectedInstanceIdStore,treeIdStore,],- (styleSourceSelections, styleSourcesIndex, selectedInstanceId, treeId) => {+ (styleSourceSelections, styleSources, selectedInstanceId, treeId) => {const styleSourceIds =styleSourceSelections.find((styleSourceSelection) =>styleSourceSelection.instanceId === selectedInstanceId)?.values ?? [];- const { styleSourcesById } = styleSourcesIndex;- const selectedInstanceStyleSources: StyleSources = [];+ const selectedInstanceStyleSources: StyleSource[] = [];let hasLocal = false;for (const styleSourceId of styleSourceIds) {- const styleSource = styleSourcesById.get(styleSourceId);+ const styleSource = styleSources.get(styleSourceId);if (styleSource !== undefined) {selectedInstanceStyleSources.push(styleSource);if (styleSource.type === "local") {diff --git a/apps/designer/app/shared/tree-utils.test.ts b/apps/designer/app/shared/tree-utils.test.tsindex de368dcaec..6b2a8e18ad 100644--- a/apps/designer/app/shared/tree-utils.test.ts+++ b/apps/designer/app/shared/tree-utils.test.ts@@ -207,12 +207,12 @@ test("clone props with new ids and apply new instance ids", () => {});test("clone style sources with new ids within provided subset", () => {- const styleSources = [- createStyleSource("local", "local1"),- createStyleSource("local", "local2"),- createStyleSource("token", "token3"),- createStyleSource("local", "local4"),- ];+ const styleSources = new Map([+ ["local1", createStyleSource("local", "local1")],+ ["local2", createStyleSource("local", "local2")],+ ["token3", createStyleSource("token", "token3")],+ ["local4", createStyleSource("local", "local4")],+ ]);const subsetIds = new Set<StyleSource["id"]>(["local2", "token3"]);expect(cloneStyleSources(styleSources, subsetIds)).toEqual({clonedStyleSources: [@@ -277,14 +277,14 @@ test("clone styles with appled new style source ids", () => {test("find subtree local style sources", () => {const subtreeIds = new Set(["instance2", "instance4"]);- const styleSources = [- createStyleSource("local", "local1"),- createStyleSource("local", "local2"),- createStyleSource("token", "token3"),- createStyleSource("local", "local4"),- createStyleSource("token", "token5"),- createStyleSource("local", "local6"),- ];+ const styleSources = new Map([+ ["local1", createStyleSource("local", "local1")],+ ["local2", createStyleSource("local", "local2")],+ ["token3", createStyleSource("token", "token3")],+ ["local4", createStyleSource("local", "local4")],+ ["token5", createStyleSource("token", "token5")],+ ["local6", createStyleSource("local", "local6")],+ ]);const styleSourceSelections = [createStyleSourceSelection("instance1", ["local1"]),createStyleSourceSelection("instance2", ["local2"]),diff --git a/apps/designer/app/shared/tree-utils.ts b/apps/designer/app/shared/tree-utils.tsindex 5b3fb9d6b8..f50ae9ff26 100644--- a/apps/designer/app/shared/tree-utils.ts+++ b/apps/designer/app/shared/tree-utils.ts@@ -170,8 +170,8 @@ export const cloneStyleSources = (subsetIds: Set<StyleSource["id"]>) => {const clonedStyleSourceIds = new Map<Instance["id"], Instance["id"]>();- const clonedStyleSources: StyleSources = [];- for (const styleSource of styleSources) {+ const clonedStyleSources: StyleSource[] = [];+ for (const styleSource of styleSources.values()) {if (subsetIds.has(styleSource.id) === false) {continue;}@@ -234,7 +234,7 @@ export const findSubtreeLocalStyleSources = (styleSourceSelections: StyleSourceSelections) => {const localStyleSourceIds = new Set<StyleSource["id"]>();- for (const styleSource of styleSources) {+ for (const styleSource of styleSources.values()) {if (styleSource.type === "local") {localStyleSourceIds.add(styleSource.id);}diff --git a/packages/project-build/src/schema/style-sources.ts b/packages/project-build/src/schema/style-sources.tsindex bf6bfc6660..f81b1a9ff1 100644--- a/packages/project-build/src/schema/style-sources.ts+++ b/packages/project-build/src/schema/style-sources.ts@@ -19,7 +19,11 @@ export const StyleSource = z.union([StyleSourceToken, StyleSourceLocal]);export type StyleSource = z.infer<typeof StyleSource>;-export const StyleSources = z.array(StyleSource);+export const StyleSourcesList = z.array(StyleSource);++export type StyleSourcesList = z.infer<typeof StyleSourcesList>;++export const StyleSources = z.map(StyleSourceId, StyleSource);export type StyleSources = z.infer<typeof StyleSources>;diff --git a/packages/project/src/db/build.ts b/packages/project/src/db/build.tsindex 8dbcd53793..5c59a591cf 100644--- a/packages/project/src/db/build.ts+++ b/packages/project/src/db/build.ts@@ -6,14 +6,14 @@ import {Project,} from "@webstudio-is/prisma-client";import { Breakpoints } from "@webstudio-is/css-data";-import { StyleSources } from "@webstudio-is/project-build";import * as db from ".";import { Build, Page, Pages } from "../shared/schema";import * as pagesUtils from "../shared/pages";import { parseStyles, serializeStyles } from "./styles";import type { AppContext } from "@webstudio-is/trpc-interface";+import { parseStyleSources, serializeStyleSources } from "./style-sources";-export const parseBuild = async (build: DbBuild): Promise<Build> => {+const parseBuild = async (build: DbBuild): Promise<Build> => {const pages = Pages.parse(JSON.parse(build.pages));return {id: build.id,@@ -24,7 +24,7 @@ export const parseBuild = async (build: DbBuild): Promise<Build> => {pages,breakpoints: Breakpoints.parse(JSON.parse(build.breakpoints)),styles: await parseStyles(build.projectId, build.styles),- styleSources: StyleSources.parse(JSON.parse(build.styleSources)),+ styleSources: Array.from(parseStyleSources(build.styleSources)),};};@@ -201,7 +201,7 @@ export const deletePage = async ({const createPages = async ({ projectId, buildId }: { projectId: Project["id"]; buildId: Build["id"] },- context: AppContext,+ _context: AppContext,client: Prisma.TransactionClient = prisma) => {const tree = await db.tree.create(@@ -322,7 +322,9 @@ export async function create(props.sourceBuild?.breakpoints ?? db.breakpoints.createValues()),styles: serializeStyles(props.sourceBuild?.styles ?? []),- styleSources: JSON.stringify(props.sourceBuild?.styleSources ?? []),+ styleSources: serializeStyleSources(+ new Map(props.sourceBuild?.styleSources ?? [])+ ),isDev: props.env === "dev",isProd: props.env === "prod",},diff --git a/packages/project/src/db/style-sources.ts b/packages/project/src/db/style-sources.tsindex eb54345650..ba5a40d984 100644--- a/packages/project/src/db/style-sources.ts+++ b/packages/project/src/db/style-sources.ts@@ -4,9 +4,23 @@ import {authorizeProject,type AppContext,} from "@webstudio-is/trpc-interface/server";-import { StyleSources } from "@webstudio-is/project-build";+import { StyleSourcesList, StyleSources } from "@webstudio-is/project-build";import type { Build, Project } from "../shared/schema";+export const parseStyleSources = (styleSourceString: string): StyleSources => {+ const styleSourcesList = StyleSourcesList.parse(+ JSON.parse(styleSourceString)+ );+ return new Map(styleSourcesList.map((item) => [item.id, item]));+};++export const serializeStyleSources = (styleSourcesMap: StyleSources) => {+ const styleSourcesList: StyleSourcesList = Array.from(+ styleSourcesMap.values()+ );+ return JSON.stringify(styleSourcesList);+};+export const patch = async ({ buildId, projectId }: { buildId: Build["id"]; projectId: Project["id"] },patches: Array<Patch>,@@ -30,7 +44,7 @@ export const patch = async (return;}- const styleSources = StyleSources.parse(JSON.parse(build.styleSources));+ const styleSources = parseStyleSources(build.styleSources);const patchedStyleSources = StyleSources.parse(applyPatches(styleSources, patches)@@ -38,7 +52,7 @@ export const patch = async (await prisma.build.update({data: {- styleSources: JSON.stringify(patchedStyleSources),+ styleSources: serializeStyleSources(patchedStyleSources),},where: {id_projectId: { projectId, id: buildId },diff --git a/packages/project/src/shared/schema.ts b/packages/project/src/shared/schema.tsindex d17c202d3e..7926d8969c 100644--- a/packages/project/src/shared/schema.ts+++ b/packages/project/src/shared/schema.ts@@ -1,6 +1,6 @@import { z, type ZodType } from "zod";import type { Breakpoints } from "@webstudio-is/css-data";-import type { Styles, StyleSources } from "@webstudio-is/project-build";+import type { Styles, StyleSource } from "@webstudio-is/project-build";import type { Data } from "@webstudio-is/react-sdk";const MIN_TITLE_LENGTH = 2;@@ -100,7 +100,7 @@ export type Build = {pages: Pages;breakpoints: Breakpoints;styles: Styles;- styleSources: StyleSources;+ styleSources: [StyleSource["id"], StyleSource][];};export type CanvasData = Data & {