client/src/api/WebAnnotationSerializer.js
author ymh <ymh.work@gmail.com>
Fri, 28 Jul 2017 19:40:35 +0200
changeset 129 d48946d164c6
parent 102 b0e36664f1f2
child 168 ea92f4fe783d
permissions -rw-r--r--
Add a first version of synchronisation Remove redux-offline dependency make the redux state fully immutable TODO: better error management TODO: make syncronization work automatically

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