Skip to content

Commit 865d016

Browse files
committed
feat(editor): include picker position calculation
1 parent c97eaa1 commit 865d016

File tree

6 files changed

+27
-9
lines changed

6 files changed

+27
-9
lines changed

packages/editor/src/ModalPicker.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export const ModalPicker: FC<ModalProps> = ({ config, onClose, pickers }) => {
115115
onClose: onModalClose,
116116
templates: templatesDictionary,
117117
mode: picker,
118+
domRect: config.domRect,
118119
})
119120
) : (
120121
<div>Unknown picker: {picker}</div>

packages/editor/src/TemplatePicker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type TemplatePickerProps = {
1212
templates?: TemplatesDictionary;
1313
onClose: (template?: Template) => void;
1414
mode?: string;
15+
domRect?: DOMRect;
1516
};
1617

1718
export type TemplatePicker<T = Record<never, never>> = React.FC<

packages/editor/src/selectionFrame/AddButton.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface AddButtonProps {
1414
position: "before" | "after";
1515
index?: number;
1616
offset?: number | { x: number; y: number };
17-
onClick?: () => void;
17+
onClick?: ($event?: any) => void;
1818
}
1919

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

28+
const domRect = addBlockButtonRef.current?.getBoundingClientRect();
29+
2830
// Custom add action
2931
if (onClick) {
30-
onClick();
32+
onClick(domRect);
3133
return;
3234
}
3335
};

packages/editor/src/selectionFrame/SelectionFrame.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ function SelectionFrame({ width, height, transform }: SelectionFrameProps) {
9696
};
9797
}, [direction, height, isAddingEnabled, width]);
9898

99-
async function handleAddButtonClick(which: "before" | "after") {
99+
async function handleAddButtonClick(
100+
domRect: DOMRect,
101+
which: "before" | "after"
102+
) {
100103
let path = focussedField.length === 1 ? focussedField[0] : undefined;
101104

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

132-
const config = await actions.openComponentPicker({ path: parentPath });
135+
const config = await actions.openComponentPicker({
136+
path: parentPath,
137+
domRect,
138+
});
133139

134140
if (config) {
135141
actions.insertItem({
@@ -148,11 +154,11 @@ function SelectionFrame({ width, height, transform }: SelectionFrameProps) {
148154
<FrameWrapper width={width} height={height} transform={transform}>
149155
<AddButton
150156
position="before"
151-
onClick={() => handleAddButtonClick("before")}
157+
onClick={(domRect) => handleAddButtonClick(domRect, "before")}
152158
/>
153159
<AddButton
154160
position="after"
155-
onClick={() => handleAddButtonClick("after")}
161+
onClick={(domRect) => handleAddButtonClick(domRect, "after")}
156162
/>
157163
</FrameWrapper>
158164
</Wrapper>

packages/editor/src/tinacms/fields/plugins/IdentityFieldPlugin.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from "@easyblocks/core/_internals";
1010
import { ButtonGhost, Icons, Typography } from "@easyblocks/design-system";
1111
import { toArray } from "@easyblocks/utils";
12-
import React, { useContext } from "react";
12+
import React, { useContext, useRef } from "react";
1313
import type { FieldRenderProps } from "react-final-form";
1414
import { css } from "styled-components";
1515
import { useEditorContext } from "../../../EditorContext";
@@ -25,6 +25,8 @@ function IdentityField({ input, field }: IdentityFieldProps) {
2525
const editorContext = useEditorContext();
2626
const panelContext = useContext(PanelContext);
2727

28+
const changeComponentButton = useRef<HTMLButtonElement>(null);
29+
2830
const isMixed = isMixedFieldValue(input.value);
2931
const config = isMixed ? null : input.value;
3032

@@ -71,8 +73,10 @@ function IdentityField({ input, field }: IdentityFieldProps) {
7173

7274
const componentPickerPath = parent.path + "." + parent.fieldName;
7375

76+
const domRect = changeComponentButton?.current?.getBoundingClientRect();
77+
7478
editorContext.actions
75-
.openComponentPicker({ path: componentPickerPath })
79+
.openComponentPicker({ path: componentPickerPath, domRect })
7680
.then((selectedConfig) => {
7781
if (!selectedConfig) {
7882
return;
@@ -142,7 +146,10 @@ function IdentityField({ input, field }: IdentityFieldProps) {
142146
<div style={{ padding: "7px 6px" }}>{titleContent}</div>
143147
)}
144148
{!isNonChangable && (
145-
<ButtonGhost onClick={handleChangeComponentType}>
149+
<ButtonGhost
150+
ref={changeComponentButton}
151+
onClick={handleChangeComponentType}
152+
>
146153
{titleContent}
147154
</ButtonGhost>
148155
)}

packages/editor/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { EditorContextType } from "./EditorContext";
1111
export type OpenComponentPickerConfig = {
1212
path: string;
1313
componentTypes?: string[];
14+
domRect?: DOMRect;
1415
};
1516

1617
export type MoveItemActionType = {

0 commit comments

Comments
 (0)