Skip to content

Commit

Permalink
feat(repo): canvascontext dont expose context null
Browse files Browse the repository at this point in the history
  • Loading branch information
maarten2424 committed May 8, 2024
1 parent d63c639 commit 4a37ec9
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,15 @@ interface RichTextProps extends InternalNoCodeComponentProps {

function RichTextEditor(props: RichTextProps) {
// const { editorContext } = (window.parent as any).editorWindowAPI;

const canvasContext = useEasyblocksCanvasContext();

if (!canvasContext) {
return null;
}

const { formValues, locales, locale, focussedField, definitions } =
useEasyblocksCanvasContext();
canvasContext;

// const {
// actions,
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/compiler/builtins/$text/$text.editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ function TextEditor(props: TextProps) {
__easyblocks: { path, runtime },
} = props;

const { formValues } = useEasyblocksCanvasContext();
const canvasContext = useEasyblocksCanvasContext();

if (!canvasContext) {
return null;
}
const { formValues } = canvasContext;

const valuePath = `${path}.value`;
const configValue = dotNotationGet(formValues, valuePath);
Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/compiler/builtins/$text/InlineTextarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ export function InlineTextarea({
}: InlineTextProps) {
const [isEnabled, setIsEnabled] = useState(false);
const textAreaRef = useRef<ElementRef<"textarea">>(null);
const { formValues, locale, locales } = useEasyblocksCanvasContext();

const canvasContext = useEasyblocksCanvasContext();

if (!canvasContext) {
return null;
}

const { formValues, locale, locales } = canvasContext;

const valuePath = `${path}.value`;
const value = dotNotationGet(formValues, valuePath);
Expand Down
41 changes: 11 additions & 30 deletions packages/core/src/components/EasyblocksCanvasProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,21 @@ import {
} from "../types";

type EasyblocksCanvasState = {
meta: CompilationMetadata | null;
compiled: CompiledShopstoryComponentConfig | null;
externalData: FetchOutputResources | null;
formValues: EditorContextType["form"]["values"] | null;
definitions: EditorContextType["definitions"] | null;
meta: CompilationMetadata;
compiled: CompiledShopstoryComponentConfig;
externalData: FetchOutputResources;
formValues: EditorContextType["form"]["values"];
definitions: EditorContextType["definitions"];
locale: EditorContextType["contextParams"]["locale"];
locales: EditorContextType["locales"];
isEditing: EditorContextType["isEditing"];
devices: EditorContextType["devices"] | null;
devices: EditorContextType["devices"];
focussedField: EditorContextType["focussedField"];
};

const initialState: EasyblocksCanvasState = {
meta: null,
compiled: null,
externalData: null,
formValues: null,
definitions: null,
locale: "",
locales: [],
isEditing: false,
devices: null,
focussedField: [],
};

const EasyblocksCanvasContext = createContext<
EasyblocksCanvasState | undefined
>(undefined);
const EasyblocksCanvasContext = createContext<EasyblocksCanvasState | null>(
null
);

type EasyblocksCanvasProviderProps = {
children: ReactNode;
Expand All @@ -49,7 +36,7 @@ type EasyblocksCanvasProviderProps = {
const EasyblocksCanvasProvider: React.FC<EasyblocksCanvasProviderProps> = ({
children,
}) => {
const [state, setState] = useState<EasyblocksCanvasState>(initialState);
const [state, setState] = useState<EasyblocksCanvasState | null>(null);

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

const useEasyblocksCanvasContext = () => {
const context = useContext(EasyblocksCanvasContext);
if (!context) {
throw new Error(
"useEasyblocksCanvasContext must be used within a EasyblocksCanvasProvider"
);
}
return context;
return useContext(EasyblocksCanvasContext);
};

export { EasyblocksCanvasProvider, useEasyblocksCanvasContext };
8 changes: 7 additions & 1 deletion packages/editor/src/CanvasRoot/CanvasRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ type CanvasRootProps = {
};

function CanvasRoot(props: CanvasRootProps) {
const { isEditing } = useEasyblocksCanvasContext();
const canvasContext = useEasyblocksCanvasContext();

if (!canvasContext) {
return null;
}

const { isEditing } = canvasContext;

useCanvasGlobalKeyboardShortcuts();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ export function BlocksControls({
index,
length,
}: BlocksControlsProps) {
const { focussedField, formValues } = useEasyblocksCanvasContext();
const canvasContext = useEasyblocksCanvasContext();

if (!canvasContext) {
return null;
}

const { focussedField, formValues } = canvasContext;

const meta = useEasyblocksMetadata();
const dndContext = useDndContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ function SelectionFrameController({
},
});

const { focussedField } = useEasyblocksCanvasContext();
const canvasContext = useEasyblocksCanvasContext();

if (!canvasContext) {
return null;
}

const { focussedField } = canvasContext;

useEffect(() => {
return () => {
Expand Down
14 changes: 5 additions & 9 deletions packages/editor/src/EditorChildWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,17 @@ export function EasyblocksCanvas({
);
}, []);

const { meta, compiled, externalData, formValues, definitions } =
useEasyblocksCanvasContext();
const canvasContext = useEasyblocksCanvasContext();

const shouldNotRender =
!enabled ||
!meta ||
!compiled ||
!externalData ||
!formValues ||
!definitions;
const shouldNotRender = !enabled || !canvasContext;

if (shouldNotRender) {
return <div>Loading...</div>;
}

const { meta, compiled, externalData, formValues, definitions } =
canvasContext;

const sortableItems = getSortableItems(formValues, definitions);

return (
Expand Down
8 changes: 7 additions & 1 deletion packages/editor/src/Placeholder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,13 @@ type TypePlaceholderComponentBuilderProps = {
export default function TypePlaceholder(
props: TypePlaceholderComponentBuilderProps
) {
const { formValues } = useEasyblocksCanvasContext();
const canvasContext = useEasyblocksCanvasContext();

if (!canvasContext) {
return null;
}

const { formValues } = canvasContext;
const meta = useEasyblocksMetadata();
const dndContext = useDndContext();

Expand Down
9 changes: 7 additions & 2 deletions packages/editor/src/useCanvasGlobalKeyboardShortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ const actions = {
};

function useCanvasGlobalKeyboardShortcuts() {
const { formValues, definitions, focussedField } =
useEasyblocksCanvasContext();
const canvasContext = useEasyblocksCanvasContext();

if (!canvasContext) {
return null;
}

const { formValues, definitions, focussedField } = canvasContext;

useEffect(() => {
function handleKeydown(event: KeyboardEvent): void {
Expand Down

0 comments on commit 4a37ec9

Please sign in to comment.