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({
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) => {