@atlaskit/editor-plugin-grid
Version:
Grid plugin for @atlaskit/editor-core
218 lines (216 loc) • 8.53 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import React from 'react';
// eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled -- Ignored via go/DSP-18766
import { withTheme } from '@emotion/react';
import classnames from 'classnames';
import { isSSR } from '@atlaskit/editor-common/core-utils';
import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/hooks';
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { PluginKey } from '@atlaskit/editor-prosemirror/state';
import { akEditorBreakoutPadding, akEditorFullPageMaxWidth, breakoutWideScaleRatio } from '@atlaskit/editor-shared-styles';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
export var GRID_SIZE = 12;
var key = new PluginKey('gridPlugin');
var createDisplayGrid = function createDisplayGrid(view) {
return function (props) {
var dispatch = view.dispatch,
state = view.state;
var tr = state.tr.setMeta(key, props);
dispatch(tr);
return true;
};
};
var sides = ['left', 'right'];
var overflowHighlight = function overflowHighlight(highlights, side, start, size) {
if (!highlights.length) {
return false;
}
var numericHighlights = highlights.filter(function (highlight) {
return typeof highlight === 'number';
});
var minHighlight = Math.min.apply(Math, _toConsumableArray(numericHighlights));
var maxHighlight = Math.max.apply(Math, _toConsumableArray(numericHighlights));
if (side === 'left') {
return minHighlight < 0 && minHighlight <= -start && (typeof size === 'number' ? minHighlight >= -(start + size) : true);
} else {
return maxHighlight > GRID_SIZE && maxHighlight >= GRID_SIZE + start && (typeof size === 'number' ? maxHighlight <= GRID_SIZE + size : true);
}
};
var gutterGridLines = function gutterGridLines(editorMaxWidth, editorWidth, highlights, shouldCalcBreakoutGridLines) {
var gridLines = [];
if (!shouldCalcBreakoutGridLines) {
return gridLines;
}
var wideSpacing = (editorMaxWidth * breakoutWideScaleRatio - editorMaxWidth) / 2;
sides.forEach(function (side) {
gridLines.push( /*#__PURE__*/React.createElement("div", {
key: side
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
,
className: classnames('gridLine', overflowHighlight(highlights, side, 0, 4) ? 'highlight' : '')
// eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Ignored via go/DSP-18766
,
style: _defineProperty({
position: 'absolute'
}, side, "-".concat(wideSpacing, "px"))
}));
gridLines.push( /*#__PURE__*/React.createElement("div", {
key: side + '-bk'
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
,
className: classnames('gridLine', highlights.indexOf('full-width') > -1 ? 'highlight' : ''),
style: _defineProperty({
// eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Ignored via go/DSP-18766
position: 'absolute'
}, side, "-".concat((editorWidth - editorMaxWidth - akEditorBreakoutPadding) / 2, "px"))
}));
});
return gridLines;
};
var lineLengthGridLines = function lineLengthGridLines(highlights) {
var gridLines = [];
var gridSpacing = 100 / GRID_SIZE;
for (var i = 0; i <= GRID_SIZE; i++) {
var style = {
paddingLeft: "".concat(gridSpacing, "%")
};
gridLines.push( /*#__PURE__*/React.createElement("div", {
key: i
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
,
className: classnames('gridLine', highlights.indexOf(i) > -1 ? 'highlight' : '')
// eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Ignored via go/DSP-18766
,
style: i < GRID_SIZE ? style : undefined
}));
}
return gridLines;
};
var Grid = function Grid(_ref3) {
var highlight = _ref3.highlight,
shouldCalcBreakoutGridLines = _ref3.shouldCalcBreakoutGridLines,
theme = _ref3.theme,
containerElement = _ref3.containerElement,
editorWidth = _ref3.editorWidth,
gridType = _ref3.gridType,
visible = _ref3.visible;
var editorMaxWidth = theme.layoutMaxWidth;
var gridLines = [].concat(_toConsumableArray(lineLengthGridLines(highlight)), _toConsumableArray(gutterGridLines(editorMaxWidth, editorWidth, highlight, shouldCalcBreakoutGridLines)));
return (
/*#__PURE__*/
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
React.createElement("div", {
className: "gridParent"
}, /*#__PURE__*/React.createElement("div", {
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
className: classnames('gridContainer', gridType),
style: {
height: "".concat(containerElement.scrollHeight, "px"),
display: visible ? 'block' : 'none'
},
"data-testid": "gridContainer"
}, gridLines))
);
};
var ThemedGrid = withTheme(Grid);
var selector = function selector(states) {
var _states$widthState, _states$gridState, _states$gridState2, _states$gridState3;
return {
width: (_states$widthState = states.widthState) === null || _states$widthState === void 0 ? void 0 : _states$widthState.width,
visible: (_states$gridState = states.gridState) === null || _states$gridState === void 0 ? void 0 : _states$gridState.visible,
gridType: (_states$gridState2 = states.gridState) === null || _states$gridState2 === void 0 ? void 0 : _states$gridState2.gridType,
highlight: (_states$gridState3 = states.gridState) === null || _states$gridState3 === void 0 ? void 0 : _states$gridState3.highlight
};
};
var ContentComponent = function ContentComponent(_ref4) {
var api = _ref4.api,
editorView = _ref4.editorView,
options = _ref4.options;
var _useSharedPluginState = useSharedPluginStateWithSelector(api, ['width', 'grid'], selector),
width = _useSharedPluginState.width,
visible = _useSharedPluginState.visible,
gridType = _useSharedPluginState.gridType,
highlight = _useSharedPluginState.highlight;
if (visible === undefined || !highlight) {
return null;
}
if (expValEquals('platform_editor_remove_grid_init_reflow', 'isEnabled', true)) {
if (!visible || !highlight) {
return null;
}
} else {
if (visible === undefined || !highlight) {
return null;
}
}
return /*#__PURE__*/React.createElement(ThemedGrid, {
shouldCalcBreakoutGridLines: options && options.shouldCalcBreakoutGridLines,
editorWidth: width !== null && width !== void 0 ? width : akEditorFullPageMaxWidth
// Ignored via go/ees005
// eslint-disable-next-line @atlaskit/editor/no-as-casting
,
containerElement: editorView.dom,
visible: visible,
gridType: gridType !== null && gridType !== void 0 ? gridType : 'full',
highlight: highlight
});
};
var EMPTY_STATE = {
visible: false,
highlight: []
};
var gridPMPlugin = new SafePlugin({
key: key,
state: {
init: function init() {
return EMPTY_STATE;
},
apply: function apply(tr, currentPluginState) {
var nextPluginState = tr.getMeta(key);
if (nextPluginState) {
return nextPluginState;
}
return currentPluginState;
}
}
});
/**
* Grid plugin to be added to an `EditorPresetBuilder` and used with `ComposableEditor`
* from `@atlaskit/editor-core`.
*/
export var gridPlugin = function gridPlugin(_ref5) {
var options = _ref5.config,
api = _ref5.api;
return {
name: 'grid',
getSharedState: function getSharedState(editorState) {
if (!editorState) {
return null;
}
return key.getState(editorState) || null;
},
actions: {
displayGrid: createDisplayGrid
},
pmPlugins: function pmPlugins() {
return [{
name: 'grid',
plugin: function plugin() {
return gridPMPlugin;
}
}];
},
contentComponent: function contentComponent(_ref6) {
var editorView = _ref6.editorView;
if (!editorView || isSSR()) {
return null;
}
return /*#__PURE__*/React.createElement(ContentComponent, {
editorView: editorView,
options: options,
api: api
});
}
};
};