Added ability to change placeholder text, and make comment replies optional

This commit is contained in:
ahammouda 2022-10-01 17:47:04 -04:00
parent f4d9ccaf08
commit 9369b56174
2 changed files with 45 additions and 25 deletions

View File

@ -7,11 +7,11 @@ import PurposeSelect, { PURPOSES } from './PurposeSelect';
const validPurposes = PURPOSES.map(p => p.value); const validPurposes = PURPOSES.map(p => p.value);
/** /**
* Comments are TextualBodies where the purpose field is either * Comments are TextualBodies where the purpose field is either
* blank or 'commenting' or 'replying' * blank or 'commenting' or 'replying'
*/ */
const isComment = (body, matchAllPurposes) => { const isComment = (body, matchAllPurposes) => {
const hasMatchingPurpose = matchAllPurposes ? const hasMatchingPurpose = matchAllPurposes ?
validPurposes.indexOf(body.purpose) > -1 : body.purpose == 'commenting' || body.purpose == 'replying'; validPurposes.indexOf(body.purpose) > -1 : body.purpose == 'commenting' || body.purpose == 'replying';
return body.type === 'TextualBody' && ( return body.type === 'TextualBody' && (
@ -19,10 +19,10 @@ const isComment = (body, matchAllPurposes) => {
); );
} }
/** /**
/* A comment should be read-only if: /* A comment should be read-only if:
/* - the global read-only flag is set /* - the global read-only flag is set
/* - the current rule is 'MINE_ONLY' and the creator ID differs /* - the current rule is 'MINE_ONLY' and the creator ID differs
/* The 'editable' config flag overrides the global setting, if any /* The 'editable' config flag overrides the global setting, if any
*/ */
const isReadOnlyComment = (body, props) => { const isReadOnlyComment = (body, props) => {
@ -56,18 +56,18 @@ const getDraftReply = (existingDraft, isReply) => {
}; };
}; };
/** /**
* Renders a list of comment bodies, followed by a 'reply' field. * Renders a list of comment bodies, followed by a 'reply' field.
*/ */
const CommentWidget = props => { const CommentWidget = props => {
// All comments // All comments
const all = props.annotation ? const all = props.annotation ?
props.annotation.bodies.filter(body => isComment(body, props.purposeSelector)) : []; props.annotation.bodies.filter(body => isComment(body, props.purposeSelector)) : [];
// Add a draft reply if there isn't one already // Add a draft reply if there isn't one already
const draftReply = getDraftReply(all.find(b => b.draft == true), all.length > 1); const draftReply = getDraftReply(all.find(b => b.draft == true), all.length > 1);
// All except draft reply // All except draft reply
const comments = all.filter(b => b != draftReply); const comments = all.filter(b => b != draftReply);
@ -89,36 +89,56 @@ const CommentWidget = props => {
return ( return (
<> <>
{ comments.map((body, idx) => { comments.map((body, idx) =>
<Comment <Comment
key={idx} key={idx}
env={props.env} env={props.env}
purposeSelector={props.purposeSelector} purposeSelector={props.purposeSelector}
readOnly={isReadOnlyComment(body, props)} readOnly={isReadOnlyComment(body, props)}
body={body} body={body}
onUpdate={props.onUpdateBody} onUpdate={props.onUpdateBody}
onDelete={props.onRemoveBody} onDelete={props.onRemoveBody}
onSaveAndClose={props.onSaveAndClose} /> onSaveAndClose={props.onSaveAndClose} />
)} )}
{ comments.length === 0 && !props.readOnly && props.disableReply && props.annotation &&
{ !props.readOnly && props.annotation &&
<div className="r6o-widget comment editable"> <div className="r6o-widget comment editable">
<TextEntryField <TextEntryField
focus={props.focus} focus={props.focus}
content={draftReply.value} content={draftReply.value}
editable={true} editable={true}
placeholder={comments.length > 0 ? i18n.t('Add a reply...') : i18n.t('Add a comment...')} placeholder={props.textPlaceHolder || i18n.t('Add a comment...') }
onChange={onEditReply} onChange={onEditReply}
onSaveAndClose={() => props.onSaveAndClose()} onSaveAndClose={() => props.onSaveAndClose()}
/> />
{ props.purposeSelector && draftReply.value.length > 0 && { props.purposeSelector && draftReply.value.length > 0 &&
<PurposeSelect <PurposeSelect
editable={true} editable={true}
content={draftReply.purpose} content={draftReply.purpose}
onChange={onChangeReplyPurpose} onChange={onChangeReplyPurpose}
onSaveAndClose={() => props.onSaveAndClose()} onSaveAndClose={() => props.onSaveAndClose()}
/> />
} }
</div>
}
{ !props.readOnly && !props.disableReply && props.annotation &&
<div className="r6o-widget comment editable">
<TextEntryField
focus={props.focus}
content={draftReply.value}
editable={true}
placeholder={comments.length > 0 ? i18n.t('Add a reply...') : (props.textPlaceHolder || i18n.t('Add a comment...'))}
onChange={onEditReply}
onSaveAndClose={() => props.onSaveAndClose()}
/>
{ props.purposeSelector && draftReply.value.length > 0 &&
<PurposeSelect
editable={true}
content={draftReply.purpose}
onChange={onChangeReplyPurpose}
onSaveAndClose={() => props.onSaveAndClose()}
/>
}
</div> </div>
} }
</> </>
@ -127,9 +147,9 @@ const CommentWidget = props => {
} }
CommentWidget.disableDelete = (annotation, props) => { CommentWidget.disableDelete = (annotation, props) => {
const commentBodies = const commentBodies =
annotation.bodies.filter(body => isComment(body, props.purposeSelector)); annotation.bodies.filter(body => isComment(body, props.purposeSelector));
return commentBodies.some(comment => isReadOnlyComment(comment, props)); return commentBodies.some(comment => isReadOnlyComment(comment, props));
} }

View File

@ -13,11 +13,11 @@ const getDraftTag = existingDraft =>
const TagWidget = props => { const TagWidget = props => {
// All tags (draft + non-draft) // All tags (draft + non-draft)
const all = props.annotation ? const all = props.annotation ?
props.annotation.bodies.filter(b => b.purpose === 'tagging') : []; props.annotation.bodies.filter(b => b.purpose === 'tagging') : [];
// Last draft tag goes into the input field // Last draft tag goes into the input field
const draftTag = getDraftTag(all.slice().reverse().find(b => b.draft)); const draftTag = getDraftTag(all.slice().reverse().find(b => b.draft));
// All except draft tag // All except draft tag
const tags = all.filter(b => b != draftTag); const tags = all.filter(b => b != draftTag);
@ -66,7 +66,7 @@ const TagWidget = props => {
if (draftTag.value.trim().length === 0) { if (draftTag.value.trim().length === 0) {
props.onAppendBody(toSubmit); props.onAppendBody(toSubmit);
} else { } else {
props.onUpdateBody(draftTag, toSubmit); props.onUpdateBody(draftTag, toSubmit);
} }
} }
@ -96,9 +96,9 @@ const TagWidget = props => {
} }
{!props.readOnly && {!props.readOnly &&
<Autocomplete <Autocomplete
focus={props.focus} focus={props.focus}
placeholder={i18n.t('Add tag...')} placeholder={props.textPlaceHolder || i18n.t('Add tag...')}
vocabulary={props.vocabulary || []} vocabulary={props.vocabulary || []}
onChange={onDraftChange} onChange={onDraftChange}
onSubmit={onSubmit}/> onSubmit={onSubmit}/>