From 23d0fd0ad658f5eb6e5d3ac45124deab4a5ddc1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E4=BA=91=E9=A3=9E?= Date: Sat, 20 Nov 2021 18:21:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=9B=BE=E7=89=87=E5=B0=BA=E5=AF=B8?= =?UTF-8?q?=E8=87=AA=E9=80=82=E5=BA=94=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/editor/core/command/CommandAdapt.ts | 2 +- src/editor/core/draw/Draw.ts | 10 +++++++++- src/editor/core/draw/particle/ImageParticle.ts | 6 ++++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/editor/core/command/CommandAdapt.ts b/src/editor/core/command/CommandAdapt.ts index fae8e5c..a96969c 100644 --- a/src/editor/core/command/CommandAdapt.ts +++ b/src/editor/core/command/CommandAdapt.ts @@ -226,8 +226,8 @@ export class CommandAdapt { elementList.splice(curIndex, 0, element) } else { elementList.splice(curIndex, endIndex - startIndex, element) - this.range.setRange(curIndex, curIndex) } + this.range.setRange(curIndex, curIndex) this.draw.render({ curIndex }) } diff --git a/src/editor/core/draw/Draw.ts b/src/editor/core/draw/Draw.ts index 0172345..9d92e96 100644 --- a/src/editor/core/draw/Draw.ts +++ b/src/editor/core/draw/Draw.ts @@ -189,7 +189,15 @@ export class Draw { boundingBoxAscent: 0, boundingBoxDescent: 0 } + const innerWidth = rightTopPoint[0] - leftTopPoint[0] if (element.type === ElementType.IMAGE) { + // 图片超出尺寸后自适应 + if (curRow.width + element.width! > innerWidth) { + // 计算剩余大小 + const surplusWidth = innerWidth - curRow.width + element.width = surplusWidth + element.height = element.height! * surplusWidth / element.width + } metrics.width = element.width! metrics.boundingBoxAscent = 0 metrics.boundingBoxDescent = element.height! @@ -205,7 +213,7 @@ export class Draw { const height = ascent + descent const rowElement: IRowElement = { ...element, metrics } // 超过限定宽度 - if (curRow.width + metrics.width > rightTopPoint[0] - leftTopPoint[0] || (i !== 0 && element.value === ZERO)) { + if (curRow.width + metrics.width > innerWidth || (i !== 0 && element.value === ZERO)) { rowList.push({ width: metrics.width, height: this.options.defaultSize, diff --git a/src/editor/core/draw/particle/ImageParticle.ts b/src/editor/core/draw/particle/ImageParticle.ts index 1f38460..42b2eab 100644 --- a/src/editor/core/draw/particle/ImageParticle.ts +++ b/src/editor/core/draw/particle/ImageParticle.ts @@ -15,14 +15,16 @@ export class ImageParticle { } render(element: IElement, x: number, y: number) { + const width = element.width! + const height = element.height! if (this.imageCache.has(element.id!)) { const img = this.imageCache.get(element.id!)! - this.ctx.drawImage(img, x, y, element.width!, element.height!) + this.ctx.drawImage(img, x, y, width, height) } else { const img = new Image() img.src = element.value img.onload = () => { - this.ctx.drawImage(img, x, y, img.width, img.height) + this.ctx.drawImage(img, x, y, width, height) this.imageCache.set(element.id!, img) } }