UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

75 lines (71 loc) 2.24 kB
import { NodeRange } from '@atlaskit/editor-prosemirror/model'; // @ts-ignore -- ReadonlyTransaction is a local declaration and will cause a TS2305 error in CCFE typecheck import { findWrapping } from '@atlaskit/editor-prosemirror/transform'; import { findTable } from '@atlaskit/editor-tables/utils'; var bail = function bail() { return { tableIsCollapsible: false }; }; /** * Checks whether we can wrap the selected table into an expand via * prosemirror-transform's `findWrapping` helper */ export var isTableCollapsible = function isTableCollapsible(tr) { var selection = tr.selection; var schema = tr.doc.type.schema; var nodePos = 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 NodeRange($pos, tr.doc.resolve(pos + node.nodeSize), $pos.depth); if (!range) { return bail(); } var canWrap = 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 */ export var 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; };