Skip to content

Commit

Permalink
fix: Image resizing
Browse files Browse the repository at this point in the history
  • Loading branch information
areknawo committed May 27, 2024
1 parent abfd753 commit 92a12ef
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions apps/web/src/lib/editor/extensions/image/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ import { dragVerticalIcon } from "#assets/icons/drag-vertical";
const ImageView: Component = () => {
const { state } = useSolidNodeView<ImageAttributes>();
const [error, setError] = createSignal(false);
const [loading, setLoading] = createSignal(false);
const [loading, setLoading] = createSignal(true);
const [objectURL, setObjectURL] = createSignal("");
const [currentSrc, setCurrentSrc] = createSignal("");
const [imageContainerRef, setImageContainerRef] = createRef<HTMLElement | null>(null);
const [referenceContainerRef, setReferenceContainerRef] = createRef<HTMLElement | null>(null);
const [menuContainerRef, setMenuContainerRef] = createRef<HTMLElement | null>(null);
const [initialResize, setInitialResize] = createRef(true);
const updateWidth = debounce((width: string) => state().updateAttributes({ width }), 250);
const updateAspectRatio = debounce(
(aspectRatio: string) => state().updateAttributes({ aspectRatio }),
Expand Down Expand Up @@ -79,8 +80,16 @@ const ImageView: Component = () => {
}
});
};
const debouncedRepositionMenu = debounce(repositionMenu, 250);
const debouncedRepositionMenu = debounce(repositionMenu, 10);
const imageResizeObserver = new ResizeObserver(([entry]) => {
if (loading()) return;

if (initialResize()) {
setInitialResize(false);

return;
}

const containerWidth = entry.target.parentElement?.clientWidth || entry.target.clientWidth;
const newWidth = `${Math.round((entry.target.clientWidth / containerWidth) * 1000) / 10}%`;

Expand Down Expand Up @@ -157,6 +166,8 @@ const ImageView: Component = () => {
if (src && src !== previousSrc) {
getImage.clear();
getImage(src);
} else {
setLoading(false);
}

return src;
Expand Down Expand Up @@ -259,7 +270,7 @@ const ImageView: Component = () => {
<div
class={clsx(
"w-full bg-gradient-to-tr flex justify-center items-center relative",
options().cover ? "min-h-48" : "rounded-2xl"
options().cover ? "" : "rounded-2xl"
)}
style={{
"padding-top": getPaddingTop()
Expand Down

0 comments on commit 92a12ef

Please sign in to comment.