Virtual whiteboard for sketching hand-drawn like diagrams
feat(editor): support Instagram embeds (#12373)
David Luzara3fdf471eefc
diff --git a/packages/element/src/embeddable.ts b/packages/element/src/embeddable.tsindex dceda3281e..013c76f1cf 100644--- a/packages/element/src/embeddable.ts+++ b/packages/element/src/embeddable.ts@@ -57,6 +57,11 @@ const RE_REDDIT =const RE_REDDIT_EMBED =/^<blockquote[\s\S]*?\shref=["'](https?:\/\/(?:www\.)?reddit\.com\/[^"']*)/i;+// share links can be prefixed with the author's username+// (instagram.com/<username>/p/<shortcode>)+const RE_INSTAGRAM =+ /^(?:https?:\/\/)?(?:www\.)?instagram\.com\/(?:[\w.]+\/)?(p|reels?|tv)\/([\w-]+)/;+const parseYouTubeLikeTimestamp = (url: string): number => {let timeParam: string | null | undefined;@@ -148,6 +153,7 @@ const ALLOWED_DOMAINS = new Set(["giphy.com","reddit.com","forms.microsoft.com",+ "instagram.com",]);const ALLOW_SAME_ORIGIN = new Set([@@ -163,6 +169,7 @@ const ALLOW_SAME_ORIGIN = new Set(["stackblitz.com","reddit.com","forms.microsoft.com",+ "instagram.com",]);export const createSrcDoc = (body: string) => {@@ -300,6 +307,30 @@ export const getEmbedLink = (};}+ const instagramLink = link.match(RE_INSTAGRAM);+ if (instagramLink) {+ const [, postType, shortcode] = instagramLink;+ // uncaptioned so the height is predictable (only depends on the media)+ // — we can't auto-resize to fit a long caption+ link = `https://www.instagram.com/${+ postType === "reels" ? "reel" : postType+ }/${shortcode}/embed/`;+ // fits portrait (4:5) posts and reels+ aspectRatio = { w: 400, h: 720 };+ embeddedLinkCache.set(originalLink, {+ link,+ intrinsicSize: aspectRatio,+ type,+ sandbox: { allowSameOrigin },+ });+ return {+ link,+ intrinsicSize: aspectRatio,+ type,+ sandbox: { allowSameOrigin },+ };+ }+const valLink = link.match(RE_VALTOWN);if (valLink) {link =diff --git a/packages/element/tests/embeddable.test.ts b/packages/element/tests/embeddable.test.tsindex 77a16045ea..e9cc798f09 100644--- a/packages/element/tests/embeddable.test.ts+++ b/packages/element/tests/embeddable.test.ts@@ -243,3 +243,31 @@ describe("Google Drive video embedding", () => {).toBe(true);});});++describe("Instagram embedding", () => {+ it.each([+ {+ url: "https://www.instagram.com/p/BsOGulcndj-/",+ expectedLink: "https://www.instagram.com/p/BsOGulcndj-/embed/",+ },+ {+ url: "https://instagram.com/reel/DSu-vbYDDlf/?igsh=MWQ1ZGUxMzBkMA==",+ expectedLink: "https://www.instagram.com/reel/DSu-vbYDDlf/embed/",+ },+ {+ url: "https://www.instagram.com/reels/DSu-vbYDDlf/",+ expectedLink: "https://www.instagram.com/reel/DSu-vbYDDlf/embed/",+ },+ {+ url: "https://www.instagram.com/world_record_egg/p/BsOGulcndj-/",+ expectedLink: "https://www.instagram.com/p/BsOGulcndj-/embed/",+ },+ ])("should normalize Instagram link: $url", ({ url, expectedLink }) => {+ expect(embeddableURLValidator(url, undefined)).toBe(true);+ const result = getEmbedLink(url);+ expect(result?.type).toBe("generic");+ if (result?.type === "generic") {+ expect(result.link).toBe(expectedLink);+ }+ });+});diff --git a/packages/excalidraw/components/App.clipboard.ts b/packages/excalidraw/components/App.clipboard.tsindex c494044c4e..f81e3dc846 100644--- a/packages/excalidraw/components/App.clipboard.ts+++ b/packages/excalidraw/components/App.clipboard.ts@@ -237,7 +237,14 @@ export class AppClipboard {const nonEmptyLines = normalizeEOL(data.text).split(/\n+/).map((s) => s.trim())- .filter(Boolean);+ .filter(Boolean)+ // some embed codes (e.g. Instagram) put the provider's loader <script>+ // on a separate line, which isn't an embed by itself+ .filter(+ (s) =>+ !/^<script\b[^>]*>\s*<\/script\b[^>]*>\s*$/i.test(s) ||+ maybeParseEmbedSrc(s) !== s,+ );const embbeddableUrls = nonEmptyLines.map((str) => maybeParseEmbedSrc(str)).filter(diff --git a/packages/excalidraw/tests/clipboard.test.tsx b/packages/excalidraw/tests/clipboard.test.tsxindex 6eee5ddee9..bd795e6cb5 100644--- a/packages/excalidraw/tests/clipboard.test.tsx+++ b/packages/excalidraw/tests/clipboard.test.tsx@@ -868,6 +868,21 @@ describe("copy and cut events", () => {});describe("paste routing", () => {+ it("should paste embed code with its loader <script> on a separate line as an embeddable", async () => {+ pasteWithCtrlCmdV(+ `<blockquote class="instagram-media" data-instgrm-permalink="https://www.instagram.com/p/BsOGulcndj-/?utm_source=ig_embed&utm_campaign=loading" data-instgrm-version="14"><div style="padding:16px;"> <a href="https://www.instagram.com/p/BsOGulcndj-/?utm_source=ig_embed&utm_campaign=loading" target="_blank">View this post on Instagram</a></div></blockquote>+<script async src="//www.instagram.com/embed.js"></script>`,+ );+ await waitFor(() => {+ expect(h.elements).toEqual([+ expect.objectContaining({+ type: "embeddable",+ link: "https://www.instagram.com/p/BsOGulcndj-/?utm_source=ig_embed&utm_campaign=loading",+ }),+ ]);+ });+ });+it("should leave a paste to the host when its onPaste returns false", async () => {unmountComponent();const onPaste = vi.fn(() => false);