-
Notifications
You must be signed in to change notification settings - Fork 1
/
generateToolbar.tsx
131 lines (114 loc) · 4.35 KB
/
generateToolbar.tsx
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
import { VisualBuilderCslpEventDetails } from "../types/visualBuilder.types";
import {
DATA_CSLP_ATTR_SELECTOR,
LIVE_PREVIEW_OUTLINE_WIDTH_IN_PX,
RIGHT_EDGE_BUFFER,
TOOLBAR_EDGE_BUFFER,
TOP_EDGE_BUFFER,
} from "../utils/constants";
import { FieldSchemaMap } from "../utils/fieldSchemaMap";
import { isFieldDisabled } from "../utils/isFieldDisabled";
import FieldToolbarComponent from "../components/FieldToolbar";
import { render } from "preact";
import FieldLabelWrapperComponent from "../components/fieldLabelWrapper";
export function appendFocusedToolbar(
eventDetails: VisualBuilderCslpEventDetails,
focusedToolbarElement: HTMLDivElement,
hideOverlay: () => void
): void {
appendFieldPathDropdown(eventDetails, focusedToolbarElement);
appendFieldToolbar(eventDetails, focusedToolbarElement, hideOverlay);
}
export function appendFieldToolbar(
eventDetails: VisualBuilderCslpEventDetails,
focusedToolbarElement: HTMLDivElement,
hideOverlay: () => void
): void {
if (
focusedToolbarElement.querySelector(
".visual-builder__focused-toolbar__multiple-field-toolbar"
)
)
return;
const wrapper = document.createDocumentFragment();
render(
<FieldToolbarComponent
eventDetails={eventDetails}
hideOverlay={hideOverlay}
/>,
wrapper
);
focusedToolbarElement.append(wrapper);
}
export function appendFieldPathDropdown(
eventDetails: VisualBuilderCslpEventDetails,
focusedToolbarElement: HTMLDivElement
): void {
if(document.querySelector(".visual-builder__focused-toolbar__field-label-wrapper"))
return;
const { editableElement: targetElement, fieldMetadata } = eventDetails;
const targetElementDimension = targetElement.getBoundingClientRect();
const distanceFromTop =
targetElementDimension.top + window.scrollY - TOOLBAR_EDGE_BUFFER;
// Position the toolbar at the top unless there's insufficient space or scrolling up is not possible (topmost element targetted).
const adjustedDistanceFromTop =
targetElementDimension.top + window.scrollY < TOP_EDGE_BUFFER
? distanceFromTop + targetElementDimension.height + TOP_EDGE_BUFFER
: distanceFromTop;
const distanceFromLeft =
targetElementDimension.left - LIVE_PREVIEW_OUTLINE_WIDTH_IN_PX;
const adjustedDistanceFromLeft = Math.max(
distanceFromLeft,
TOOLBAR_EDGE_BUFFER
);
const targetElementRightEdgeOffset =
window.scrollX + window.innerWidth - targetElementDimension.left;
if (targetElementRightEdgeOffset < RIGHT_EDGE_BUFFER) {
// Overflow / Cutoff on right edge
focusedToolbarElement.style.justifyContent = "flex-end";
focusedToolbarElement.style.left = `${
targetElementDimension.right + LIVE_PREVIEW_OUTLINE_WIDTH_IN_PX
}px`;
} else {
focusedToolbarElement.style.justifyContent = "flex-start"; // default
focusedToolbarElement.style.left = `${adjustedDistanceFromLeft}px`;
}
focusedToolbarElement.style.top = `${adjustedDistanceFromTop}px`;
const parentPaths = collectParentCSLPPaths(targetElement, 2);
const wrapper = document.createDocumentFragment();
render(
<FieldLabelWrapperComponent
fieldMetadata={fieldMetadata}
eventDetails={eventDetails}
parentPaths={parentPaths}
getParentEditableElement={(cslp: string) => {
const parentElement = targetElement.closest(
`[${DATA_CSLP_ATTR_SELECTOR}="${cslp}"]`
) as HTMLElement | null;
return parentElement;
}}
/>,
wrapper
);
focusedToolbarElement.appendChild(wrapper);
}
function collectParentCSLPPaths(
targetElement: Element,
count: number
): Array<string> {
const cslpPaths: Array<string> = [];
let currentElement = targetElement.parentElement;
while (count > 0 || currentElement === window.document.body) {
if (!currentElement) {
return cslpPaths;
}
if (currentElement.hasAttribute(DATA_CSLP_ATTR_SELECTOR)) {
cslpPaths.push(
currentElement.getAttribute(DATA_CSLP_ATTR_SELECTOR) as string
);
count--;
}
currentElement = currentElement.parentElement;
}
return cslpPaths;
}