UNPKG

@atlaskit/editor-plugin-table

Version:

Table plugin for the @atlaskit/editor

45 lines 2.16 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import { pluginFactory } from '@atlaskit/editor-common/utils'; import { pluginKey } from './plugin-key'; var reducer = function reducer(pluginState, action) { if (action.name === 'UPDATE') { var updated = false; var updatedState = pluginState.map(function (oldTableState) { var replace = oldTableState.pos === action.state.pos; if (replace) { updated = true; } return replace ? action.state : oldTableState; }); if (!updated) { // new, add it updatedState.push(action.state); } return updatedState; } else if (action.name === 'REMOVE') { return pluginState.filter(function (rowState) { return rowState.pos !== action.pos; }); } return pluginState; }; var _pluginFactory = pluginFactory(pluginKey, reducer, { mapping: function mapping(tr, pluginState) { if (tr.docChanged) { return pluginState.map(function (rowInfo) { var remapped = tr.mapping.mapResult(rowInfo.pos); return remapped ? _objectSpread(_objectSpread({}, rowInfo), {}, { pos: remapped.pos }) : undefined; }).filter(function (f) { return f !== undefined; }); } return pluginState; } }), createPluginState = _pluginFactory.createPluginState, createCommand = _pluginFactory.createCommand; export { createPluginState, createCommand };