Updated WebAnnotation/Selection equals method

This commit is contained in:
Rainer Simon 2020-11-28 17:35:44 +01:00
parent ffee4858d3
commit 103e89673c
5 changed files with 17 additions and 9 deletions

7
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@recogito/recogito-client-core", "name": "@recogito/recogito-client-core",
"version": "0.2.4", "version": "0.2.5",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
@ -3304,6 +3304,11 @@
"integrity": "sha1-lpGEQOMEGnpBT4xS48V06zw+HgU=", "integrity": "sha1-lpGEQOMEGnpBT4xS48V06zw+HgU=",
"dev": true "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": { "fast-json-stable-stringify": {
"version": "2.1.0", "version": "2.1.0",
"resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz",

View File

@ -44,6 +44,7 @@
"dependencies": { "dependencies": {
"axios": "^0.19.0", "axios": "^0.19.0",
"downshift": "^5.4.6", "downshift": "^5.4.6",
"fast-deep-equal": "^3.1.3",
"node-polyglot": "^2.4.0", "node-polyglot": "^2.4.0",
"preact": "^10.4.1", "preact": "^10.4.1",
"react-autosize-textarea": "^7.1.0", "react-autosize-textarea": "^7.1.0",

View File

@ -1,4 +1,5 @@
import { v4 as uuid } from 'uuid'; import { v4 as uuid } from 'uuid';
import * as equals from 'fast-deep-equal';
export default class WebAnnotation { export default class WebAnnotation {
@ -24,7 +25,7 @@ export default class WebAnnotation {
return new WebAnnotation({ ...this.underlying, ...opt_props}, this.opts); 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) { isEqual(other) {
if (other?.type !== 'Annotation') { if (other?.type !== 'Annotation') {
return false; return false;
@ -32,8 +33,9 @@ export default class WebAnnotation {
return true; return true;
} else if (!this.underlying.id || !other.underlying.id) { } else if (!this.underlying.id || !other.underlying.id) {
return false; return false;
} else {
return equals(this.underlying, other.underlying);
} }
return this.underlying.id === other.underlying.id
} }
get readOnly() { get readOnly() {

View File

@ -41,9 +41,8 @@ const Editor = props => {
// on move. Therefore, don't update if a) props.annotation equals // on move. Therefore, don't update if a) props.annotation equals
// the currentAnnotation, or props.annotation and currentAnnotations are // the currentAnnotation, or props.annotation and currentAnnotations are
// a selection, just created by the user. // a selection, just created by the user.
const preventUpdate = currentAnnotation?.isEqual(annotation) || const preventUpdate = currentAnnotation?.isEqual(annotation);
(currentAnnotation?.isSelection && annotation?.isSelection);
if (!preventUpdate) if (!preventUpdate)
setCurrentAnnotation(annotation); setCurrentAnnotation(annotation);

View File

@ -1,5 +1,6 @@
import WebAnnotation from '../WebAnnotation'; import WebAnnotation from '../WebAnnotation';
import { v4 as uuid } from 'uuid'; import { v4 as uuid } from 'uuid';
import * as equals from 'fast-deep-equal';
/** /**
* An "annotation in draft mode". Really the same * An "annotation in draft mode". Really the same
@ -8,10 +9,10 @@ import { v4 as uuid } from 'uuid';
*/ */
export default class Selection { export default class Selection {
constructor(target) { constructor(target, body) {
this.underlying = { this.underlying = {
type: 'Selection', type: 'Selection',
body: [], body: body || [],
target target
} }
} }
@ -45,7 +46,7 @@ export default class Selection {
if (!other) { if (!other) {
return false; return false;
} else { } else {
return this.underlying === other.underlying; return equals(this.underlying, other.underlying);
} }
} }