|
@@ -1,4 +1,5 @@
|
|
|
import $ from './util/dom-core.js';
|
|
|
+import { gennerOneLine } from './util/lineUtil.js';
|
|
|
import Jquery from 'jquery';
|
|
|
import yzPage from './yzPage.js';
|
|
|
import { UA, isFunction, replaceHtmlSymbol, getParentByClassname, trim, throttle, isEmptyElement, getParentNodeByClass } from './util/util.js';
|
|
@@ -86,6 +87,114 @@ yzPageManager.prototype = {
|
|
|
*/
|
|
|
_initEvents: function () {
|
|
|
this._realtimeSavePosition();
|
|
|
+ this._enterEvent();
|
|
|
+ this._backEvent();
|
|
|
+ this._delEvent();
|
|
|
+
|
|
|
+ this._contentchangeEvent();
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * function 内容变化事件监听
|
|
|
+ */
|
|
|
+ _contentchangeEvent: function () {
|
|
|
+ const $textElem = this.$el;
|
|
|
+ const editor =this.editor;
|
|
|
+ let _this = this;
|
|
|
+ Jquery($textElem[0]).on('DOMNodeInserted', function (e) {
|
|
|
+ console.log('insert');
|
|
|
+ })
|
|
|
+
|
|
|
+ Jquery($textElem[0]).on('DOMNodeRemoved', function (e) {
|
|
|
+ console.log('remove');
|
|
|
+ })
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * function del键监听
|
|
|
+ */
|
|
|
+ _delEvent: function () {
|
|
|
+ const $textElem = this.$el;
|
|
|
+ const editor =this.editor;
|
|
|
+ let _this = this;
|
|
|
+ Jquery($textElem[0]).on('keydown', function (e) {
|
|
|
+ if (e.keyCode !== 46) {
|
|
|
+ // 不是回车键
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+ if (getParentByClassname($(e.target), 'js-answer-header-title')) {
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+ let $currentLine = _this.editorPosition.currentLine;
|
|
|
+ if ($currentLine) {
|
|
|
+ let selection = _this.editor.selection;
|
|
|
+ if (selection.isCartEnd($currentLine)) {
|
|
|
+ let nextLine = $currentLine[0].nextSibling;
|
|
|
+ if (nextLine && nextLine.className && nextLine.className.indexOf('js-lsiten-line') > -1 && isEmptyElement(nextLine)) {
|
|
|
+ $(nextLine).remove();
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * function 退格键监听
|
|
|
+ */
|
|
|
+ _backEvent () {
|
|
|
+ const $textElem = this.$el;
|
|
|
+ const editor =this.editor;
|
|
|
+ let _this = this;
|
|
|
+ Jquery($textElem[0]).on('keydown', function (e) {
|
|
|
+ if (e.keyCode !== 8) {
|
|
|
+ // 不是回车键
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+ if (getParentByClassname($(e.target), 'js-answer-header-title')) {
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+ let $currentLine = _this.editorPosition.currentLine;
|
|
|
+ if ($currentLine) {
|
|
|
+ if (isEmptyElement($currentLine[0])) {
|
|
|
+ let prevLine = $currentLine[0].previousSibling;
|
|
|
+ if (prevLine && prevLine.className && prevLine.className.indexOf('js-lsiten-line') > -1) {
|
|
|
+ $currentLine.remove();
|
|
|
+ prevLine.focus();
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * function 监听enter键事件
|
|
|
+ */
|
|
|
+ _enterEvent: function () {
|
|
|
+ const $textElem = this.$el;
|
|
|
+ const editor =this.editor;
|
|
|
+ let _this = this;
|
|
|
+ Jquery($textElem[0]).on('keydown', function (e) {
|
|
|
+ if (e.keyCode !== 13) {
|
|
|
+ // 不是回车键
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+ if (getParentByClassname($(e.target), 'js-answer-header-title')) {
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+ let $currentLine = _this.editorPosition.currentLine;
|
|
|
+ if ($currentLine) {
|
|
|
+ let $newLine = gennerOneLine();
|
|
|
+ $newLine.insertAfter($currentLine);
|
|
|
+ $newLine.focus();
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ })
|
|
|
},
|
|
|
/**
|
|
|
* function 实时保存当前编辑信息
|
|
@@ -116,8 +225,10 @@ yzPageManager.prototype = {
|
|
|
/**
|
|
|
* 更新当前编辑信息
|
|
|
*/
|
|
|
- _updatePositionInfo: function (e) {
|
|
|
- let $target = $(e.target || e.toElement);
|
|
|
+ _updatePositionInfo: function (e, $ele) {
|
|
|
+ let $target = $ele || $(e.target || e.toElement);
|
|
|
+ let $border = getParentByClassname($target, 'js-lsiten-border');
|
|
|
+ let $line = getParentByClassname($target, 'js-lsiten-line');
|
|
|
let $paragraph = getParentByClassname($target, 'js-paragraph-view');
|
|
|
if (!$paragraph) {
|
|
|
return false;
|
|
@@ -138,7 +249,20 @@ yzPageManager.prototype = {
|
|
|
this.editorPosition = {
|
|
|
currentParagraph: $paragraph,
|
|
|
currentColumn: this.currentPage.$colums[this.currentPage.currentColumn],
|
|
|
- currentPage: this.currentPage
|
|
|
+ currentPage: this.currentPage,
|
|
|
+ currentBorder: $border,
|
|
|
+ currentLine: $line
|
|
|
+ }
|
|
|
+
|
|
|
+ let selection = this.editor.selection;
|
|
|
+ if ($line) {
|
|
|
+ if (!selection.getRange() || !selection.getSelectionContainerElem()) {
|
|
|
+ selection.createRangeByElem($line, true);
|
|
|
+ } else {
|
|
|
+ selection.saveRange();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ selection.setEmptyRange();
|
|
|
}
|
|
|
},
|
|
|
_pagesWidth: {
|