UNPKG

@alilc/lowcode-shell

Version:

Shell Layer for AliLowCodeEngine

249 lines (231 loc) 7.04 kB
import _extends from "@babel/runtime/helpers/extends"; import _createClass from "@babel/runtime/helpers/createClass"; import { globalContext } from '@alilc/lowcode-editor-core'; import { IPublicEnumTransformStage } from '@alilc/lowcode-types'; import { DocumentModel as ShellDocumentModel } from '../model'; import { SimulatorHost } from './simulator-host'; import { editorSymbol, projectSymbol, simulatorHostSymbol, documentSymbol } from '../symbols'; import { getLogger } from '@alilc/lowcode-utils'; var logger = getLogger({ level: 'warn', bizName: 'shell-project' }); var innerProjectSymbol = Symbol('innerProject'); export var Project = /*#__PURE__*/function () { function Project(project, workspaceMode) { if (workspaceMode === void 0) { workspaceMode = false; } this.workspaceMode = workspaceMode; this[innerProjectSymbol] = void 0; this[simulatorHostSymbol] = void 0; this[innerProjectSymbol] = project; } Project.create = function create(project, workspaceMode) { if (workspaceMode === void 0) { workspaceMode = false; } return new Project(project, workspaceMode); } /** * 获取当前的 document * @returns */; var _proto = Project.prototype; /** * 打开一个 document * @param doc * @returns */ _proto.openDocument = function openDocument(doc) { var documentModel = this[projectSymbol].open(doc); if (!documentModel) { return null; } return ShellDocumentModel.create(documentModel); } /** * 创建一个 document * @param data * @returns */; _proto.createDocument = function createDocument(data) { var doc = this[projectSymbol].createDocument(data); return ShellDocumentModel.create(doc); } /** * 删除一个 document * @param doc */; _proto.removeDocument = function removeDocument(doc) { this[projectSymbol].removeDocument(doc[documentSymbol]); } /** * 根据 fileName 获取 document * @param fileName * @returns */; _proto.getDocumentByFileName = function getDocumentByFileName(fileName) { var innerDocumentModel = this[projectSymbol].getDocumentByFileName(fileName); return ShellDocumentModel.create(innerDocumentModel); } /** * 根据 id 获取 document * @param id * @returns */; _proto.getDocumentById = function getDocumentById(id) { return ShellDocumentModel.create(this[projectSymbol].getDocument(id)); } /** * 导出 project * @returns */; _proto.exportSchema = function exportSchema(stage) { if (stage === void 0) { stage = IPublicEnumTransformStage.Render; } return this[projectSymbol].getSchema(stage); } /** * 导入 project * @param schema 待导入的 project 数据 */; _proto.importSchema = function importSchema(schema) { this[projectSymbol].load(schema, true); } /** * 获取当前的 document * @returns */; _proto.getCurrentDocument = function getCurrentDocument() { return ShellDocumentModel.create(this[projectSymbol].currentDocument); } /** * 增加一个属性的管道处理函数 * @param transducer * @param stage */; _proto.addPropsTransducer = function addPropsTransducer(transducer, stage) { this[projectSymbol].designer.addPropsReducer(transducer, stage); } /** * 绑定删除文档事件 * @param fn * @returns */; _proto.onRemoveDocument = function onRemoveDocument(fn) { return this[editorSymbol].eventBus.on('designer.document.remove', function (data) { return fn(data); }); } /** * 当前 project 内的 document 变更事件 */; _proto.onChangeDocument = function onChangeDocument(fn) { var offFn = this[projectSymbol].onCurrentDocumentChange(function (originalDoc) { fn(ShellDocumentModel.create(originalDoc)); }); if (this[projectSymbol].currentDocument) { fn(ShellDocumentModel.create(this[projectSymbol].currentDocument)); } return offFn; } /** * 当前 project 的模拟器 ready 事件 */; _proto.onSimulatorHostReady = function onSimulatorHostReady(fn) { var offFn = this[projectSymbol].onSimulatorReady(function (simulator) { fn(SimulatorHost.create(simulator)); }); return offFn; } /** * 当前 project 的渲染器 ready 事件 */; _proto.onSimulatorRendererReady = function onSimulatorRendererReady(fn) { var offFn = this[projectSymbol].onRendererReady(function () { fn(); }); return offFn; } /** * 设置多语言语料 * 数据格式参考 https://github.com/alibaba/lowcode-engine/blob/main/specs/lowcode-spec.md#2434%E5%9B%BD%E9%99%85%E5%8C%96%E5%A4%9A%E8%AF%AD%E8%A8%80%E7%B1%BB%E5%9E%8Baa * @param value object * @returns */; _proto.setI18n = function setI18n(value) { this[projectSymbol].set('i18n', value); } /** * 设置项目配置 * @param value object * @returns */; _proto.setConfig = function setConfig() { if (arguments.length === 2) { var _extends2; var oldConfig = this[projectSymbol].get('config'); this[projectSymbol].set('config', _extends({}, oldConfig, (_extends2 = {}, _extends2[arguments.length <= 0 ? undefined : arguments[0]] = arguments.length <= 1 ? undefined : arguments[1], _extends2))); } else { this[projectSymbol].set('config', arguments.length <= 0 ? undefined : arguments[0]); } }; return _createClass(Project, [{ key: projectSymbol, get: function get() { if (this.workspaceMode) { return this[innerProjectSymbol]; } var workspace = globalContext.get('workspace'); if (workspace.isActive) { var _workspace$window; if (!((_workspace$window = workspace.window) !== null && _workspace$window !== void 0 && _workspace$window.innerProject)) { logger.error('project api 调用时机出现问题,请检查'); return this[innerProjectSymbol]; } return workspace.window.innerProject; } return this[innerProjectSymbol]; } }, { key: editorSymbol, get: function get() { var _this$projectSymbol; return (_this$projectSymbol = this[projectSymbol]) === null || _this$projectSymbol === void 0 ? void 0 : _this$projectSymbol.designer.editor; } }, { key: "currentDocument", get: function get() { return this.getCurrentDocument(); } /** * 获取当前 project 下所有 documents * @returns */ }, { key: "documents", get: function get() { return this[projectSymbol].documents.map(function (doc) { return ShellDocumentModel.create(doc); }); } /** * 获取模拟器的 host */ }, { key: "simulatorHost", get: function get() { return SimulatorHost.create(this[projectSymbol].simulator || this[simulatorHostSymbol]); } /** * @deprecated use .simulatorHost instead. */ }, { key: "simulator", get: function get() { return this.simulatorHost; } }]); }();