From 5daa95644c69220c7ced9c4806e5794a5496db9b Mon Sep 17 00:00:00 2001 From: irinatelnova Date: Tue, 11 May 2021 12:06:27 +0300 Subject: [PATCH] #339 Fix multiline text render --- .../src/script/render/restruct/ReText.ts | 45 +++++++++---------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/packages/ketcher-react/src/script/render/restruct/ReText.ts b/packages/ketcher-react/src/script/render/restruct/ReText.ts index 494e7d2199..86dd9014f8 100644 --- a/packages/ketcher-react/src/script/render/restruct/ReText.ts +++ b/packages/ketcher-react/src/script/render/restruct/ReText.ts @@ -39,7 +39,7 @@ class ReText extends ReObject { } getReferencePoints(): Array { - const { p0, p1 } = this.getRelBox() + const { p0, p1 } = this.getRelBox(this.paths) const p = this.item.position const w = Math.abs(Vec2.diff(p0, p1).x) / 40 @@ -58,44 +58,39 @@ class ReText extends ReObject { } highlightPath(render: any): any { - const { p0, p1 } = this.getRelBox() + const { p0, p1 } = this.getRelBox(this.paths) const topLeft = p0.sub(render.options.offset) const { x: width, y: height } = p1.sub(p0) return render.paper.rect(topLeft.x, topLeft.y, width, height, 5) } - getRelBox(): { p0: Vec2; p1: Vec2 } { - const lastRow: Array = this.paths[this.paths.length - 1] - const widestRow: Array = this.paths.reduce( + getRelBox(paths: Array>): { p0: Vec2; p1: Vec2 } { + const firstElOfFirstRow: any = paths[0][0] + const leftEdge = firstElOfFirstRow.getBBox().x + + const firstRow: Array = paths[0] + const topEdge: number = Math.min(...firstRow.map(path => path.getBBox().y)) + + const widestRow: Array = paths.reduce( (widestRow, nextRow) => this.getRowWidth(nextRow) > this.getRowWidth(widestRow) ? nextRow : widestRow, - this.paths[0] + paths[0] ) - - const firstElOfFirstRow: any = this.paths[0][0] const lastElOfWidestRow: any = widestRow[widestRow.length - 1] - - const rightEdgeOfWidestRow: number = + const rightEdge: number = lastElOfWidestRow.getBBox().x + lastElOfWidestRow.getBBox().width - const bottomEdgeOfLastRow: number = lastRow.reduce((acc, nextEl) => { - const bottomEdgeOfEl = nextEl.getBBox().y + nextEl.getBBox().height - if (bottomEdgeOfEl > acc) { - acc = bottomEdgeOfEl - } - - return acc - }, 0) + const lastRow: Array = paths[paths.length - 1] + const bottomEdge: number = Math.max( + ...lastRow.map(path => path.getBBox().y + path.getBBox().height) + ) return { - p0: new Vec2( - firstElOfFirstRow.getBBox().x, - firstElOfFirstRow.getBBox().y - ), - p1: new Vec2(rightEdgeOfWidestRow, bottomEdgeOfLastRow) + p0: new Vec2(leftEdge, topEdge), + p1: new Vec2(rightEdge, bottomEdge) } } @@ -156,7 +151,9 @@ class ReText extends ReObject { }) this.paths.push(row) - shiftY += row[0].getBBox().height + + const { p0, p1 } = this.getRelBox([row]) + shiftY += Math.abs(Vec2.diff(p0, p1).y) }) render.ctab.addReObjectPath(