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

Code before:
  } catch (error: any) {
    console.error(`error in parseHTMLFromPaste: ${error.message}`);
  }

  return null;
};

export const readSystemClipboard = async () => {
  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;
  }

Review comment (bug):
`read` has generally worse support than `readText`, so now pasting text might just fail if `read` is unsupported (related to my previous comment)

Code after:
    const doc = new DOMParser().parseFromString(html, MIME_TYPES.html);

    const content = parseHTMLTree(doc.body);

    if (content.length) {
      return { type: "mixedContent", value: content };
    }
  } catch (error: any) {
    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;