scroll.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import Parchment from 'parchment';
  2. import Emitter from '../core/emitter';
  3. import Block, { BlockEmbed } from './block';
  4. import Break from './break';
  5. import CodeBlock from '../formats/code';
  6. import Container from './container';
  7. function isLine(blot) {
  8. return (blot instanceof Block || blot instanceof BlockEmbed);
  9. }
  10. class Scroll extends Parchment.Scroll {
  11. constructor(domNode, config) {
  12. super(domNode);
  13. this.emitter = config.emitter;
  14. if (Array.isArray(config.whitelist)) {
  15. this.whitelist = config.whitelist.reduce(function(whitelist, format) {
  16. whitelist[format] = true;
  17. return whitelist;
  18. }, {});
  19. }
  20. // Some reason fixes composition issues with character languages in Windows/Chrome, Safari
  21. this.domNode.addEventListener('DOMNodeInserted', function() {});
  22. this.optimize();
  23. this.enable();
  24. }
  25. batchStart() {
  26. this.batch = true;
  27. }
  28. batchEnd() {
  29. this.batch = false;
  30. this.optimize();
  31. }
  32. deleteAt(index, length) {
  33. let [first, offset] = this.line(index);
  34. let [last, ] = this.line(index + length);
  35. super.deleteAt(index, length);
  36. if (last != null && first !== last && offset > 0) {
  37. if (first instanceof BlockEmbed || last instanceof BlockEmbed) {
  38. this.optimize();
  39. return;
  40. }
  41. if (first instanceof CodeBlock) {
  42. let newlineIndex = first.newlineIndex(first.length(), true);
  43. if (newlineIndex > -1) {
  44. first = first.split(newlineIndex + 1);
  45. if (first === last) {
  46. this.optimize();
  47. return;
  48. }
  49. }
  50. } else if (last instanceof CodeBlock) {
  51. let newlineIndex = last.newlineIndex(0);
  52. if (newlineIndex > -1) {
  53. last.split(newlineIndex + 1);
  54. }
  55. }
  56. let ref = last.children.head instanceof Break ? null : last.children.head;
  57. first.moveChildren(last, ref);
  58. first.remove();
  59. }
  60. this.optimize();
  61. }
  62. enable(enabled = true) {
  63. this.domNode.setAttribute('contenteditable', enabled);
  64. }
  65. formatAt(index, length, format, value) {
  66. if (this.whitelist != null && !this.whitelist[format]) return;
  67. super.formatAt(index, length, format, value);
  68. this.optimize();
  69. }
  70. insertAt(index, value, def) {
  71. if (def != null && this.whitelist != null && !this.whitelist[value]) return;
  72. if (index >= this.length()) {
  73. if (def == null || Parchment.query(value, Parchment.Scope.BLOCK) == null) {
  74. let blot = Parchment.create(this.statics.defaultChild);
  75. this.appendChild(blot);
  76. if (def == null && value.endsWith('\n')) {
  77. value = value.slice(0, -1);
  78. }
  79. blot.insertAt(0, value, def);
  80. } else {
  81. let embed = Parchment.create(value, def);
  82. this.appendChild(embed);
  83. }
  84. } else {
  85. super.insertAt(index, value, def);
  86. }
  87. this.optimize();
  88. }
  89. insertBefore(blot, ref) {
  90. if (blot.statics.scope === Parchment.Scope.INLINE_BLOT) {
  91. let wrapper = Parchment.create(this.statics.defaultChild);
  92. wrapper.appendChild(blot);
  93. blot = wrapper;
  94. }
  95. super.insertBefore(blot, ref);
  96. }
  97. leaf(index) {
  98. return this.path(index).pop() || [null, -1];
  99. }
  100. line(index) {
  101. if (index === this.length()) {
  102. return this.line(index - 1);
  103. }
  104. return this.descendant(isLine, index);
  105. }
  106. lines(index = 0, length = Number.MAX_VALUE) {
  107. let getLines = (blot, index, length) => {
  108. let lines = [], lengthLeft = length;
  109. blot.children.forEachAt(index, length, function(child, index, length) {
  110. if (isLine(child)) {
  111. lines.push(child);
  112. } else if (child instanceof Parchment.Container) {
  113. lines = lines.concat(getLines(child, index, lengthLeft));
  114. }
  115. lengthLeft -= length;
  116. });
  117. return lines;
  118. };
  119. return getLines(this, index, length);
  120. }
  121. optimize(mutations = [], context = {}) {
  122. if (this.batch === true) return;
  123. super.optimize(mutations, context);
  124. if (mutations.length > 0) {
  125. this.emitter.emit(Emitter.events.SCROLL_OPTIMIZE, mutations, context);
  126. }
  127. }
  128. path(index) {
  129. return super.path(index).slice(1); // Exclude self
  130. }
  131. update(mutations) {
  132. if (this.batch === true) return;
  133. let source = Emitter.sources.USER;
  134. if (typeof mutations === 'string') {
  135. source = mutations;
  136. }
  137. if (!Array.isArray(mutations)) {
  138. mutations = this.observer.takeRecords();
  139. }
  140. if (mutations.length > 0) {
  141. this.emitter.emit(Emitter.events.SCROLL_BEFORE_UPDATE, source, mutations);
  142. }
  143. super.update(mutations.concat([])); // pass copy
  144. if (mutations.length > 0) {
  145. this.emitter.emit(Emitter.events.SCROLL_UPDATE, source, mutations);
  146. }
  147. }
  148. }
  149. Scroll.blotName = 'scroll';
  150. Scroll.className = 'ql-editor';
  151. Scroll.tagName = 'DIV';
  152. Scroll.defaultChild = 'block';
  153. Scroll.allowedChildren = [Block, BlockEmbed, Container];
  154. export default Scroll;