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
naming things is hard
Oleg Isonen36c35f861d55
diff --git a/app/canvas/shared/tree.ts b/app/canvas/shared/tree.tsindex fc93b2a51d..d855bc0c7c 100644--- a/app/canvas/shared/tree.ts+++ b/app/canvas/shared/tree.ts@@ -13,7 +13,7 @@ import {useSelectedInstance,} from "./nano-values";import { useSubscribe } from "./pubsub";-import { transaction } from "~/lib/sync-engine";+import { createTransaction } from "~/lib/sync-engine";export const usePopulateRootInstance = (tree: Tree) => {const [, setRootInstance] = useRootInstance();@@ -96,7 +96,7 @@ export const useDeleteInstance = () => {// The way it is now it will actually still enable parallel deletion props editing and restoration.// Contra: we are piling them up.// Potentially we could also solve this by periodically removing unused props after while when instance was deleted- transaction([rootInstanceContainer], (rootInstance) => {+ createTransaction([rootInstanceContainer], (rootInstance) => {if (rootInstance !== undefined) {deleteInstanceMutable(rootInstance, id);}diff --git a/app/lib/sync-engine/index.ts b/app/lib/sync-engine/index.tsindex 7b33afa502..02eaba738e 100644--- a/app/lib/sync-engine/index.ts+++ b/app/lib/sync-engine/index.ts@@ -1,3 +1,3 @@-export { register, transaction } from "./store";+export { register, createTransaction } from "./store";export { sync, type SyncItem } from "./sync-queue";export { undo, redo } from "./transactions-manager";diff --git a/app/lib/sync-engine/store.ts b/app/lib/sync-engine/store.tsindex a4b0a45d9e..eca44d04ec 100644--- a/app/lib/sync-engine/store.ts+++ b/app/lib/sync-engine/store.ts@@ -25,7 +25,9 @@ type UnwrapContainers<Containers extends Array<ValueContainer<unknown>>> = {: never;};-export const transaction = <Containers extends ValueContainer<Any>[]>(+export const createTransaction = <+ Containers extends Array<ValueContainer<Any>>+>(containers: [...Containers],recipe: (...values: UnwrapContainers<Containers>) => void): UnwrapContainers<Containers> => {@@ -35,7 +37,7 @@ export const transaction = <Containers extends ValueContainer<Any>[]>(drafts.push(createDraft(container.value));}recipe(...drafts);- const current = new Transaction();+ const transaction = new Transaction();const values = [] as unknown as Values;drafts.forEach((draft, index) => {const namespace = registry.get(containers[index]);@@ -47,7 +49,7 @@ export const transaction = <Containers extends ValueContainer<Any>[]>(const value = finishDraft(draft,(patches: Array<Patch>, revisePatches: Array<Patch>) => {- current.add({+ transaction.add({namespace,patches,revisePatches,@@ -57,7 +59,6 @@ export const transaction = <Containers extends ValueContainer<Any>[]>();values.push(value);});- current.apply("patches");- add(current);+ add(transaction);return values;};diff --git a/app/lib/sync-engine/transaction.ts b/app/lib/sync-engine/transaction.tsindex c6c5cf06a2..c4b3dcbf29 100644--- a/app/lib/sync-engine/transaction.ts+++ b/app/lib/sync-engine/transaction.ts@@ -20,9 +20,15 @@ export class Transaction {constructor() {this.id = ObjectId().toString();}- apply(type: "patches" | "revisePatches") {+ applyPatches() {for (const change of this.specs) {- const value = applyPatches(change.container.value, change[type]);+ const value = applyPatches(change.container.value, change.patches);+ change.container.dispatch(value);+ }+ }+ applyRevisePatches() {+ for (const change of this.specs) {+ const value = applyPatches(change.container.value, change.revisePatches);change.container.dispatch(value);}}diff --git a/app/lib/sync-engine/transactions-manager.ts b/app/lib/sync-engine/transactions-manager.tsindex 84a6ba7db4..73639253f5 100644--- a/app/lib/sync-engine/transactions-manager.ts+++ b/app/lib/sync-engine/transactions-manager.ts@@ -3,29 +3,33 @@ import { type Transaction } from "./transaction";const max = 100;-const current: Array<Transaction> = [];-const undone: Array<Transaction> = [];+const currentStack: Array<Transaction> = [];+const undoneStack: Array<Transaction> = [];export const add = (transaction: Transaction) => {- current.push(transaction);+ transaction.applyPatches();+ currentStack.push(transaction);enqueue(transaction.id, transaction.getChanges());- if (current.length > max) {- current.shift();+ if (currentStack.length > max) {+ currentStack.shift();}+ // After we add a change, we can't redo something we have undone before.+ // It would make undo unpredictable, because there are new changes.+ undoneStack.splice(0);};export const undo = () => {- const transaction = current.pop();+ const transaction = currentStack.pop();if (transaction === undefined) return;+ transaction.applyRevisePatches();enqueue(transaction.id, transaction.getReviseChanges());- undone.push(transaction);- transaction.apply("revisePatches");+ undoneStack.push(transaction);};export const redo = () => {- const transaction = undone.pop();+ const transaction = undoneStack.pop();if (transaction === undefined) return;+ transaction.applyPatches();+ currentStack.push(transaction);enqueue(transaction.id, transaction.getChanges());- add(transaction);- transaction.apply("patches");};