# HG changeset patch # User Alexandre Segura # Date 1498561939 -7200 # Node ID 2e939d9cf193ae2e0ac52aa6660ab90a353b25f3 # Parent 69eaef18b01b267b3492739f2a05c18e0bb9e822 Introduce WebAnnotationSerializer. diff -r 69eaef18b01b -r 2e939d9cf193 client/src/actions/notesActions.js --- a/client/src/actions/notesActions.js Tue Jun 27 11:38:26 2017 +0200 +++ b/client/src/actions/notesActions.js Tue Jun 27 13:12:19 2017 +0200 @@ -1,6 +1,7 @@ import uuidV1 from 'uuid/v1'; import * as types from '../constants/actionTypes'; +import WebAnnotationSerializer from '../api/WebAnnotationSerializer'; export const addNote = (session, data) => { const noteId = uuidV1(); @@ -24,7 +25,7 @@ html: data.html, tc_start: data.startedAt, tc_end: data.finishedAt, - categorization: JSON.stringify(data.categories), + categorization: WebAnnotationSerializer.serialize(note), margin_note: data.marginComment, } @@ -67,7 +68,7 @@ raw: JSON.stringify(data.raw), plain: data.plain, html: data.html, - categorization: JSON.stringify(data.categories), + categorization: WebAnnotationSerializer.serialize(note), margin_note: data.marginComment, } diff -r 69eaef18b01b -r 2e939d9cf193 client/src/api/WebAnnotationSerializer.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/src/api/WebAnnotationSerializer.js Tue Jun 27 13:12:19 2017 +0200 @@ -0,0 +1,47 @@ +import Note from '../store/noteRecord'; + +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 }) + } + + return Object.assign({}, annotation, { + "target": { + "source": source, + "selector": { + "type": "TextQuoteSelector", + "exact": category.text, + } + } + }) + }); + + } + +} + +export default WebAnnotationSerializer diff -r 69eaef18b01b -r 2e939d9cf193 client/src/api/__tests__/WebAnnotationSerializer.test.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/src/api/__tests__/WebAnnotationSerializer.test.js Tue Jun 27 13:12:19 2017 +0200 @@ -0,0 +1,76 @@ +import Note from '../../store/noteRecord'; + +import WebAnnotationSerializer from '../WebAnnotationSerializer'; + +it('serializes as expected', () => { + + const category = { + "key": "keyword", + "name": "Mot-clé", + "color": "#2ECC71", + "text": "Foo" + }; + + const comment = { + "key": "comment", + "name": "Commentaire", + "color": "#3498DB", + "hasComment": true, + "comment": "Bar", + "text": "Baz" + } + + const note = new Note({ + _id: '123456', + session: '9876543', + + plain: 'Foo', + raw: {}, + html: '', + + startedAt: '', + finishedAt: '', + + categories: [ category, comment ], + + marginComment: '' + }) + + const actual = WebAnnotationSerializer.serialize(note); + + const expected = [ + { + "@context": "http://www.w3.org/ns/anno.jsonld", + "type": "Annotation", + "id": 0, + "target": { + "source": "/session/9876543/notes/123456", + "selector": { + "type": "TextQuoteSelector", + "exact": "Foo" + } + } + }, + { + "@context": "http://www.w3.org/ns/anno.jsonld", + "type": "Annotation", + "id": 1, + "body": { + "type": "TextualBody", + "value": "Bar", + "format": "text/plain" + }, + "target": { + "source": "/session/9876543/notes/123456", + "selector": { + "type": "TextQuoteSelector", + "exact": "Baz" + } + } + } + ]; + + // console.log(JSON.stringify(actual, null, 2)); + + expect(actual).toMatchObject(expected); +});