UNPKG

@alilc/lowcode-shell

Version:

Shell Layer for AliLowCodeEngine

345 lines (323 loc) 11.3 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.DocumentModel = void 0; var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _lowcodeTypes = require("@alilc/lowcode-types"); var _lowcodeUtils = require("@alilc/lowcode-utils"); var _node = require("./node"); var _selection = require("./selection"); var _detecting = require("./detecting"); var _history = require("./history"); var _dropLocation = require("./drop-location"); var _api = require("../api"); var _prop = require("./prop"); var _modalNodesManager = require("./modal-nodes-manager"); var _symbols = require("../symbols"); function _createForOfIteratorHelperLoose(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (t) return (t = t.call(r)).next.bind(t); if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var o = 0; return function () { return o >= r.length ? { done: !0 } : { done: !1, value: r[o++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } var shellDocSymbol = Symbol('shellDocSymbol'); var DocumentModel = exports.DocumentModel = /*#__PURE__*/function () { function DocumentModel(document) { var _document$designer; this[_symbols.documentSymbol] = void 0; this[_symbols.editorSymbol] = void 0; this._focusNode = void 0; this.selection = void 0; this.detecting = void 0; this.history = void 0; /** * @deprecated use canvas API instead */ this.canvas = void 0; this[_symbols.documentSymbol] = document; this[_symbols.editorSymbol] = (_document$designer = document.designer) === null || _document$designer === void 0 ? void 0 : _document$designer.editor; this.selection = new _selection.Selection(document); this.detecting = new _detecting.Detecting(document); this.history = new _history.History(document); this.canvas = new _api.Canvas(this[_symbols.editorSymbol]); this._focusNode = _node.Node.create(this[_symbols.documentSymbol].focusNode); } DocumentModel.create = function create(document) { if (!document) { return null; } // @ts-ignore 直接返回已挂载的 shell doc 实例 if (document[shellDocSymbol]) { return document[shellDocSymbol]; } var shellDoc = new DocumentModel(document); // @ts-ignore 直接返回已挂载的 shell doc 实例 document[shellDocSymbol] = shellDoc; return shellDoc; } /** * id */; var _proto = DocumentModel.prototype; /** * 根据 nodeId 返回 Node 实例 * get node instance by nodeId * @param {string} nodeId */ _proto.getNodeById = function getNodeById(nodeId) { return _node.Node.create(this[_symbols.documentSymbol].getNode(nodeId)); } /** * 导入 schema * @param schema */; _proto.importSchema = function importSchema(schema) { this[_symbols.documentSymbol]["import"](schema); this[_symbols.editorSymbol].eventBus.emit('shell.document.importSchema', schema); } /** * 导出 schema * @param stage * @returns */; _proto.exportSchema = function exportSchema(stage) { if (stage === void 0) { stage = _lowcodeTypes.IPublicEnumTransformStage.Render; } return this[_symbols.documentSymbol]["export"](stage); } /** * 插入节点 * @param parent * @param thing * @param at * @param copy * @returns */; _proto.insertNode = function insertNode(parent, thing, at, copy) { var node = this[_symbols.documentSymbol].insertNode(parent[_symbols.nodeSymbol] ? parent[_symbols.nodeSymbol] : parent, thing !== null && thing !== void 0 && thing[_symbols.nodeSymbol] ? thing[_symbols.nodeSymbol] : thing, at, copy); return _node.Node.create(node); } /** * 创建一个节点 * @param data * @returns */; _proto.createNode = function createNode(data) { return _node.Node.create(this[_symbols.documentSymbol].createNode(data)); } /** * 移除指定节点/节点id * @param idOrNode */; _proto.removeNode = function removeNode(idOrNode) { this[_symbols.documentSymbol].removeNode(idOrNode); } /** * componentsMap of documentModel * @param extraComps * @returns */; _proto.getComponentsMap = function getComponentsMap(extraComps) { return this[_symbols.documentSymbol].getComponentsMap(extraComps); } /** * 检查拖拽放置的目标节点是否可以放置该拖拽对象 * @param dropTarget 拖拽放置的目标节点 * @param dragObject 拖拽的对象 * @returns boolean 是否可以放置 */; _proto.checkNesting = function checkNesting(dropTarget, dragObject) { var innerDragObject = dragObject; if ((0, _lowcodeUtils.isDragNodeObject)(dragObject)) { var _innerDragObject$node; innerDragObject.nodes = (_innerDragObject$node = innerDragObject.nodes) === null || _innerDragObject$node === void 0 ? void 0 : _innerDragObject$node.map(function (node) { return node[_symbols.nodeSymbol] || node; }); } return this[_symbols.documentSymbol].checkNesting(dropTarget[_symbols.nodeSymbol] || dropTarget, innerDragObject); } /** * 当前 document 新增节点事件 */; _proto.onAddNode = function onAddNode(fn) { return this[_symbols.documentSymbol].onNodeCreate(function (node) { fn(_node.Node.create(node)); }); } /** * 当前 document 新增节点事件,此时节点已经挂载到 document 上 */; _proto.onMountNode = function onMountNode(fn) { return this[_symbols.documentSymbol].onMountNode(function (_ref) { var node = _ref.node; fn({ node: _node.Node.create(node) }); }); } /** * 当前 document 删除节点事件 */; _proto.onRemoveNode = function onRemoveNode(fn) { return this[_symbols.documentSymbol].onNodeDestroy(function (node) { fn(_node.Node.create(node)); }); } /** * 当前 document 的 hover 变更事件 */; _proto.onChangeDetecting = function onChangeDetecting(fn) { return this[_symbols.documentSymbol].designer.detecting.onDetectingChange(function (node) { fn(_node.Node.create(node)); }); } /** * 当前 document 的选中变更事件 */; _proto.onChangeSelection = function onChangeSelection(fn) { return this[_symbols.documentSymbol].selection.onSelectionChange(function (ids) { fn(ids); }); } /** * 当前 document 的节点显隐状态变更事件 * @param fn */; _proto.onChangeNodeVisible = function onChangeNodeVisible(fn) { return this[_symbols.documentSymbol].onChangeNodeVisible(function (node, visible) { fn(_node.Node.create(node), visible); }); } /** * 当前 document 的节点 children 变更事件 * @param fn */; _proto.onChangeNodeChildren = function onChangeNodeChildren(fn) { return this[_symbols.documentSymbol].onChangeNodeChildren(function (info) { if (!info) { return; } fn({ type: info.type, node: _node.Node.create(info.node) }); }); } /** * 当前 document 节点属性修改事件 * @param fn */; _proto.onChangeNodeProp = function onChangeNodeProp(fn) { var _this = this; var callback = function callback(info) { fn({ key: info.key, oldValue: info.oldValue, newValue: info.newValue, prop: _prop.Prop.create(info.prop), node: _node.Node.create(info.node) }); }; this[_symbols.editorSymbol].on(_lowcodeTypes.GlobalEvent.Node.Prop.InnerChange, callback); return function () { _this[_symbols.editorSymbol].off(_lowcodeTypes.GlobalEvent.Node.Prop.InnerChange, callback); }; } /** * import schema event * @param fn */; _proto.onImportSchema = function onImportSchema(fn) { return this[_symbols.editorSymbol].eventBus.on('shell.document.importSchema', fn); }; _proto.isDetectingNode = function isDetectingNode(node) { return this.detecting.current === node; }; _proto.onFocusNodeChanged = function onFocusNodeChanged(fn) { if (!fn) { return function () {}; } return this[_symbols.editorSymbol].eventBus.on('shell.document.focusNodeChanged', function (payload) { var document = payload.document, focusNode = payload.focusNode; fn(document, focusNode); }); }; _proto.onDropLocationChanged = function onDropLocationChanged(fn) { if (!fn) { return function () {}; } return this[_symbols.editorSymbol].eventBus.on('document.dropLocation.changed', function (payload) { var document = payload.document; fn(document); }); }; return (0, _createClass2["default"])(DocumentModel, [{ key: "id", get: function get() { return this[_symbols.documentSymbol].id; }, set: function set(id) { this[_symbols.documentSymbol].id = id; } /** * 获取当前文档所属的 project * @returns */ }, { key: "project", get: function get() { return _api.Project.create(this[_symbols.documentSymbol].project, true); } /** * 获取文档的根节点 * root node of this documentModel * @returns */ }, { key: "root", get: function get() { return _node.Node.create(this[_symbols.documentSymbol].rootNode); } }, { key: "focusNode", get: function get() { return this._focusNode || this.root; }, set: function set(node) { this._focusNode = node; this[_symbols.editorSymbol].eventBus.emit('shell.document.focusNodeChanged', { document: this, focusNode: node }); } /** * 获取文档下所有节点 Map, key 为 nodeId * get map of all nodes , using node.id as key */ }, { key: "nodesMap", get: function get() { var map = new Map(); for (var _iterator = _createForOfIteratorHelperLoose(this[_symbols.documentSymbol].nodesMap.keys()), _step; !(_step = _iterator()).done;) { var id = _step.value; map.set(id, this.getNodeById(id)); } return map; } /** * 模态节点管理 */ }, { key: "modalNodesManager", get: function get() { return _modalNodesManager.ModalNodesManager.create(this[_symbols.documentSymbol].modalNodesManager); } }, { key: "dropLocation", get: function get() { return _dropLocation.DropLocation.create(this[_symbols.documentSymbol].dropLocation); }, set: function set(loc) { this[_symbols.documentSymbol].dropLocation = loc; } }]); }();