UNPKG

@atlaskit/editor-common

Version:

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

85 lines (84 loc) 3.32 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.getGuidelineSnaps = exports.findClosestSnap = void 0; var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _memoizeOne = _interopRequireDefault(require("memoize-one")); var _constants = require("./constants"); var _utils = require("./utils"); /** * Returns keys of guidelines that are closest to the image and withthin the snapGap. * If both default and dynamic guidelines present, only returns default guidelines */ var findClosestSnap = exports.findClosestSnap = function findClosestSnap(mediaSingleWidth, snapArray, guidelineSnaps) { var snapGap = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0; var closestGapIndex = snapArray.reduce(function (prev, curr, index) { return Math.abs(curr - mediaSingleWidth) < Math.abs(snapArray[prev] - mediaSingleWidth) ? index : prev; }, 0); var gap = Math.abs(snapArray[closestGapIndex] - mediaSingleWidth); if (gap < snapGap) { var snappingWidth = snapArray[closestGapIndex]; var guidelineKeys = []; // find wich guideline have the matching snap width guidelineSnaps.forEach(function (gs) { if (gs.width === snappingWidth) { guidelineKeys.push(gs.guidelineKey); } }); var defaultGuidelineKeys = guidelineKeys.filter(function (guidelineKey) { return !guidelineKey.startsWith(_constants.MEDIA_DYNAMIC_GUIDELINE_PREFIX); }); return { gap: gap, // only highlight default guidelines // when there are both default and dynamic guidelines to be highlighted keys: defaultGuidelineKeys.length && defaultGuidelineKeys.length < guidelineKeys.length ? defaultGuidelineKeys : guidelineKeys }; } return { gap: gap, keys: [] }; }; var getGuidelineSnaps = exports.getGuidelineSnaps = (0, _memoizeOne.default)(function (guidelines, editorWidth) { var layout = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'center'; var offset = editorWidth / 2; var getPositionX = function getPositionX(position) { return (0, _utils.isVerticalPosition)(position) ? position.x : 0; }; var calcXSnaps = function calcXSnaps(guidelineReference) { var snapsWidth = guidelineReference.filter(function (g) { return g.width > 0; }).map(function (g) { return g.width; }); var uniqueSnapWidth = (0, _toConsumableArray2.default)(new Set(snapsWidth)); return snapsWidth.length ? uniqueSnapWidth : undefined; }; var guidelinesSnapsReference = guidelines.map(function (guideline) { var positionX = getPositionX(guideline.position); if (['align-end', 'wrap-right'].includes(layout)) { return { guidelineKey: guideline.key, width: offset - positionX }; } else if (['align-start', 'wrap-left'].includes(layout)) { return { guidelineKey: guideline.key, width: positionX + offset }; } return { guidelineKey: guideline.key, width: Math.abs(positionX) * 2 }; }); return { guidelineReference: guidelinesSnapsReference, snaps: { x: calcXSnaps(guidelinesSnapsReference) } }; });