Skip to content

Virtual whiteboard for sketching hand-drawn like diagrams

feat(editor): autoplay Google Drive videos when the embed is activated (#12246)

David Luzar4c00f31ddcc2
diff --git a/packages/excalidraw/components/App.tsx b/packages/excalidraw/components/App.tsx
index 848532a8fd..2c9a8eaa53 100644
--- a/packages/excalidraw/components/App.tsx
+++ b/packages/excalidraw/components/App.tsx
@@ -579,6 +579,11 @@ const YOUTUBE_VIDEO_STATES = new Map<
ValueOf<typeof YOUTUBE_STATES>
>();
+/**
+ * Google Drive embeds already reloaded with autoplay
+ */
+const GOOGLE_DRIVE_AUTOPLAYED = new WeakSet<HTMLIFrameElement>();
+
const MAX_EMBEDDABLE_VIEWPORT_SCALE = 4;
let lastPointerUp: (() => void) | null = null;
@@ -1692,6 +1697,22 @@ class App extends React.Component<AppProps, AppState> {
);
}
+ // Drive's player has no postMessage API, but its preview page honors
+ // `autoplay=1`, so reload the embed with it on first activation.
+ // `location.replace` keeps the reload out of the session history and
+ // leaves `src` alone, so a remounted iframe doesn't autoplay.
+ if (
+ iframe.src.startsWith("https://drive.google.com/file/d/") &&
+ // we can't update unconditionally, since
+ // change src reloads the iframe
+ !GOOGLE_DRIVE_AUTOPLAYED.has(iframe)
+ ) {
+ GOOGLE_DRIVE_AUTOPLAYED.add(iframe);
+ const url = new URL(iframe.src);
+ url.searchParams.set("autoplay", "1");
+ iframe.contentWindow.location.replace(url.toString());
+ }
+
return true;
}
diff --git a/packages/excalidraw/tests/embeddable.test.tsx b/packages/excalidraw/tests/embeddable.test.tsx
new file mode 100644
index 0000000000..7749371f87
--- /dev/null
+++ b/packages/excalidraw/tests/embeddable.test.tsx
@@ -0,0 +1,75 @@
+import { vi } from "vitest";
+
+import { Excalidraw } from "../index";
+
+import { API } from "./helpers/api";
+import { Pointer } from "./helpers/ui";
+import {
+ mockBoundingClientRect,
+ render,
+ restoreOriginalGetBoundingClientRect,
+ waitFor,
+} from "./test-utils";
+
+const { h } = window;
+
+const mouse = new Pointer("mouse");
+
+describe("embeddables", () => {
+ beforeEach(() => {
+ mockBoundingClientRect();
+ });
+
+ afterEach(() => {
+ restoreOriginalGetBoundingClientRect();
+ });
+
+ it("autoplays a Google Drive video on first activation only", async () => {
+ await render(<Excalidraw viewModeEnabled />);
+ await waitFor(() => expect(h.state.width).toBe(200));
+
+ // link set upfront: embeds are validated (and cached) once per element
+ API.setElements([
+ {
+ ...API.createElement({
+ type: "embeddable",
+ x: 20,
+ y: 20,
+ width: 120,
+ height: 90,
+ }),
+ link: "https://drive.google.com/file/d/aaa/view",
+ },
+ ]);
+
+ const iframe = await waitFor(() => {
+ const iframe = document.querySelector("iframe.excalidraw__embeddable");
+ expect(iframe).not.toBe(null);
+ return iframe!;
+ });
+ // jsdom's `location.replace` can't be spied on
+ const replace = vi.fn();
+ Object.defineProperty(iframe, "contentWindow", {
+ value: { location: { replace } },
+ });
+
+ const activate = async () => {
+ mouse.clickAt(80, 65);
+ await waitFor(() => {
+ expect(h.state.activeEmbeddable?.state).toBe("active");
+ });
+ };
+
+ await activate();
+ // replace (not `src`) so the reload doesn't add a history entry
+ expect(replace).toHaveBeenCalledWith(
+ "https://drive.google.com/file/d/aaa/preview?autoplay=1",
+ );
+
+ // re-activating must not reload (and restart) the video
+ mouse.clickAt(5, 5);
+ expect(h.state.activeEmbeddable).toBe(null);
+ await activate();
+ expect(replace).toHaveBeenCalledTimes(1);
+ });
+});