From f58f3093212372fc6d83354d9bf8f4f0edeaae87 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 27 May 2024 14:12:38 -0400 Subject: [PATCH] Simplify layout of full-width images --- app/scenes/Document/components/Document.tsx | 22 ++++++++++++++++--- shared/editor/components/Image.tsx | 24 +++------------------ shared/editor/components/Styles.ts | 2 +- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/app/scenes/Document/components/Document.tsx b/app/scenes/Document/components/Document.tsx index abd93bd49..1a505d53d 100644 --- a/app/scenes/Document/components/Document.tsx +++ b/app/scenes/Document/components/Document.tsx @@ -17,6 +17,7 @@ import { import { toast } from "sonner"; import styled from "styled-components"; import breakpoint from "styled-components-breakpoint"; +import useComponentSize from "@shared/editor/components/hooks/useComponentSize"; import { s } from "@shared/styles"; import { NavigationNode } from "@shared/types"; import { ProsemirrorHelper, Heading } from "@shared/utils/ProsemirrorHelper"; @@ -437,8 +438,7 @@ class DocumentScene extends React.Component { } }} /> - { )} - + ); } @@ -622,4 +622,20 @@ const MaxWidth = styled(Flex)` `}; `; +const FullWidthContainer = (props: React.ComponentProps) => { + const ref = React.useRef(null); + const rect = useComponentSize(ref.current); + + return ( + + ); +}; + export default withTranslation()(withStores(withRouter(DocumentScene))); diff --git a/shared/editor/components/Image.tsx b/shared/editor/components/Image.tsx index ebfc0e653..464a75019 100644 --- a/shared/editor/components/Image.tsx +++ b/shared/editor/components/Image.tsx @@ -29,10 +29,7 @@ const Image = (props: Props) => { const [loaded, setLoaded] = React.useState(false); const [naturalWidth, setNaturalWidth] = React.useState(node.attrs.width); const [naturalHeight, setNaturalHeight] = React.useState(node.attrs.height); - const containerBounds = useComponentSize( - document.body.querySelector("#full-width-container") - ); - const documentBounds = props.view.dom.getBoundingClientRect(); + const documentBounds = useComponentSize(props.view.dom); const maxWidth = documentBounds.width; const { width, height, setSize, handlePointerDown, dragging } = useDragResize( { @@ -60,21 +57,11 @@ const Image = (props: Props) => { }, [node.attrs.width]); const widthStyle = isFullWidth - ? { width: containerBounds.width } + ? { width: "var(--container-width)" } : { width: width || "auto" }; - const containerStyle = isFullWidth - ? ({ - "--offset": `${-( - documentBounds.left - - containerBounds.left + - getPadding(props.view.dom) - )}px`, - } as React.CSSProperties) - : undefined; - return ( -
+
{ ); }; -function getPadding(element: Element) { - const computedStyle = window.getComputedStyle(element, null); - return parseFloat(computedStyle.paddingLeft); -} - function getPlaceholder(width: number, height: number) { return ``; } diff --git a/shared/editor/components/Styles.ts b/shared/editor/components/Styles.ts index 8f8253163..1eda156fd 100644 --- a/shared/editor/components/Styles.ts +++ b/shared/editor/components/Styles.ts @@ -553,7 +553,7 @@ iframe.embed { max-width: 100vw; clear: both; position: initial; - ${props.rtl ? `margin-right: var(--offset)` : `margin-left: var(--offset)`}; + transform: translateX(calc(50% + var(--container-width) * -0.5)); img { max-width: 100vw;