@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
191 lines (188 loc) • 7.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.showResizeHandleLine = exports.setTableHovered = exports.hoverTable = exports.hoverRows = exports.hoverMergedCells = exports.hoverColumns = exports.hoverCell = exports.hideResizeHandleLine = exports.clearHoverSelection = void 0;
var _tableMap = require("@atlaskit/editor-tables/table-map");
var _utils = require("@atlaskit/editor-tables/utils");
var _types = require("../../types");
var _pluginFactory = require("../plugin-factory");
var _decoration = require("../utils/decoration");
var _table = require("../utils/table");
var _updatePluginStateDecorations = require("../utils/update-plugin-state-decorations");
// #region Imports
var makeArray = function makeArray(n) {
return Array.from(Array(n).keys());
};
var hoverMergedCells = exports.hoverMergedCells = function hoverMergedCells() {
return (0, _pluginFactory.createCommand)(function (state) {
var mergedCellsPositions = (0, _table.getMergedCellsPositions)(state.tr);
if (!mergedCellsPositions.length) {
return false;
}
var table = (0, _utils.findTable)(state.tr.selection);
if (!table) {
return false;
}
var mergedCells = mergedCellsPositions.map(function (pos) {
return {
pos: pos + table.start,
start: pos + table.start + 1,
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
node: table.node.nodeAt(pos)
};
});
var decorations = (0, _decoration.createCellHoverDecoration)(mergedCells);
return {
type: 'HOVER_MERGED_CELLS',
data: {
decorationSet: (0, _updatePluginStateDecorations.updatePluginStateDecorations)(state, decorations, _types.TableDecorations.CELL_CONTROLS_HOVER)
}
};
}, function (tr) {
return tr.setMeta('addToHistory', false);
});
};
var hoverColumns = exports.hoverColumns = function hoverColumns(hoveredColumns, isInDanger) {
return (0, _pluginFactory.createCommand)(function (state) {
var cells = (0, _utils.getCellsInColumn)(hoveredColumns)(state.tr.selection);
var _getPluginState = (0, _pluginFactory.getPluginState)(state),
isDragAndDropEnabled = _getPluginState.isDragAndDropEnabled;
if (!cells) {
return false;
}
var decorations = (0, _decoration.createControlsHoverDecoration)(cells, 'column', state.tr, isDragAndDropEnabled, hoveredColumns, isInDanger);
return {
type: 'HOVER_COLUMNS',
data: {
decorationSet: (0, _updatePluginStateDecorations.updatePluginStateDecorations)(state, decorations, _types.TableDecorations.COLUMN_CONTROLS_HOVER),
hoveredColumns: hoveredColumns,
isInDanger: isInDanger
}
};
}, function (tr) {
return tr.setMeta('addToHistory', false);
});
};
var hoverRows = exports.hoverRows = function hoverRows(hoveredRows, isInDanger) {
return (0, _pluginFactory.createCommand)(function (state) {
var cells = (0, _utils.getCellsInRow)(hoveredRows)(state.selection);
if (!cells) {
return false;
}
var _getPluginState2 = (0, _pluginFactory.getPluginState)(state),
isDragAndDropEnabled = _getPluginState2.isDragAndDropEnabled;
var decorations = (0, _decoration.createControlsHoverDecoration)(cells, 'row', state.tr, isDragAndDropEnabled, hoveredRows, isInDanger);
return {
type: 'HOVER_ROWS',
data: {
decorationSet: (0, _updatePluginStateDecorations.updatePluginStateDecorations)(state, decorations, _types.TableDecorations.ROW_CONTROLS_HOVER),
hoveredRows: hoveredRows,
isInDanger: isInDanger
}
};
}, function (tr) {
return tr.setMeta('addToHistory', false);
});
};
var hoverTable = exports.hoverTable = function hoverTable(isInDanger, isSelected) {
return (0, _pluginFactory.createCommand)(function (state) {
var table = (0, _utils.findTable)(state.selection);
if (!table) {
return false;
}
var map = _tableMap.TableMap.get(table.node);
var hoveredColumns = makeArray(map.width);
var hoveredRows = makeArray(map.height);
var cells = (0, _utils.getCellsInRow)(hoveredRows)(state.selection);
if (!cells) {
return false;
}
var _getPluginState3 = (0, _pluginFactory.getPluginState)(state),
isDragAndDropEnabled = _getPluginState3.isDragAndDropEnabled;
var decorations = (0, _decoration.createControlsHoverDecoration)(cells, 'table', state.tr, isDragAndDropEnabled, [], isInDanger, isSelected);
return {
type: 'HOVER_TABLE',
data: {
decorationSet: (0, _updatePluginStateDecorations.updatePluginStateDecorations)(state, decorations, _types.TableDecorations.TABLE_CONTROLS_HOVER),
hoveredColumns: hoveredColumns,
hoveredRows: hoveredRows,
isInDanger: isInDanger,
isWholeTableInDanger: isInDanger
}
};
}, function (tr) {
return tr.setMeta('addToHistory', false);
});
};
var clearHoverSelection = exports.clearHoverSelection = function clearHoverSelection() {
return (0, _pluginFactory.createCommand)(function (state) {
return {
type: 'CLEAR_HOVER_SELECTION',
data: {
decorationSet: (0, _updatePluginStateDecorations.updatePluginStateDecorations)(state, [], _types.TableDecorations.ALL_CONTROLS_HOVER),
isInDanger: false,
isWholeTableInDanger: false
}
};
});
};
var showResizeHandleLine = exports.showResizeHandleLine = function showResizeHandleLine(cellColumnPositioning) {
return (0, _pluginFactory.createCommand)(function (state) {
var _getPluginState4 = (0, _pluginFactory.getPluginState)(state),
isDragAndDropEnabled = _getPluginState4.isDragAndDropEnabled;
return {
type: 'SHOW_RESIZE_HANDLE_LINE',
data: {
decorationSet: (0, _updatePluginStateDecorations.updatePluginStateDecorations)(state, (0, _decoration.createColumnLineResize)(state.selection, cellColumnPositioning, isDragAndDropEnabled), _types.TableDecorations.COLUMN_RESIZING_HANDLE_LINE)
}
};
});
};
var hideResizeHandleLine = exports.hideResizeHandleLine = function hideResizeHandleLine() {
return (0, _pluginFactory.createCommand)(function (state) {
return {
type: 'HIDE_RESIZE_HANDLE_LINE',
data: {
decorationSet: (0, _updatePluginStateDecorations.updatePluginStateDecorations)(state, [], _types.TableDecorations.COLUMN_RESIZING_HANDLE_LINE)
}
};
});
};
var setTableHovered = exports.setTableHovered = function setTableHovered(hovered) {
return (0, _pluginFactory.createCommand)(function () {
return {
type: 'TABLE_HOVERED',
data: {
isTableHovered: hovered
}
};
}, function (tr) {
return tr.setMeta('addToHistory', false);
});
};
var hoverCell = exports.hoverCell = function hoverCell(rowIndex, colIndex) {
return (0, _pluginFactory.createCommand)(function (state) {
var _getPluginState5 = (0, _pluginFactory.getPluginState)(state),
prevHoveredCell = _getPluginState5.hoveredCell;
// If no arguments have been passed then the intention it to reset the hover cell data
var clear = rowIndex === undefined && colIndex === undefined;
var nextRowIndex = clear ? undefined : rowIndex !== null && rowIndex !== void 0 ? rowIndex : prevHoveredCell.rowIndex;
var nextColIndex = clear ? undefined : colIndex !== null && colIndex !== void 0 ? colIndex : prevHoveredCell.colIndex;
if (nextRowIndex === prevHoveredCell.rowIndex && nextColIndex === prevHoveredCell.colIndex) {
return false;
}
return {
type: 'HOVER_CELL',
data: {
hoveredCell: {
rowIndex: nextRowIndex,
colIndex: nextColIndex
}
}
};
}, function (tr) {
return tr.setMeta('addToHistory', false);
});
};