@turbox3d/design-engine
Version:
Large-scale design application engine library
294 lines • 9.69 kB
JavaScript
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _typeof from "@babel/runtime/helpers/esm/typeof";
import _regeneratorRuntime from "@babel/runtime/regenerator";
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
var __decorate = this && this.__decorate || function (decorators, target, key, desc) {
var c = arguments.length,
r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc,
d;
if ((typeof Reflect === "undefined" ? "undefined" : _typeof(Reflect)) === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { config, Domain, mutation, reactor, TimeTravel } from '@turbox3d/reactivity';
import { warn } from '@turbox3d/shared';
var DocumentSystem = /*#__PURE__*/function (_Domain) {
function DocumentSystem() {
var _this;
_classCallCheck(this, DocumentSystem);
_this = _callSuper(this, DocumentSystem, arguments);
_this.models = new Map();
_this.loading = false;
_this.undoable = false;
_this.redoable = false;
_this.disableUndoRedo = false;
_this.maxStepNumber = 20;
_this.name = 'document';
return _this;
}
_inherits(DocumentSystem, _Domain);
return _createClass(DocumentSystem, [{
key: "initDomainContext",
value: function initDomainContext() {
return {
isNeedRecord: true
};
}
/** 添加模型 */
}, {
key: "addModel",
value: function addModel(model) {
var _this2 = this;
var add = function add(m) {
if (_this2.models.has(m.id)) {
return;
}
_this2.models.set(m.id, m);
};
if (Array.isArray(model)) {
model.forEach(function (m) {
return add(m);
});
return;
}
add(model);
}
/** 删除模型 */
}, {
key: "removeModel",
value: function removeModel(model) {
var _this3 = this;
if (Array.isArray(model)) {
model.forEach(function (m) {
return _this3.removeModelById(m.id);
});
return;
}
this.removeModelById(model.id);
}
/** 根据 id 删除模型 */
}, {
key: "removeModelById",
value: function removeModelById(id) {
var _this4 = this;
if (Array.isArray(id)) {
id.forEach(function (i) {
return _this4.models["delete"](i);
});
return;
}
this.models["delete"](id);
}
/** 清空所有模型 */
}, {
key: "clear",
value: function clear() {
this.models.clear();
}
/** 查找模型 */
}, {
key: "findModel",
value: function findModel(model) {
var _this5 = this;
if (Array.isArray(model)) {
return model.map(function (m) {
return _this5.findModelById(m.id);
});
}
return this.findModelById(model.id);
}
/** 根据 id 查找模型 */
}, {
key: "findModelById",
value: function findModelById(id) {
var _this6 = this;
if (Array.isArray(id)) {
return id.map(function (i) {
return _this6.models.get(i);
});
}
return this.models.get(id);
}
/** 创建操作历史记录 */
}, {
key: "createTimeTravel",
value: function createTimeTravel(name) {
var _this7 = this;
var maxStepNumber = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 20;
if (TimeTravel.get(name)) {
warn("Time travel ".concat(name, " already exists."));
return;
}
this.name = name;
this.maxStepNumber = maxStepNumber;
config({
timeTravel: {
isActive: true,
maxStepNumber: maxStepNumber
}
});
TimeTravel.create(name);
TimeTravel.get(name).onChange = function (undoable, redoable, type, action) {
_this7.updateTimeTravelStatus(undoable, redoable);
_this7.historyChangeCustomHook(type, action);
};
}
/** 切换到指定的历史记录 */
}, {
key: "applyTimeTravel",
value: function applyTimeTravel() {
TimeTravel["switch"](this.name);
}
/** 历史记录变更会触发的自定义钩子,子类可以重写 */
}, {
key: "historyChangeCustomHook",
value: function historyChangeCustomHook(type, action) {
//
}
/** 清空历史记录 */
}, {
key: "clearTimeTravel",
value: function clearTimeTravel() {
TimeTravel["switch"](this.name);
TimeTravel.clear();
}
/** 暂停记录历史 */
}, {
key: "pauseRecord",
value: function pauseRecord() {
TimeTravel["switch"](this.name);
TimeTravel.pause();
}
/** 继续记录历史 */
}, {
key: "resumeRecord",
value: function resumeRecord() {
TimeTravel["switch"](this.name);
TimeTravel.resume();
}
/** 撤销 */
}, {
key: "undo",
value: function undo() {
if (this.disableUndoRedo) {
return;
}
if (!this.undoable) {
return;
}
TimeTravel["switch"](this.name);
var t = TimeTravel.get(this.name);
if (!t) {
return;
}
t.undo();
}
/** 重做 */
}, {
key: "redo",
value: function redo() {
if (this.disableUndoRedo) {
return;
}
if (!this.redoable) {
return;
}
TimeTravel["switch"](this.name);
var t = TimeTravel.get(this.name);
if (!t) {
return;
}
t.redo();
}
/** 文档加载状态设置为加载中 */
}, {
key: "isLoading",
value: function isLoading() {
this.loading = true;
}
/** 文档加载状态设置为加载完成 */
}, {
key: "isLoaded",
value: function isLoaded() {
this.loading = false;
}
/** 自动保存 */
}, {
key: "autoSave",
value: (function () {
var _autoSave = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
case "end":
return _context.stop();
}
}, _callee);
}));
function autoSave() {
return _autoSave.apply(this, arguments);
}
return autoSave;
}() /** 载入文档 */)
}, {
key: "load",
value: (function () {
var _load = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
case "end":
return _context2.stop();
}
}, _callee2);
}));
function load() {
return _load.apply(this, arguments);
}
return load;
}() /** 保存文档 */)
}, {
key: "save",
value: (function () {
var _save = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) switch (_context3.prev = _context3.next) {
case 0:
case "end":
return _context3.stop();
}
}, _callee3);
}));
function save() {
return _save.apply(this, arguments);
}
return save;
}())
}, {
key: "updateTimeTravelStatus",
value: function updateTimeTravelStatus() {
var undo = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
var redo = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
this.undoable = undo;
this.redoable = redo;
}
}]);
}(Domain);
export { DocumentSystem as default };
__decorate([reactor()], DocumentSystem.prototype, "models", void 0);
__decorate([reactor(false, false)], DocumentSystem.prototype, "loading", void 0);
__decorate([reactor(false, false)], DocumentSystem.prototype, "undoable", void 0);
__decorate([reactor(false, false)], DocumentSystem.prototype, "redoable", void 0);
__decorate([reactor(false, false)], DocumentSystem.prototype, "disableUndoRedo", void 0);
__decorate([mutation], DocumentSystem.prototype, "addModel", null);
__decorate([mutation], DocumentSystem.prototype, "removeModel", null);
__decorate([mutation], DocumentSystem.prototype, "removeModelById", null);
__decorate([mutation], DocumentSystem.prototype, "clear", null);
__decorate([mutation], DocumentSystem.prototype, "isLoading", null);
__decorate([mutation], DocumentSystem.prototype, "isLoaded", null);
__decorate([mutation], DocumentSystem.prototype, "updateTimeTravelStatus", null);