mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-11-20 12:44:38 +01:00
build: decouple package deps and introduce yarn workspaces (#7415)
* feat: decouple package deps and introduce yarn workspaces * update root directory * fix * fix scripts * fix lint * update path in scripts * remove yarn.lock files from packages * ignore workspace * dummy * dummy * remove comment check * revert workflow changes * ignore ws when installing gh actions * remove log * update path * fix * fix typo
This commit is contained in:
100
packages/excalidraw/components/hoc/withInternalFallback.test.tsx
Normal file
100
packages/excalidraw/components/hoc/withInternalFallback.test.tsx
Normal file
@@ -0,0 +1,100 @@
|
||||
import { render, queryAllByTestId } from "../../tests/test-utils";
|
||||
import { Excalidraw, MainMenu } from "../../index";
|
||||
|
||||
describe("Test internal component fallback rendering", () => {
|
||||
it("should render only one menu per excalidraw instance (custom menu first scenario)", async () => {
|
||||
const { container } = await render(
|
||||
<div>
|
||||
<Excalidraw>
|
||||
<MainMenu>test</MainMenu>
|
||||
</Excalidraw>
|
||||
<Excalidraw />
|
||||
</div>,
|
||||
);
|
||||
|
||||
expect(queryAllByTestId(container, "main-menu-trigger")?.length).toBe(2);
|
||||
|
||||
const excalContainers = container.querySelectorAll<HTMLDivElement>(
|
||||
".excalidraw-container",
|
||||
);
|
||||
|
||||
expect(
|
||||
queryAllByTestId(excalContainers[0], "main-menu-trigger")?.length,
|
||||
).toBe(1);
|
||||
expect(
|
||||
queryAllByTestId(excalContainers[1], "main-menu-trigger")?.length,
|
||||
).toBe(1);
|
||||
});
|
||||
|
||||
it("should render only one menu per excalidraw instance (default menu first scenario)", async () => {
|
||||
const { container } = await render(
|
||||
<div>
|
||||
<Excalidraw />
|
||||
<Excalidraw>
|
||||
<MainMenu>test</MainMenu>
|
||||
</Excalidraw>
|
||||
</div>,
|
||||
);
|
||||
|
||||
expect(queryAllByTestId(container, "main-menu-trigger")?.length).toBe(2);
|
||||
|
||||
const excalContainers = container.querySelectorAll<HTMLDivElement>(
|
||||
".excalidraw-container",
|
||||
);
|
||||
|
||||
expect(
|
||||
queryAllByTestId(excalContainers[0], "main-menu-trigger")?.length,
|
||||
).toBe(1);
|
||||
expect(
|
||||
queryAllByTestId(excalContainers[1], "main-menu-trigger")?.length,
|
||||
).toBe(1);
|
||||
});
|
||||
|
||||
it("should render only one menu per excalidraw instance (two custom menus scenario)", async () => {
|
||||
const { container } = await render(
|
||||
<div>
|
||||
<Excalidraw>
|
||||
<MainMenu>test</MainMenu>
|
||||
</Excalidraw>
|
||||
<Excalidraw>
|
||||
<MainMenu>test</MainMenu>
|
||||
</Excalidraw>
|
||||
</div>,
|
||||
);
|
||||
|
||||
expect(queryAllByTestId(container, "main-menu-trigger")?.length).toBe(2);
|
||||
|
||||
const excalContainers = container.querySelectorAll<HTMLDivElement>(
|
||||
".excalidraw-container",
|
||||
);
|
||||
|
||||
expect(
|
||||
queryAllByTestId(excalContainers[0], "main-menu-trigger")?.length,
|
||||
).toBe(1);
|
||||
expect(
|
||||
queryAllByTestId(excalContainers[1], "main-menu-trigger")?.length,
|
||||
).toBe(1);
|
||||
});
|
||||
|
||||
it("should render only one menu per excalidraw instance (two default menus scenario)", async () => {
|
||||
const { container } = await render(
|
||||
<div>
|
||||
<Excalidraw />
|
||||
<Excalidraw />
|
||||
</div>,
|
||||
);
|
||||
|
||||
expect(queryAllByTestId(container, "main-menu-trigger")?.length).toBe(2);
|
||||
|
||||
const excalContainers = container.querySelectorAll<HTMLDivElement>(
|
||||
".excalidraw-container",
|
||||
);
|
||||
|
||||
expect(
|
||||
queryAllByTestId(excalContainers[0], "main-menu-trigger")?.length,
|
||||
).toBe(1);
|
||||
expect(
|
||||
queryAllByTestId(excalContainers[1], "main-menu-trigger")?.length,
|
||||
).toBe(1);
|
||||
});
|
||||
});
|
||||
73
packages/excalidraw/components/hoc/withInternalFallback.tsx
Normal file
73
packages/excalidraw/components/hoc/withInternalFallback.tsx
Normal file
@@ -0,0 +1,73 @@
|
||||
import { atom, useAtom } from "jotai";
|
||||
import React, { useLayoutEffect, useRef } from "react";
|
||||
import { useTunnels } from "../../context/tunnels";
|
||||
|
||||
export const withInternalFallback = <P,>(
|
||||
componentName: string,
|
||||
Component: React.FC<P>,
|
||||
) => {
|
||||
const renderAtom = atom(0);
|
||||
|
||||
const WrapperComponent: React.FC<
|
||||
P & {
|
||||
__fallback?: boolean;
|
||||
}
|
||||
> = (props) => {
|
||||
const { jotaiScope } = useTunnels();
|
||||
// for rerenders
|
||||
const [, setCounter] = useAtom(renderAtom, jotaiScope);
|
||||
// for initial & subsequent renders. Tracked as component state
|
||||
// due to excalidraw multi-instance scanerios.
|
||||
const metaRef = useRef({
|
||||
// flag set on initial render to tell the fallback component to skip the
|
||||
// render until mount counter are initialized. This is because the counter
|
||||
// is initialized in an effect, and thus we could end rendering both
|
||||
// components at the same time until counter is initialized.
|
||||
preferHost: false,
|
||||
counter: 0,
|
||||
});
|
||||
|
||||
useLayoutEffect(() => {
|
||||
const meta = metaRef.current;
|
||||
setCounter((c) => {
|
||||
const next = c + 1;
|
||||
meta.counter = next;
|
||||
|
||||
return next;
|
||||
});
|
||||
return () => {
|
||||
setCounter((c) => {
|
||||
const next = c - 1;
|
||||
meta.counter = next;
|
||||
if (!next) {
|
||||
meta.preferHost = false;
|
||||
}
|
||||
return next;
|
||||
});
|
||||
};
|
||||
}, [setCounter]);
|
||||
|
||||
if (!props.__fallback) {
|
||||
metaRef.current.preferHost = true;
|
||||
}
|
||||
|
||||
// ensure we don't render fallback and host components at the same time
|
||||
if (
|
||||
// either before the counters are initialized
|
||||
(!metaRef.current.counter &&
|
||||
props.__fallback &&
|
||||
metaRef.current.preferHost) ||
|
||||
// or after the counters are initialized, and both are rendered
|
||||
// (this is the default when host renders as well)
|
||||
(metaRef.current.counter > 1 && props.__fallback)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return <Component {...props} />;
|
||||
};
|
||||
|
||||
WrapperComponent.displayName = componentName;
|
||||
|
||||
return WrapperComponent;
|
||||
};
|
||||
Reference in New Issue
Block a user