@manifest-editor/creator-api 1.4.3 → 2.0.1-next.0.ede7d58
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/dist/index.cjs +1 -662
- package/dist/index.d.cts +274 -232
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +274 -232
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -651
- package/dist/index.js.map +1 -0
- package/package.json +18 -23
package/dist/index.js
CHANGED
|
@@ -1,651 +1,2 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
|
|
3
|
-
var __publicField = (obj, key, value) => {
|
|
4
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
-
return value;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
// src/Creator.ts
|
|
9
|
-
import { Vault } from "@iiif/helpers/vault";
|
|
10
|
-
import { entityActions } from "@iiif/helpers/vault/actions";
|
|
11
|
-
|
|
12
|
-
// src/CreatorRuntime.ts
|
|
13
|
-
import { addMappings, batchActions, importEntities } from "@iiif/helpers/vault/actions";
|
|
14
|
-
|
|
15
|
-
// src/CreatorResource.ts
|
|
16
|
-
import { HAS_PART, PART_OF } from "@iiif/parser";
|
|
17
|
-
import { references } from "@manifest-editor/editor-api";
|
|
18
|
-
|
|
19
|
-
// src/ReferencedResource.ts
|
|
20
|
-
import { isSpecificResource } from "@iiif/parser";
|
|
21
|
-
var ReferencedResource = class {
|
|
22
|
-
constructor(reference, vault) {
|
|
23
|
-
__publicField(this, "vault");
|
|
24
|
-
__publicField(this, "original");
|
|
25
|
-
__publicField(this, "reference");
|
|
26
|
-
__publicField(this, "resource");
|
|
27
|
-
this.vault = vault;
|
|
28
|
-
this.original = reference;
|
|
29
|
-
this.reference = isSpecificResource(reference) ? reference.source : reference;
|
|
30
|
-
this.resource = isSpecificResource(reference) ? reference : { type: "SpecificResource", source: this.reference };
|
|
31
|
-
}
|
|
32
|
-
// @todo editable specific resource.
|
|
33
|
-
ref() {
|
|
34
|
-
return this.reference;
|
|
35
|
-
}
|
|
36
|
-
specificResource() {
|
|
37
|
-
return this.resource;
|
|
38
|
-
}
|
|
39
|
-
optionalSpecificResource() {
|
|
40
|
-
if (isSpecificResource(this.original)) {
|
|
41
|
-
return {
|
|
42
|
-
...this.original,
|
|
43
|
-
source: { id: this.original.source?.id, type: this.original.source?.type }
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
return this.original;
|
|
47
|
-
}
|
|
48
|
-
get() {
|
|
49
|
-
return this.vault.get(this.resource);
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
// src/utils.ts
|
|
54
|
-
import {
|
|
55
|
-
emptyAgent,
|
|
56
|
-
emptyAnnotation,
|
|
57
|
-
emptyAnnotationPage,
|
|
58
|
-
emptyCanvas,
|
|
59
|
-
emptyCollection,
|
|
60
|
-
emptyManifest,
|
|
61
|
-
emptyRange,
|
|
62
|
-
emptyService,
|
|
63
|
-
toRef
|
|
64
|
-
} from "@iiif/parser";
|
|
65
|
-
function resolveType(type) {
|
|
66
|
-
switch (type) {
|
|
67
|
-
case "Image":
|
|
68
|
-
case "Video":
|
|
69
|
-
case "Sound":
|
|
70
|
-
case "Dataset":
|
|
71
|
-
case "Text":
|
|
72
|
-
case "TextualBody":
|
|
73
|
-
case "Composite":
|
|
74
|
-
case "List":
|
|
75
|
-
case "Independents":
|
|
76
|
-
case "Audience":
|
|
77
|
-
return "ContentResource";
|
|
78
|
-
case "ImageService1":
|
|
79
|
-
case "ImageService2":
|
|
80
|
-
case "ImageService3":
|
|
81
|
-
return "Service";
|
|
82
|
-
}
|
|
83
|
-
return type;
|
|
84
|
-
}
|
|
85
|
-
var emptyTypes = {
|
|
86
|
-
Annotation: emptyAnnotation,
|
|
87
|
-
AnnotationPage: emptyAnnotationPage,
|
|
88
|
-
Canvas: emptyCanvas,
|
|
89
|
-
Collection: emptyCollection,
|
|
90
|
-
Manifest: emptyManifest,
|
|
91
|
-
Range: emptyRange,
|
|
92
|
-
ResourceProvider: emptyAgent,
|
|
93
|
-
Service: emptyService
|
|
94
|
-
};
|
|
95
|
-
function getEmptyType(type) {
|
|
96
|
-
const key = resolveType(type);
|
|
97
|
-
return emptyTypes[key] || {};
|
|
98
|
-
}
|
|
99
|
-
function randomId() {
|
|
100
|
-
return `${Math.random().toString(36).substr(2)}-${Date.now().toString(36)}`;
|
|
101
|
-
}
|
|
102
|
-
function matchBasedOnResource(resource, list, options) {
|
|
103
|
-
const supported = [];
|
|
104
|
-
if (list.length === 0) {
|
|
105
|
-
return [];
|
|
106
|
-
}
|
|
107
|
-
const parent = toRef(resource.parent);
|
|
108
|
-
const property = resource.property;
|
|
109
|
-
const filter = resource.filter;
|
|
110
|
-
for (const def of list) {
|
|
111
|
-
if (filter) {
|
|
112
|
-
if (!def.tags)
|
|
113
|
-
continue;
|
|
114
|
-
if (!def.tags.includes(filter)) {
|
|
115
|
-
continue;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
if (parent && property) {
|
|
119
|
-
if (def.supports.parentTypes) {
|
|
120
|
-
if (!def.supports.parentTypes.includes(parent.type)) {
|
|
121
|
-
continue;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
if (def.supports.parentFields) {
|
|
125
|
-
if (!def.supports.parentFields.includes(property)) {
|
|
126
|
-
continue;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
if (def.supports.parentFieldMap) {
|
|
130
|
-
const type = def.supports.parentFieldMap[parent.type];
|
|
131
|
-
if (!type || !type.includes(property)) {
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
if (def.supports.disallowPainting && resource.isPainting) {
|
|
136
|
-
continue;
|
|
137
|
-
}
|
|
138
|
-
if (def.supports.custom) {
|
|
139
|
-
const isValid = def.supports.custom({ property, resource: parent, atIndex: resource.index }, options.vault);
|
|
140
|
-
if (!isValid) {
|
|
141
|
-
continue;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
if (!def.supports.initialData && Object.keys(resource.initialData || {}).length !== 0) {
|
|
145
|
-
continue;
|
|
146
|
-
}
|
|
147
|
-
if (def.resourceType !== resource.type) {
|
|
148
|
-
if (!(def.additionalTypes || []).includes(resource.type)) {
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
supported.push(def);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
return supported;
|
|
156
|
-
}
|
|
157
|
-
function creatorHelper(ctx, parent, property, definition) {
|
|
158
|
-
return (payload, options = {}) => {
|
|
159
|
-
if (typeof parent !== "string") {
|
|
160
|
-
options.parent = {
|
|
161
|
-
property,
|
|
162
|
-
resource: parent,
|
|
163
|
-
atIndex: options?.parent?.atIndex
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
return ctx.create(definition, payload, options);
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// src/CreatorResource.ts
|
|
171
|
-
var CreatorResource = class _CreatorResource {
|
|
172
|
-
constructor(inputData, vault) {
|
|
173
|
-
__publicField(this, "resource");
|
|
174
|
-
__publicField(this, "warnings", []);
|
|
175
|
-
__publicField(this, "errors", []);
|
|
176
|
-
__publicField(this, "partOf");
|
|
177
|
-
__publicField(this, "references", []);
|
|
178
|
-
__publicField(this, "embedded", []);
|
|
179
|
-
__publicField(this, "vault");
|
|
180
|
-
__publicField(this, "specificResource");
|
|
181
|
-
this.vault = vault;
|
|
182
|
-
let data = { ...inputData };
|
|
183
|
-
if (data.type === "SpecificResource") {
|
|
184
|
-
const source = data.source;
|
|
185
|
-
this.specificResource = { ...data, source: { id: source.id, type: resolveType(source.type) } };
|
|
186
|
-
data = source;
|
|
187
|
-
}
|
|
188
|
-
const properties = Object.keys(data);
|
|
189
|
-
const defaultType = getEmptyType(data.type);
|
|
190
|
-
for (const key of properties) {
|
|
191
|
-
if (references.inlineProperties.includes(key) || !references.all.includes(key)) {
|
|
192
|
-
continue;
|
|
193
|
-
}
|
|
194
|
-
if (data.type === "Range" && key === "items") {
|
|
195
|
-
const newRange = [];
|
|
196
|
-
for (const range of data[key]) {
|
|
197
|
-
if (range instanceof ReferencedResource) {
|
|
198
|
-
this.references.push(range);
|
|
199
|
-
newRange.push(range);
|
|
200
|
-
continue;
|
|
201
|
-
}
|
|
202
|
-
const rangeRef = new ReferencedResource(range, vault);
|
|
203
|
-
newRange.push(rangeRef);
|
|
204
|
-
data[key] = rangeRef;
|
|
205
|
-
}
|
|
206
|
-
data[key] = newRange;
|
|
207
|
-
continue;
|
|
208
|
-
}
|
|
209
|
-
if (data.type === "Manifest" && key === "start") {
|
|
210
|
-
const start = data[key];
|
|
211
|
-
if (start instanceof ReferencedResource) {
|
|
212
|
-
this.references.push(start);
|
|
213
|
-
continue;
|
|
214
|
-
}
|
|
215
|
-
const startRef = new ReferencedResource(start, vault);
|
|
216
|
-
data[key] = startRef;
|
|
217
|
-
continue;
|
|
218
|
-
}
|
|
219
|
-
if (data.type === "Annotation" && key === "target") {
|
|
220
|
-
let target = data[key];
|
|
221
|
-
if (target instanceof ReferencedResource) {
|
|
222
|
-
this.references.push(target);
|
|
223
|
-
continue;
|
|
224
|
-
}
|
|
225
|
-
if (typeof target === "string") {
|
|
226
|
-
target = { id: target, type: "Canvas" };
|
|
227
|
-
}
|
|
228
|
-
const targetRef = new ReferencedResource(target, vault);
|
|
229
|
-
data[key] = targetRef;
|
|
230
|
-
continue;
|
|
231
|
-
}
|
|
232
|
-
if (references.single.includes(key)) {
|
|
233
|
-
continue;
|
|
234
|
-
}
|
|
235
|
-
if (references.externalProperties.includes(key) || references.internalProperties.includes(key)) {
|
|
236
|
-
const _items = data[key] || [];
|
|
237
|
-
const items = Array.isArray(_items) ? _items : [_items];
|
|
238
|
-
const newItems = [];
|
|
239
|
-
for (const item of items) {
|
|
240
|
-
if (item instanceof ReferencedResource) {
|
|
241
|
-
this.references.push(item);
|
|
242
|
-
newItems.push(item);
|
|
243
|
-
continue;
|
|
244
|
-
}
|
|
245
|
-
if (item instanceof _CreatorResource) {
|
|
246
|
-
this.embedded.push(item);
|
|
247
|
-
newItems.push(item);
|
|
248
|
-
continue;
|
|
249
|
-
}
|
|
250
|
-
const exists = this.vault.get(item, { skipSelfReturn: true });
|
|
251
|
-
if (exists) {
|
|
252
|
-
const type = resolveType(item.type);
|
|
253
|
-
const newItem = new ReferencedResource({ id: item.id, type }, vault);
|
|
254
|
-
this.references.push(newItem);
|
|
255
|
-
newItems.push(newItem);
|
|
256
|
-
} else {
|
|
257
|
-
const newItem = new _CreatorResource(item, vault);
|
|
258
|
-
this.embedded.push(newItem);
|
|
259
|
-
newItems.push(newItem);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
data[key] = newItems;
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
this.resource = { ...defaultType, ...data };
|
|
266
|
-
}
|
|
267
|
-
// This is what is returned from the creator instance
|
|
268
|
-
get() {
|
|
269
|
-
return this.resource;
|
|
270
|
-
}
|
|
271
|
-
ref() {
|
|
272
|
-
if (this.specificResource) {
|
|
273
|
-
return this.specificResource;
|
|
274
|
-
}
|
|
275
|
-
return { id: this.resource.id, type: resolveType(this.resource.type) };
|
|
276
|
-
}
|
|
277
|
-
getAllReferencedResources() {
|
|
278
|
-
return this.references;
|
|
279
|
-
}
|
|
280
|
-
getAllEmbeddedResources() {
|
|
281
|
-
const allEmbedded = [];
|
|
282
|
-
for (const resource of this.embedded) {
|
|
283
|
-
allEmbedded.push(...resource.getAllEmbeddedResources());
|
|
284
|
-
}
|
|
285
|
-
allEmbedded.push(...this.embedded);
|
|
286
|
-
return allEmbedded;
|
|
287
|
-
}
|
|
288
|
-
getVaultResource() {
|
|
289
|
-
const resource = this.resource;
|
|
290
|
-
const properties = Object.keys(resource);
|
|
291
|
-
const newResource = {};
|
|
292
|
-
for (const key of properties) {
|
|
293
|
-
if (references.inlineProperties.includes(key) || !references.all.includes(key)) {
|
|
294
|
-
newResource[key] = resource[key];
|
|
295
|
-
continue;
|
|
296
|
-
}
|
|
297
|
-
if (references.single.includes(key)) {
|
|
298
|
-
const ref = resource[key];
|
|
299
|
-
if (ref instanceof ReferencedResource) {
|
|
300
|
-
if (resource.type === "Manifest" && key === "start" || resource.type === "Annotation" && key === "target") {
|
|
301
|
-
newResource[key] = ref.specificResource();
|
|
302
|
-
} else {
|
|
303
|
-
newResource[key] = ref.optionalSpecificResource();
|
|
304
|
-
}
|
|
305
|
-
} else {
|
|
306
|
-
newResource[key] = resource[key];
|
|
307
|
-
}
|
|
308
|
-
continue;
|
|
309
|
-
}
|
|
310
|
-
if (resource[key]) {
|
|
311
|
-
newResource[key] = [];
|
|
312
|
-
for (const item of resource[key] || []) {
|
|
313
|
-
if (item instanceof ReferencedResource) {
|
|
314
|
-
newResource[key].push(item.optionalSpecificResource());
|
|
315
|
-
}
|
|
316
|
-
if (item instanceof _CreatorResource) {
|
|
317
|
-
newResource[key].push(item.ref());
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
if (this.partOf) {
|
|
323
|
-
newResource[HAS_PART] = newResource[HAS_PART] || [];
|
|
324
|
-
newResource[HAS_PART].push(this.partOf);
|
|
325
|
-
}
|
|
326
|
-
return newResource;
|
|
327
|
-
}
|
|
328
|
-
setPartOf(id) {
|
|
329
|
-
const ref = this.ref();
|
|
330
|
-
this.partOf = {
|
|
331
|
-
id: ref.id,
|
|
332
|
-
type: resolveType(ref.type),
|
|
333
|
-
[PART_OF]: id
|
|
334
|
-
};
|
|
335
|
-
}
|
|
336
|
-
};
|
|
337
|
-
|
|
338
|
-
// src/CreatorInstance.ts
|
|
339
|
-
var CreatorInstance = class {
|
|
340
|
-
constructor(vault, options, createConfigs, previewVault) {
|
|
341
|
-
__publicField(this, "vault");
|
|
342
|
-
__publicField(this, "previewVault");
|
|
343
|
-
__publicField(this, "configs");
|
|
344
|
-
__publicField(this, "options");
|
|
345
|
-
this.vault = vault;
|
|
346
|
-
this.previewVault = previewVault;
|
|
347
|
-
this.options = options;
|
|
348
|
-
this.configs = createConfigs;
|
|
349
|
-
}
|
|
350
|
-
getPreviewVault() {
|
|
351
|
-
return this.previewVault;
|
|
352
|
-
}
|
|
353
|
-
getTarget() {
|
|
354
|
-
const target = this.options.target || this.options.parent?.resource;
|
|
355
|
-
const position = this.options.initialData?.selector;
|
|
356
|
-
if (position && position.type === "polygon") {
|
|
357
|
-
const position2 = this.options.initialData?.selector;
|
|
358
|
-
const { width, height } = this.options.initialData?.on || {};
|
|
359
|
-
if (position2.shape.points.length === 0) {
|
|
360
|
-
return target;
|
|
361
|
-
}
|
|
362
|
-
if (position2.shape.points.length === 1) {
|
|
363
|
-
const points = position2.shape.points[0];
|
|
364
|
-
return {
|
|
365
|
-
type: "SpecificResource",
|
|
366
|
-
source: target,
|
|
367
|
-
selector: {
|
|
368
|
-
type: "PointSelector",
|
|
369
|
-
x: points[0],
|
|
370
|
-
y: points[1]
|
|
371
|
-
}
|
|
372
|
-
};
|
|
373
|
-
}
|
|
374
|
-
if (position2.shape.points.length === 4) {
|
|
375
|
-
const [topLeft, topRight, bottomRight, bottomLeft] = position2.shape.points;
|
|
376
|
-
if (topLeft[0] === topRight[0] && topLeft[1] === bottomLeft[1] && topRight[1] === bottomRight[1] && bottomLeft[0] === bottomRight[0]) {
|
|
377
|
-
const x = Math.min(topLeft[0], topRight[0], bottomRight[0], bottomLeft[0]);
|
|
378
|
-
const y = Math.min(topLeft[1], topRight[1], bottomRight[1], bottomLeft[1]);
|
|
379
|
-
const x2 = Math.max(topLeft[0], topRight[0], bottomRight[0], bottomLeft[0]);
|
|
380
|
-
const y2 = Math.max(topLeft[1], topRight[1], bottomRight[1], bottomLeft[1]);
|
|
381
|
-
const width2 = x2 - x;
|
|
382
|
-
const height2 = y2 - y;
|
|
383
|
-
return {
|
|
384
|
-
type: "SpecificResource",
|
|
385
|
-
source: target,
|
|
386
|
-
selector: {
|
|
387
|
-
type: "FragmentSelector",
|
|
388
|
-
value: `xywh=${[~~x, ~~y, ~~width2, ~~height2].join(",")}`
|
|
389
|
-
}
|
|
390
|
-
};
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
const el = position2.shape.open ? "polyline" : "polygon";
|
|
394
|
-
return {
|
|
395
|
-
type: "SpecificResource",
|
|
396
|
-
source: target,
|
|
397
|
-
selector: {
|
|
398
|
-
type: "SvgSelector",
|
|
399
|
-
value: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}" width="${width}" height="${height}"><${el} points="${position2.shape.points.map((p) => p.join(",")).join(" ")}" /></svg>`
|
|
400
|
-
}
|
|
401
|
-
};
|
|
402
|
-
}
|
|
403
|
-
if (target && position) {
|
|
404
|
-
return {
|
|
405
|
-
type: "SpecificResource",
|
|
406
|
-
source: target,
|
|
407
|
-
selector: {
|
|
408
|
-
type: "FragmentSelector",
|
|
409
|
-
value: `xywh=${[~~position.x, ~~position.y, ~~position.width, ~~position.height].join(",")}`
|
|
410
|
-
}
|
|
411
|
-
};
|
|
412
|
-
}
|
|
413
|
-
return target;
|
|
414
|
-
}
|
|
415
|
-
getParent() {
|
|
416
|
-
return this.options.parent?.resource;
|
|
417
|
-
}
|
|
418
|
-
getParentResource() {
|
|
419
|
-
const parent = this.getParent();
|
|
420
|
-
if (parent) {
|
|
421
|
-
return {
|
|
422
|
-
type: "SpecificResource",
|
|
423
|
-
source: parent
|
|
424
|
-
};
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
generateId(type, parent) {
|
|
428
|
-
if (parent && parent instanceof ReferencedResource) {
|
|
429
|
-
parent = parent.ref();
|
|
430
|
-
}
|
|
431
|
-
return `${(parent || this.options.parent?.resource)?.id}/${type}/${randomId()}`;
|
|
432
|
-
}
|
|
433
|
-
ref(idOrRef) {
|
|
434
|
-
let id = "";
|
|
435
|
-
let type = "";
|
|
436
|
-
if (typeof idOrRef === "string") {
|
|
437
|
-
id = idOrRef;
|
|
438
|
-
} else {
|
|
439
|
-
id = idOrRef.id;
|
|
440
|
-
type = idOrRef.type;
|
|
441
|
-
}
|
|
442
|
-
const state = this.vault.getState();
|
|
443
|
-
const realType = state.iiif.mapping[id];
|
|
444
|
-
if (!realType) {
|
|
445
|
-
type = type || "unknown";
|
|
446
|
-
} else {
|
|
447
|
-
type = realType;
|
|
448
|
-
}
|
|
449
|
-
return new ReferencedResource({ id, type }, this.vault);
|
|
450
|
-
}
|
|
451
|
-
embed(data) {
|
|
452
|
-
return new CreatorResource(data, this.vault);
|
|
453
|
-
}
|
|
454
|
-
async create(definition, payload, options) {
|
|
455
|
-
const foundDefinition = this.configs.find((t) => t.id === definition);
|
|
456
|
-
if (!foundDefinition) {
|
|
457
|
-
throw new Error(`Creator config ${definition} not found`);
|
|
458
|
-
}
|
|
459
|
-
const runtime = new CreatorRuntime(this.vault, foundDefinition, payload, this.configs, this.previewVault, options);
|
|
460
|
-
return await runtime.run();
|
|
461
|
-
}
|
|
462
|
-
};
|
|
463
|
-
|
|
464
|
-
// src/CreatorRuntime.ts
|
|
465
|
-
var CreatorRuntime = class {
|
|
466
|
-
constructor(vault, definition, payload, createConfigs, previewVault, options) {
|
|
467
|
-
// This will hold state for the creation process
|
|
468
|
-
__publicField(this, "resource", null);
|
|
469
|
-
__publicField(this, "payload");
|
|
470
|
-
__publicField(this, "vault");
|
|
471
|
-
__publicField(this, "previewVault");
|
|
472
|
-
__publicField(this, "definition");
|
|
473
|
-
__publicField(this, "options");
|
|
474
|
-
__publicField(this, "configs");
|
|
475
|
-
this.vault = vault;
|
|
476
|
-
this.previewVault = previewVault;
|
|
477
|
-
this.definition = definition;
|
|
478
|
-
this.payload = payload;
|
|
479
|
-
this.configs = createConfigs;
|
|
480
|
-
this.options = {
|
|
481
|
-
targetType: options?.targetType || definition.resourceType,
|
|
482
|
-
...options || {}
|
|
483
|
-
};
|
|
484
|
-
}
|
|
485
|
-
async run() {
|
|
486
|
-
const instance = new CreatorInstance(this.vault, this.options, this.configs, this.previewVault);
|
|
487
|
-
const result = await this.definition.create(this.payload, instance);
|
|
488
|
-
if (Array.isArray(result)) {
|
|
489
|
-
const results = [];
|
|
490
|
-
for (const item of result) {
|
|
491
|
-
if (item instanceof CreatorResource) {
|
|
492
|
-
results.push(item);
|
|
493
|
-
} else {
|
|
494
|
-
results.push(new CreatorResource(item, this.vault));
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
this.resource = results;
|
|
498
|
-
return this.resource;
|
|
499
|
-
}
|
|
500
|
-
if (result instanceof CreatorResource) {
|
|
501
|
-
this.resource = result;
|
|
502
|
-
} else {
|
|
503
|
-
this.resource = new CreatorResource(result, this.vault);
|
|
504
|
-
}
|
|
505
|
-
return this.resource;
|
|
506
|
-
}
|
|
507
|
-
getActions() {
|
|
508
|
-
if (!this.resource) {
|
|
509
|
-
return [];
|
|
510
|
-
}
|
|
511
|
-
const allResources = Array.isArray(this.resource) ? this.resource : [this.resource];
|
|
512
|
-
const actions = [];
|
|
513
|
-
const resources = {};
|
|
514
|
-
const mapping = {};
|
|
515
|
-
for (const resource of allResources) {
|
|
516
|
-
if (this.options.parent) {
|
|
517
|
-
resource.setPartOf(this.options.parent.resource.id);
|
|
518
|
-
}
|
|
519
|
-
const embedded = resource.getAllEmbeddedResources();
|
|
520
|
-
const allItems = [...embedded, resource];
|
|
521
|
-
for (const item of allItems) {
|
|
522
|
-
const resource2 = item.getVaultResource();
|
|
523
|
-
if (!resource2.type || !resource2.id) {
|
|
524
|
-
continue;
|
|
525
|
-
}
|
|
526
|
-
const type = resolveType(resource2.type);
|
|
527
|
-
resources[type] = resources[type] ? resources[type] : {};
|
|
528
|
-
resources[type][resource2.id] = resource2;
|
|
529
|
-
mapping[resource2.id] = type;
|
|
530
|
-
}
|
|
531
|
-
actions.push(importEntities({ entities: resources }));
|
|
532
|
-
actions.push(addMappings({ mapping }));
|
|
533
|
-
}
|
|
534
|
-
return actions;
|
|
535
|
-
}
|
|
536
|
-
commit(afterActions = []) {
|
|
537
|
-
if (!this.resource) {
|
|
538
|
-
throw new Error("Creator has not been run");
|
|
539
|
-
}
|
|
540
|
-
const actions = this.getActions();
|
|
541
|
-
this.vault.dispatch(batchActions({ actions: [...actions, ...afterActions] }));
|
|
542
|
-
if (Array.isArray(this.resource)) {
|
|
543
|
-
return this.resource.map((resource) => resource.ref());
|
|
544
|
-
}
|
|
545
|
-
return this.resource.ref();
|
|
546
|
-
}
|
|
547
|
-
};
|
|
548
|
-
|
|
549
|
-
// src/Creator.ts
|
|
550
|
-
var Creator = class {
|
|
551
|
-
constructor(vault, configs, previewVault) {
|
|
552
|
-
__publicField(this, "configs");
|
|
553
|
-
__publicField(this, "vault");
|
|
554
|
-
__publicField(this, "previewVault");
|
|
555
|
-
this.configs = configs || [];
|
|
556
|
-
this.vault = vault;
|
|
557
|
-
this.previewVault = previewVault || new Vault();
|
|
558
|
-
}
|
|
559
|
-
matchBasedOnResource(resource) {
|
|
560
|
-
return matchBasedOnResource(resource, this.configs || [], { vault: this.vault });
|
|
561
|
-
}
|
|
562
|
-
async create(definition, payload, options) {
|
|
563
|
-
const foundDefinition = this.configs.find((t) => t.id === definition);
|
|
564
|
-
if (!foundDefinition) {
|
|
565
|
-
throw new Error(`Creator config ${definition} not found`);
|
|
566
|
-
}
|
|
567
|
-
if (options?.parent) {
|
|
568
|
-
if (foundDefinition.supports.parentTypes) {
|
|
569
|
-
if (!foundDefinition.supports.parentTypes.includes(options.parent.resource.type)) {
|
|
570
|
-
throw new Error(`Definition ${definition} does not support parent type ${options.parent.resource.type}`);
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
if (foundDefinition.supports.parentFields) {
|
|
574
|
-
if (!foundDefinition.supports.parentFields.includes(options.parent.property)) {
|
|
575
|
-
throw new Error(`Definition ${definition} does not support parent field ${options.parent.property}`);
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
if (foundDefinition.supports.parentFieldMap) {
|
|
579
|
-
const type = foundDefinition.supports.parentFieldMap[options.parent.resource.type];
|
|
580
|
-
if (!type || !type.includes(options.parent.property)) {
|
|
581
|
-
throw new Error(
|
|
582
|
-
`Definition ${definition} does not support parent ${options.parent.property} / ${options.parent.property}`
|
|
583
|
-
);
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
if (foundDefinition.supports.custom) {
|
|
587
|
-
const isValid = foundDefinition.supports.custom(options.parent, this.vault);
|
|
588
|
-
if (!isValid) {
|
|
589
|
-
throw new Error(`Definition does not support resource`);
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
const runtime = new CreatorRuntime(this.vault, foundDefinition, payload, this.configs, this.previewVault, options);
|
|
594
|
-
const run = await runtime.run();
|
|
595
|
-
const allResources = Array.isArray(run) ? run : [run];
|
|
596
|
-
const afterActions = [];
|
|
597
|
-
for (const resource of allResources) {
|
|
598
|
-
if (options?.parent) {
|
|
599
|
-
if (options.parent.property === "start" || options.parent.property === "target") {
|
|
600
|
-
afterActions.push(
|
|
601
|
-
entityActions.modifyEntityField({
|
|
602
|
-
id: options.parent.resource.id,
|
|
603
|
-
type: options.parent.resource.type,
|
|
604
|
-
value: resource.ref(),
|
|
605
|
-
key: options.parent.property
|
|
606
|
-
})
|
|
607
|
-
);
|
|
608
|
-
} else {
|
|
609
|
-
afterActions.push(
|
|
610
|
-
entityActions.addReference({
|
|
611
|
-
id: options.parent.resource.id,
|
|
612
|
-
type: options.parent.resource.type,
|
|
613
|
-
reference: resource.ref(),
|
|
614
|
-
key: options.parent.property,
|
|
615
|
-
index: options.parent.atIndex
|
|
616
|
-
})
|
|
617
|
-
);
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
const result = runtime.commit(afterActions);
|
|
622
|
-
if (options?.parent) {
|
|
623
|
-
if (foundDefinition.sideEffects) {
|
|
624
|
-
for (const sideEffect of foundDefinition.sideEffects) {
|
|
625
|
-
if (sideEffect.run) {
|
|
626
|
-
const allResults = Array.isArray(result) ? result : [result];
|
|
627
|
-
for (const item of allResults) {
|
|
628
|
-
await sideEffect.run(item, { vault: this.vault, options });
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
return result;
|
|
635
|
-
}
|
|
636
|
-
};
|
|
637
|
-
|
|
638
|
-
// src/creator-register.ts
|
|
639
|
-
function defineCreator(options) {
|
|
640
|
-
return options;
|
|
641
|
-
}
|
|
642
|
-
export {
|
|
643
|
-
Creator,
|
|
644
|
-
CreatorResource,
|
|
645
|
-
creatorHelper,
|
|
646
|
-
defineCreator,
|
|
647
|
-
getEmptyType,
|
|
648
|
-
matchBasedOnResource,
|
|
649
|
-
randomId,
|
|
650
|
-
resolveType
|
|
651
|
-
};
|
|
1
|
+
import{Vault as e}from"@iiif/helpers/vault";import{addMappings as t,batchActions as n,entityActions as r,importEntities as i}from"@iiif/helpers/vault/actions";import{HAS_PART as a,PART_OF as o,emptyAgent as s,emptyAnnotation as c,emptyAnnotationPage as l,emptyCanvas as u,emptyCollection as d,emptyManifest as f,emptyRange as p,emptyService as m,isSpecificResource as h,toRef as g}from"@iiif/parser";import{references as _}from"@manifest-editor/editor-api";import{annotationResponseToSelector as v,seraliseSupportedSelector as y}from"react-iiif-vault";function b(e){"@babel/helpers - typeof";return b=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},b(e)}function x(e,t){if(b(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(b(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function S(e){var t=x(e,`string`);return b(t)==`symbol`?t:t+``}function C(e,t,n){return(t=S(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var w=class{constructor(e,t){C(this,`vault`,void 0),C(this,`original`,void 0),C(this,`reference`,void 0),C(this,`resource`,void 0),this.vault=t,this.original=e,this.reference=h(e)?e.source:e,this.resource=h(e)?e:{type:`SpecificResource`,source:this.reference}}ref(){return this.reference}specificResource(){return this.resource}optionalSpecificResource(){return h(this.original)?{...this.original,source:{id:this.original.source?.id,type:this.original.source?.type}}:this.original}get(){return this.vault.get(this.resource)}};function T(e){switch(e){case`Image`:case`Video`:case`Sound`:case`Dataset`:case`Text`:case`TextualBody`:case`Composite`:case`List`:case`Independents`:case`Audience`:return`ContentResource`;case`ImageService1`:case`ImageService2`:case`ImageService3`:return`Service`}return e}const E={Annotation:c,AnnotationPage:l,Canvas:u,Collection:d,Manifest:f,Range:p,ResourceProvider:s,Service:m};function D(e){return E[T(e)]||{}}function O(){return`${Math.random().toString(36).substring(2)}-${Date.now().toString(36)}`}function k(e,t,n){let r=[];if(t.length===0)return[];let i=g(e.parent),a=e.property,o=e.filter;for(let s of t)if(!(o&&(!s.tags||!s.tags.includes(o)))&&i&&a){if(s.supports.parentTypes&&!s.supports.parentTypes.includes(i.type)||s.supports.parentFields&&!s.supports.parentFields.includes(a))continue;if(s.supports.parentFieldMap){let e=s.supports.parentFieldMap[i.type];if(!e||!e.includes(a))continue}if(s.supports.disallowPainting&&e.isPainting||s.supports.onlyPainting&&!e.isPainting||s.supports.custom&&!s.supports.custom({property:a,resource:i,atIndex:e.index},n.vault)||!s.supports.initialData&&Object.keys(e.initialData||{}).length!==0||s.resourceType!==e.type&&!(s.additionalTypes||[]).includes(e.type))continue;r.push(s)}return r}function A(e,t,n,r){return(i,a={})=>(typeof t!=`string`&&(a.parent={property:n,resource:t,atIndex:a?.parent?.atIndex}),e.create(r,i,a))}var j=class e{constructor(t,n){C(this,`resource`,void 0),C(this,`warnings`,[]),C(this,`errors`,[]),C(this,`partOf`,void 0),C(this,`references`,[]),C(this,`embedded`,[]),C(this,`vault`,void 0),C(this,`specificResource`,void 0),this.vault=n;let r={...t};if(r.type===`SpecificResource`){let e=r.source;this.specificResource={...r,source:{id:e.id,type:T(e.type)}},r=e}let i=Object.keys(r),a=D(r.type);for(let t of i)if(!(_.inlineProperties.includes(t)||!_.all.includes(t))){if(r.type===`Range`&&t===`items`){let e=[];for(let i of r[t]){if(i instanceof w){this.references.push(i),e.push(i);continue}let a=new w(i,n);e.push(a),r[t]=a}r[t]=e;continue}if(r.type===`Manifest`&&t===`start`){let e=r[t];if(e instanceof w){this.references.push(e);continue}r[t]=new w(e,n);continue}if(r.type===`Annotation`&&t===`target`){let e=r[t];if(e instanceof w){this.references.push(e);continue}typeof e==`string`&&(e={id:e,type:`Canvas`}),r[t]=new w(e,n);continue}if(!_.single.includes(t)&&(_.externalProperties.includes(t)||_.internalProperties.includes(t))){let i=r[t]||[],a=Array.isArray(i)?i:[i],o=[];for(let t of a){if(t instanceof w){this.references.push(t),o.push(t);continue}if(t instanceof e){this.embedded.push(t),o.push(t);continue}if(this.vault.get(t,{skipSelfReturn:!0})){let e=T(t.type),r=new w({id:t.id,type:e},n);this.references.push(r),o.push(r)}else{let r=new e(t,n);this.embedded.push(r),o.push(r)}}r[t]=o}}this.resource={...a,...r}}get(){return this.resource}getSpecificResource(){let e=this.ref();return h(e)?e:{type:`SpecificResource`,source:e}}ref(){return this.specificResource?this.specificResource:{id:this.resource.id,type:T(this.resource.type)}}getAllReferencedResources(){return this.references}getAllEmbeddedResources(){let e=[];for(let t of this.embedded)e.push(...t.getAllEmbeddedResources());return e.push(...this.embedded),e}getVaultResource(){let t=this.resource,n=Object.keys(t),r={};for(let i of n){if(_.inlineProperties.includes(i)||!_.all.includes(i)){r[i]=t[i];continue}if(_.single.includes(i)){let e=t[i];e instanceof w?t.type===`Manifest`&&i===`start`||t.type===`Annotation`&&i===`target`?r[i]=e.specificResource():r[i]=e.optionalSpecificResource():r[i]=t[i];continue}if(t[i]){r[i]=[];for(let n of t[i]||[]){if(t.type===`Range`&&i===`items`){n instanceof w&&(n.ref().type===`Canvas`?r[i].push(n.specificResource()):r[i].push(n.ref())),n instanceof e&&(n.ref().type===`Canvas`?r[i].push(n.getSpecificResource()):r[i].push(n.ref()));continue}n instanceof w&&r[i].push(n.optionalSpecificResource()),n instanceof e&&r[i].push(n.ref())}}}return this.partOf&&(r[a]=r[a]||[],r[a].push(this.partOf)),r}setPartOf(e){let t=this.ref();this.partOf={id:t.id,type:T(t.type),[o]:e}}},M=class{constructor(e,t,n,r){C(this,`vault`,void 0),C(this,`previewVault`,void 0),C(this,`configs`,void 0),C(this,`options`,void 0),C(this,`target`,void 0),C(this,`selector`,void 0),C(this,`serialisedSelector`,null),this.vault=e,this.previewVault=r,this.options=t,this.configs=n,this.target=this.options.target||this.options.parent?.resource;let i=this.options.initialData?.selector;this.selector=i?v(i):null,this.options.initialData?.getSerialisedSelector&&(this.serialisedSelector=this.options.initialData.getSerialisedSelector())}getPreviewVault(){return this.previewVault}setSelector(e){this.selector=e}getSerialisedSelector(){if(this.serialisedSelector)return this.serialisedSelector;let e=this.selector;if(e){let t=y(e,this.options.initialData?.on);if(t)return t}return null}getTarget(){let e=this.target,t=this.getSerialisedSelector();return t?{type:`SpecificResource`,source:e,selector:t}:e}getParent(){return this.options.parent?.resource}getParentResource(){let e=this.getParent();if(e)return{type:`SpecificResource`,source:e}}generateId(e,t){return t&&t instanceof w&&(t=t.ref()),this.options.rootId&&(t={id:this.options.rootId,type:`none`}),`${(t||this.options.parent?.resource)?.id}/${e}/${O()}`}ref(e){let t=``,n=``;return typeof e==`string`?t=e:(t=e.id,n=e.type),n=this.vault.getState().iiif.mapping[t]||n||`unknown`,new w({id:t,type:n},this.vault)}embed(e){return new j(e,this.vault)}async create(e,t,n){let r=this.configs.find(t=>t.id===e);if(!r)throw Error(`Creator config ${e} not found`);return await new N(this.vault,r,t,this.configs,this.previewVault,n).run()}},N=class{constructor(e,t,n,r,i,a){C(this,`resource`,null),C(this,`payload`,void 0),C(this,`vault`,void 0),C(this,`previewVault`,void 0),C(this,`definition`,void 0),C(this,`options`,void 0),C(this,`configs`,void 0),this.vault=e,this.previewVault=i,this.definition=t,this.payload=n,this.configs=r,this.options={targetType:a?.targetType||t.resourceType,...a||{}}}async run(){let e=new M(this.vault,this.options,this.configs,this.previewVault),t=await this.definition.create(this.payload,e);if(Array.isArray(t)){let e=[];for(let n of t)n instanceof j?e.push(n):e.push(new j(n,this.vault));return this.resource=e,this.resource}return t instanceof j?this.resource=t:this.resource=new j(t,this.vault),this.resource}getActions(){if(!this.resource)return[];let e=Array.isArray(this.resource)?this.resource:[this.resource],n=[],r={},a={};for(let o of e){this.options.parent&&o.setPartOf(this.options.parent.resource.id);let e=[...o.getAllEmbeddedResources(),o];for(let t of e){let e=t.getVaultResource();if(!e.type||!e.id)continue;let n=T(e.type);r[n]=r[n]?r[n]:{},r[n][e.id]=e,a[e.id]=n}n.push(i({entities:r})),n.push(t({mapping:a}))}return n}commit(e=[]){if(!this.resource)throw Error(`Creator has not been run`);let t=this.getActions();return this.vault.dispatch(n({actions:[...t,...e]})),Array.isArray(this.resource)?this.resource.map(e=>e.ref()):this.resource.ref()}},P=class{constructor(t,n,r){C(this,`configs`,void 0),C(this,`vault`,void 0),C(this,`previewVault`,void 0),this.configs=n||[],this.vault=t,this.previewVault=r||new e}matchBasedOnResource(e){return k(e,this.configs||[],{vault:this.vault})}async create(e,t,n){let i=this.configs.find(t=>t.id===e);if(!i)throw Error(`Creator config ${e} not found`);if(n?.parent){if(i.supports.parentTypes&&!i.supports.parentTypes.includes(n.parent.resource.type))throw Error(`Definition ${e} does not support parent type ${n.parent.resource.type}`);if(i.supports.parentFields&&!i.supports.parentFields.includes(n.parent.property))throw Error(`Definition ${e} does not support parent field ${n.parent.property}`);if(i.supports.parentFieldMap){let t=i.supports.parentFieldMap[n.parent.resource.type];if(!t||!t.includes(n.parent.property))throw Error(`Definition ${e} does not support parent ${n.parent.property} / ${n.parent.property}`)}if(i.supports.custom&&!i.supports.custom(n.parent,this.vault))throw Error(`Definition does not support resource`)}let a=new N(this.vault,i,t,this.configs,this.previewVault,n),o=await a.run(),s=Array.isArray(o)?o:[o],c=[];for(let e of s)n?.parent&&(n.parent.property===`start`||n.parent.property===`target`?c.push(r.modifyEntityField({id:n.parent.resource.id,type:n.parent.resource.type,value:e.ref(),key:n.parent.property})):c.push(r.addReference({id:n.parent.resource.id,type:n.parent.resource.type,reference:e.ref(),key:n.parent.property,index:n.parent.atIndex})));let l=a.commit(c);if(n?.parent&&i.sideEffects){for(let e of i.sideEffects)if(e.run){let t=Array.isArray(l)?l:[l];for(let r of t)await e.run(r,{vault:this.vault,options:n})}}return l}};function F(e){return e}export{P as Creator,j as CreatorResource,A as creatorHelper,F as defineCreator,D as getEmptyType,k as matchBasedOnResource,O as randomId,T as resolveType};
|
|
2
|
+
//# sourceMappingURL=index.js.map
|