From 47efacddd1ef179535996c348c75f06e23eccbf4 Mon Sep 17 00:00:00 2001 From: Mark Tolmacs Date: Mon, 28 Jul 2025 15:41:00 +0200 Subject: [PATCH] Add watchState debug method to window.h --- packages/excalidraw/components/App.tsx | 44 ++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/packages/excalidraw/components/App.tsx b/packages/excalidraw/components/App.tsx index 6ac387d2b6..c237b8b782 100644 --- a/packages/excalidraw/components/App.tsx +++ b/packages/excalidraw/components/App.tsx @@ -2491,6 +2491,47 @@ class App extends React.Component { return this.setState(...args); }, }, + watchState: { + configurable: true, + value: ( + callback: + | (( + prevState: Parameters, + nextState: Parameters | null, + ) => void) + | undefined, + ) => { + if (callback) { + (window as any).__originalSetState = this.setState; + this.setState = new Proxy(this.setState, { + apply: (target, thisArg, [state, cb]) => { + const prevState = thisArg.state; + let newState: Parameters | null = null; + + // Log state change for debugging + if (typeof state === "function") { + newState = state(prevState, this.props); + } else if (state) { + newState = state; + } + + try { + callback(prevState, newState); + } catch (error) { + console.warn("Error in watchState callback:", error); + } + + if (newState) { + target.bind(thisArg)(newState as any, cb); + } + }, + }); + } else if ((window as any).__originalSetState) { + this.setState = (window as any).__originalSetState; + delete (window as any).__originalSetState; + } + }, + }, app: { configurable: true, value: this, @@ -11515,6 +11556,8 @@ class App extends React.Component { }; } + watchState = () => {}; + private async updateLanguage() { const currentLang = languages.find((lang) => lang.code === this.props.langCode) || @@ -11534,6 +11577,7 @@ declare global { elements: readonly ExcalidrawElement[]; state: AppState; setState: React.Component["setState"]; + watchState: (prev: any, next: any) => void | undefined; app: InstanceType; history: History; store: Store;