Add image paste support to WYSIWYG editor #18

Merged
Copilot merged 4 commits from copilot/add-image-paste-functionality into main 2025-11-19 15:58:08 -06:00
Copilot commented 2025-11-19 15:36:52 -06:00 (Migrated from github.com)

Enables pasting images directly into the WYSIWYG editor from clipboard, including screenshots and copied image files.

Changes

  • Image paste handler: Added paste event listener that detects image data in clipboard, converts to base64 via FileReader, and inserts at cursor position
  • Toolbar image button: Added image upload capability with file picker (accepts image/*)
  • Format support: Added 'image' to allowed Quill formats array
  • React optimization: Memoized handlers with useCallback and useMemo, proper event cleanup in useEffect

Implementation

// Paste event handler
useEffect(() => {
  const quill = quillRef.current?.getEditor();
  if (!quill) return;

  const handlePaste = (e) => {
    const items = e.clipboardData?.items;
    for (let i = 0; i < items?.length; i++) {
      if (items[i].type.indexOf('image') !== -1) {
        e.preventDefault();
        const file = items[i].getAsFile();
        const reader = new FileReader();
        reader.onload = (event) => {
          const range = quill.getSelection(true);
          quill.insertEmbed(range.index, 'image', event.target.result);
          quill.setSelection(range.index + 1);
        };
        reader.readAsDataURL(file);
        break;
      }
    }
  };

  quill.root.addEventListener('paste', handlePaste);
  return () => quill.root.removeEventListener('paste', handlePaste);
}, []);

Images are embedded as base64 data URLs, requiring no backend changes.

Demo

WYSIWYG editor with image paste support

Users can now:

  • Paste images with Ctrl+V / Cmd+V
  • Click toolbar image button to upload files
  • Paste screenshots directly into content

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • backend
    • Triggering command: node /home/REDACTED/work/noodlenook/noodlenook/frontend/node_modules/.bin/vite (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

Add the ability to paste images into the WYSIWYG editor


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Enables pasting images directly into the WYSIWYG editor from clipboard, including screenshots and copied image files. ## Changes - **Image paste handler**: Added paste event listener that detects image data in clipboard, converts to base64 via FileReader, and inserts at cursor position - **Toolbar image button**: Added image upload capability with file picker (accepts `image/*`) - **Format support**: Added `'image'` to allowed Quill formats array - **React optimization**: Memoized handlers with `useCallback` and `useMemo`, proper event cleanup in `useEffect` ## Implementation ```jsx // Paste event handler useEffect(() => { const quill = quillRef.current?.getEditor(); if (!quill) return; const handlePaste = (e) => { const items = e.clipboardData?.items; for (let i = 0; i < items?.length; i++) { if (items[i].type.indexOf('image') !== -1) { e.preventDefault(); const file = items[i].getAsFile(); const reader = new FileReader(); reader.onload = (event) => { const range = quill.getSelection(true); quill.insertEmbed(range.index, 'image', event.target.result); quill.setSelection(range.index + 1); }; reader.readAsDataURL(file); break; } } }; quill.root.addEventListener('paste', handlePaste); return () => quill.root.removeEventListener('paste', handlePaste); }, []); ``` Images are embedded as base64 data URLs, requiring no backend changes. ## Demo ![WYSIWYG editor with image paste support](https://github.com/user-attachments/assets/6ab2424c-004a-4d4b-a849-5e4786556bad) Users can now: - Paste images with Ctrl+V / Cmd+V - Click toolbar image button to upload files - Paste screenshots directly into content > [!WARNING] > > <details> > <summary>Firewall rules blocked me from connecting to one or more addresses (expand for details)</summary> > > #### I tried to connect to the following addresses, but was blocked by firewall rules: > > - `backend` > - Triggering command: `node /home/REDACTED/work/noodlenook/noodlenook/frontend/node_modules/.bin/vite` (dns block) > > If you need me to access, download, or install something from one of these locations, you can either: > > - Configure [Actions setup steps](https://gh.io/copilot/actions-setup-steps) to set up my environment, which run before the firewall is enabled > - Add the appropriate URLs or hosts to the custom allowlist in this repository's [Copilot coding agent settings](https://github.com/SluberskiHomeLab/noodlenook/settings/copilot/coding_agent) (admins only) > > </details> <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > Add the ability to paste images into the WYSIWYG editor </details> <!-- START COPILOT CODING AGENT TIPS --> --- 💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs.
SluberskiHomeLab (Migrated from github.com) reviewed 2025-11-19 15:36:52 -06:00
Sign in to join this conversation.
No description provided.