UNPKG

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