UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

85 lines (84 loc) 3.83 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } /** * This creates a collection of points relative to the center line of each supplied length. For example; a length of * 100 centered to 0 would result in the left position of -50 and a right position of +50. * * Length values should be unique, as duplicates will be trimmed from the result. * * @example * createGuidelinesFromLengths([100, 200, 200, -200, 201]) => [ * {left: -50, right: 50, length: 100}, * {left: -100, right: 100, length: 200}, * {left: 100, right: -100, length: -200}, * {left: -100.5, right: 100.5, length: 201}, * ] * When length is 0, return {left: 0, right: 0, length: 0} * * @param lengths A colection of length values which will be split into a left & right guides. * @returns A collection of LengthGuide objects which can be used to draw left & right guides */ export var createGuidesFromLengths = function createGuidesFromLengths(lengths) { var hasFullWidthGuide = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; return Array.from(new Set(lengths)).reduce(function (acc, length, index) { var h = length * 0.5; if (length === 0) { return [].concat(_toConsumableArray(acc), [{ left: 0, right: 0, length: length }]); } if (!h || !Number.isFinite(length)) { // Filter out nonsensical values, null, undefined, NaN, empty string return acc; } return [].concat(_toConsumableArray(acc), [_objectSpread({ left: -h, right: h, length: length }, hasFullWidthGuide && index === lengths.length - 1 ? { isFullWidth: true } : {})]); }, []); }; /** * This creates a Guideline configuration generating a collection of guideline pairs from each supplied length value. * Each length value generates a guideline config for both the left and right side of the length. * When length is 0, generate a guideline at position: {x: 0} * */ export var createFixedGuidelinesFromLengths = function createFixedGuidelinesFromLengths(lengths) { var key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'guide'; var hasFullWidthGuide = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; return createGuidesFromLengths(lengths, hasFullWidthGuide).reduce(function (acc, _ref) { var left = _ref.left, right = _ref.right, length = _ref.length, isFullWidth = _ref.isFullWidth; if (length === 0) { return [].concat(_toConsumableArray(acc), [{ key: "".concat(key, "-").concat(length, "-centre"), position: { x: left } }]); } else { return [].concat(_toConsumableArray(acc), [{ key: "".concat(key, "-").concat(length, "-left"), position: { x: left } }, _objectSpread({ key: "".concat(key, "-").concat(length, "-right"), position: { x: right } }, isFullWidth ? { isFullWidth: true } : {})]); } }, []); };