diff --git a/editors/demo/src/Editoria/Editoria.js b/editors/demo/src/Editoria/Editoria.js index aaa6ad801ce114851e6c462c6eaa1253592d0d0e..8837b278e3d8c3e0e7c6f5a3ea237f63a71cfb22 100644 --- a/editors/demo/src/Editoria/Editoria.js +++ b/editors/demo/src/Editoria/Editoria.js @@ -58,9 +58,9 @@ const Editoria = () => { value={demo} // readonly layout={layout} - onChange={debounce(source => { - console.log(JSON.stringify(source)); - }, 200)} + // onChange={debounce(source => { + // console.log(JSON.stringify(source)); + // }, 200)} user={user} scrollMargin={200} scrollThreshold={200} diff --git a/editors/demo/src/Editors.js b/editors/demo/src/Editors.js index 78a82dca42828507ad8168cf3eb705360720ee0d..0e36a5c07cdfbce6f7c2095a8b1470e6f4c781f5 100644 --- a/editors/demo/src/Editors.js +++ b/editors/demo/src/Editors.js @@ -90,7 +90,7 @@ const Editors = () => { case 'oen': return <OEN />; default: - return <Editoria />; + return <HHMI />; } }; diff --git a/wax-prosemirror-core/src/utilities/commands/Commands.js b/wax-prosemirror-core/src/utilities/commands/Commands.js index a8fbe6bdcc09824b393a79e7a990fc72a0b3c831..90f7b1ad8b131c1535c5fccc2059b6fd2aeb0569 100644 --- a/wax-prosemirror-core/src/utilities/commands/Commands.js +++ b/wax-prosemirror-core/src/utilities/commands/Commands.js @@ -136,11 +136,19 @@ const createComment = ( viewid, conversation = [], title = '', + posFrom, + posTo, ) => { const { selection: { $from, $to }, tr, } = state; + let fromPosition = $from.pos; + let toPosition = $to.pos; + if ($from.pos === $to.pos) { + fromPosition = posFrom; + toPosition = posTo; + } let footnote = false; let footnoteNode; state.doc.nodesBetween($from.pos, $to.pos, (node, from) => { @@ -173,14 +181,21 @@ const createComment = ( title, ); } - - toggleMark(state.config.schema.marks.comment, { - id: uuidv4(), - group, - conversation, - viewid, - title, - })(state, dispatch); + dispatch( + state.tr + .addMark( + fromPosition, + toPosition, + state.config.schema.marks.comment.create({ + id: uuidv4(), + group, + viewid, + conversation, + title, + }), + ) + .setMeta('forceUpdate', true), + ); }; const createCommentOnSingleFootnote = ( diff --git a/wax-prosemirror-services/src/CommentsService/components/ConnectedComment.js b/wax-prosemirror-services/src/CommentsService/components/ConnectedComment.js index 2435c2be684357b790c397fbca3c04a30326e15a..b119fc666f5f4c8e40a616bf5cb65b11738925e6 100644 --- a/wax-prosemirror-services/src/CommentsService/components/ConnectedComment.js +++ b/wax-prosemirror-services/src/CommentsService/components/ConnectedComment.js @@ -2,10 +2,9 @@ /* eslint react/prop-types: 0 */ import React, { useContext, useMemo, useState, useEffect } from 'react'; import { TextSelection } from 'prosemirror-state'; -import { last, maxBy } from 'lodash'; +import { last, maxBy, minBy } from 'lodash'; import styled from 'styled-components'; import { WaxContext, DocumentHelpers, Commands } from 'wax-prosemirror-core'; -import { v4 as uuidv4 } from 'uuid'; import { override } from '@pubsweet/ui-toolkit'; import CommentBox from './ui/comments/CommentBox'; @@ -88,7 +87,6 @@ export default ({ comment, top, commentId, recalculateTops, users }) => { comment.attrs.title = title || comment.attrs.title; comment.attrs.conversation.push(obj); - const id = uuidv4(); allCommentsWithSameId.forEach(singleComment => { activeView.dispatch( activeView.state.tr.removeMark( @@ -97,36 +95,22 @@ export default ({ comment, top, commentId, recalculateTops, users }) => { commentMark, ), ); - - if (activeViewId !== 'main') { - activeView.dispatch( - activeView.state.tr - .addMark( - singleComment.pos, - singleComment.pos + singleComment.node.nodeSize, - commentMark.create({ - id, - group: comment.attrs.group, - viewid: comment.attrs.viewid, - conversation: comment.attrs.conversation, - title: comment.attrs.title, - }), - ) - .setMeta('forceUpdate', true), - ); - } }); - if (activeViewId === 'main') { - Commands.createComment( - pmViews.main.state, - pmViews.main.dispatch, - comment.attrs.group, - comment.attrs.viewid, - comment.attrs.conversation, - comment.attrs.title, - ); - } + const minPos = minBy(allCommentsWithSameId, 'pos'); + const maxPos = maxBy(allCommentsWithSameId, 'pos'); + + Commands.createComment( + activeView.state, + activeView.dispatch, + comment.attrs.group, + comment.attrs.viewid, + comment.attrs.conversation, + comment.attrs.title, + minPos.pos, + maxPos.pos + last(allCommentsWithSameId).node.nodeSize, + ); + activeView.focus(); recalculateTops(); };