openannotation / annotator

Annotation tools for the web. Select text, images, or (nearly) anything else, and add your notes.
http://annotatorjs.org
Other
2.67k stars 532 forks source link

No annotations after successful responce from custom API to annotator.storage #691

Closed alordiel closed 5 years ago

alordiel commented 5 years ago

Hello, I'm trying to create a WordPress plugin using annotator v.2 and the store module with targeting the site's custom API for storing the annotations. I have so far created the API and had worked out to get at least to receive the "POST" method on creating annotation.

The problem I got is that whenever the user adds an annotation it get's stored, the API returns the ID and status 200 but on the front end the annotation disappeared and it seems that is not created at all.

My JS is follow:

    app.include(annotator.ui.main);
    app.include(annotator.storage.http, {
      prefix: 'https://msa.test/wp-json/eedition-notes/v1'
    });
    app.start()

Request header

Request URL: https://msa.test/wp-json/eedition-notes/v1/annotations
Request method: POST
Host: msa.test
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://msa.test/lektion-1-deutsch/
Content-Type: application/json; charset=utf-8
X-Requested-With: XMLHttpRequest
Content-Length: 388
Cookie: ...some cookies...
DNT: 1
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

Response

Access-Control-Allow-Headers: Authorization, Content-Type
Content-Type: application/json; charset=UTF-8
Status code: 200
Version: HTTP 1.1

Response payload

{"id":"5b717d03d02cd","uri":"https:\/\/msa.test","user":"spas"}

Am I missing something?

tilgovi commented 5 years ago

The annotation needs to have a ranges property for it to be highlighted under the default ui plugin. You'll have to return the full annotation from the response. What does the request look like?

alordiel commented 5 years ago

I'm returning what is written in the documentation so basically It is only the id, uri, and user. I will give a try with returning the ranges as well. And will let you know if it is working.

alordiel commented 5 years ago

Thanks @tilgovi That was it. So now my php code is returning something like this as API response:

$json = $request->get_json_params();
// some other code
return ['id'=> $my_id, 'uri'=>site_url(), 'ranges'=> $json['ranges'], 'text'=> $json['text']];

And it works just at it should.