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

POC: Feat/remove window parent #40

Closed

Conversation

maarten2424
Copy link
Contributor

POC removing window.parent.editorWindowAPI

This removes window.parent.editorWindowAPI in favour of postMessage functionality. This way the editor and the rendered page can be hosted on different domains.

Copy link

vercel bot commented Apr 9, 2024

@maarten2424 is attempting to deploy a commit to the Shopstory Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

@r00dY r00dY left a comment

Choose a reason for hiding this comment

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

This PR is a very very good attempt to clean up an important thing, great work!

FYI, There were a several reasons we didn't do communication between windows with Window.postMessage, but most of them don't apply now, they're historical (from Shopstory).

There's one big thing that must be remembered here: when postMessage is used a copy is created, and creating a copy of EditorContext is something that can't be done, there can be should be only context instance.

IMO the best way to approach this task correctly is to assume that in the canvas (child window with preview) there is no editorContext at all. Since there can be only one instance of editor context and it must live in parent window, then whatever exists in the child window shouldn't be named this way. After editorContext is removed from child window then it will trigger all types of errors across the system, which is good. Each one of them should be fixed by either:

  1. For events: adding new postMessage events instead of calling editorContext.actions or editorContext.form.change etc.
  2. For data: instead of using editorContext.form.values let's just pass the required values directly to a child window (there won't be many, probably 3-4) like entry (which is used instead of editorContext.form.values) etc.

This approach is the best because it will automatically create a proper and minimal "surface area" between parent and child. Right now we're passing entire editorContext which is absolutely unnecessary, probably 2% of this context is used in a child.

Let me know if my explanation is clear.

@@ -272,7 +272,7 @@ function RichTextEditor(props: RichTextProps) {

if (event.data.type === "@easyblocks-editor/rich-text-changed") {
const { payload } = event.data;
const { editorContext } = (window.parent as any).editorWindowAPI;
const { editorContext } = (window as any).editorWindowAPI;
Copy link
Contributor

Choose a reason for hiding this comment

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

editorContext is used heavily in this file and it should be the instance from the editor, not the copy in canvas.

type: "@easyblocks/init",
meta: JSON.stringify(meta),
compiled: JSON.stringify(renderableContent),
editorContext: JSON.stringify({
Copy link
Contributor

Choose a reason for hiding this comment

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

The problem with postMessage is that it requires you to create serialized copies. In that case editorContext is well, a context, and there should be only one reference of it. If you use a copy of editor context in the canvas then it just can't work properly.

@maarten2424 maarten2424 deleted the feat/remove-window-parent branch May 21, 2024 13:06
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.

None yet

2 participants