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
fix: fix undo/redo (#2408)
Bogdan Chadkinaf35ba937d0c
diff --git a/apps/builder/app/builder/shared/sync/sync-server.ts b/apps/builder/app/builder/shared/sync/sync-server.tsindex 930ab21c0c..edeafb85d3 100644--- a/apps/builder/app/builder/shared/sync/sync-server.ts+++ b/apps/builder/app/builder/shared/sync/sync-server.ts@@ -1,7 +1,7 @@import { useEffect } from "react";import { useBeforeUnload } from "react-use";import { atom } from "nanostores";-import { sync } from "immerhin";+import store, { sync } from "immerhin";import { Project } from "@webstudio-is/project";import type { Build } from "@webstudio-is/project-build";import type { AuthPermit } from "@webstudio-is/trpc-interface/index.server";@@ -25,26 +25,11 @@ const MAX_ALLOWED_API_ERRORS = 5;const INTERVAL_ERROR = 5000;const MAX_INTERVAL_ERROR = 2 * 60000;-const persistedNamespaces = new Set([- "pages",- "breakpoints",- "instances",- "styles",- "styleSources",- "styleSourceSelections",- "props",- "dataSources",- "assets",-]);--const filterTransactions = (- transactions: ReturnType<typeof sync>,- namespaces: Set<string>-) => {+const filterServerTransactions = (transactions: ReturnType<typeof sync>) => {const filteredTransactions: ReturnType<typeof sync> = [];for (const transaction of transactions) {const filteredChanges = transaction.changes.filter((change) =>- namespaces.has(change.namespace)+ store.containers.has(change.namespace));if (filteredChanges.length !== 0) {filteredTransactions.push({@@ -300,7 +285,7 @@ const useSyncProject = async ({});const updateProjectTransactions = () => {- const transactions = filterTransactions(sync(), persistedNamespaces);+ const transactions = filterServerTransactions(sync());if (transactions.length === 0) {return;}diff --git a/apps/builder/app/shared/commands-emitter.ts b/apps/builder/app/shared/commands-emitter.tsindex 57b799c12b..3558321113 100644--- a/apps/builder/app/shared/commands-emitter.ts+++ b/apps/builder/app/shared/commands-emitter.ts@@ -1,7 +1,7 @@import { isHotkeyPressed } from "react-hotkeys-hook";import { atom, onMount } from "nanostores";-import store from "immerhin";import { $publisher, subscribe } from "~/shared/pubsub";+import { clientImmerhinStore } from "~/shared/sync";type CommandMeta<CommandName extends string> = {// @todo category, description@@ -61,11 +61,14 @@ export const createCommandsEmitter = <CommandName extends string>({// schedule store.set to the next tick// so store.listen is executed after store.set belowPromise.resolve().then(() => {- store.createTransaction([$commandMetas], (commandMetas) => {- for (const { handler, ...meta } of commands) {- commandMetas.set(meta.name, meta);+ clientImmerhinStore.createTransaction(+ [$commandMetas],+ (commandMetas) => {+ for (const { handler, ...meta } of commands) {+ commandMetas.set(meta.name, meta);+ }}- });+ );});});}diff --git a/apps/builder/app/shared/sync/sync-stores.ts b/apps/builder/app/shared/sync/sync-stores.tsindex 0c94609796..0f3053e2fd 100644--- a/apps/builder/app/shared/sync/sync-stores.ts+++ b/apps/builder/app/shared/sync/sync-stores.ts@@ -1,4 +1,4 @@-import store, { type Change } from "immerhin";+import store, { Store, type Change } from "immerhin";import { enableMapSet } from "immer";import { atom, type WritableAtom } from "nanostores";import { useEffect } from "react";@@ -53,11 +53,13 @@ declare module "~/shared/pubsub" {sendStoreChanges: {// distinct source to avoid infinite loopsource: SyncEventSource;+ namespace: "client" | "server";changes: Change[];};}}+export const clientImmerhinStore = new Store();const clientStores = new Map<string, WritableAtom<unknown>>();const initializedStores = new Set<string>();@@ -72,7 +74,7 @@ export const registerContainers = () => {store.register("props", propsStore);store.register("dataSources", dataSourcesStore);store.register("assets", assetsStore);- store.register("commandMetas", $commandMetas);+ clientImmerhinStore.register("commandMetas", $commandMetas);// synchronize whole statesclientStores.set("project", projectStore);clientStores.set("dataSourceVariables", dataSourceVariablesStore);@@ -131,12 +133,17 @@ export const registerContainers = () => {const syncStoresChanges = (name: SyncEventSource, publish: Publish) => {const unsubscribeRemoteChanges = subscribe("sendStoreChanges",- ({ source, changes }) => {+ ({ source, namespace, changes }) => {/// prevent reapplying own changesif (source === name) {return;}- store.createTransactionFromChanges(changes, "remote");+ if (namespace === "server") {+ store.createTransactionFromChanges(changes, "remote");+ }+ if (namespace === "client") {+ clientImmerhinStore.createTransactionFromChanges(changes, "remote");+ }});@@ -151,6 +158,25 @@ const syncStoresChanges = (name: SyncEventSource, publish: Publish) => {type: "sendStoreChanges",payload: {source: name,+ namespace: "server",+ changes,+ },+ });+ }+ );++ const unsubscribeClientImmerhinStoreChanges = clientImmerhinStore.subscribe(+ (_transactionId, changes, source) => {+ // prevent sending remote patches back+ if (source === "remote") {+ return;+ }++ publish({+ type: "sendStoreChanges",+ payload: {+ source: name,+ namespace: "client",changes,},});@@ -160,6 +186,7 @@ const syncStoresChanges = (name: SyncEventSource, publish: Publish) => {return () => {unsubscribeRemoteChanges();unsubscribeStoreChanges();+ unsubscribeClientImmerhinStoreChanges();};};@@ -179,6 +206,10 @@ const syncStoresState = (name: SyncEventSource, publish: Publish) => {if (container) {container.set(value);}+ const clientContainer = clientImmerhinStore.containers.get(namespace);+ if (clientContainer) {+ clientContainer.set(value);+ }// apply state stores dataconst stateStore = clientStores.get(namespace);if (stateStore) {