UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

81 lines (76 loc) 2.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isTableCollapsible = exports.collapseSelectedTable = void 0; var _model = require("@atlaskit/editor-prosemirror/model"); var _transform = require("@atlaskit/editor-prosemirror/transform"); var _utils = require("@atlaskit/editor-tables/utils"); // @ts-ignore -- ReadonlyTransaction is a local declaration and will cause a TS2305 error in CCFE typecheck var bail = function bail() { return { tableIsCollapsible: false }; }; /** * Checks whether we can wrap the selected table into an expand via * prosemirror-transform's `findWrapping` helper */ var isTableCollapsible = exports.isTableCollapsible = function isTableCollapsible(tr) { var selection = tr.selection; var schema = tr.doc.type.schema; var nodePos = (0, _utils.findTable)(selection); if (!nodePos) { return bail(); } var expand = schema.nodes.expand; var node = nodePos.node, pos = nodePos.pos; var $pos = tr.doc.resolve(pos); var range = new _model.NodeRange($pos, tr.doc.resolve(pos + node.nodeSize), $pos.depth); if (!range) { return bail(); } var canWrap = (0, _transform.findWrapping)(range, expand); if (canWrap === null) { return bail(); } return { tableIsCollapsible: true, range: range, /** * Do we ever want to deal with the result of `findWrapping`? Probably not, * but we have it anyway. */ findWrappingRes: canWrap }; }; /** * Collapses the selected table into an expand given a transaction via * `Transform.wrap`. * * Will return undefined if it cannot determine the relevant table from a * selection, or if the table itself isn't collapsible. * * @param tr * @returns Transaction | undefined */ var collapseSelectedTable = exports.collapseSelectedTable = function collapseSelectedTable(tr) { var canCollapse = isTableCollapsible(tr); var expand = tr.doc.type.schema.nodes.expand; if (!canCollapse.range || !canCollapse.tableIsCollapsible) { return undefined; } /** * TODO: add attrs: { __expanded: false } when * - it is working with new collab (CEMS-1204) * - synchrony is no longer used * * (via confluence-frontend, "this feature" referencing allowInteractiveExpand) * `we can NEVER allow this feature to be enabled for the synchrony-powered editor */ tr.wrap(canCollapse.range, [{ type: expand }]).setMeta('scrollIntoView', true); return tr; };