Skip to content
Snippets Groups Projects
Commit 7d9c4b84 authored by chris's avatar chris
Browse files

correct carret position on active

parent 21836e89
No related branches found
No related tags found
1 merge request!129React peer dependency
...@@ -22,7 +22,7 @@ export default ({ comment, activeView, user }) => { ...@@ -22,7 +22,7 @@ export default ({ comment, activeView, user }) => {
const handleKeyDown = event => { const handleKeyDown = event => {
if (event.key === 'Enter' || event.which === 13) { if (event.key === 'Enter' || event.which === 13) {
saveComment(); saveComment(event);
} }
}; };
...@@ -74,7 +74,7 @@ export default ({ comment, activeView, user }) => { ...@@ -74,7 +74,7 @@ export default ({ comment, activeView, user }) => {
}; };
const resolveComment = event => { const resolveComment = event => {
event.stopPropagation(); if (event) event.stopPropagation();
let maxPos = comment.pos; let maxPos = comment.pos;
let minPos = comment.pos; let minPos = comment.pos;
......
import React, { useState, useEffect, useContext } from 'react'; import React, { useState, useEffect, useContext } from 'react';
import { TextSelection } from 'prosemirror-state'; import { TextSelection } from 'prosemirror-state';
import { last } from 'lodash'; import { last, maxBy } from 'lodash';
import { Transition } from 'react-transition-group'; import { Transition } from 'react-transition-group';
import styled from 'styled-components'; import styled from 'styled-components';
...@@ -59,18 +59,19 @@ export default ({ comment, top, dataBox }) => { ...@@ -59,18 +59,19 @@ export default ({ comment, top, dataBox }) => {
const setCommentActive = () => { const setCommentActive = () => {
const viewId = comment.attrs.viewid; const viewId = comment.attrs.viewid;
let maxPos = comment.pos;
const allCommentsWithSameId = DocumentHelpers.findAllMarksWithSameId(view[viewId].state, comment);
allCommentsWithSameId.forEach(singleComment => { if (active) {
const markPosition = DocumentHelpers.findMarkPosition(view[viewId], singleComment.pos, 'comment'); view[viewId].focus();
if (markPosition.to > maxPos) maxPos = markPosition.to; return false;
}); }
const allCommentsWithSameId = DocumentHelpers.findAllMarksWithSameId(view[viewId].state, comment);
if (!active && allCommentsWithSameId.length > 1) maxPos += last(allCommentsWithSameId).node.nodeSize; const maxPos = maxBy(allCommentsWithSameId, 'pos');
maxPos.pos += last(allCommentsWithSameId).node.nodeSize;
view[viewId].dispatch( view[viewId].dispatch(
view[viewId].state.tr.setSelection(new TextSelection(view[viewId].state.tr.doc.resolve(maxPos, maxPos))), view[viewId].state.tr.setSelection(new TextSelection(view[viewId].state.tr.doc.resolve(maxPos.pos, maxPos.pos))),
); );
view[viewId].focus(); view[viewId].focus();
......
...@@ -96,7 +96,6 @@ const findAllMarksWithSameId = (state, mark) => { ...@@ -96,7 +96,6 @@ const findAllMarksWithSameId = (state, mark) => {
return allMarksWithSameId; return allMarksWithSameId;
}; };
// TODO Also find fragmented marks
const findMarkPosition = (activeView, initialPos, markType) => { const findMarkPosition = (activeView, initialPos, markType) => {
const $pos = activeView.state.tr.doc.resolve(initialPos); const $pos = activeView.state.tr.doc.resolve(initialPos);
const { parent } = $pos; const { parent } = $pos;
......
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