Repository: excalidraw/excalidraw
File: packages/excalidraw/clipboard.ts (TypeScript)

Code before:
  const types: { [key in AllowedPasteMimeTypes]?: string } = {};

  let clipboardItems: ClipboardItems;

  try {
    clipboardItems = await navigator.clipboard?.read();
  } catch (error: any) {
    if (error.name === "DataError") {
      console.warn(
        `navigator.clipboard.read() error, clipboard is probably empty: ${error.message}`,
      );
      return types;
    }
    throw error;
  }

  try {
    if (navigator.clipboard?.read) {
      const readClipboard = await navigator.clipboard?.read();
      const textItem = readClipboard.find((item) => 
        item.types.includes("text/plain")
      );
      if (textItem) {
        const readText = await navigator.clipboard.readText();
        if (readText) {
          return { "text/plain": readText };
        }
      }
      const imageItem = readClipboard.find((item) => 
        item.types.some(type => type.startsWith("image/"))
      );
      if (imageItem) {
        const imageType = imageItem.types.find(type => type.startsWith("image/")) || "image/png";
        const imageBlob = await imageItem.getType(imageType);
        const imageUrl = URL.createObjectURL(imageBlob);
        return { [imageType]: imageUrl };
      }
    }
  } catch (error: any) {
    // @ts-ignore
    console.warn(`Error reading system clipboard: ${error.message}`);
    throw error;
  }

  for (const item of clipboardItems) {
    for (const type of item.types) {
      if (!isMemberOf(ALLOWED_PASTE_MIME_TYPES, type)) {
        continue;
      }
      try {
        types[type] = await (await item.getType(type)).text();

Review comment (bug):
This seems a bit strange, as it assumes all clipboard items are of the same exact type. Could we extend it to support pasting any types together? At the same time, when one type fails, the whole paste should not fail and continue.


Code after:
    console.error(`error in parseHTMLFromPaste: ${error.message}`);
  }

  return null;
};

/**
 * Reads OS clipboard programmatically. May not work on all browsers.
 * Will prompt user for permission if not granted.
 */
export const readSystemClipboard = async () => {
  const types: { [key in AllowedPasteMimeTypes]?: string | File } = {};

  let clipboardItems: ClipboardItems;

  try {
    clipboardItems = await navigator.clipboard?.read();
  } catch (error: any) {
    try {
      if (navigator.clipboard?.readText) {
        console.warn(
          `navigator.clipboard.readText() failed (${error.message}). Failling back to navigator.clipboard.read()`,
        );
        const readText = await navigator.clipboard?.readText();
        if (readText) {
          return { [MIME_TYPES.text]: readText };
        }
      }
    } catch (error: any) {
      // @ts-ignore
      if (navigator.clipboard?.read) {
        console.warn(
          `navigator.clipboard.readText() failed (${error.message}). Failling back to navigator.clipboard.read()`,
        );
      } else {
        if (error.name === "DataError") {
          console.warn(
            `navigator.clipboard.read() error, clipboard is probably empty: ${error.message}`,
          );
          return types;
        }

        throw error;
      }
    }
    throw error;
  }

  for (const item of clipboardItems) {
    for (const type of item.types) {
      if (!isMemberOf(ALLOWED_PASTE_MIME_TYPES, type)) {