Skip to content

Commit

Permalink
Simplify selectionCollapsed, move to dom.js
Browse files Browse the repository at this point in the history
FIX: Fixes reading of selection in Chrome in a shadow DOM.
  • Loading branch information
marijnh committed Mar 20, 2017
1 parent 6e3d72c commit 1d6df3e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
11 changes: 11 additions & 0 deletions src/dom.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const browser = require("./browser")

const domIndex = exports.domIndex = function(node) {
for (var index = 0;; index++) {
node = node.previousSibling
Expand Down Expand Up @@ -50,3 +52,12 @@ function hasBlockDesc(dom) {
let desc = dom.pmViewDesc
return desc && desc.node && desc.node.isBlock
}

// Work around Chrome issue https://bugs.chromium.org/p/chromium/issues/detail?id=447523
// (isCollapsed inappropriately returns true in shadow dom)
exports.selectionCollapsed = function(domSel) {
let collapsed = domSel.isCollapsed
if (collapsed && browser.chrome && domSel.rangeCount && !domSel.getRangeAt(0).collapsed)
collapsed = false
return collapsed
}
3 changes: 2 additions & 1 deletion src/domchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const {Selection} = require("prosemirror-state")
const {Mapping} = require("prosemirror-transform")

const {TrackMappings} = require("./trackmappings")
const {selectionBetween, selectionCollapsed} = require("./selection")
const {selectionBetween} = require("./selection")
const {selectionCollapsed} = require("./dom")

class DOMChange {
constructor(view, composing) {
Expand Down
16 changes: 1 addition & 15 deletions src/selection.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {TextSelection, NodeSelection} = require("prosemirror-state")

const browser = require("./browser")
const {selectionCollapsed} = require("./dom")

// Track the state of the current editor selection. Keeps the editor
// selection in sync with the DOM selection by polling for changes,
Expand Down Expand Up @@ -220,21 +221,6 @@ function temporarilyEditable(view, pos) {
}
}

function selectionCollapsed(selection) {
if (!selection.isCollapsed) {
return false
}

for (let i = 0, k = selection.rangeCount; i < k; i++) {
if (!selection.getRangeAt(i).collapsed) {
return false
}
}

return true
}
exports.selectionCollapsed = selectionCollapsed

function removeClassOnSelectionChange(view) {
document.removeEventListener("selectionchange", view.hideSelectionGuard)
let domSel = view.root.getSelection()
Expand Down

0 comments on commit 1d6df3e

Please sign in to comment.