UNPKG

@atlaskit/editor-common

Version:

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

76 lines (69 loc) 2.61 kB
import { akEditorDefaultLayoutWidth, akEditorFullWidthLayoutWidth, akEditorGutterPadding } from '@atlaskit/editor-shared-styles'; import { getMediaSinglePixelWidth, roundToNearest } from '../media-single'; export const isNumber = x => typeof x === 'number' && !isNaN(x) && isFinite(x); export const isRange = range => !!(typeof range === 'object' && range && 'start' in range && 'end' in range); export const isVerticalPosition = pos => isNumber(pos.x); /** * Returns the type of guideline based on a guideline key and a collection of guidelines */ export const getGuidelineTypeFromKey = (keys, guidelines) => { if (guidelines.length === 0) { return 'none'; } // Check for default guidelines on key if (keys.some(key => ['grid_', 'wide_', 'full_width'].some(defaultGuideline => key.indexOf(defaultGuideline) >= 0))) { return 'default'; } // Check for temporary guidelines if (keys.some(key => ['media_'].some(temoporaryGuideline => key.indexOf(temoporaryGuideline) >= 0))) { return 'temporary'; } // Check for relative guidelines if (keys.some(key => ['relative_'].some(relativeGuideline => 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 const getContainerWidthOrFullEditorWidth = containerWidth => Math.min(containerWidth - akEditorGutterPadding * 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 const getMediaSingleDimensions = (mediaSingle, editorWidth = akEditorDefaultLayoutWidth) => { if (mediaSingle.type !== mediaSingle.type.schema.nodes.mediaSingle) { return null; } const mediaNode = mediaSingle.firstChild; const { width, height } = (mediaNode === null || mediaNode === void 0 ? void 0 : mediaNode.attrs) || {}; // e.g. external image if (!width || !height) { return null; } const ratio = parseFloat((height / width).toFixed(2)); if (!mediaSingle.attrs.width) { return { width, height, originalWidth: width, originalHeight: height, ratio }; } const pixelWidth = getMediaSinglePixelWidth(mediaSingle.attrs.width, editorWidth, mediaSingle.attrs.widthType); return { width: roundToNearest(pixelWidth), height: roundToNearest(pixelWidth * ratio), originalWidth: width, originalHeight: height, ratio }; };