UNPKG

@atlaskit/editor-common

Version:

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

97 lines (90 loc) 3.18 kB
import _typeof from "@babel/runtime/helpers/typeof"; import { akEditorDefaultLayoutWidth, akEditorFullWidthLayoutWidth, akEditorGutterPaddingDynamic } from '@atlaskit/editor-shared-styles'; import { getMediaSinglePixelWidth, roundToNearest } from '../media-single'; export var isNumber = function isNumber(x) { return typeof x === 'number' && !isNaN(x) && isFinite(x); }; export var isRange = function isRange(range) { return !!(_typeof(range) === 'object' && range && 'start' in range && 'end' in range); }; export var isVerticalPosition = function isVerticalPosition(pos) { return isNumber(pos.x); }; /** * Returns the type of guideline based on a guideline key and a collection of guidelines */ export var getGuidelineTypeFromKey = function getGuidelineTypeFromKey(keys, guidelines) { if (guidelines.length === 0) { return 'none'; } // Check for default guidelines on key if (keys.some(function (key) { return ['grid_', 'wide_', 'full_width'].some(function (defaultGuideline) { return key.indexOf(defaultGuideline) >= 0; }); })) { return 'default'; } // Check for temporary guidelines if (keys.some(function (key) { return ['media_'].some(function (temoporaryGuideline) { return key.indexOf(temoporaryGuideline) >= 0; }); })) { return 'temporary'; } // Check for relative guidelines if (keys.some(function (key) { return ['relative_'].some(function (relativeGuideline) { return key.indexOf(relativeGuideline) >= 0; }); })) { return 'relative'; } return 'none'; }; /** * Calculates container or full editor width taking in account editor full width layout * width and editor gutter padding. */ export var getContainerWidthOrFullEditorWidth = function getContainerWidthOrFullEditorWidth(containerWidth) { return Math.min(containerWidth - akEditorGutterPaddingDynamic() * 2, akEditorFullWidthLayoutWidth) / 2; }; /** * * @param mediaSingle the mediaSingle node * @param editorWidth default 760, only use default if the mediaSingle is using pixel width * @returns null or dimensions info */ export var getMediaSingleDimensions = function getMediaSingleDimensions(mediaSingle) { var editorWidth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : akEditorDefaultLayoutWidth; if (mediaSingle.type !== mediaSingle.type.schema.nodes.mediaSingle) { return null; } var mediaNode = mediaSingle.firstChild; var _ref = (mediaNode === null || mediaNode === void 0 ? void 0 : mediaNode.attrs) || {}, width = _ref.width, height = _ref.height; // e.g. external image if (!width || !height) { return null; } var ratio = parseFloat((height / width).toFixed(2)); if (!mediaSingle.attrs.width) { return { width: width, height: height, originalWidth: width, originalHeight: height, ratio: ratio }; } var pixelWidth = getMediaSinglePixelWidth(mediaSingle.attrs.width, editorWidth, mediaSingle.attrs.widthType); return { width: roundToNearest(pixelWidth), height: roundToNearest(pixelWidth * ratio), originalWidth: width, originalHeight: height, ratio: ratio }; };