Skip to content

Commit

Permalink
feat: render embeds lazily (excalidraw#7519)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwelle committed Jan 4, 2024
1 parent aecf608 commit 23b3e12
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions packages/excalidraw/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ class App extends React.Component<AppProps, AppState> {
public files: BinaryFiles = {};
public imageCache: AppClassProperties["imageCache"] = new Map();
private iFrameRefs = new Map<ExcalidrawElement["id"], HTMLIFrameElement>();
private initializedEmbeds = new Set<ExcalidrawIframeLikeElement["id"]>();

hitLinkElement?: NonDeletedExcalidrawElement;
lastPointerDownEvent: React.PointerEvent<HTMLElement> | null = null;
Expand Down Expand Up @@ -900,6 +901,23 @@ class App extends React.Component<AppProps, AppState> {
this.state,
);

const isVisible = isElementInViewport(
el,
normalizedWidth,
normalizedHeight,
this.state,
);
const hasBeenInitialized = this.initializedEmbeds.has(el.id);

if (isVisible && !hasBeenInitialized) {
this.initializedEmbeds.add(el.id);
}
const shouldRender = isVisible || hasBeenInitialized;

if (!shouldRender) {
return null;
}

let src: IframeData | null;

if (isIframeElement(el)) {
Expand Down Expand Up @@ -1041,14 +1059,6 @@ class App extends React.Component<AppProps, AppState> {
src = getEmbedLink(toValidURL(el.link || ""));
}

// console.log({ src });

const isVisible = isElementInViewport(
el,
normalizedWidth,
normalizedHeight,
this.state,
);
const isActive =
this.state.activeEmbeddable?.element === el &&
this.state.activeEmbeddable?.state === "active";
Expand Down

0 comments on commit 23b3e12

Please sign in to comment.