client/src/api/WebAnnotationSerializer.js
author duong tam kien <tk@deveha.com>
Tue, 18 Jul 2017 18:15:51 +0200
changeset 121 21ac67ebf9e7
parent 102 b0e36664f1f2
child 129 d48946d164c6
permissions -rw-r--r--
all screens are now covered (but not completed)

class WebAnnotationSerializer {

  static serialize = (note) => {

    const categories = note.categories;

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

    const source = "/session/" + note.session + "/notes/" + note._id;

    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