diff --git a/app/components/MeasuredContainer.tsx b/app/components/MeasuredContainer.tsx deleted file mode 100644 index 241059f590..0000000000 --- a/app/components/MeasuredContainer.tsx +++ /dev/null @@ -1,28 +0,0 @@ -import * as React from "react"; -import useMeasure from "react-use-measure"; - -export const MeasuredContainer = ({ - as: As = "div", - name, - children, - ...rest -}: { - as: T; - name: string; - children?: React.ReactNode; -} & React.ComponentProps) => { - const [measureRef, rect] = useMeasure(); - - return ( - - {children} - - ); -}; diff --git a/app/editor/components/MediaDimension.tsx b/app/editor/components/MediaDimension.tsx index cc02eb2222..e216a723dd 100644 --- a/app/editor/components/MediaDimension.tsx +++ b/app/editor/components/MediaDimension.tsx @@ -3,8 +3,8 @@ import { useCallback, useEffect, useRef, useState } from "react"; import styled from "styled-components"; import Flex from "@shared/components/Flex"; import Text from "@shared/components/Text"; +import { contentWidth } from "@shared/editor/lib/contentWidth"; import { resolvePDFDimensions } from "@shared/editor/lib/pdf"; -import { EditorStyleHelper } from "@shared/editor/styles/EditorStyleHelper"; import { extraArea } from "@shared/styles"; import Input, { NativeInput, Outline } from "~/components/Input"; import { useEditor } from "./EditorContext"; @@ -17,7 +17,6 @@ type Dimension = { }; export function MediaDimension() { - const ref = useRef(null); const isDraggingRef = useRef(false); const boundsRef = useRef< | { @@ -51,11 +50,8 @@ export function MediaDimension() { height: false, }); - if (!boundsRef.current && ref.current) { - const docWidth = parseInt( - getComputedStyle(ref.current).getPropertyValue("--document-width") - ); - const maxWidth = docWidth - EditorStyleHelper.padding * 2; + if (!boundsRef.current) { + const maxWidth = contentWidth(view.dom); const constrainedWidth = Math.min(width, maxWidth); // Ensure media width does not exceed the max width of the editor. const aspectRatio = height / constrainedWidth; @@ -277,7 +273,7 @@ export function MediaDimension() { }, [width, height, localDimension, isOutsideBounds]); return ( - + } {(can.update || readOnly) && ( Loading…}> - +
- +
)} diff --git a/app/scenes/Document/components/Document.tsx b/app/scenes/Document/components/Document.tsx index 5b7df6c65b..6fac545814 100644 --- a/app/scenes/Document/components/Document.tsx +++ b/app/scenes/Document/components/Document.tsx @@ -24,7 +24,6 @@ import LoadingIndicator from "~/components/LoadingIndicator"; import PageTitle from "~/components/PageTitle"; import PlaceholderDocument from "~/components/PlaceholderDocument"; import RegisterKeyDown from "~/components/RegisterKeyDown"; -import { MeasuredContainer } from "~/components/MeasuredContainer"; import type { Editor as TEditor } from "~/editor"; import type { Properties } from "~/types"; import useEventListener from "~/hooks/useEventListener"; @@ -395,9 +394,7 @@ function DocumentScene({ }} handler={onPublish} /> - } > - )} - + {showContents && ( {children} - + ); } @@ -622,6 +617,10 @@ const EditorContainer = styled.div` const Background = styled(Container)` position: relative; background: ${s("background")}; + + // A container unit, unlike a measured value, does not restyle the document on resize. + container-type: inline-size; + --container-width: 100cqi; `; const ReferencesWrapper = styled.div` diff --git a/shared/editor/components/hooks/useDragResize.ts b/shared/editor/components/hooks/useDragResize.ts index 9c897e07db..3b617c405c 100644 --- a/shared/editor/components/hooks/useDragResize.ts +++ b/shared/editor/components/hooks/useDragResize.ts @@ -1,4 +1,5 @@ import * as React from "react"; +import { contentWidth } from "../../lib/contentWidth"; import { EditorStyleHelper } from "../../styles/EditorStyleHelper"; type DragDirection = @@ -315,12 +316,7 @@ export default function useDragResize(props: Params): ReturnValue { event.stopPropagation(); // Calculate constraints once at the start of dragging as it's relatively expensive operation - const max = ref.current - ? parseInt( - getComputedStyle(ref.current).getPropertyValue("--document-width") - ) - - EditorStyleHelper.padding * 2 - : Infinity; + const max = ref.current ? contentWidth(ref.current) : Infinity; setMaxWidth(max); setSizeAtDragStart({ // When no width has been set yet the element is displayed at full width, diff --git a/shared/editor/lib/contentWidth.ts b/shared/editor/lib/contentWidth.ts new file mode 100644 index 0000000000..fe29c0122b --- /dev/null +++ b/shared/editor/lib/contentWidth.ts @@ -0,0 +1,20 @@ +/** + * Returns the width that blocks can fill in the editor that contains the + * element, which is the editor's width without its own padding. + * + * @param element an element in the editor, or the editor element itself. + * @returns the width in pixels, or Infinity when the element is not in an editor. + */ +export function contentWidth(element: Element): number { + const editor = element.closest(".ProseMirror"); + if (!editor) { + return Infinity; + } + + const style = getComputedStyle(editor); + return ( + editor.clientWidth - + parseFloat(style.paddingLeft) - + parseFloat(style.paddingRight) + ); +}