@manifest-editor/creator-api 1.0.2
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/LICENSE +21 -0
- package/README.md +79 -0
- package/dist/index.cjs +557 -0
- package/dist/index.d.cts +204 -0
- package/dist/index.d.ts +204 -0
- package/dist/index.js +545 -0
- package/package.json +63 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
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
|
+
|
|
11
|
+
// src/CreatorRuntime.ts
|
|
12
|
+
import { importEntities, addMappings, batchActions } from "@iiif/helpers/vault/actions";
|
|
13
|
+
|
|
14
|
+
// src/CreatorResource.ts
|
|
15
|
+
import { references } from "@manifest-editor/editor-api";
|
|
16
|
+
|
|
17
|
+
// src/ReferencedResource.ts
|
|
18
|
+
import { isSpecificResource } from "@iiif/parser";
|
|
19
|
+
var ReferencedResource = class {
|
|
20
|
+
constructor(reference, vault) {
|
|
21
|
+
__publicField(this, "vault");
|
|
22
|
+
__publicField(this, "original");
|
|
23
|
+
__publicField(this, "reference");
|
|
24
|
+
__publicField(this, "resource");
|
|
25
|
+
this.vault = vault;
|
|
26
|
+
this.original = reference;
|
|
27
|
+
this.reference = isSpecificResource(reference) ? reference.source : reference;
|
|
28
|
+
this.resource = isSpecificResource(reference) ? reference : { type: "SpecificResource", source: this.reference };
|
|
29
|
+
}
|
|
30
|
+
// @todo editable specific resource.
|
|
31
|
+
ref() {
|
|
32
|
+
return this.reference;
|
|
33
|
+
}
|
|
34
|
+
specificResource() {
|
|
35
|
+
return this.resource;
|
|
36
|
+
}
|
|
37
|
+
optionalSpecificResource() {
|
|
38
|
+
if (isSpecificResource(this.original)) {
|
|
39
|
+
return {
|
|
40
|
+
...this.original,
|
|
41
|
+
source: { id: this.original.source?.id, type: this.original.source?.type }
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
return this.original;
|
|
45
|
+
}
|
|
46
|
+
get() {
|
|
47
|
+
return this.vault.get(this.resource);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// src/utils.ts
|
|
52
|
+
import {
|
|
53
|
+
emptyAgent,
|
|
54
|
+
emptyAnnotation,
|
|
55
|
+
emptyAnnotationPage,
|
|
56
|
+
emptyCanvas,
|
|
57
|
+
emptyCollection,
|
|
58
|
+
emptyManifest,
|
|
59
|
+
emptyRange,
|
|
60
|
+
emptyService
|
|
61
|
+
} from "@iiif/parser";
|
|
62
|
+
function resolveType(type) {
|
|
63
|
+
switch (type) {
|
|
64
|
+
case "Image":
|
|
65
|
+
case "Video":
|
|
66
|
+
case "Sound":
|
|
67
|
+
case "Dataset":
|
|
68
|
+
case "Text":
|
|
69
|
+
case "TextualBody":
|
|
70
|
+
case "Composite":
|
|
71
|
+
case "List":
|
|
72
|
+
case "Independents":
|
|
73
|
+
case "Audience":
|
|
74
|
+
return "ContentResource";
|
|
75
|
+
case "ImageService1":
|
|
76
|
+
case "ImageService2":
|
|
77
|
+
case "ImageService3":
|
|
78
|
+
return "Service";
|
|
79
|
+
}
|
|
80
|
+
return type;
|
|
81
|
+
}
|
|
82
|
+
var emptyTypes = {
|
|
83
|
+
Annotation: emptyAnnotation,
|
|
84
|
+
AnnotationPage: emptyAnnotationPage,
|
|
85
|
+
Canvas: emptyCanvas,
|
|
86
|
+
Collection: emptyCollection,
|
|
87
|
+
Manifest: emptyManifest,
|
|
88
|
+
Range: emptyRange,
|
|
89
|
+
ResourceProvider: emptyAgent,
|
|
90
|
+
Service: emptyService
|
|
91
|
+
};
|
|
92
|
+
function getEmptyType(type) {
|
|
93
|
+
const key = resolveType(type);
|
|
94
|
+
return emptyTypes[key] || {};
|
|
95
|
+
}
|
|
96
|
+
function randomId() {
|
|
97
|
+
return `${Math.random().toString(36).substr(2)}-${Date.now().toString(36)}`;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// src/CreatorResource.ts
|
|
101
|
+
import { HAS_PART, PART_OF } from "@iiif/parser";
|
|
102
|
+
var CreatorResource = class _CreatorResource {
|
|
103
|
+
constructor(data, vault) {
|
|
104
|
+
__publicField(this, "resource");
|
|
105
|
+
__publicField(this, "warnings", []);
|
|
106
|
+
__publicField(this, "errors", []);
|
|
107
|
+
__publicField(this, "partOf");
|
|
108
|
+
__publicField(this, "references", []);
|
|
109
|
+
__publicField(this, "embedded", []);
|
|
110
|
+
__publicField(this, "vault");
|
|
111
|
+
__publicField(this, "specificResource");
|
|
112
|
+
this.vault = vault;
|
|
113
|
+
if (data.type === "SpecificResource") {
|
|
114
|
+
const source = data.source;
|
|
115
|
+
this.specificResource = { ...data, source: { id: source.id, type: resolveType(source.type) } };
|
|
116
|
+
data = source;
|
|
117
|
+
}
|
|
118
|
+
const properties = Object.keys(data);
|
|
119
|
+
const defaultType = getEmptyType(data.type);
|
|
120
|
+
for (const key of properties) {
|
|
121
|
+
if (references.inlineProperties.includes(key) || !references.all.includes(key)) {
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (data.type === "Range" && key === "items") {
|
|
125
|
+
const newRange = [];
|
|
126
|
+
for (const range of data[key]) {
|
|
127
|
+
if (range instanceof ReferencedResource) {
|
|
128
|
+
this.references.push(range);
|
|
129
|
+
newRange.push(range);
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
const rangeRef = new ReferencedResource(range, vault);
|
|
133
|
+
newRange.push(rangeRef);
|
|
134
|
+
data[key] = rangeRef;
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
data[key] = newRange;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (data.type === "Manifest" && key === "start") {
|
|
141
|
+
const start = data[key];
|
|
142
|
+
if (start instanceof ReferencedResource) {
|
|
143
|
+
this.references.push(start);
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
const startRef = new ReferencedResource(start, vault);
|
|
147
|
+
data[key] = startRef;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (data.type === "Annotation" && key === "target") {
|
|
151
|
+
let target = data[key];
|
|
152
|
+
if (target instanceof ReferencedResource) {
|
|
153
|
+
this.references.push(target);
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (typeof target === "string") {
|
|
157
|
+
target = { id: target, type: "Canvas" };
|
|
158
|
+
}
|
|
159
|
+
const targetRef = new ReferencedResource(target, vault);
|
|
160
|
+
data[key] = targetRef;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (references.single.includes(key)) {
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
if (references.externalProperties.includes(key) || references.internalProperties.includes(key)) {
|
|
167
|
+
const _items = data[key] || [];
|
|
168
|
+
const items = Array.isArray(_items) ? _items : [_items];
|
|
169
|
+
const newItems = [];
|
|
170
|
+
for (const item of items) {
|
|
171
|
+
if (item instanceof ReferencedResource) {
|
|
172
|
+
this.references.push(item);
|
|
173
|
+
newItems.push(item);
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if (item instanceof _CreatorResource) {
|
|
177
|
+
this.embedded.push(item);
|
|
178
|
+
newItems.push(item);
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
const exists = this.vault.get(item, { skipSelfReturn: true });
|
|
182
|
+
if (exists) {
|
|
183
|
+
const type = resolveType(item.type);
|
|
184
|
+
const newItem = new ReferencedResource({ id: item.id, type }, vault);
|
|
185
|
+
this.references.push(newItem);
|
|
186
|
+
newItems.push(newItem);
|
|
187
|
+
} else {
|
|
188
|
+
const newItem = new _CreatorResource(item, vault);
|
|
189
|
+
this.embedded.push(newItem);
|
|
190
|
+
newItems.push(newItem);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
data[key] = newItems;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
this.resource = { ...defaultType, ...data };
|
|
197
|
+
}
|
|
198
|
+
// This is what is returned from the creator instance
|
|
199
|
+
get() {
|
|
200
|
+
return this.resource;
|
|
201
|
+
}
|
|
202
|
+
ref() {
|
|
203
|
+
if (this.specificResource) {
|
|
204
|
+
return this.specificResource;
|
|
205
|
+
}
|
|
206
|
+
return { id: this.resource.id, type: resolveType(this.resource.type) };
|
|
207
|
+
}
|
|
208
|
+
getAllReferencedResources() {
|
|
209
|
+
return this.references;
|
|
210
|
+
}
|
|
211
|
+
getAllEmbeddedResources() {
|
|
212
|
+
const allEmbedded = [];
|
|
213
|
+
for (const resource of this.embedded) {
|
|
214
|
+
allEmbedded.push(...resource.getAllEmbeddedResources());
|
|
215
|
+
}
|
|
216
|
+
allEmbedded.push(...this.embedded);
|
|
217
|
+
return allEmbedded;
|
|
218
|
+
}
|
|
219
|
+
getVaultResource() {
|
|
220
|
+
const resource = this.resource;
|
|
221
|
+
const properties = Object.keys(resource);
|
|
222
|
+
const newResource = {};
|
|
223
|
+
for (const key of properties) {
|
|
224
|
+
if (references.inlineProperties.includes(key) || !references.all.includes(key)) {
|
|
225
|
+
newResource[key] = resource[key];
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
if (references.single.includes(key)) {
|
|
229
|
+
const ref = resource[key];
|
|
230
|
+
if (ref instanceof ReferencedResource) {
|
|
231
|
+
if (resource.type === "Manifest" && key === "start" || resource.type === "Annotation" && key === "target") {
|
|
232
|
+
newResource[key] = ref.specificResource();
|
|
233
|
+
} else {
|
|
234
|
+
newResource[key] = ref.optionalSpecificResource();
|
|
235
|
+
}
|
|
236
|
+
} else {
|
|
237
|
+
newResource[key] = resource[key];
|
|
238
|
+
}
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (resource[key]) {
|
|
242
|
+
newResource[key] = [];
|
|
243
|
+
for (const item of resource[key] || []) {
|
|
244
|
+
if (item instanceof ReferencedResource) {
|
|
245
|
+
newResource[key].push(item.optionalSpecificResource());
|
|
246
|
+
}
|
|
247
|
+
if (item instanceof _CreatorResource) {
|
|
248
|
+
newResource[key].push(item.ref());
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
if (this.partOf) {
|
|
254
|
+
newResource[HAS_PART] = newResource[HAS_PART] || [];
|
|
255
|
+
newResource[HAS_PART].push(this.partOf);
|
|
256
|
+
}
|
|
257
|
+
return newResource;
|
|
258
|
+
}
|
|
259
|
+
setPartOf(id) {
|
|
260
|
+
const ref = this.ref();
|
|
261
|
+
this.partOf = {
|
|
262
|
+
id: ref.id,
|
|
263
|
+
type: resolveType(ref.type),
|
|
264
|
+
[PART_OF]: id
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
// src/CreatorInstance.ts
|
|
270
|
+
var CreatorInstance = class {
|
|
271
|
+
constructor(vault, options, createConfigs, previewVault) {
|
|
272
|
+
__publicField(this, "vault");
|
|
273
|
+
__publicField(this, "previewVault");
|
|
274
|
+
__publicField(this, "configs");
|
|
275
|
+
__publicField(this, "options");
|
|
276
|
+
this.vault = vault;
|
|
277
|
+
this.previewVault = previewVault;
|
|
278
|
+
this.options = options;
|
|
279
|
+
this.configs = createConfigs;
|
|
280
|
+
}
|
|
281
|
+
getPreviewVault() {
|
|
282
|
+
return this.previewVault;
|
|
283
|
+
}
|
|
284
|
+
getTarget() {
|
|
285
|
+
const target = this.options.target || this.options.parent?.resource;
|
|
286
|
+
const position = this.options.initialData?.selector;
|
|
287
|
+
if (position && position.type === "polygon") {
|
|
288
|
+
const position2 = this.options.initialData?.selector;
|
|
289
|
+
const { width, height } = this.options.initialData?.on || {};
|
|
290
|
+
if (position2.shape.points.length === 0) {
|
|
291
|
+
return target;
|
|
292
|
+
}
|
|
293
|
+
if (position2.shape.points.length === 1) {
|
|
294
|
+
const points = position2.shape.points[0];
|
|
295
|
+
return {
|
|
296
|
+
type: "SpecificResource",
|
|
297
|
+
source: target,
|
|
298
|
+
selector: {
|
|
299
|
+
type: "PointSelector",
|
|
300
|
+
x: points[0],
|
|
301
|
+
y: points[1]
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
if (position2.shape.points.length === 4) {
|
|
306
|
+
const [topLeft, topRight, bottomRight, bottomLeft] = position2.shape.points;
|
|
307
|
+
if (topLeft[0] === topRight[0] && topLeft[1] === bottomLeft[1] && topRight[1] === bottomRight[1] && bottomLeft[0] === bottomRight[0]) {
|
|
308
|
+
const x = Math.min(topLeft[0], topRight[0], bottomRight[0], bottomLeft[0]);
|
|
309
|
+
const y = Math.min(topLeft[1], topRight[1], bottomRight[1], bottomLeft[1]);
|
|
310
|
+
const x2 = Math.max(topLeft[0], topRight[0], bottomRight[0], bottomLeft[0]);
|
|
311
|
+
const y2 = Math.max(topLeft[1], topRight[1], bottomRight[1], bottomLeft[1]);
|
|
312
|
+
const width2 = x2 - x;
|
|
313
|
+
const height2 = y2 - y;
|
|
314
|
+
return {
|
|
315
|
+
type: "SpecificResource",
|
|
316
|
+
source: target,
|
|
317
|
+
selector: {
|
|
318
|
+
type: "FragmentSelector",
|
|
319
|
+
value: `xywh=${[~~x, ~~y, ~~width2, ~~height2].join(",")}`
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
const el = position2.shape.open ? "polyline" : "polygon";
|
|
325
|
+
return {
|
|
326
|
+
type: "SpecificResource",
|
|
327
|
+
source: target,
|
|
328
|
+
selector: {
|
|
329
|
+
type: "SvgSelector",
|
|
330
|
+
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>`
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
if (target && position) {
|
|
335
|
+
return {
|
|
336
|
+
type: "SpecificResource",
|
|
337
|
+
source: target,
|
|
338
|
+
selector: {
|
|
339
|
+
type: "FragmentSelector",
|
|
340
|
+
value: `xywh=${[~~position.x, ~~position.y, ~~position.width, ~~position.height].join(",")}`
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
return target;
|
|
345
|
+
}
|
|
346
|
+
getParent() {
|
|
347
|
+
return this.options.parent?.resource;
|
|
348
|
+
}
|
|
349
|
+
getParentResource() {
|
|
350
|
+
const parent = this.getParent();
|
|
351
|
+
if (parent) {
|
|
352
|
+
return {
|
|
353
|
+
type: "SpecificResource",
|
|
354
|
+
source: parent
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
generateId(type, parent) {
|
|
359
|
+
if (parent && parent instanceof ReferencedResource) {
|
|
360
|
+
parent = parent.ref();
|
|
361
|
+
}
|
|
362
|
+
return `${(parent || this.options.parent?.resource)?.id}/${type}/${randomId()}`;
|
|
363
|
+
}
|
|
364
|
+
ref(idOrRef) {
|
|
365
|
+
let id = "";
|
|
366
|
+
let type = "";
|
|
367
|
+
if (typeof idOrRef === "string") {
|
|
368
|
+
id = idOrRef;
|
|
369
|
+
} else {
|
|
370
|
+
id = idOrRef.id;
|
|
371
|
+
type = idOrRef.type;
|
|
372
|
+
}
|
|
373
|
+
const state = this.vault.getState();
|
|
374
|
+
const realType = state.iiif.mapping[id];
|
|
375
|
+
if (!realType) {
|
|
376
|
+
type = type || "unknown";
|
|
377
|
+
} else {
|
|
378
|
+
type = realType;
|
|
379
|
+
}
|
|
380
|
+
return new ReferencedResource({ id, type }, this.vault);
|
|
381
|
+
}
|
|
382
|
+
embed(data) {
|
|
383
|
+
return new CreatorResource(data, this.vault);
|
|
384
|
+
}
|
|
385
|
+
async create(definition, payload, options) {
|
|
386
|
+
const foundDefinition = this.configs.find((t) => t.id === definition);
|
|
387
|
+
if (!foundDefinition) {
|
|
388
|
+
throw new Error(`Creator config ${definition} not found`);
|
|
389
|
+
}
|
|
390
|
+
const runtime = new CreatorRuntime(this.vault, foundDefinition, payload, this.configs, this.previewVault, options);
|
|
391
|
+
return await runtime.run();
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
// src/CreatorRuntime.ts
|
|
396
|
+
var CreatorRuntime = class {
|
|
397
|
+
constructor(vault, definition, payload, createConfigs, previewVault, options) {
|
|
398
|
+
// This will hold state for the creation process
|
|
399
|
+
__publicField(this, "resource", null);
|
|
400
|
+
__publicField(this, "payload");
|
|
401
|
+
__publicField(this, "vault");
|
|
402
|
+
__publicField(this, "previewVault");
|
|
403
|
+
__publicField(this, "definition");
|
|
404
|
+
__publicField(this, "options");
|
|
405
|
+
__publicField(this, "configs");
|
|
406
|
+
this.vault = vault;
|
|
407
|
+
this.previewVault = previewVault;
|
|
408
|
+
this.definition = definition;
|
|
409
|
+
this.payload = payload;
|
|
410
|
+
this.configs = createConfigs;
|
|
411
|
+
this.options = {
|
|
412
|
+
targetType: (options || {}).targetType || definition.resourceType,
|
|
413
|
+
...options || {}
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
async run() {
|
|
417
|
+
const instance = new CreatorInstance(this.vault, this.options, this.configs, this.previewVault);
|
|
418
|
+
const result = await this.definition.create(this.payload, instance);
|
|
419
|
+
if (result instanceof CreatorResource) {
|
|
420
|
+
this.resource = result;
|
|
421
|
+
} else {
|
|
422
|
+
this.resource = new CreatorResource(result, this.vault);
|
|
423
|
+
}
|
|
424
|
+
return this.resource;
|
|
425
|
+
}
|
|
426
|
+
getActions() {
|
|
427
|
+
if (!this.resource) {
|
|
428
|
+
return [];
|
|
429
|
+
}
|
|
430
|
+
const actions = [];
|
|
431
|
+
const resources = {};
|
|
432
|
+
const mapping = {};
|
|
433
|
+
if (this.options.parent) {
|
|
434
|
+
this.resource.setPartOf(this.options.parent.resource.id);
|
|
435
|
+
}
|
|
436
|
+
const embedded = this.resource.getAllEmbeddedResources();
|
|
437
|
+
const allItems = [...embedded, this.resource];
|
|
438
|
+
for (const item of allItems) {
|
|
439
|
+
const resource = item.getVaultResource();
|
|
440
|
+
if (!resource.type || !resource.id) {
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
const type = resolveType(resource.type);
|
|
444
|
+
resources[type] = resources[type] ? resources[type] : {};
|
|
445
|
+
resources[type][resource.id] = resource;
|
|
446
|
+
mapping[resource.id] = type;
|
|
447
|
+
}
|
|
448
|
+
actions.push(importEntities({ entities: resources }));
|
|
449
|
+
actions.push(addMappings({ mapping }));
|
|
450
|
+
return actions;
|
|
451
|
+
}
|
|
452
|
+
commit(afterActions = []) {
|
|
453
|
+
if (!this.resource) {
|
|
454
|
+
throw new Error("Creator has not been run");
|
|
455
|
+
}
|
|
456
|
+
const actions = this.getActions();
|
|
457
|
+
this.vault.dispatch(batchActions({ actions: [...actions, ...afterActions] }));
|
|
458
|
+
return this.resource.ref();
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
// src/Creator.ts
|
|
463
|
+
import { entityActions } from "@iiif/helpers/vault/actions";
|
|
464
|
+
var Creator = class {
|
|
465
|
+
constructor(vault, configs, previewVault) {
|
|
466
|
+
__publicField(this, "configs");
|
|
467
|
+
__publicField(this, "vault");
|
|
468
|
+
__publicField(this, "previewVault");
|
|
469
|
+
this.configs = configs || [];
|
|
470
|
+
this.vault = vault;
|
|
471
|
+
this.previewVault = previewVault || new Vault();
|
|
472
|
+
}
|
|
473
|
+
async create(definition, payload, options) {
|
|
474
|
+
const foundDefinition = this.configs.find((t) => t.id === definition);
|
|
475
|
+
if (!foundDefinition) {
|
|
476
|
+
throw new Error(`Creator config ${definition} not found`);
|
|
477
|
+
}
|
|
478
|
+
if (options?.parent) {
|
|
479
|
+
if (foundDefinition.supports.parentTypes) {
|
|
480
|
+
if (!foundDefinition.supports.parentTypes.includes(options.parent.resource.type)) {
|
|
481
|
+
throw new Error(`Definition ${definition} does not support parent type ${options.parent.resource.type}`);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
if (foundDefinition.supports.parentFields) {
|
|
485
|
+
if (!foundDefinition.supports.parentFields.includes(options.parent.property)) {
|
|
486
|
+
throw new Error(`Definition ${definition} does not support parent field ${options.parent.property}`);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
if (foundDefinition.supports.parentFieldMap) {
|
|
490
|
+
const type = foundDefinition.supports.parentFieldMap[options.parent.resource.type];
|
|
491
|
+
if (!type || !type.includes(options.parent.property)) {
|
|
492
|
+
throw new Error(
|
|
493
|
+
`Definition ${definition} does not support parent ${options.parent.property} / ${options.parent.property}`
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
if (foundDefinition.supports.custom) {
|
|
498
|
+
const isValid = foundDefinition.supports.custom(options.parent, this.vault);
|
|
499
|
+
if (!isValid) {
|
|
500
|
+
throw new Error(`Definition does not support resource`);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
const runtime = new CreatorRuntime(this.vault, foundDefinition, payload, this.configs, this.previewVault, options);
|
|
505
|
+
const resource = await runtime.run();
|
|
506
|
+
const afterActions = [];
|
|
507
|
+
if (options?.parent) {
|
|
508
|
+
if (options.parent.property === "start" || options.parent.property === "target") {
|
|
509
|
+
afterActions.push(
|
|
510
|
+
entityActions.modifyEntityField({
|
|
511
|
+
id: options.parent.resource.id,
|
|
512
|
+
type: options.parent.resource.type,
|
|
513
|
+
value: resource.ref(),
|
|
514
|
+
key: options.parent.property
|
|
515
|
+
})
|
|
516
|
+
);
|
|
517
|
+
} else {
|
|
518
|
+
afterActions.push(
|
|
519
|
+
entityActions.addReference({
|
|
520
|
+
id: options.parent.resource.id,
|
|
521
|
+
type: options.parent.resource.type,
|
|
522
|
+
reference: resource.ref(),
|
|
523
|
+
key: options.parent.property,
|
|
524
|
+
index: options.parent.atIndex
|
|
525
|
+
})
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
const result = runtime.commit(afterActions);
|
|
530
|
+
if (options?.parent) {
|
|
531
|
+
if (foundDefinition.sideEffects) {
|
|
532
|
+
for (const sideEffect of foundDefinition.sideEffects) {
|
|
533
|
+
if (sideEffect.run) {
|
|
534
|
+
await sideEffect.run(result, { vault: this.vault, options });
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
return result;
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
export {
|
|
543
|
+
Creator,
|
|
544
|
+
CreatorResource
|
|
545
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@manifest-editor/creator-api",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"bugs": "https://github.com/digirati-co-uk/iiif-manifest-editor/issues",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/digirati-co-uk/iiif-manifest-editor.git",
|
|
9
|
+
"directory": "packages/creator-api"
|
|
10
|
+
},
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=18.14.1"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"iiif",
|
|
16
|
+
"editor",
|
|
17
|
+
"typescript"
|
|
18
|
+
],
|
|
19
|
+
"main": "./dist/index.cjs",
|
|
20
|
+
"module": "./dist/index.js",
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"import": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"default": "./dist/index.js"
|
|
30
|
+
},
|
|
31
|
+
"require": {
|
|
32
|
+
"types": "./dist/index.d.cts",
|
|
33
|
+
"default": "./dist/index.cjs"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsup src/index.ts",
|
|
39
|
+
"dev": "tsup src/index.ts --watch",
|
|
40
|
+
"typecheck": "tsc --noEmit",
|
|
41
|
+
"lint": "publint",
|
|
42
|
+
"prepack": "tsup src/index.ts"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@iiif/helpers": "^1.2.12",
|
|
46
|
+
"@iiif/parser": "^2.1.2",
|
|
47
|
+
"@iiif/presentation-3": "^2.2.3",
|
|
48
|
+
"@iiif/presentation-3-normalized": "^0.9.7",
|
|
49
|
+
"@manifest-editor/editor-api": "^1.0.2",
|
|
50
|
+
"polygon-editor": "^0.0.4",
|
|
51
|
+
"react-iiif-vault": "^1.2.1"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@repo/eslint-config": "0.0.0",
|
|
55
|
+
"@types/react": "^18.2.74",
|
|
56
|
+
"tiny-invariant": "^1.3.3",
|
|
57
|
+
"tsup": "^8.0.2"
|
|
58
|
+
},
|
|
59
|
+
"publishConfig": {
|
|
60
|
+
"access": "public"
|
|
61
|
+
},
|
|
62
|
+
"gitHead": "083fd2ce7d40755fb1857490e006e9854906d031"
|
|
63
|
+
}
|