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