From 4fab6976d11eab71a9ec04cfadf8661b80fa0fda Mon Sep 17 00:00:00 2001 From: Pillar Cat <149342718+pillar-cat@users.noreply.github.com> Date: Wed, 6 Dec 2023 14:42:11 +0800 Subject: [PATCH] fix: editor's image cannot be resized proportionally (#4993) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind bug /area editor #### What this PR does / why we need it: 修复#4985 中调整图片大小时的高度变化bug #### Which issue(s) this PR fixes: Fixes #4985 #### Special notes for your reviewer: 似乎是因为横纵比aspectRatio在调整大小的doDrag函数中被动态的更新数值,导致依赖aspectRatio来计算的高度也会出现问题。直接把aspectRatio移动到函数外,只在挂载后更新一次值即可解决。 #### Does this PR introduce a user-facing change? ```release-note 这会修复当前console的editor中图片比例拉伸的bug ``` --- .../editor/src/extensions/image/ImageView.vue | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/console/packages/editor/src/extensions/image/ImageView.vue b/console/packages/editor/src/extensions/image/ImageView.vue index dc3e92077..837f5e748 100644 --- a/console/packages/editor/src/extensions/image/ImageView.vue +++ b/console/packages/editor/src/extensions/image/ImageView.vue @@ -50,9 +50,17 @@ function handleSetFocus() { props.editor.commands.setNodeSelection(props.getPos()); } +const aspectRatio = ref(0); const inputRef = ref(); const resizeRef = ref(); +function onImageLoaded() { + if (!resizeRef.value) return; + + aspectRatio.value = + resizeRef.value.clientWidth / resizeRef.value.clientHeight; +} + onMounted(() => { if (!src.value) { inputRef.value.focus(); @@ -73,20 +81,17 @@ onMounted(() => { function doDrag(e: MouseEvent) { if (!resizeRef.value) return; - const aspectRatio = - resizeRef.value.clientWidth / resizeRef.value.clientHeight; - const newWidth = Math.min( startWidth + e.clientX - startX, resizeRef.value.parentElement?.clientWidth || 0 ); const width = newWidth.toFixed(0) + "px"; - const height = (newWidth / aspectRatio).toFixed(0) + "px"; + const height = (newWidth / aspectRatio.value).toFixed(0) + "px"; props.editor .chain() .updateAttributes(Image.name, { width, height }) - .setNodeSelection(props.editor.state.selection.from) + .setNodeSelection(props.getPos()) .focus() .run(); } @@ -128,6 +133,7 @@ onMounted(() => { :alt="alt" :href="href" class="w-full h-full" + @load="onImageLoaded" />