From 3c7682a84ce1fb932b259d068adb3488bab44a54 Mon Sep 17 00:00:00 2001 From: Rainer Simon Date: Sat, 26 Jun 2021 14:19:40 +0200 Subject: [PATCH] Closes #63 --- src/editor/Editor.jsx | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/editor/Editor.jsx b/src/editor/Editor.jsx index b0a4d47..51e3113 100644 --- a/src/editor/Editor.jsx +++ b/src/editor/Editor.jsx @@ -100,10 +100,24 @@ const Editor = props => { }) ); - // Just a convenience shorthand - const onUpsertBody = (maybePrevious, updated) => maybePrevious ? - onUpdateBody(maybePrevious, updated) : - onAppendBody(updated); + /** A convenience shorthand **/ + const onUpsertBody = (arg1, arg2) => { + if (arg1 == null && arg2 != null) { + // Append arg 2 as a new body + onAppendBody(arg2); + } else if (arg1 != null && arg2 != null) { + // Replace body arg1 with body arg2 + onUpdateBody(arg1, arg2); + } else if (arg1 != null && arg2 == null) { + // Find the first body with the same purpose as arg1, + // and upsert + const existing = currentAnnotation.bodies.find(b => b.purpose === arg1.purpose); + if (existing) + onUpdateBody(existing, arg1); + else + onAppendBody(arg1); + } + } const onSetProperty = (property, value) => { // A list of properties the user is NOT allowed to set