Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const annotationChanged = async (annotations, action, options) => {
- if (options.imported) return;
- const xfdfString = await annotManager.exportAnnotCommand();
- const parser = new DOMParser();
- const commandData = parser.parseFromString(xfdfString, 'text/xml');
- const addedAnnots = commandData.getElementsByTagName('add')[0];
- const modifiedAnnots = commandData.getElementsByTagName('modify')[0];
- const deletedAnnots = commandData.getElementsByTagName('delete')[0];
- let data = {
- session_id: user.session_id,
- uid: user.uid,
- socket_token: user.socket_token,
- publisherId,
- offline: false,
- annotations: []
- };
- // Added annotations
- addedAnnots.childNodes.forEach(child => {
- const annotation = generateAnnotationObject(child, 'add');
- if (annotation) data.annotations.push(annotation);
- });
- // Modified annotations
- modifiedAnnots.childNodes.forEach(child => {
- const annotation = generateAnnotationObject(child, 'modify');
- if (annotation) data.annotations.push(annotation);
- });
- // Deleted annotations
- deletedAnnots.childNodes.forEach(child => {
- const annotation = generateAnnotationObject(child, 'delete');
- if (annotation) data.annotations.push(annotation);
- });
- socket.emit('updateAnnotations', data);
- debug.info('Socket updateAnnotations sent');
- };
- const generateAnnotationObject = (annotation, action) => {
- if (annotation.nodeType === annotation.TEXT_NODE) return false;
- return {
- id:
- annotation.getAttribute('name') || annotation.childNodes[0].textContent,
- page_number: parseInt(annotation.getAttribute('page')) + 1,
- xfdf: serializer.serializeToString(annotation),
- issue_id: parseInt(mif.issue_id),
- updated: Date.now(),
- deleted: action === 'delete'
- };
- };
- const updateAnnotations = annotations => {
- debug.info('Received updateAnnotations socket');
- annotations.forEach(async ({ id, deleted, xfdf }) => {
- const annotation = await annotManager.getAnnotationById(id);
- if (annotation && deleted) {
- return annotManager.deleteAnnotation(annotation);
- }
- const action = annotation ? 'modify' : 'add';
- const xfdfCmd = convertToXfdf(xfdf, action);
- const annotationInstances = await annotManager.importAnnotCommand(
- xfdfCmd
- );
- return annotManager.drawAnnotationsFromList(annotationInstances);
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement