From b0404b10b6d775d4a0bc48072947f95e5753e117 Mon Sep 17 00:00:00 2001 From: David Luzar <5153846+dwelle@users.noreply.github.com> Date: Mon, 23 Feb 2026 20:20:37 +0100 Subject: [PATCH] chore(debug): add debug.logChanged() and make easy to import (#10828) --- {excalidraw-app => packages/common}/debug.ts | 74 +++++++++++++++++--- packages/common/src/index.ts | 1 + 2 files changed, 67 insertions(+), 8 deletions(-) rename {excalidraw-app => packages/common}/debug.ts (77%) diff --git a/excalidraw-app/debug.ts b/packages/common/debug.ts similarity index 77% rename from excalidraw-app/debug.ts rename to packages/common/debug.ts index 1ef136fb02..efb6fefb46 100644 --- a/excalidraw-app/debug.ts +++ b/packages/common/debug.ts @@ -1,9 +1,3 @@ -declare global { - interface Window { - debug: typeof Debug; - } -} - const lessPrecise = (num: number, precision = 5) => parseFloat(num.toPrecision(precision)); @@ -157,6 +151,70 @@ export class Debug { return ret; }; }; + + private static CHANGED_CACHE: Record> = {}; + + public static logChanged(name: string, obj: Record) { + const prev = Debug.CHANGED_CACHE[name]; + + Debug.CHANGED_CACHE[name] = obj; + + if (!prev) { + return; + } + + const allKeys = new Set([...Object.keys(prev), ...Object.keys(obj)]); + const changed: Record = {}; + + for (const key of allKeys) { + const prevVal = prev[key]; + const nextVal = obj[key]; + if (!deepEqual(prevVal, nextVal)) { + changed[key] = { prev: prevVal, next: nextVal }; + } + } + + if (Object.keys(changed).length > 0) { + console.info(`[${name}] changed:`, changed); + } + } +} + +function deepEqual(a: unknown, b: unknown): boolean { + if (Object.is(a, b)) { + return true; + } + + if ( + a === null || + b === null || + typeof a !== "object" || + typeof b !== "object" + ) { + return false; + } + + if (Array.isArray(a) !== Array.isArray(b)) { + return false; + } + + const keysA = Object.keys(a as Record); + const keysB = Object.keys(b as Record); + + if (keysA.length !== keysB.length) { + return false; + } + + for (const key of keysA) { + if ( + !deepEqual( + (a as Record)[key], + (b as Record)[key], + ) + ) { + return false; + } + } + + return true; } -//@ts-ignore -window.debug = Debug; diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index 7d6bf5b0dc..ca5397ddd1 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -12,3 +12,4 @@ export * from "./url"; export * from "./utils"; export * from "./emitter"; export * from "./editorInterface"; +export { Debug } from "../debug";