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

Handle remove focus event #290

Merged
merged 4 commits into from
Oct 29, 2024
Merged
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
63 changes: 33 additions & 30 deletions src/visualBuilder/generators/generateOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function hideFocusOverlay(elements: HideOverlayParams): void {
visualBuilderOverlayWrapper,
focusedToolbar,
resizeObserver,
noTrigger
noTrigger,
} = elements;

if (visualBuilderOverlayWrapper) {
Expand All @@ -119,13 +119,23 @@ export function hideFocusOverlay(elements: HideOverlayParams): void {
}
});

if(!noTrigger){
if (!noTrigger) {
sendFieldEvent({
visualBuilderContainer,
eventType: VisualBuilderPostMessageEvents.UPDATE_FIELD,
});
} else {
const { previousSelectedEditableDOM, focusFieldValue } =
VisualBuilder.VisualBuilderGlobalState.value || {};
if (
previousSelectedEditableDOM &&
"innerText" in previousSelectedEditableDOM &&
focusFieldValue != null
) {
previousSelectedEditableDOM.innerText = focusFieldValue;
}
}

VisualBuilder.VisualBuilderGlobalState.value.focusFieldValue = null;
cleanIndividualFieldResidual({
overlayWrapper: visualBuilderOverlayWrapper,
visualBuilderContainer: visualBuilderContainer,
Expand All @@ -136,23 +146,22 @@ export function hideFocusOverlay(elements: HideOverlayParams): void {
}
interface ISendFieldEventParams {
visualBuilderContainer: HTMLElement | null;
eventType: VisualBuilderPostMessageEvents.UPDATE_FIELD | VisualBuilderPostMessageEvents.SYNC_FIELD
eventType:
| VisualBuilderPostMessageEvents.UPDATE_FIELD
| VisualBuilderPostMessageEvents.SYNC_FIELD;
}
export function sendFieldEvent(options: ISendFieldEventParams): void {
const {
visualBuilderContainer,
eventType
} = options;
const previousSelectedEditableDOM = VisualBuilder.VisualBuilderGlobalState.value.previousSelectedEditableDOM;
const { visualBuilderContainer, eventType } = options;
const previousSelectedEditableDOM =
VisualBuilder.VisualBuilderGlobalState.value
.previousSelectedEditableDOM;
const pseudoEditableElement = visualBuilderContainer?.querySelector(
"div.visual-builder__pseudo-editable-element"
);
if (
previousSelectedEditableDOM &&
(
previousSelectedEditableDOM.hasAttribute("contenteditable") ||
pseudoEditableElement
)
(previousSelectedEditableDOM.hasAttribute("contenteditable") ||
pseudoEditableElement)
) {
const actualEditedElement =
pseudoEditableElement ||
Expand All @@ -164,37 +173,31 @@ export function sendFieldEvent(options: ISendFieldEventParams): void {
: actualEditedElement.textContent;

const fieldMetadata = extractDetailsFromCslp(
previousSelectedEditableDOM.getAttribute(
"data-cslp"
) as string
previousSelectedEditableDOM.getAttribute("data-cslp") as string
);

FieldSchemaMap.getFieldSchema(
fieldMetadata.content_type_uid,
fieldMetadata.fieldPath
)
.then((fieldSchema) => {
if (fieldSchema && eventType === VisualBuilderPostMessageEvents.UPDATE_FIELD) {
if (
fieldSchema &&
eventType === VisualBuilderPostMessageEvents.UPDATE_FIELD
) {
const fieldType = getFieldType(fieldSchema);
if (
fieldType &&
fieldType === FieldDataType.MULTILINE
) {
data =
getMultilinePlaintext(actualEditedElement);
if (fieldType && fieldType === FieldDataType.MULTILINE) {
data = getMultilinePlaintext(actualEditedElement);
(actualEditedElement as HTMLElement).innerText =
data as string;
}
}
})
.finally(() => {
visualBuilderPostMessage?.send(
eventType,
{
data,
fieldMetadata,
}
);
visualBuilderPostMessage?.send(eventType, {
data,
fieldMetadata,
});
});
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/visualBuilder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ interface VisualBuilderGlobalStateImpl {
previousSelectedEditableDOM: HTMLElement | Element | null;
previousHoveredTargetDOM: Element | null;
previousEmptyBlockParents: Element[] | [];
focusFieldValue: string | null;
audienceMode: boolean;
locale: string;
variant: string | null;
Expand All @@ -63,6 +64,7 @@ export class VisualBuilder {
previousSelectedEditableDOM: null,
previousHoveredTargetDOM: null,
previousEmptyBlockParents: [],
focusFieldValue: null,
audienceMode: false,
locale: Config.get().stackDetails.masterLocale || "en-us",
variant: null,
Expand Down Expand Up @@ -336,6 +338,7 @@ export class VisualBuilder {
previousSelectedEditableDOM: null,
previousHoveredTargetDOM: null,
previousEmptyBlockParents: [],
focusFieldValue: null,
audienceMode: false,
locale: "en-us",
variant: null,
Expand Down
4 changes: 4 additions & 0 deletions src/visualBuilder/utils/handleIndividualFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ export async function handleIndividualFields(
// * title, single single_line, single multi_line, single number
if (ALLOWED_INLINE_EDITABLE_FIELD.includes(fieldType)) {
let actualEditableField = editableElement as HTMLElement;

VisualBuilder.VisualBuilderGlobalState.value.focusFieldValue =
actualEditableField?.innerText;

const elementComputedDisplay =
window.getComputedStyle(actualEditableField).display;

Expand Down
Loading