Skip to content

Fix clipboard if content editable is in an iframe. - #4649

Merged
zurfyx merged 1 commit into
meta:mainfrom
imdreamrunner:fix-iframe-clipboard
Jun 15, 2023
Merged

zurfyx merged 1 commit into
meta:mainfrom
imdreamrunner:fix-iframe-clipboard

Conversation

@imdreamrunner

Copy link
Copy Markdown
Contributor

This is also my first contribution attempt to the project. Sorry in advance if something doesn't make sense!

In #2108 we support for iframe (use React.Portal to render the content editable area in an iframe). It works well.

Recently I notice that some of the clipboard logic doesn't work in iframe. More specifically, I found that DecoratorNode won't be exported on copy.

After digging into it, I found that is caused by global class variables having different instances in different window. There is more detail about this behaviour in MDN.

In this diff, the issue I personally encounter has been fixed. But there are wider usage of instanceof in the codebase.

Overall, I want to hear from maintainers about

  • Whether it's okay to directly use internal field _window from LexicalEditor
  • Should I add another example / another mode of <Editor /> for usage with iframe

@vercel

vercel Bot commented Jun 13, 2023 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 15, 2023 1:21am
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 15, 2023 1:21am

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 13, 2023
@imdreamrunner
imdreamrunner marked this pull request as ready for review June 13, 2023 12:49

@zurfyx zurfyx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for addressing this, left a small comment regarding how we do the instanceof comparison. We have be careful it works fine for headless mode, including JSDOM which is quite popular. I proposed a similar approach to what we use for importDOM, where instead of instance checks we look for specific properties instead. It's not as type-safe but it will work for non-browser environments.

Comment thread packages/lexical-rich-text/src/index.ts Outdated
Comment on lines +994 to +1000
event instanceof getClassFromEditorWindow(editor, ClipboardEvent)
? event
: null,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
event instanceof getClassFromEditorWindow(editor, ClipboardEvent)
? event
: null,
Object.getPrototypeOf(event).constructor.name === ClipboardEvent.name ? event : null,

where ClipboardEvent.name would be hoisted

Given that we're also using this in the Clipboard module we can also abstract this on a clipboard exported function.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea! I updated the PR using the new approach and not relying on the window object. I've created a util function that at least makes TypeScript happy about it.

@zurfyx zurfyx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Some final nits

Comment thread packages/lexical-utils/src/index.ts Resolved
Comment thread packages/lexical-utils/src/index.ts Resolved
Comment thread packages/lexical-utils/src/index.ts Resolved
Comment thread packages/lexical-utils/src/index.ts Resolved
Comment thread packages/lexical-utils/src/index.ts Resolved
Comment thread packages/lexical-rich-text/src/index.ts Resolved

@zurfyx zurfyx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you for iterating on this!

This branch was successfully deployed

2 active deployments
Preview – lexical-playground — 0a8774e9 Deployed Jun 15, 2023 by vercel[bot]
Preview – lexical — 0a8774e9 Deployed Jun 15, 2023 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants