From 58800f87ed504e1082770434e98c16038b30408f Mon Sep 17 00:00:00 2001 From: chris <kokosias@yahoo.gr> Date: Tue, 15 Sep 2020 15:54:03 +0300 Subject: [PATCH] use from utilities --- .../src/trackChanges/FindSelectedChanges.js | 12 +++++--- .../src/trackChanges/helpers.js | 30 ++++--------------- .../src/document/DocumentHelpers.js | 2 ++ 3 files changed, 15 insertions(+), 29 deletions(-) diff --git a/wax-prosemirror-plugins/src/trackChanges/FindSelectedChanges.js b/wax-prosemirror-plugins/src/trackChanges/FindSelectedChanges.js index 893d9eb78..14f24e872 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 56d2e03cb..029409702 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 95b238ad2..3815010c8 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; -- GitLab