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

ZUI editor pasting prototype #2468

Merged

Conversation

richardolsson
Copy link
Member

Description

This PR adds support for pasting links and images in the refactored ZUIEditor. I've also done some research into how copy/paste works.

Pasting in Remirror/ProseMirror is handled almost automatically, using the parseDOM() hook in the MarkExtensionSpec. When copy/pasting, the browser will paste the HTML, and ProseMirror will parse it using the matching extension. If there is no matching extension, the HTML will be stripped. For example, pasting bold text when BoldExtension is enabled inserts text marked with the BoldExtension mark, but if we disable the extension, the text is pasted with the bold styling stripped, as a regular paragraph. Perfect!

Our custom extensions LinkExtension and ImageExtension did not properly implement parseDOM(), because we did not actually parse any attributes. All this PR does in order to make pasting work is to parse and transfer the correct attributes from the pasted DOM.

Pasting is already supported by the builtin extensions we use, i.e. HeaderExtension and BoldExtension. I have tried them and they work as expected. As we add more extensions, builtin or custom, we need to make sure pasting works as it should.

I have also researched the more advanced createPasteRules() hook which allows an extension to create more arbitrary paste rules, which might become necessary if we build extensions with a more arbitrary DOM representation. Here's a rough cut from that experimentation.

  createPasteRules(): PasteRule[] {
    return [
      {
        priority: ExtensionPriority.Critical,
        //regexp: new RegExp(/<a\s*.*<\/\s*a\s*>/, 'i'),
        regexp: /Clara/,
        getAttributes(match, isReplacement) {
          console.log('get attributes', match, isReplacement);
          return {};
        },
        transformMatch(match) {
          console.log('PASTE', match);
          return false;
        },
        type: 'mark',
        markType: this.type,
      },
    ];
  }

Screenshots

None

Changes

  • Adds attribute parsing to our LinkExtension
  • Adds attribute parsing to our ImageExtension

Notes to reviewer

None

Related issues

Undocumented

Base automatically changed from undocumented/ZUI-editor-refactor to undocumented/ZUI-text-editor January 17, 2025 08:53
@richardolsson
Copy link
Member Author

Will merge without review, but please have a look at this when you get a chance @ziggabyte, because it contains some useful information.

@richardolsson richardolsson merged commit 26d63ab into undocumented/ZUI-text-editor Jan 17, 2025
6 checks passed
@richardolsson richardolsson deleted the undocumented/zui-editor-paste branch January 17, 2025 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant