-
Notifications
You must be signed in to change notification settings - Fork 1
/
handleIndividualFields.ts
303 lines (274 loc) · 10.8 KB
/
handleIndividualFields.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import { debounce, throttle } from "lodash-es";
import { VisualBuilder } from "..";
import {
generatePseudoEditableElement,
isEllipsisActive,
} from "../generators/generatePseudoEditableField";
import { VisualBuilderCslpEventDetails } from "../types/visualBuilder.types";
import {
ALLOWED_INLINE_EDITABLE_FIELD,
VISUAL_BUILDER_FIELD_TYPE_ATTRIBUTE_KEY,
} from "./constants";
import { FieldSchemaMap } from "./fieldSchemaMap";
import { getFieldData } from "./getFieldData";
import { getFieldType } from "./getFieldType";
import { handleFieldInput, handleFieldKeyDown } from "./handleFieldMouseDown";
import { isFieldDisabled } from "./isFieldDisabled";
import visualBuilderPostMessage from "./visualBuilderPostMessage";
import {
handleAddButtonsForMultiple,
removeAddInstanceButtons,
} from "./multipleElementAddButton";
import { VisualBuilderPostMessageEvents } from "./types/postMessage.types";
import { updateFocussedState } from "./updateFocussedState";
import { FieldDataType } from "./types/index.types";
import { getMultilinePlaintext } from "./getMultilinePlaintext";
/**
* It handles all the fields based on their data type and its "multiple" property.
* @param eventDetails The event details object that contain cslp and field metadata.
* @param elements The elements object that contain the visual builder wrapper.
*/
export async function handleIndividualFields(
eventDetails: VisualBuilderCslpEventDetails,
elements: {
visualBuilderContainer: HTMLDivElement;
resizeObserver: ResizeObserver;
lastEditedField: Element | null;
}
): Promise<void> {
const { fieldMetadata, editableElement } = eventDetails;
const { visualBuilderContainer, lastEditedField, resizeObserver } =
elements;
const {
content_type_uid,
entry_uid,
locale,
fieldPath,
fieldPathWithIndex,
} = fieldMetadata;
const [fieldSchema, expectedFieldData] = await Promise.all([
FieldSchemaMap.getFieldSchema(content_type_uid, fieldPath),
getFieldData(
{ content_type_uid, entry_uid, locale },
fieldPathWithIndex
),
]);
// if value is an array, get the value for the instance
const expectedFieldInstanceData = Array.isArray(expectedFieldData)
? expectedFieldData.at(fieldMetadata.multipleFieldMetadata.index)
: undefined;
const fieldType = getFieldType(fieldSchema);
const { isDisabled: disabled } = isFieldDisabled(fieldSchema, eventDetails);
editableElement.setAttribute(
VISUAL_BUILDER_FIELD_TYPE_ATTRIBUTE_KEY,
fieldType
);
if (
fieldSchema &&
(fieldSchema?.multiple ||
(fieldSchema?.data_type === "reference" &&
// @ts-ignore
fieldSchema?.field_metadata.ref_multiple))
) {
if (lastEditedField !== editableElement) {
const addButtonLabel =
fieldSchema.data_type === "blocks"
? // ? `Add ${fieldSchema.display_name ?? "Modular Block"}`
"Add Section"
: undefined;
handleAddButtonsForMultiple(
eventDetails,
{
editableElement: eventDetails.editableElement,
visualBuilderContainer: visualBuilderContainer,
resizeObserver: resizeObserver,
},
{
fieldSchema,
expectedFieldData,
disabled,
label: addButtonLabel,
}
);
}
// * fields could be handled as they are in a single instance
if (eventDetails.fieldMetadata.multipleFieldMetadata.index > -1) {
handleSingleField(
{
editableElement,
visualBuilderContainer,
resizeObserver: elements.resizeObserver,
},
{ expectedFieldData: expectedFieldInstanceData, disabled }
);
}
} else {
handleSingleField(
{
editableElement,
visualBuilderContainer,
resizeObserver: elements.resizeObserver,
},
{ expectedFieldData, disabled }
);
}
/**
* Handles all the fields based on their data type.
*/
function handleSingleField(
elements: {
editableElement: Element;
visualBuilderContainer: HTMLDivElement;
resizeObserver: ResizeObserver;
},
config: { expectedFieldData: string; disabled?: boolean }
) {
const { editableElement, visualBuilderContainer } = elements;
if (config.disabled) {
return;
}
// * title, single single_line, single multi_line, single number
if (ALLOWED_INLINE_EDITABLE_FIELD.includes(fieldType)) {
if(fieldSchema.multiple){
const isFieldContainer = Number(fieldMetadata.instance.fieldPathWithIndex.split('.').at(-1))
if(Number.isNaN(isFieldContainer)) return;
}
let actualEditableField = editableElement as HTMLElement;
VisualBuilder.VisualBuilderGlobalState.value.focusFieldValue =
actualEditableField?.innerText;
const elementComputedDisplay =
window.getComputedStyle(actualEditableField).display;
let textContent =
(editableElement as HTMLElement).innerText ||
editableElement.textContent ||
"";
if (fieldType === FieldDataType.MULTILINE) {
textContent = getMultilinePlaintext(actualEditableField);
actualEditableField.addEventListener("paste", pasteAsPlainText);
}
const expectedTextContent = config.expectedFieldData;
if (
textContent !== expectedTextContent ||
isEllipsisActive(editableElement as HTMLElement)
) {
// TODO: Testing will be don in the E2E.
const pseudoEditableField = generatePseudoEditableElement(
{ editableElement: editableElement as HTMLElement },
{ textContent: config.expectedFieldData }
);
(editableElement as HTMLElement).style.visibility = "hidden";
// set field type attribute to the pseudo editable field
// ensures proper keydown handling similar to the actual editable field
pseudoEditableField.setAttribute(
VISUAL_BUILDER_FIELD_TYPE_ATTRIBUTE_KEY,
fieldType
);
visualBuilderContainer.appendChild(pseudoEditableField);
actualEditableField = pseudoEditableField;
if (fieldType === FieldDataType.MULTILINE)
actualEditableField.addEventListener(
"paste",
pasteAsPlainText
);
// we will unobserve this in hideOverlay
elements.resizeObserver.observe(pseudoEditableField);
} else if (elementComputedDisplay === "inline") {
// if the editable field is inline
const onInlineElementInput = throttle(() => {
const overlayWrapper = visualBuilderContainer.querySelector(
".visual-builder__overlay__wrapper"
) as HTMLDivElement;
const focusedToolbar = visualBuilderContainer.querySelector(
".visual-builder__focused-toolbar"
) as HTMLDivElement;
updateFocussedState({
editableElement: actualEditableField,
visualBuilderContainer,
overlayWrapper,
focusedToolbar,
resizeObserver,
});
}, 200);
actualEditableField.addEventListener(
"input",
onInlineElementInput
);
}
actualEditableField.setAttribute("contenteditable", "true");
actualEditableField.addEventListener("input", handleFieldInput);
actualEditableField.addEventListener("keydown", handleFieldKeyDown);
// focus on the contenteditable element to start accepting input
actualEditableField.focus();
return;
}
}
}
export function cleanIndividualFieldResidual(elements: {
overlayWrapper: HTMLDivElement;
visualBuilderContainer: HTMLDivElement | null;
focusedToolbar: HTMLDivElement | null;
resizeObserver: ResizeObserver;
}): void {
const { overlayWrapper, visualBuilderContainer, focusedToolbar } = elements;
removeAddInstanceButtons(
{
eventTarget: null,
visualBuilderContainer: visualBuilderContainer,
overlayWrapper: overlayWrapper,
},
true
);
const previousSelectedEditableDOM =
VisualBuilder.VisualBuilderGlobalState.value
.previousSelectedEditableDOM;
if (previousSelectedEditableDOM) {
previousSelectedEditableDOM.removeAttribute(
VISUAL_BUILDER_FIELD_TYPE_ATTRIBUTE_KEY
);
previousSelectedEditableDOM.removeAttribute("contenteditable");
previousSelectedEditableDOM.removeEventListener(
"input",
handleFieldInput
);
previousSelectedEditableDOM.removeEventListener(
"keydown",
handleFieldKeyDown
);
previousSelectedEditableDOM.removeEventListener(
"paste",
pasteAsPlainText
);
// Note - this happens in two places, 1. hideOverlay and 2. here
// TODO maybe see all usages of both functions and try to do it in one place
elements.resizeObserver.unobserve(previousSelectedEditableDOM);
}
const pseudoEditableElement = visualBuilderContainer?.querySelector(
".visual-builder__pseudo-editable-element"
);
if (pseudoEditableElement) {
elements.resizeObserver.unobserve(pseudoEditableElement);
pseudoEditableElement.removeEventListener("paste", pasteAsPlainText);
pseudoEditableElement.remove();
if (previousSelectedEditableDOM) {
(previousSelectedEditableDOM as HTMLElement).style.removeProperty(
"visibility"
);
}
}
if (focusedToolbar) {
focusedToolbar.innerHTML = "";
}
}
const pasteAsPlainText = debounce(
(e: Event) => {
e.preventDefault();
const clipboardData = (e as ClipboardEvent).clipboardData;
document.execCommand(
"inserttext",
false,
clipboardData?.getData("text/plain")
);
},
100,
{ leading: true }
);