@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
131 lines (128 loc) • 8.01 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.TableComponentWithSharedState = void 0;
var _react = _interopRequireDefault(require("react"));
var _hooks = require("@atlaskit/editor-common/hooks");
var _editorTables = require("@atlaskit/editor-tables");
var _TableComponent = _interopRequireDefault(require("./TableComponent"));
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-explicit-any
/**
* Use useSharedPluginState to control re-renders from plugin dependencies
*/
var TableComponentWithSharedState = exports.TableComponentWithSharedState = function TableComponentWithSharedState(_ref) {
var view = _ref.view,
options = _ref.options,
getNode = _ref.getNode,
dispatchAnalyticsEvent = _ref.dispatchAnalyticsEvent,
api = _ref.api,
getEditorFeatureFlags = _ref.getEditorFeatureFlags,
eventDispatcher = _ref.eventDispatcher,
allowColumnResizing = _ref.allowColumnResizing,
allowControls = _ref.allowControls,
getPos = _ref.getPos,
forwardRef = _ref.forwardRef,
allowTableAlignment = _ref.allowTableAlignment,
allowTableResizing = _ref.allowTableResizing,
allowFixedColumnWidthOption = _ref.allowFixedColumnWidthOption;
var _useSharedPluginState = (0, _hooks.useSharedPluginStateWithSelector)(api, ['table', 'width', 'media', 'selection', 'editorViewMode', 'interaction'], function (states) {
var _states$tableState, _states$tableState2, _states$tableState3, _states$tableState4, _states$tableState5, _states$tableState6, _states$tableState7, _states$tableState8, _states$tableState9, _states$tableState0, _states$tableState1, _states$mediaState, _states$selectionStat, _states$editorViewMod, _states$widthState, _states$widthState2, _states$interactionSt;
return {
// tableState
isTableResizing: (_states$tableState = states.tableState) === null || _states$tableState === void 0 ? void 0 : _states$tableState.isTableResizing,
isHeaderColumnEnabled: (_states$tableState2 = states.tableState) === null || _states$tableState2 === void 0 ? void 0 : _states$tableState2.isHeaderColumnEnabled,
isHeaderRowEnabled: (_states$tableState3 = states.tableState) === null || _states$tableState3 === void 0 ? void 0 : _states$tableState3.isHeaderRowEnabled,
ordering: (_states$tableState4 = states.tableState) === null || _states$tableState4 === void 0 ? void 0 : _states$tableState4.ordering,
isResizing: (_states$tableState5 = states.tableState) === null || _states$tableState5 === void 0 ? void 0 : _states$tableState5.isResizing,
isInDanger: (_states$tableState6 = states.tableState) === null || _states$tableState6 === void 0 ? void 0 : _states$tableState6.isInDanger,
hoveredCell: (_states$tableState7 = states.tableState) === null || _states$tableState7 === void 0 ? void 0 : _states$tableState7.hoveredCell,
hoveredRows: (_states$tableState8 = states.tableState) === null || _states$tableState8 === void 0 ? void 0 : _states$tableState8.hoveredRows,
isTableHovered: (_states$tableState9 = states.tableState) === null || _states$tableState9 === void 0 ? void 0 : _states$tableState9.isTableHovered,
isWholeTableInDanger: (_states$tableState0 = states.tableState) === null || _states$tableState0 === void 0 ? void 0 : _states$tableState0.isWholeTableInDanger,
// Required so that table control re-renders
targetCellPosition: (_states$tableState1 = states.tableState) === null || _states$tableState1 === void 0 ? void 0 : _states$tableState1.targetCellPosition,
// mediaState
isFullscreen: (_states$mediaState = states.mediaState) === null || _states$mediaState === void 0 ? void 0 : _states$mediaState.isFullscreen,
// selectionState
selection: (_states$selectionStat = states.selectionState) === null || _states$selectionStat === void 0 ? void 0 : _states$selectionStat.selection,
// editorViewModeState
mode: (_states$editorViewMod = states.editorViewModeState) === null || _states$editorViewMod === void 0 ? void 0 : _states$editorViewMod.mode,
// widthState
width: (_states$widthState = states.widthState) === null || _states$widthState === void 0 ? void 0 : _states$widthState.width,
lineLength: (_states$widthState2 = states.widthState) === null || _states$widthState2 === void 0 ? void 0 : _states$widthState2.lineLength,
// interactionState
interaction: (_states$interactionSt = states.interactionState) === null || _states$interactionSt === void 0 ? void 0 : _states$interactionSt.interactionState
};
}),
isTableResizing = _useSharedPluginState.isTableResizing,
isHeaderColumnEnabled = _useSharedPluginState.isHeaderColumnEnabled,
isHeaderRowEnabled = _useSharedPluginState.isHeaderRowEnabled,
ordering = _useSharedPluginState.ordering,
isResizing = _useSharedPluginState.isResizing,
isInDanger = _useSharedPluginState.isInDanger,
hoveredCell = _useSharedPluginState.hoveredCell,
hoveredRows = _useSharedPluginState.hoveredRows,
isTableHovered = _useSharedPluginState.isTableHovered,
isWholeTableInDanger = _useSharedPluginState.isWholeTableInDanger,
interaction = _useSharedPluginState.interaction,
isFullscreen = _useSharedPluginState.isFullscreen,
lineLength = _useSharedPluginState.lineLength,
mode = _useSharedPluginState.mode,
selection = _useSharedPluginState.selection,
width = _useSharedPluginState.width;
var isLivePageViewMode = mode === 'view';
/**
* ED-19810
* There is a getPos issue coming from this code. We need to apply this workaround for now and apply a patch
* directly to confluence since this bug is now in production.
*/
var currentTablePos;
try {
currentTablePos = getPos ? getPos() : undefined;
} catch (e) {
// eslint-disable-line no-unused-vars
currentTablePos = undefined;
}
var selectedTable = (0, _editorTables.findTable)(view.state.selection);
var tablePos = selectedTable && selectedTable.start - 1;
var tableActive = typeof currentTablePos === 'number' && typeof tablePos === 'number' && currentTablePos === tablePos && !isTableResizing;
return /*#__PURE__*/_react.default.createElement(_TableComponent.default, {
view: view,
allowColumnResizing: allowColumnResizing,
eventDispatcher: eventDispatcher,
getPos: getPos,
isMediaFullscreen: isFullscreen,
options: options,
allowControls: allowControls,
isHeaderRowEnabled: isHeaderRowEnabled !== null && isHeaderRowEnabled !== void 0 ? isHeaderRowEnabled : false,
isHeaderColumnEnabled: isHeaderColumnEnabled !== null && isHeaderColumnEnabled !== void 0 ? isHeaderColumnEnabled : false,
isDragAndDropEnabled: (options === null || options === void 0 ? void 0 : options.isDragAndDropEnabled) && !isLivePageViewMode,
isTableScalingEnabled: options === null || options === void 0 ? void 0 : options.isTableScalingEnabled,
allowTableAlignment: allowTableAlignment,
allowTableResizing: allowTableResizing,
allowFixedColumnWidthOption: allowFixedColumnWidthOption,
tableActive: tableActive && !isLivePageViewMode && interaction !== 'hasNotHadInteraction',
ordering: ordering,
isResizing: isResizing,
getNode: getNode
// eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed)
,
containerWidth: {
width: width !== null && width !== void 0 ? width : 0,
lineLength: lineLength
},
contentDOM: forwardRef,
getEditorFeatureFlags: getEditorFeatureFlags,
dispatchAnalyticsEvent: dispatchAnalyticsEvent,
pluginInjectionApi: api,
isInDanger: !!isInDanger,
hoveredRows: hoveredRows,
hoveredCell: hoveredCell,
isTableHovered: isTableHovered,
isWholeTableInDanger: isWholeTableInDanger,
selection: selection
});
};