Skip to content

Commit

Permalink
Move to new Excalidraw APIs for 0.17 update (#5277)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivailop7 authored Nov 23, 2023
1 parent 7ec15be commit 91864ff
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 19 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/lexical-playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"preview": "vite preview"
},
"dependencies": {
"@excalidraw/excalidraw": "^0.15.3",
"@excalidraw/excalidraw": "^0.17.0",
"@lexical/clipboard": "0.12.4",
"@lexical/code": "0.12.4",
"@lexical/file": "0.12.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ type Props = {
) => void;
};

export const useCallbackRefState = () => {
const [refValue, setRefValue] =
React.useState<ExcalidrawImperativeAPI | null>(null);
const refCallback = React.useCallback(
(value: ExcalidrawImperativeAPI | null) => setRefValue(value),
[],
);
return [refValue, refCallback] as const;
};

/**
* @explorer-desc
* A component which renders a modal with Excalidraw (a painting app)
Expand All @@ -77,7 +87,7 @@ export default function ExcalidrawModal({
onClose,
}: Props): ReactPortal | null {
const excaliDrawModelRef = useRef<HTMLDivElement | null>(null);
const excaliDrawSceneRef = useRef<ExcalidrawImperativeAPI>(null);
const [excalidrawAPI, excalidrawAPIRefCallback] = useCallbackRefState();
const [discardModalOpen, setDiscardModalOpen] = useState(false);
const [elements, setElements] =
useState<ReadonlyArray<ExcalidrawElementFragment>>(initialElements);
Expand Down Expand Up @@ -139,7 +149,7 @@ export default function ExcalidrawModal({

const save = () => {
if (elements.filter((el) => !el.isDeleted).length > 0) {
const appState = excaliDrawSceneRef?.current?.getAppState();
const appState = excalidrawAPI?.getAppState();
// We only need a subset of the state
const partialState: Partial<AppState> = {
exportBackground: appState.exportBackground,
Expand Down Expand Up @@ -212,12 +222,6 @@ export default function ExcalidrawModal({
setFiles(fls);
};

// This is a hacky work-around for Excalidraw + Vite.
// In DEV, Vite pulls this in fine, in prod it doesn't. It seems
// like a module resolution issue with ESM vs CJS?
const _Excalidraw =
Excalidraw.$$typeof != null ? Excalidraw : Excalidraw.default;

return createPortal(
<div className="ExcalidrawModal__overlay" role="dialog">
<div
Expand All @@ -226,9 +230,9 @@ export default function ExcalidrawModal({
tabIndex={-1}>
<div className="ExcalidrawModal__row">
{discardModalOpen && <ShowDiscardDialog />}
<_Excalidraw
<Excalidraw
onChange={onChange}
ref={excaliDrawSceneRef}
excalidrawAPI={excalidrawAPIRefCallback}
initialData={{
appState: initialAppState || {isLoading: false},
elements: initialElements,
Expand Down
3 changes: 3 additions & 0 deletions packages/lexical-playground/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ const moduleResolution = [

// https://vitejs.dev/config/
export default defineConfig({
define: {
"process.env.IS_PREACT": process.env.IS_PREACT,
},
plugins: [
replaceCodePlugin({
replacements: [
Expand Down
3 changes: 3 additions & 0 deletions packages/lexical-playground/vite.prod.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ const moduleResolution = [

// https://vitejs.dev/config/
export default defineConfig({
define: {
"process.env.IS_PREACT": process.env.IS_PREACT,
},
plugins: [
replaceCodePlugin({
replacements: [
Expand Down

2 comments on commit 91864ff

@vercel
Copy link

@vercel vercel bot commented on 91864ff Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical – ./packages/lexical-website

lexical-git-main-fbopensource.vercel.app
lexical-fbopensource.vercel.app
www.lexical.dev
lexicaljs.com
lexical.dev
lexicaljs.org

@vercel
Copy link

@vercel vercel bot commented on 91864ff Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical-playground – ./packages/lexical-playground

lexical-playground-fbopensource.vercel.app
playground.lexical.dev
lexical-playground-git-main-fbopensource.vercel.app
lexical-playground.vercel.app

Please sign in to comment.