diff --git a/wax-prosemirror-plugins/src/trackChanges/FindSelectedChanges.js b/wax-prosemirror-plugins/src/trackChanges/FindSelectedChanges.js index 893d9eb78d3fcc82a7acdf992fee33c36a5f1088..14f24e8723b208abd40416d70f214c6fe1c68864 100644 --- a/wax-prosemirror-plugins/src/trackChanges/FindSelectedChanges.js +++ b/wax-prosemirror-plugins/src/trackChanges/FindSelectedChanges.js @@ -1,4 +1,4 @@ -import { getFromToMark } from './helpers'; +import { DocumentHelpers } from 'wax-prosemirror-utilities'; const findSelectedChanges = state => { const { selection } = state; @@ -87,19 +87,23 @@ const findSelectedChanges = state => { if (insertionMark) { selectedChanges.insertion = insertionSize ? { from: insertionPos, to: insertionPos + insertionSize } - : getFromToMark(state.doc, insertionPos, insertionMark); + : DocumentHelpers.findMarkPosition(state, insertionPos, 'insertion'); } if (deletionMark) { selectedChanges.deletion = deletionSize ? { from: deletionPos, to: deletionPos + deletionSize } - : getFromToMark(state.doc, deletionPos, deletionMark); + : DocumentHelpers.findMarkPosition(state, deletionPos, 'deletion'); } if (formatChangeMark) { selectedChanges.formatChange = formatChangeSize ? { from: formatChangePos, to: formatChangePos + formatChangeSize } - : getFromToMark(state.doc, formatChangePos, formatChangeMark); + : DocumentHelpers.findMarkPosition( + state, + formatChangePos, + 'format_change', + ); } return selectedChanges; }; diff --git a/wax-prosemirror-plugins/src/trackChanges/helpers.js b/wax-prosemirror-plugins/src/trackChanges/helpers.js index 56d2e03cbc7f0757cd23dd5d115b05e6b32b209d..0294097026a2f96d2e68c0165f67d5b25f244649 100644 --- a/wax-prosemirror-plugins/src/trackChanges/helpers.js +++ b/wax-prosemirror-plugins/src/trackChanges/helpers.js @@ -1,4 +1,5 @@ import { Decoration, DecorationSet } from 'prosemirror-view'; +import { DocumentHelpers } from 'wax-prosemirror-utilities'; import { key, @@ -44,8 +45,11 @@ export function setSelectedChanges(state, type, pos) { if (!mark) { return; } + + DocumentHelpers.findMarkPosition(state, pos, type); + const selectedChange = node.isInline - ? getFromToMark(tr.doc, pos, mark) + ? DocumentHelpers.findMarkPosition(state, pos, type) : { from: pos, to: pos + node.nodeSize }; let decos = DecorationSet.empty; let spec; @@ -78,27 +82,3 @@ export function deactivateAllSelectedChanges(tr) { }; return tr.setMeta(key, pluginState).setMeta('track', true); } - -// From https://discuss.prosemirror.net/t/expanding-the-selection-to-the-active-mark/478/2 with some bugs fixed -export function getFromToMark(doc, pos, mark) { - const $pos = doc.resolve(pos); - const { parent } = $pos; - const start = parent.childAfter($pos.parentOffset); - if (!start.node) { - return null; - } - let startIndex = $pos.index(); - let startPos = $pos.start() + start.offset; - while (startIndex > 0 && mark.isInSet(parent.child(startIndex - 1).marks)) { - startPos -= parent.child(--startIndex).nodeSize; - } - let endIndex = $pos.index() + 1; - let endPos = $pos.start() + start.offset + start.node.nodeSize; - while ( - endIndex < parent.childCount && - mark.isInSet(parent.child(endIndex).marks) - ) { - endPos += parent.child(endIndex++).nodeSize; - } - return { from: startPos, to: endPos }; -} diff --git a/wax-prosemirror-utilities/src/document/DocumentHelpers.js b/wax-prosemirror-utilities/src/document/DocumentHelpers.js index 95b238ad2ec2bda95c70a4469b5fbecc234f5763..3815010c85fd29af77fe2d748892895315e89946 100644 --- a/wax-prosemirror-utilities/src/document/DocumentHelpers.js +++ b/wax-prosemirror-utilities/src/document/DocumentHelpers.js @@ -96,6 +96,8 @@ const findAllMarksWithSameId = (state, mark) => { return allMarksWithSameId; }; +// From https://discuss.prosemirror.net/t/expanding-the-selection-to-the-active-mark/478/2 + const findMarkPosition = (state, initialPos, markType) => { const $pos = state.tr.doc.resolve(initialPos); const { parent } = $pos;