@myrmidon/cadmus-core 0.0.23 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -24
- package/esm2020/lib/cadmus-core.module.mjs +20 -0
- package/esm2020/lib/datation.mjs +521 -0
- package/esm2020/lib/guards/pending-changes.guard.mjs +24 -0
- package/esm2020/lib/historical-date.mjs +299 -0
- package/esm2020/lib/models.mjs +11 -0
- package/esm2020/lib/pipes/sort.pipe.mjs +42 -0
- package/esm2020/lib/services/gravatar.service.mjs +30 -0
- package/esm2020/lib/services/library-route.service.mjs +211 -0
- package/esm2020/lib/services/text-layer.service.mjs +682 -0
- package/{esm2015/lib/text-range.js → esm2020/lib/text-range.mjs} +1 -1
- package/esm2020/lib/token-location.mjs +74 -0
- package/esm2020/lib/token-point.mjs +65 -0
- package/esm2020/lib/validators/cadmus-validators.mjs +61 -0
- package/{esm2015/lib/validators/password.validator.js → esm2020/lib/validators/password.validator.mjs} +6 -6
- package/{esm2015/myrmidon-cadmus-core.js → esm2020/myrmidon-cadmus-core.mjs} +0 -0
- package/esm2020/public-api.mjs +19 -0
- package/fesm2015/{myrmidon-cadmus-core.js → myrmidon-cadmus-core.mjs} +154 -536
- package/fesm2015/myrmidon-cadmus-core.mjs.map +1 -0
- package/fesm2020/myrmidon-cadmus-core.mjs +2127 -0
- package/fesm2020/myrmidon-cadmus-core.mjs.map +1 -0
- package/lib/cadmus-core.module.d.ts +8 -0
- package/lib/datation.d.ts +2 -2
- package/lib/guards/pending-changes.guard.d.ts +3 -0
- package/lib/historical-date.d.ts +7 -7
- package/lib/models.d.ts +1 -18
- package/lib/pipes/sort.pipe.d.ts +3 -0
- package/lib/services/gravatar.service.d.ts +3 -0
- package/lib/services/library-route.service.d.ts +10 -7
- package/lib/services/text-layer.service.d.ts +9 -6
- package/lib/text-range.d.ts +1 -1
- package/lib/token-location.d.ts +1 -1
- package/lib/token-point.d.ts +1 -1
- package/lib/validators/cadmus-validators.d.ts +11 -0
- package/lib/validators/password.validator.d.ts +2 -4
- package/myrmidon-cadmus-core.d.ts +1 -0
- package/package.json +21 -9
- package/public-api.d.ts +0 -6
- package/bundles/myrmidon-cadmus-core.umd.js +0 -2943
- package/bundles/myrmidon-cadmus-core.umd.js.map +0 -1
- package/esm2015/lib/cadmus-core.module.js +0 -14
- package/esm2015/lib/datation.js +0 -520
- package/esm2015/lib/guards/pending-changes.guard.js +0 -22
- package/esm2015/lib/historical-date.js +0 -297
- package/esm2015/lib/models.js +0 -11
- package/esm2015/lib/pipes/sort.pipe.js +0 -37
- package/esm2015/lib/roman-number.js +0 -155
- package/esm2015/lib/services/env.service.js +0 -76
- package/esm2015/lib/services/env.service.provider.js +0 -31
- package/esm2015/lib/services/error.service.js +0 -38
- package/esm2015/lib/services/gravatar.service.js +0 -28
- package/esm2015/lib/services/library-route.service.js +0 -208
- package/esm2015/lib/services/local-storage.service.js +0 -99
- package/esm2015/lib/services/text-layer.service.js +0 -680
- package/esm2015/lib/token-location.js +0 -74
- package/esm2015/lib/token-point.js +0 -65
- package/esm2015/lib/utils.js +0 -36
- package/esm2015/lib/validators/cadmus-validators.js +0 -40
- package/esm2015/public-api.js +0 -25
- package/fesm2015/myrmidon-cadmus-core.js.map +0 -1
- package/lib/roman-number.d.ts +0 -34
- package/lib/services/env.service.d.ts +0 -52
- package/lib/services/env.service.provider.d.ts +0 -15
- package/lib/services/error.service.d.ts +0 -13
- package/lib/services/local-storage.service.d.ts +0 -32
- package/lib/utils.d.ts +0 -4
- package/myrmidon-cadmus-core.metadata.json +0 -1
|
@@ -0,0 +1,2127 @@
|
|
|
1
|
+
import { RomanNumber, NgToolsModule } from '@myrmidon/ng-tools';
|
|
2
|
+
import * as i0 from '@angular/core';
|
|
3
|
+
import { Injectable, Pipe, Inject, NgModule } from '@angular/core';
|
|
4
|
+
import * as gravatar from 'gravatar';
|
|
5
|
+
import * as rangy from 'rangy';
|
|
6
|
+
import { CommonModule } from '@angular/common';
|
|
7
|
+
import { HttpClientModule } from '@angular/common/http';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Default options for formatting a datation.
|
|
11
|
+
*/
|
|
12
|
+
const DATATION_FORMAT_OPTIONS = {
|
|
13
|
+
aboutText: 'c.',
|
|
14
|
+
bcText: 'BC',
|
|
15
|
+
adText: 'AD',
|
|
16
|
+
dayNames: ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'],
|
|
17
|
+
monthNames: [
|
|
18
|
+
'jan',
|
|
19
|
+
'feb',
|
|
20
|
+
'mar',
|
|
21
|
+
'apr',
|
|
22
|
+
'may',
|
|
23
|
+
'jun',
|
|
24
|
+
'jul',
|
|
25
|
+
'aug',
|
|
26
|
+
'sep',
|
|
27
|
+
'oct',
|
|
28
|
+
'nov',
|
|
29
|
+
'dec',
|
|
30
|
+
],
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Single datation point in a historical date.
|
|
34
|
+
*/
|
|
35
|
+
// https://github.com/angular/angular/issues/18867#issuecomment-357484102
|
|
36
|
+
// @dynamic
|
|
37
|
+
class Datation {
|
|
38
|
+
/**
|
|
39
|
+
* Create a new datation, optionally setting its data.
|
|
40
|
+
* @param datation The optional data to be copied into the newly created datation.
|
|
41
|
+
*/
|
|
42
|
+
constructor(datation) {
|
|
43
|
+
this.value = 0;
|
|
44
|
+
if (datation) {
|
|
45
|
+
this.copyFrom(datation);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Sanitize the hint text representing a Datation's hint so that it does not
|
|
50
|
+
* include the reserved sequence -- (which gets replaced with Unicode EM
|
|
51
|
+
* space U+2014), nor the braces. This is because the double dashes are used
|
|
52
|
+
* to separate two datations, and braces to mark a hint in a datation.
|
|
53
|
+
* Also, the whitespaces are all flattened to simple spaces, and normalized
|
|
54
|
+
* (no whitespaces at start/end and no sequence of whitespaces).
|
|
55
|
+
* If the resulting sanitized string is empty, or it was null when received,
|
|
56
|
+
* null is returned.
|
|
57
|
+
* @param hint The hint or null/undefined.
|
|
58
|
+
* @returns The sanitized hint or undefined.
|
|
59
|
+
*/
|
|
60
|
+
static sanitizeHint(hint) {
|
|
61
|
+
if (!hint || !hint.trim()) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
// replace reserved characters
|
|
65
|
+
hint = hint.replace('--', '\u2014');
|
|
66
|
+
hint = hint.replace('{', '');
|
|
67
|
+
hint = hint.replace('}', '');
|
|
68
|
+
// flatten and normalize whitespaces
|
|
69
|
+
const s = hint.replace(/\s+/g, ' ').trim();
|
|
70
|
+
return s ? s : undefined;
|
|
71
|
+
}
|
|
72
|
+
static regexEscape(text) {
|
|
73
|
+
// https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript
|
|
74
|
+
return text.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Get BC and AD patterns for matching.
|
|
78
|
+
* @param options The datation format options.
|
|
79
|
+
*/
|
|
80
|
+
static getErasOptionsForRegex(options = DATATION_FORMAT_OPTIONS) {
|
|
81
|
+
return [
|
|
82
|
+
options.bcText ? this.regexEscape(options.bcText) : 'BC',
|
|
83
|
+
options.adText ? this.regexEscape(options.adText) : 'AD',
|
|
84
|
+
];
|
|
85
|
+
}
|
|
86
|
+
static getParserRegex(options) {
|
|
87
|
+
const bcad = this.getErasOptionsForRegex(options);
|
|
88
|
+
return new RegExp(
|
|
89
|
+
// about (1)
|
|
90
|
+
'(c\\.)?\\s*' +
|
|
91
|
+
// day (2)
|
|
92
|
+
'(?:([0123]?\\d)\\s+)?' +
|
|
93
|
+
// month (3)
|
|
94
|
+
'(?:([a-zA-Z]{3,})\\s+)?' +
|
|
95
|
+
// year/span (4,5)
|
|
96
|
+
'(?:' +
|
|
97
|
+
'(?:(\\d+)(?:\\s*/\\s*(\\d+))?)|' +
|
|
98
|
+
// century (6)
|
|
99
|
+
'([ivxIVX]+)' +
|
|
100
|
+
')\\s*' +
|
|
101
|
+
// era (7)
|
|
102
|
+
'(' +
|
|
103
|
+
bcad[0] +
|
|
104
|
+
'|' +
|
|
105
|
+
bcad[1] +
|
|
106
|
+
')?' +
|
|
107
|
+
// perhaps (8)
|
|
108
|
+
'(\\s*\\?)?' +
|
|
109
|
+
// hint (9)
|
|
110
|
+
'(?:\\s*\\{([^}]+)\\})?', 'i');
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Parse the specified text representing a datation.
|
|
114
|
+
* @param text The text to be parsed.
|
|
115
|
+
* @param options The formatter options.
|
|
116
|
+
* @returns The parsed datation, or undefined if invalid or empty.
|
|
117
|
+
* Note that here we return undefined to mean a non-existing point.
|
|
118
|
+
* We do not want a non-existing point to be represented as a null,
|
|
119
|
+
* as this would be included as in the JSON code when serializing.
|
|
120
|
+
*/
|
|
121
|
+
static parse(text, options = DATATION_FORMAT_OPTIONS) {
|
|
122
|
+
if (!text) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
const datation = new Datation();
|
|
126
|
+
const datationRegex = this.getParserRegex(options);
|
|
127
|
+
const m = datationRegex.exec(text);
|
|
128
|
+
if (!m) {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
// about
|
|
132
|
+
if (m[1]) {
|
|
133
|
+
datation.isApproximate = true;
|
|
134
|
+
}
|
|
135
|
+
// day
|
|
136
|
+
if (m[2]) {
|
|
137
|
+
datation.day = parseInt(m[2], 10);
|
|
138
|
+
}
|
|
139
|
+
// month
|
|
140
|
+
if (m[3]) {
|
|
141
|
+
const lowerMonth = m[3].toLowerCase();
|
|
142
|
+
const i = options.monthNames.findIndex((name) => name.toLowerCase() === lowerMonth);
|
|
143
|
+
if (i > -1) {
|
|
144
|
+
datation.month = i + 1;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
// year, span
|
|
148
|
+
if (m[4]) {
|
|
149
|
+
datation.value = parseInt(m[4], 10);
|
|
150
|
+
if (m[5]) {
|
|
151
|
+
datation.isSpan = true;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
datation.value = RomanNumber.fromRoman(m[6].toUpperCase());
|
|
156
|
+
datation.isCentury = true;
|
|
157
|
+
}
|
|
158
|
+
// era
|
|
159
|
+
if (m[7]) {
|
|
160
|
+
const era = m[7].replace(' ', '').toLowerCase();
|
|
161
|
+
if (era === options.bcText.toLowerCase()) {
|
|
162
|
+
datation.value = -datation.value;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
// dubious
|
|
166
|
+
if (m[8]) {
|
|
167
|
+
datation.isDubious = true;
|
|
168
|
+
}
|
|
169
|
+
// hint
|
|
170
|
+
if (m[9]) {
|
|
171
|
+
datation.hint = Datation.sanitizeHint(m[9]);
|
|
172
|
+
}
|
|
173
|
+
return datation;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Strip any era code from the specified datation format string.
|
|
177
|
+
* @param format The format string.
|
|
178
|
+
*/
|
|
179
|
+
static stripFormatStringEra(format) {
|
|
180
|
+
if (!format) {
|
|
181
|
+
return 'i';
|
|
182
|
+
}
|
|
183
|
+
// convert ga gp Ga Gp into simple g G
|
|
184
|
+
format = format.replace(new RegExp('([gG])[ap]', 'gi'), '$1');
|
|
185
|
+
// replace g/G with i/I
|
|
186
|
+
format = format.replace('g', 'i');
|
|
187
|
+
format = format.replace('G', 'I');
|
|
188
|
+
// remove ea ep Ea Ep with preceding w/W if any
|
|
189
|
+
format = format.replace(new RegExp('[wW]?[eE][ap]?', 'gi'), '');
|
|
190
|
+
return format;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Reset this datation.
|
|
194
|
+
*/
|
|
195
|
+
reset() {
|
|
196
|
+
this.value = 0;
|
|
197
|
+
this.isCentury = false;
|
|
198
|
+
this.isSpan = false;
|
|
199
|
+
this.isApproximate = false;
|
|
200
|
+
this.day = 0;
|
|
201
|
+
this.month = 0;
|
|
202
|
+
this.hint = undefined;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Copy data from the specified source datatation.
|
|
206
|
+
* @param datation The source datation.
|
|
207
|
+
*/
|
|
208
|
+
copyFrom(datation) {
|
|
209
|
+
this.value = datation.value;
|
|
210
|
+
this.isCentury = datation.isCentury;
|
|
211
|
+
this.isSpan = datation.isSpan;
|
|
212
|
+
this.isApproximate = datation.isApproximate;
|
|
213
|
+
this.isDubious = datation.isDubious;
|
|
214
|
+
this.day = datation.day;
|
|
215
|
+
this.month = datation.month;
|
|
216
|
+
this.hint = datation.hint ? datation.hint : undefined;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* True if this datation is undefined. A datation is undefined when
|
|
220
|
+
* its value is 0. Undefined datations are used as unknown points in a range
|
|
221
|
+
* for termini ante/post.
|
|
222
|
+
*/
|
|
223
|
+
isUndefined() {
|
|
224
|
+
return !this.value;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Get a sort value from this datation.
|
|
228
|
+
*/
|
|
229
|
+
getSortValue() {
|
|
230
|
+
let result;
|
|
231
|
+
if (this.isCentury) {
|
|
232
|
+
result = this.value * 100 + (this.value < 0 ? 50 : -50);
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
result = this.value;
|
|
236
|
+
}
|
|
237
|
+
if (this.isSpan) {
|
|
238
|
+
result += 0.5;
|
|
239
|
+
}
|
|
240
|
+
if (this.month && this.month > 0 && this.month <= 12) {
|
|
241
|
+
result += this.month / 12;
|
|
242
|
+
}
|
|
243
|
+
if (this.day && this.day > 0 && this.day <= 31) {
|
|
244
|
+
result += this.day / (12 * 31);
|
|
245
|
+
}
|
|
246
|
+
return result;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Get the 2-years span end value from this datation.
|
|
250
|
+
* A span year is always represented with its beginning year: e.g. -776/5
|
|
251
|
+
* is -776 with span, and 56/7 is 56 with span. This method returns the end year
|
|
252
|
+
* for a span, i.e. in the above samples -775 and 57 respectively. If this datation
|
|
253
|
+
* is not a span, the function just returns the unique year value.
|
|
254
|
+
*/
|
|
255
|
+
getSpanEnd() {
|
|
256
|
+
if (this.isCentury) {
|
|
257
|
+
return 0;
|
|
258
|
+
}
|
|
259
|
+
return this.isSpan ? this.value + 1 : this.value;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Get the century number corresponding this datation.
|
|
263
|
+
* This function returns 0 if the datation is null, or the century number,
|
|
264
|
+
* less than 0 if B.C. The result is based on the century value, if
|
|
265
|
+
* this is a century datation, or on the year, if this is not a century
|
|
266
|
+
* datation (e.g. -5 for 480 B.C., +1 for 31 A.D.).
|
|
267
|
+
*/
|
|
268
|
+
getCentury() {
|
|
269
|
+
if (this.value === 0) {
|
|
270
|
+
return 0;
|
|
271
|
+
}
|
|
272
|
+
return this.isCentury
|
|
273
|
+
? this.value
|
|
274
|
+
: Math.trunc(this.value / 100 + (this.value < 0 ? -1 : 1));
|
|
275
|
+
}
|
|
276
|
+
matchTokenAt(token, text, index) {
|
|
277
|
+
if (index + token.length > text.length) {
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
for (let i = 0; i < token.length; i++) {
|
|
281
|
+
if (token.charAt(i) !== text.charAt(i + index)) {
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
parseFormatString(format) {
|
|
288
|
+
if (!format) {
|
|
289
|
+
return ['G'];
|
|
290
|
+
}
|
|
291
|
+
// expand macros
|
|
292
|
+
const macros = [
|
|
293
|
+
'ga',
|
|
294
|
+
'awdwMMMwvwepwpwh',
|
|
295
|
+
'Ga',
|
|
296
|
+
'awdwMMMwvwepwpwh',
|
|
297
|
+
'gp',
|
|
298
|
+
'awdwMMMwvweawpwh',
|
|
299
|
+
'Gp',
|
|
300
|
+
'awdwMMMwvweawpwh',
|
|
301
|
+
'g',
|
|
302
|
+
'awdwMMMwvwewpwh',
|
|
303
|
+
'G',
|
|
304
|
+
'AwdwMMMwVwewPwH',
|
|
305
|
+
'i',
|
|
306
|
+
'awdwMMMwvwpwh',
|
|
307
|
+
'I',
|
|
308
|
+
'AwdwMMMwVwPwH',
|
|
309
|
+
];
|
|
310
|
+
const sb = [];
|
|
311
|
+
let fi = 0;
|
|
312
|
+
let matched;
|
|
313
|
+
while (fi < format.length) {
|
|
314
|
+
matched = false;
|
|
315
|
+
for (let mi = 0; mi < macros.length; mi += 2) {
|
|
316
|
+
if (this.matchTokenAt(macros[mi], format, fi)) {
|
|
317
|
+
sb.push(macros[mi + 1]);
|
|
318
|
+
fi += macros[mi].length;
|
|
319
|
+
matched = true;
|
|
320
|
+
break;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if (!matched) {
|
|
324
|
+
sb.push(format[fi]);
|
|
325
|
+
fi++;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
format = sb.join();
|
|
329
|
+
// split into tokens and literals
|
|
330
|
+
const tokens = [
|
|
331
|
+
'ddd',
|
|
332
|
+
'MMM',
|
|
333
|
+
'dd',
|
|
334
|
+
'ea',
|
|
335
|
+
'ep',
|
|
336
|
+
'Ea',
|
|
337
|
+
'Ep',
|
|
338
|
+
'ga',
|
|
339
|
+
'Ga',
|
|
340
|
+
'gp',
|
|
341
|
+
'Gp',
|
|
342
|
+
'MM',
|
|
343
|
+
];
|
|
344
|
+
fi = 0;
|
|
345
|
+
const results = [];
|
|
346
|
+
while (fi < format.length) {
|
|
347
|
+
matched = false;
|
|
348
|
+
for (let ti = 0; ti < tokens.length; ti++) {
|
|
349
|
+
if (this.matchTokenAt(tokens[ti], format, fi)) {
|
|
350
|
+
results.push(tokens[ti]);
|
|
351
|
+
fi += tokens[ti].length;
|
|
352
|
+
matched = true;
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
if (!matched) {
|
|
357
|
+
results.push(format.charAt(fi));
|
|
358
|
+
fi++;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
return results;
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Convert this datation to a string.
|
|
365
|
+
* @param format The format string, which can include any of the following:
|
|
366
|
+
* v = year or century value, lowercase
|
|
367
|
+
* V = year or century value, uppercase
|
|
368
|
+
* c = century, lowercase
|
|
369
|
+
* C = century, uppercase
|
|
370
|
+
* a or A = 'about' marker when present
|
|
371
|
+
* p or P = 'perhaps' marker when present
|
|
372
|
+
* d = day number when present
|
|
373
|
+
* dd = 2-digits (0-padded at left) day number when present
|
|
374
|
+
* ddd = weekday name when present
|
|
375
|
+
* M = month number when present
|
|
376
|
+
* MM = 2-digits (0-padded at left) month number when present
|
|
377
|
+
* MMM = month name when present
|
|
378
|
+
* e = era
|
|
379
|
+
* E = era, all uppercase
|
|
380
|
+
* ea = era, only if ante Christum natum
|
|
381
|
+
* ep = era, only if post Christum natum
|
|
382
|
+
* Ea = era all uppercase, only if ante Christum natum
|
|
383
|
+
* Ep = era all uppercase, only if post Christum natum
|
|
384
|
+
* h or H = hint when present
|
|
385
|
+
* w or W = smart whitespace (appended only when left char if any
|
|
386
|
+
* is not a whitespace)
|
|
387
|
+
* s or S = sort value
|
|
388
|
+
* g = generic: equal to awdwMMMwvwewpwh
|
|
389
|
+
* G = generic uppercase: equal to AwdwMMMwVwewPwH
|
|
390
|
+
* ga = generic ante Christum: era expressed only when post Christum
|
|
391
|
+
* natum. Equal to awdwMMMwvwepwpwh
|
|
392
|
+
* Ga = generic uppercase ante Christum: equal to awdwMMMwvwepwpwh
|
|
393
|
+
* gp = generic post Christum: era expressed only when ante Christum
|
|
394
|
+
* natum. Equal to awdwMMMwvweawpwh
|
|
395
|
+
* Gp = generic uppercase post Christum: equal to awdwMMMwvweawpwh
|
|
396
|
+
* i = generic with implicit (=not expressed) era: equal to awdwMMMwvwpwh
|
|
397
|
+
* I = generic uppercase with implicit (=not expressed) era: equal to
|
|
398
|
+
* AwdwMMMwVwPwH
|
|
399
|
+
* @param options The options.
|
|
400
|
+
*/
|
|
401
|
+
toString(format = 'G', options = DATATION_FORMAT_OPTIONS) {
|
|
402
|
+
if (this.isUndefined()) {
|
|
403
|
+
return '';
|
|
404
|
+
}
|
|
405
|
+
if (!format) {
|
|
406
|
+
format = 'G';
|
|
407
|
+
}
|
|
408
|
+
if (!options) {
|
|
409
|
+
options = DATATION_FORMAT_OPTIONS;
|
|
410
|
+
}
|
|
411
|
+
const sb = [];
|
|
412
|
+
const tokens = this.parseFormatString(format);
|
|
413
|
+
tokens.forEach((token) => {
|
|
414
|
+
switch (token) {
|
|
415
|
+
case 'v':
|
|
416
|
+
case 'V':
|
|
417
|
+
// year or century
|
|
418
|
+
if (this.isCentury) {
|
|
419
|
+
sb.push(token === 'v'
|
|
420
|
+
? RomanNumber.toRoman(Math.abs(this.value)).toLowerCase()
|
|
421
|
+
: RomanNumber.toRoman(Math.abs(this.value)).toUpperCase());
|
|
422
|
+
}
|
|
423
|
+
else {
|
|
424
|
+
const year = Math.abs(this.value);
|
|
425
|
+
sb.push(year.toString());
|
|
426
|
+
if (this.isSpan) {
|
|
427
|
+
sb.push(`/${year + (this.value < 0 ? -1 : +1)}`);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
break;
|
|
431
|
+
case 'c':
|
|
432
|
+
case 'C':
|
|
433
|
+
const century = Math.abs(this.getCentury());
|
|
434
|
+
sb.push(token === 'c'
|
|
435
|
+
? RomanNumber.toRoman(century).toLowerCase()
|
|
436
|
+
: RomanNumber.toRoman(century).toUpperCase());
|
|
437
|
+
break;
|
|
438
|
+
case 'a':
|
|
439
|
+
case 'A':
|
|
440
|
+
if (this.isApproximate) {
|
|
441
|
+
sb.push(options.aboutText);
|
|
442
|
+
}
|
|
443
|
+
break;
|
|
444
|
+
case 'p':
|
|
445
|
+
case 'P':
|
|
446
|
+
if (this.isDubious) {
|
|
447
|
+
sb.push('?');
|
|
448
|
+
}
|
|
449
|
+
break;
|
|
450
|
+
case 'd':
|
|
451
|
+
if (this.day) {
|
|
452
|
+
sb.push(this.day.toString());
|
|
453
|
+
}
|
|
454
|
+
break;
|
|
455
|
+
case 'dd':
|
|
456
|
+
if (this.day) {
|
|
457
|
+
sb.push(this.day.toString().padStart(2, '0'));
|
|
458
|
+
}
|
|
459
|
+
break;
|
|
460
|
+
case 'ddd':
|
|
461
|
+
if (this.month && this.day) {
|
|
462
|
+
const date = new Date(this.value, this.month, this.day);
|
|
463
|
+
const dow = date.getDay();
|
|
464
|
+
sb.push(options.dayNames[dow]);
|
|
465
|
+
}
|
|
466
|
+
break;
|
|
467
|
+
case 'M':
|
|
468
|
+
if (this.month) {
|
|
469
|
+
sb.push(this.month.toString());
|
|
470
|
+
}
|
|
471
|
+
break;
|
|
472
|
+
case 'MM':
|
|
473
|
+
if (this.month) {
|
|
474
|
+
sb.push(this.month.toString().padStart(2, '0'));
|
|
475
|
+
}
|
|
476
|
+
break;
|
|
477
|
+
case 'MMM':
|
|
478
|
+
if (this.month) {
|
|
479
|
+
sb.push(options.monthNames[this.month - 1]);
|
|
480
|
+
}
|
|
481
|
+
break;
|
|
482
|
+
case 'e':
|
|
483
|
+
sb.push(this.value < 0 ? options.bcText : options.adText);
|
|
484
|
+
break;
|
|
485
|
+
case 'E':
|
|
486
|
+
sb.push(this.value < 0
|
|
487
|
+
? options.bcText.toUpperCase()
|
|
488
|
+
: options.adText.toUpperCase());
|
|
489
|
+
break;
|
|
490
|
+
case 'ea':
|
|
491
|
+
if (this.value < 0) {
|
|
492
|
+
sb.push(options.bcText);
|
|
493
|
+
}
|
|
494
|
+
break;
|
|
495
|
+
case 'ep':
|
|
496
|
+
if (this.value > 0) {
|
|
497
|
+
sb.push(options.adText);
|
|
498
|
+
}
|
|
499
|
+
break;
|
|
500
|
+
case 'h':
|
|
501
|
+
case 'H':
|
|
502
|
+
if (this.hint) {
|
|
503
|
+
sb.push(` {${this.hint}}`);
|
|
504
|
+
}
|
|
505
|
+
break;
|
|
506
|
+
case 'w':
|
|
507
|
+
case 'W':
|
|
508
|
+
if (sb.length > 0) {
|
|
509
|
+
const last = sb[sb.length - 1];
|
|
510
|
+
if (!last.endsWith(' ')) {
|
|
511
|
+
sb.push(' ');
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
break;
|
|
515
|
+
case 's':
|
|
516
|
+
case 'S':
|
|
517
|
+
sb.push(this.getSortValue().toString());
|
|
518
|
+
break;
|
|
519
|
+
default:
|
|
520
|
+
// literal
|
|
521
|
+
sb.push(token);
|
|
522
|
+
break;
|
|
523
|
+
}
|
|
524
|
+
});
|
|
525
|
+
return sb.join('').trim().replace(new RegExp('\\s+', 'g'), ' ');
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
const APPROX_DELTA = 10;
|
|
530
|
+
/**
|
|
531
|
+
* Type of historical date.
|
|
532
|
+
*/
|
|
533
|
+
var HistoricalDateType;
|
|
534
|
+
(function (HistoricalDateType) {
|
|
535
|
+
HistoricalDateType[HistoricalDateType["undefined"] = 0] = "undefined";
|
|
536
|
+
HistoricalDateType[HistoricalDateType["point"] = 1] = "point";
|
|
537
|
+
HistoricalDateType[HistoricalDateType["range"] = 2] = "range";
|
|
538
|
+
})(HistoricalDateType || (HistoricalDateType = {}));
|
|
539
|
+
/**
|
|
540
|
+
* A historical date, including 0, 1, or 2 datation points.
|
|
541
|
+
*/
|
|
542
|
+
class HistoricalDate {
|
|
543
|
+
constructor(date) {
|
|
544
|
+
if (!date) {
|
|
545
|
+
this.a = new Datation();
|
|
546
|
+
// B=undefined for a single point
|
|
547
|
+
}
|
|
548
|
+
else {
|
|
549
|
+
this.a = new Datation(date.a);
|
|
550
|
+
if (date.b) {
|
|
551
|
+
this.b = new Datation(date.b);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Parse the specified text representing a historical datation.
|
|
557
|
+
* @param text The text to be parsed.
|
|
558
|
+
* @param options The datation formatter options.
|
|
559
|
+
* @return The datation, or undefined if invalid.
|
|
560
|
+
*/
|
|
561
|
+
static parse(text, options = DATATION_FORMAT_OPTIONS) {
|
|
562
|
+
if (!text) {
|
|
563
|
+
return null;
|
|
564
|
+
}
|
|
565
|
+
const dates = text.split('--');
|
|
566
|
+
let s1 = dates[0].trim();
|
|
567
|
+
const s2 = dates.length > 1 ? dates[1].trim() : null;
|
|
568
|
+
const date = new HistoricalDate();
|
|
569
|
+
if (dates.length > 1) {
|
|
570
|
+
// if both terms are specified, integrate BC/AD indication in 1st if required
|
|
571
|
+
if (s1 && s2) {
|
|
572
|
+
const bcad = Datation.getErasOptionsForRegex(options);
|
|
573
|
+
const tailRegexp = new RegExp(
|
|
574
|
+
// era (1)
|
|
575
|
+
'(' +
|
|
576
|
+
bcad[0] +
|
|
577
|
+
'|' +
|
|
578
|
+
bcad[1] +
|
|
579
|
+
')?' +
|
|
580
|
+
// dubious (2)
|
|
581
|
+
'(\\s*\\?)?' +
|
|
582
|
+
// hint (3)
|
|
583
|
+
'(?:\\s*\\{?:([^)]+)\\})?\\s*$', 'gi');
|
|
584
|
+
const m1 = tailRegexp.exec(s1);
|
|
585
|
+
// if 1st has no era try integration from 2nd
|
|
586
|
+
if (m1 && m1[1] && s2) {
|
|
587
|
+
// get era from 2nd (if 2nd hasn't it too, give up and AD will be assumed)
|
|
588
|
+
const m2 = tailRegexp.exec(s2);
|
|
589
|
+
if (m2 && m2[1]) {
|
|
590
|
+
const era = m2[1] || 'p.C.';
|
|
591
|
+
const sb1 = [];
|
|
592
|
+
if (m1.index > 0) {
|
|
593
|
+
sb1.push(s1.substr(0, m1.index));
|
|
594
|
+
}
|
|
595
|
+
sb1.push(' ' + era);
|
|
596
|
+
if (m1[2]) {
|
|
597
|
+
sb1.push('?');
|
|
598
|
+
}
|
|
599
|
+
if (m1[3]) {
|
|
600
|
+
sb1.push(` (${m1[3]})`);
|
|
601
|
+
}
|
|
602
|
+
s1 = sb1.join('');
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
date.setStartPoint(Datation.parse(s1, options));
|
|
607
|
+
if (s2) {
|
|
608
|
+
date.setEndPoint(Datation.parse(s2, options));
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
else {
|
|
612
|
+
// here we have a s2, even if empty. When empty, it represents
|
|
613
|
+
// an unknown point in a terminus ante/post, and as such we
|
|
614
|
+
// must set B=null. When B=undefined, it's not a range but a point.
|
|
615
|
+
const d = Datation.parse(s1, options);
|
|
616
|
+
if (d && !d.isUndefined()) {
|
|
617
|
+
date.setSinglePoint(d);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
return date;
|
|
621
|
+
}
|
|
622
|
+
getStartPoint() {
|
|
623
|
+
return this.getDateType() === HistoricalDateType.range ? this.a : undefined;
|
|
624
|
+
}
|
|
625
|
+
setStartPoint(value) {
|
|
626
|
+
this.a = new Datation(value);
|
|
627
|
+
// it's a range, ensure max is not null
|
|
628
|
+
if (!this.b) {
|
|
629
|
+
this.b = new Datation();
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
getEndPoint() {
|
|
633
|
+
return this.getDateType() === HistoricalDateType.range ? this.b : undefined;
|
|
634
|
+
}
|
|
635
|
+
setEndPoint(value) {
|
|
636
|
+
this.b = new Datation(value);
|
|
637
|
+
}
|
|
638
|
+
getSinglePoint() {
|
|
639
|
+
return this.getDateType() === HistoricalDateType.point ? this.a : undefined;
|
|
640
|
+
}
|
|
641
|
+
setSinglePoint(value) {
|
|
642
|
+
this.a = new Datation(value);
|
|
643
|
+
// it's a point, ensure max is undefined
|
|
644
|
+
this.b = undefined;
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* True if this date is undefined.
|
|
648
|
+
*/
|
|
649
|
+
isUndefined() {
|
|
650
|
+
return (!this.a || (this.a.isUndefined() && (!this.b || this.b.isUndefined())));
|
|
651
|
+
}
|
|
652
|
+
/**
|
|
653
|
+
* Get the type of this date.
|
|
654
|
+
*/
|
|
655
|
+
getDateType() {
|
|
656
|
+
if (this.isUndefined()) {
|
|
657
|
+
return HistoricalDateType.undefined;
|
|
658
|
+
}
|
|
659
|
+
return this.b ? HistoricalDateType.range : HistoricalDateType.point;
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* Validate this date.
|
|
663
|
+
* @returns Error message if invalid, or null if valid.
|
|
664
|
+
*/
|
|
665
|
+
validate() {
|
|
666
|
+
if (!this.a) {
|
|
667
|
+
return 'Missing point A';
|
|
668
|
+
}
|
|
669
|
+
if (this.getDateType() === HistoricalDateType.range) {
|
|
670
|
+
const av = this.a.getSortValue();
|
|
671
|
+
const bv = this.b?.getSortValue() || 0;
|
|
672
|
+
if (av && bv && av >= bv) {
|
|
673
|
+
return 'Point A is past point B';
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
return null;
|
|
677
|
+
}
|
|
678
|
+
/**
|
|
679
|
+
* Get the sort value for this date.
|
|
680
|
+
*/
|
|
681
|
+
getSortValue() {
|
|
682
|
+
switch (this.getDateType()) {
|
|
683
|
+
case HistoricalDateType.point:
|
|
684
|
+
return this.a.getSortValue();
|
|
685
|
+
case HistoricalDateType.range:
|
|
686
|
+
if (this.a.isUndefined()) {
|
|
687
|
+
return this.b.getSortValue() - APPROX_DELTA;
|
|
688
|
+
}
|
|
689
|
+
if (!this.b || this.b.isUndefined()) {
|
|
690
|
+
return this.a.getSortValue() + APPROX_DELTA;
|
|
691
|
+
}
|
|
692
|
+
return (this.a.getSortValue() + this.b.getSortValue()) / 2;
|
|
693
|
+
default:
|
|
694
|
+
return 0;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
/**
|
|
698
|
+
* Build a textual representation of this date.
|
|
699
|
+
* @param format The format string.
|
|
700
|
+
* @param options The formatter options.
|
|
701
|
+
*/
|
|
702
|
+
toString(format = 'G', options = DATATION_FORMAT_OPTIONS) {
|
|
703
|
+
const sb = [];
|
|
704
|
+
switch (this.getDateType()) {
|
|
705
|
+
case HistoricalDateType.point:
|
|
706
|
+
sb.push(this.a.toString(format, options));
|
|
707
|
+
break;
|
|
708
|
+
case HistoricalDateType.range:
|
|
709
|
+
// if both terms are present and belong to same era, omit it in 1st
|
|
710
|
+
if (!this.a.isUndefined() &&
|
|
711
|
+
this.b &&
|
|
712
|
+
!this.b.isUndefined() &&
|
|
713
|
+
this.a.value * this.b.value > 0) {
|
|
714
|
+
sb.push(this.a.toString(Datation.stripFormatStringEra(format), options));
|
|
715
|
+
sb.push(' -- ');
|
|
716
|
+
sb.push(this.b.toString(format, options));
|
|
717
|
+
break;
|
|
718
|
+
}
|
|
719
|
+
if (!this.a.isUndefined()) {
|
|
720
|
+
sb.push(this.a.toString(format, options) + ' ');
|
|
721
|
+
}
|
|
722
|
+
sb.push('--');
|
|
723
|
+
if (this.b && !this.b.isUndefined()) {
|
|
724
|
+
sb.push(this.b.toString(format, options));
|
|
725
|
+
}
|
|
726
|
+
break;
|
|
727
|
+
}
|
|
728
|
+
return sb.join('');
|
|
729
|
+
}
|
|
730
|
+
/**
|
|
731
|
+
* Get the approximate year value from the specified century value.
|
|
732
|
+
* @param century The century number, negative if B.C.
|
|
733
|
+
*/
|
|
734
|
+
centuryToYear(century) {
|
|
735
|
+
return century * 100 + (century < 0 ? 50 : -50);
|
|
736
|
+
}
|
|
737
|
+
/**
|
|
738
|
+
* Convert this date into a single approximate year value.
|
|
739
|
+
* For centuries, the central century year is returned (e.g. -350 for IV B.C.
|
|
740
|
+
* and +350 for IV A.D.); when the date is a range with a single boundary only
|
|
741
|
+
* (max or min) the corresponding year minus or plus a predefined delta value
|
|
742
|
+
* (10) is returned; if it's a range with both boundaries (max and min),
|
|
743
|
+
* the middle year between them is returned.
|
|
744
|
+
* @param useTerminusSpan True to add or remove the predefined span from
|
|
745
|
+
* a terminus ante or post.
|
|
746
|
+
*/
|
|
747
|
+
toYear(useTerminusSpan = true) {
|
|
748
|
+
let year = 0;
|
|
749
|
+
switch (this.getDateType()) {
|
|
750
|
+
case HistoricalDateType.point:
|
|
751
|
+
year = Math.trunc(this.a.isCentury ? this.centuryToYear(this.a.value) : this.a.value);
|
|
752
|
+
break;
|
|
753
|
+
case HistoricalDateType.range:
|
|
754
|
+
// min is missing: terminus ante
|
|
755
|
+
if (this.a.isUndefined() && this.b) {
|
|
756
|
+
year = Math.trunc((this.b.isCentury
|
|
757
|
+
? this.centuryToYear(this.b.value)
|
|
758
|
+
: this.b.value) - (useTerminusSpan ? APPROX_DELTA : 0));
|
|
759
|
+
break;
|
|
760
|
+
}
|
|
761
|
+
// max is missing: terminus post
|
|
762
|
+
if (!this.b || this.b.isUndefined()) {
|
|
763
|
+
year = Math.trunc((this.a.isCentury
|
|
764
|
+
? this.centuryToYear(this.a.value)
|
|
765
|
+
: this.a.value) + (useTerminusSpan ? APPROX_DELTA : 0));
|
|
766
|
+
break;
|
|
767
|
+
}
|
|
768
|
+
// both min and max
|
|
769
|
+
const min = this.a.isCentury
|
|
770
|
+
? this.centuryToYear(this.a.value)
|
|
771
|
+
: this.a.value;
|
|
772
|
+
const max = this.b.isCentury
|
|
773
|
+
? this.centuryToYear(this.b.value)
|
|
774
|
+
: this.b.value;
|
|
775
|
+
year = Math.trunc((max - min) / 2 + min);
|
|
776
|
+
if (year === 0) {
|
|
777
|
+
year = 1;
|
|
778
|
+
}
|
|
779
|
+
break;
|
|
780
|
+
}
|
|
781
|
+
return year;
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* True if any value (either in point or in range) has an about flag.
|
|
785
|
+
*/
|
|
786
|
+
isAbout() {
|
|
787
|
+
let about = false;
|
|
788
|
+
switch (this.getDateType()) {
|
|
789
|
+
case HistoricalDateType.point:
|
|
790
|
+
about = this.a.isApproximate;
|
|
791
|
+
break;
|
|
792
|
+
case HistoricalDateType.range:
|
|
793
|
+
if (!this.a.isUndefined()) {
|
|
794
|
+
about = this.a.isApproximate;
|
|
795
|
+
break;
|
|
796
|
+
}
|
|
797
|
+
if (this.b && !this.b.isUndefined()) {
|
|
798
|
+
about = this.b.isApproximate;
|
|
799
|
+
}
|
|
800
|
+
break;
|
|
801
|
+
}
|
|
802
|
+
return about ? true : false;
|
|
803
|
+
}
|
|
804
|
+
/**
|
|
805
|
+
* True if any value (either in point or in range) has an about flag.
|
|
806
|
+
*/
|
|
807
|
+
isDubious() {
|
|
808
|
+
let dubious = false;
|
|
809
|
+
switch (this.getDateType()) {
|
|
810
|
+
case HistoricalDateType.point:
|
|
811
|
+
dubious = this.a.isDubious;
|
|
812
|
+
break;
|
|
813
|
+
case HistoricalDateType.range:
|
|
814
|
+
if (!this.a.isUndefined()) {
|
|
815
|
+
dubious = this.a.isDubious;
|
|
816
|
+
break;
|
|
817
|
+
}
|
|
818
|
+
if (this.b && !this.b.isUndefined()) {
|
|
819
|
+
dubious = this.b.isDubious;
|
|
820
|
+
}
|
|
821
|
+
break;
|
|
822
|
+
}
|
|
823
|
+
return dubious ? true : false;
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
/**
|
|
828
|
+
* Data pin value type in a DataPinDefinition.
|
|
829
|
+
*/
|
|
830
|
+
var DataPinValueType;
|
|
831
|
+
(function (DataPinValueType) {
|
|
832
|
+
DataPinValueType[DataPinValueType["string"] = 0] = "string";
|
|
833
|
+
DataPinValueType[DataPinValueType["boolean"] = 1] = "boolean";
|
|
834
|
+
DataPinValueType[DataPinValueType["integer"] = 2] = "integer";
|
|
835
|
+
DataPinValueType[DataPinValueType["decimal"] = 3] = "decimal";
|
|
836
|
+
})(DataPinValueType || (DataPinValueType = {}));
|
|
837
|
+
|
|
838
|
+
/**
|
|
839
|
+
* A simple text range, expressed as start character index and characters
|
|
840
|
+
* length. This is used for misspellings operations.
|
|
841
|
+
*/
|
|
842
|
+
class TextRange {
|
|
843
|
+
constructor(start, length) {
|
|
844
|
+
if (start < 0)
|
|
845
|
+
throw new RangeError('TextRange start less than 0');
|
|
846
|
+
if (length < 0)
|
|
847
|
+
throw new RangeError('TextRange length less than 0');
|
|
848
|
+
this.start = start;
|
|
849
|
+
this.length = length;
|
|
850
|
+
}
|
|
851
|
+
/**
|
|
852
|
+
* Parse the specified text representing a range, in the form "startxlength"
|
|
853
|
+
* or just "start" when length is 1.
|
|
854
|
+
*
|
|
855
|
+
* @param text The text.
|
|
856
|
+
*/
|
|
857
|
+
static parse(text) {
|
|
858
|
+
if (!text) {
|
|
859
|
+
return null;
|
|
860
|
+
}
|
|
861
|
+
// [1]=start, ([2]=length)
|
|
862
|
+
const m = new RegExp('(\\d+)(?:[×x](\\d+))?', 'gm').exec(text);
|
|
863
|
+
if (!m) {
|
|
864
|
+
return null;
|
|
865
|
+
}
|
|
866
|
+
return new TextRange(parseInt(m[1], 10), m[2] ? parseInt(m[2], 10) : 1);
|
|
867
|
+
}
|
|
868
|
+
/**
|
|
869
|
+
* Get the inclusive end of the range.
|
|
870
|
+
*/
|
|
871
|
+
end() {
|
|
872
|
+
const n = this.start + this.length;
|
|
873
|
+
return n > 0 ? n - 1 : 0;
|
|
874
|
+
}
|
|
875
|
+
/**
|
|
876
|
+
* True if this range value is equal to the other range value.
|
|
877
|
+
* @param other The other range.
|
|
878
|
+
*/
|
|
879
|
+
isEqual(other) {
|
|
880
|
+
if (!other) {
|
|
881
|
+
return false;
|
|
882
|
+
}
|
|
883
|
+
return this.start === other.start && this.length === other.length;
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* Get a parsable textual representation of this range.
|
|
887
|
+
* @returns Text.
|
|
888
|
+
*/
|
|
889
|
+
toString() {
|
|
890
|
+
return this.length === 1
|
|
891
|
+
? this.start.toString()
|
|
892
|
+
: `${this.start}×${this.length}`;
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
/**
|
|
897
|
+
* A single point in a token-based textual location.
|
|
898
|
+
*/
|
|
899
|
+
class TokenPoint {
|
|
900
|
+
constructor(y, x, at = 0, run = 0) {
|
|
901
|
+
this.y = y;
|
|
902
|
+
this.x = x;
|
|
903
|
+
this.at = at;
|
|
904
|
+
this.run = run;
|
|
905
|
+
}
|
|
906
|
+
/**
|
|
907
|
+
* Parse the specified text representing a token-based point location.
|
|
908
|
+
* @param text The text to parse, in the forms Y.X or Y.X@AxR or
|
|
909
|
+
* Y.X@A, where R=1.
|
|
910
|
+
*/
|
|
911
|
+
static parse(text) {
|
|
912
|
+
const r = new RegExp('^(\\d+)\\.(\\d+)(?:@(\\d+)(?:x(\\d+))?)?$', 'g');
|
|
913
|
+
const m = r.exec(text);
|
|
914
|
+
if (!m) {
|
|
915
|
+
return null;
|
|
916
|
+
}
|
|
917
|
+
const pt = new TokenPoint(parseInt(m[1], 10), parseInt(m[2], 10), m[3] ? parseInt(m[3], 10) : 0);
|
|
918
|
+
if (m[3]) {
|
|
919
|
+
if (m[4]) {
|
|
920
|
+
pt.run = m[4] ? parseInt(m[4], 10) : 1;
|
|
921
|
+
}
|
|
922
|
+
else {
|
|
923
|
+
pt.run = 1;
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
return pt;
|
|
927
|
+
}
|
|
928
|
+
toString() {
|
|
929
|
+
let s = `${this.y}.${this.x}`;
|
|
930
|
+
if (this.at && this.run) {
|
|
931
|
+
s += `@${this.at}`;
|
|
932
|
+
if (this.run !== 1) {
|
|
933
|
+
s += `x${this.run}`;
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
return s;
|
|
937
|
+
}
|
|
938
|
+
compareTo(other) {
|
|
939
|
+
if (this.y !== other.y) {
|
|
940
|
+
return this.y - other.y;
|
|
941
|
+
}
|
|
942
|
+
if (this.x !== other.x) {
|
|
943
|
+
return this.x - other.x;
|
|
944
|
+
}
|
|
945
|
+
if (this.at !== other.at) {
|
|
946
|
+
return this.at - other.at;
|
|
947
|
+
}
|
|
948
|
+
return this.run - other.run;
|
|
949
|
+
}
|
|
950
|
+
integralCompareTo(other) {
|
|
951
|
+
if (this.y !== other.y) {
|
|
952
|
+
return this.y - other.y;
|
|
953
|
+
}
|
|
954
|
+
if (this.x !== other.x) {
|
|
955
|
+
return this.x - other.x;
|
|
956
|
+
}
|
|
957
|
+
return 0;
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
class TokenLocation {
|
|
962
|
+
constructor(primary, secondary) {
|
|
963
|
+
this.primary = primary;
|
|
964
|
+
this.secondary = secondary;
|
|
965
|
+
}
|
|
966
|
+
/**
|
|
967
|
+
* Parse the text representing a token-based location,
|
|
968
|
+
* which has the form "primary-secondary", or just "primary",
|
|
969
|
+
* where each of these points has the format required by
|
|
970
|
+
* TokenPoint.parse (e.g. Y.X or Y.X@AxR or Y.X@A, where R=1).
|
|
971
|
+
* @param text The text to parse.
|
|
972
|
+
*/
|
|
973
|
+
static parse(text) {
|
|
974
|
+
if (!text) {
|
|
975
|
+
return null;
|
|
976
|
+
}
|
|
977
|
+
const pair = text.split('-', 2);
|
|
978
|
+
if (pair.length === 0) {
|
|
979
|
+
return null;
|
|
980
|
+
}
|
|
981
|
+
return new TokenLocation(TokenPoint.parse(pair[0]), pair.length > 1 ? TokenPoint.parse(pair[1]) || undefined : undefined);
|
|
982
|
+
}
|
|
983
|
+
isRange() {
|
|
984
|
+
// note that != checks for both null and undefined, whereas !== does not
|
|
985
|
+
return this.secondary != null;
|
|
986
|
+
}
|
|
987
|
+
toString() {
|
|
988
|
+
return this.secondary
|
|
989
|
+
? `${this.primary.toString()}-${this.secondary.toString()}`
|
|
990
|
+
: this.primary.toString();
|
|
991
|
+
}
|
|
992
|
+
compareTo(other) {
|
|
993
|
+
const n1 = this.primary.compareTo(other.primary);
|
|
994
|
+
if (n1 !== 0) {
|
|
995
|
+
return n1;
|
|
996
|
+
}
|
|
997
|
+
if (this.secondary && other.secondary) {
|
|
998
|
+
return this.secondary.compareTo(other.secondary);
|
|
999
|
+
}
|
|
1000
|
+
if (!this.secondary && !other.secondary) {
|
|
1001
|
+
return 0;
|
|
1002
|
+
}
|
|
1003
|
+
return this.secondary ? 1 : -1;
|
|
1004
|
+
}
|
|
1005
|
+
overlaps(other) {
|
|
1006
|
+
// empty coords never overlaps
|
|
1007
|
+
if (!other.primary) {
|
|
1008
|
+
return false;
|
|
1009
|
+
}
|
|
1010
|
+
// (in the following comments A=this and B=other)
|
|
1011
|
+
// 1) if B is a point, cases are:
|
|
1012
|
+
// 1.1. A is a point: A overlaps with B (both points) if they're equal.
|
|
1013
|
+
// 1.2. A is a range: A (range) overlaps with B (point)
|
|
1014
|
+
// when A-left is <= B-left and A-right is >= B-right.
|
|
1015
|
+
if (other.secondary == null) {
|
|
1016
|
+
return !this.secondary
|
|
1017
|
+
? this.primary.integralCompareTo(other.primary) === 0
|
|
1018
|
+
: this.primary.compareTo(other.primary) <= 0 &&
|
|
1019
|
+
this.secondary.compareTo(other.primary) >= 0;
|
|
1020
|
+
}
|
|
1021
|
+
// 2) if B is a range, cases are:
|
|
1022
|
+
// 2.1. A is a point: A (point) overlaps with B (range) when B-left <= A and
|
|
1023
|
+
// B-right >= A
|
|
1024
|
+
// 2.2. A is a range: A (range) overlaps with B (range)
|
|
1025
|
+
// when B-right >= A-left and B-left <= A-right
|
|
1026
|
+
return !this.secondary
|
|
1027
|
+
? other.primary.compareTo(this.primary) <= 0 &&
|
|
1028
|
+
other.secondary.compareTo(this.primary) >= 0
|
|
1029
|
+
: other.secondary.compareTo(this.primary) >= 0 &&
|
|
1030
|
+
other.primary.compareTo(this.secondary) <= 0;
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
class PendingChangesGuard {
|
|
1035
|
+
canDeactivate(component) {
|
|
1036
|
+
// if there are no pending changes, just allow deactivation; else confirm first
|
|
1037
|
+
return !component || component.canDeactivate()
|
|
1038
|
+
? true
|
|
1039
|
+
: // NOTE: this warning message will only be shown when navigating
|
|
1040
|
+
// elsewhere within your angular app; when navigating away from your
|
|
1041
|
+
// angular app, the browser will show a generic warning message.
|
|
1042
|
+
// see http://stackoverflow.com/a/42207299/7307355
|
|
1043
|
+
confirm('Warning: your changes have not been saved. ' +
|
|
1044
|
+
'Press Cancel to go back and save, OK to continue.');
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
PendingChangesGuard.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: PendingChangesGuard, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
1048
|
+
PendingChangesGuard.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: PendingChangesGuard, providedIn: 'root' });
|
|
1049
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: PendingChangesGuard, decorators: [{
|
|
1050
|
+
type: Injectable,
|
|
1051
|
+
args: [{
|
|
1052
|
+
providedIn: 'root',
|
|
1053
|
+
}]
|
|
1054
|
+
}] });
|
|
1055
|
+
|
|
1056
|
+
/**
|
|
1057
|
+
* Sort pipe. Use like *ngFor="let i of items | sort:'propname'".
|
|
1058
|
+
*/
|
|
1059
|
+
class SortPipe {
|
|
1060
|
+
transform(value, propName) {
|
|
1061
|
+
if (!value || !Array.isArray(value)) {
|
|
1062
|
+
return value;
|
|
1063
|
+
}
|
|
1064
|
+
return [...value].sort((a, b) => {
|
|
1065
|
+
if (!a && !b) {
|
|
1066
|
+
return 0;
|
|
1067
|
+
}
|
|
1068
|
+
if (!a && b) {
|
|
1069
|
+
return -1;
|
|
1070
|
+
}
|
|
1071
|
+
if (a && !b) {
|
|
1072
|
+
return 1;
|
|
1073
|
+
}
|
|
1074
|
+
if (a[propName] < b[propName]) {
|
|
1075
|
+
return -1;
|
|
1076
|
+
}
|
|
1077
|
+
else if (a[propName] === b[propName]) {
|
|
1078
|
+
return 0;
|
|
1079
|
+
}
|
|
1080
|
+
else if (a[propName] > b[propName]) {
|
|
1081
|
+
return 1;
|
|
1082
|
+
}
|
|
1083
|
+
return 0;
|
|
1084
|
+
});
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
SortPipe.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: SortPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe });
|
|
1088
|
+
SortPipe.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: SortPipe, name: "sort" });
|
|
1089
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: SortPipe, decorators: [{
|
|
1090
|
+
type: Pipe,
|
|
1091
|
+
args: [{
|
|
1092
|
+
name: 'sort',
|
|
1093
|
+
}]
|
|
1094
|
+
}] });
|
|
1095
|
+
|
|
1096
|
+
// npm i gravatar --save
|
|
1097
|
+
// npm i @types/gravatar --save
|
|
1098
|
+
class GravatarService {
|
|
1099
|
+
/**
|
|
1100
|
+
* Builds the URL to the Gravatar avatar corresponding to the specified email
|
|
1101
|
+
* address.
|
|
1102
|
+
* @param email: The email address.
|
|
1103
|
+
* @param size The required size in pixels (1-512).
|
|
1104
|
+
*/
|
|
1105
|
+
buildGravatarUrl(email, size = 80) {
|
|
1106
|
+
if (!email) {
|
|
1107
|
+
return '';
|
|
1108
|
+
}
|
|
1109
|
+
return gravatar.url(email, { s: size.toString() });
|
|
1110
|
+
// const hash = this._hash.getMd5(email.trim().toLowerCase());
|
|
1111
|
+
// return this.buildGravatarUrlFromHash(hash, size);
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
GravatarService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: GravatarService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
1115
|
+
GravatarService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: GravatarService, providedIn: 'root' });
|
|
1116
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: GravatarService, decorators: [{
|
|
1117
|
+
type: Injectable,
|
|
1118
|
+
args: [{
|
|
1119
|
+
providedIn: 'root'
|
|
1120
|
+
}]
|
|
1121
|
+
}] });
|
|
1122
|
+
|
|
1123
|
+
/**
|
|
1124
|
+
* Library route service.
|
|
1125
|
+
* This service is used to build routes to part and fragment editors,
|
|
1126
|
+
* which are hosted in an open list of library modules in the Cadmus
|
|
1127
|
+
* application.
|
|
1128
|
+
* This architecture allows lazy-loading part- and fragment-editors
|
|
1129
|
+
* only when they are required.
|
|
1130
|
+
* Each part type with its dumb UI editor component is located in a
|
|
1131
|
+
* library, named after the name of the group the part belongs to. For
|
|
1132
|
+
* instance, the model and dumb UI editor component for a part belonging
|
|
1133
|
+
* to the "general" group are located in the "parts/general/general-ui"
|
|
1134
|
+
* library.
|
|
1135
|
+
* In turn, the feature UI editor component for the same part is located
|
|
1136
|
+
* in the "parts/general/general-feature" library. This library has
|
|
1137
|
+
* a route for each part editor. For instance, the NotePart editor
|
|
1138
|
+
* component route is "items/:iid/general/it.vedph.note/:pid", where
|
|
1139
|
+
* ":iid" is the item ID and ":pid" the part ID.
|
|
1140
|
+
*/
|
|
1141
|
+
class LibraryRouteService {
|
|
1142
|
+
constructor(_partEditorKeys) {
|
|
1143
|
+
this._partEditorKeys = _partEditorKeys;
|
|
1144
|
+
}
|
|
1145
|
+
/**
|
|
1146
|
+
* Strip the eventual fragment-role ID from the specified part's role ID.
|
|
1147
|
+
* For instance, a part's role ID like "fr.it.vedph.comment:scholarly"
|
|
1148
|
+
* becomes "fr.it.vedph.comment".
|
|
1149
|
+
*
|
|
1150
|
+
* @param roleId The part's role ID.
|
|
1151
|
+
*/
|
|
1152
|
+
stripFragmentRoleId(roleId) {
|
|
1153
|
+
// remove the optional :fr-role from roleId
|
|
1154
|
+
const i = roleId.lastIndexOf(':');
|
|
1155
|
+
if (i > -1) {
|
|
1156
|
+
roleId = roleId.substring(0, i);
|
|
1157
|
+
}
|
|
1158
|
+
return roleId;
|
|
1159
|
+
}
|
|
1160
|
+
/**
|
|
1161
|
+
* Get the fragment type and role from the part's role ID. This starts
|
|
1162
|
+
* with "fr.", and might be followed by the role proper, introduced
|
|
1163
|
+
* by the first dot if any. For instance, "fr.it.vedph.comment" would just
|
|
1164
|
+
* return "fr.it.vedph.comment", while "fr.it.vedph.comment:scholarly"
|
|
1165
|
+
* would return "fr.it.vedph.comment". This is required to navigate to
|
|
1166
|
+
* the correct fragment editor, which is the same for each fragment type,
|
|
1167
|
+
* independently from its role.
|
|
1168
|
+
* @param role The part role ID.
|
|
1169
|
+
* @returns An object where frTypeId=fragment type ID and frRoleId=fragment role
|
|
1170
|
+
* ID, or null.
|
|
1171
|
+
*/
|
|
1172
|
+
getFragmentTypeAndRole(roleId) {
|
|
1173
|
+
if (!roleId || !roleId.startsWith('fr.')) {
|
|
1174
|
+
return null;
|
|
1175
|
+
}
|
|
1176
|
+
const i = roleId.indexOf(':');
|
|
1177
|
+
if (i > -1) {
|
|
1178
|
+
return {
|
|
1179
|
+
frTypeId: roleId.substring(0, i),
|
|
1180
|
+
frRoleId: roleId.substring(i + 1),
|
|
1181
|
+
};
|
|
1182
|
+
}
|
|
1183
|
+
else {
|
|
1184
|
+
return {
|
|
1185
|
+
frTypeId: roleId,
|
|
1186
|
+
frRoleId: undefined,
|
|
1187
|
+
};
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
/**
|
|
1191
|
+
* Find among the specified parts definitions the one matching the specified
|
|
1192
|
+
* type ID, and eventually role ID when specified.
|
|
1193
|
+
*
|
|
1194
|
+
* @param partDefs The parts definitions.
|
|
1195
|
+
* @param typeId The type ID.
|
|
1196
|
+
* @param roleId The role ID or null.
|
|
1197
|
+
* @returns Part definition or undefined if not found.
|
|
1198
|
+
*/
|
|
1199
|
+
findPartDefinition(partDefs, typeId, roleId = null) {
|
|
1200
|
+
if (!partDefs) {
|
|
1201
|
+
return undefined;
|
|
1202
|
+
}
|
|
1203
|
+
// if role ID is specified, find both type and role
|
|
1204
|
+
if (roleId) {
|
|
1205
|
+
const reqRoleId = this.stripFragmentRoleId(roleId);
|
|
1206
|
+
return partDefs.find((d) => {
|
|
1207
|
+
return (d.typeId === typeId &&
|
|
1208
|
+
d.roleId &&
|
|
1209
|
+
this.stripFragmentRoleId(d.roleId) === reqRoleId);
|
|
1210
|
+
});
|
|
1211
|
+
}
|
|
1212
|
+
// else just find type ID
|
|
1213
|
+
return partDefs.find((d) => {
|
|
1214
|
+
return d.typeId === typeId;
|
|
1215
|
+
});
|
|
1216
|
+
}
|
|
1217
|
+
/**
|
|
1218
|
+
* Get the library editor key from the specified part type, looking up
|
|
1219
|
+
* the specified parts definitions.
|
|
1220
|
+
*
|
|
1221
|
+
* @param typeId The part's type ID.
|
|
1222
|
+
* @param roleId The part's role ID.
|
|
1223
|
+
*/
|
|
1224
|
+
getEditorKeyFromPartType(typeId, roleId) {
|
|
1225
|
+
// find the part type ID
|
|
1226
|
+
const partGroupKey = this._partEditorKeys[typeId];
|
|
1227
|
+
if (!partGroupKey) {
|
|
1228
|
+
return {
|
|
1229
|
+
partKey: 'default',
|
|
1230
|
+
};
|
|
1231
|
+
}
|
|
1232
|
+
// additionally find the role ID if specified
|
|
1233
|
+
if (roleId) {
|
|
1234
|
+
const reqRoleId = this.stripFragmentRoleId(roleId);
|
|
1235
|
+
return {
|
|
1236
|
+
partKey: partGroupKey.part,
|
|
1237
|
+
frKey: partGroupKey.fragments[reqRoleId],
|
|
1238
|
+
};
|
|
1239
|
+
}
|
|
1240
|
+
return {
|
|
1241
|
+
partKey: partGroupKey.part,
|
|
1242
|
+
};
|
|
1243
|
+
}
|
|
1244
|
+
/**
|
|
1245
|
+
* Decompose the specified editor key into part and fragment.
|
|
1246
|
+
* A simple editor key will just provide the same value for both.
|
|
1247
|
+
*
|
|
1248
|
+
* @param key The editor key to decompose.
|
|
1249
|
+
*/
|
|
1250
|
+
decomposeEditorKey(key) {
|
|
1251
|
+
const i = key.indexOf(' ');
|
|
1252
|
+
if (i > -1) {
|
|
1253
|
+
return {
|
|
1254
|
+
partKey: key.substr(0, i),
|
|
1255
|
+
frKey: key.substr(i + 1),
|
|
1256
|
+
};
|
|
1257
|
+
}
|
|
1258
|
+
else {
|
|
1259
|
+
return {
|
|
1260
|
+
partKey: key,
|
|
1261
|
+
frKey: key,
|
|
1262
|
+
};
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
/**
|
|
1266
|
+
* Build the route to the part editor corresponding to the specified
|
|
1267
|
+
* part.
|
|
1268
|
+
*
|
|
1269
|
+
* A part editing route is like
|
|
1270
|
+
* "/items/<id>/<part-group>/<part-typeid>/<part-id>?rid=<role-id>",
|
|
1271
|
+
* where the role ID is optional, and may include the fragment role ID
|
|
1272
|
+
* as a suffix preceded by a colon.
|
|
1273
|
+
*
|
|
1274
|
+
* A fragment editing route is like
|
|
1275
|
+
* "/items/<id>/<part-group>/fragment/<part-id>/<fr-typeid>/<loc>?rid=<role-id>",
|
|
1276
|
+
* where the role ID is as above, and the fragment's type ID is derived
|
|
1277
|
+
* from the part's role ID, stripping the eventual fragment's role ID out.
|
|
1278
|
+
*
|
|
1279
|
+
* @param itemId The ID of the item the parts belongs to.
|
|
1280
|
+
* @param partId The part's ID.
|
|
1281
|
+
* @param typeId The part's type ID.
|
|
1282
|
+
* @param roleId The optional part's role ID.
|
|
1283
|
+
* @returns Object with a route property and an optional rid property
|
|
1284
|
+
* representing the role ID, which will be rendered as a query parameter.
|
|
1285
|
+
*/
|
|
1286
|
+
buildPartEditorRoute(itemId, partId, typeId, roleId) {
|
|
1287
|
+
let route;
|
|
1288
|
+
const editorKey = this.getEditorKeyFromPartType(typeId);
|
|
1289
|
+
// /items/<id>/<part-group>/<part-typeid>/<part-id>?rid=<role-id>
|
|
1290
|
+
route = `/items/${itemId}/${editorKey.partKey}/${typeId}/${partId}`;
|
|
1291
|
+
return {
|
|
1292
|
+
route: route,
|
|
1293
|
+
rid: roleId,
|
|
1294
|
+
};
|
|
1295
|
+
}
|
|
1296
|
+
/**
|
|
1297
|
+
* Build the route to the fragment editor corresponding to the specified
|
|
1298
|
+
* layers part and location.
|
|
1299
|
+
*
|
|
1300
|
+
* @param partDefs The parts definitions.
|
|
1301
|
+
* @param itemId The ID of the item the parts belongs to.
|
|
1302
|
+
* @param partId The part's ID.
|
|
1303
|
+
* @param typeId The part's type ID.
|
|
1304
|
+
* @param roleId The part's role ID.
|
|
1305
|
+
* @param loc The fragment's location.
|
|
1306
|
+
* @returns Object with a route property and an optional rid property
|
|
1307
|
+
* representing the role ID, which will be rendered as a query parameter.
|
|
1308
|
+
*/
|
|
1309
|
+
buildFragmentEditorRoute(partDefs, itemId, partId, typeId, roleId, loc) {
|
|
1310
|
+
let route;
|
|
1311
|
+
const editorKey = this.getEditorKeyFromPartType(typeId, roleId);
|
|
1312
|
+
const { frTypeId, frRoleId } = this.getFragmentTypeAndRole(roleId);
|
|
1313
|
+
route = `/items/${itemId}/${editorKey.frKey}/fragment/${partId}/${frTypeId}/${loc}`;
|
|
1314
|
+
return {
|
|
1315
|
+
route: route,
|
|
1316
|
+
rid: frRoleId,
|
|
1317
|
+
};
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
LibraryRouteService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: LibraryRouteService, deps: [{ token: 'partEditorKeys' }], target: i0.ɵɵFactoryTarget.Injectable });
|
|
1321
|
+
LibraryRouteService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: LibraryRouteService, providedIn: 'root' });
|
|
1322
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: LibraryRouteService, decorators: [{
|
|
1323
|
+
type: Injectable,
|
|
1324
|
+
args: [{
|
|
1325
|
+
providedIn: 'root',
|
|
1326
|
+
}]
|
|
1327
|
+
}], ctorParameters: function () { return [{ type: undefined, decorators: [{
|
|
1328
|
+
type: Inject,
|
|
1329
|
+
args: ['partEditorKeys']
|
|
1330
|
+
}] }]; } });
|
|
1331
|
+
|
|
1332
|
+
class TextLayerService {
|
|
1333
|
+
constructor() {
|
|
1334
|
+
this._wsRegex = new RegExp('^\\s$', 'g');
|
|
1335
|
+
}
|
|
1336
|
+
isWhitespace(c) {
|
|
1337
|
+
if (!c) {
|
|
1338
|
+
return false;
|
|
1339
|
+
}
|
|
1340
|
+
if (c.length !== 1) {
|
|
1341
|
+
c = c.charAt(0);
|
|
1342
|
+
}
|
|
1343
|
+
return this._wsRegex.test(c);
|
|
1344
|
+
}
|
|
1345
|
+
/**
|
|
1346
|
+
* Extract the lines with their tokens from the specified text.
|
|
1347
|
+
* @param text the source text.
|
|
1348
|
+
* @returns lines.
|
|
1349
|
+
*/
|
|
1350
|
+
getLines(text) {
|
|
1351
|
+
const results = [];
|
|
1352
|
+
if (!text) {
|
|
1353
|
+
return results;
|
|
1354
|
+
}
|
|
1355
|
+
const lines = text.split('\n');
|
|
1356
|
+
for (let y = 0; y < lines.length; y++) {
|
|
1357
|
+
const line = lines[y].trim();
|
|
1358
|
+
const l = {
|
|
1359
|
+
y: y + 1,
|
|
1360
|
+
tokens: [],
|
|
1361
|
+
};
|
|
1362
|
+
let x = 0;
|
|
1363
|
+
let xBeg = 0;
|
|
1364
|
+
while (x < line.length) {
|
|
1365
|
+
if (this.isWhitespace(line.charAt(x))) {
|
|
1366
|
+
l.tokens.push(line.substr(xBeg, x - xBeg));
|
|
1367
|
+
x++;
|
|
1368
|
+
while (x < line.length && this.isWhitespace(line.charAt(x))) {
|
|
1369
|
+
x++;
|
|
1370
|
+
}
|
|
1371
|
+
xBeg = x;
|
|
1372
|
+
}
|
|
1373
|
+
else {
|
|
1374
|
+
x++;
|
|
1375
|
+
}
|
|
1376
|
+
}
|
|
1377
|
+
if (xBeg < x) {
|
|
1378
|
+
l.tokens.push(line.substr(xBeg));
|
|
1379
|
+
}
|
|
1380
|
+
results.push(l);
|
|
1381
|
+
}
|
|
1382
|
+
return results;
|
|
1383
|
+
}
|
|
1384
|
+
/*
|
|
1385
|
+
* Rendition: HTML rendering from a base text and a set of fragments locations.
|
|
1386
|
+
* Each line in the base text is rendered with p; each token in a line is
|
|
1387
|
+
* rendered with its text value, and tokens are separated by space. Layer
|
|
1388
|
+
* fragments are rendered inside a span, with an id equal to the fragment location
|
|
1389
|
+
* plus a suffix beginning with _, added only for the purpose of ensuring unique
|
|
1390
|
+
* IDs in the rendered HTML code. Example:
|
|
1391
|
+
* <div>
|
|
1392
|
+
* <p id="y1">this is line 1.</p>
|
|
1393
|
+
* <p id="y2">here's a <span id="f2.3_0">fragment</span>.</p>
|
|
1394
|
+
* </div>
|
|
1395
|
+
*/
|
|
1396
|
+
/**
|
|
1397
|
+
* Render the opening span for a layer fragment.
|
|
1398
|
+
* @param loc location.
|
|
1399
|
+
* @param locPortion the index of the token's portion (0-N). This is used
|
|
1400
|
+
* to build a unique ID for each HTML span rendering a layer fragment.
|
|
1401
|
+
* @param sb the target strings array.
|
|
1402
|
+
* @param isSelected true if selected.
|
|
1403
|
+
*/
|
|
1404
|
+
renderOpeningSpan(loc, locPortion, sb, isSelected) {
|
|
1405
|
+
sb.push(`<span id="f${loc.toString()}_${locPortion}" class="fr${isSelected ? 'fr-sel' : ''}">`);
|
|
1406
|
+
}
|
|
1407
|
+
/**
|
|
1408
|
+
* Render the specified token intersecting the specified end-fragment location.
|
|
1409
|
+
* @param pt The point.
|
|
1410
|
+
* @param token The token.
|
|
1411
|
+
* @param sb The target array.
|
|
1412
|
+
*/
|
|
1413
|
+
renderTokenAtEnd(pt, token, sb) {
|
|
1414
|
+
if (pt.at) {
|
|
1415
|
+
// ...to]
|
|
1416
|
+
sb.push(token.substr(pt.at - 1, pt.run));
|
|
1417
|
+
sb.push('</span>');
|
|
1418
|
+
// ...ken
|
|
1419
|
+
if (pt.at - 1 + (pt.run || 0) < token.length) {
|
|
1420
|
+
sb.push(token.substr(pt.at - 1 + (pt.run || 0)));
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
else {
|
|
1424
|
+
// ...token]
|
|
1425
|
+
sb.push(token);
|
|
1426
|
+
sb.push('</span>');
|
|
1427
|
+
}
|
|
1428
|
+
}
|
|
1429
|
+
/**
|
|
1430
|
+
* Render the specified token intersecting the specified single-point location.
|
|
1431
|
+
* The received token starts and ends a single-token range.
|
|
1432
|
+
* @param loc location.
|
|
1433
|
+
* @param token token.
|
|
1434
|
+
* @param sb target strings array.
|
|
1435
|
+
* @param isSelected True if the token is selected.
|
|
1436
|
+
*/
|
|
1437
|
+
renderTokenAtPointStart(loc, token, sb, isSelected) {
|
|
1438
|
+
// 1.token's left-part
|
|
1439
|
+
if (loc.primary.at) {
|
|
1440
|
+
// to...
|
|
1441
|
+
sb.push(token.substr(0, loc.primary.at - 1));
|
|
1442
|
+
}
|
|
1443
|
+
// 2.span: [
|
|
1444
|
+
this.renderOpeningSpan(loc, 0, sb, isSelected);
|
|
1445
|
+
// 3.token's right-part (...ken]/...ke]n or ...token])
|
|
1446
|
+
this.renderTokenAtEnd(loc.primary, token, sb);
|
|
1447
|
+
}
|
|
1448
|
+
/**
|
|
1449
|
+
* Render the specified token intersecting the specified range start location.
|
|
1450
|
+
* The received token starts a multi-tokens range.
|
|
1451
|
+
* @param loc location.
|
|
1452
|
+
* @param locPortion the index of the token's portion (0-N). This is used
|
|
1453
|
+
* to build a unique ID for each HTML span rendering a layer fragment.
|
|
1454
|
+
* @param token token.
|
|
1455
|
+
* @param sb target strings array.
|
|
1456
|
+
* @param isSelected True if the token is selected.
|
|
1457
|
+
*/
|
|
1458
|
+
renderTokenAtRangeStart(loc, token, sb, isSelected) {
|
|
1459
|
+
// 1.token's left-part (=before at)
|
|
1460
|
+
if (loc.primary.at) {
|
|
1461
|
+
// to...
|
|
1462
|
+
sb.push(token.substr(0, loc.primary.at - 1));
|
|
1463
|
+
}
|
|
1464
|
+
// 2.span: [
|
|
1465
|
+
this.renderOpeningSpan(loc, 0, sb, isSelected);
|
|
1466
|
+
// 3.token's right-part (...ken or ...token)
|
|
1467
|
+
if (loc.primary.at) {
|
|
1468
|
+
// ...ken
|
|
1469
|
+
sb.push(token.substr(loc.primary.at - 1));
|
|
1470
|
+
}
|
|
1471
|
+
else {
|
|
1472
|
+
// ...token
|
|
1473
|
+
sb.push(token);
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
renderTokenAtStart(loc, token, sb, isSelected) {
|
|
1477
|
+
if (!loc.secondary) {
|
|
1478
|
+
// single
|
|
1479
|
+
this.renderTokenAtPointStart(loc, token, sb, isSelected);
|
|
1480
|
+
return false;
|
|
1481
|
+
}
|
|
1482
|
+
else {
|
|
1483
|
+
// range
|
|
1484
|
+
this.renderTokenAtRangeStart(loc, token, sb, isSelected);
|
|
1485
|
+
return true;
|
|
1486
|
+
}
|
|
1487
|
+
}
|
|
1488
|
+
/**
|
|
1489
|
+
* Render to HTML the base text decorated with its layer fragments.
|
|
1490
|
+
* @param text The base text.
|
|
1491
|
+
* @param locations The locations of all the fragments
|
|
1492
|
+
* of the same type to be rendered on top of the base text.
|
|
1493
|
+
* @param selectedLoc The optional location of the selected
|
|
1494
|
+
* fragment if any. The selected fragment gets decorated with an additional
|
|
1495
|
+
* class value.
|
|
1496
|
+
*/
|
|
1497
|
+
render(text, locations, selectedLoc) {
|
|
1498
|
+
if (!text) {
|
|
1499
|
+
return '<div></div>';
|
|
1500
|
+
}
|
|
1501
|
+
// get lines from text
|
|
1502
|
+
const sb = [];
|
|
1503
|
+
const lines = this.getLines(text);
|
|
1504
|
+
let loc = undefined;
|
|
1505
|
+
let tokenSelected = false;
|
|
1506
|
+
if (!locations) {
|
|
1507
|
+
locations = [];
|
|
1508
|
+
}
|
|
1509
|
+
// for each line in text
|
|
1510
|
+
sb.push('<div>');
|
|
1511
|
+
for (let y = 0; y < lines.length; y++) {
|
|
1512
|
+
sb.push(`<p id="y${y + 1}">`);
|
|
1513
|
+
// reopen a span if continuing a fragment from prev line
|
|
1514
|
+
if (loc) {
|
|
1515
|
+
this.renderOpeningSpan(loc, y + 1 - loc.primary.y, sb, tokenSelected);
|
|
1516
|
+
}
|
|
1517
|
+
// for each token in line
|
|
1518
|
+
const line = lines[y];
|
|
1519
|
+
for (let x = 0; x < line.tokens.length; x++) {
|
|
1520
|
+
// add separator if required
|
|
1521
|
+
if (x > 0) {
|
|
1522
|
+
sb.push(' ');
|
|
1523
|
+
}
|
|
1524
|
+
const token = line.tokens[x];
|
|
1525
|
+
// if inside a fragment, check if this token is the last
|
|
1526
|
+
if (loc && loc.secondary) {
|
|
1527
|
+
if (loc.secondary.y === y + 1 && loc.secondary.x === x + 1) {
|
|
1528
|
+
this.renderTokenAtEnd(loc.secondary, token, sb);
|
|
1529
|
+
loc = undefined;
|
|
1530
|
+
}
|
|
1531
|
+
else {
|
|
1532
|
+
sb.push(token);
|
|
1533
|
+
}
|
|
1534
|
+
}
|
|
1535
|
+
else {
|
|
1536
|
+
// else, check if this token starts a fragment
|
|
1537
|
+
loc = locations.find((l) => {
|
|
1538
|
+
return l.primary.y === y + 1 && l.primary.x === x + 1;
|
|
1539
|
+
});
|
|
1540
|
+
if (loc) {
|
|
1541
|
+
tokenSelected = selectedLoc ? loc.overlaps(selectedLoc) : false;
|
|
1542
|
+
if (!this.renderTokenAtStart(loc, token, sb, tokenSelected)) {
|
|
1543
|
+
loc = undefined;
|
|
1544
|
+
}
|
|
1545
|
+
}
|
|
1546
|
+
else {
|
|
1547
|
+
sb.push(token);
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
// close a span if pending at p end
|
|
1552
|
+
if (loc) {
|
|
1553
|
+
sb.push('</span>');
|
|
1554
|
+
}
|
|
1555
|
+
sb.push('</p>');
|
|
1556
|
+
}
|
|
1557
|
+
sb.push('</div>');
|
|
1558
|
+
return sb.join('');
|
|
1559
|
+
}
|
|
1560
|
+
/*
|
|
1561
|
+
Location: starting from user selection on a rendered text, get the corresponding
|
|
1562
|
+
layer fragment location. This differs according to the purpose of the location
|
|
1563
|
+
to be got: if getting a location for an existing fragment, at least 1 decorated
|
|
1564
|
+
text (span) should be included in the selection, which does not require to be
|
|
1565
|
+
exact; if getting a location for a new fragment, no decorated text (span) can be
|
|
1566
|
+
included in the selection (two fragments in the same layer can never overlap),
|
|
1567
|
+
which must be exact.
|
|
1568
|
+
*/
|
|
1569
|
+
/**
|
|
1570
|
+
* Get the Y bounds from the specified selection.
|
|
1571
|
+
* @param range The selection range.
|
|
1572
|
+
* @param forNew True if getting bounds for a new fragment.
|
|
1573
|
+
* @returns Range, inclusive, or undefined.
|
|
1574
|
+
*/
|
|
1575
|
+
getYBoundsFromRange(range, forNew) {
|
|
1576
|
+
let p = null;
|
|
1577
|
+
switch (range.commonAncestorContainer.nodeType) {
|
|
1578
|
+
case Node.TEXT_NODE:
|
|
1579
|
+
// single-p selection, parent=p
|
|
1580
|
+
p = range.commonAncestorContainer.parentElement;
|
|
1581
|
+
if (forNew && (p?.nodeName !== 'P' || !p.hasAttribute('id'))) {
|
|
1582
|
+
return null;
|
|
1583
|
+
}
|
|
1584
|
+
if (p?.nodeName === 'SPAN') {
|
|
1585
|
+
p = p.parentElement;
|
|
1586
|
+
}
|
|
1587
|
+
const y = parseInt(p.getAttribute('id').substr(1), 10);
|
|
1588
|
+
// return [y, y];
|
|
1589
|
+
return { start: y, end: y };
|
|
1590
|
+
case Node.ELEMENT_NODE:
|
|
1591
|
+
// div only is allowed; in this case, it's a multi-p range
|
|
1592
|
+
if (range.commonAncestorContainer.nodeName !== 'DIV') {
|
|
1593
|
+
return null;
|
|
1594
|
+
}
|
|
1595
|
+
// for each div's child p element:
|
|
1596
|
+
let yMin = 0;
|
|
1597
|
+
let yMax = 0;
|
|
1598
|
+
let inside = false;
|
|
1599
|
+
for (let i = 0; i < range.commonAncestorContainer.childNodes.length; i++) {
|
|
1600
|
+
const child = range.commonAncestorContainer.childNodes[i];
|
|
1601
|
+
if (child.nodeType !== Node.ELEMENT_NODE || child.nodeName !== 'P') {
|
|
1602
|
+
continue;
|
|
1603
|
+
}
|
|
1604
|
+
// check if we're entering the start-p
|
|
1605
|
+
if (!inside && child.contains(range.startContainer)) {
|
|
1606
|
+
inside = true;
|
|
1607
|
+
}
|
|
1608
|
+
// collect y if inside
|
|
1609
|
+
if (inside) {
|
|
1610
|
+
const iy = parseInt(child.getAttribute('id').substr(1), 10);
|
|
1611
|
+
if (!yMin || iy < yMin) {
|
|
1612
|
+
yMin = iy;
|
|
1613
|
+
}
|
|
1614
|
+
if (!yMax || iy > yMax) {
|
|
1615
|
+
yMax = iy;
|
|
1616
|
+
}
|
|
1617
|
+
// check if we have exited the last p
|
|
1618
|
+
if (child.contains(range.endContainer)) {
|
|
1619
|
+
break;
|
|
1620
|
+
}
|
|
1621
|
+
}
|
|
1622
|
+
}
|
|
1623
|
+
return {
|
|
1624
|
+
start: yMin,
|
|
1625
|
+
end: yMax,
|
|
1626
|
+
};
|
|
1627
|
+
}
|
|
1628
|
+
return null;
|
|
1629
|
+
}
|
|
1630
|
+
locatePrevSpace(line, index) {
|
|
1631
|
+
while (index > -1 && line.charAt(index) !== ' ') {
|
|
1632
|
+
index--;
|
|
1633
|
+
}
|
|
1634
|
+
return index;
|
|
1635
|
+
}
|
|
1636
|
+
locateNextSpace(line, index) {
|
|
1637
|
+
while (index < line.length && line.charAt(index) !== ' ') {
|
|
1638
|
+
index++;
|
|
1639
|
+
}
|
|
1640
|
+
return index === line.length ? -1 : index;
|
|
1641
|
+
}
|
|
1642
|
+
/**
|
|
1643
|
+
* Get the bounds of the token any character of which is indexed
|
|
1644
|
+
* by index.
|
|
1645
|
+
* @param line The text line.
|
|
1646
|
+
* @param index The index to the text line to start from.
|
|
1647
|
+
* @returns Bounds where start=index of the first character of the token,
|
|
1648
|
+
* and end=index of the character past the token's last character;
|
|
1649
|
+
* or null if index was not valid.
|
|
1650
|
+
*/
|
|
1651
|
+
getTokenBounds(line, index) {
|
|
1652
|
+
if (line.charAt(index) === ' ') {
|
|
1653
|
+
return null;
|
|
1654
|
+
}
|
|
1655
|
+
let start = this.locatePrevSpace(line, index);
|
|
1656
|
+
if (start === -1) {
|
|
1657
|
+
start = 0;
|
|
1658
|
+
}
|
|
1659
|
+
else {
|
|
1660
|
+
start++;
|
|
1661
|
+
}
|
|
1662
|
+
let end = this.locateNextSpace(line, index);
|
|
1663
|
+
if (end === -1) {
|
|
1664
|
+
end = line.length;
|
|
1665
|
+
}
|
|
1666
|
+
// return [start, end];
|
|
1667
|
+
return {
|
|
1668
|
+
start,
|
|
1669
|
+
end,
|
|
1670
|
+
};
|
|
1671
|
+
}
|
|
1672
|
+
/**
|
|
1673
|
+
* Return the received selection bounds, adjusted to exclude spaces.
|
|
1674
|
+
*/
|
|
1675
|
+
getAdjustedSelectionBounds(selStart, selEnd, line) {
|
|
1676
|
+
// corner case: if on space, move to right
|
|
1677
|
+
if (line.charAt(selStart) === ' ') {
|
|
1678
|
+
selStart++;
|
|
1679
|
+
}
|
|
1680
|
+
// corner case: if included trailing space, move to left
|
|
1681
|
+
if (selEnd > 0 && line.charAt(selEnd - 1) === ' ') {
|
|
1682
|
+
selEnd--;
|
|
1683
|
+
}
|
|
1684
|
+
// return [selStart, selEnd];
|
|
1685
|
+
return {
|
|
1686
|
+
start: selStart,
|
|
1687
|
+
end: selEnd,
|
|
1688
|
+
};
|
|
1689
|
+
}
|
|
1690
|
+
/**
|
|
1691
|
+
* Get the start coordinates for the selected range and line.
|
|
1692
|
+
* @param range The selected range.
|
|
1693
|
+
* @param line The reference text line.
|
|
1694
|
+
*/
|
|
1695
|
+
getStartCoordsFromRange(selStart, selEnd, line) {
|
|
1696
|
+
// get adjusted selection bounds
|
|
1697
|
+
const selBounds = this.getAdjustedSelectionBounds(selStart, selEnd, line);
|
|
1698
|
+
// get the bounds of the first selected token
|
|
1699
|
+
const tokBounds = this.getTokenBounds(line, selBounds.start);
|
|
1700
|
+
// calculate x by counting the tokens before the target token
|
|
1701
|
+
let x = 1;
|
|
1702
|
+
let j = tokBounds.start - 1;
|
|
1703
|
+
while (j > -1) {
|
|
1704
|
+
if (line.charAt(j) === ' ') {
|
|
1705
|
+
x++;
|
|
1706
|
+
}
|
|
1707
|
+
j--;
|
|
1708
|
+
}
|
|
1709
|
+
// calculate at,run only when either of these is true:
|
|
1710
|
+
// (a) selection starts past the 1st char of the token (al[pha);
|
|
1711
|
+
// (b) selection ends before the last char of the token (al]pha);
|
|
1712
|
+
// run is calculated only if (b) is true.
|
|
1713
|
+
let at = 0;
|
|
1714
|
+
let run = 0;
|
|
1715
|
+
if (selBounds.start > tokBounds.start || selBounds.end < tokBounds.end) {
|
|
1716
|
+
at = 1 + selBounds.start - tokBounds.start;
|
|
1717
|
+
run =
|
|
1718
|
+
(selBounds.end < tokBounds.end
|
|
1719
|
+
? selBounds.end // al]pha
|
|
1720
|
+
: tokBounds.end) - // alpha]
|
|
1721
|
+
Math.max(tokBounds.start, selBounds.start);
|
|
1722
|
+
}
|
|
1723
|
+
return {
|
|
1724
|
+
y: 0,
|
|
1725
|
+
x,
|
|
1726
|
+
at,
|
|
1727
|
+
run,
|
|
1728
|
+
};
|
|
1729
|
+
}
|
|
1730
|
+
getEndCoordsFromRange(selStart, selEnd, line) {
|
|
1731
|
+
// get adjusted selection bounds
|
|
1732
|
+
const selBounds = this.getAdjustedSelectionBounds(selStart, selEnd, line);
|
|
1733
|
+
// get the bounds of the last selected token
|
|
1734
|
+
const tokBounds = this.getTokenBounds(line, selBounds.end - 1);
|
|
1735
|
+
// calculate x by counting the tokens before the target token
|
|
1736
|
+
let x = 1;
|
|
1737
|
+
let j = selBounds.end - 1;
|
|
1738
|
+
while (j > -1) {
|
|
1739
|
+
if (line.charAt(j) === ' ') {
|
|
1740
|
+
x++;
|
|
1741
|
+
}
|
|
1742
|
+
j--;
|
|
1743
|
+
}
|
|
1744
|
+
// calculate at,run only when selection ends before the 1st char of the token
|
|
1745
|
+
// (al]pha); run is calculated only if at is calculated.
|
|
1746
|
+
let at = 0;
|
|
1747
|
+
let run = 0;
|
|
1748
|
+
if (selBounds.end < tokBounds.end) {
|
|
1749
|
+
at =
|
|
1750
|
+
selBounds.start > tokBounds.start
|
|
1751
|
+
? 1 + selBounds.start - tokBounds.start
|
|
1752
|
+
: 1;
|
|
1753
|
+
run = selBounds.end - tokBounds.start;
|
|
1754
|
+
}
|
|
1755
|
+
return {
|
|
1756
|
+
y: 0,
|
|
1757
|
+
x,
|
|
1758
|
+
at,
|
|
1759
|
+
run,
|
|
1760
|
+
};
|
|
1761
|
+
}
|
|
1762
|
+
getSpanLocFromId(id) {
|
|
1763
|
+
if (!id) {
|
|
1764
|
+
return null;
|
|
1765
|
+
}
|
|
1766
|
+
const r = new RegExp('^f([-\\d+.@x]+)', 'g');
|
|
1767
|
+
const m = r.exec(id);
|
|
1768
|
+
if (!m) {
|
|
1769
|
+
return null;
|
|
1770
|
+
}
|
|
1771
|
+
return TokenLocation.parse(m[1]);
|
|
1772
|
+
}
|
|
1773
|
+
visitNodes(node, visitor) {
|
|
1774
|
+
if (!visitor(node)) {
|
|
1775
|
+
return;
|
|
1776
|
+
}
|
|
1777
|
+
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
1778
|
+
for (let i = 0; i < node.childNodes.length; i++) {
|
|
1779
|
+
const child = node.childNodes[i];
|
|
1780
|
+
this.visitNodes(child, visitor);
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1783
|
+
}
|
|
1784
|
+
findFirstDescendantSpan(range) {
|
|
1785
|
+
let inside = false;
|
|
1786
|
+
let span = null;
|
|
1787
|
+
this.visitNodes(range.commonAncestorContainer, (current) => {
|
|
1788
|
+
if (current === range.startContainer) {
|
|
1789
|
+
inside = true;
|
|
1790
|
+
}
|
|
1791
|
+
if (current.nodeType === Node.ELEMENT_NODE &&
|
|
1792
|
+
current.nodeName === 'SPAN') {
|
|
1793
|
+
span = current;
|
|
1794
|
+
return false;
|
|
1795
|
+
}
|
|
1796
|
+
if (current === range.endContainer) {
|
|
1797
|
+
inside = false;
|
|
1798
|
+
}
|
|
1799
|
+
return true;
|
|
1800
|
+
});
|
|
1801
|
+
return span;
|
|
1802
|
+
}
|
|
1803
|
+
/**
|
|
1804
|
+
* Get the base offset for the specified node. This is used
|
|
1805
|
+
* when the start container of the selected range has any
|
|
1806
|
+
* preceding sibling node, which happens only when you have
|
|
1807
|
+
* any spans in the p element (=the line). In this case,
|
|
1808
|
+
* the selection offset starts from the start container node,
|
|
1809
|
+
* which is preceded by other nodes including other text.
|
|
1810
|
+
* As we must get the line-based offset, we have to start
|
|
1811
|
+
* from a base offset equal to the sum of the lengths of
|
|
1812
|
+
* all the preceding siblings.
|
|
1813
|
+
*/
|
|
1814
|
+
getNodeBaseOffset(node) {
|
|
1815
|
+
let offset = 0;
|
|
1816
|
+
let n = node;
|
|
1817
|
+
n = node.previousSibling;
|
|
1818
|
+
while (n) {
|
|
1819
|
+
offset +=
|
|
1820
|
+
n.nodeType === Node.ELEMENT_NODE
|
|
1821
|
+
? n.innerText?.length || 0
|
|
1822
|
+
: n.textContent?.length || 0;
|
|
1823
|
+
n = n.previousSibling;
|
|
1824
|
+
}
|
|
1825
|
+
return offset;
|
|
1826
|
+
}
|
|
1827
|
+
/**
|
|
1828
|
+
* Get the location corresponding to the user selection,
|
|
1829
|
+
* for editing the first fragment included in it.
|
|
1830
|
+
* @param range The selected range. You can get it from the
|
|
1831
|
+
* current selection using getSelectedRange().
|
|
1832
|
+
* @returns The token location or null.
|
|
1833
|
+
*/
|
|
1834
|
+
getSelectedLocationForEdit(range) {
|
|
1835
|
+
if (!range) {
|
|
1836
|
+
return null;
|
|
1837
|
+
}
|
|
1838
|
+
// just find the first descendant span in selection
|
|
1839
|
+
let span = null;
|
|
1840
|
+
switch (range.commonAncestorContainer.nodeType) {
|
|
1841
|
+
case Node.ELEMENT_NODE:
|
|
1842
|
+
span = range.commonAncestorContainer;
|
|
1843
|
+
if (span.nodeName !== 'SPAN') {
|
|
1844
|
+
span = this.findFirstDescendantSpan(range);
|
|
1845
|
+
}
|
|
1846
|
+
break;
|
|
1847
|
+
case Node.TEXT_NODE:
|
|
1848
|
+
span = range.commonAncestorContainer.parentElement;
|
|
1849
|
+
if (span &&
|
|
1850
|
+
(span.nodeType !== Node.ELEMENT_NODE || span.nodeName !== 'SPAN')) {
|
|
1851
|
+
span = null;
|
|
1852
|
+
}
|
|
1853
|
+
break;
|
|
1854
|
+
}
|
|
1855
|
+
return span ? this.getSpanLocFromId(span.getAttribute('id')) : null;
|
|
1856
|
+
}
|
|
1857
|
+
textLineToString(line) {
|
|
1858
|
+
if (!line || !line.tokens) {
|
|
1859
|
+
return '';
|
|
1860
|
+
}
|
|
1861
|
+
return line.tokens.join(' ');
|
|
1862
|
+
}
|
|
1863
|
+
buildCoords(start, end, singleToken) {
|
|
1864
|
+
const sb = [];
|
|
1865
|
+
// start: y.x
|
|
1866
|
+
sb.push(`${start.y}.${start.x}`);
|
|
1867
|
+
if (singleToken) {
|
|
1868
|
+
// single token: check for at,run
|
|
1869
|
+
if (start.at) {
|
|
1870
|
+
sb.push(`@${start.at}`);
|
|
1871
|
+
if (start.run && start.run > 1) {
|
|
1872
|
+
sb.push(`x${start.run}`);
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
}
|
|
1876
|
+
else {
|
|
1877
|
+
// not a single token
|
|
1878
|
+
// first token
|
|
1879
|
+
if (start.at) {
|
|
1880
|
+
sb.push(`@${start.at}x${start.run}`);
|
|
1881
|
+
}
|
|
1882
|
+
// last token
|
|
1883
|
+
sb.push(`-${end.y}.${end.x}`);
|
|
1884
|
+
if (end.at) {
|
|
1885
|
+
sb.push(`@1x${end.run}`);
|
|
1886
|
+
}
|
|
1887
|
+
}
|
|
1888
|
+
return sb.join('');
|
|
1889
|
+
}
|
|
1890
|
+
/**
|
|
1891
|
+
* Recursively find the node matching the specified matcher
|
|
1892
|
+
* function, starting from the specified node (included).
|
|
1893
|
+
*
|
|
1894
|
+
* @param node The node to start from.
|
|
1895
|
+
* @param matcher The matcher function, which receives a node
|
|
1896
|
+
* to match, and returns 1=match, 0=no match, -1=stop the
|
|
1897
|
+
* find operation with a no match.
|
|
1898
|
+
*/
|
|
1899
|
+
findNode(node, matcher) {
|
|
1900
|
+
// self
|
|
1901
|
+
const n = matcher(node);
|
|
1902
|
+
if (n === 1) {
|
|
1903
|
+
return node;
|
|
1904
|
+
}
|
|
1905
|
+
if (n === -1) {
|
|
1906
|
+
return null;
|
|
1907
|
+
}
|
|
1908
|
+
// children
|
|
1909
|
+
if (node.hasChildNodes()) {
|
|
1910
|
+
for (let i = 0; i < node.childNodes.length; i++) {
|
|
1911
|
+
const child = node.childNodes[i];
|
|
1912
|
+
const found = this.findNode(child, matcher);
|
|
1913
|
+
if (found) {
|
|
1914
|
+
return found;
|
|
1915
|
+
}
|
|
1916
|
+
}
|
|
1917
|
+
}
|
|
1918
|
+
return null;
|
|
1919
|
+
}
|
|
1920
|
+
selHasAnySpan(range) {
|
|
1921
|
+
let inside = false;
|
|
1922
|
+
if (
|
|
1923
|
+
// the selected text node must not be child of a span
|
|
1924
|
+
range.startContainer.parentElement?.tagName === 'SPAN' ||
|
|
1925
|
+
range.startContainer.parentElement?.tagName === 'SPAN' ||
|
|
1926
|
+
// the nodes tree from the common ancestor, from the start node
|
|
1927
|
+
// up to the end node, must not include any span
|
|
1928
|
+
this.findNode(range.commonAncestorContainer, (node) => {
|
|
1929
|
+
// start checking only when start node reached
|
|
1930
|
+
if (node === range.startContainer) {
|
|
1931
|
+
inside = true;
|
|
1932
|
+
}
|
|
1933
|
+
if (!inside) {
|
|
1934
|
+
return 0;
|
|
1935
|
+
}
|
|
1936
|
+
// stop checking when end node reached
|
|
1937
|
+
if (node === range.endContainer) {
|
|
1938
|
+
return -1;
|
|
1939
|
+
}
|
|
1940
|
+
// node cannot be a span
|
|
1941
|
+
if (node.nodeType === Node.ELEMENT_NODE &&
|
|
1942
|
+
node.tagName === 'SPAN') {
|
|
1943
|
+
return 1;
|
|
1944
|
+
}
|
|
1945
|
+
return 0;
|
|
1946
|
+
})) {
|
|
1947
|
+
return true;
|
|
1948
|
+
}
|
|
1949
|
+
return false;
|
|
1950
|
+
}
|
|
1951
|
+
/**
|
|
1952
|
+
* Get the location corresponding to the user selection,
|
|
1953
|
+
* for adding a new fragment with the same extension.
|
|
1954
|
+
* @param range The selected range. You can get it from the
|
|
1955
|
+
* current selection using getSelectedRange().
|
|
1956
|
+
* @param text The full base text.
|
|
1957
|
+
* @returns TokenLocation The token location or undefined.
|
|
1958
|
+
*/
|
|
1959
|
+
getSelectedLocationForNew(range, text) {
|
|
1960
|
+
if (!range || this.selHasAnySpan(range)) {
|
|
1961
|
+
return null;
|
|
1962
|
+
}
|
|
1963
|
+
// get y bounds (min, max; equal if single-line)
|
|
1964
|
+
const yBounds = this.getYBoundsFromRange(range, true);
|
|
1965
|
+
if (!yBounds) {
|
|
1966
|
+
return null;
|
|
1967
|
+
}
|
|
1968
|
+
// get lines and calculate start/end coords
|
|
1969
|
+
const lines = this.getLines(text);
|
|
1970
|
+
const startLine = this.textLineToString(lines[yBounds.start - 1]);
|
|
1971
|
+
const endLine = this.textLineToString(lines[yBounds.end - 1]);
|
|
1972
|
+
// get the base offsets
|
|
1973
|
+
const startBaseOffset = this.getNodeBaseOffset(range.startContainer);
|
|
1974
|
+
const endBaseOffset = this.getNodeBaseOffset(range.endContainer);
|
|
1975
|
+
// get the start (including the base offset)
|
|
1976
|
+
const startLineEndOffset = yBounds.start !== yBounds.end
|
|
1977
|
+
? startLine.length
|
|
1978
|
+
: range.endOffset + startBaseOffset;
|
|
1979
|
+
const start = this.getStartCoordsFromRange(range.startOffset + startBaseOffset, startLineEndOffset, startLine);
|
|
1980
|
+
start.y = yBounds.start;
|
|
1981
|
+
// get the end (including the base offset)
|
|
1982
|
+
const end = yBounds.start !== yBounds.end
|
|
1983
|
+
? this.getEndCoordsFromRange(0, range.endOffset + endBaseOffset, endLine)
|
|
1984
|
+
: this.getEndCoordsFromRange(range.startOffset + endBaseOffset, range.endOffset + endBaseOffset, endLine);
|
|
1985
|
+
end.y = yBounds.end;
|
|
1986
|
+
const singleLine = start.y === end.y;
|
|
1987
|
+
const singleToken = singleLine && start.x === end.x;
|
|
1988
|
+
const coords = this.buildCoords(start, end, singleToken);
|
|
1989
|
+
return TokenLocation.parse(coords);
|
|
1990
|
+
}
|
|
1991
|
+
/**
|
|
1992
|
+
* Get the currently selected range in the browser's document.
|
|
1993
|
+
* This uses the rangy library.
|
|
1994
|
+
*/
|
|
1995
|
+
getSelectedRange() {
|
|
1996
|
+
const sel = rangy.getSelection();
|
|
1997
|
+
if (sel.isCollapsed) {
|
|
1998
|
+
return null;
|
|
1999
|
+
}
|
|
2000
|
+
return sel.getRangeAt(0);
|
|
2001
|
+
}
|
|
2002
|
+
}
|
|
2003
|
+
TextLayerService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: TextLayerService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
2004
|
+
TextLayerService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: TextLayerService, providedIn: 'root' });
|
|
2005
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: TextLayerService, decorators: [{
|
|
2006
|
+
type: Injectable,
|
|
2007
|
+
args: [{ providedIn: 'root' }]
|
|
2008
|
+
}], ctorParameters: function () { return []; } });
|
|
2009
|
+
|
|
2010
|
+
// https://github.com/angular/angular/blob/3b7d4ebbd6445364889d61b7b1b1c65206534d34/packages/forms/src/validators.ts
|
|
2011
|
+
// https://indepth.dev/posts/1319/the-best-way-to-implement-custom-validators
|
|
2012
|
+
/**
|
|
2013
|
+
* Cadmus general-purpose custom validators.
|
|
2014
|
+
*/
|
|
2015
|
+
// @dynamic
|
|
2016
|
+
class CadmusValidators {
|
|
2017
|
+
static hasValidLength(value) {
|
|
2018
|
+
// non-strict comparison is intentional,
|
|
2019
|
+
// to check for both `null` and `undefined` values
|
|
2020
|
+
return value != null && typeof value.length === 'number';
|
|
2021
|
+
}
|
|
2022
|
+
/**
|
|
2023
|
+
* Validate an array or string for a minimum required length.
|
|
2024
|
+
* Differently from the standard minLength validator, this does
|
|
2025
|
+
* not exclude 0 from the values to be checked. The standard
|
|
2026
|
+
* validator instead does not validate the value if this is 0.
|
|
2027
|
+
*
|
|
2028
|
+
* @param minLength Minimum length required.
|
|
2029
|
+
* @returns Null if valid, else an object with minlength with
|
|
2030
|
+
* requiredLength and actualLength.
|
|
2031
|
+
*/
|
|
2032
|
+
static strictMinLengthValidator(minLength) {
|
|
2033
|
+
return (control) => {
|
|
2034
|
+
// don't validate values without `length` property
|
|
2035
|
+
if (!this.hasValidLength(control.value)) {
|
|
2036
|
+
return null;
|
|
2037
|
+
}
|
|
2038
|
+
return control.value.length < minLength
|
|
2039
|
+
? {
|
|
2040
|
+
minlength: {
|
|
2041
|
+
requiredLength: minLength,
|
|
2042
|
+
actualLength: control.value.length,
|
|
2043
|
+
},
|
|
2044
|
+
}
|
|
2045
|
+
: null;
|
|
2046
|
+
};
|
|
2047
|
+
}
|
|
2048
|
+
/**
|
|
2049
|
+
* Conditionally validate using the specified validator
|
|
2050
|
+
* when predicate is true.
|
|
2051
|
+
* See https://medium.com/ngx/3-ways-to-implement-conditional-validation-of-reactive-forms-c59ed6fc3325.
|
|
2052
|
+
*
|
|
2053
|
+
* @param predicate The predicate function to use when determining
|
|
2054
|
+
* if the validator should be used.
|
|
2055
|
+
* @param validator The conditional validator to use.
|
|
2056
|
+
* @returns Validation results.
|
|
2057
|
+
*/
|
|
2058
|
+
static conditionalValidator(predicate, validator) {
|
|
2059
|
+
return (control) => {
|
|
2060
|
+
if (!control.parent) {
|
|
2061
|
+
return null;
|
|
2062
|
+
}
|
|
2063
|
+
if (predicate()) {
|
|
2064
|
+
return validator(control);
|
|
2065
|
+
}
|
|
2066
|
+
return null;
|
|
2067
|
+
};
|
|
2068
|
+
}
|
|
2069
|
+
}
|
|
2070
|
+
|
|
2071
|
+
class PasswordValidator {
|
|
2072
|
+
static isValidPassword(control) {
|
|
2073
|
+
if (!control.value) {
|
|
2074
|
+
return null;
|
|
2075
|
+
}
|
|
2076
|
+
if (control.value.length < 8) {
|
|
2077
|
+
return {
|
|
2078
|
+
passwordTooShort: true,
|
|
2079
|
+
};
|
|
2080
|
+
}
|
|
2081
|
+
if (!/.*[A-Z].*/.test(control.value)) {
|
|
2082
|
+
return {
|
|
2083
|
+
noUpperInPassword: true,
|
|
2084
|
+
};
|
|
2085
|
+
}
|
|
2086
|
+
if (!/.*[a-z].*/.test(control.value)) {
|
|
2087
|
+
return {
|
|
2088
|
+
noLowerInPassword: true,
|
|
2089
|
+
};
|
|
2090
|
+
}
|
|
2091
|
+
if (!/.*[A-Z].*/.test(control.value)) {
|
|
2092
|
+
return {
|
|
2093
|
+
noUpperInPassword: true,
|
|
2094
|
+
};
|
|
2095
|
+
}
|
|
2096
|
+
if (!/.*[-`~!@#$%^&*()_+=\[\]{};:'",.<>/?|\\].*/.test(control.value)) {
|
|
2097
|
+
return {
|
|
2098
|
+
noSymbolInPassword: true,
|
|
2099
|
+
};
|
|
2100
|
+
}
|
|
2101
|
+
return null;
|
|
2102
|
+
}
|
|
2103
|
+
}
|
|
2104
|
+
|
|
2105
|
+
class CadmusCoreModule {
|
|
2106
|
+
}
|
|
2107
|
+
CadmusCoreModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: CadmusCoreModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
|
|
2108
|
+
CadmusCoreModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: CadmusCoreModule, declarations: [SortPipe], imports: [CommonModule, HttpClientModule, NgToolsModule], exports: [SortPipe] });
|
|
2109
|
+
CadmusCoreModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: CadmusCoreModule, imports: [[CommonModule, HttpClientModule, NgToolsModule]] });
|
|
2110
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: CadmusCoreModule, decorators: [{
|
|
2111
|
+
type: NgModule,
|
|
2112
|
+
args: [{
|
|
2113
|
+
imports: [CommonModule, HttpClientModule, NgToolsModule],
|
|
2114
|
+
declarations: [SortPipe],
|
|
2115
|
+
exports: [SortPipe],
|
|
2116
|
+
}]
|
|
2117
|
+
}] });
|
|
2118
|
+
|
|
2119
|
+
/*
|
|
2120
|
+
* Public API Surface of cadmus-core
|
|
2121
|
+
*/
|
|
2122
|
+
|
|
2123
|
+
/**
|
|
2124
|
+
* Generated bundle index. Do not edit.
|
|
2125
|
+
*/
|
|
2126
|
+
|
|
2127
|
+
export { CadmusCoreModule, CadmusValidators, DATATION_FORMAT_OPTIONS, DataPinValueType, Datation, GravatarService, HistoricalDate, HistoricalDateType, LibraryRouteService, PasswordValidator, PendingChangesGuard, SortPipe, TextLayerService, TextRange, TokenLocation, TokenPoint };
|