diff --git a/simple-mind-map/index.js b/simple-mind-map/index.js index 761b4909..39003ccc 100644 --- a/simple-mind-map/index.js +++ b/simple-mind-map/index.js @@ -82,6 +82,22 @@ class MindMap { // 该检查可以通过customCheckEnableShortcut选项来覆盖 this.editNodeClassList = [] + // 扩展的节点形状列表 + /* + { + createShape: (node) => { + return path + }, + getPadding: ({ node, width, height, paddingX, paddingY }) => { + return { + paddingX: 0, + paddingY: 0 + } + } + } + */ + this.extendShapeList = [] + // 画布 this.initContainer() @@ -675,6 +691,26 @@ class MindMap { } } + // 扩展节点形状 + addShape(shape) { + if (!shape) return + const exist = this.extendShapeList.find(item => { + return item.name === shape.name + }) + if (exist) return + this.extendShapeList.push(shape) + } + + // 删除扩展的形状 + removeShape(name) { + const index = this.extendShapeList.findIndex(item => { + return item.name === name + }) + if (index !== -1) { + this.extendShapeList.splice(index, 1) + } + } + // 添加插件 addPlugin(plugin, opt) { let index = MindMap.hasPlugin(plugin) diff --git a/simple-mind-map/src/core/render/node/Shape.js b/simple-mind-map/src/core/render/node/Shape.js index 85e52517..704f1129 100644 --- a/simple-mind-map/src/core/render/node/Shape.js +++ b/simple-mind-map/src/core/render/node/Shape.js @@ -52,14 +52,36 @@ export default class Shape { paddingX: actHeight > actWidth ? actOffset / 2 : 0, paddingY: actHeight < actWidth ? actOffset / 2 : 0 } - default: - return { + } + const extendShape = this.getShapeFromExtendList(shape) + if (extendShape) { + return ( + extendShape.getPadding({ + node: this.node, + width, + height, + paddingX, + paddingY + }) || { paddingX: 0, paddingY: 0 } + ) + } else { + return { + paddingX: 0, + paddingY: 0 + } } } + // 从形状扩展列表里获取指定名称的形状 + getShapeFromExtendList(shape) { + return this.mindMap.extendShapeList.find(item => { + return item.name === shape + }) + } + // 创建形状节点 createShape() { const shape = this.node.getShape() @@ -92,6 +114,12 @@ export default class Shape { // 圆 node = this.createCircle() } + if (!node) { + const extendShape = this.getShapeFromExtendList(shape) + if (extendShape) { + node = extendShape.createShape(this.node) + } + } return node }