Skip to content
Snippets Groups Projects
Commit 58800f87 authored by chris's avatar chris
Browse files

use from utilities

parent d4da5fbe
No related branches found
No related tags found
1 merge request!160add track change plugin through service
import { getFromToMark } from './helpers'; import { DocumentHelpers } from 'wax-prosemirror-utilities';
const findSelectedChanges = state => { const findSelectedChanges = state => {
const { selection } = state; const { selection } = state;
...@@ -87,19 +87,23 @@ const findSelectedChanges = state => { ...@@ -87,19 +87,23 @@ const findSelectedChanges = state => {
if (insertionMark) { if (insertionMark) {
selectedChanges.insertion = insertionSize selectedChanges.insertion = insertionSize
? { from: insertionPos, to: insertionPos + insertionSize } ? { from: insertionPos, to: insertionPos + insertionSize }
: getFromToMark(state.doc, insertionPos, insertionMark); : DocumentHelpers.findMarkPosition(state, insertionPos, 'insertion');
} }
if (deletionMark) { if (deletionMark) {
selectedChanges.deletion = deletionSize selectedChanges.deletion = deletionSize
? { from: deletionPos, to: deletionPos + deletionSize } ? { from: deletionPos, to: deletionPos + deletionSize }
: getFromToMark(state.doc, deletionPos, deletionMark); : DocumentHelpers.findMarkPosition(state, deletionPos, 'deletion');
} }
if (formatChangeMark) { if (formatChangeMark) {
selectedChanges.formatChange = formatChangeSize selectedChanges.formatChange = formatChangeSize
? { from: formatChangePos, to: formatChangePos + formatChangeSize } ? { from: formatChangePos, to: formatChangePos + formatChangeSize }
: getFromToMark(state.doc, formatChangePos, formatChangeMark); : DocumentHelpers.findMarkPosition(
state,
formatChangePos,
'format_change',
);
} }
return selectedChanges; return selectedChanges;
}; };
......
import { Decoration, DecorationSet } from 'prosemirror-view'; import { Decoration, DecorationSet } from 'prosemirror-view';
import { DocumentHelpers } from 'wax-prosemirror-utilities';
import { import {
key, key,
...@@ -44,8 +45,11 @@ export function setSelectedChanges(state, type, pos) { ...@@ -44,8 +45,11 @@ export function setSelectedChanges(state, type, pos) {
if (!mark) { if (!mark) {
return; return;
} }
DocumentHelpers.findMarkPosition(state, pos, type);
const selectedChange = node.isInline const selectedChange = node.isInline
? getFromToMark(tr.doc, pos, mark) ? DocumentHelpers.findMarkPosition(state, pos, type)
: { from: pos, to: pos + node.nodeSize }; : { from: pos, to: pos + node.nodeSize };
let decos = DecorationSet.empty; let decos = DecorationSet.empty;
let spec; let spec;
...@@ -78,27 +82,3 @@ export function deactivateAllSelectedChanges(tr) { ...@@ -78,27 +82,3 @@ export function deactivateAllSelectedChanges(tr) {
}; };
return tr.setMeta(key, pluginState).setMeta('track', true); 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 };
}
...@@ -96,6 +96,8 @@ const findAllMarksWithSameId = (state, mark) => { ...@@ -96,6 +96,8 @@ const findAllMarksWithSameId = (state, mark) => {
return allMarksWithSameId; return allMarksWithSameId;
}; };
// From https://discuss.prosemirror.net/t/expanding-the-selection-to-the-active-mark/478/2
const findMarkPosition = (state, initialPos, markType) => { const findMarkPosition = (state, initialPos, markType) => {
const $pos = state.tr.doc.resolve(initialPos); const $pos = 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