Skip to content

Commit 4a37ec9

Browse files
committed
feat(repo): canvascontext dont expose context null
1 parent d63c639 commit 4a37ec9

File tree

10 files changed

+73
-48
lines changed

10 files changed

+73
-48
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,15 @@ interface RichTextProps extends InternalNoCodeComponentProps {
7272

7373
function RichTextEditor(props: RichTextProps) {
7474
// const { editorContext } = (window.parent as any).editorWindowAPI;
75+
76+
const canvasContext = useEasyblocksCanvasContext();
77+
78+
if (!canvasContext) {
79+
return null;
80+
}
81+
7582
const { formValues, locales, locale, focussedField, definitions } =
76-
useEasyblocksCanvasContext();
83+
canvasContext;
7784

7885
// const {
7986
// actions,

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ function TextEditor(props: TextProps) {
1717
__easyblocks: { path, runtime },
1818
} = props;
1919

20-
const { formValues } = useEasyblocksCanvasContext();
20+
const canvasContext = useEasyblocksCanvasContext();
21+
22+
if (!canvasContext) {
23+
return null;
24+
}
25+
const { formValues } = canvasContext;
2126

2227
const valuePath = `${path}.value`;
2328
const configValue = dotNotationGet(formValues, valuePath);

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ export function InlineTextarea({
1818
}: InlineTextProps) {
1919
const [isEnabled, setIsEnabled] = useState(false);
2020
const textAreaRef = useRef<ElementRef<"textarea">>(null);
21-
const { formValues, locale, locales } = useEasyblocksCanvasContext();
21+
22+
const canvasContext = useEasyblocksCanvasContext();
23+
24+
if (!canvasContext) {
25+
return null;
26+
}
27+
28+
const { formValues, locale, locales } = canvasContext;
2229

2330
const valuePath = `${path}.value`;
2431
const value = dotNotationGet(formValues, valuePath);

packages/core/src/components/EasyblocksCanvasProvider.tsx

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,21 @@ import {
1313
} from "../types";
1414

1515
type EasyblocksCanvasState = {
16-
meta: CompilationMetadata | null;
17-
compiled: CompiledShopstoryComponentConfig | null;
18-
externalData: FetchOutputResources | null;
19-
formValues: EditorContextType["form"]["values"] | null;
20-
definitions: EditorContextType["definitions"] | null;
16+
meta: CompilationMetadata;
17+
compiled: CompiledShopstoryComponentConfig;
18+
externalData: FetchOutputResources;
19+
formValues: EditorContextType["form"]["values"];
20+
definitions: EditorContextType["definitions"];
2121
locale: EditorContextType["contextParams"]["locale"];
2222
locales: EditorContextType["locales"];
2323
isEditing: EditorContextType["isEditing"];
24-
devices: EditorContextType["devices"] | null;
24+
devices: EditorContextType["devices"];
2525
focussedField: EditorContextType["focussedField"];
2626
};
2727

28-
const initialState: EasyblocksCanvasState = {
29-
meta: null,
30-
compiled: null,
31-
externalData: null,
32-
formValues: null,
33-
definitions: null,
34-
locale: "",
35-
locales: [],
36-
isEditing: false,
37-
devices: null,
38-
focussedField: [],
39-
};
40-
41-
const EasyblocksCanvasContext = createContext<
42-
EasyblocksCanvasState | undefined
43-
>(undefined);
28+
const EasyblocksCanvasContext = createContext<EasyblocksCanvasState | null>(
29+
null
30+
);
4431

4532
type EasyblocksCanvasProviderProps = {
4633
children: ReactNode;
@@ -49,7 +36,7 @@ type EasyblocksCanvasProviderProps = {
4936
const EasyblocksCanvasProvider: React.FC<EasyblocksCanvasProviderProps> = ({
5037
children,
5138
}) => {
52-
const [state, setState] = useState<EasyblocksCanvasState>(initialState);
39+
const [state, setState] = useState<EasyblocksCanvasState | null>(null);
5340

5441
useEffect(() => {
5542
const handler = (event: any) => {
@@ -72,13 +59,7 @@ const EasyblocksCanvasProvider: React.FC<EasyblocksCanvasProviderProps> = ({
7259
};
7360

7461
const useEasyblocksCanvasContext = () => {
75-
const context = useContext(EasyblocksCanvasContext);
76-
if (!context) {
77-
throw new Error(
78-
"useEasyblocksCanvasContext must be used within a EasyblocksCanvasProvider"
79-
);
80-
}
81-
return context;
62+
return useContext(EasyblocksCanvasContext);
8263
};
8364

8465
export { EasyblocksCanvasProvider, useEasyblocksCanvasContext };

packages/editor/src/CanvasRoot/CanvasRoot.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ type CanvasRootProps = {
77
};
88

99
function CanvasRoot(props: CanvasRootProps) {
10-
const { isEditing } = useEasyblocksCanvasContext();
10+
const canvasContext = useEasyblocksCanvasContext();
11+
12+
if (!canvasContext) {
13+
return null;
14+
}
15+
16+
const { isEditing } = canvasContext;
1117

1218
useCanvasGlobalKeyboardShortcuts();
1319

packages/editor/src/EditableComponentBuilder/BlockControls.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ export function BlocksControls({
4444
index,
4545
length,
4646
}: BlocksControlsProps) {
47-
const { focussedField, formValues } = useEasyblocksCanvasContext();
47+
const canvasContext = useEasyblocksCanvasContext();
48+
49+
if (!canvasContext) {
50+
return null;
51+
}
52+
53+
const { focussedField, formValues } = canvasContext;
4854

4955
const meta = useEasyblocksMetadata();
5056
const dndContext = useDndContext();

packages/editor/src/EditableComponentBuilder/SelectionFrameController.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,13 @@ function SelectionFrameController({
110110
},
111111
});
112112

113-
const { focussedField } = useEasyblocksCanvasContext();
113+
const canvasContext = useEasyblocksCanvasContext();
114+
115+
if (!canvasContext) {
116+
return null;
117+
}
118+
119+
const { focussedField } = canvasContext;
114120

115121
useEffect(() => {
116122
return () => {

packages/editor/src/EditorChildWindow.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,17 @@ export function EasyblocksCanvas({
7575
);
7676
}, []);
7777

78-
const { meta, compiled, externalData, formValues, definitions } =
79-
useEasyblocksCanvasContext();
78+
const canvasContext = useEasyblocksCanvasContext();
8079

81-
const shouldNotRender =
82-
!enabled ||
83-
!meta ||
84-
!compiled ||
85-
!externalData ||
86-
!formValues ||
87-
!definitions;
80+
const shouldNotRender = !enabled || !canvasContext;
8881

8982
if (shouldNotRender) {
9083
return <div>Loading...</div>;
9184
}
9285

86+
const { meta, compiled, externalData, formValues, definitions } =
87+
canvasContext;
88+
9389
const sortableItems = getSortableItems(formValues, definitions);
9490

9591
return (

packages/editor/src/Placeholder.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,13 @@ type TypePlaceholderComponentBuilderProps = {
121121
export default function TypePlaceholder(
122122
props: TypePlaceholderComponentBuilderProps
123123
) {
124-
const { formValues } = useEasyblocksCanvasContext();
124+
const canvasContext = useEasyblocksCanvasContext();
125+
126+
if (!canvasContext) {
127+
return null;
128+
}
129+
130+
const { formValues } = canvasContext;
125131
const meta = useEasyblocksMetadata();
126132
const dndContext = useDndContext();
127133

packages/editor/src/useCanvasGlobalKeyboardShortcuts.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,13 @@ const actions = {
6767
};
6868

6969
function useCanvasGlobalKeyboardShortcuts() {
70-
const { formValues, definitions, focussedField } =
71-
useEasyblocksCanvasContext();
70+
const canvasContext = useEasyblocksCanvasContext();
71+
72+
if (!canvasContext) {
73+
return null;
74+
}
75+
76+
const { formValues, definitions, focussedField } = canvasContext;
7277

7378
useEffect(() => {
7479
function handleKeydown(event: KeyboardEvent): void {

0 commit comments

Comments
 (0)