diff --git a/wax-prosemirror-core/src/utilities/commands/Commands.js b/wax-prosemirror-core/src/utilities/commands/Commands.js index 64d14487222f59565c4f1c32d85e76a0f8aa9db2..e9cf82a1beabb236946146da90ad2304388e1684 100644 --- a/wax-prosemirror-core/src/utilities/commands/Commands.js +++ b/wax-prosemirror-core/src/utilities/commands/Commands.js @@ -128,7 +128,7 @@ const isOnSameTextBlock = state => { return false; }; -const createComment = (state, dispatch, group, viewid) => { +const createComment = (state, dispatch, group, viewid, conversation = []) => { const { selection: { $from, $to }, tr, @@ -147,20 +147,38 @@ const createComment = (state, dispatch, group, viewid) => { footnoteNode.content.size + 2 === state.selection.to - state.selection.from ) { - return createCommentOnSingleFootnote(state, dispatch, group, viewid); + return createCommentOnSingleFootnote( + state, + dispatch, + group, + viewid, + conversation, + ); } - return createCommentOnFootnote(state, dispatch, group, viewid); + return createCommentOnFootnote( + state, + dispatch, + group, + viewid, + conversation, + ); } toggleMark(state.config.schema.marks.comment, { id: uuidv4(), group, - conversation: [], + conversation, viewid, })(state, dispatch); }; -const createCommentOnSingleFootnote = (state, dispatch, group, viewid) => { +const createCommentOnSingleFootnote = ( + state, + dispatch, + group, + viewid, + conversation, +) => { const { tr } = state; tr.step( new AddMarkStep( @@ -169,15 +187,21 @@ const createCommentOnSingleFootnote = (state, dispatch, group, viewid) => { state.config.schema.marks.comment.create({ id: uuidv4(), group, - conversation: [], + conversation, viewid, }), ), - ); + ).setMeta('forceUpdate', true); dispatch(tr); }; -const createCommentOnFootnote = (state, dispatch, group, viewid) => { +const createCommentOnFootnote = ( + state, + dispatch, + group, + viewid, + conversation, +) => { const { selection: { $from }, selection, @@ -231,11 +255,11 @@ const createCommentOnFootnote = (state, dispatch, group, viewid) => { state.config.schema.marks.comment.create({ id, group, - conversation: [], + conversation, viewid, }), ), - ); + ).setMeta('forceUpdate', true); }); dispatch(tr); diff --git a/wax-prosemirror-services/src/AiService/components/AskAIOverlay.js b/wax-prosemirror-services/src/AiService/components/AskAIOverlay.js index c6008d9d64c41a735843bd500a9a26cb3059f45c..b1360269fa8ac3e705c95246678b0d06aa7c6400 100644 --- a/wax-prosemirror-services/src/AiService/components/AskAIOverlay.js +++ b/wax-prosemirror-services/src/AiService/components/AskAIOverlay.js @@ -103,7 +103,10 @@ const SubmitButton = styled.button` const AskAIOverlay = ({ setPosition, position, config }) => { const { t, i18n } = useTranslation(); - const { activeView, options } = useContext(WaxContext); + const { + pmViews: { main }, + options, + } = useContext(WaxContext); const [result, setResult] = useState(''); const [isSubmitted, setIsSubmitted] = useState(false); const [isLoading, setIsLoading] = useState(false); @@ -111,11 +114,11 @@ const AskAIOverlay = ({ setPosition, position, config }) => { const inputRef = useRef(null); useLayoutEffect(() => { - const WaxSurface = activeView.dom.getBoundingClientRect(); - const { selection } = activeView.state; + const WaxSurface = main.dom.getBoundingClientRect(); + const { selection } = main.state; const { to } = selection; - // const start = activeView.coordsAtPos(from); - const end = activeView.coordsAtPos(to - 1); + // const start = main.coordsAtPos(from); + const end = main.coordsAtPos(to - 1); const overLayComponent = document.getElementById('ai-overlay'); if (!overLayComponent) return; const overLayComponentCoords = overLayComponent.getBoundingClientRect(); @@ -145,7 +148,7 @@ const AskAIOverlay = ({ setPosition, position, config }) => { }; const handleInsertTextBelow = () => { - replaceSelectedText(activeView, result); + replaceSelectedText(main, result); }; const handleSubmit = async () => { @@ -158,8 +161,8 @@ const AskAIOverlay = ({ setPosition, position, config }) => { setIsLoading(true); // Get the highlighted text from the editor - const { from, to } = activeView.state.selection; - const highlightedText = activeView.state.doc.textBetween(from, to); + const { from, to } = main.state.selection; + const highlightedText = main.state.doc.textBetween(from, to); // Combine the user's input and the highlighted text const combinedInput = `${inputValue}\n\nHighlighted Text: ${highlightedText}`; @@ -177,7 +180,7 @@ const AskAIOverlay = ({ setPosition, position, config }) => { }; const handleReplaceText = () => { - replaceSelectedText(activeView, result, true); + replaceSelectedText(main, result, true); }; const discardResults = () => { diff --git a/wax-prosemirror-services/src/CommentsService/components/ConnectedComment.js b/wax-prosemirror-services/src/CommentsService/components/ConnectedComment.js index bc7501c2a230415530a9d6476c654d7f293f1f11..2c7310e6fe2ecef635241968c755466256ffc31d 100644 --- a/wax-prosemirror-services/src/CommentsService/components/ConnectedComment.js +++ b/wax-prosemirror-services/src/CommentsService/components/ConnectedComment.js @@ -4,7 +4,7 @@ import React, { useContext, useMemo, useState, useEffect } from 'react'; import { TextSelection } from 'prosemirror-state'; import { last, maxBy } from 'lodash'; import styled from 'styled-components'; -import { WaxContext, DocumentHelpers } from 'wax-prosemirror-core'; +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'; @@ -33,6 +33,7 @@ export default ({ comment, top, commentId, recalculateTops }) => { }, app, activeView, + activeViewId, } = context; const [isActive, setIsActive] = useState(false); @@ -48,7 +49,6 @@ export default ({ comment, top, commentId, recalculateTops }) => { comment, ); } - const commentMark = state.schema.marks.comment; const styles = { @@ -93,22 +93,34 @@ export default ({ comment, top, commentId, recalculateTops }) => { ), ); - 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') { + 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, + ); + } activeView.focus(); recalculateTops(); }; diff --git a/wax-prosemirror-services/src/CommentsService/components/ui/comments/CommentReply.js b/wax-prosemirror-services/src/CommentsService/components/ui/comments/CommentReply.js index dc7cd2ca9a955eb22acf39537117daac5b49447f..2b6489e3845fa643d47b7c2daf864c656d231f37 100644 --- a/wax-prosemirror-services/src/CommentsService/components/ui/comments/CommentReply.js +++ b/wax-prosemirror-services/src/CommentsService/components/ui/comments/CommentReply.js @@ -97,7 +97,7 @@ const CommentReply = props => { useEffect(() => { setTimeout(() => { if (commentTitle.current && isNewComment) commentTitle.current.focus(); - if (commentInput.current && !isNewComment) commentInput.current.focus(); + if (!commentTitle.current && isNewComment) commentInput.current.focus(); }); }, []);