@ghentcdh/annotation-api 0.0.1-alpha.15 → 0.0.1-alpha.17

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.
Files changed (3) hide show
  1. package/index.cjs +12 -272
  2. package/index.js +15 -270
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -9,9 +9,6 @@ let _nestjs_swagger = require("@nestjs/swagger");
9
9
  //#region src/lib/annotation/annotation.repository.ts
10
10
  var AnnotationCrudRepository = class {};
11
11
  //#endregion
12
- //#region ../annotation-core/src/lib/types/key-label.type.ts
13
- var KeyLabelSchema = zod.z.object();
14
- //#endregion
15
12
  //#region ../annotation-core/src/lib/types/source.model.ts
16
13
  var SourceTypesList = ["text"];
17
14
  var SourceTextSchema = zod.z.object({
@@ -52,13 +49,13 @@ var defaultConfig = {
52
49
  app: "ghentcdh",
53
50
  prefix: "ghentcdh",
54
51
  isDev: false,
55
- cacheTTLms: 1200 * 1e3
52
+ cacheTTLms: 12e5
56
53
  };
57
54
  var resolveConfig = (config = {}) => {
58
55
  const baseUrl = config.baseUrl ?? defaultConfig.baseUrl;
59
56
  const normalizedBaseUrl = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
60
57
  const isDev = config.isDev ?? false;
61
- const cacheTTLms = isDev ? 0 : 1200 * 1e3;
58
+ const cacheTTLms = isDev ? 0 : 12e5;
62
59
  return {
63
60
  baseUrl: normalizedBaseUrl,
64
61
  contextUrl: `${normalizedBaseUrl}ns/`,
@@ -157,15 +154,14 @@ var AnnotationJsonResourceSchema = AnnotationJsonResourceShape.transform((obj) =
157
154
  //#endregion
158
155
  //#region ../annotation-core/src/lib/types/annotation-definition.type.ts
159
156
  var annotationColumnDefinition = zod.z.custom();
160
- var AnnotationResourceSchema = AnnotationJsonResourceShape.pick({
161
- id: true,
162
- name: true,
163
- title: true,
164
- operations: true,
165
- annotation: true
166
- }).extend({
157
+ var { id, name, title, operations } = AnnotationJsonResourceShape.shape;
158
+ var AnnotationResourceSchema = zod.z.object({
159
+ id,
160
+ name,
161
+ title,
162
+ operations,
167
163
  annotation: AnnotationConfigSchema,
168
- context: zod.z.instanceof(_ghentcdh_w3c_utils.ContextBuilder).optional().nullish(),
164
+ context: zod.z.custom(() => true).optional().nullish(),
169
165
  schemas: zod.z.record(zod.z.string(), _ghentcdh_crouton_core.ViewConfigSchema.partial()).optional().nullish()
170
166
  });
171
167
  //#endregion
@@ -199,273 +195,22 @@ var createAnnotationStyleBodyUnsafe = (annotationConfig, style) => {
199
195
  return parsed;
200
196
  };
201
197
  //#endregion
202
- //#region ../annotation-core/src/lib/utils/annotation-utils.ts
203
- var mapRelationsToLinks = (sourceUri, builders, annotations) => {
204
- return annotations.map((link) => {
205
- const builder = builders.get(link.id);
206
- const specifyResource = builder.getSpecificResourceTargets(sourceUri);
207
- if (!specifyResource || specifyResource.length === 0) return null;
208
- const relations = builder.getSpecificResourceTargets().map((s) => s.source).filter((s) => s !== sourceUri).map((uri) => {
209
- return annotations.find((a) => a.id === uri);
210
- }).filter(Boolean);
211
- return {
212
- purpose: getAnnotationType(builder),
213
- annotation: link,
214
- relations
215
- };
216
- }).filter(Boolean);
217
- };
218
- var getAnnotationType = (builder) => {
219
- return getAnnotationStyle(builder)?.id ?? "text";
220
- };
221
- var getAnnotationStyle = (builder) => {
222
- return builder.getBodiesByPurpose("styling").find((b) => b.type === "AnnotationStyle");
223
- };
224
- var getAnnotationPosition = (id, builder, allowedChildrenPerType) => {
225
- const sourceUri = builder.getSpecificResourceTargets()[0]?.source;
226
- if (!sourceUri) return null;
227
- const textPositionSelector = builder.getTextPositionSelector(sourceUri)[0];
228
- if (!textPositionSelector) return null;
229
- const type = getAnnotationStyle(builder)?.id ?? "text";
230
- return {
231
- id,
232
- start: textPositionSelector.start,
233
- end: textPositionSelector.end,
234
- allowedChildren: allowedChildrenPerType[type] ?? [],
235
- sourceUri: sourceUri ?? "",
236
- type,
237
- minX: textPositionSelector.start,
238
- minY: 0,
239
- maxX: textPositionSelector.end,
240
- maxY: 0
241
- };
242
- };
243
- var rootId = "ROOT";
244
- var createId = () => {
245
- return `mela:new-annotation:${Date.now()}`;
246
- };
247
- var updateSelector = (builder, selector) => {
248
- if (!builder.getSpecificResourceTargets(selector.source).length) builder.addTarget({
249
- type: "SpecificResource",
250
- source: selector.source
251
- });
252
- builder.updateTextPositionSelector({
253
- start: selector.start,
254
- end: selector.end
255
- }, selector.source);
256
- builder.updateTextQuoteSelector({
257
- prefix: selector.prefix,
258
- suffix: selector.suffix,
259
- exact: selector.exact
260
- }, selector.source);
261
- return builder;
262
- };
263
- var AnnotationUtilsImpl = class {
264
- annotationConfig;
265
- builderMap = /* @__PURE__ */ new Map();
266
- childParentMap = /* @__PURE__ */ new Map();
267
- parentChildMap = /* @__PURE__ */ new Map();
268
- linkedAnnotations = /* @__PURE__ */ new Map();
269
- annotations = [];
270
- allowedChildrenPerType = {};
271
- constructor(annotationConfig) {
272
- this.annotationConfig = annotationConfig;
273
- }
274
- getBuilder(annotation) {
275
- const builder = this.builderMap.get(annotation.id);
276
- if (!builder) throw new Error(`Annotation builder not found: ${annotation.id}`);
277
- return builder;
278
- }
279
- setAnnotations(annotations, allowedChildrenPerType) {
280
- this.allowedChildrenPerType = allowedChildrenPerType;
281
- this.annotations = annotations;
282
- this.parentChildMap.clear();
283
- this.childParentMap.clear();
284
- this.builderMap.clear();
285
- const annotationsWithPositons = [];
286
- const annotationIdMap = /* @__PURE__ */ new Map();
287
- annotations.forEach((annotation) => {
288
- const builder = (0, _ghentcdh_w3c_utils.w3cAnnotation)(annotation);
289
- this.builderMap.set(annotation.id, builder);
290
- const position = getAnnotationPosition(annotation.id, builder, this.allowedChildrenPerType);
291
- annotationIdMap.set(annotation.id, annotation);
292
- if (position) annotationsWithPositons.push(position);
293
- });
294
- annotationsWithPositons.forEach((annotation) => {
295
- const parentAnnotation = annotationsWithPositons.filter((a) => a.id !== annotation.id && a.sourceUri === annotation.sourceUri && a.start <= annotation.start && a.end >= annotation.end && a.allowedChildren.some((c) => c.key === annotation.type)).sort((a, b) => a.end - a.start - (b.end - b.start))[0];
296
- const parentId = parentAnnotation?.id ?? rootId;
297
- const children = this.parentChildMap.get(parentId) ?? [];
298
- children.push(annotationIdMap.get(annotation.id));
299
- this.parentChildMap.set(parentAnnotation?.id ?? rootId, children);
300
- this.childParentMap.set(annotation.id, annotationIdMap.get(parentId) ?? null);
301
- });
302
- annotations.forEach((annotation) => {
303
- this.linkedAnnotations.set(annotation.id, mapRelationsToLinks(annotation.id, this.builderMap, annotations));
304
- });
305
- return this;
306
- }
307
- getLinks(annotation) {
308
- return this.linkedAnnotations.get(annotation.id) ?? [];
309
- }
310
- getTextPositionSelector(annotation, sourceUri) {
311
- return this.getBuilder(annotation).getTextPositionSelector(sourceUri)[0];
312
- }
313
- isAnnotationUri = (uri) => {
314
- return uri.startsWith("mela:annotation/");
315
- };
316
- getParentSelector = (builder) => {
317
- return builder.getSpecificResourceTargets().find((t) => this.isAnnotationUri(t.source)) ?? null;
318
- };
319
- getParent(annotation) {
320
- const builder = this.getBuilder(annotation);
321
- const parentUri = this.getParentSelector(builder);
322
- if (!parentUri) return null;
323
- return this.annotations.find((a) => a.id === parentUri.source) ?? null;
324
- }
325
- createAnnotation(fromAnnotation, type, data, selectors) {
326
- const styleBody = AnnotationStyleContextBuilder(this.annotationConfig).toAnnotationBody(type);
327
- const builder = (0, _ghentcdh_w3c_utils.w3cAnnotation)(fromAnnotation ?? void 0).setMotivation("tagging").addBody(styleBody);
328
- if (!fromAnnotation?.id) builder.setId(createId());
329
- if (data && Object.keys(data).length > 0) {
330
- data.type = data.type ?? "AnnotationMetadata";
331
- data.purpose = data.purpose ?? getAnnotationType(builder);
332
- builder.updateBodyByType(data.type, data);
333
- }
334
- for (const selector of selectors) updateSelector(builder, selector);
335
- return builder.build();
336
- }
337
- createLinkAnnotation = (sourceAnnotation, targetAnnotation, type, data) => {
338
- const styleBody = AnnotationStyleContextBuilder(this.annotationConfig).toAnnotationBody(type);
339
- const builder = (0, _ghentcdh_w3c_utils.w3cAnnotation)().setMotivation("tagging").setId(createId()).addBody(styleBody);
340
- if (data && Object.keys(data).length > 0) {
341
- data.type = data.type ?? "AnnotationMetadata";
342
- data.purpose = data.purpose ?? getAnnotationType(builder);
343
- builder.updateBodyByType(data.type, data);
344
- }
345
- builder.addTarget({ source: sourceAnnotation.id }).addTarget({ source: targetAnnotation.id });
346
- return builder.build();
347
- };
348
- cancel() {
349
- this.setAnnotations(this.annotations, this.allowedChildrenPerType);
350
- }
351
- getSourceUri = (annotation) => {
352
- const builder = this.getBuilder(annotation);
353
- const sourceUri = builder.getSourceUri();
354
- if (!sourceUri) {
355
- console.warn("no source uri");
356
- return null;
357
- }
358
- return {
359
- textPositionSelector: builder.getTextPositionSelector(sourceUri)[0],
360
- sourceUri
361
- };
362
- };
363
- getMetadata = (annotation) => {
364
- return this.getBuilder(annotation).getBodiesByType("AnnotationMetadata")?.[0] ?? null;
365
- };
366
- getAnnotationStyle = (annotation) => {
367
- return getAnnotationStyle(this.getBuilder(annotation));
368
- };
369
- getAnnotationType = (annotation) => {
370
- return getAnnotationType(this.getBuilder(annotation));
371
- };
372
- createAnnotationFromSelector(type, annotation, selector) {
373
- const builder = (0, _ghentcdh_w3c_utils.w3cAnnotation)(annotation ?? void 0).setMotivation("tagging").setId(createId());
374
- const styleBody = AnnotationStyleContextBuilder(this.annotationConfig).toAnnotationBody(type);
375
- builder.addBody(styleBody);
376
- if (selector) updateSelector(builder, selector);
377
- return builder.build();
378
- }
379
- };
380
- var annotationUtils = (annotationConfig) => {
381
- return new AnnotationUtilsImpl(annotationConfig);
382
- };
383
- //#endregion
384
- //#region ../annotation-core/src/lib/utils/annotation-render.style.ts
385
- var defaultRender = (utils) => (annotation) => {
386
- return (utils?.getAnnotationStyle(annotation))?.target ?? "default";
387
- };
388
- var styleFn = (listStyles, utils) => (annotation) => {
389
- const style = utils?.getAnnotationStyle(annotation);
390
- if (!style) return "default";
391
- const styleId = style?.id ?? "default";
392
- if (!listStyles.includes(styleId)) {
393
- _ghentcdh_annotated_text.Debugger.debug("styleFn", `No style found for ${styleId}`);
394
- if (style.color) return { default: (0, _ghentcdh_annotated_text.createHighlightStyle)(style.color) };
395
- return "default";
396
- }
397
- return styleId;
398
- };
399
- //#endregion
400
- //#region ../annotation-core/src/lib/utils/annotation-configuration.factory.ts
401
- var groupById = (defs, valueKey) => {
402
- if (!defs) return {};
403
- return defs.reduce((acc, def) => {
404
- acc[def.id] = valueKey ? def[valueKey] : def;
405
- return acc;
406
- }, {});
407
- };
408
- var createAnnotationConfiguration = (annotationDefinitions, utils, textAdapter, annotationAdapter) => {
409
- const definitions = annotationDefinitions ?? [];
410
- const definitionsMap = groupById(definitions);
411
- const rootTypes = definitions.filter((d) => d.annotation.isRoot).map((d) => ({
412
- key: d.id,
413
- label: d.label
414
- }));
415
- const styles = groupById(definitions, "style");
416
- if (!styles["default"]) styles["default"] = {
417
- label: "Annotation",
418
- isRoot: true,
419
- style: { default: (0, _ghentcdh_annotated_text.createHighlightStyle)("#dd7777") },
420
- allowedChildren: [],
421
- allowedLinks: [],
422
- views: {}
423
- };
424
- const listStyles = Object.keys(styles);
425
- const allowedChildrenPerType = groupById(definitions, "allowedChildren");
426
- const defaultAnnotationAdapter = (source) => (0, _ghentcdh_annotated_text.W3CAnnotationAdapter)(source ? { sourceUri: source.uri } : {});
427
- const renderParams = () => ({ renderFn: defaultRender(utils) });
428
- const styleParams = () => ({ styleFn: styleFn(listStyles, utils) });
429
- const _createAnnotatedText = (id, sourceModel) => {
430
- const _textAdapter = textAdapter?.() ?? (0, _ghentcdh_annotated_text.PlainTextAdapter)();
431
- const annotatedText = (0, _ghentcdh_annotated_text.createAnnotatedText)(id, {
432
- annotationAdapter: annotationAdapter?.() ?? defaultAnnotationAdapter(sourceModel),
433
- textAdapter: _textAdapter
434
- });
435
- annotatedText.setSnapper(new _ghentcdh_annotated_text.WordSnapper()).setRenderParams(renderParams()).setStyleParams(styleParams()).registerStyles(styles);
436
- if (sourceModel) {
437
- const { content } = sourceModel;
438
- annotatedText.setText(content.text).setTextAdapterParams({ textDirection: content.textDirection });
439
- }
440
- return annotatedText;
441
- };
442
- return {
443
- allowedChildrenPerType,
444
- definitions,
445
- getDefinition: (id) => {
446
- return definitionsMap[id] ?? void 0;
447
- },
448
- rootTypes,
449
- createAnnotatedText: _createAnnotatedText
450
- };
451
- };
452
- //#endregion
453
198
  //#region src/lib/utils/annotation.context-builder.ts
454
199
  var ANNOTATION_DEF_CONFIG_TOKEN = Symbol("ANNOTATION_DEF_CONFIG_TOKEN");
455
200
  //#endregion
456
- //#region \0@oxc-project+runtime@0.139.0/helpers/esm/decorateMetadata.js
201
+ //#region \0@oxc-project+runtime@0.151.0/helpers/esm/decorateMetadata.js
457
202
  function __decorateMetadata(k, v) {
458
203
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
459
204
  }
460
205
  //#endregion
461
- //#region \0@oxc-project+runtime@0.139.0/helpers/esm/decorateParam.js
206
+ //#region \0@oxc-project+runtime@0.151.0/helpers/esm/decorateParam.js
462
207
  function __decorateParam(paramIndex, decorator) {
463
208
  return function(target, key) {
464
209
  decorator(target, key, paramIndex);
465
210
  };
466
211
  }
467
212
  //#endregion
468
- //#region \0@oxc-project+runtime@0.139.0/helpers/esm/decorate.js
213
+ //#region \0@oxc-project+runtime@0.151.0/helpers/esm/decorate.js
469
214
  function __decorate(decorators, target, key, desc) {
470
215
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
471
216
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -839,18 +584,13 @@ Object.defineProperty(exports, "AnnotationW3cMapperService", {
839
584
  });
840
585
  exports.BASELINE_RESOURCE_VERSION = BASELINE_RESOURCE_VERSION;
841
586
  exports.CURRENT_RESOURCE_VERSION = CURRENT_RESOURCE_VERSION;
842
- exports.KeyLabelSchema = KeyLabelSchema;
843
587
  exports.SourceModelSchema = SourceModelSchema;
844
588
  exports.SourceTextSchema = SourceTextSchema;
845
589
  exports.annotationColumnDefinition = annotationColumnDefinition;
846
- exports.annotationUtils = annotationUtils;
847
590
  exports.baseContextBuilder = baseContextBuilder;
848
591
  exports.buildGridStyle = buildGridStyle;
849
592
  exports.buildPrefix = buildPrefix;
850
- exports.createAnnotationConfiguration = createAnnotationConfiguration;
851
593
  exports.createAnnotationContext = createAnnotationContext;
852
594
  exports.createAnnotationStyleBodyUnsafe = createAnnotationStyleBodyUnsafe;
853
- exports.defaultRender = defaultRender;
854
595
  exports.getPaneArea = getPaneArea;
855
596
  exports.resolveConfig = resolveConfig;
856
- exports.styleFn = styleFn;
package/index.js CHANGED
@@ -1,16 +1,13 @@
1
1
  import { Body, Controller, Delete, Get, Inject, Injectable, Module, Param, Patch, Post } from "@nestjs/common";
2
2
  import { z } from "zod";
3
- import { ContextBuilder, contextBuilder, w3cAnnotation } from "@ghentcdh/w3c-utils";
3
+ import { contextBuilder } from "@ghentcdh/w3c-utils";
4
4
  import { JsonOperationsSchema, ResourceJsonShape, ViewConfigSchema, labelFromId, registerResourceExtensions } from "@ghentcdh/crouton-core";
5
- import { Debugger, PlainTextAdapter, W3CAnnotationAdapter, WordSnapper, createAnnotatedText, createHighlightStyle } from "@ghentcdh/annotated-text";
5
+ import { Debugger } from "@ghentcdh/annotated-text";
6
6
  import { CroutonApiModule, ResourceConfigRegistry } from "@ghentcdh/crouton-api";
7
7
  import { ApiBody, ApiOperation, ApiParam, ApiResponse, ApiTags } from "@nestjs/swagger";
8
8
  //#region src/lib/annotation/annotation.repository.ts
9
9
  var AnnotationCrudRepository = class {};
10
10
  //#endregion
11
- //#region ../annotation-core/src/lib/types/key-label.type.ts
12
- var KeyLabelSchema = z.object();
13
- //#endregion
14
11
  //#region ../annotation-core/src/lib/types/source.model.ts
15
12
  var SourceTypesList = ["text"];
16
13
  var SourceTextSchema = z.object({
@@ -51,13 +48,13 @@ var defaultConfig = {
51
48
  app: "ghentcdh",
52
49
  prefix: "ghentcdh",
53
50
  isDev: false,
54
- cacheTTLms: 1200 * 1e3
51
+ cacheTTLms: 12e5
55
52
  };
56
53
  var resolveConfig = (config = {}) => {
57
54
  const baseUrl = config.baseUrl ?? defaultConfig.baseUrl;
58
55
  const normalizedBaseUrl = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
59
56
  const isDev = config.isDev ?? false;
60
- const cacheTTLms = isDev ? 0 : 1200 * 1e3;
57
+ const cacheTTLms = isDev ? 0 : 12e5;
61
58
  return {
62
59
  baseUrl: normalizedBaseUrl,
63
60
  contextUrl: `${normalizedBaseUrl}ns/`,
@@ -156,15 +153,14 @@ var AnnotationJsonResourceSchema = AnnotationJsonResourceShape.transform((obj) =
156
153
  //#endregion
157
154
  //#region ../annotation-core/src/lib/types/annotation-definition.type.ts
158
155
  var annotationColumnDefinition = z.custom();
159
- var AnnotationResourceSchema = AnnotationJsonResourceShape.pick({
160
- id: true,
161
- name: true,
162
- title: true,
163
- operations: true,
164
- annotation: true
165
- }).extend({
156
+ var { id, name, title, operations } = AnnotationJsonResourceShape.shape;
157
+ var AnnotationResourceSchema = z.object({
158
+ id,
159
+ name,
160
+ title,
161
+ operations,
166
162
  annotation: AnnotationConfigSchema,
167
- context: z.instanceof(ContextBuilder).optional().nullish(),
163
+ context: z.custom(() => true).optional().nullish(),
168
164
  schemas: z.record(z.string(), ViewConfigSchema.partial()).optional().nullish()
169
165
  });
170
166
  //#endregion
@@ -198,273 +194,22 @@ var createAnnotationStyleBodyUnsafe = (annotationConfig, style) => {
198
194
  return parsed;
199
195
  };
200
196
  //#endregion
201
- //#region ../annotation-core/src/lib/utils/annotation-utils.ts
202
- var mapRelationsToLinks = (sourceUri, builders, annotations) => {
203
- return annotations.map((link) => {
204
- const builder = builders.get(link.id);
205
- const specifyResource = builder.getSpecificResourceTargets(sourceUri);
206
- if (!specifyResource || specifyResource.length === 0) return null;
207
- const relations = builder.getSpecificResourceTargets().map((s) => s.source).filter((s) => s !== sourceUri).map((uri) => {
208
- return annotations.find((a) => a.id === uri);
209
- }).filter(Boolean);
210
- return {
211
- purpose: getAnnotationType(builder),
212
- annotation: link,
213
- relations
214
- };
215
- }).filter(Boolean);
216
- };
217
- var getAnnotationType = (builder) => {
218
- return getAnnotationStyle(builder)?.id ?? "text";
219
- };
220
- var getAnnotationStyle = (builder) => {
221
- return builder.getBodiesByPurpose("styling").find((b) => b.type === "AnnotationStyle");
222
- };
223
- var getAnnotationPosition = (id, builder, allowedChildrenPerType) => {
224
- const sourceUri = builder.getSpecificResourceTargets()[0]?.source;
225
- if (!sourceUri) return null;
226
- const textPositionSelector = builder.getTextPositionSelector(sourceUri)[0];
227
- if (!textPositionSelector) return null;
228
- const type = getAnnotationStyle(builder)?.id ?? "text";
229
- return {
230
- id,
231
- start: textPositionSelector.start,
232
- end: textPositionSelector.end,
233
- allowedChildren: allowedChildrenPerType[type] ?? [],
234
- sourceUri: sourceUri ?? "",
235
- type,
236
- minX: textPositionSelector.start,
237
- minY: 0,
238
- maxX: textPositionSelector.end,
239
- maxY: 0
240
- };
241
- };
242
- var rootId = "ROOT";
243
- var createId = () => {
244
- return `mela:new-annotation:${Date.now()}`;
245
- };
246
- var updateSelector = (builder, selector) => {
247
- if (!builder.getSpecificResourceTargets(selector.source).length) builder.addTarget({
248
- type: "SpecificResource",
249
- source: selector.source
250
- });
251
- builder.updateTextPositionSelector({
252
- start: selector.start,
253
- end: selector.end
254
- }, selector.source);
255
- builder.updateTextQuoteSelector({
256
- prefix: selector.prefix,
257
- suffix: selector.suffix,
258
- exact: selector.exact
259
- }, selector.source);
260
- return builder;
261
- };
262
- var AnnotationUtilsImpl = class {
263
- annotationConfig;
264
- builderMap = /* @__PURE__ */ new Map();
265
- childParentMap = /* @__PURE__ */ new Map();
266
- parentChildMap = /* @__PURE__ */ new Map();
267
- linkedAnnotations = /* @__PURE__ */ new Map();
268
- annotations = [];
269
- allowedChildrenPerType = {};
270
- constructor(annotationConfig) {
271
- this.annotationConfig = annotationConfig;
272
- }
273
- getBuilder(annotation) {
274
- const builder = this.builderMap.get(annotation.id);
275
- if (!builder) throw new Error(`Annotation builder not found: ${annotation.id}`);
276
- return builder;
277
- }
278
- setAnnotations(annotations, allowedChildrenPerType) {
279
- this.allowedChildrenPerType = allowedChildrenPerType;
280
- this.annotations = annotations;
281
- this.parentChildMap.clear();
282
- this.childParentMap.clear();
283
- this.builderMap.clear();
284
- const annotationsWithPositons = [];
285
- const annotationIdMap = /* @__PURE__ */ new Map();
286
- annotations.forEach((annotation) => {
287
- const builder = w3cAnnotation(annotation);
288
- this.builderMap.set(annotation.id, builder);
289
- const position = getAnnotationPosition(annotation.id, builder, this.allowedChildrenPerType);
290
- annotationIdMap.set(annotation.id, annotation);
291
- if (position) annotationsWithPositons.push(position);
292
- });
293
- annotationsWithPositons.forEach((annotation) => {
294
- const parentAnnotation = annotationsWithPositons.filter((a) => a.id !== annotation.id && a.sourceUri === annotation.sourceUri && a.start <= annotation.start && a.end >= annotation.end && a.allowedChildren.some((c) => c.key === annotation.type)).sort((a, b) => a.end - a.start - (b.end - b.start))[0];
295
- const parentId = parentAnnotation?.id ?? rootId;
296
- const children = this.parentChildMap.get(parentId) ?? [];
297
- children.push(annotationIdMap.get(annotation.id));
298
- this.parentChildMap.set(parentAnnotation?.id ?? rootId, children);
299
- this.childParentMap.set(annotation.id, annotationIdMap.get(parentId) ?? null);
300
- });
301
- annotations.forEach((annotation) => {
302
- this.linkedAnnotations.set(annotation.id, mapRelationsToLinks(annotation.id, this.builderMap, annotations));
303
- });
304
- return this;
305
- }
306
- getLinks(annotation) {
307
- return this.linkedAnnotations.get(annotation.id) ?? [];
308
- }
309
- getTextPositionSelector(annotation, sourceUri) {
310
- return this.getBuilder(annotation).getTextPositionSelector(sourceUri)[0];
311
- }
312
- isAnnotationUri = (uri) => {
313
- return uri.startsWith("mela:annotation/");
314
- };
315
- getParentSelector = (builder) => {
316
- return builder.getSpecificResourceTargets().find((t) => this.isAnnotationUri(t.source)) ?? null;
317
- };
318
- getParent(annotation) {
319
- const builder = this.getBuilder(annotation);
320
- const parentUri = this.getParentSelector(builder);
321
- if (!parentUri) return null;
322
- return this.annotations.find((a) => a.id === parentUri.source) ?? null;
323
- }
324
- createAnnotation(fromAnnotation, type, data, selectors) {
325
- const styleBody = AnnotationStyleContextBuilder(this.annotationConfig).toAnnotationBody(type);
326
- const builder = w3cAnnotation(fromAnnotation ?? void 0).setMotivation("tagging").addBody(styleBody);
327
- if (!fromAnnotation?.id) builder.setId(createId());
328
- if (data && Object.keys(data).length > 0) {
329
- data.type = data.type ?? "AnnotationMetadata";
330
- data.purpose = data.purpose ?? getAnnotationType(builder);
331
- builder.updateBodyByType(data.type, data);
332
- }
333
- for (const selector of selectors) updateSelector(builder, selector);
334
- return builder.build();
335
- }
336
- createLinkAnnotation = (sourceAnnotation, targetAnnotation, type, data) => {
337
- const styleBody = AnnotationStyleContextBuilder(this.annotationConfig).toAnnotationBody(type);
338
- const builder = w3cAnnotation().setMotivation("tagging").setId(createId()).addBody(styleBody);
339
- if (data && Object.keys(data).length > 0) {
340
- data.type = data.type ?? "AnnotationMetadata";
341
- data.purpose = data.purpose ?? getAnnotationType(builder);
342
- builder.updateBodyByType(data.type, data);
343
- }
344
- builder.addTarget({ source: sourceAnnotation.id }).addTarget({ source: targetAnnotation.id });
345
- return builder.build();
346
- };
347
- cancel() {
348
- this.setAnnotations(this.annotations, this.allowedChildrenPerType);
349
- }
350
- getSourceUri = (annotation) => {
351
- const builder = this.getBuilder(annotation);
352
- const sourceUri = builder.getSourceUri();
353
- if (!sourceUri) {
354
- console.warn("no source uri");
355
- return null;
356
- }
357
- return {
358
- textPositionSelector: builder.getTextPositionSelector(sourceUri)[0],
359
- sourceUri
360
- };
361
- };
362
- getMetadata = (annotation) => {
363
- return this.getBuilder(annotation).getBodiesByType("AnnotationMetadata")?.[0] ?? null;
364
- };
365
- getAnnotationStyle = (annotation) => {
366
- return getAnnotationStyle(this.getBuilder(annotation));
367
- };
368
- getAnnotationType = (annotation) => {
369
- return getAnnotationType(this.getBuilder(annotation));
370
- };
371
- createAnnotationFromSelector(type, annotation, selector) {
372
- const builder = w3cAnnotation(annotation ?? void 0).setMotivation("tagging").setId(createId());
373
- const styleBody = AnnotationStyleContextBuilder(this.annotationConfig).toAnnotationBody(type);
374
- builder.addBody(styleBody);
375
- if (selector) updateSelector(builder, selector);
376
- return builder.build();
377
- }
378
- };
379
- var annotationUtils = (annotationConfig) => {
380
- return new AnnotationUtilsImpl(annotationConfig);
381
- };
382
- //#endregion
383
- //#region ../annotation-core/src/lib/utils/annotation-render.style.ts
384
- var defaultRender = (utils) => (annotation) => {
385
- return (utils?.getAnnotationStyle(annotation))?.target ?? "default";
386
- };
387
- var styleFn = (listStyles, utils) => (annotation) => {
388
- const style = utils?.getAnnotationStyle(annotation);
389
- if (!style) return "default";
390
- const styleId = style?.id ?? "default";
391
- if (!listStyles.includes(styleId)) {
392
- Debugger.debug("styleFn", `No style found for ${styleId}`);
393
- if (style.color) return { default: createHighlightStyle(style.color) };
394
- return "default";
395
- }
396
- return styleId;
397
- };
398
- //#endregion
399
- //#region ../annotation-core/src/lib/utils/annotation-configuration.factory.ts
400
- var groupById = (defs, valueKey) => {
401
- if (!defs) return {};
402
- return defs.reduce((acc, def) => {
403
- acc[def.id] = valueKey ? def[valueKey] : def;
404
- return acc;
405
- }, {});
406
- };
407
- var createAnnotationConfiguration = (annotationDefinitions, utils, textAdapter, annotationAdapter) => {
408
- const definitions = annotationDefinitions ?? [];
409
- const definitionsMap = groupById(definitions);
410
- const rootTypes = definitions.filter((d) => d.annotation.isRoot).map((d) => ({
411
- key: d.id,
412
- label: d.label
413
- }));
414
- const styles = groupById(definitions, "style");
415
- if (!styles["default"]) styles["default"] = {
416
- label: "Annotation",
417
- isRoot: true,
418
- style: { default: createHighlightStyle("#dd7777") },
419
- allowedChildren: [],
420
- allowedLinks: [],
421
- views: {}
422
- };
423
- const listStyles = Object.keys(styles);
424
- const allowedChildrenPerType = groupById(definitions, "allowedChildren");
425
- const defaultAnnotationAdapter = (source) => W3CAnnotationAdapter(source ? { sourceUri: source.uri } : {});
426
- const renderParams = () => ({ renderFn: defaultRender(utils) });
427
- const styleParams = () => ({ styleFn: styleFn(listStyles, utils) });
428
- const _createAnnotatedText = (id, sourceModel) => {
429
- const _textAdapter = textAdapter?.() ?? PlainTextAdapter();
430
- const annotatedText = createAnnotatedText(id, {
431
- annotationAdapter: annotationAdapter?.() ?? defaultAnnotationAdapter(sourceModel),
432
- textAdapter: _textAdapter
433
- });
434
- annotatedText.setSnapper(new WordSnapper()).setRenderParams(renderParams()).setStyleParams(styleParams()).registerStyles(styles);
435
- if (sourceModel) {
436
- const { content } = sourceModel;
437
- annotatedText.setText(content.text).setTextAdapterParams({ textDirection: content.textDirection });
438
- }
439
- return annotatedText;
440
- };
441
- return {
442
- allowedChildrenPerType,
443
- definitions,
444
- getDefinition: (id) => {
445
- return definitionsMap[id] ?? void 0;
446
- },
447
- rootTypes,
448
- createAnnotatedText: _createAnnotatedText
449
- };
450
- };
451
- //#endregion
452
197
  //#region src/lib/utils/annotation.context-builder.ts
453
198
  var ANNOTATION_DEF_CONFIG_TOKEN = Symbol("ANNOTATION_DEF_CONFIG_TOKEN");
454
199
  //#endregion
455
- //#region \0@oxc-project+runtime@0.139.0/helpers/esm/decorateMetadata.js
200
+ //#region \0@oxc-project+runtime@0.151.0/helpers/esm/decorateMetadata.js
456
201
  function __decorateMetadata(k, v) {
457
202
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
458
203
  }
459
204
  //#endregion
460
- //#region \0@oxc-project+runtime@0.139.0/helpers/esm/decorateParam.js
205
+ //#region \0@oxc-project+runtime@0.151.0/helpers/esm/decorateParam.js
461
206
  function __decorateParam(paramIndex, decorator) {
462
207
  return function(target, key) {
463
208
  decorator(target, key, paramIndex);
464
209
  };
465
210
  }
466
211
  //#endregion
467
- //#region \0@oxc-project+runtime@0.139.0/helpers/esm/decorate.js
212
+ //#region \0@oxc-project+runtime@0.151.0/helpers/esm/decorate.js
468
213
  function __decorate(decorators, target, key, desc) {
469
214
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
470
215
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -810,4 +555,4 @@ AnnotationApiModule = _AnnotationApiModule = __decorate([Module({
810
555
  exports: []
811
556
  })], AnnotationApiModule);
812
557
  //#endregion
813
- export { AnnotationApiModule, AnnotationConfigSchema, AnnotationContextService, AnnotationCrudRepository, AnnotationJsonResourceSchema, AnnotationJsonResourceShape, AnnotationMetadataType, AnnotationResourceSchema, AnnotationStyleContextBuilder, AnnotationStyleType, AnnotationW3cMapperService, BASELINE_RESOURCE_VERSION, CURRENT_RESOURCE_VERSION, KeyLabelSchema, SourceModelSchema, SourceTextSchema, annotationColumnDefinition, annotationUtils, baseContextBuilder, buildGridStyle, buildPrefix, createAnnotationConfiguration, createAnnotationContext, createAnnotationStyleBodyUnsafe, defaultRender, getPaneArea, resolveConfig, styleFn };
558
+ export { AnnotationApiModule, AnnotationConfigSchema, AnnotationContextService, AnnotationCrudRepository, AnnotationJsonResourceSchema, AnnotationJsonResourceShape, AnnotationMetadataType, AnnotationResourceSchema, AnnotationStyleContextBuilder, AnnotationStyleType, AnnotationW3cMapperService, BASELINE_RESOURCE_VERSION, CURRENT_RESOURCE_VERSION, SourceModelSchema, SourceTextSchema, annotationColumnDefinition, baseContextBuilder, buildGridStyle, buildPrefix, createAnnotationContext, createAnnotationStyleBodyUnsafe, getPaneArea, resolveConfig };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ghentcdh/annotation-api",
3
- "version": "0.0.1-alpha.15",
3
+ "version": "0.0.1-alpha.17",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/GhentCDH/annotation-editor"
@@ -10,7 +10,7 @@
10
10
  "types": "./index.d.ts",
11
11
  "peerDependencies": {
12
12
  "@ghentcdh/w3c-utils": "^1.0.11",
13
- "@nestjs/common": "^11.1.19"
13
+ "@nestjs/common": "^12.0.0"
14
14
  },
15
15
  "dependencies": {
16
16
  "@ghentcdh/crouton-api": "0.0.1-alpha.71"