-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
chunk-tail-details.ts
185 lines (157 loc) · 5.5 KB
/
chunk-tail-details.ts
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import type { TailDetails, AppendTail } from '../../core/tail-details';
import ChangeDetails from '../../core/change-details';
import { isString } from '../../core/utils';
import ContinuousTailDetails from '../../core/continuous-tail-details';
import IMask from '../../core/holder';
import type MaskedPattern from '../pattern';
export
type ChunksTailState = Pick<ChunksTailDetails,
| 'from'
| 'stop'
| 'blockIndex'
> & { chunks: Array<TailDetails['state']> };
export default
class ChunksTailDetails implements TailDetails {
declare chunks: Array<TailDetails>;
declare from: number;
declare stop?: number;
/** */
declare blockIndex?: number;
constructor (chunks: Array<TailDetails>=[], from: number=0) {
this.chunks = chunks;
this.from = from;
}
toString (): string {
return this.chunks.map(String).join('');
}
extend (tailChunk: string | String | TailDetails): void {
if (!String(tailChunk)) return;
tailChunk = (isString(tailChunk) ? new ContinuousTailDetails(String(tailChunk)) : tailChunk) as TailDetails;
const lastChunk = this.chunks[this.chunks.length-1];
const extendLast = lastChunk &&
// if stops are same or tail has no stop
(lastChunk.stop === tailChunk.stop || tailChunk.stop == null) &&
// if tail chunk goes just after last chunk
tailChunk.from === (lastChunk.from + lastChunk.toString().length);
if (tailChunk instanceof ContinuousTailDetails) {
// check the ability to extend previous chunk
if (extendLast) {
// extend previous chunk
lastChunk.extend(tailChunk.toString());
} else {
// append new chunk
this.chunks.push(tailChunk);
}
} else if (tailChunk instanceof ChunksTailDetails) {
if (tailChunk.stop == null) {
// unwrap floating chunks to parent, keeping `from` pos
let firstTailChunk;
while (tailChunk.chunks.length && tailChunk.chunks[0].stop == null) {
firstTailChunk = tailChunk.chunks.shift() as TailDetails; // not possible to be `undefined` because length was checked above
firstTailChunk.from += tailChunk.from;
this.extend(firstTailChunk);
}
}
// if tail chunk still has value
if (tailChunk.toString()) {
// if chunks contains stops, then popup stop to container
tailChunk.stop = tailChunk.blockIndex;
this.chunks.push(tailChunk);
}
}
}
appendTo (masked: AppendTail | MaskedPattern): ChangeDetails {
if (!(masked instanceof IMask.MaskedPattern)) {
const tail = new ContinuousTailDetails(this.toString());
return tail.appendTo(masked);
}
const details = new ChangeDetails();
for (let ci=0; ci < this.chunks.length; ++ci) {
const chunk = this.chunks[ci];
const lastBlockIter = masked._mapPosToBlock(masked.displayValue.length);
const stop = chunk.stop;
let chunkBlock;
if (stop != null &&
// if block not found or stop is behind lastBlock
(!lastBlockIter || lastBlockIter.index <= stop)
) {
if (
chunk instanceof ChunksTailDetails ||
// for continuous block also check if stop is exist
masked._stops.indexOf(stop) >= 0
) {
details.aggregate(masked._appendPlaceholder(stop));
}
chunkBlock = chunk instanceof ChunksTailDetails && masked._blocks[stop];
}
if (chunkBlock) {
const tailDetails = chunkBlock.appendTail(chunk);
details.aggregate(tailDetails);
// get not inserted chars
const remainChars = chunk.toString().slice(tailDetails.rawInserted.length);
if (remainChars) details.aggregate(masked.append(remainChars, { tail: true }));
} else {
details.aggregate(masked.append(chunk.toString(), { tail: true }));
}
}
return details;
}
get state (): ChunksTailState {
return {
chunks: this.chunks.map(c => c.state),
from: this.from,
stop: this.stop,
blockIndex: this.blockIndex,
};
}
set state (state: ChunksTailState) {
const { chunks, ...props } = state;
Object.assign(this, props);
this.chunks = chunks.map(cstate => {
const chunk = "chunks" in cstate ?
new ChunksTailDetails() :
new ContinuousTailDetails();
chunk.state = cstate;
return chunk;
});
}
unshift (beforePos?: number): string {
if (!this.chunks.length || (beforePos != null && this.from >= beforePos)) return '';
const chunkShiftPos = beforePos != null ? beforePos - this.from : beforePos;
let ci=0;
while (ci < this.chunks.length) {
const chunk = this.chunks[ci];
const shiftChar = chunk.unshift(chunkShiftPos);
if (chunk.toString()) {
// chunk still contains value
// but not shifted - means no more available chars to shift
if (!shiftChar) break;
++ci;
} else {
// clean if chunk has no value
this.chunks.splice(ci, 1);
}
if (shiftChar) return shiftChar;
}
return '';
}
shift (): string {
if (!this.chunks.length) return '';
let ci=this.chunks.length-1;
while (0 <= ci) {
const chunk = this.chunks[ci];
const shiftChar = chunk.shift();
if (chunk.toString()) {
// chunk still contains value
// but not shifted - means no more available chars to shift
if (!shiftChar) break;
--ci;
} else {
// clean if chunk has no value
this.chunks.splice(ci, 1);
}
if (shiftChar) return shiftChar;
}
return '';
}
}