Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(editor): include picker position calculation #70

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 10 additions & 3 deletions packages/editor/src/tinacms/fields/plugins/IdentityFieldPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "@easyblocks/core/_internals";
import { ButtonGhost, Icons, Typography } from "@easyblocks/design-system";
import { toArray } from "@easyblocks/utils";
import React, { useContext } from "react";
import React, { useContext, useRef } from "react";
import type { FieldRenderProps } from "react-final-form";
import { css } from "styled-components";
import { useEditorContext } from "../../../EditorContext";
Expand All @@ -25,6 +25,8 @@ function IdentityField({ input, field }: IdentityFieldProps) {
const editorContext = useEditorContext();
const panelContext = useContext(PanelContext);

const changeComponentButton = useRef<HTMLButtonElement>(null);

const isMixed = isMixedFieldValue(input.value);
const config = isMixed ? null : input.value;

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

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

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

editorContext.actions
.openComponentPicker({ path: componentPickerPath })
.openComponentPicker({ path: componentPickerPath, domRect })
.then((selectedConfig) => {
if (!selectedConfig) {
return;
Expand Down Expand Up @@ -142,7 +146,10 @@ function IdentityField({ input, field }: IdentityFieldProps) {
<div style={{ padding: "7px 6px" }}>{titleContent}</div>
)}
{!isNonChangable && (
<ButtonGhost onClick={handleChangeComponentType}>
<ButtonGhost
ref={changeComponentButton}
onClick={handleChangeComponentType}
>
{titleContent}
</ButtonGhost>
)}
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