@atlaskit/editor-plugin-selection
Version:
Selection plugin for @atlaskit/editor-core
204 lines (194 loc) • 7.97 kB
JavaScript
import { CAPTION_PLACEHOLDER_ID } from '@atlaskit/editor-common/media-single/constants';
import { Side } from '@atlaskit/editor-common/selection';
import { GapCursorSelection } from '@atlaskit/editor-common/selection/gap-cursor/selection';
import { TableSharedCssClassName } from '@atlaskit/editor-common/styles';
import { UnsupportedSharedCssClassName } from '@atlaskit/editor-common/styles/unsupported-content';
import { editorExperiment } from '@atlaskit/tmp-editor-statsig/editor-experiment';
export var isLeftCursor = function isLeftCursor(side) {
return side === Side.LEFT;
};
export function getMediaNearPos(doc, $pos, schema) {
var dir = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : -1;
var $currentPos = $pos;
var currentNode = null;
var _schema$nodes = schema.nodes,
mediaSingle = _schema$nodes.mediaSingle,
media = _schema$nodes.media,
mediaGroup = _schema$nodes.mediaGroup;
do {
$currentPos = doc.resolve(dir === -1 ? $currentPos.before() : $currentPos.after());
if (!$currentPos) {
return null;
}
currentNode = (dir === -1 ? $currentPos.nodeBefore : $currentPos.nodeAfter) || $currentPos.parent;
if (!currentNode || currentNode.type === schema.nodes.doc) {
return null;
}
if (currentNode.type === mediaSingle || currentNode.type === media || currentNode.type === mediaGroup) {
return currentNode;
}
} while ($currentPos.depth > 0);
return null;
}
export var isTextBlockNearPos = function isTextBlockNearPos(doc, schema, $pos, dir) {
var $currentPos = $pos;
var currentNode = dir === -1 ? $currentPos.nodeBefore : $currentPos.nodeAfter;
// If next node is a text or a text block bail out early.
if (currentNode && (currentNode.isTextblock || currentNode.isText)) {
return true;
}
while ($currentPos.depth > 0) {
$currentPos = doc.resolve(dir === -1 ? $currentPos.before() : $currentPos.after());
if (!$currentPos) {
return false;
}
currentNode = (dir === -1 ? $currentPos.nodeBefore : $currentPos.nodeAfter) || $currentPos.parent;
if (!currentNode || currentNode.type === schema.nodes.doc) {
return false;
}
if (currentNode.isTextblock) {
return true;
}
}
var childNode = currentNode;
while (childNode && childNode.firstChild) {
childNode = childNode.firstChild;
if (childNode && (childNode.isTextblock || childNode.isText)) {
return true;
}
}
return false;
};
export function getLayoutModeFromTargetNode(node) {
var layout;
if (node.attrs.layout) {
layout = node.attrs.layout;
}
if (node.marks && node.marks.length) {
layout = (node.marks.find(function (mark) {
return mark.type.name === 'breakout';
}) || {
attrs: {
mode: ''
}
}).attrs.mode;
}
if (node.type.name === 'table' && node.attrs.width) {
layout = 'fixed-width';
}
if (['wide', 'full-width', 'fixed-width'].indexOf(layout) === -1) {
return '';
}
return layout;
}
/**
* Whether `node`, starting at `start`, is an atom whose DOM contains the click target.
*/
var isClickInsideAtom = function isClickInsideAtom(elem, node, start, view) {
// ProseMirror counts text as an atom, but a text node can't contain an element.
if (!(node !== null && node !== void 0 && node.isAtom) || node.isText) {
return false;
}
var dom = view.nodeDOM(start);
return dom instanceof Node && dom.contains(elem);
};
/**
* Rule 1: no gap cursor can be placed for a click at `nodePos`.
*
* Skips clicks on text and inline atoms (mentions, emoji, status, dates, inline cards), where
* `nodePos` is inside a textblock. Inside a `mediaSingle` the handler places the gap cursor beside
* it instead, so clicks beside nested images aren't skipped.
*/
var cannotPlaceGapCursorAt = function cannotPlaceGapCursorAt($pos) {
return $pos.parent.type.name !== 'mediaSingle' && !GapCursorSelection.valid($pos);
};
/**
* Rule 2: the click target is inside the DOM of the atom before or after `nodePos`.
*
* Skips clicks on block atoms (images, block and embed cards, extensions, dividers), which the
* handler already ignored via `posAtCoords.inside`. Clicks beside them, outside their DOM, aren't
* skipped.
*/
var isClickOnAdjacentAtom = function isClickOnAdjacentAtom(elem, $pos, view) {
var _nodeBefore$nodeSize;
var nodeAfter = $pos.nodeAfter,
nodeBefore = $pos.nodeBefore,
pos = $pos.pos;
return isClickInsideAtom(elem, nodeAfter, pos, view) || isClickInsideAtom(elem, nodeBefore, pos - ((_nodeBefore$nodeSize = nodeBefore === null || nodeBefore === void 0 ? void 0 : nodeBefore.nodeSize) !== null && _nodeBefore$nodeSize !== void 0 ? _nodeBefore$nodeSize : 0), view);
};
/**
* Whether the gap cursor click handler would return false whatever `posAtCoords` returns, so the
* layout read can be skipped.
*/
var isClickWithoutGapCursor = function isClickWithoutGapCursor(elem, _ref) {
var view = _ref.view,
nodePos = _ref.nodePos;
var doc = view.state.doc;
if (nodePos < 0 || nodePos > doc.content.size) {
return false;
}
var $pos = doc.resolve(nodePos);
return cannotPlaceGapCursorAt($pos) || isClickOnAdjacentAtom(elem, $pos, view);
};
/**
* Whether a click should be left to other handlers instead of placing a gap cursor.
*
* Pass `clickContext` to also ignore clicks where no gap cursor can be placed: clicks on text or
* inline content, and clicks on an atom node such as media, cards or extensions.
*/
export var isIgnoredClick = function isIgnoredClick(elem, clickContext) {
if ((elem === null || elem === void 0 ? void 0 : elem.nodeName) === 'BUTTON' || elem !== null && elem !== void 0 && elem.closest('button')) {
return true;
}
// check if we're clicking an image caption placeholder
if (elem !== null && elem !== void 0 && elem.closest("[data-id=\"".concat(CAPTION_PLACEHOLDER_ID, "\"]"))) {
return true;
}
// check if no gap cursor can be placed for this click (text, inline content or an atom node).
// This runs before the table check so clicks inside tables are covered too.
if (elem && clickContext && isClickWithoutGapCursor(elem, clickContext)) {
return true;
}
// check if target node has a parent table node
var tableWrap;
var node = elem;
while (node) {
if (node.className && (node.getAttribute('class') || '').indexOf(TableSharedCssClassName.TABLE_CONTAINER) > -1) {
tableWrap = node;
break;
}
// Ignored via go/ees005
// eslint-disable-next-line @atlaskit/editor/no-as-casting
node = node.parentNode;
}
if (tableWrap) {
var rowControls = tableWrap.querySelector(".".concat(TableSharedCssClassName.TABLE_ROW_CONTROLS_WRAPPER));
var isColumnControlsDecoration = elem && elem.classList && elem.classList.contains(TableSharedCssClassName.TABLE_COLUMN_CONTROLS_DECORATIONS);
return rowControls && rowControls.contains(elem) || isColumnControlsDecoration;
}
// Check if unsupported node selection
// (without this, selection requires double clicking in FF due to posAtCoords differences)
if (elem !== null && elem !== void 0 && elem.closest(".".concat(UnsupportedSharedCssClassName.BLOCK_CONTAINER))) {
return true;
}
return false;
};
/*
Calculates custom style for breakout mode
Mainly to handle table width to include the numbered column width as well
*/
export var getComputedStyleForLayoutMode = function getComputedStyleForLayoutMode(dom, node, style) {
if (node && node.type.name === 'table') {
var tableContainer = dom.querySelector('.pm-table-container');
if (tableContainer) {
return window.getComputedStyle(tableContainer);
}
}
if (editorExperiment('advanced_layouts', true) && node && node.type.name === 'layoutSection') {
var layoutSectionContainer = dom.querySelector('.layout-section-container');
if (layoutSectionContainer) {
return window.getComputedStyle(layoutSectionContainer);
}
}
return style;
};