1 | import Text from './text';
|
2 | import Parchment from 'parchment';
|
3 |
|
4 |
|
5 | class 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 | }
|
41 | Inline.allowedChildren = [Inline, Parchment.Embed, Text];
|
42 |
|
43 | Inline.order = [
|
44 | 'cursor', 'inline',
|
45 | 'underline', 'strike', 'italic', 'bold', 'script',
|
46 | 'link', 'code'
|
47 | ];
|
48 |
|
49 |
|
50 | export default Inline;
|