-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Support rendering Draft-js into iframe #1877
Conversation
We do that to access them from whenever it's needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR @haikyuu! I'll run some internal smoke tests to ensure we're not regressing with these changes. Otherwise looking quite good. Please take a look at my comments in the meantime, especially the issue with getAnonymizedEditorDOM
.
return document.createTextNode( | ||
const documentObject = getCorrectDocumentFromNode(node); | ||
|
||
return documentObject.createTextNode( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you chain the call to getCorrectDocumentFromNode(node)
without an intermediate assignment here and in function addPointToSelection
, since you're not reusing the value?
@@ -76,11 +81,10 @@ function getAnonymizedEditorDOM( | |||
): string { | |||
// grabbing the DOM content of the Draft editor | |||
let currentNode = node; | |||
// this should only be used after checking with isElement | |||
let castedNode: Element = (currentNode: any); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should also assign it on line 91 when reassigning currentNode = currentNode.parentNode
); | ||
if (isHTMLAnchorElement(node)) { | ||
const castedNode: HTMLAnchorElement = (node: any); | ||
return ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You lost a !!
before the returned expression. I'd also suggest to return Boolean(<expression>)
instead.
@@ -0,0 +1,14 @@ | |||
function isElement(node) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also check if node.nodeType === Node.ELEMENT_NODE
@@ -0,0 +1,14 @@ | |||
function isHTMLAnchorElement(node) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd implement this checking that, as @sophiebits suggested in another PR, isElement(node) && node.nodeName === 'A'
if (!node || !node.ownerDocument) { | ||
return false; | ||
} | ||
if (!node.ownerDocument.defaultView) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isElement(node) && node.nodeName === 'IMG'
I also found an issue when the iframed Editor has a |
Ah, nevermind - that issue is caused by the absence of the |
@haikyuu do you need any help to address the review feedback? I'd like to merge this since it's in a good shape and there are many interested consumers! |
I was quite busy these days. I'll fix them next week once i have some spare time👍 |
Hi @haikyuu, would you have time to look into the finishing touch to this PR? Several Draft.js users who are eager to embed editors in an iframe would be thankful for your efforts 😄 |
Hey @haikyuu, I'm also blocked by the iframe issue and I would love to see this PR merged :) |
@marek-baranowski @claudiopro continued the work on #1938 👍 |
Closing this since it's continued on #1938 instead. |
Summary: **Summary** Takes over and applies requested PR feedback to #1877 by haikyuu. Refer to original PR for context and discussion. **Test Plan** Run some manual smoke tests on the editor running in an iframe as follows: ``` yarn python3 -m http.server 8080 . open http://localhost:8080/examples/draft-0-10-0/iframe/iframe.html ``` Pull Request resolved: #1938 Reviewed By: claudiopro Differential Revision: D13137413 Pulled By: jack-arms fbshipit-source-id: efcdbabc7d8d2aff4fbebc8b06c22d57756ebc12
…ve#1938) Summary: **Summary** Takes over and applies requested PR feedback to facebookarchive#1877 by haikyuu. Refer to original PR for context and discussion. **Test Plan** Run some manual smoke tests on the editor running in an iframe as follows: ``` yarn python3 -m http.server 8080 . open http://localhost:8080/examples/draft-0-10-0/iframe/iframe.html ``` Pull Request resolved: facebookarchive#1938 Reviewed By: claudiopro Differential Revision: D13137413 Pulled By: jack-arms fbshipit-source-id: efcdbabc7d8d2aff4fbebc8b06c22d57756ebc12
…ve#1938) Summary: **Summary** Takes over and applies requested PR feedback to facebookarchive#1877 by haikyuu. Refer to original PR for context and discussion. **Test Plan** Run some manual smoke tests on the editor running in an iframe as follows: ``` yarn python3 -m http.server 8080 . open http://localhost:8080/examples/draft-0-10-0/iframe/iframe.html ``` Pull Request resolved: facebookarchive#1938 Reviewed By: claudiopro Differential Revision: D13137413 Pulled By: jack-arms fbshipit-source-id: efcdbabc7d8d2aff4fbebc8b06c22d57756ebc12
Summary
This pull request adds support for draft-js usage inside iframes. There is another WIP PR which tries to achieve the same thing #765.
Note that this was mainly broken because
instanceof
does not check objects across iframes.In other words (code)
iframeElement instanceof Element === false
whileiframeElement instanceof iframeDocument.Element === true
.And the other reason is
getSelection
depends on the window object as well.Checking if a node is instance of
Element
,HTMLElement
...Depending on the context, we need a way to access the right iframe window object to check using
instanceof
.nodeType
property instead (used in acusti's PR )Challenges
window
/defaultView
. It was fixed by adding another check.Accessing the window or document object in other cases
getSelection
...Whenever the
event
is accessible, we use it to access theownerDocument
Places skipped (they use the document and global objects)
getSafeBodyFromHTML
: it creates a new document anywaygetTextContentFromFiles
: it uses theglobal.fileReader
Other challenges
In order to fix some flow errors, i had to force casting node types in some places. Note that i only did this after
instanceof
checks, so it's safe. Those checks were not detected by flow because we useinstanceof
to check in iframe window or in the main window. One solution to fix this all together is to check nodes using a utility likefbjs
'isNode
but i prefer to keep the changes minimal.Known issues
cmd+b
would apply the inline style but the selection will collapse. It happens only once, and doesn't happen outside an iframe. You can check it here and reproduce it in the sandboxTest Plan
I created a sandbox to test
draft-js
editor. It has 2 editors inside separate iframes, and one in the main window.