@atlaskit/editor-plugin-selection
Version:
Selection plugin for @atlaskit/editor-core
61 lines (55 loc) • 1.67 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isValidSelection = isValidSelection;
exports.onMouseOut = onMouseOut;
var _state = require("@atlaskit/editor-prosemirror/state");
function isValidSelection(selection) {
if (selection.empty) {
return false;
}
var $anchor = selection.$anchor,
$head = selection.$head;
// Head must be at the start of its parent node's content
if ($head.parentOffset !== 0) {
return false;
}
// Head must be nested (e.g., in a panel > paragraph, not directly in doc)
if ($head.depth <= 1) {
return false;
}
// Head must be in the first top-level block (before depth 1 is 0)
if ($head.before(1) !== 0) {
return false;
}
// Selection must not span within a shared deeper structure
if ($head.sharedDepth($anchor.pos) !== 0) {
return false;
}
// Head must be in the first block at every level
var isFirstBlockInTree = true;
for (var i = $head.depth - 1; i >= 0; i--) {
if ($head.index(i) !== 0) {
isFirstBlockInTree = false;
break;
}
}
if (!isFirstBlockInTree) {
return false;
}
return true;
}
function onMouseOut(view, event) {
// Only trigger during a mouse drag
if (event.buttons === 0) {
return false;
}
if (view.state.selection instanceof _state.TextSelection && isValidSelection(view.state.selection)) {
// Set selection with head at document start (position 0)
var selection = new _state.TextSelection(view.state.doc.resolve(view.state.selection.$anchor.pos), view.state.doc.resolve(0));
view.dispatch(view.state.tr.setSelection(selection));
return true;
}
return false;
}