@atlaskit/editor-plugin-block-controls
Version:
Block controls plugin for @atlaskit/editor-core
86 lines (82 loc) • 3.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isStepText = exports.isStepDelete = exports.getTrMetadata = void 0;
var _transform = require("@atlaskit/editor-prosemirror/transform");
/**
* Checks if step adds inline char
* @param s
* @returns True if step adds inline char
*/
var isStepText = exports.isStepText = function isStepText(s) {
var content = s.slice.content.firstChild;
return s.from === s.to && s.slice.content.childCount === 1 && !!content && !!content.text;
};
/**
* Checks if step is an inline delete/backspace (replace range from -> from + 1 with empty content)
* @param s
* @returns True if delete/backspace
*/
var isStepDelete = exports.isStepDelete = function isStepDelete(s) {
var content = s.slice.content;
return s.to === s.from + 1 && content.size === 0;
};
/**
* Check if content being replaced is replaced with another of the same size
* This can occur on empty docs and 'backspace' is pressed, the paragraph node is re-drawn
* @param s
* @returns True if content being replaced is replaced with another of the same size
*/
var isStepContentReplacedWithAnotherOfSameSize = function isStepContentReplacedWithAnotherOfSameSize(s) {
if (s instanceof _transform.ReplaceAroundStep) {
var replacedContentSize = s.to - s.from;
return s.slice.content.size === replacedContentSize;
}
return false;
};
/**
* Get metadata from the transaction.
* @param tr
* @returns Min 'from', max 'to' (from + slice size, or mapped 'to', whichever is larger). If no steps, returns pos range of entire doc.
* Number of ReplaceStep and ReplaceAroundStep steps 'numReplaceSteps'.
* 'isAllText' if all steps are represent adding inline text or a backspace/delete or no-op
*/
var getTrMetadata = exports.getTrMetadata = function getTrMetadata(tr, flags) {
var from;
var to;
var numReplaceSteps = 0;
var isAllText = true;
var isReplacedWithSameSize = false;
tr.steps.forEach(function (s) {
if (s instanceof _transform.ReplaceStep || s instanceof _transform.ReplaceAroundStep) {
if (s instanceof _transform.ReplaceAroundStep || s instanceof _transform.ReplaceStep && !isStepText(s) && !isStepDelete(s)) {
isAllText = false;
}
// Fixes drag handle and quick insert button overlap issues
if (flags.toolbarFlagsEnabled) {
// platform_editor_controls note: fixes drag handle and quick insert button overlap issues
isReplacedWithSameSize = isStepContentReplacedWithAnotherOfSameSize(s);
}
var mappedTo = tr.mapping.map(s.to);
var $to = s.from + s.slice.size;
$to = $to > mappedTo ? $to : mappedTo;
from = from === undefined || from > s.from ? s.from : from;
to = to === undefined || to < $to ? $to : to;
numReplaceSteps++;
}
});
if (from === undefined) {
from = 0;
}
if (to === undefined || to > tr.doc.nodeSize - 2) {
to = tr.doc.nodeSize - 2;
}
return {
from: from,
to: to,
numReplaceSteps: numReplaceSteps,
isAllText: isAllText,
isReplacedWithSameSize: isReplacedWithSameSize
};
};