-
Notifications
You must be signed in to change notification settings - Fork 108
/
Chunk.js
146 lines (113 loc) · 2.85 KB
/
Chunk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
export default function Chunk ( start, end, content ) {
this.start = start;
this.end = end;
this.original = content;
this.intro = '';
this.outro = '';
this.content = content;
this.storeName = false;
this.edited = false;
// we make these non-enumerable, for sanity while debugging
Object.defineProperties( this, {
previous: { writable: true, value: null },
next: { writable: true, value: null }
});
}
Chunk.prototype = {
append ( content ) {
this.outro += content;
},
clone () {
const chunk = new Chunk( this.start, this.end, this.original );
chunk.intro = this.intro;
chunk.outro = this.outro;
chunk.content = this.content;
chunk.storeName = this.storeName;
chunk.edited = this.edited;
return chunk;
},
contains ( index ) {
return this.start < index && index < this.end;
},
eachNext ( fn ) {
let chunk = this;
while ( chunk ) {
fn( chunk );
chunk = chunk.next;
}
},
eachPrevious ( fn ) {
let chunk = this;
while ( chunk ) {
fn( chunk );
chunk = chunk.previous;
}
},
edit ( content, storeName ) {
this.content = content;
this.intro = '';
this.outro = '';
this.storeName = storeName;
this.edited = true;
return this;
},
prepend ( content ) {
this.intro = content + this.intro;
},
split ( index ) {
const sliceIndex = index - this.start;
const originalBefore = this.original.slice( 0, sliceIndex );
const originalAfter = this.original.slice( sliceIndex );
this.original = originalBefore;
const newChunk = new Chunk( index, this.end, originalAfter );
newChunk.outro = this.outro;
this.outro = '';
this.end = index;
if ( this.edited ) {
// TODO is this block necessary?...
newChunk.edit( '', false );
this.content = '';
} else {
this.content = originalBefore;
}
newChunk.next = this.next;
if ( newChunk.next ) newChunk.next.previous = newChunk;
newChunk.previous = this;
this.next = newChunk;
return newChunk;
},
toString () {
return this.intro + this.content + this.outro;
},
trimEnd ( rx ) {
this.outro = this.outro.replace( rx, '' );
if ( this.outro.length ) return true;
const trimmed = this.content.replace( rx, '' );
if ( trimmed.length ) {
if ( trimmed !== this.content ) {
this.split( this.start + trimmed.length ).edit( '', false );
}
return true;
} else {
this.edit( '', false );
this.intro = this.intro.replace( rx, '' );
if ( this.intro.length ) return true;
}
},
trimStart ( rx ) {
this.intro = this.intro.replace( rx, '' );
if ( this.intro.length ) return true;
const trimmed = this.content.replace( rx, '' );
if ( trimmed.length ) {
if ( trimmed !== this.content ) {
this.split( this.end - trimmed.length );
this.edit( '', false );
}
return true;
} else {
this.edit( '', false );
this.outro = this.outro.replace( rx, '' );
if ( this.outro.length ) return true;
}
}
};