Skip to content

Commit c5507b5

Browse files
committed
feat(editor): form-change event
1 parent c600b1d commit c5507b5

File tree

8 files changed

+225
-106
lines changed

8 files changed

+225
-106
lines changed

packages/core/src/compiler/builtins/$richText/$richText.editor.tsx

Lines changed: 170 additions & 85 deletions
Large diffs are not rendered by default.

packages/core/src/compiler/builtins/$richText/utils/getEditorSelectionFromFocusedFields.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { parseFocusedRichTextPartConfigPath } from "./parseRichTextPartConfigPat
44

55
function getEditorSelectionFromFocusedFields(
66
focusedFields: Array<string>,
7-
form: any
7+
formValues: any
88
): Selection {
99
try {
1010
const anchorFocusedField = focusedFields[0];
@@ -26,7 +26,7 @@ function getEditorSelectionFromFocusedFields(
2626
focus: {
2727
offset: parsedFocusedField.range
2828
? parsedFocusedField.range[1]
29-
: dotNotationGet(form.values, focusFocusedField).value.length,
29+
: dotNotationGet(formValues, focusFocusedField).value.length,
3030
path: parsedFocusedField.path,
3131
},
3232
};

packages/core/src/compiler/builtins/$text/$text.editor.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { dotNotationGet } from "@easyblocks/utils";
33
import React, { ReactElement } from "react";
44
import { InternalNoCodeComponentProps } from "../../../components/ComponentBuilder/ComponentBuilder";
55
import { InlineTextarea } from "./InlineTextarea";
6+
import { useEasyblocksCanvasContext } from "../../../_internals";
67

78
type TextProps = {
89
value: string | undefined;
@@ -16,9 +17,10 @@ function TextEditor(props: TextProps) {
1617
__easyblocks: { path, runtime },
1718
} = props;
1819

19-
const { form } = (window.parent as any).editorWindowAPI.editorContext;
20+
const { formValues } = useEasyblocksCanvasContext();
21+
2022
const valuePath = `${path}.value`;
21-
const configValue = dotNotationGet(form.values, valuePath);
23+
const configValue = dotNotationGet(formValues, valuePath);
2224
const isLocalTextReference = configValue.id?.startsWith("local.");
2325

2426
return (

packages/core/src/compiler/builtins/$text/InlineTextarea.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React, { ElementRef, useRef, useState } from "react";
33
import { flushSync } from "react-dom";
44
import TextareaAutosize from "react-textarea-autosize";
55
import { useTextValue } from "../useTextValue";
6+
import { useEasyblocksCanvasContext } from "../../../_internals";
67

78
interface InlineTextProps {
89
path: string;
@@ -17,19 +18,21 @@ export function InlineTextarea({
1718
}: InlineTextProps) {
1819
const [isEnabled, setIsEnabled] = useState(false);
1920
const textAreaRef = useRef<ElementRef<"textarea">>(null);
21+
const { formValues, locale, locales } = useEasyblocksCanvasContext();
2022

21-
const {
22-
form,
23-
contextParams: { locale },
24-
locales,
25-
} = (window.parent as any).editorWindowAPI.editorContext;
2623
const valuePath = `${path}.value`;
27-
const value = dotNotationGet(form.values, valuePath);
24+
const value = dotNotationGet(formValues, valuePath);
2825

2926
const inputProps = useTextValue(
3027
value,
3128
(val: string | null) => {
32-
form.change(valuePath, val);
29+
window.parent.postMessage({
30+
type: "@easyblocks-editor/form-change",
31+
payload: {
32+
key: valuePath,
33+
value: val,
34+
},
35+
});
3336
},
3437
locale,
3538
locales,

packages/core/src/components/EasyblocksCanvasProvider.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ type EasyblocksCanvasState = {
1818
externalData: FetchOutputResources | null;
1919
formValues: EditorContextType["form"]["values"] | null;
2020
definitions: EditorContextType["definitions"] | null;
21-
locale: EditorContextType["contextParams"]["locale"] | null;
22-
locales: EditorContextType["locales"] | null;
21+
locale: EditorContextType["contextParams"]["locale"];
22+
locales: EditorContextType["locales"];
2323
isEditing: EditorContextType["isEditing"];
2424
devices: EditorContextType["devices"] | null;
25-
focussedField: EditorContextType["focussedField"] | null;
25+
focussedField: EditorContextType["focussedField"];
2626
};
2727

2828
const initialState: EasyblocksCanvasState = {
@@ -31,11 +31,11 @@ const initialState: EasyblocksCanvasState = {
3131
externalData: null,
3232
formValues: null,
3333
definitions: null,
34-
locale: null,
35-
locales: null,
34+
locale: "",
35+
locales: [],
3636
isEditing: false,
3737
devices: null,
38-
focussedField: null,
38+
focussedField: [],
3939
};
4040

4141
const EasyblocksCanvasContext = createContext<

packages/core/src/events.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,18 @@ type UndoEvent = MessageEvent<
136136
}
137137
>
138138
>;
139+
140+
type FormChangeEvent = MessageEvent<
141+
EasyblocksEditorEventData<
142+
"@easyblocks-editor/form-change",
143+
{
144+
key: string;
145+
value: any;
146+
focussedField?: Array<string> | string;
147+
}
148+
>
149+
>;
150+
139151
type RedoEvent = MessageEvent<
140152
EasyblocksEditorEventData<
141153
"@easyblocks-editor/redo",
@@ -193,4 +205,5 @@ export type {
193205
UndoEvent,
194206
RedoEvent,
195207
SetFocussedFieldEvent,
208+
FormChangeEvent,
196209
};

packages/editor/src/EasyblocksEditor.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,10 @@ export function EasyblocksEditor(props: EasyblocksEditorProps) {
3838
if (window.parent !== window.self && window.parent.isShopstoryEditor) {
3939
setSelectedWindowToChild();
4040
} else {
41-
// setSelectedWindowToParent();
42-
setSelectedWindowToChild();
41+
setSelectedWindowToParent();
4342
}
4443
} catch (error) {
45-
// setSelectedWindowToParent();
46-
setSelectedWindowToChild();
44+
setSelectedWindowToParent();
4745
}
4846
}
4947
}, []);

packages/editor/src/Editor.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
ChangeResponsiveEvent,
2828
CompilationContextType,
2929
ComponentPickerOpenedEvent,
30+
FormChangeEvent,
3031
ItemInsertedEvent,
3132
ItemMovedEvent,
3233
RedoEvent,
@@ -894,7 +895,24 @@ const EditorContent = ({
894895
| SetFocussedFieldEvent
895896
| UndoEvent
896897
| RedoEvent
898+
| FormChangeEvent
897899
) {
900+
if (event.data.type === "@easyblocks-editor/form-change") {
901+
if (event.data.payload.focussedField) {
902+
actions.runChange(() => {
903+
if (
904+
event.data.type === "@easyblocks-editor/form-change" &&
905+
Array.isArray(event.data.payload.focussedField)
906+
) {
907+
form.change(event.data.payload.key, event.data.payload.value);
908+
return event.data.payload.focussedField;
909+
}
910+
});
911+
} else {
912+
form.change(event.data.payload.key, event.data.payload.value);
913+
}
914+
}
915+
898916
if (event.data.type === "@easyblocks-editor/focus-field") {
899917
handleSetFocussedField(event.data.payload.target);
900918
}

0 commit comments

Comments
 (0)