TypeScript types and utility functions for handling Web Annotations.

web-annotation-utils/lib/ WebAnnotation.d.ts
111 lines
4.0 KiB

  1. import type { OneOrMore, OneOrMoreIncluding, ZeroOrMore } from './multiplicity-utils.js';
  2. /**
  3. * A Web Annotation object.
  4. *
  5. * This is an interpretation of the Web Annotation Data Model:
  6. * <https://www.w3.org/TR/2017/REC-annotation-model-20170223/>
  7. *
  8. * TODO Deal more systemically with ‘relations’, i.e. values that could be
  9. * either a nested object or a URI referring to such an object.
  10. */
  11. export interface WebAnnotation {
  12. '@context': OneOrMoreIncluding<string, 'http://www.w3.org/ns/anno.jsonld'>;
  13. type: OneOrMoreIncluding<string, 'Annotation'>;
  14. id: string;
  15. target: OneOrMore<Target>;
  16. creator?: OneOrMore<Agent>;
  17. created?: UtcDateTime;
  18. generator?: OneOrMore<Agent>;
  19. generated?: UtcDateTime;
  20. modified?: UtcDateTime;
  21. motivation?: OneOrMore<Motivation>;
  22. audience?: ZeroOrMore<Audience>;
  23. rights?: ZeroOrMore<string>;
  24. canonical?: string;
  25. via?: ZeroOrMore<string>;
  26. body?: BodyChoice | OneOrMore<Body>;
  27. bodyValue?: string;
  28. }
  29. /**
  30. * A slightly stricter type for WebAnnotation, not allowing both a body and bodyValue.
  31. */
  32. export declare type WebAnnotationStrict = WebAnnotation & (WithBody | WithBodyValue | WithoutBody);
  33. interface WithBody {
  34. body: BodyChoice | OneOrMore<Body>;
  35. bodyValue?: undefined;
  36. }
  37. interface WithBodyValue {
  38. body?: undefined;
  39. bodyValue: string;
  40. }
  41. interface WithoutBody {
  42. body?: undefined;
  43. bodyValue?: undefined;
  44. }
  45. export declare type Body = string | BodyObject;
  46. export declare type BodyObject = {
  47. creator?: OneOrMore<Agent>;
  48. created?: UtcDateTime;
  49. modified?: UtcDateTime;
  50. purpose?: OneOrMore<Motivation>;
  51. } & (TextualBody | SpecificResource | ExternalResource);
  52. export declare type Target = string | SpecificResource | ExternalResource;
  53. export declare type Agent = string | {
  54. id?: string;
  55. type?: OneOrMore<'Person' | 'Organization' | 'Software'>;
  56. name?: OneOrMore<string>;
  57. nickname?: OneOrMore<string>;
  58. email?: OneOrMore<string>;
  59. email_sha1?: OneOrMore<string>;
  60. homepage?: OneOrMore<string>;
  61. };
  62. export declare type Audience = string | {
  63. id?: string;
  64. type?: string;
  65. };
  66. export interface BodyChoice {
  67. type: 'Choice';
  68. items: Body[];
  69. }
  70. export interface TextualBody extends Omit<ExternalResource, 'id' | 'type'> {
  71. id?: string;
  72. type: 'TextualBody';
  73. value: string;
  74. }
  75. export interface SpecificResource {
  76. id?: string;
  77. type?: 'SpecificResource';
  78. source: string;
  79. selector?: string | OneOrMore<Selector>;
  80. accessibility?: AccessibilityFeatures;
  81. rights?: ZeroOrMore<string>;
  82. canonical?: string;
  83. via?: ZeroOrMore<string>;
  84. }
  85. export interface Selector {
  86. type?: string;
  87. refinedBy?: Selector;
  88. }
  89. export interface ExternalResource {
  90. id: string;
  91. type?: OneOrMore<'Dataset' | 'Image' | 'Video' | 'Sound' | 'Text'>;
  92. format?: OneOrMore<string>;
  93. language?: OneOrMore<string>;
  94. processingLanguage?: string;
  95. textDirection?: 'ltr' | 'rtl' | 'auto';
  96. accessibility?: AccessibilityFeatures;
  97. rights?: ZeroOrMore<string>;
  98. canonical?: string;
  99. via?: ZeroOrMore<string>;
  100. }
  101. export declare type Motivation = 'assessing' | 'bookmarking' | 'classifying' | 'commenting' | 'describing' | 'editing' | 'highlighting' | 'identifying' | 'linking' | 'moderating' | 'questioning' | 'replying' | 'tagging';
  102. declare type UtcDateTime = `${string}Z`;
  103. declare global {
  104. interface Date {
  105. toISOString(): UtcDateTime;
  106. }
  107. }
  108. export declare type AccessibilityFeatures = ZeroOrMore<AccessibilityFeature> | 'none' | ['none'];
  109. export declare type AccessibilityFeature = 'annotations' | 'ARIA' | 'bookmarks' | 'index' | 'printPageNumbers' | 'readingOrder' | 'structuralNavigation' | 'tableOfContents' | 'taggedPDF' | 'alternativeText' | 'audioDescription' | 'captions' | 'describedMath' | 'longDescription' | 'rubyAnnotations' | 'signLanguage' | 'transcript' | 'displayTransformability' | 'synchronizedAudioText' | 'timingControl' | 'unlocked' | 'ChemML' | 'latex' | 'MathML' | 'ttsMarkup' | 'highContrastAudio' | 'highContrastDisplay' | 'largePrint' | 'braille' | 'tactileGraphic' | 'tactileObject';
  110. export {};