![]() System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /var/www/html/libs/absol-full/dist/js/ |
Upload File : |
/*** module: node_modules/absol-form/js/kfeditor/KFEditor.js ***/ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KFESerializer = KFESerializer; exports.default = void 0; var _Fragment = _interopRequireDefault(require("absol/src/AppPattern/Fragment")); var _OOP = _interopRequireWildcard(require("absol/src/HTML5/OOP")); require("../../css/kfeditor.css"); var _FCore = require("../core/FCore"); var _CMDTool = _interopRequireWildcard(require("../fragment/CMDTool")); var _EventEmitter = _interopRequireDefault(require("absol/src/HTML5/EventEmitter")); var _Context = _interopRequireDefault(require("absol/src/AppPattern/Context")); var _keyboard = require("absol/src/Input/keyboard"); var _KFEngine = _interopRequireDefault(require("./KFEngine")); var _KFECmd = _interopRequireDefault(require("./KFECmd")); /** * @augments EventEmitter * @augments Fragment * @constructor */ function KFEditor() { _EventEmitter.default.call(this); _Fragment.default.call(this); this.serializer = new KFESerializer(); this.cmdDelegate = new KFECmdDelegate(this); this.cmdTool = new _CMDTool.default(); this.cmdTool.delegate = this.cmdDelegate; this.inputCtrl = new KFTextInputController(this); this.rangeCtrl = new KFRangeController(this); this.engine = new _KFEngine.default(this); this.undoMng = new KFUndoManager(this); } (0, _OOP.mixClass)(KFEditor, _EventEmitter.default, _Fragment.default); KFEditor.prototype.createView = function () { this.$view = (0, _FCore._)({ class: 'kf-editor', child: [{ class: 'kfe-header', child: [this.cmdTool.getView()] }, { class: 'kfe-body', child: [{ class: 'kfe-main', child: [{ class: 'kfe-foreground' }, { class: 'kfe-range-ctn' }, { attr: { contenteditable: true, spellcheck: false }, class: 'kfe-content' }] }] }] }); this.$body = (0, _FCore.$)('.kfe-body', this.$view); this.$content = (0, _FCore.$)('.kfe-content', this.$view); this.inputCtrl.onCreateView(); this.rangeCtrl.onCreateView(); }; KFEditor.prototype.setData = function (data) { this.serializer.dataToDom(data, this.$content); this.engine.normalizeContent(); this.undoMng.clear(); this.undoMng.commit(); }; KFEditor.prototype.getData = function () { return this.serializer.domToData(this.$content); }; KFEditor.prototype.onStart = function () { this.rangeCtrl.start(true); var oldData = localStorage.getItem("test_data"); if (oldData) { this.setData(oldData); } else { fetch('./kfeditor_sample.htm').then(res => res.text()).then(text => { this.setData(text); setTimeout(() => { var range = document.createRange(); range.setStart(this.$content, 5); var sel = document.getSelection(); sel.removeAllRanges(); sel.addRange(range); }, 200); }); } //test }; KFEditor.prototype.onResume = function () { this.rangeCtrl.resume(); }; KFEditor.prototype.onPause = function () { this.rangeCtrl.pause(); }; KFEditor.prototype.onStop = function () { this.rangeCtrl.stop(); }; var _default = KFEditor; /** * * @param {KFEditor} editor * @constructor */ exports.default = _default; function KFTextInputController(editor) { this.editor = editor; } KFTextInputController.prototype.onCreateView = function () { this.$content = this.editor.$content; this.$content.on('focus', this.ev_focus.bind(this)).on('blur', this.ev_blur.bind(this)).on('keydown', this.ev_keyDown.bind(this)).on('keyup', this.ev_keyUp.bind(this)).on('mouseup', this.ev_mouseUp.bind(this)); this.editor.$body.on('click', this.ev_clickBody.bind(this)); }; KFTextInputController.prototype.ev_focus = function () { console.log('focus'); }; KFTextInputController.prototype.ev_blur = function () { console.log('blur'); }; KFTextInputController.prototype.ev_clickBody = function (event) { var range; if (event.target.hasClass && event.target.hasClass('kfe-main')) { range = document.createRange(); range.setStart(this.$content, this.$content.childNodes.length); this.editor.engine.setRanges(range); } }; KFTextInputController.prototype.ev_keyDown = function (event) { var kb = (0, _keyboard.keyboardEventToKeyBindingIdent)(event); var egn = this.editor.engine; var whenPresses = {}; whenPresses.enter = () => { event.preventDefault(); egn.deleteContentAtCurrentSelection(); egn.breakParagraphAtCurrentSelection(); // this.editor }; whenPresses.backspace = function () { event.preventDefault(); if (egn.isSelectionCollapsed()) { egn.deleteBackAtCurrentSelection(); } else { egn.deleteContentAtCurrentSelection(); } }; whenPresses.delete = () => { event.preventDefault(); if (egn.isSelectionCollapsed()) {} else { egn.deleteContentAtCurrentSelection(); } }; whenPresses['ctrl-z'] = () => { event.preventDefault(); this.editor.undoMng.undo(); }; whenPresses['ctrl-y'] = () => { event.preventDefault(); this.editor.undoMng.redo(); }; if (whenPresses[kb]) { whenPresses[kb](); } else { egn.updateRanges(); } }; KFTextInputController.prototype.normalizeRangeDelay = function () { setTimeout(() => { this.editor.$content.focus(); var egn = this.editor.engine; var range = egn.getRanges()[0]; var normalizedRange = egn.computeNormalizeRange(range); if (normalizedRange !== range) { egn.setRanges([normalizedRange]); } this.editor.cmdDelegate.updateVisibility(); }, 30); }; KFTextInputController.prototype.ev_keyUp = function (event) { this.normalizeRangeDelay(); this.editor.undoMng.commitDelay(); }; KFTextInputController.prototype.ev_mouseUp = function () { this.normalizeRangeDelay(); }; /** * @extends Context * @param editor * @constructor */ function KFRangeController(editor) { _Context.default.call(this); this.editor = editor; this.ev_selectionChange = this.ev_selectionChange.bind(this); this.$ranges = []; } _OOP.default.mixClass(KFRangeController, _Context.default); KFRangeController.prototype.onCreateView = function () { this.$rangeCtn = (0, _FCore.$)('.kfe-range-ctn', this.editor.$view); this.editor.$content.on('blur', this.ev_blur.bind(this)).on('focus', this.ev_focus.bind(this)); }; KFRangeController.prototype.onResume = function () { document.addEventListener('selectionchange', this.ev_selectionChange); }; KFRangeController.prototype.onPause = function () { document.removeEventListener('selectionchange', this.ev_selectionChange); }; KFRangeController.prototype.ev_selectionChange = function (event) { if (document.activeElement !== this.editor.$content) return; var egn = this.editor.engine; egn.updateRanges(); var ranges = egn.getRanges(); this.viewRanges(ranges); this.editor.undoMng.commitDelay(); }; KFRangeController.prototype.viewRanges = function (ranges) { ranges = ranges || []; var bounds = ranges.reduce((ac, range) => { var sBounds = range.getClientRects(); for (var i = 0; i < sBounds.length; ++i) { ac.push(sBounds[i]); } return ac; }, []); var rootBound = this.$rangeCtn.getBoundingClientRect(); while (this.$ranges.length > bounds.length) { this.$ranges.pop().remove(); } while (this.$ranges.length < bounds.length) { this.$ranges.push((0, _FCore._)('.kfe-range').addTo(this.$rangeCtn)); } var i; for (i = 0; i < bounds.length; ++i) { this.$ranges[i].addStyle({ top: bounds[i].top - rootBound.top + 'px', left: bounds[i].left - rootBound.left + 'px', width: bounds[i].width + 'px', height: bounds[i].height + 'px' }); } }; KFRangeController.prototype.ev_blur = function (event) { this.$rangeCtn.removeClass('as-focus'); }; KFRangeController.prototype.ev_focus = function (event) { this.$rangeCtn.addClass('as-focus'); }; /** * * @param {KFEditor} editor * @constructor */ function KFUndoManager(editor) { this.editor = editor; this.comitTO = -1; this.stack = []; this.topIdx = -1; } KFUndoManager.prototype.commitDelay = function () { clearTimeout(this.comitTO); this.comitTO = setTimeout(this.commit.bind(this), 300); }; KFUndoManager.prototype.commit = function () { var data = this.editor.engine.getData(); var topData = this.stack[this.topIdx]; if (topData && topData.hash === data.hash) { this.stack[this.topIdx] = data; } else { this.topIdx++; this.stack.splice(this.topIdx, 0, data); this.stack.splice(this.topIdx + 1); } this.editor.cmdDelegate.updateVisibility(); }; KFUndoManager.prototype.clear = function () { this.stack = []; this.topIdx = -1; this.editor.cmdDelegate.updateVisibility(); }; KFUndoManager.prototype.canUndo = function () { return this.topIdx > 0; }; KFUndoManager.prototype.undo = function () { if (!this.canUndo()) return; this.topIdx--; var data = this.stack[this.topIdx]; this.editor.engine.setData(data); this.editor.cmdDelegate.updateVisibility(); }; KFUndoManager.prototype.canRedo = function () { return this.topIdx < this.stack.length - 1; }; KFUndoManager.prototype.redo = function () { if (!this.canRedo()) return; this.topIdx++; var data = this.stack[this.topIdx]; this.editor.engine.setData(data); this.editor.cmdDelegate.updateVisibility(); }; /** * @augments CMDToolDelegate * @augments CMDRunner * @param {KFEditor} editor * @constructor */ function KFECmdDelegate(editor) { this.editor = editor; _CMDTool.CMDToolDelegate.call(this); } (0, _OOP.mixClass)(KFECmdDelegate, _CMDTool.CMDToolDelegate); KFECmdDelegate.prototype.getCmdGroupTree = function () { return [['save'], ['undo', 'redo'], ['formatTextBold', 'formatTextItalic'], ['formatTextAlignLeft', 'formatTextAlignCenter', 'formatTextAlignRight'], ['insertImage']]; }; KFECmdDelegate.prototype.execCmd = function (name, ...args) { var hd = _KFECmd.default[name]; if (hd) return _KFECmd.default[name].exec.apply(this.editor, args); }; KFECmdDelegate.prototype.getCmdDescriptor = function (name) { var res = _CMDTool.CMDToolDelegate.prototype.getCmdDescriptor.apply(this, arguments); var hd = _KFECmd.default[name]; if (hd && hd.descriptor) res = Object.assign({}, res, hd.descriptor.apply(this.editor, arguments)); return res; }; function KFESerializer() {} /** * * @param {string|any} data * @param {AElement|HTMLElement} ctnElt */ KFESerializer.prototype.dataToDom = function (data, ctnElt) { ctnElt.innerHTML = data; // this.cleanContent(ctnElt); }; /** * * @param {AElement|HTMLElement} ctnElt */ KFESerializer.prototype.cleanContent = function (ctnElt) { var visit = elt => { var cElt, i; if (elt.tagName === 'TABLE') { for (i = elt.childNodes.length - 1; i >= 0; --i) { cElt = elt.childNodes[i]; if (cElt.tagName !== 'THEAD' && cElt.tagName !== 'TBODY') { cElt.remove(); } } } else if (elt.tagName === 'TR') { for (i = elt.childNodes.length - 1; i >= 0; --i) { cElt = elt.childNodes[i]; if (cElt.tagName !== 'TD' && cElt.tagName !== 'TH') { cElt.remove(); } } } else if (elt.tagName === 'THEAD' || elt.tagName === 'TBODY') { for (i = elt.childNodes.length - 1; i >= 0; --i) { cElt = elt.childNodes[i]; if (cElt.tagName !== 'TR') { cElt.remove(); } } } if (elt.childNodes) Array.prototype.forEach.call(elt.childNodes, cElt => visit(cElt)); }; visit(ctnElt); }; /** * * @param {AElement|HTMLElement} ctnElt * @return {string|any} */ KFESerializer.prototype.domToData = function (ctnElt) { return ctnElt.innerHTML; };