From d0a61241c8484d871851225da403e153c83c9bb4 Mon Sep 17 00:00:00 2001 From: Jocs Date: Mon, 18 Dec 2017 10:58:05 +0800 Subject: [PATCH] bug: fix the bug selection can not begin from the right to the left --- src/editor/contentState/updateCtrl.js | 1 + src/editor/selection.js | 42 ++++++++++++++++++++------- src/editor/utils/domManipulate.js | 9 ++++++ 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/editor/contentState/updateCtrl.js b/src/editor/contentState/updateCtrl.js index d4a8898a..941d558d 100644 --- a/src/editor/contentState/updateCtrl.js +++ b/src/editor/contentState/updateCtrl.js @@ -169,6 +169,7 @@ const updateCtrl = ContentState => { end.offset !== oldEnd.offset ) { this.cursor = { start, end } + this.render() } return } diff --git a/src/editor/selection.js b/src/editor/selection.js index dc630282..5c2ae939 100644 --- a/src/editor/selection.js +++ b/src/editor/selection.js @@ -11,7 +11,8 @@ import { findPreviousSibling, findNearestParagraph, getClosestBlockContainer, - getCursorPositionWithinMarkedText + getCursorPositionWithinMarkedText, + compareParagraphsOrder } from './utils/domManipulate' const filterOnlyParentElements = node => { @@ -668,6 +669,8 @@ class Selection { * @param {boolean} moveCursorToStart A boolean representing whether or not to set the caret to the beginning of the prior selection. */ clearSelection (moveCursorToStart) { + const { rangeCount } = this.doc.getSelection() + if (!rangeCount) return if (moveCursorToStart) { this.doc.getSelection().collapseToStart() } else { @@ -747,8 +750,8 @@ class Selection { getCursorRange () { const { anchorNode, anchorOffset, focusNode, focusOffset } = this.doc.getSelection() - const startParagraph = findNearestParagraph(anchorNode) - const endParagraph = findNearestParagraph(focusNode) + let startParagraph = findNearestParagraph(anchorNode) + let endParagraph = findNearestParagraph(focusNode) const getOffsetOfParagraph = (node, paragraph) => { let offset = 0 @@ -765,14 +768,31 @@ class Selection { : offset + getOffsetOfParagraph(node.parentNode, paragraph) } - return { - start: { - key: startParagraph.id, - offset: getOffsetOfParagraph(anchorNode, startParagraph) + anchorOffset - }, - end: { - key: endParagraph.id, - offset: getOffsetOfParagraph(focusNode, endParagraph) + focusOffset + if (startParagraph === endParagraph) { + const key = startParagraph.id + const offset1 = getOffsetOfParagraph(anchorNode, startParagraph) + anchorOffset + const offset2 = getOffsetOfParagraph(focusNode, endParagraph) + focusOffset + return { + start: { key, offset: Math.min(offset1, offset2) }, + end: { key, offset: Math.max(offset1, offset2) } + } + } else { + const order = compareParagraphsOrder(startParagraph, endParagraph) + + const rawCursor = { + start: { + key: startParagraph.id, + offset: getOffsetOfParagraph(anchorNode, startParagraph) + anchorOffset + }, + end: { + key: endParagraph.id, + offset: getOffsetOfParagraph(focusNode, endParagraph) + focusOffset + } + } + if (order) { + return rawCursor + } else { + return { start: rawCursor.end, end: rawCursor.start } } } } diff --git a/src/editor/utils/domManipulate.js b/src/editor/utils/domManipulate.js index 0e573353..39b860ab 100644 --- a/src/editor/utils/domManipulate.js +++ b/src/editor/utils/domManipulate.js @@ -120,6 +120,15 @@ export const getClosestBlockContainer = node => { }) } +export const compareParagraphsOrder = (paragraph1, paragraph2) => { + const allParagraph = [...document.querySelectorAll(`.${CLASS_OR_ID['AG_PARAGRAPH']}`)] + + const p1Index = allParagraph.indexOf(paragraph1) + const p2Index = allParagraph.indexOf(paragraph2) + + return p1Index < p2Index +} + export const getCursorPositionWithinMarkedText = (markedText, cursorOffset) => { const chunks = [] let match