Skip to content

Commit c6c354b

Browse files
fix: psuedo-editable element re-positioning logic
1 parent 57d4c43 commit c6c354b

6 files changed

Lines changed: 136 additions & 91 deletions

File tree

src/visualBuilder/components/pseudoEditableField.tsx

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import classNames from "classnames";
2-
import getCamelCaseStyles from "../utils/getCamelCaseStyles";
3-
import getStyleOfAnElement from "../utils/getStyleOfAnElement";
42
import React from "preact/compat";
53
import { visualBuilderStyles } from "../visualBuilder.style";
4+
import { getPsuedoEditableElementStyles } from "../utils/getPsuedoEditableStylesElement";
65

76
interface PseudoEditableFieldProps {
87
editableElement: HTMLElement;
@@ -12,27 +11,14 @@ interface PseudoEditableFieldProps {
1211
function PseudoEditableFieldComponent(
1312
props: PseudoEditableFieldProps
1413
): JSX.Element {
15-
const styles = getCamelCaseStyles(
16-
getStyleOfAnElement(props.editableElement)
17-
);
18-
// Get the offsetTop and offsetLeft of the editable element and set the position of the pseudo editable element
19-
// The pseudo editable element is positioned absolutely at the same location as the editable element
20-
const rect = props.editableElement.getBoundingClientRect();
21-
22-
styles.position = "absolute";
23-
styles.top = `${rect.top + window.scrollY}px`;
24-
styles.left = `${rect.left + window.scrollX}px`;
25-
// setting height to auto so that the element can grow based on the content
26-
// and the resize observer can detect the change in height
27-
styles.height = "auto";
28-
styles.whiteSpace = "pre-line"
29-
styles.textTransform = "none"
14+
const styles = getPsuedoEditableElementStyles(props.editableElement, true);
3015

3116
return (
3217
<div
33-
className={
34-
classNames("visual-builder__pseudo-editable-element", visualBuilderStyles()["visual-builder__pseudo-editable-element"])
35-
}
18+
className={classNames(
19+
"visual-builder__pseudo-editable-element",
20+
visualBuilderStyles()["visual-builder__pseudo-editable-element"]
21+
)}
3622
data-testid="visual-builder__pseudo-editable-element"
3723
style={styles}
3824
>

src/visualBuilder/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ export class VisualBuilder {
7676
visualBuilderContainer: this.visualBuilderContainer,
7777
overlayWrapper: this.overlayWrapper,
7878
focusedToolbar: this.focusedToolbar,
79-
resizeObserver: this.resizeObserver,
8079
});
8180
}
8281

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import getCamelCaseStyles from "./getCamelCaseStyles";
2+
import getStyleOfAnElement from "./getStyleOfAnElement";
3+
4+
export function getPsuedoEditableElementStyles(
5+
psuedoEditableElement: HTMLElement,
6+
camelCase?: boolean
7+
): { [key: string]: string } {
8+
let styles = getStyleOfAnElement(psuedoEditableElement);
9+
if (camelCase) {
10+
styles = getCamelCaseStyles(styles);
11+
}
12+
// Get the offsetTop and offsetLeft of the editable element and set the position of the pseudo editable element
13+
// The pseudo editable element is positioned absolutely at the same location as the editable element
14+
const rect = psuedoEditableElement.getBoundingClientRect();
15+
16+
styles.position = "absolute";
17+
styles.top = `${rect.top + window.scrollY}px`;
18+
styles.left = `${rect.left + window.scrollX}px`;
19+
// setting height to auto so that the element can grow based on the content
20+
// and the resize observer can detect the change in height
21+
styles.height = "auto";
22+
styles.whiteSpace = "pre-line";
23+
styles.textTransform = "none";
24+
25+
return styles;
26+
}

src/visualBuilder/utils/getStyleOfAnElement.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export default function getStyleOfAnElement(element: HTMLElement): {
1717
"right",
1818
"bottom",
1919
"text-overflow",
20+
// allows seeing the text from CMS field as-is
21+
"text-transform",
2022
"margin",
2123
"margin-block-end",
2224
"margin-block-start",

src/visualBuilder/utils/handleIndividualFields.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ export async function handleIndividualFields(
200200
editableElement: actualEditableField,
201201
visualBuilderContainer,
202202
overlayWrapper,
203-
resizeObserver,
204203
focusedToolbar,
205204
});
206205
}, 200);

0 commit comments

Comments
 (0)