cursor.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { EmbedBlot, Scope } from 'parchment';
  2. import TextBlot from './text';
  3. class Cursor extends EmbedBlot {
  4. static value() {
  5. return undefined;
  6. }
  7. constructor(scroll, domNode, selection) {
  8. super(scroll, domNode);
  9. this.selection = selection;
  10. this.textNode = document.createTextNode(Cursor.CONTENTS);
  11. this.domNode.appendChild(this.textNode);
  12. this.savedLength = 0;
  13. }
  14. detach() {
  15. // super.detach() will also clear domNode.__blot
  16. if (this.parent != null) this.parent.removeChild(this);
  17. }
  18. format(name, value) {
  19. if (this.savedLength !== 0) {
  20. super.format(name, value);
  21. return;
  22. }
  23. let target = this;
  24. let index = 0;
  25. while (target != null && target.statics.scope !== Scope.BLOCK_BLOT) {
  26. index += target.offset(target.parent);
  27. target = target.parent;
  28. }
  29. if (target != null) {
  30. this.savedLength = Cursor.CONTENTS.length;
  31. target.optimize();
  32. target.formatAt(index, Cursor.CONTENTS.length, name, value);
  33. this.savedLength = 0;
  34. }
  35. }
  36. index(node, offset) {
  37. if (node === this.textNode) return 0;
  38. return super.index(node, offset);
  39. }
  40. length() {
  41. return this.savedLength;
  42. }
  43. position() {
  44. return [this.textNode, this.textNode.data.length];
  45. }
  46. remove() {
  47. super.remove();
  48. this.parent = null;
  49. }
  50. restore() {
  51. if (this.selection.composing || this.parent == null) return null;
  52. const range = this.selection.getNativeRange();
  53. // Link format will insert text outside of anchor tag
  54. while (
  55. this.domNode.lastChild != null &&
  56. this.domNode.lastChild !== this.textNode
  57. ) {
  58. this.domNode.parentNode.insertBefore(
  59. this.domNode.lastChild,
  60. this.domNode,
  61. );
  62. }
  63. const prevTextBlot = this.prev instanceof TextBlot ? this.prev : null;
  64. const prevTextLength = prevTextBlot ? prevTextBlot.length() : 0;
  65. const nextTextBlot = this.next instanceof TextBlot ? this.next : null;
  66. const nextText = nextTextBlot ? nextTextBlot.text : '';
  67. const { textNode } = this;
  68. // take text from inside this blot and reset it
  69. const newText = textNode.data.split(Cursor.CONTENTS).join('');
  70. textNode.data = Cursor.CONTENTS;
  71. // proactively merge TextBlots around cursor so that optimization
  72. // doesn't lose the cursor. the reason we are here in cursor.restore
  73. // could be that the user clicked in prevTextBlot or nextTextBlot, or
  74. // the user typed something.
  75. let mergedTextBlot;
  76. if (prevTextBlot) {
  77. mergedTextBlot = prevTextBlot;
  78. if (newText || nextTextBlot) {
  79. prevTextBlot.insertAt(prevTextBlot.length(), newText + nextText);
  80. if (nextTextBlot) {
  81. nextTextBlot.remove();
  82. }
  83. }
  84. } else if (nextTextBlot) {
  85. mergedTextBlot = nextTextBlot;
  86. nextTextBlot.insertAt(0, newText);
  87. } else {
  88. const newTextNode = document.createTextNode(newText);
  89. mergedTextBlot = this.scroll.create(newTextNode);
  90. this.parent.insertBefore(mergedTextBlot, this);
  91. }
  92. this.remove();
  93. if (range) {
  94. // calculate selection to restore
  95. const remapOffset = (node, offset) => {
  96. if (prevTextBlot && node === prevTextBlot.domNode) {
  97. return offset;
  98. }
  99. if (node === textNode) {
  100. return prevTextLength + offset - 1;
  101. }
  102. if (nextTextBlot && node === nextTextBlot.domNode) {
  103. return prevTextLength + newText.length + offset;
  104. }
  105. return null;
  106. };
  107. const start = remapOffset(range.start.node, range.start.offset);
  108. const end = remapOffset(range.end.node, range.end.offset);
  109. if (start !== null && end !== null) {
  110. return {
  111. startNode: mergedTextBlot.domNode,
  112. startOffset: start,
  113. endNode: mergedTextBlot.domNode,
  114. endOffset: end,
  115. };
  116. }
  117. }
  118. return null;
  119. }
  120. update(mutations, context) {
  121. if (
  122. mutations.some(mutation => {
  123. return (
  124. mutation.type === 'characterData' && mutation.target === this.textNode
  125. );
  126. })
  127. ) {
  128. const range = this.restore();
  129. if (range) context.range = range;
  130. }
  131. }
  132. value() {
  133. return '';
  134. }
  135. }
  136. Cursor.blotName = 'cursor';
  137. Cursor.className = 'ql-cursor';
  138. Cursor.tagName = 'span';
  139. Cursor.CONTENTS = '\uFEFF'; // Zero width no break space
  140. export default Cursor;