Skip to content
Snippets Groups Projects
Commit b8c3defd authored by Christos's avatar Christos
Browse files

Merge branch 'fix-comments' into 'master'

Fix comments

See merge request !521
parents 8d401cad a6e096e7
No related branches found
No related tags found
1 merge request!521Fix comments
Pipeline #56131 passed with stages
in 2 minutes and 53 seconds
......@@ -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);
......
......@@ -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 = () => {
......
......@@ -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();
};
......
......@@ -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();
});
}, []);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment