Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble with Recognizing URLs in Quill Editor #4140

Open
ivanadrzaic opened this issue Apr 25, 2024 · 1 comment
Open

Trouble with Recognizing URLs in Quill Editor #4140

ivanadrzaic opened this issue Apr 25, 2024 · 1 comment

Comments

@ivanadrzaic
Copy link

Can someone help me with how to make the link visible when pasting, not just the text?
So, I want the Quill editor to somehow recognize that it's a URL.

If I copy https://www.youtube.com/
it should turn into a link, not just text.

I've tried this way and it's not working for me.


export class PrimeNgQuillComponent {
  @ViewChild('quillEditor') quillEditor!: QuillEditorComponent;

  quillModel: string = "";
  simpleUrlRegex = /https?:\/\/[^\s]+/g;

  constructor(
    @Inject(QUILL_CONFIG_TOKEN) private config: QuillConfig
  ) {
    // console.log('PrimeNgQuill Module - Global COnfig', config);
    this.config.modules = {
      ...this.config.modules,
      clipboard: {
        matchers: [
          [Node.TEXT_NODE, (node: any, delta: Delta) => {
            const data = this.quillModel
            const matches = data.match(this.simpleUrlRegex);

            if (matches && matches.length > 0) {
              const ops = [];
              let str = data;
              matches.forEach((match) => {
                const split = str.split(match);
                const beforeLink = split.shift();
                ops.push({ insert: beforeLink });
                ops.push({ insert: match, attributes: { link: match } });
                str = split.join(match);
              });

              ops.push({ insert: str });
              delta.ops = ops;
            }
            return delta;
          }],
        ]
      },
     keyboard: {
        bindings: {
             space: {
            key: 'Space',
            prefix: /https?:\/\/[^\s]+/,
            shiftKey: true,
            handler: (function spaceToLink() {
              let prevOffset = 0;
              return function handler(this: any, range: any) {
                const text = this.quill.getText(prevOffset, range.index) as string;
                const match = text.match(this.simpleUrlRegex);

                if (!match) {
                  prevOffset = range.index;
                  return true;
                }
                const url = match[match.length - 1];
                const ops = [];
                ops.push({ retain: range.index - url.length });
                ops.push({ delete: url.length });
                ops.push({ insert: url, attributes: { link: url } });
                this.quill.updateContents({ ops });
                prevOffset = range.index;
                return true;
              };
            })(),
          },
         }
       },
   
@ludejun
Copy link

ludejun commented May 2, 2024

You can use quill-react-commercial.

You can copy url as link like below gif.
quill-copy-link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants