mirror of
https://github.com/outline/outline.git
synced 2026-11-22 05:51:43 +03:00
perf: Stop restyling the document on every frame of the sidebar animation (#14132)
The document container and editor exposed their measured widths as inherited CSS variables, so each frame of a width animation restyled every element in the document. Full width blocks now use a container unit, and media resizing measures the editor content width when it is needed.
This commit is contained in:
1 parent
ef7753dc57
commit
e14dfcfcb4
6 files changed
+36
-54
No files matched your search
@@ -1,28 +0,0 @@
|
||||
import * as React from "react";
|
||||
import useMeasure from "react-use-measure";
|
||||
|
||||
export const MeasuredContainer = <T extends React.ElementType>({
|
||||
as: As = "div",
|
||||
name,
|
||||
children,
|
||||
...rest
|
||||
}: {
|
||||
as: T;
|
||||
name: string;
|
||||
children?: React.ReactNode;
|
||||
} & React.ComponentProps<T>) => {
|
||||
const [measureRef, rect] = useMeasure();
|
||||
|
||||
return (
|
||||
<As
|
||||
{...rest}
|
||||
ref={measureRef}
|
||||
style={{
|
||||
[`--${name}-width`]: `${rect.width}px`,
|
||||
[`--${name}-height`]: `${rect.height}px`,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</As>
|
||||
);
|
||||
};
|
||||
@@ -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<HTMLDivElement>(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 (
|
||||
<StyledFlex ref={ref} align="center">
|
||||
<StyledFlex align="center">
|
||||
<StyledInput
|
||||
label={t("Image width")}
|
||||
labelHidden
|
||||
|
||||
@@ -13,7 +13,6 @@ import type Document from "~/models/Document";
|
||||
import Editor from "~/components/Editor";
|
||||
import LoadingIndicator from "~/components/LoadingIndicator";
|
||||
import Text from "~/components/Text";
|
||||
import { MeasuredContainer } from "~/components/MeasuredContainer";
|
||||
import { withUIExtensions } from "~/editor/extensions";
|
||||
import useCurrentUser from "~/hooks/useCurrentUser";
|
||||
import usePolicy from "~/hooks/usePolicy";
|
||||
@@ -90,7 +89,7 @@ function Overview({ collection, readOnly }: Props) {
|
||||
{collections.isSaving && <LoadingIndicator />}
|
||||
{(can.update || readOnly) && (
|
||||
<Suspense fallback={<Placeholder>Loading…</Placeholder>}>
|
||||
<MeasuredContainer name="document">
|
||||
<div>
|
||||
<Editor
|
||||
defaultValue={collection.data}
|
||||
onChange={handleSave}
|
||||
@@ -104,7 +103,7 @@ function Overview({ collection, readOnly }: Props) {
|
||||
editorStyle={editorStyle}
|
||||
/>
|
||||
<div ref={childRef} />
|
||||
</MeasuredContainer>
|
||||
</div>
|
||||
</Suspense>
|
||||
)}
|
||||
</>
|
||||
|
||||
@@ -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}
|
||||
/>
|
||||
<MeasuredContainer
|
||||
as={Background}
|
||||
name="container"
|
||||
<Background
|
||||
column
|
||||
auto
|
||||
onDragOver={handleDragOver}
|
||||
@@ -447,9 +444,7 @@ function DocumentScene({
|
||||
</EditorContainer>
|
||||
}
|
||||
>
|
||||
<MeasuredContainer
|
||||
name="document"
|
||||
as={EditorContainer}
|
||||
<EditorContainer
|
||||
docFullWidth={document.fullWidth}
|
||||
showContents={showContents}
|
||||
tocPosition={tocPos}
|
||||
@@ -500,7 +495,7 @@ function DocumentScene({
|
||||
</Editor>
|
||||
</>
|
||||
)}
|
||||
</MeasuredContainer>
|
||||
</EditorContainer>
|
||||
{showContents && (
|
||||
<ContentsContainer
|
||||
docFullWidth={document.fullWidth}
|
||||
@@ -513,7 +508,7 @@ function DocumentScene({
|
||||
</Main>
|
||||
{children}
|
||||
</Container>
|
||||
</MeasuredContainer>
|
||||
</Background>
|
||||
</ErrorBoundary>
|
||||
);
|
||||
}
|
||||
@@ -622,6 +617,10 @@ const EditorContainer = styled.div<EditorContainerProps>`
|
||||
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`
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user