Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const annotationChanged = async (annotations, action, options) => {
  2.     if (options.imported) return;
  3.  
  4.     const xfdfString = await annotManager.exportAnnotCommand();
  5.     const parser = new DOMParser();
  6.     const commandData = parser.parseFromString(xfdfString, 'text/xml');
  7.     const addedAnnots = commandData.getElementsByTagName('add')[0];
  8.     const modifiedAnnots = commandData.getElementsByTagName('modify')[0];
  9.     const deletedAnnots = commandData.getElementsByTagName('delete')[0];
  10.  
  11.     let data = {
  12.       session_id: user.session_id,
  13.       uid: user.uid,
  14.       socket_token: user.socket_token,
  15.       publisherId,
  16.       offline: false,
  17.       annotations: []
  18.     };
  19.  
  20.     // Added annotations
  21.     addedAnnots.childNodes.forEach(child => {
  22.       const annotation = generateAnnotationObject(child, 'add');
  23.       if (annotation) data.annotations.push(annotation);
  24.     });
  25.  
  26.     // Modified annotations
  27.     modifiedAnnots.childNodes.forEach(child => {
  28.       const annotation = generateAnnotationObject(child, 'modify');
  29.       if (annotation) data.annotations.push(annotation);
  30.     });
  31.  
  32.     // Deleted annotations
  33.     deletedAnnots.childNodes.forEach(child => {
  34.       const annotation = generateAnnotationObject(child, 'delete');
  35.       if (annotation) data.annotations.push(annotation);
  36.     });
  37.  
  38.     socket.emit('updateAnnotations', data);
  39.     debug.info('Socket updateAnnotations sent');
  40.   };
  41.  
  42.   const generateAnnotationObject = (annotation, action) => {
  43.     if (annotation.nodeType === annotation.TEXT_NODE) return false;
  44.  
  45.     return {
  46.       id:
  47.         annotation.getAttribute('name') || annotation.childNodes[0].textContent,
  48.       page_number: parseInt(annotation.getAttribute('page')) + 1,
  49.       xfdf: serializer.serializeToString(annotation),
  50.       issue_id: parseInt(mif.issue_id),
  51.       updated: Date.now(),
  52.       deleted: action === 'delete'
  53.     };
  54.   };
  55.  
  56.   const updateAnnotations = annotations => {
  57.     debug.info('Received updateAnnotations socket');
  58.  
  59.     annotations.forEach(async ({ id, deleted, xfdf }) => {
  60.       const annotation = await annotManager.getAnnotationById(id);
  61.       if (annotation && deleted) {
  62.         return annotManager.deleteAnnotation(annotation);
  63.       }
  64.  
  65.       const action = annotation ? 'modify' : 'add';
  66.       const xfdfCmd = convertToXfdf(xfdf, action);
  67.       const annotationInstances = await annotManager.importAnnotCommand(
  68.         xfdfCmd
  69.       );
  70.       return annotManager.drawAnnotationsFromList(annotationInstances);
  71.     });
  72.   };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement