client/src/api/WebAnnotationSerializer.js
author ymh <ymh.work@gmail.com>
Thu, 29 Jul 2021 19:04:01 +0200
changeset 209 a01e6a4dc9d2
parent 168 ea92f4fe783d
permissions -rw-r--r--
Add changes for deployments

import * as R from 'ramda';

class WebAnnotationSerializer {

  static serialize = (note) => {

    const categories = R.prop('categories', note);

    const baseAnnotation = {
      '@context': "http://www.w3.org/ns/anno.jsonld",
      "type":     "Annotation",
    }

    const source = "/session/" + R.prop('session', note) + "/notes/" + R.prop('_id', note);

    return categories.map((category, index) => {

      let annotation = Object.assign({}, baseAnnotation, {
        id: index
      });

      if (category.hasOwnProperty('hasComment') && category.hasComment) {
        const body = {
          "type": "TextualBody",
          "value": category.comment,
          "format": "text/plain"
        };

        annotation = Object.assign({}, annotation, { body })
      }

      const selectors = [
        {
          "type": "TextQuoteSelector",
          "exact": category.text,
        }, {
          "type": "TextPositionSelector",
          "start": category.selection.start,
          "end": category.selection.end,
        }
      ]

      return Object.assign({}, annotation, {
        "target": {
          "source": source,
          "selector": selectors
        }
      })
    });

  }

}

export default WebAnnotationSerializer