From 103e89673c6bd980e5167e240089f4f8bf9b59ed Mon Sep 17 00:00:00 2001 From: Rainer Simon Date: Sat, 28 Nov 2020 17:35:44 +0100 Subject: [PATCH] Updated WebAnnotation/Selection equals method --- package-lock.json | 7 ++++++- package.json | 1 + src/WebAnnotation.js | 6 ++++-- src/editor/Editor.jsx | 5 ++--- src/selection/Selection.js | 7 ++++--- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index e077a0b..07df5e6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@recogito/recogito-client-core", - "version": "0.2.4", + "version": "0.2.5", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -3304,6 +3304,11 @@ "integrity": "sha1-lpGEQOMEGnpBT4xS48V06zw+HgU=", "dev": true }, + "fast-deep-equal": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", + "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==" + }, "fast-json-stable-stringify": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", diff --git a/package.json b/package.json index ed5afbc..6137703 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "dependencies": { "axios": "^0.19.0", "downshift": "^5.4.6", + "fast-deep-equal": "^3.1.3", "node-polyglot": "^2.4.0", "preact": "^10.4.1", "react-autosize-textarea": "^7.1.0", diff --git a/src/WebAnnotation.js b/src/WebAnnotation.js index f01ea9e..fb07086 100644 --- a/src/WebAnnotation.js +++ b/src/WebAnnotation.js @@ -1,4 +1,5 @@ import { v4 as uuid } from 'uuid'; +import * as equals from 'fast-deep-equal'; export default class WebAnnotation { @@ -24,7 +25,7 @@ export default class WebAnnotation { return new WebAnnotation({ ...this.underlying, ...opt_props}, this.opts); } - /** An equality check based on the underlying object or (if given) ID **/ + /** An equality check based on the underlying object **/ isEqual(other) { if (other?.type !== 'Annotation') { return false; @@ -32,8 +33,9 @@ export default class WebAnnotation { return true; } else if (!this.underlying.id || !other.underlying.id) { return false; + } else { + return equals(this.underlying, other.underlying); } - return this.underlying.id === other.underlying.id } get readOnly() { diff --git a/src/editor/Editor.jsx b/src/editor/Editor.jsx index e75bbb6..552d27f 100644 --- a/src/editor/Editor.jsx +++ b/src/editor/Editor.jsx @@ -41,9 +41,8 @@ const Editor = props => { // on move. Therefore, don't update if a) props.annotation equals // the currentAnnotation, or props.annotation and currentAnnotations are // a selection, just created by the user. - const preventUpdate = currentAnnotation?.isEqual(annotation) || - (currentAnnotation?.isSelection && annotation?.isSelection); - + const preventUpdate = currentAnnotation?.isEqual(annotation); + if (!preventUpdate) setCurrentAnnotation(annotation); diff --git a/src/selection/Selection.js b/src/selection/Selection.js index 784e453..59a8216 100644 --- a/src/selection/Selection.js +++ b/src/selection/Selection.js @@ -1,5 +1,6 @@ import WebAnnotation from '../WebAnnotation'; import { v4 as uuid } from 'uuid'; +import * as equals from 'fast-deep-equal'; /** * An "annotation in draft mode". Really the same @@ -8,10 +9,10 @@ import { v4 as uuid } from 'uuid'; */ export default class Selection { - constructor(target) { + constructor(target, body) { this.underlying = { type: 'Selection', - body: [], + body: body || [], target } } @@ -45,7 +46,7 @@ export default class Selection { if (!other) { return false; } else { - return this.underlying === other.underlying; + return equals(this.underlying, other.underlying); } }