client/src/api/WebAnnotationSerializer.js
author ymh <ymh.work@gmail.com>
Wed, 05 Sep 2018 12:27:52 +0200
changeset 155 e55ae84508bf
parent 129 d48946d164c6
child 168 ea92f4fe783d
permissions -rw-r--r--
Clean CreateSession component and remove trace of previous create session button

class WebAnnotationSerializer {

  static serialize = (note) => {

    const categories = note.get('categories');

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

    const source = "/session/" + note.get('session') + "/notes/" + note.get('_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