Skip to content

Commit

Permalink
feat(editor): include picker position calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
maarten2424 committed Jul 31, 2024
1 parent c97eaa1 commit cd7144f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/editor/src/ModalPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export const ModalPicker: FC<ModalProps> = ({ config, onClose, pickers }) => {
onClose: onModalClose,
templates: templatesDictionary,
mode: picker,
domRect: config.domRect,
})
) : (
<div>Unknown picker: {picker}</div>
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/TemplatePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type TemplatePickerProps = {
templates?: TemplatesDictionary;
onClose: (template?: Template) => void;
mode?: string;
domRect?: DOMRect;
};

export type TemplatePicker<T = Record<never, never>> = React.FC<
Expand Down
6 changes: 4 additions & 2 deletions packages/editor/src/selectionFrame/AddButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface AddButtonProps {
position: "before" | "after";
index?: number;
offset?: number | { x: number; y: number };
onClick?: () => void;
onClick?: ($event?: any) => void;
}

function AddButton({ position, index, offset, onClick }: AddButtonProps) {
Expand All @@ -25,9 +25,11 @@ function AddButton({ position, index, offset, onClick }: AddButtonProps) {
event.stopPropagation();
event.preventDefault();

const domRect = addBlockButtonRef.current?.getBoundingClientRect();

// Custom add action
if (onClick) {
onClick();
onClick(domRect);
return;
}
};
Expand Down
14 changes: 10 additions & 4 deletions packages/editor/src/selectionFrame/SelectionFrame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ function SelectionFrame({ width, height, transform }: SelectionFrameProps) {
};
}, [direction, height, isAddingEnabled, width]);

async function handleAddButtonClick(which: "before" | "after") {
async function handleAddButtonClick(
domRect: DOMRect,
which: "before" | "after"
) {
let path = focussedField.length === 1 ? focussedField[0] : undefined;

if (!path) {
Expand Down Expand Up @@ -129,7 +132,10 @@ function SelectionFrame({ width, height, transform }: SelectionFrameProps) {
const parentPath =
parent.path + (parent.path === "" ? "" : ".") + parent.fieldName;

const config = await actions.openComponentPicker({ path: parentPath });
const config = await actions.openComponentPicker({
path: parentPath,
domRect,
});

if (config) {
actions.insertItem({
Expand All @@ -148,11 +154,11 @@ function SelectionFrame({ width, height, transform }: SelectionFrameProps) {
<FrameWrapper width={width} height={height} transform={transform}>
<AddButton
position="before"
onClick={() => handleAddButtonClick("before")}
onClick={(domRect) => handleAddButtonClick(domRect, "before")}
/>
<AddButton
position="after"
onClick={() => handleAddButtonClick("after")}
onClick={(domRect) => handleAddButtonClick(domRect, "after")}
/>
</FrameWrapper>
</Wrapper>
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { EditorContextType } from "./EditorContext";
export type OpenComponentPickerConfig = {
path: string;
componentTypes?: string[];
domRect?: DOMRect;
};

export type MoveItemActionType = {
Expand Down

0 comments on commit cd7144f

Please sign in to comment.