@myrmidon/cadmus-core 0.0.24 → 1.0.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 +2 -2
- package/esm2020/lib/cadmus-core.module.mjs +20 -0
- package/esm2020/lib/guards/pending-changes.guard.mjs +24 -0
- package/esm2020/lib/models.mjs +11 -0
- package/esm2020/lib/pipes/sort.pipe.mjs +42 -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/{esm2015/lib/validators/cadmus-validators.js → esm2020/lib/validators/cadmus-validators.mjs} +0 -0
- package/{esm2015/myrmidon-cadmus-core.js → esm2020/myrmidon-cadmus-core.mjs} +0 -0
- package/esm2020/public-api.mjs +14 -0
- package/fesm2015/myrmidon-cadmus-core.mjs +1252 -0
- package/fesm2015/myrmidon-cadmus-core.mjs.map +1 -0
- package/fesm2020/myrmidon-cadmus-core.mjs +1248 -0
- package/fesm2020/myrmidon-cadmus-core.mjs.map +1 -0
- package/lib/cadmus-core.module.d.ts +8 -0
- package/lib/guards/pending-changes.guard.d.ts +3 -0
- package/lib/models.d.ts +1 -66
- package/lib/pipes/sort.pipe.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/myrmidon-cadmus-core.d.ts +1 -0
- package/package.json +25 -13
- package/public-api.d.ts +0 -10
- package/bundles/myrmidon-cadmus-core.umd.js +0 -2964
- 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/password.validator.js +0 -34
- package/esm2015/public-api.js +0 -25
- package/fesm2015/myrmidon-cadmus-core.js +0 -2535
- package/fesm2015/myrmidon-cadmus-core.js.map +0 -1
- package/lib/datation.d.ts +0 -157
- package/lib/historical-date.d.ts +0 -84
- 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/gravatar.service.d.ts +0 -9
- package/lib/services/local-storage.service.d.ts +0 -32
- package/lib/utils.d.ts +0 -4
- package/lib/validators/password.validator.d.ts +0 -6
- package/myrmidon-cadmus-core.metadata.json +0 -1
|
@@ -0,0 +1,1252 @@
|
|
|
1
|
+
import * as i0 from '@angular/core';
|
|
2
|
+
import { Injectable, Pipe, Inject, NgModule } from '@angular/core';
|
|
3
|
+
import * as rangy from 'rangy';
|
|
4
|
+
import { CommonModule } from '@angular/common';
|
|
5
|
+
import { HttpClientModule } from '@angular/common/http';
|
|
6
|
+
import { NgToolsModule } from '@myrmidon/ng-tools';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Data pin value type in a DataPinDefinition.
|
|
10
|
+
*/
|
|
11
|
+
var DataPinValueType;
|
|
12
|
+
(function (DataPinValueType) {
|
|
13
|
+
DataPinValueType[DataPinValueType["string"] = 0] = "string";
|
|
14
|
+
DataPinValueType[DataPinValueType["boolean"] = 1] = "boolean";
|
|
15
|
+
DataPinValueType[DataPinValueType["integer"] = 2] = "integer";
|
|
16
|
+
DataPinValueType[DataPinValueType["decimal"] = 3] = "decimal";
|
|
17
|
+
})(DataPinValueType || (DataPinValueType = {}));
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A simple text range, expressed as start character index and characters
|
|
21
|
+
* length. This is used for misspellings operations.
|
|
22
|
+
*/
|
|
23
|
+
class TextRange {
|
|
24
|
+
constructor(start, length) {
|
|
25
|
+
if (start < 0)
|
|
26
|
+
throw new RangeError('TextRange start less than 0');
|
|
27
|
+
if (length < 0)
|
|
28
|
+
throw new RangeError('TextRange length less than 0');
|
|
29
|
+
this.start = start;
|
|
30
|
+
this.length = length;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Parse the specified text representing a range, in the form "startxlength"
|
|
34
|
+
* or just "start" when length is 1.
|
|
35
|
+
*
|
|
36
|
+
* @param text The text.
|
|
37
|
+
*/
|
|
38
|
+
static parse(text) {
|
|
39
|
+
if (!text) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
// [1]=start, ([2]=length)
|
|
43
|
+
const m = new RegExp('(\\d+)(?:[×x](\\d+))?', 'gm').exec(text);
|
|
44
|
+
if (!m) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
return new TextRange(parseInt(m[1], 10), m[2] ? parseInt(m[2], 10) : 1);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Get the inclusive end of the range.
|
|
51
|
+
*/
|
|
52
|
+
end() {
|
|
53
|
+
const n = this.start + this.length;
|
|
54
|
+
return n > 0 ? n - 1 : 0;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* True if this range value is equal to the other range value.
|
|
58
|
+
* @param other The other range.
|
|
59
|
+
*/
|
|
60
|
+
isEqual(other) {
|
|
61
|
+
if (!other) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
return this.start === other.start && this.length === other.length;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Get a parsable textual representation of this range.
|
|
68
|
+
* @returns Text.
|
|
69
|
+
*/
|
|
70
|
+
toString() {
|
|
71
|
+
return this.length === 1
|
|
72
|
+
? this.start.toString()
|
|
73
|
+
: `${this.start}×${this.length}`;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* A single point in a token-based textual location.
|
|
79
|
+
*/
|
|
80
|
+
class TokenPoint {
|
|
81
|
+
constructor(y, x, at = 0, run = 0) {
|
|
82
|
+
this.y = y;
|
|
83
|
+
this.x = x;
|
|
84
|
+
this.at = at;
|
|
85
|
+
this.run = run;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Parse the specified text representing a token-based point location.
|
|
89
|
+
* @param text The text to parse, in the forms Y.X or Y.X@AxR or
|
|
90
|
+
* Y.X@A, where R=1.
|
|
91
|
+
*/
|
|
92
|
+
static parse(text) {
|
|
93
|
+
const r = new RegExp('^(\\d+)\\.(\\d+)(?:@(\\d+)(?:x(\\d+))?)?$', 'g');
|
|
94
|
+
const m = r.exec(text);
|
|
95
|
+
if (!m) {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
const pt = new TokenPoint(parseInt(m[1], 10), parseInt(m[2], 10), m[3] ? parseInt(m[3], 10) : 0);
|
|
99
|
+
if (m[3]) {
|
|
100
|
+
if (m[4]) {
|
|
101
|
+
pt.run = m[4] ? parseInt(m[4], 10) : 1;
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
pt.run = 1;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return pt;
|
|
108
|
+
}
|
|
109
|
+
toString() {
|
|
110
|
+
let s = `${this.y}.${this.x}`;
|
|
111
|
+
if (this.at && this.run) {
|
|
112
|
+
s += `@${this.at}`;
|
|
113
|
+
if (this.run !== 1) {
|
|
114
|
+
s += `x${this.run}`;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return s;
|
|
118
|
+
}
|
|
119
|
+
compareTo(other) {
|
|
120
|
+
if (this.y !== other.y) {
|
|
121
|
+
return this.y - other.y;
|
|
122
|
+
}
|
|
123
|
+
if (this.x !== other.x) {
|
|
124
|
+
return this.x - other.x;
|
|
125
|
+
}
|
|
126
|
+
if (this.at !== other.at) {
|
|
127
|
+
return this.at - other.at;
|
|
128
|
+
}
|
|
129
|
+
return this.run - other.run;
|
|
130
|
+
}
|
|
131
|
+
integralCompareTo(other) {
|
|
132
|
+
if (this.y !== other.y) {
|
|
133
|
+
return this.y - other.y;
|
|
134
|
+
}
|
|
135
|
+
if (this.x !== other.x) {
|
|
136
|
+
return this.x - other.x;
|
|
137
|
+
}
|
|
138
|
+
return 0;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
class TokenLocation {
|
|
143
|
+
constructor(primary, secondary) {
|
|
144
|
+
this.primary = primary;
|
|
145
|
+
this.secondary = secondary;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Parse the text representing a token-based location,
|
|
149
|
+
* which has the form "primary-secondary", or just "primary",
|
|
150
|
+
* where each of these points has the format required by
|
|
151
|
+
* TokenPoint.parse (e.g. Y.X or Y.X@AxR or Y.X@A, where R=1).
|
|
152
|
+
* @param text The text to parse.
|
|
153
|
+
*/
|
|
154
|
+
static parse(text) {
|
|
155
|
+
if (!text) {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
const pair = text.split('-', 2);
|
|
159
|
+
if (pair.length === 0) {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
return new TokenLocation(TokenPoint.parse(pair[0]), pair.length > 1 ? TokenPoint.parse(pair[1]) || undefined : undefined);
|
|
163
|
+
}
|
|
164
|
+
isRange() {
|
|
165
|
+
// note that != checks for both null and undefined, whereas !== does not
|
|
166
|
+
return this.secondary != null;
|
|
167
|
+
}
|
|
168
|
+
toString() {
|
|
169
|
+
return this.secondary
|
|
170
|
+
? `${this.primary.toString()}-${this.secondary.toString()}`
|
|
171
|
+
: this.primary.toString();
|
|
172
|
+
}
|
|
173
|
+
compareTo(other) {
|
|
174
|
+
const n1 = this.primary.compareTo(other.primary);
|
|
175
|
+
if (n1 !== 0) {
|
|
176
|
+
return n1;
|
|
177
|
+
}
|
|
178
|
+
if (this.secondary && other.secondary) {
|
|
179
|
+
return this.secondary.compareTo(other.secondary);
|
|
180
|
+
}
|
|
181
|
+
if (!this.secondary && !other.secondary) {
|
|
182
|
+
return 0;
|
|
183
|
+
}
|
|
184
|
+
return this.secondary ? 1 : -1;
|
|
185
|
+
}
|
|
186
|
+
overlaps(other) {
|
|
187
|
+
// empty coords never overlaps
|
|
188
|
+
if (!other.primary) {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
// (in the following comments A=this and B=other)
|
|
192
|
+
// 1) if B is a point, cases are:
|
|
193
|
+
// 1.1. A is a point: A overlaps with B (both points) if they're equal.
|
|
194
|
+
// 1.2. A is a range: A (range) overlaps with B (point)
|
|
195
|
+
// when A-left is <= B-left and A-right is >= B-right.
|
|
196
|
+
if (other.secondary == null) {
|
|
197
|
+
return !this.secondary
|
|
198
|
+
? this.primary.integralCompareTo(other.primary) === 0
|
|
199
|
+
: this.primary.compareTo(other.primary) <= 0 &&
|
|
200
|
+
this.secondary.compareTo(other.primary) >= 0;
|
|
201
|
+
}
|
|
202
|
+
// 2) if B is a range, cases are:
|
|
203
|
+
// 2.1. A is a point: A (point) overlaps with B (range) when B-left <= A and
|
|
204
|
+
// B-right >= A
|
|
205
|
+
// 2.2. A is a range: A (range) overlaps with B (range)
|
|
206
|
+
// when B-right >= A-left and B-left <= A-right
|
|
207
|
+
return !this.secondary
|
|
208
|
+
? other.primary.compareTo(this.primary) <= 0 &&
|
|
209
|
+
other.secondary.compareTo(this.primary) >= 0
|
|
210
|
+
: other.secondary.compareTo(this.primary) >= 0 &&
|
|
211
|
+
other.primary.compareTo(this.secondary) <= 0;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
class PendingChangesGuard {
|
|
216
|
+
canDeactivate(component) {
|
|
217
|
+
// if there are no pending changes, just allow deactivation; else confirm first
|
|
218
|
+
return !component || component.canDeactivate()
|
|
219
|
+
? true
|
|
220
|
+
: // NOTE: this warning message will only be shown when navigating
|
|
221
|
+
// elsewhere within your angular app; when navigating away from your
|
|
222
|
+
// angular app, the browser will show a generic warning message.
|
|
223
|
+
// see http://stackoverflow.com/a/42207299/7307355
|
|
224
|
+
confirm('Warning: your changes have not been saved. ' +
|
|
225
|
+
'Press Cancel to go back and save, OK to continue.');
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
PendingChangesGuard.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.3", ngImport: i0, type: PendingChangesGuard, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
229
|
+
PendingChangesGuard.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.0.3", ngImport: i0, type: PendingChangesGuard, providedIn: 'root' });
|
|
230
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.3", ngImport: i0, type: PendingChangesGuard, decorators: [{
|
|
231
|
+
type: Injectable,
|
|
232
|
+
args: [{
|
|
233
|
+
providedIn: 'root',
|
|
234
|
+
}]
|
|
235
|
+
}] });
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Sort pipe. Use like *ngFor="let i of items | sort:'propname'".
|
|
239
|
+
*/
|
|
240
|
+
class SortPipe {
|
|
241
|
+
transform(value, propName) {
|
|
242
|
+
if (!value || !Array.isArray(value)) {
|
|
243
|
+
return value;
|
|
244
|
+
}
|
|
245
|
+
return [...value].sort((a, b) => {
|
|
246
|
+
if (!a && !b) {
|
|
247
|
+
return 0;
|
|
248
|
+
}
|
|
249
|
+
if (!a && b) {
|
|
250
|
+
return -1;
|
|
251
|
+
}
|
|
252
|
+
if (a && !b) {
|
|
253
|
+
return 1;
|
|
254
|
+
}
|
|
255
|
+
if (a[propName] < b[propName]) {
|
|
256
|
+
return -1;
|
|
257
|
+
}
|
|
258
|
+
else if (a[propName] === b[propName]) {
|
|
259
|
+
return 0;
|
|
260
|
+
}
|
|
261
|
+
else if (a[propName] > b[propName]) {
|
|
262
|
+
return 1;
|
|
263
|
+
}
|
|
264
|
+
return 0;
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
SortPipe.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.3", ngImport: i0, type: SortPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe });
|
|
269
|
+
SortPipe.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "12.0.0", version: "13.0.3", ngImport: i0, type: SortPipe, name: "sort" });
|
|
270
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.3", ngImport: i0, type: SortPipe, decorators: [{
|
|
271
|
+
type: Pipe,
|
|
272
|
+
args: [{
|
|
273
|
+
name: 'sort',
|
|
274
|
+
}]
|
|
275
|
+
}] });
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Library route service.
|
|
279
|
+
* This service is used to build routes to part and fragment editors,
|
|
280
|
+
* which are hosted in an open list of library modules in the Cadmus
|
|
281
|
+
* application.
|
|
282
|
+
* This architecture allows lazy-loading part- and fragment-editors
|
|
283
|
+
* only when they are required.
|
|
284
|
+
* Each part type with its dumb UI editor component is located in a
|
|
285
|
+
* library, named after the name of the group the part belongs to. For
|
|
286
|
+
* instance, the model and dumb UI editor component for a part belonging
|
|
287
|
+
* to the "general" group are located in the "parts/general/general-ui"
|
|
288
|
+
* library.
|
|
289
|
+
* In turn, the feature UI editor component for the same part is located
|
|
290
|
+
* in the "parts/general/general-feature" library. This library has
|
|
291
|
+
* a route for each part editor. For instance, the NotePart editor
|
|
292
|
+
* component route is "items/:iid/general/it.vedph.note/:pid", where
|
|
293
|
+
* ":iid" is the item ID and ":pid" the part ID.
|
|
294
|
+
*/
|
|
295
|
+
class LibraryRouteService {
|
|
296
|
+
constructor(_partEditorKeys) {
|
|
297
|
+
this._partEditorKeys = _partEditorKeys;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Strip the eventual fragment-role ID from the specified part's role ID.
|
|
301
|
+
* For instance, a part's role ID like "fr.it.vedph.comment:scholarly"
|
|
302
|
+
* becomes "fr.it.vedph.comment".
|
|
303
|
+
*
|
|
304
|
+
* @param roleId The part's role ID.
|
|
305
|
+
*/
|
|
306
|
+
stripFragmentRoleId(roleId) {
|
|
307
|
+
// remove the optional :fr-role from roleId
|
|
308
|
+
const i = roleId.lastIndexOf(':');
|
|
309
|
+
if (i > -1) {
|
|
310
|
+
roleId = roleId.substring(0, i);
|
|
311
|
+
}
|
|
312
|
+
return roleId;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Get the fragment type and role from the part's role ID. This starts
|
|
316
|
+
* with "fr.", and might be followed by the role proper, introduced
|
|
317
|
+
* by the first dot if any. For instance, "fr.it.vedph.comment" would just
|
|
318
|
+
* return "fr.it.vedph.comment", while "fr.it.vedph.comment:scholarly"
|
|
319
|
+
* would return "fr.it.vedph.comment". This is required to navigate to
|
|
320
|
+
* the correct fragment editor, which is the same for each fragment type,
|
|
321
|
+
* independently from its role.
|
|
322
|
+
* @param role The part role ID.
|
|
323
|
+
* @returns An object where frTypeId=fragment type ID and frRoleId=fragment role
|
|
324
|
+
* ID, or null.
|
|
325
|
+
*/
|
|
326
|
+
getFragmentTypeAndRole(roleId) {
|
|
327
|
+
if (!roleId || !roleId.startsWith('fr.')) {
|
|
328
|
+
return null;
|
|
329
|
+
}
|
|
330
|
+
const i = roleId.indexOf(':');
|
|
331
|
+
if (i > -1) {
|
|
332
|
+
return {
|
|
333
|
+
frTypeId: roleId.substring(0, i),
|
|
334
|
+
frRoleId: roleId.substring(i + 1),
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
return {
|
|
339
|
+
frTypeId: roleId,
|
|
340
|
+
frRoleId: undefined,
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Find among the specified parts definitions the one matching the specified
|
|
346
|
+
* type ID, and eventually role ID when specified.
|
|
347
|
+
*
|
|
348
|
+
* @param partDefs The parts definitions.
|
|
349
|
+
* @param typeId The type ID.
|
|
350
|
+
* @param roleId The role ID or null.
|
|
351
|
+
* @returns Part definition or undefined if not found.
|
|
352
|
+
*/
|
|
353
|
+
findPartDefinition(partDefs, typeId, roleId = null) {
|
|
354
|
+
if (!partDefs) {
|
|
355
|
+
return undefined;
|
|
356
|
+
}
|
|
357
|
+
// if role ID is specified, find both type and role
|
|
358
|
+
if (roleId) {
|
|
359
|
+
const reqRoleId = this.stripFragmentRoleId(roleId);
|
|
360
|
+
return partDefs.find((d) => {
|
|
361
|
+
return (d.typeId === typeId &&
|
|
362
|
+
d.roleId &&
|
|
363
|
+
this.stripFragmentRoleId(d.roleId) === reqRoleId);
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
// else just find type ID
|
|
367
|
+
return partDefs.find((d) => {
|
|
368
|
+
return d.typeId === typeId;
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Get the library editor key from the specified part type, looking up
|
|
373
|
+
* the specified parts definitions.
|
|
374
|
+
*
|
|
375
|
+
* @param typeId The part's type ID.
|
|
376
|
+
* @param roleId The part's role ID.
|
|
377
|
+
*/
|
|
378
|
+
getEditorKeyFromPartType(typeId, roleId) {
|
|
379
|
+
// find the part type ID
|
|
380
|
+
const partGroupKey = this._partEditorKeys[typeId];
|
|
381
|
+
if (!partGroupKey) {
|
|
382
|
+
return {
|
|
383
|
+
partKey: 'default',
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
// additionally find the role ID if specified
|
|
387
|
+
if (roleId) {
|
|
388
|
+
const reqRoleId = this.stripFragmentRoleId(roleId);
|
|
389
|
+
return {
|
|
390
|
+
partKey: partGroupKey.part,
|
|
391
|
+
frKey: partGroupKey.fragments[reqRoleId],
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
return {
|
|
395
|
+
partKey: partGroupKey.part,
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Decompose the specified editor key into part and fragment.
|
|
400
|
+
* A simple editor key will just provide the same value for both.
|
|
401
|
+
*
|
|
402
|
+
* @param key The editor key to decompose.
|
|
403
|
+
*/
|
|
404
|
+
decomposeEditorKey(key) {
|
|
405
|
+
const i = key.indexOf(' ');
|
|
406
|
+
if (i > -1) {
|
|
407
|
+
return {
|
|
408
|
+
partKey: key.substr(0, i),
|
|
409
|
+
frKey: key.substr(i + 1),
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
else {
|
|
413
|
+
return {
|
|
414
|
+
partKey: key,
|
|
415
|
+
frKey: key,
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* Build the route to the part editor corresponding to the specified
|
|
421
|
+
* part.
|
|
422
|
+
*
|
|
423
|
+
* A part editing route is like
|
|
424
|
+
* "/items/<id>/<part-group>/<part-typeid>/<part-id>?rid=<role-id>",
|
|
425
|
+
* where the role ID is optional, and may include the fragment role ID
|
|
426
|
+
* as a suffix preceded by a colon.
|
|
427
|
+
*
|
|
428
|
+
* A fragment editing route is like
|
|
429
|
+
* "/items/<id>/<part-group>/fragment/<part-id>/<fr-typeid>/<loc>?rid=<role-id>",
|
|
430
|
+
* where the role ID is as above, and the fragment's type ID is derived
|
|
431
|
+
* from the part's role ID, stripping the eventual fragment's role ID out.
|
|
432
|
+
*
|
|
433
|
+
* @param itemId The ID of the item the parts belongs to.
|
|
434
|
+
* @param partId The part's ID.
|
|
435
|
+
* @param typeId The part's type ID.
|
|
436
|
+
* @param roleId The optional part's role ID.
|
|
437
|
+
* @returns Object with a route property and an optional rid property
|
|
438
|
+
* representing the role ID, which will be rendered as a query parameter.
|
|
439
|
+
*/
|
|
440
|
+
buildPartEditorRoute(itemId, partId, typeId, roleId) {
|
|
441
|
+
let route;
|
|
442
|
+
const editorKey = this.getEditorKeyFromPartType(typeId);
|
|
443
|
+
// /items/<id>/<part-group>/<part-typeid>/<part-id>?rid=<role-id>
|
|
444
|
+
route = `/items/${itemId}/${editorKey.partKey}/${typeId}/${partId}`;
|
|
445
|
+
return {
|
|
446
|
+
route: route,
|
|
447
|
+
rid: roleId,
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Build the route to the fragment editor corresponding to the specified
|
|
452
|
+
* layers part and location.
|
|
453
|
+
*
|
|
454
|
+
* @param partDefs The parts definitions.
|
|
455
|
+
* @param itemId The ID of the item the parts belongs to.
|
|
456
|
+
* @param partId The part's ID.
|
|
457
|
+
* @param typeId The part's type ID.
|
|
458
|
+
* @param roleId The part's role ID.
|
|
459
|
+
* @param loc The fragment's location.
|
|
460
|
+
* @returns Object with a route property and an optional rid property
|
|
461
|
+
* representing the role ID, which will be rendered as a query parameter.
|
|
462
|
+
*/
|
|
463
|
+
buildFragmentEditorRoute(partDefs, itemId, partId, typeId, roleId, loc) {
|
|
464
|
+
let route;
|
|
465
|
+
const editorKey = this.getEditorKeyFromPartType(typeId, roleId);
|
|
466
|
+
const { frTypeId, frRoleId } = this.getFragmentTypeAndRole(roleId);
|
|
467
|
+
route = `/items/${itemId}/${editorKey.frKey}/fragment/${partId}/${frTypeId}/${loc}`;
|
|
468
|
+
return {
|
|
469
|
+
route: route,
|
|
470
|
+
rid: frRoleId,
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
LibraryRouteService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.3", ngImport: i0, type: LibraryRouteService, deps: [{ token: 'partEditorKeys' }], target: i0.ɵɵFactoryTarget.Injectable });
|
|
475
|
+
LibraryRouteService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.0.3", ngImport: i0, type: LibraryRouteService, providedIn: 'root' });
|
|
476
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.3", ngImport: i0, type: LibraryRouteService, decorators: [{
|
|
477
|
+
type: Injectable,
|
|
478
|
+
args: [{
|
|
479
|
+
providedIn: 'root',
|
|
480
|
+
}]
|
|
481
|
+
}], ctorParameters: function () {
|
|
482
|
+
return [{ type: undefined, decorators: [{
|
|
483
|
+
type: Inject,
|
|
484
|
+
args: ['partEditorKeys']
|
|
485
|
+
}] }];
|
|
486
|
+
} });
|
|
487
|
+
|
|
488
|
+
class TextLayerService {
|
|
489
|
+
constructor() {
|
|
490
|
+
this._wsRegex = new RegExp('^\\s$', 'g');
|
|
491
|
+
}
|
|
492
|
+
isWhitespace(c) {
|
|
493
|
+
if (!c) {
|
|
494
|
+
return false;
|
|
495
|
+
}
|
|
496
|
+
if (c.length !== 1) {
|
|
497
|
+
c = c.charAt(0);
|
|
498
|
+
}
|
|
499
|
+
return this._wsRegex.test(c);
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Extract the lines with their tokens from the specified text.
|
|
503
|
+
* @param text the source text.
|
|
504
|
+
* @returns lines.
|
|
505
|
+
*/
|
|
506
|
+
getLines(text) {
|
|
507
|
+
const results = [];
|
|
508
|
+
if (!text) {
|
|
509
|
+
return results;
|
|
510
|
+
}
|
|
511
|
+
const lines = text.split('\n');
|
|
512
|
+
for (let y = 0; y < lines.length; y++) {
|
|
513
|
+
const line = lines[y].trim();
|
|
514
|
+
const l = {
|
|
515
|
+
y: y + 1,
|
|
516
|
+
tokens: [],
|
|
517
|
+
};
|
|
518
|
+
let x = 0;
|
|
519
|
+
let xBeg = 0;
|
|
520
|
+
while (x < line.length) {
|
|
521
|
+
if (this.isWhitespace(line.charAt(x))) {
|
|
522
|
+
l.tokens.push(line.substr(xBeg, x - xBeg));
|
|
523
|
+
x++;
|
|
524
|
+
while (x < line.length && this.isWhitespace(line.charAt(x))) {
|
|
525
|
+
x++;
|
|
526
|
+
}
|
|
527
|
+
xBeg = x;
|
|
528
|
+
}
|
|
529
|
+
else {
|
|
530
|
+
x++;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
if (xBeg < x) {
|
|
534
|
+
l.tokens.push(line.substr(xBeg));
|
|
535
|
+
}
|
|
536
|
+
results.push(l);
|
|
537
|
+
}
|
|
538
|
+
return results;
|
|
539
|
+
}
|
|
540
|
+
/*
|
|
541
|
+
* Rendition: HTML rendering from a base text and a set of fragments locations.
|
|
542
|
+
* Each line in the base text is rendered with p; each token in a line is
|
|
543
|
+
* rendered with its text value, and tokens are separated by space. Layer
|
|
544
|
+
* fragments are rendered inside a span, with an id equal to the fragment location
|
|
545
|
+
* plus a suffix beginning with _, added only for the purpose of ensuring unique
|
|
546
|
+
* IDs in the rendered HTML code. Example:
|
|
547
|
+
* <div>
|
|
548
|
+
* <p id="y1">this is line 1.</p>
|
|
549
|
+
* <p id="y2">here's a <span id="f2.3_0">fragment</span>.</p>
|
|
550
|
+
* </div>
|
|
551
|
+
*/
|
|
552
|
+
/**
|
|
553
|
+
* Render the opening span for a layer fragment.
|
|
554
|
+
* @param loc location.
|
|
555
|
+
* @param locPortion the index of the token's portion (0-N). This is used
|
|
556
|
+
* to build a unique ID for each HTML span rendering a layer fragment.
|
|
557
|
+
* @param sb the target strings array.
|
|
558
|
+
* @param isSelected true if selected.
|
|
559
|
+
*/
|
|
560
|
+
renderOpeningSpan(loc, locPortion, sb, isSelected) {
|
|
561
|
+
sb.push(`<span id="f${loc.toString()}_${locPortion}" class="fr${isSelected ? 'fr-sel' : ''}">`);
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Render the specified token intersecting the specified end-fragment location.
|
|
565
|
+
* @param pt The point.
|
|
566
|
+
* @param token The token.
|
|
567
|
+
* @param sb The target array.
|
|
568
|
+
*/
|
|
569
|
+
renderTokenAtEnd(pt, token, sb) {
|
|
570
|
+
if (pt.at) {
|
|
571
|
+
// ...to]
|
|
572
|
+
sb.push(token.substr(pt.at - 1, pt.run));
|
|
573
|
+
sb.push('</span>');
|
|
574
|
+
// ...ken
|
|
575
|
+
if (pt.at - 1 + (pt.run || 0) < token.length) {
|
|
576
|
+
sb.push(token.substr(pt.at - 1 + (pt.run || 0)));
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
else {
|
|
580
|
+
// ...token]
|
|
581
|
+
sb.push(token);
|
|
582
|
+
sb.push('</span>');
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* Render the specified token intersecting the specified single-point location.
|
|
587
|
+
* The received token starts and ends a single-token range.
|
|
588
|
+
* @param loc location.
|
|
589
|
+
* @param token token.
|
|
590
|
+
* @param sb target strings array.
|
|
591
|
+
* @param isSelected True if the token is selected.
|
|
592
|
+
*/
|
|
593
|
+
renderTokenAtPointStart(loc, token, sb, isSelected) {
|
|
594
|
+
// 1.token's left-part
|
|
595
|
+
if (loc.primary.at) {
|
|
596
|
+
// to...
|
|
597
|
+
sb.push(token.substr(0, loc.primary.at - 1));
|
|
598
|
+
}
|
|
599
|
+
// 2.span: [
|
|
600
|
+
this.renderOpeningSpan(loc, 0, sb, isSelected);
|
|
601
|
+
// 3.token's right-part (...ken]/...ke]n or ...token])
|
|
602
|
+
this.renderTokenAtEnd(loc.primary, token, sb);
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* Render the specified token intersecting the specified range start location.
|
|
606
|
+
* The received token starts a multi-tokens range.
|
|
607
|
+
* @param loc location.
|
|
608
|
+
* @param locPortion the index of the token's portion (0-N). This is used
|
|
609
|
+
* to build a unique ID for each HTML span rendering a layer fragment.
|
|
610
|
+
* @param token token.
|
|
611
|
+
* @param sb target strings array.
|
|
612
|
+
* @param isSelected True if the token is selected.
|
|
613
|
+
*/
|
|
614
|
+
renderTokenAtRangeStart(loc, token, sb, isSelected) {
|
|
615
|
+
// 1.token's left-part (=before at)
|
|
616
|
+
if (loc.primary.at) {
|
|
617
|
+
// to...
|
|
618
|
+
sb.push(token.substr(0, loc.primary.at - 1));
|
|
619
|
+
}
|
|
620
|
+
// 2.span: [
|
|
621
|
+
this.renderOpeningSpan(loc, 0, sb, isSelected);
|
|
622
|
+
// 3.token's right-part (...ken or ...token)
|
|
623
|
+
if (loc.primary.at) {
|
|
624
|
+
// ...ken
|
|
625
|
+
sb.push(token.substr(loc.primary.at - 1));
|
|
626
|
+
}
|
|
627
|
+
else {
|
|
628
|
+
// ...token
|
|
629
|
+
sb.push(token);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
renderTokenAtStart(loc, token, sb, isSelected) {
|
|
633
|
+
if (!loc.secondary) {
|
|
634
|
+
// single
|
|
635
|
+
this.renderTokenAtPointStart(loc, token, sb, isSelected);
|
|
636
|
+
return false;
|
|
637
|
+
}
|
|
638
|
+
else {
|
|
639
|
+
// range
|
|
640
|
+
this.renderTokenAtRangeStart(loc, token, sb, isSelected);
|
|
641
|
+
return true;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* Render to HTML the base text decorated with its layer fragments.
|
|
646
|
+
* @param text The base text.
|
|
647
|
+
* @param locations The locations of all the fragments
|
|
648
|
+
* of the same type to be rendered on top of the base text.
|
|
649
|
+
* @param selectedLoc The optional location of the selected
|
|
650
|
+
* fragment if any. The selected fragment gets decorated with an additional
|
|
651
|
+
* class value.
|
|
652
|
+
*/
|
|
653
|
+
render(text, locations, selectedLoc) {
|
|
654
|
+
if (!text) {
|
|
655
|
+
return '<div></div>';
|
|
656
|
+
}
|
|
657
|
+
// get lines from text
|
|
658
|
+
const sb = [];
|
|
659
|
+
const lines = this.getLines(text);
|
|
660
|
+
let loc = undefined;
|
|
661
|
+
let tokenSelected = false;
|
|
662
|
+
if (!locations) {
|
|
663
|
+
locations = [];
|
|
664
|
+
}
|
|
665
|
+
// for each line in text
|
|
666
|
+
sb.push('<div>');
|
|
667
|
+
for (let y = 0; y < lines.length; y++) {
|
|
668
|
+
sb.push(`<p id="y${y + 1}">`);
|
|
669
|
+
// reopen a span if continuing a fragment from prev line
|
|
670
|
+
if (loc) {
|
|
671
|
+
this.renderOpeningSpan(loc, y + 1 - loc.primary.y, sb, tokenSelected);
|
|
672
|
+
}
|
|
673
|
+
// for each token in line
|
|
674
|
+
const line = lines[y];
|
|
675
|
+
for (let x = 0; x < line.tokens.length; x++) {
|
|
676
|
+
// add separator if required
|
|
677
|
+
if (x > 0) {
|
|
678
|
+
sb.push(' ');
|
|
679
|
+
}
|
|
680
|
+
const token = line.tokens[x];
|
|
681
|
+
// if inside a fragment, check if this token is the last
|
|
682
|
+
if (loc && loc.secondary) {
|
|
683
|
+
if (loc.secondary.y === y + 1 && loc.secondary.x === x + 1) {
|
|
684
|
+
this.renderTokenAtEnd(loc.secondary, token, sb);
|
|
685
|
+
loc = undefined;
|
|
686
|
+
}
|
|
687
|
+
else {
|
|
688
|
+
sb.push(token);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
else {
|
|
692
|
+
// else, check if this token starts a fragment
|
|
693
|
+
loc = locations.find((l) => {
|
|
694
|
+
return l.primary.y === y + 1 && l.primary.x === x + 1;
|
|
695
|
+
});
|
|
696
|
+
if (loc) {
|
|
697
|
+
tokenSelected = selectedLoc ? loc.overlaps(selectedLoc) : false;
|
|
698
|
+
if (!this.renderTokenAtStart(loc, token, sb, tokenSelected)) {
|
|
699
|
+
loc = undefined;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
else {
|
|
703
|
+
sb.push(token);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
// close a span if pending at p end
|
|
708
|
+
if (loc) {
|
|
709
|
+
sb.push('</span>');
|
|
710
|
+
}
|
|
711
|
+
sb.push('</p>');
|
|
712
|
+
}
|
|
713
|
+
sb.push('</div>');
|
|
714
|
+
return sb.join('');
|
|
715
|
+
}
|
|
716
|
+
/*
|
|
717
|
+
Location: starting from user selection on a rendered text, get the corresponding
|
|
718
|
+
layer fragment location. This differs according to the purpose of the location
|
|
719
|
+
to be got: if getting a location for an existing fragment, at least 1 decorated
|
|
720
|
+
text (span) should be included in the selection, which does not require to be
|
|
721
|
+
exact; if getting a location for a new fragment, no decorated text (span) can be
|
|
722
|
+
included in the selection (two fragments in the same layer can never overlap),
|
|
723
|
+
which must be exact.
|
|
724
|
+
*/
|
|
725
|
+
/**
|
|
726
|
+
* Get the Y bounds from the specified selection.
|
|
727
|
+
* @param range The selection range.
|
|
728
|
+
* @param forNew True if getting bounds for a new fragment.
|
|
729
|
+
* @returns Range, inclusive, or undefined.
|
|
730
|
+
*/
|
|
731
|
+
getYBoundsFromRange(range, forNew) {
|
|
732
|
+
let p = null;
|
|
733
|
+
switch (range.commonAncestorContainer.nodeType) {
|
|
734
|
+
case Node.TEXT_NODE:
|
|
735
|
+
// single-p selection, parent=p
|
|
736
|
+
p = range.commonAncestorContainer.parentElement;
|
|
737
|
+
if (forNew && ((p === null || p === void 0 ? void 0 : p.nodeName) !== 'P' || !p.hasAttribute('id'))) {
|
|
738
|
+
return null;
|
|
739
|
+
}
|
|
740
|
+
if ((p === null || p === void 0 ? void 0 : p.nodeName) === 'SPAN') {
|
|
741
|
+
p = p.parentElement;
|
|
742
|
+
}
|
|
743
|
+
const y = parseInt(p.getAttribute('id').substr(1), 10);
|
|
744
|
+
// return [y, y];
|
|
745
|
+
return { start: y, end: y };
|
|
746
|
+
case Node.ELEMENT_NODE:
|
|
747
|
+
// div only is allowed; in this case, it's a multi-p range
|
|
748
|
+
if (range.commonAncestorContainer.nodeName !== 'DIV') {
|
|
749
|
+
return null;
|
|
750
|
+
}
|
|
751
|
+
// for each div's child p element:
|
|
752
|
+
let yMin = 0;
|
|
753
|
+
let yMax = 0;
|
|
754
|
+
let inside = false;
|
|
755
|
+
for (let i = 0; i < range.commonAncestorContainer.childNodes.length; i++) {
|
|
756
|
+
const child = range.commonAncestorContainer.childNodes[i];
|
|
757
|
+
if (child.nodeType !== Node.ELEMENT_NODE || child.nodeName !== 'P') {
|
|
758
|
+
continue;
|
|
759
|
+
}
|
|
760
|
+
// check if we're entering the start-p
|
|
761
|
+
if (!inside && child.contains(range.startContainer)) {
|
|
762
|
+
inside = true;
|
|
763
|
+
}
|
|
764
|
+
// collect y if inside
|
|
765
|
+
if (inside) {
|
|
766
|
+
const iy = parseInt(child.getAttribute('id').substr(1), 10);
|
|
767
|
+
if (!yMin || iy < yMin) {
|
|
768
|
+
yMin = iy;
|
|
769
|
+
}
|
|
770
|
+
if (!yMax || iy > yMax) {
|
|
771
|
+
yMax = iy;
|
|
772
|
+
}
|
|
773
|
+
// check if we have exited the last p
|
|
774
|
+
if (child.contains(range.endContainer)) {
|
|
775
|
+
break;
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
return {
|
|
780
|
+
start: yMin,
|
|
781
|
+
end: yMax,
|
|
782
|
+
};
|
|
783
|
+
}
|
|
784
|
+
return null;
|
|
785
|
+
}
|
|
786
|
+
locatePrevSpace(line, index) {
|
|
787
|
+
while (index > -1 && line.charAt(index) !== ' ') {
|
|
788
|
+
index--;
|
|
789
|
+
}
|
|
790
|
+
return index;
|
|
791
|
+
}
|
|
792
|
+
locateNextSpace(line, index) {
|
|
793
|
+
while (index < line.length && line.charAt(index) !== ' ') {
|
|
794
|
+
index++;
|
|
795
|
+
}
|
|
796
|
+
return index === line.length ? -1 : index;
|
|
797
|
+
}
|
|
798
|
+
/**
|
|
799
|
+
* Get the bounds of the token any character of which is indexed
|
|
800
|
+
* by index.
|
|
801
|
+
* @param line The text line.
|
|
802
|
+
* @param index The index to the text line to start from.
|
|
803
|
+
* @returns Bounds where start=index of the first character of the token,
|
|
804
|
+
* and end=index of the character past the token's last character;
|
|
805
|
+
* or null if index was not valid.
|
|
806
|
+
*/
|
|
807
|
+
getTokenBounds(line, index) {
|
|
808
|
+
if (line.charAt(index) === ' ') {
|
|
809
|
+
return null;
|
|
810
|
+
}
|
|
811
|
+
let start = this.locatePrevSpace(line, index);
|
|
812
|
+
if (start === -1) {
|
|
813
|
+
start = 0;
|
|
814
|
+
}
|
|
815
|
+
else {
|
|
816
|
+
start++;
|
|
817
|
+
}
|
|
818
|
+
let end = this.locateNextSpace(line, index);
|
|
819
|
+
if (end === -1) {
|
|
820
|
+
end = line.length;
|
|
821
|
+
}
|
|
822
|
+
// return [start, end];
|
|
823
|
+
return {
|
|
824
|
+
start,
|
|
825
|
+
end,
|
|
826
|
+
};
|
|
827
|
+
}
|
|
828
|
+
/**
|
|
829
|
+
* Return the received selection bounds, adjusted to exclude spaces.
|
|
830
|
+
*/
|
|
831
|
+
getAdjustedSelectionBounds(selStart, selEnd, line) {
|
|
832
|
+
// corner case: if on space, move to right
|
|
833
|
+
if (line.charAt(selStart) === ' ') {
|
|
834
|
+
selStart++;
|
|
835
|
+
}
|
|
836
|
+
// corner case: if included trailing space, move to left
|
|
837
|
+
if (selEnd > 0 && line.charAt(selEnd - 1) === ' ') {
|
|
838
|
+
selEnd--;
|
|
839
|
+
}
|
|
840
|
+
// return [selStart, selEnd];
|
|
841
|
+
return {
|
|
842
|
+
start: selStart,
|
|
843
|
+
end: selEnd,
|
|
844
|
+
};
|
|
845
|
+
}
|
|
846
|
+
/**
|
|
847
|
+
* Get the start coordinates for the selected range and line.
|
|
848
|
+
* @param range The selected range.
|
|
849
|
+
* @param line The reference text line.
|
|
850
|
+
*/
|
|
851
|
+
getStartCoordsFromRange(selStart, selEnd, line) {
|
|
852
|
+
// get adjusted selection bounds
|
|
853
|
+
const selBounds = this.getAdjustedSelectionBounds(selStart, selEnd, line);
|
|
854
|
+
// get the bounds of the first selected token
|
|
855
|
+
const tokBounds = this.getTokenBounds(line, selBounds.start);
|
|
856
|
+
// calculate x by counting the tokens before the target token
|
|
857
|
+
let x = 1;
|
|
858
|
+
let j = tokBounds.start - 1;
|
|
859
|
+
while (j > -1) {
|
|
860
|
+
if (line.charAt(j) === ' ') {
|
|
861
|
+
x++;
|
|
862
|
+
}
|
|
863
|
+
j--;
|
|
864
|
+
}
|
|
865
|
+
// calculate at,run only when either of these is true:
|
|
866
|
+
// (a) selection starts past the 1st char of the token (al[pha);
|
|
867
|
+
// (b) selection ends before the last char of the token (al]pha);
|
|
868
|
+
// run is calculated only if (b) is true.
|
|
869
|
+
let at = 0;
|
|
870
|
+
let run = 0;
|
|
871
|
+
if (selBounds.start > tokBounds.start || selBounds.end < tokBounds.end) {
|
|
872
|
+
at = 1 + selBounds.start - tokBounds.start;
|
|
873
|
+
run =
|
|
874
|
+
(selBounds.end < tokBounds.end
|
|
875
|
+
? selBounds.end // al]pha
|
|
876
|
+
: tokBounds.end) - // alpha]
|
|
877
|
+
Math.max(tokBounds.start, selBounds.start);
|
|
878
|
+
}
|
|
879
|
+
return {
|
|
880
|
+
y: 0,
|
|
881
|
+
x,
|
|
882
|
+
at,
|
|
883
|
+
run,
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
getEndCoordsFromRange(selStart, selEnd, line) {
|
|
887
|
+
// get adjusted selection bounds
|
|
888
|
+
const selBounds = this.getAdjustedSelectionBounds(selStart, selEnd, line);
|
|
889
|
+
// get the bounds of the last selected token
|
|
890
|
+
const tokBounds = this.getTokenBounds(line, selBounds.end - 1);
|
|
891
|
+
// calculate x by counting the tokens before the target token
|
|
892
|
+
let x = 1;
|
|
893
|
+
let j = selBounds.end - 1;
|
|
894
|
+
while (j > -1) {
|
|
895
|
+
if (line.charAt(j) === ' ') {
|
|
896
|
+
x++;
|
|
897
|
+
}
|
|
898
|
+
j--;
|
|
899
|
+
}
|
|
900
|
+
// calculate at,run only when selection ends before the 1st char of the token
|
|
901
|
+
// (al]pha); run is calculated only if at is calculated.
|
|
902
|
+
let at = 0;
|
|
903
|
+
let run = 0;
|
|
904
|
+
if (selBounds.end < tokBounds.end) {
|
|
905
|
+
at =
|
|
906
|
+
selBounds.start > tokBounds.start
|
|
907
|
+
? 1 + selBounds.start - tokBounds.start
|
|
908
|
+
: 1;
|
|
909
|
+
run = selBounds.end - tokBounds.start;
|
|
910
|
+
}
|
|
911
|
+
return {
|
|
912
|
+
y: 0,
|
|
913
|
+
x,
|
|
914
|
+
at,
|
|
915
|
+
run,
|
|
916
|
+
};
|
|
917
|
+
}
|
|
918
|
+
getSpanLocFromId(id) {
|
|
919
|
+
if (!id) {
|
|
920
|
+
return null;
|
|
921
|
+
}
|
|
922
|
+
const r = new RegExp('^f([-\\d+.@x]+)', 'g');
|
|
923
|
+
const m = r.exec(id);
|
|
924
|
+
if (!m) {
|
|
925
|
+
return null;
|
|
926
|
+
}
|
|
927
|
+
return TokenLocation.parse(m[1]);
|
|
928
|
+
}
|
|
929
|
+
visitNodes(node, visitor) {
|
|
930
|
+
if (!visitor(node)) {
|
|
931
|
+
return;
|
|
932
|
+
}
|
|
933
|
+
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
934
|
+
for (let i = 0; i < node.childNodes.length; i++) {
|
|
935
|
+
const child = node.childNodes[i];
|
|
936
|
+
this.visitNodes(child, visitor);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
findFirstDescendantSpan(range) {
|
|
941
|
+
let inside = false;
|
|
942
|
+
let span = null;
|
|
943
|
+
this.visitNodes(range.commonAncestorContainer, (current) => {
|
|
944
|
+
if (current === range.startContainer) {
|
|
945
|
+
inside = true;
|
|
946
|
+
}
|
|
947
|
+
if (current.nodeType === Node.ELEMENT_NODE &&
|
|
948
|
+
current.nodeName === 'SPAN') {
|
|
949
|
+
span = current;
|
|
950
|
+
return false;
|
|
951
|
+
}
|
|
952
|
+
if (current === range.endContainer) {
|
|
953
|
+
inside = false;
|
|
954
|
+
}
|
|
955
|
+
return true;
|
|
956
|
+
});
|
|
957
|
+
return span;
|
|
958
|
+
}
|
|
959
|
+
/**
|
|
960
|
+
* Get the base offset for the specified node. This is used
|
|
961
|
+
* when the start container of the selected range has any
|
|
962
|
+
* preceding sibling node, which happens only when you have
|
|
963
|
+
* any spans in the p element (=the line). In this case,
|
|
964
|
+
* the selection offset starts from the start container node,
|
|
965
|
+
* which is preceded by other nodes including other text.
|
|
966
|
+
* As we must get the line-based offset, we have to start
|
|
967
|
+
* from a base offset equal to the sum of the lengths of
|
|
968
|
+
* all the preceding siblings.
|
|
969
|
+
*/
|
|
970
|
+
getNodeBaseOffset(node) {
|
|
971
|
+
var _a, _b;
|
|
972
|
+
let offset = 0;
|
|
973
|
+
let n = node;
|
|
974
|
+
n = node.previousSibling;
|
|
975
|
+
while (n) {
|
|
976
|
+
offset +=
|
|
977
|
+
n.nodeType === Node.ELEMENT_NODE
|
|
978
|
+
? ((_a = n.innerText) === null || _a === void 0 ? void 0 : _a.length) || 0
|
|
979
|
+
: ((_b = n.textContent) === null || _b === void 0 ? void 0 : _b.length) || 0;
|
|
980
|
+
n = n.previousSibling;
|
|
981
|
+
}
|
|
982
|
+
return offset;
|
|
983
|
+
}
|
|
984
|
+
/**
|
|
985
|
+
* Get the location corresponding to the user selection,
|
|
986
|
+
* for editing the first fragment included in it.
|
|
987
|
+
* @param range The selected range. You can get it from the
|
|
988
|
+
* current selection using getSelectedRange().
|
|
989
|
+
* @returns The token location or null.
|
|
990
|
+
*/
|
|
991
|
+
getSelectedLocationForEdit(range) {
|
|
992
|
+
if (!range) {
|
|
993
|
+
return null;
|
|
994
|
+
}
|
|
995
|
+
// just find the first descendant span in selection
|
|
996
|
+
let span = null;
|
|
997
|
+
switch (range.commonAncestorContainer.nodeType) {
|
|
998
|
+
case Node.ELEMENT_NODE:
|
|
999
|
+
span = range.commonAncestorContainer;
|
|
1000
|
+
if (span.nodeName !== 'SPAN') {
|
|
1001
|
+
span = this.findFirstDescendantSpan(range);
|
|
1002
|
+
}
|
|
1003
|
+
break;
|
|
1004
|
+
case Node.TEXT_NODE:
|
|
1005
|
+
span = range.commonAncestorContainer.parentElement;
|
|
1006
|
+
if (span &&
|
|
1007
|
+
(span.nodeType !== Node.ELEMENT_NODE || span.nodeName !== 'SPAN')) {
|
|
1008
|
+
span = null;
|
|
1009
|
+
}
|
|
1010
|
+
break;
|
|
1011
|
+
}
|
|
1012
|
+
return span ? this.getSpanLocFromId(span.getAttribute('id')) : null;
|
|
1013
|
+
}
|
|
1014
|
+
textLineToString(line) {
|
|
1015
|
+
if (!line || !line.tokens) {
|
|
1016
|
+
return '';
|
|
1017
|
+
}
|
|
1018
|
+
return line.tokens.join(' ');
|
|
1019
|
+
}
|
|
1020
|
+
buildCoords(start, end, singleToken) {
|
|
1021
|
+
const sb = [];
|
|
1022
|
+
// start: y.x
|
|
1023
|
+
sb.push(`${start.y}.${start.x}`);
|
|
1024
|
+
if (singleToken) {
|
|
1025
|
+
// single token: check for at,run
|
|
1026
|
+
if (start.at) {
|
|
1027
|
+
sb.push(`@${start.at}`);
|
|
1028
|
+
if (start.run && start.run > 1) {
|
|
1029
|
+
sb.push(`x${start.run}`);
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
else {
|
|
1034
|
+
// not a single token
|
|
1035
|
+
// first token
|
|
1036
|
+
if (start.at) {
|
|
1037
|
+
sb.push(`@${start.at}x${start.run}`);
|
|
1038
|
+
}
|
|
1039
|
+
// last token
|
|
1040
|
+
sb.push(`-${end.y}.${end.x}`);
|
|
1041
|
+
if (end.at) {
|
|
1042
|
+
sb.push(`@1x${end.run}`);
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
return sb.join('');
|
|
1046
|
+
}
|
|
1047
|
+
/**
|
|
1048
|
+
* Recursively find the node matching the specified matcher
|
|
1049
|
+
* function, starting from the specified node (included).
|
|
1050
|
+
*
|
|
1051
|
+
* @param node The node to start from.
|
|
1052
|
+
* @param matcher The matcher function, which receives a node
|
|
1053
|
+
* to match, and returns 1=match, 0=no match, -1=stop the
|
|
1054
|
+
* find operation with a no match.
|
|
1055
|
+
*/
|
|
1056
|
+
findNode(node, matcher) {
|
|
1057
|
+
// self
|
|
1058
|
+
const n = matcher(node);
|
|
1059
|
+
if (n === 1) {
|
|
1060
|
+
return node;
|
|
1061
|
+
}
|
|
1062
|
+
if (n === -1) {
|
|
1063
|
+
return null;
|
|
1064
|
+
}
|
|
1065
|
+
// children
|
|
1066
|
+
if (node.hasChildNodes()) {
|
|
1067
|
+
for (let i = 0; i < node.childNodes.length; i++) {
|
|
1068
|
+
const child = node.childNodes[i];
|
|
1069
|
+
const found = this.findNode(child, matcher);
|
|
1070
|
+
if (found) {
|
|
1071
|
+
return found;
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
return null;
|
|
1076
|
+
}
|
|
1077
|
+
selHasAnySpan(range) {
|
|
1078
|
+
var _a, _b;
|
|
1079
|
+
let inside = false;
|
|
1080
|
+
if (
|
|
1081
|
+
// the selected text node must not be child of a span
|
|
1082
|
+
((_a = range.startContainer.parentElement) === null || _a === void 0 ? void 0 : _a.tagName) === 'SPAN' ||
|
|
1083
|
+
((_b = range.startContainer.parentElement) === null || _b === void 0 ? void 0 : _b.tagName) === 'SPAN' ||
|
|
1084
|
+
// the nodes tree from the common ancestor, from the start node
|
|
1085
|
+
// up to the end node, must not include any span
|
|
1086
|
+
this.findNode(range.commonAncestorContainer, (node) => {
|
|
1087
|
+
// start checking only when start node reached
|
|
1088
|
+
if (node === range.startContainer) {
|
|
1089
|
+
inside = true;
|
|
1090
|
+
}
|
|
1091
|
+
if (!inside) {
|
|
1092
|
+
return 0;
|
|
1093
|
+
}
|
|
1094
|
+
// stop checking when end node reached
|
|
1095
|
+
if (node === range.endContainer) {
|
|
1096
|
+
return -1;
|
|
1097
|
+
}
|
|
1098
|
+
// node cannot be a span
|
|
1099
|
+
if (node.nodeType === Node.ELEMENT_NODE &&
|
|
1100
|
+
node.tagName === 'SPAN') {
|
|
1101
|
+
return 1;
|
|
1102
|
+
}
|
|
1103
|
+
return 0;
|
|
1104
|
+
})) {
|
|
1105
|
+
return true;
|
|
1106
|
+
}
|
|
1107
|
+
return false;
|
|
1108
|
+
}
|
|
1109
|
+
/**
|
|
1110
|
+
* Get the location corresponding to the user selection,
|
|
1111
|
+
* for adding a new fragment with the same extension.
|
|
1112
|
+
* @param range The selected range. You can get it from the
|
|
1113
|
+
* current selection using getSelectedRange().
|
|
1114
|
+
* @param text The full base text.
|
|
1115
|
+
* @returns TokenLocation The token location or undefined.
|
|
1116
|
+
*/
|
|
1117
|
+
getSelectedLocationForNew(range, text) {
|
|
1118
|
+
if (!range || this.selHasAnySpan(range)) {
|
|
1119
|
+
return null;
|
|
1120
|
+
}
|
|
1121
|
+
// get y bounds (min, max; equal if single-line)
|
|
1122
|
+
const yBounds = this.getYBoundsFromRange(range, true);
|
|
1123
|
+
if (!yBounds) {
|
|
1124
|
+
return null;
|
|
1125
|
+
}
|
|
1126
|
+
// get lines and calculate start/end coords
|
|
1127
|
+
const lines = this.getLines(text);
|
|
1128
|
+
const startLine = this.textLineToString(lines[yBounds.start - 1]);
|
|
1129
|
+
const endLine = this.textLineToString(lines[yBounds.end - 1]);
|
|
1130
|
+
// get the base offsets
|
|
1131
|
+
const startBaseOffset = this.getNodeBaseOffset(range.startContainer);
|
|
1132
|
+
const endBaseOffset = this.getNodeBaseOffset(range.endContainer);
|
|
1133
|
+
// get the start (including the base offset)
|
|
1134
|
+
const startLineEndOffset = yBounds.start !== yBounds.end
|
|
1135
|
+
? startLine.length
|
|
1136
|
+
: range.endOffset + startBaseOffset;
|
|
1137
|
+
const start = this.getStartCoordsFromRange(range.startOffset + startBaseOffset, startLineEndOffset, startLine);
|
|
1138
|
+
start.y = yBounds.start;
|
|
1139
|
+
// get the end (including the base offset)
|
|
1140
|
+
const end = yBounds.start !== yBounds.end
|
|
1141
|
+
? this.getEndCoordsFromRange(0, range.endOffset + endBaseOffset, endLine)
|
|
1142
|
+
: this.getEndCoordsFromRange(range.startOffset + endBaseOffset, range.endOffset + endBaseOffset, endLine);
|
|
1143
|
+
end.y = yBounds.end;
|
|
1144
|
+
const singleLine = start.y === end.y;
|
|
1145
|
+
const singleToken = singleLine && start.x === end.x;
|
|
1146
|
+
const coords = this.buildCoords(start, end, singleToken);
|
|
1147
|
+
return TokenLocation.parse(coords);
|
|
1148
|
+
}
|
|
1149
|
+
/**
|
|
1150
|
+
* Get the currently selected range in the browser's document.
|
|
1151
|
+
* This uses the rangy library.
|
|
1152
|
+
*/
|
|
1153
|
+
getSelectedRange() {
|
|
1154
|
+
const sel = rangy.getSelection();
|
|
1155
|
+
if (sel.isCollapsed) {
|
|
1156
|
+
return null;
|
|
1157
|
+
}
|
|
1158
|
+
return sel.getRangeAt(0);
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
TextLayerService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.3", ngImport: i0, type: TextLayerService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
1162
|
+
TextLayerService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.0.3", ngImport: i0, type: TextLayerService, providedIn: 'root' });
|
|
1163
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.3", ngImport: i0, type: TextLayerService, decorators: [{
|
|
1164
|
+
type: Injectable,
|
|
1165
|
+
args: [{ providedIn: 'root' }]
|
|
1166
|
+
}], ctorParameters: function () { return []; } });
|
|
1167
|
+
|
|
1168
|
+
// https://github.com/angular/angular/blob/3b7d4ebbd6445364889d61b7b1b1c65206534d34/packages/forms/src/validators.ts
|
|
1169
|
+
// https://indepth.dev/posts/1319/the-best-way-to-implement-custom-validators
|
|
1170
|
+
/**
|
|
1171
|
+
* Cadmus general-purpose custom validators.
|
|
1172
|
+
*/
|
|
1173
|
+
// @dynamic
|
|
1174
|
+
class CadmusValidators {
|
|
1175
|
+
static hasValidLength(value) {
|
|
1176
|
+
// non-strict comparison is intentional,
|
|
1177
|
+
// to check for both `null` and `undefined` values
|
|
1178
|
+
return value != null && typeof value.length === 'number';
|
|
1179
|
+
}
|
|
1180
|
+
/**
|
|
1181
|
+
* Validate an array or string for a minimum required length.
|
|
1182
|
+
* Differently from the standard minLength validator, this does
|
|
1183
|
+
* not exclude 0 from the values to be checked. The standard
|
|
1184
|
+
* validator instead does not validate the value if this is 0.
|
|
1185
|
+
*
|
|
1186
|
+
* @param minLength Minimum length required.
|
|
1187
|
+
* @returns Null if valid, else an object with minlength with
|
|
1188
|
+
* requiredLength and actualLength.
|
|
1189
|
+
*/
|
|
1190
|
+
static strictMinLengthValidator(minLength) {
|
|
1191
|
+
return (control) => {
|
|
1192
|
+
// don't validate values without `length` property
|
|
1193
|
+
if (!this.hasValidLength(control.value)) {
|
|
1194
|
+
return null;
|
|
1195
|
+
}
|
|
1196
|
+
return control.value.length < minLength
|
|
1197
|
+
? {
|
|
1198
|
+
minlength: {
|
|
1199
|
+
requiredLength: minLength,
|
|
1200
|
+
actualLength: control.value.length,
|
|
1201
|
+
},
|
|
1202
|
+
}
|
|
1203
|
+
: null;
|
|
1204
|
+
};
|
|
1205
|
+
}
|
|
1206
|
+
/**
|
|
1207
|
+
* Conditionally validate using the specified validator
|
|
1208
|
+
* when predicate is true.
|
|
1209
|
+
* See https://medium.com/ngx/3-ways-to-implement-conditional-validation-of-reactive-forms-c59ed6fc3325.
|
|
1210
|
+
*
|
|
1211
|
+
* @param predicate The predicate function to use when determining
|
|
1212
|
+
* if the validator should be used.
|
|
1213
|
+
* @param validator The conditional validator to use.
|
|
1214
|
+
* @returns Validation results.
|
|
1215
|
+
*/
|
|
1216
|
+
static conditionalValidator(predicate, validator) {
|
|
1217
|
+
return (control) => {
|
|
1218
|
+
if (!control.parent) {
|
|
1219
|
+
return null;
|
|
1220
|
+
}
|
|
1221
|
+
if (predicate()) {
|
|
1222
|
+
return validator(control);
|
|
1223
|
+
}
|
|
1224
|
+
return null;
|
|
1225
|
+
};
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
class CadmusCoreModule {
|
|
1230
|
+
}
|
|
1231
|
+
CadmusCoreModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.3", ngImport: i0, type: CadmusCoreModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
|
|
1232
|
+
CadmusCoreModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.0.3", ngImport: i0, type: CadmusCoreModule, declarations: [SortPipe], imports: [CommonModule, HttpClientModule, NgToolsModule], exports: [SortPipe] });
|
|
1233
|
+
CadmusCoreModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.0.3", ngImport: i0, type: CadmusCoreModule, imports: [[CommonModule, HttpClientModule, NgToolsModule]] });
|
|
1234
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.3", ngImport: i0, type: CadmusCoreModule, decorators: [{
|
|
1235
|
+
type: NgModule,
|
|
1236
|
+
args: [{
|
|
1237
|
+
imports: [CommonModule, HttpClientModule, NgToolsModule],
|
|
1238
|
+
declarations: [SortPipe],
|
|
1239
|
+
exports: [SortPipe],
|
|
1240
|
+
}]
|
|
1241
|
+
}] });
|
|
1242
|
+
|
|
1243
|
+
/*
|
|
1244
|
+
* Public API Surface of cadmus-core
|
|
1245
|
+
*/
|
|
1246
|
+
|
|
1247
|
+
/**
|
|
1248
|
+
* Generated bundle index. Do not edit.
|
|
1249
|
+
*/
|
|
1250
|
+
|
|
1251
|
+
export { CadmusCoreModule, CadmusValidators, DataPinValueType, LibraryRouteService, PendingChangesGuard, SortPipe, TextLayerService, TextRange, TokenLocation, TokenPoint };
|
|
1252
|
+
//# sourceMappingURL=myrmidon-cadmus-core.mjs.map
|