aron / annotator.offline.js

Offline storage for the Annotator
http://github.com/aron/annotator.offline.js
MIT License
43 stars 9 forks source link

[help] how to add a unique id to every annotation in every page? #2

Closed casql closed 10 years ago

casql commented 10 years ago

Hi everyone,

I am using annotator offline in a publication I am building with the Baker Framework. All of the pages (articles) are very similarly built, the only thing that changes is the actual text, not the markup.

The problem I am getting is that an annotation made in (say) the first p inside the #content div of a singular article causes the same annotation to appear in every first p of the #content div in every article.

Is there a way around this? I have read the documentation and I suppose I would have to add an ID to every annotation made usinf getUniqueKey and/or setAnnotationData but I have no clue of how to write it. What I have so far is the plugin initiated with the sample code that you have in the example:

                jQuery('#content').annotator().annotator("addPlugin", "Offline", {
                  online: function (plugin) {
                    // Do something when the browser is online.
                  },
                  offline: function (plugin) {
                    // Do something when the browser goes offline.
                  }
                });

Anything helps, thanks a lot!

aron commented 10 years ago

It seems like you want to use a combination of setAnnotationData and shouldLoadAnnotation:

jQuery('#content').annotator().annotator("addPlugin", "Offline", {
  setAnnotationData: function (ann) {
    if (!ann.pageName) {
      ann.pageName = jQuery('title').text(); // Use the page title
    }
  },
  shouldLoadAnnotation: function (ann) {
    return ann.pageName === jQuery('title').text();
  }
});

Here, you'll need to replace jQuery('title').text() with something unique to the page.

casql commented 10 years ago

It works! Thank you very much, I was nowhere near to writing anything like this. Thank you!

aron commented 10 years ago

Great, I'll add a block to the README to clarify this.