X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/bb1254117933390e69cce22169400f8206393e9d..b1f5495a7f46e83c39a856417e4c2524c9425438:/resources/js/editor/util.js diff --git a/resources/js/editor/util.js b/resources/js/editor/util.js index e1746a725..ad077b923 100644 --- a/resources/js/editor/util.js +++ b/resources/js/editor/util.js @@ -45,6 +45,25 @@ export function nullifyEmptyValues(object) { return clean; } +/** + * @param {PmEditorState} state + * @param {PmSelection} selection + * @param {PmMarkType} markType + * @return {{from: Number, to: Number}} + */ +export function expandSelectionToMark(state, selection, markType) { + let {from, to} = selection; + const noRange = (from === to); + if (noRange) { + const markRange = markRangeAtPosition(state, markType, from); + if (markRange.from !== -1) { + from = markRange.from; + to = markRange.to; + } + } + return {from, to}; +} + /** * @param {PmEditorState} state * @param {PmMarkType} markType @@ -54,7 +73,7 @@ export function nullifyEmptyValues(object) { export function markRangeAtPosition(state, markType, pos) { const $pos = state.doc.resolve(pos); - const { parent, parentOffset } = $pos; + const {parent, parentOffset} = $pos; const start = parent.childAfter(parentOffset); if (!start.node) return {from: -1, to: -1}; @@ -73,7 +92,7 @@ export function markRangeAtPosition(state, markType, pos) { endPos += parent.child(endIndex).nodeSize; endIndex += 1; } - return { from: startPos, to: endPos }; + return {from: startPos, to: endPos}; } /**