UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

82 lines (81 loc) • 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sortByColumn = void 0; var _constants = require("@atlaskit/custom-steps/constants"); var _sortColumn = require("@atlaskit/custom-steps/sort-column"); var _compareNodes = require("@atlaskit/editor-common/utils/compareNodes"); var _state = require("@atlaskit/editor-prosemirror/state"); var _tableMap = require("@atlaskit/editor-tables/table-map"); var _utils = require("@atlaskit/editor-tables/utils"); var _getSelectionRect = require("@atlaskit/editor-tables/utils/get-selection-rect"); var _isSelectionType = require("@atlaskit/editor-tables/utils/is-selection-type"); var _pluginFactory = require("../plugin-factory"); var createGetInlineCardTextFromStore = function createGetInlineCardTextFromStore(attrs) { var _ref = attrs, data = _ref.data; // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any if (data && (data.name || data.title)) { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any return data.name || data.title; } var _ref2 = attrs, cardUrl = _ref2.url; return cardUrl; }; var sortByColumn = exports.sortByColumn = function sortByColumn(columnIndex) { var order = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _constants.TableSortOrder.DESC; return (0, _pluginFactory.createCommand)(function () { return { type: 'SORT_TABLE', data: { ordering: { columnIndex: columnIndex, order: order } } }; }, function (tr, state) { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion var table = (0, _utils.findTable)(tr.selection); if (!table || !table.node) { return tr; } var selectionRect = (0, _isSelectionType.isSelectionType)(tr.selection, 'cell') ? // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion (0, _getSelectionRect.getSelectionRect)(tr.selection) : (0, _utils.findCellRectClosestToPos)(tr.selection.$from); if (!selectionRect) { return tr; } var tablePluginState = (0, _pluginFactory.getPluginState)(state); var tableArray = (0, _utils.convertTableNodeToArrayOfRows)(table.node); var headerRow; if (tablePluginState.isHeaderRowEnabled) { headerRow = tableArray.shift(); } var compareNodesInOrder = (0, _compareNodes.createCompareNodes)({ getInlineCardTextFromStore: createGetInlineCardTextFromStore }, order); var sortedTable = tableArray.sort(function (rowA, rowB) { return compareNodesInOrder(rowA[columnIndex], rowB[columnIndex]); }); if (headerRow) { sortedTable.unshift(headerRow); } var newTableNode = (0, _utils.convertArrayOfRowsToTableNode)(table.node, sortedTable); tr.replaceWith(table.pos, table.pos + table.node.nodeSize, newTableNode); var pos = _tableMap.TableMap.get(table.node).positionAt(selectionRect.top, columnIndex, table.node); var prev = tablePluginState.ordering; var next = { columnIndex: columnIndex, order: order }; tr.step(new _sortColumn.TableSortStep(table.pos, prev, next)); return tr.setSelection(_state.Selection.near(tr.doc.resolve(table.start + pos))); }); };