@kaitify/core 0.0.1-beta.32 → 0.0.1-beta.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2123,6 +2123,7 @@ class History {
2123
2123
  * 更新当前记录的编辑器的光标
2124
2124
  */
2125
2125
  updateSelection(selection) {
2126
+ if (this.records.length === 0) return;
2126
2127
  const record = this.records[this.records.length - 1];
2127
2128
  const newSelection = this.cloneSelection(record.nodes, selection);
2128
2129
  this.records[this.records.length - 1].selection = newSelection;
@@ -2969,18 +2970,17 @@ const formatPlaceholderMerge = ({ editor, node }) => {
2969
2970
  const formatLineBreakSpaceText = ({ editor, node }) => {
2970
2971
  if (node.isText() && !node.isEmpty()) {
2971
2972
  node.textContent = node.textContent.replace(/\r\n/g, "\n").replace(/\u00A0/g, " ").replace(/\uFEFF/g, getZeroWidthText());
2972
- let startInBreak = false;
2973
- let endInBreak = false;
2973
+ let startPrevNumber = 0;
2974
+ let endPrevNumber = 0;
2975
+ const regExp = new RegExp(`\\n(?!${getZeroWidthText()})`, "g");
2974
2976
  if (editor.selection.focused()) {
2975
2977
  if (editor.selection.start.node.isEqual(node)) {
2976
- const offset = editor.selection.start.offset > 0 ? editor.selection.start.offset - 1 : 0;
2977
- const chart = node.textContent[offset];
2978
- startInBreak = chart === "\n";
2978
+ const preText = editor.selection.start.offset > 0 ? node.textContent.slice(0, editor.selection.start.offset) : "";
2979
+ startPrevNumber = (preText.match(regExp) || []).length;
2979
2980
  }
2980
2981
  if (editor.selection.end.node.isEqual(node)) {
2981
- const offset = editor.selection.end.offset > 0 ? editor.selection.end.offset - 1 : 0;
2982
- const chart = node.textContent[offset];
2983
- endInBreak = chart === "\n";
2982
+ const preText = editor.selection.end.offset > 0 ? node.textContent.slice(0, editor.selection.end.offset) : "";
2983
+ endPrevNumber = (preText.match(regExp) || []).length;
2984
2984
  }
2985
2985
  }
2986
2986
  node.textContent = node.textContent.replace(/\n/g, (chart, index) => {
@@ -2990,11 +2990,11 @@ const formatLineBreakSpaceText = ({ editor, node }) => {
2990
2990
  }
2991
2991
  return chart;
2992
2992
  });
2993
- if (startInBreak) {
2994
- editor.selection.start.offset += 1;
2993
+ if (startPrevNumber > 0) {
2994
+ editor.selection.start.offset += startPrevNumber;
2995
2995
  }
2996
- if (endInBreak) {
2997
- editor.selection.end.offset += 1;
2996
+ if (endPrevNumber > 0) {
2997
+ editor.selection.end.offset += endPrevNumber;
2998
2998
  }
2999
2999
  }
3000
3000
  };
@@ -35228,11 +35228,15 @@ const CodeBlockExtension = () => Extension.create({
35228
35228
  tag: "pre"
35229
35229
  });
35230
35230
  if (!!codeBlockNode && this.selection.start.node.isText()) {
35231
- const parentNode = this.selection.start.node.parent;
35232
- const previousNode = this.selection.start.node.getPrevious(parentNode.children);
35233
- const nextNode = this.selection.start.node.getNext(parentNode.children);
35234
- if (previousNode && previousNode.isText() && /\n(\s*)\n$/.test(previousNode.textContent) && !nextNode) {
35235
- previousNode.textContent = previousNode.textContent.replace(/\n(\s*)\n$/, "");
35231
+ const index = this.selection.start.offset === 0 ? 0 : this.selection.start.offset - 1;
35232
+ const textContent = this.selection.start.node.textContent;
35233
+ const currentChar = textContent.charAt(index);
35234
+ const p1Char = index - 1 >= 0 ? textContent.charAt(index - 1) : null;
35235
+ const p2Char = index - 2 >= 0 ? textContent.charAt(index - 2) : null;
35236
+ const isLastChar = index === textContent.length - 1;
35237
+ const isLastText = this.selection.start.node.lastInTargetNode(codeBlockNode);
35238
+ if (currentChar === "\n" && p1Char !== null && p2Char !== null && isZeroWidthText(p1Char) && p2Char === "\n" && isLastChar && isLastText) {
35239
+ this.selection.start.node.textContent = this.selection.start.node.textContent.slice(0, index - 2);
35236
35240
  const paragraph = KNode.create({
35237
35241
  type: "block",
35238
35242
  tag: this.blockRenderTag,
@@ -38166,7 +38170,6 @@ class Editor {
38166
38170
  await delay();
38167
38171
  this.internalCauseSelectionChange = false;
38168
38172
  this.scrollViewToSelection();
38169
- this.history.updateSelection(this.selection);
38170
38173
  if (typeof this.onSelectionUpdate == "function") {
38171
38174
  this.onSelectionUpdate.apply(this, [this.selection]);
38172
38175
  }
@@ -38201,13 +38204,20 @@ class Editor {
38201
38204
  event.off(this.$el, "beforeinput.kaitify compositionstart.kaitify compositionupdate.kaitify compositionend.kaitify keydown.kaitify keyup.kaitify copy.kaitify focus.kaitify blur.kaitify");
38202
38205
  }
38203
38206
  /**
38204
- * 获取编辑器的纯文本内容
38207
+ * 获取编辑器的纯文本内容,excludeBreak表示是否排除换行符\n,excludeZeroWidthText表示是否排除零宽度空白字符
38205
38208
  */
38206
- getContent() {
38209
+ getContent(excludeBreak = false, excludeZeroWidthText = false) {
38207
38210
  if (!this.$el) {
38208
38211
  return "";
38209
38212
  }
38210
- return this.$el.textContent ?? "";
38213
+ let text2 = this.$el.textContent ?? "";
38214
+ if (excludeBreak) {
38215
+ text2 = text2.split("\n").join("");
38216
+ }
38217
+ if (excludeZeroWidthText) {
38218
+ text2 = text2.split(getZeroWidthText()).join("");
38219
+ }
38220
+ return text2;
38211
38221
  }
38212
38222
  /**
38213
38223
  * 获取编辑器的html内容,该方法会返回一个包含style标签和div标签的html内容。自行展示html内容时可保证样式与编辑器一致,但是对于附件等有交互事件的元素交互事件会失效