From 2852d0c67c1377061f505047178108582f7ac9f4 Mon Sep 17 00:00:00 2001 From: Rainer Simon Date: Wed, 30 Jun 2021 18:57:55 +0200 Subject: [PATCH] onAppendBody + onRemoveBody now support arrays of bodies as arg (#65) --- src/editor/Editor.jsx | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/editor/Editor.jsx b/src/editor/Editor.jsx index 57e0ee9..65925ef 100644 --- a/src/editor/Editor.jsx +++ b/src/editor/Editor.jsx @@ -95,18 +95,33 @@ export default class Editor extends Component { }) } - onAppendBody = (body, saveImmediately) => this.updateCurrentAnnotation({ - body: [ ...this.state.currentAnnotation.bodies, { ...body, ...this.creationMeta(body) } ] - }, saveImmediately); + // Shorthand + toArray = body => + Array.isArray(body) ? body : [ body ]; + + onAppendBody = (bodyOrBodies, saveImmediately) => { + const toAppend = this.toArray(bodyOrBodies).map(b => + ({ ...b, ...this.creationMeta(b) })); - onUpdateBody = (previous, updated, saveImmediately) => this.updateCurrentAnnotation({ - body: this.state.currentAnnotation.bodies.map(body => - body === previous ? { ...updated, ...this.creationMeta(updated) } : body) - }, saveImmediately); + this.updateCurrentAnnotation({ + body: [ ...this.state.currentAnnotation.bodies, ...toAppend ] + }, saveImmediately); + } - onRemoveBody = (body, saveImmediately) => this.updateCurrentAnnotation({ - body: this.state.currentAnnotation.bodies.filter(b => b !== body) - }, saveImmediately); + onUpdateBody = (previous, updated, saveImmediately) => { + this.updateCurrentAnnotation({ + body: this.state.currentAnnotation.bodies.map(body => + body === previous ? { ...updated, ...this.creationMeta(updated) } : body) + }, saveImmediately); + } + + onRemoveBody = (bodyOrBodies, saveImmediately) => { + const toRemove = this.toArray(bodyOrBodies); + + this.updateCurrentAnnotation({ + body: this.state.currentAnnotation.bodies.filter(b => !toRemove.includes(b)) + }, saveImmediately); + } /** A convenience shorthand **/ onUpsertBody = (arg1, arg2, saveImmediately) => {