From 15dd1374dbabd1b8de3f4063a7477d072812c9de Mon Sep 17 00:00:00 2001 From: Rainer Simon Date: Mon, 18 May 2020 08:43:39 +0200 Subject: [PATCH] Handling creation metadata (creator, created, modified) --- src/Environment.js | 14 +++++++++ src/editor/Editor.jsx | 29 +++++++++++++++++-- src/editor/widgets/comment/Comment.jsx | 11 ++++++- .../editor/widgets/comment/_comment.scss | 11 ++++++- 4 files changed, 60 insertions(+), 5 deletions(-) diff --git a/src/Environment.js b/src/Environment.js index 15b91d2..b3ac006 100644 --- a/src/Environment.js +++ b/src/Environment.js @@ -1,3 +1,8 @@ +/** + * Difference between server time and client time, in milliseconds + */ +let serverTimeDifference = 0; + export default { /** @@ -14,4 +19,13 @@ export default { * avatarURL .... OPTIONAL + not supported at the moment */ + // Sets a server time to + setServerTime: serverNow => { + const browserNow = Date.now() + serverTimeDifference = serverNow - browserNow; + }, + + getCurrentTimeAdjusted: () => + (new Date(Date.now() + serverTimeDifference)).toISOString() + } \ No newline at end of file diff --git a/src/editor/Editor.jsx b/src/editor/Editor.jsx index 0eb2efc..0524209 100644 --- a/src/editor/Editor.jsx +++ b/src/editor/Editor.jsx @@ -1,5 +1,6 @@ import React from 'preact/compat'; import { useState, useRef, useEffect } from 'preact/hooks'; +import Environment from '../Environment'; import setPosition from './setPosition'; import i18n from '../i18n'; @@ -63,15 +64,36 @@ const Editor = props => { return () => resizeObserver.disconnect(); }, []); + // Creator and created/modified timestamp metadata + const creationMeta = body => { + const meta = {}; + + // No point in adding meta while we're in draft state + if (!body.draft) { + const { user } = Environment; + if (user) meta.creator = {}; + if (user.id) meta.creator.id = user.id; + if (user.displayName) meta.creator.name = user.displayName; + + if (body.created) + body.modified = Environment.getCurrentTimeAdjusted(); + else + body.created = Environment.getCurrentTimeAdjusted(); + } + + return meta; + } + const onAppendBody = body => setCurrentAnnotation( currentAnnotation.clone({ - body: [ ...currentAnnotation.bodies, body ] + body: [ ...currentAnnotation.bodies, { ...body, ...creationMeta(body) } ] }) ); const onUpdateBody = (previous, updated) => setCurrentAnnotation( currentAnnotation.clone({ - body: currentAnnotation.bodies.map(body => body === previous ? updated : body) + body: currentAnnotation.bodies.map(body => + body === previous ? { ...updated, ...creationMeta(updated) } : body) }) ); @@ -84,7 +106,8 @@ const Editor = props => { const onOk = _ => { // Removes the 'draft' flag from all bodies const undraft = annotation => annotation.clone({ - body : annotation.bodies.map(({ draft, ...rest }) => rest) + body : annotation.bodies.map(({ draft, ...rest }) => + draft ? { ...rest, ...creationMeta(rest) } : rest ) }); // Current annotation is either a selection (if it was created from diff --git a/src/editor/widgets/comment/Comment.jsx b/src/editor/widgets/comment/Comment.jsx index 45bd775..1496de6 100644 --- a/src/editor/widgets/comment/Comment.jsx +++ b/src/editor/widgets/comment/Comment.jsx @@ -26,7 +26,10 @@ const Comment = props => { return props.readOnly ? (
- {props.body.value} +
{props.body.value}
+
+
{props.body.creator?.name}
+
) : (
@@ -36,6 +39,12 @@ const Comment = props => { onChange={onUpdateComment} onSaveAndClose={props.onSaveAndClose} /> + + { props.body.creator && +
+
{props.body.creator.name}
+
+ }