import { Scope, ScrollBlot, ContainerBlot } from 'parchment'; import Emitter from '../core/emitter'; import Block, { BlockEmbed } from './block'; import Break from './break'; import Container from './container'; function isLine(blot) { return blot instanceof Block || blot instanceof BlockEmbed; } class Scroll extends ScrollBlot { constructor(registry, domNode, { emitter }) { super(registry, domNode); this.emitter = emitter; this.batch = false; this.optimize(); this.enable(); this.domNode.addEventListener('dragstart', e => this.handleDragStart(e)); } batchStart() { if (!Array.isArray(this.batch)) { this.batch = []; } } batchEnd() { const mutations = this.batch; this.batch = false; this.update(mutations); } emitMount(blot) { this.emitter.emit(Emitter.events.SCROLL_BLOT_MOUNT, blot); } emitUnmount(blot) { this.emitter.emit(Emitter.events.SCROLL_BLOT_UNMOUNT, blot); } deleteAt(index, length) { const [first, offset] = this.line(index); const [last] = this.line(index + length); super.deleteAt(index, length); if (last != null && first !== last && offset > 0) { if (first instanceof BlockEmbed || last instanceof BlockEmbed) { this.optimize(); return; } const ref = last.children.head instanceof Break ? null : last.children.head; first.moveChildren(last, ref); first.remove(); } this.optimize(); } enable(enabled = true) { this.domNode.setAttribute('contenteditable', enabled); } formatAt(index, length, format, value) { super.formatAt(index, length, format, value); this.optimize(); } handleDragStart(event) { event.preventDefault(); } insertAt(index, value, def) { if (index >= this.length()) { if (def == null || this.scroll.query(value, Scope.BLOCK) == null) { const blot = this.scroll.create(this.statics.defaultChild.blotName); this.appendChild(blot); if (def == null && value.endsWith('\n')) { blot.insertAt(0, value.slice(0, -1), def); } else { blot.insertAt(0, value, def); } } else { const embed = this.scroll.create(value, def); this.appendChild(embed); } } else { super.insertAt(index, value, def); } this.optimize(); } insertBefore(blot, ref) { if (blot.statics.scope === Scope.INLINE_BLOT) { const wrapper = this.scroll.create(this.statics.defaultChild.blotName); wrapper.appendChild(blot); super.insertBefore(wrapper, ref); } else { super.insertBefore(blot, ref); } } isEnabled() { return this.domNode.getAttribute('contenteditable') === 'true'; } leaf(index) { return this.path(index).pop() || [null, -1]; } line(index) { if (index === this.length()) { return this.line(index - 1); } return this.descendant(isLine, index); } lines(index = 0, length = Number.MAX_VALUE) { const getLines = (blot, blotIndex, blotLength) => { let lines = []; let lengthLeft = blotLength; blot.children.forEachAt( blotIndex, blotLength, (child, childIndex, childLength) => { if (isLine(child)) { lines.push(child); } else if (child instanceof ContainerBlot) { lines = lines.concat(getLines(child, childIndex, lengthLeft)); } lengthLeft -= childLength; }, ); return lines; }; return getLines(this, index, length); } optimize(mutations = [], context = {}) { if (this.batch) return; super.optimize(mutations, context); if (mutations.length > 0) { this.emitter.emit(Emitter.events.SCROLL_OPTIMIZE, mutations, context); } } path(index) { return super.path(index).slice(1); // Exclude self } remove() { // Never remove self } update(mutations) { if (this.batch) { if (Array.isArray(mutations)) { this.batch = this.batch.concat(mutations); } return; } let source = Emitter.sources.USER; if (typeof mutations === 'string') { source = mutations; } if (!Array.isArray(mutations)) { mutations = this.observer.takeRecords(); } if (mutations.length > 0) { this.emitter.emit(Emitter.events.SCROLL_BEFORE_UPDATE, source, mutations); } super.update(mutations.concat([])); // pass copy if (mutations.length > 0) { this.emitter.emit(Emitter.events.SCROLL_UPDATE, source, mutations); } } } Scroll.blotName = 'scroll'; Scroll.className = 'ql-editor'; Scroll.tagName = 'DIV'; Scroll.defaultChild = Block; Scroll.allowedChildren = [Block, BlockEmbed, Container]; export default Scroll;