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