@atlaskit/editor-plugin-quick-insert
Version:
Quick insert plugin for @atlaskit/editor-core
110 lines (106 loc) • 5.27 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getArrowKeyTargetIndex = exports.RETURN_TO_SEARCH = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
var RETURN_TO_SEARCH = exports.RETURN_TO_SEARCH = -1;
// Options whose tops are within a pixel share a row, absorbing sub-pixel layout differences.
var ROW_TOLERANCE = 1;
var getCenter = function getCenter(_ref) {
var left = _ref.left,
width = _ref.width;
return left + width / 2;
};
var findNearestInRow = function findNearestInRow(options, rowTop, center) {
var _nearest;
var nearest;
options.forEach(function (option, index) {
if (option.isDisabled || Math.abs(option.top - rowTop) > ROW_TOLERANCE) {
return;
}
var distance = Math.abs(getCenter(option) - center);
if (!nearest || distance < nearest.distance) {
nearest = {
distance: distance,
index: index
};
}
});
return (_nearest = nearest) === null || _nearest === void 0 ? void 0 : _nearest.index;
};
/**
* Finds the option an arrow key moves to in the rendered result grids. Rows can have different
* column counts, so vertical moves pick the nearest enabled option in the next row that has one.
* Returns `RETURN_TO_SEARCH` when moving before the first option, or `undefined` to stay put.
*/
var getArrowKeyTargetIndex = exports.getArrowKeyTargetIndex = function getArrowKeyTargetIndex(options, currentIndex, key) {
if (currentIndex < 0) {
// Only ArrowDown enters the results from search; the other keys keep their native caret moves.
if (key !== 'ArrowDown') {
return undefined;
}
var firstEnabled = options.findIndex(function (_ref2) {
var isDisabled = _ref2.isDisabled;
return !isDisabled;
});
return firstEnabled < 0 ? undefined : firstEnabled;
}
if (key === 'ArrowLeft' || key === 'ArrowRight') {
var step = key === 'ArrowRight' ? 1 : -1;
for (var index = currentIndex + step; index >= 0 && index < options.length; index += step) {
if (!options[index].isDisabled) {
return index;
}
}
return key === 'ArrowLeft' ? RETURN_TO_SEARCH : undefined;
}
// Rows can hold different numbers of tiles (2 wide tiles above 3 regular ones in Recommended), so
// the row below or above, and the tile nearest the current one in it, come from screen positions.
var positions = options.map(function (_ref3) {
var getRect = _ref3.getRect,
isDisabled = _ref3.isDisabled;
var _getRect = getRect(),
left = _getRect.left,
top = _getRect.top,
width = _getRect.width;
return {
isDisabled: isDisabled,
left: left,
top: top,
width: width
};
});
var current = positions[currentIndex];
var isDown = key === 'ArrowDown';
// The rows past the current one in the direction of travel, each identified by its top edge,
// nearest first so the first row with an enabled tile wins.
var rowsAhead = (0, _toConsumableArray2.default)(new Set(positions.map(function (_ref4) {
var top = _ref4.top;
return top;
}).filter(function (top) {
return isDown ? top > current.top + ROW_TOLERANCE : top < current.top - ROW_TOLERANCE;
}))).sort(function (first, second) {
return isDown ? first - second : second - first;
});
var _iterator = _createForOfIteratorHelper(rowsAhead),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var rowTop = _step.value;
var target = findNearestInRow(positions, rowTop, getCenter(current));
if (target !== undefined) {
return target;
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return isDown ? undefined : RETURN_TO_SEARCH;
};