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 c515c38
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 34 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
29 changes: 5 additions & 24 deletions packages/core/src/components/EasyblocksCanvasProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,9 @@ type EasyblocksCanvasState = {
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
8 changes: 7 additions & 1 deletion packages/editor/src/EditorChildWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,14 @@ export function EasyblocksCanvas({
);
}, []);

const canvasContext = useEasyblocksCanvasContext();

if (!canvasContext) {
return null;
}

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

const shouldNotRender =
!enabled ||
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 c515c38

Please sign in to comment.