UNPKG

1.41 kBJavaScriptView Raw
1import Text from './text';
2import Parchment from 'parchment';
3
4
5class Inline extends Parchment.Inline {
6 static compare(self, other) {
7 let selfIndex = Inline.order.indexOf(self);
8 let otherIndex = Inline.order.indexOf(other);
9 if (selfIndex >= 0 || otherIndex >= 0) {
10 return selfIndex - otherIndex;
11 } else if (self === other) {
12 return 0;
13 } else if (self < other) {
14 return -1;
15 } else {
16 return 1;
17 }
18 }
19
20 formatAt(index, length, name, value) {
21 if (Inline.compare(this.statics.blotName, name) < 0 && Parchment.query(name, Parchment.Scope.BLOT)) {
22 let blot = this.isolate(index, length);
23 if (value) {
24 blot.wrap(name, value);
25 }
26 } else {
27 super.formatAt(index, length, name, value);
28 }
29 }
30
31 optimize(context) {
32 super.optimize(context);
33 if (this.parent instanceof Inline &&
34 Inline.compare(this.statics.blotName, this.parent.statics.blotName) > 0) {
35 let parent = this.parent.isolate(this.offset(), this.length());
36 this.moveChildren(parent);
37 parent.wrap(this);
38 }
39 }
40}
41Inline.allowedChildren = [Inline, Parchment.Embed, Text];
42// Lower index means deeper in the DOM tree, since not found (-1) is for embeds
43Inline.order = [
44 'cursor', 'inline', // Must be lower
45 'underline', 'strike', 'italic', 'bold', 'script',
46 'link', 'code' // Must be higher
47];
48
49
50export default Inline;