@annotorious/annotorious 3.7.14-beta → 3.7.15

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.
@@ -2,7 +2,7 @@ import { W3CAnnotation, W3CAnnotationTarget } from '@annotorious/core';
2
2
  import { FragmentSelector } from './fragment';
3
3
  import { SVGSelector } from './svg';
4
4
  export interface W3CImageAnnotation extends W3CAnnotation {
5
- target: W3CImageAnnotationTarget | W3CImageAnnotationTarget[];
5
+ target: W3CImageAnnotationTarget | W3CImageAnnotationTarget[] | string;
6
6
  }
7
7
  export interface W3CImageAnnotationTarget extends W3CAnnotationTarget {
8
8
  selector: W3CImageSelector | W3CImageSelector[];
@@ -4,5 +4,6 @@ export interface FragmentSelector {
4
4
  conformsTo: 'http://www.w3.org/TR/media-frags/';
5
5
  value: string;
6
6
  }
7
+ export declare const isFragmentSelector: (selector: any) => boolean;
7
8
  export declare const parseFragmentSelector: (fragmentOrSelector: FragmentSelector | string, invertY?: boolean) => Rectangle;
8
9
  export declare const serializeFragmentSelector: (geometry: RectangleGeometry) => FragmentSelector;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@annotorious/annotorious",
3
- "version": "3.7.14-beta",
3
+ "version": "3.7.15",
4
4
  "description": "Add image annotation functionality to any web page with a few lines of JavaScript",
5
5
  "author": "Rainer Simon",
6
6
  "license": "BSD-3-Clause",
@@ -51,7 +51,7 @@
51
51
  "vitest": "^3.2.4"
52
52
  },
53
53
  "dependencies": {
54
- "@annotorious/core": "3.7.14-beta",
54
+ "@annotorious/core": "3.7.15",
55
55
  "dequal": "^2.0.3",
56
56
  "rbush": "^4.0.1",
57
57
  "simplify-js": "^1.2.4",
@@ -4,7 +4,7 @@ import type { SVGSelector } from './svg';
4
4
 
5
5
  export interface W3CImageAnnotation extends W3CAnnotation {
6
6
 
7
- target: W3CImageAnnotationTarget | W3CImageAnnotationTarget[];
7
+ target: W3CImageAnnotationTarget | W3CImageAnnotationTarget[] | string;
8
8
 
9
9
  }
10
10
 
@@ -4,7 +4,7 @@ import type { FormatAdapter, ParseResult, W3CAnnotation } from '@annotorious/cor
4
4
  import { ShapeType } from '../core';
5
5
  import type { ImageAnnotation, RectangleGeometry } from '../core';
6
6
  import type {FragmentSelector } from './fragment';
7
- import { parseFragmentSelector, serializeFragmentSelector } from './fragment';
7
+ import { isFragmentSelector, parseFragmentSelector, serializeFragmentSelector } from './fragment';
8
8
  import type { SVGSelector } from './svg';
9
9
  import { parseSVGSelector, serializeSVGSelector } from './svg';
10
10
  import type { W3CImageAnnotation } from './W3CImageAnnotation';
@@ -58,7 +58,7 @@ export const parseW3CImageAnnotation = (
58
58
  ? w3cTarget.selector[0] : w3cTarget.selector;
59
59
 
60
60
  const selector =
61
- typeof w3cSelector === 'string' || w3cSelector?.type === 'FragmentSelector' ?
61
+ isFragmentSelector(w3cSelector) ?
62
62
  parseFragmentSelector(w3cSelector as FragmentSelector, opts.invertY) :
63
63
  w3cSelector?.type === 'SvgSelector' ?
64
64
  parseSVGSelector(w3cSelector as SVGSelector) : undefined;
@@ -133,7 +133,7 @@ export const serializeW3CImageAnnotation = (
133
133
  // Remove core properties that should not appear in the W3C annotation
134
134
  delete serialized.bodies;
135
135
 
136
- if ('annotation' in serialized.target)
136
+ if (typeof serialized.target !== 'string' && 'annotation' in serialized.target)
137
137
  delete serialized.target.annotation;
138
138
 
139
139
  return serialized;
@@ -11,6 +11,23 @@ export interface FragmentSelector {
11
11
 
12
12
  }
13
13
 
14
+ export const isFragmentSelector = (
15
+ selector: any
16
+ ): boolean => {
17
+ if (selector?.type === 'FragmentSelector')
18
+ return true;
19
+
20
+ if (typeof selector === 'string') {
21
+ const hashIndex = selector.indexOf("#");
22
+ if (hashIndex < 0) return false;
23
+
24
+ const xywh = /^#xywh(?:=(?:pixel:|percent:)?)\s*\d+(\.\d*)?,\s*\d+(\.\d*)?,\s*\d+(\.\d*)?,\s*\d+(\.\d*)?$/i;
25
+ return xywh.test(selector);
26
+ }
27
+
28
+ return false;
29
+ }
30
+
14
31
  export const parseFragmentSelector = (
15
32
  fragmentOrSelector: FragmentSelector | string,
16
33
  invertY = false
@@ -21,6 +38,9 @@ export const parseFragmentSelector = (
21
38
  const regex = /(xywh)=(pixel|percent)?:?(.+?),(.+?),(.+?),(.+)*/g;
22
39
 
23
40
  const matches = [...fragment.matchAll(regex)][0];
41
+
42
+ if (!matches) throw new Error('Not a MediaFragment: ' + fragment);
43
+
24
44
  const [_, prefix, unit, a, b, c, d] = matches;
25
45
 
26
46
  if (prefix !== 'xywh') throw new Error('Unsupported MediaFragment: ' + fragment);