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