Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

editor: make image size clamping responsive

Abdullah Atta42571b079425
diff --git a/packages/editor/src/extensions/image/component.tsx b/packages/editor/src/extensions/image/component.tsx
index 5371c597b..cd0376fad 100644
--- a/packages/editor/src/extensions/image/component.tsx
+++ b/packages/editor/src/extensions/image/component.tsx
@@ -20,7 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { ThemeUIStyleObject } from "@theme-ui/core";
import { Box, Flex, Image, Text } from "@theme-ui/components";
import { ImageAttributes } from "./image";
-import { useEffect, useState } from "react";
+import { useEffect, useRef, useState } from "react";
import { SelectionBasedReactNodeViewProps } from "../react";
import { DesktopOnly } from "../../components/responsive";
import { Icon } from "@notesnook/ui";
@@ -42,6 +42,7 @@ import { motion } from "framer-motion";
import { useObserver } from "../../hooks/use-observer";
import { Attachment, ImageAlignmentOptions } from "../attachment";
import DataURL from "@notesnook/core/dist/utils/dataurl";
+import { debounce } from "../../utils/debounce";
export const AnimatedImage = motion(Image);
@@ -60,6 +61,11 @@ export function ImageComponent(
threshold: 0.2,
once: true
});
+ const sizeRef = useRef(
+ clampSize(node.attrs, editor.view.dom.clientWidth, aspectRatio)
+ );
+ const editorObserverRef = useRef<ResizeObserver>();
+
const float = isMobile ? false : node.attrs.float;
let align = node.attrs.align;
@@ -69,11 +75,23 @@ export function ImageComponent(
const isReadonly = !editor.isEditable;
const isSVG = !!mime && mime.includes("/svg");
- const { height, width } = clampSize(
- node.attrs,
- editor.view.dom.clientWidth,
- aspectRatio
- );
+ useEffect(() => {
+ function onResize(
+ _entries: ResizeObserverEntry[],
+ _observer: ResizeObserver
+ ) {
+ sizeRef.current = clampSize(
+ node.attrs,
+ editor.view.dom.clientWidth,
+ aspectRatio
+ );
+ }
+ editorObserverRef.current = new ResizeObserver(debounce(onResize, 1000));
+ editorObserverRef.current?.observe(editor.view.dom);
+ return () => {
+ editorObserverRef.current?.disconnect();
+ };
+ });
useEffect(() => {
if (!inView) return;
@@ -100,7 +118,7 @@ export function ImageComponent(
<Box
sx={{
...getAlignmentStyles(node.attrs),
- height: float ? height : "unset",
+ height: float ? sizeRef.current.height : "unset",
position: "relative",
mt: isSVG ? `24px` : 0,
":hover .drag-handle, :active .drag-handle": {
@@ -112,8 +130,8 @@ export function ImageComponent(
style={{ marginTop: 5 }}
enabled={editor.isEditable && !float}
selected={selected}
- width={width}
- height={height}
+ width={sizeRef.current.width}
+ height={sizeRef.current.height}
onResize={(width, height) => {
editor.commands.setImageSize({ width, height });
}}
@@ -221,8 +239,10 @@ export function ImageComponent(
position: "absolute",
top: 0,
left: 0,
- width: editor.isEditable && !float ? "100%" : width,
- height: editor.isEditable && !float ? "100%" : height,
+ width:
+ editor.isEditable && !float ? "100%" : sizeRef.current.width,
+ height:
+ editor.isEditable && !float ? "100%" : sizeRef.current.height,
bg: "background-secondary",
border: selected
? "2px solid var(--accent)"
@@ -237,7 +257,7 @@ export function ImageComponent(
>
<Icon
path={Icons.image}
- size={width ? width * 0.2 : 72}
+ size={sizeRef.current.width ? sizeRef.current.width * 0.2 : 72}
color="gray"
/>
</Flex>
@@ -262,8 +282,8 @@ export function ImageComponent(
title={title}
sx={{
objectFit: "contain",
- width: editor.isEditable ? "100%" : width,
- height: editor.isEditable ? "100%" : height,
+ width: editor.isEditable ? "100%" : sizeRef.current.width,
+ height: editor.isEditable ? "100%" : sizeRef.current.height,
border: selected
? "2px solid var(--accent) !important"
: "2px solid transparent !important",
@@ -288,7 +308,10 @@ export function ImageComponent(
const orignalWidth = naturalWidth || clientWidth;
const orignalHeight = naturalHeight || clientHeight;
const naturalAspectRatio = orignalWidth / orignalHeight;
- const fixedDimensions = fixAspectRatio(width, naturalAspectRatio);
+ const fixedDimensions = fixAspectRatio(
+ sizeRef.current.width,
+ naturalAspectRatio
+ );
if (src && !DataURL.isValid(src) && canParse(src)) {
const image = await downloadImage(src, downloadOptions);
@@ -318,7 +341,7 @@ export function ImageComponent(
{ query: makeImageQuery(src, hash), ignoreEdit: true }
)
);
- } else if (height !== fixedDimensions.height) {
+ } else if (sizeRef.current.height !== fixedDimensions.height) {
await editor.threadsafe((editor) =>
editor.commands.updateAttachment(fixedDimensions, {
query: makeImageQuery(src, hash),