quill icon indicating copy to clipboard operation
quill copied to clipboard

Insert image with url

Open bilalmalkoc opened this issue 8 years ago • 6 comments

Is possible insert image without uploading? Just put image link, and display it in <img src"" /> tag.

bilalmalkoc avatar Mar 27 '18 19:03 bilalmalkoc

custom handler + insertEmbed. why don't you read the guides first?

Logged in just to down vote this comment.

Its kind of lazy that the developers left out such a crucial part of wysiwyg editing.

also here is some actually useful code

var toolbarOptions = [
            ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
            ['blockquote', 'code-block'],

            [{ 'header': 1 }, { 'header': 2 }],               // custom button values
            [{ 'list': 'ordered' }, { 'list': 'bullet' }],
            [{ 'script': 'sub' }, { 'script': 'super' }],      // superscript/subscript
            [{ 'indent': '-1' }, { 'indent': '+1' }],          // outdent/indent
            [{ 'direction': 'rtl' }],                         // text direction

            [{ 'size': ['small', false, 'large', 'huge'] }],  // custom dropdown
            [{ 'header': [1, 2, 3, 4, 5, 6, false] }],

            [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
            [{ 'font': [] }],
            [{ 'align': [] }],

            ['clean'],                                         // remove formatting button
            ['image']
        ];

        var quill = new Quill('#editor', {
            theme: 'snow',
            modules: {
                toolbar: {
                    container: toolbarOptions,
                    handlers: {
                        image: imageHandler
                    }
                }
            },
        });

        function imageHandler() {
            var range = this.quill.getSelection();
            var value = prompt('What is the image URL');
            if(value){
                this.quill.insertEmbed(range.index, 'image', value, Quill.sources.USER);
            }
        }

Gib-git avatar Apr 15 '19 17:04 Gib-git

@Gibigbig Hi! I am using Markdown with nix-quill. I've tried inserting your part of the code in the app module, but apparently Quill is not packed there. May you help me fixing this?

Anatoly03 avatar Sep 21 '19 10:09 Anatoly03

@Gibigbig Hi! I am using Markdown with nix-quill. I've tried inserting your part of the code in the app module, but apparently Quill is not packed there. May you help me fixing this?

I'm also using ngx-quill. I managed to get this to work in Angular by:

import { QuillModules, defaultModules } from 'ngx-quill';

export class MyComponent {

    quillModules: QuillModules = {
    toolbar: {
      container: defaultModules.toolbar, 
      handlers: {
        image: imageHandler
      }
    }
  };

}

imageHandler(this: any) {
    const tooltip = this.quill.theme.tooltip;
    const originalSave = tooltip.save;
    const originalHide = tooltip.hide;
    tooltip.save = function(this: any) {
      const range = this.quill.getSelection(true);
      const value = this.textbox.value;
      if (value) {
        this.quill.insertEmbed(range.index, 'image', value, 'user');
      }
    };
    // Called on hide and save.
    tooltip.hide = function (this: any) {
       tooltip.save = originalSave;
       tooltip.hide = originalHide;
       tooltip.hide();
    };
    tooltip.edit('image');
    tooltip.textbox.placeholder = "Embed URL";
  }

<quill-editor [modules]="quillModules"></quill-editor>

chrisbmoore avatar Mar 25 '20 04:03 chrisbmoore

@chrisbmoore Hi, you have to update you tooltip.save funtion . You can change your code like this: const value = this.textbox.value to const value = tooltip.textbox.value

sionnaouattara avatar Oct 25 '20 19:10 sionnaouattara

This should work with React v.17, Next.js vs.12 and React Quill v.2:

import React, { useMemo, useRef } from "react"
import dynamic from "next/dynamic"
import "react-quill/dist/quill.snow.css"

const ReactQuill = dynamic(
  async () => {
    const ReactQuill = (await import("react-quill")).default

    return ({
      forwardedRef,
      ...rest
    }) => <ReactQuill ref={forwardedRef} {...rest} />
  },
  {
    ssr: false,
  },
)

export const Editor = ({ value, onValueChange }) => {
  const quillRef = useRef(null)
  const modules = useMemo(
    () => ({
      toolbar: {
        container: [
          [{ header: [1, 2, 3, false] }],
          ["image"],
        ],
        handlers: {
          image: imageHandler,
        },
      },
    }),
    [],
  )

  function imageHandler() {
    if (!quillRef.current) return

    const editor = quillRef.current.getEditor()
    const range = editor.getSelection()
    const value = prompt("Please enter the image URL")

    if (value && range) {
      editor.insertEmbed(range.index, "image", value, "user")
    }
  }

  return (
      <ReactQuill
        forwardedRef={quillRef}
        theme="snow"
        defaultValue={value}
        onChange={onValueChange}
        modules={modules}
      />
  )
}

dbruvers avatar Jan 18 '23 13:01 dbruvers

Quill 2.0 has been released (announcement post) with many changes and fixes. If this is still an issue please create a new issue after reviewing our updated Contributing guide :pray:

quill-bot avatar Apr 17 '24 10:04 quill-bot