onAppendBody + onRemoveBody now support arrays of bodies as arg (#65)

This commit is contained in:
Rainer Simon 2021-06-30 18:57:55 +02:00
parent 3a6b6ac1db
commit 2852d0c67c
1 changed files with 25 additions and 10 deletions

View File

@ -95,18 +95,33 @@ export default class Editor extends Component {
}) })
} }
onAppendBody = (body, saveImmediately) => this.updateCurrentAnnotation({ // Shorthand
body: [ ...this.state.currentAnnotation.bodies, { ...body, ...this.creationMeta(body) } ] toArray = body =>
}, saveImmediately); 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({ this.updateCurrentAnnotation({
body: this.state.currentAnnotation.bodies.map(body => body: [ ...this.state.currentAnnotation.bodies, ...toAppend ]
body === previous ? { ...updated, ...this.creationMeta(updated) } : body) }, saveImmediately);
}, saveImmediately); }
onRemoveBody = (body, saveImmediately) => this.updateCurrentAnnotation({ onUpdateBody = (previous, updated, saveImmediately) => {
body: this.state.currentAnnotation.bodies.filter(b => b !== body) this.updateCurrentAnnotation({
}, saveImmediately); 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 **/ /** A convenience shorthand **/
onUpsertBody = (arg1, arg2, saveImmediately) => { onUpsertBody = (arg1, arg2, saveImmediately) => {