Skip to content
Snippets Groups Projects
Select Git revision
  • e79e44b3d629cd919501fb1d64c2ae72c21540d6
  • demo_ci_gitlab_pages default
  • demo_gitlab_ci
  • 5-images-in-annotations
  • 5-final-images
  • 5-chpk-images-in-annot
  • tetras-main protected
  • 5-rebase-images-in-annot
  • 5-wip-images-in-annot
  • tmp
  • 1-edit-annotations-on-videos
  • 5-old-images-in-annotations
  • old_demo_ci_gitlab_pages
  • images_annotations
  • wip
  • devsetup
  • wip-annot-video-ui
  • wip-annotations-on-videos
  • master
  • v0.4.0_react16
  • wip-debugging-annotations
21 results

LocalStorageAdapter.js

Blame
  • Forked from IIIF / Mirador / Mirador annotations
    Source project has a limited visibility.
    LocalStorageAdapter.js 1.50 KiB
    /** */
    export default class LocalStorageAdapter {
      /** */
      constructor(annotationPageId) {
        this.annotationPageId = annotationPageId;
      }
    
      /** */
      async create(annotation) {
        const emptyAnnoPage = {
          id: this.annotationPageId,
          items: [],
          type: 'AnnotationPage',
        };
        const annotationPage = await this.all() || emptyAnnoPage;
        annotationPage.items.push(annotation);
        localStorage.setItem(this.annotationPageId, JSON.stringify(annotationPage));
        return annotationPage;
      }
    
      /** */
      async update(annotation) {
        const annotationPage = await this.all();
        if (annotationPage) {
          const currentIndex = annotationPage.items.findIndex((item) => item.id === annotation.id);
          annotationPage.items.splice(currentIndex, 1, annotation);
          localStorage.setItem(this.annotationPageId, JSON.stringify(annotationPage));
          return annotationPage;
        }
        return null;
      }
    
      /** */
      async delete(annoId) {
        const annotationPage = await this.all();
        if (annotationPage) {
          annotationPage.items = annotationPage.items.filter((item) => item.id !== annoId);
        }
        localStorage.setItem(this.annotationPageId, JSON.stringify(annotationPage));
        return annotationPage;
      }
    
      /** */
      async get(annoId) {
        const annotationPage = await this.all();
        if (annotationPage) {
          return annotationPage.items.find((item) => item.id === annoId);
        }
        return null;
      }
    
      /** */
      async all() {
        return JSON.parse(localStorage.getItem(this.annotationPageId));
      }
    }