Skip to content

Commit

Permalink
fix(editor): use loaded postmessage event for initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
maarten2424 committed Apr 30, 2024
1 parent c16ff45 commit 3c8dd71
Show file tree
Hide file tree
Showing 11 changed files with 429 additions and 158 deletions.
133 changes: 77 additions & 56 deletions packages/core/src/compiler/builtins/$richText/$richText.editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,15 @@ function RichTextEditor(props: RichTextProps) {
// } = editorContext;

const setFocussedField = (field: Array<string> | string) => {
window.parent.postMessage({
type: "@easyblocks-editor/focus-field",
payload: {
target: field,
window.parent.postMessage(
{
type: "@easyblocks-editor/focus-field",
payload: {
target: field,
},
},
});
"*"
);
};

const {
Expand Down Expand Up @@ -263,13 +266,16 @@ function RichTextEditor(props: RichTextProps) {
);
// form.change(path, nextRichTextElement);

window.parent.postMessage({
type: "@easyblocks-editor/form-change",
payload: {
key: path,
value: nextRichTextElement,
window.parent.postMessage(
{
type: "@easyblocks-editor/form-change",
payload: {
key: path,
value: nextRichTextElement,
},
},
});
"*"
);
}
}
},
Expand Down Expand Up @@ -349,14 +355,17 @@ function RichTextEditor(props: RichTextProps) {
getAbsoluteRichTextPartPath(focusedRichTextPart, path, locale)
);

window.parent.postMessage({
type: "@easyblocks-editor/form-change",
payload: {
key: path,
value: newRichTextElement,
focussedField: newFocusedFields,
window.parent.postMessage(
{
type: "@easyblocks-editor/form-change",
payload: {
key: path,
value: newRichTextElement,
focussedField: newFocusedFields,
},
},
});
"*"
);
}
}

Expand Down Expand Up @@ -544,22 +553,25 @@ function RichTextEditor(props: RichTextProps) {

previousRichTextComponentConfig.current = newRichTextElement;

window.parent.postMessage({
type: "@easyblocks-editor/form-change",
payload: {
key: path,
value: newRichTextElement,
...(editor.selection
? {
focussedField: getFocusedFieldsFromSlateSelection(
editor,
path,
locale
),
}
: {}),
window.parent.postMessage(
{
type: "@easyblocks-editor/form-change",
payload: {
key: path,
value: newRichTextElement,
...(editor.selection
? {
focussedField: getFocusedFieldsFromSlateSelection(
editor,
path,
locale
),
}
: {}),
},
},
});
"*"
);
}, RICH_TEXT_CONFIG_SYNC_THROTTLE_TIMEOUT),
[isConfigChanged, locale]
);
Expand Down Expand Up @@ -661,13 +673,16 @@ function RichTextEditor(props: RichTextProps) {

// form.change(path, nextRichTextComponentConfig);

window.parent.postMessage({
type: "@easyblocks-editor/form-change",
payload: {
key: path,
value: nextRichTextComponentConfig,
window.parent.postMessage(
{
type: "@easyblocks-editor/form-change",
payload: {
key: path,
value: nextRichTextComponentConfig,
},
},
});
"*"
);
} else {
// If current and fallback value is missing we have:
// - empty Slate value
Expand All @@ -679,13 +694,16 @@ function RichTextEditor(props: RichTextProps) {
editor.children as Array<BlockElement>
);
// form.change(path, nextRichTextComponentConfig);
window.parent.postMessage({
type: "@easyblocks-editor/form-change",
payload: {
key: path,
value: nextRichTextComponentConfig,
window.parent.postMessage(
{
type: "@easyblocks-editor/form-change",
payload: {
key: path,
value: nextRichTextComponentConfig,
},
},
});
"*"
);
}

previousRichTextComponentConfig.current = nextRichTextComponentConfig;
Expand Down Expand Up @@ -801,18 +819,21 @@ function RichTextEditor(props: RichTextProps) {
// return nextFocusedFields;
// });

window.parent.postMessage({
type: "@easyblocks-editor/form-change",
payload: {
key: richTextElementsConfigPath,
value: nextElements,
focussedField: getFocusedFieldsFromSlateSelection(
temporaryEditor,
path,
locale
),
window.parent.postMessage(
{
type: "@easyblocks-editor/form-change",
payload: {
key: richTextElementsConfigPath,
value: nextElements,
focussedField: getFocusedFieldsFromSlateSelection(
temporaryEditor,
path,
locale
),
},
},
});
"*"
);

lastChangeReason.current = "paste";
} else if (
Expand Down
15 changes: 9 additions & 6 deletions packages/core/src/compiler/builtins/$text/InlineTextarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ export function InlineTextarea({
const inputProps = useTextValue(
value,
(val: string | null) => {
window.parent.postMessage({
type: "@easyblocks-editor/form-change",
payload: {
key: valuePath,
value: val,
window.parent.postMessage(
{
type: "@easyblocks-editor/form-change",
payload: {
key: valuePath,
value: val,
},
},
});
"*"
);
},
locale,
locales,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,16 @@ function getCompiledSubcomponents(
name: path,
index: 0,
block: event.data.payload.config,
})
}),
"*"
);
}
}
}

window.addEventListener("message", handleComponentPickerCloseMessage);

window.parent.postMessage(componentPickerOpened(originalPath));
window.parent.postMessage(componentPickerOpened(originalPath), "*");
}}
meta={meta}
/>,
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ type UndoEvent = MessageEvent<
>
>;

type CanvasLoadedEvent = MessageEvent<
EasyblocksEditorEventData<
"@easyblocks-editor/canvas-loaded",
{
type: "@easyblocks-editor/canvas-loaded";
}
>
>;

type FormChangeEvent = MessageEvent<
EasyblocksEditorEventData<
"@easyblocks-editor/form-change",
Expand Down Expand Up @@ -206,4 +215,5 @@ export type {
RedoEvent,
SetFocussedFieldEvent,
FormChangeEvent,
CanvasLoadedEvent,
};
16 changes: 11 additions & 5 deletions packages/editor/src/CanvasRoot/CanvasRoot.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { ReactNode } from "react";
import { useEasyblocksCanvasContext } from "@easyblocks/core/_internals";
import { useCanvasGlobalKeyboardShortcuts } from "../useCanvasGlobalKeyboardShortcuts";

type CanvasRootProps = {
children: ReactNode;
Expand All @@ -8,16 +9,21 @@ type CanvasRootProps = {
function CanvasRoot(props: CanvasRootProps) {
const { isEditing } = useEasyblocksCanvasContext();

useCanvasGlobalKeyboardShortcuts();

return (
<div
onClick={() => {
if (isEditing) {
window.parent.postMessage({
type: "@easyblocks-editor/focus-field",
payload: {
target: [],
window.parent.postMessage(
{
type: "@easyblocks-editor/focus-field",
payload: {
target: [],
},
},
});
"*"
);
}
}}
>
Expand Down
13 changes: 8 additions & 5 deletions packages/editor/src/EditableComponentBuilder/BlockControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,15 @@ export function BlocksControls({

const nextFocusedField = getNextFocusedField();

window.parent.postMessage({
type: "@easyblocks-editor/focus-field",
payload: {
target: nextFocusedField,
window.parent.postMessage(
{
type: "@easyblocks-editor/focus-field",
payload: {
target: nextFocusedField,
},
},
});
"*"
);

if (isMultipleSelection) {
document.getSelection()?.removeAllRanges();
Expand Down
Loading

0 comments on commit 3c8dd71

Please sign in to comment.