From e676bff45310eaa9b076d798d93aa9c32990fea8 Mon Sep 17 00:00:00 2001 From: wanglin2 <1013335014@qq.com> Date: Tue, 18 Apr 2023 16:40:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9A=E5=BD=93=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E8=8A=82=E7=82=B9=E6=96=87=E6=9C=AC=E6=97=B6=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E5=9C=A8=E7=94=BB=E5=B8=83=E5=A4=96=E6=97=B6=E7=A7=BB?= =?UTF-8?q?=E5=85=A5=E7=94=BB=E5=B8=83=E5=86=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/src/TextEdit.js | 4 +++- simple-mind-map/src/View.js | 7 +++++++ simple-mind-map/src/utils/index.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/simple-mind-map/src/TextEdit.js b/simple-mind-map/src/TextEdit.js index f2f9330b..b371f658 100644 --- a/simple-mind-map/src/TextEdit.js +++ b/simple-mind-map/src/TextEdit.js @@ -1,4 +1,4 @@ -import { getStrWithBrFromHtml } from './utils' +import { getStrWithBrFromHtml, checkNodeOuter } from './utils' // 节点文字编辑类 export default class TextEdit { @@ -60,6 +60,8 @@ export default class TextEdit { // 显示文本编辑框 show(node) { + let { offsetLeft, offsetTop } = checkNodeOuter(this.mindMap, node) + this.mindMap.view.translateXY(offsetLeft, offsetTop) let rect = node._textData.node.node.getBoundingClientRect() if (this.mindMap.richText) { this.mindMap.richText.showEditText(node, rect) diff --git a/simple-mind-map/src/View.js b/simple-mind-map/src/View.js index 16f160bc..2898a047 100644 --- a/simple-mind-map/src/View.js +++ b/simple-mind-map/src/View.js @@ -124,6 +124,13 @@ class View { } } + // 平移x,y方向 + translateXY(x, y) { + this.x += x + this.y += y + this.transform() + } + // 平移x方向 translateX(step) { this.x += step diff --git a/simple-mind-map/src/utils/index.js b/simple-mind-map/src/utils/index.js index 3dade497..476cb974 100644 --- a/simple-mind-map/src/utils/index.js +++ b/simple-mind-map/src/utils/index.js @@ -302,4 +302,34 @@ export const nextTick = function (fn, ctx) { pending = true timerFunc(handle, 0) } +} + +// 检查节点是否超出画布 +export const checkNodeOuter = (mindMap, node) => { + let elRect = mindMap.elRect + let { scaleX, scaleY, translateX, translateY } = mindMap.draw.transform() + let { left, top, width, height } = node + let right = (left + width) * scaleX + translateX + let bottom = (top + height) * scaleY + translateY + left = left * scaleX + translateX + top = top * scaleY + translateY + let offsetLeft = 0 + let offsetTop = 0 + if (left < 0) { + offsetLeft = -left + } + if (right > elRect.width) { + offsetLeft = -(right - elRect.width) + } + if (top < 0) { + offsetTop = -top + } + if (bottom > elRect.height) { + offsetTop = -(bottom - elRect.height) + } + return { + isOuter: offsetLeft !== 0 || offsetTop !== 0, + offsetLeft, + offsetTop + } } \ No newline at end of file