-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmouseClick.ts
211 lines (181 loc) · 6.9 KB
/
mouseClick.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import {
cleanIndividualFieldResidual,
handleIndividualFields,
} from "../utils/handleIndividualFields";
import {
getCsDataOfElement,
getDOMEditStack,
} from "../utils/getCsDataOfElement";
import { appendFocusedToolbar } from "../generators/generateToolbar";
import { addFocusOverlay, hideOverlay } from "../generators/generateOverlay";
import visualBuilderPostMessage from "../utils/visualBuilderPostMessage";
import { VisualBuilderPostMessageEvents } from "../utils/types/postMessage.types";
import { VisualBuilder } from "..";
import { FieldSchemaMap } from "../utils/fieldSchemaMap";
import { isFieldDisabled } from "../utils/isFieldDisabled";
import EventListenerHandlerParams from "./types";
import { toggleHighlightedCommentIconDisplay } from "../generators/generateHighlightedComment";
type HandleBuilderInteractionParams = Omit<
EventListenerHandlerParams,
"eventDetails" | "customCursor"
>;
type AddFocusOverlayParams = Pick<
EventListenerHandlerParams,
"overlayWrapper" | "resizeObserver"
> & { editableElement: Element; isFieldDisabled?: boolean };
type AddFocusedToolbarParams = Pick<
EventListenerHandlerParams,
"eventDetails" | "focusedToolbar"
> & { hideOverlay: () => void };
function addOverlay(params: AddFocusOverlayParams) {
if (!params.overlayWrapper || !params.editableElement) return;
addFocusOverlay(
params.editableElement,
params.overlayWrapper,
params.isFieldDisabled
);
params.resizeObserver.observe(params.editableElement);
}
export function addFocusedToolbar(params: AddFocusedToolbarParams): void {
const { editableElement } = params.eventDetails;
if (!editableElement || !params.focusedToolbar) return;
appendFocusedToolbar(params.eventDetails, params.focusedToolbar, params.hideOverlay);
}
async function handleBuilderInteraction(
params: HandleBuilderInteractionParams
): Promise<void> {
const eventTarget = params.event.target as HTMLElement | null;
const isAnchorElement = eventTarget instanceof HTMLAnchorElement;
const elementHasCslp =
eventTarget &&
(eventTarget.hasAttribute("data-cslp") ||
eventTarget.closest("[data-cslp]"));
// prevent default behavior for anchor elements and elements with cslp attribute
if (
isAnchorElement ||
(elementHasCslp && !eventTarget.closest(".visual-builder__empty-block"))
) {
params.event.preventDefault();
params.event.stopPropagation();
}
const eventDetails = getCsDataOfElement(params.event);
visualBuilderPostMessage
?.send(VisualBuilderPostMessageEvents.MOUSE_CLICK, {
cslpData: eventDetails?.cslpData,
fieldMetadata: eventDetails?.fieldMetadata,
})
.catch((err) => {
console.warn("Error while sending post message", err);
});
if (
!eventDetails ||
!params.overlayWrapper ||
!params.visualBuilderContainer
) {
return;
}
const { editableElement, fieldMetadata } = eventDetails;
if (
VisualBuilder.VisualBuilderGlobalState.value
.previousSelectedEditableDOM &&
VisualBuilder.VisualBuilderGlobalState.value
.previousSelectedEditableDOM !== editableElement
) {
cleanIndividualFieldResidual({
overlayWrapper: params.overlayWrapper,
visualBuilderContainer: params.visualBuilderContainer,
focusedToolbar: params.focusedToolbar,
resizeObserver: params.resizeObserver,
});
}
// if the selected element is our empty block element, return
if (
editableElement.classList.contains(
"visual-builder__empty-block-parent"
) ||
editableElement.classList.contains("visual-builder__empty-block")
) {
return;
}
// when previous and current selected element is same, return.
// this also avoids inserting psuedo-editable field (field data is
// not equal to text content in DOM) when performing mouse
// selections in the content editable
const previousSelectedElement =
VisualBuilder.VisualBuilderGlobalState.value
.previousSelectedEditableDOM;
if (
previousSelectedElement &&
previousSelectedElement === editableElement
) {
return;
}
VisualBuilder.VisualBuilderGlobalState.value.previousSelectedEditableDOM =
editableElement;
addOverlay({
overlayWrapper: params.overlayWrapper,
resizeObserver: params.resizeObserver,
editableElement: editableElement,
});
addFocusedToolbar({
eventDetails: eventDetails,
focusedToolbar: params.focusedToolbar,
hideOverlay: () => {
hideOverlay({
visualBuilderContainer: params.visualBuilderContainer,
visualBuilderOverlayWrapper: params.overlayWrapper,
focusedToolbar: params.focusedToolbar,
resizeObserver: params.resizeObserver,
});
}
});
const { content_type_uid, fieldPath, cslpValue } = fieldMetadata;
toggleHighlightedCommentIconDisplay(cslpValue, false);
const fieldSchema = await FieldSchemaMap.getFieldSchema(
content_type_uid,
fieldPath
);
if (fieldSchema) {
// after field schema is available re-add disabled overlay
const { isDisabled } = isFieldDisabled(fieldSchema, eventDetails);
if (isDisabled) {
addOverlay({
overlayWrapper: params.overlayWrapper,
resizeObserver: params.resizeObserver,
editableElement: editableElement,
isFieldDisabled: true,
});
}
}
// This is most probably redundant code, as the handleIndividualFields function
// takes care of this
// TODO: Remove this
// if (
// fieldSchema.data_type === "block" ||
// fieldSchema.multiple ||
// (fieldSchema.data_type === "reference" &&
// // @ts-ignore
// fieldSchema.field_metadata.ref_multiple)
// ) {
// handleAddButtonsForMultiple(eventDetails, {
// editableElement: editableElement,
// visualBuilderContainer: params.visualBuilderContainer,
// resizeObserver: params.resizeObserver,
// });
// } else {
// removeAddInstanceButtons({
// eventTarget: params.event.target,
// visualBuilderContainer: params.visualBuilderContainer,
// overlayWrapper: params.overlayWrapper,
// });
// }
visualBuilderPostMessage?.send(VisualBuilderPostMessageEvents.FOCUS_FIELD, {
DOMEditStack: getDOMEditStack(editableElement),
});
await handleIndividualFields(eventDetails, {
visualBuilderContainer: params.visualBuilderContainer,
resizeObserver: params.resizeObserver,
lastEditedField: previousSelectedElement,
});
}
export default handleBuilderInteraction;