bug: fix the bug selection can not begin from the right to the left

This commit is contained in:
Jocs 2017-12-18 10:58:05 +08:00
parent d5a715cc3a
commit d0a61241c8
3 changed files with 41 additions and 11 deletions

View File

@ -169,6 +169,7 @@ const updateCtrl = ContentState => {
end.offset !== oldEnd.offset end.offset !== oldEnd.offset
) { ) {
this.cursor = { start, end } this.cursor = { start, end }
this.render()
} }
return return
} }

View File

@ -11,7 +11,8 @@ import {
findPreviousSibling, findPreviousSibling,
findNearestParagraph, findNearestParagraph,
getClosestBlockContainer, getClosestBlockContainer,
getCursorPositionWithinMarkedText getCursorPositionWithinMarkedText,
compareParagraphsOrder
} from './utils/domManipulate' } from './utils/domManipulate'
const filterOnlyParentElements = node => { 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. * @param {boolean} moveCursorToStart A boolean representing whether or not to set the caret to the beginning of the prior selection.
*/ */
clearSelection (moveCursorToStart) { clearSelection (moveCursorToStart) {
const { rangeCount } = this.doc.getSelection()
if (!rangeCount) return
if (moveCursorToStart) { if (moveCursorToStart) {
this.doc.getSelection().collapseToStart() this.doc.getSelection().collapseToStart()
} else { } else {
@ -747,8 +750,8 @@ class Selection {
getCursorRange () { getCursorRange () {
const { anchorNode, anchorOffset, focusNode, focusOffset } = this.doc.getSelection() const { anchorNode, anchorOffset, focusNode, focusOffset } = this.doc.getSelection()
const startParagraph = findNearestParagraph(anchorNode) let startParagraph = findNearestParagraph(anchorNode)
const endParagraph = findNearestParagraph(focusNode) let endParagraph = findNearestParagraph(focusNode)
const getOffsetOfParagraph = (node, paragraph) => { const getOffsetOfParagraph = (node, paragraph) => {
let offset = 0 let offset = 0
@ -765,7 +768,18 @@ class Selection {
: offset + getOffsetOfParagraph(node.parentNode, paragraph) : offset + getOffsetOfParagraph(node.parentNode, paragraph)
} }
if (startParagraph === endParagraph) {
const key = startParagraph.id
const offset1 = getOffsetOfParagraph(anchorNode, startParagraph) + anchorOffset
const offset2 = getOffsetOfParagraph(focusNode, endParagraph) + focusOffset
return { return {
start: { key, offset: Math.min(offset1, offset2) },
end: { key, offset: Math.max(offset1, offset2) }
}
} else {
const order = compareParagraphsOrder(startParagraph, endParagraph)
const rawCursor = {
start: { start: {
key: startParagraph.id, key: startParagraph.id,
offset: getOffsetOfParagraph(anchorNode, startParagraph) + anchorOffset offset: getOffsetOfParagraph(anchorNode, startParagraph) + anchorOffset
@ -775,6 +789,12 @@ class Selection {
offset: getOffsetOfParagraph(focusNode, endParagraph) + focusOffset offset: getOffsetOfParagraph(focusNode, endParagraph) + focusOffset
} }
} }
if (order) {
return rawCursor
} else {
return { start: rawCursor.end, end: rawCursor.start }
}
}
} }
getSelectionEnd () { getSelectionEnd () {

View File

@ -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) => { export const getCursorPositionWithinMarkedText = (markedText, cursorOffset) => {
const chunks = [] const chunks = []
let match let match