@atlaskit/editor-plugin-table
Version:
Table plugin for the @atlaskit/editor
40 lines • 1.16 kB
JavaScript
import { pluginFactory } from '@atlaskit/editor-common/utils';
import { pluginKey } from './plugin-key';
const reducer = (pluginState, action) => {
if (action.name === 'UPDATE') {
let updated = false;
const updatedState = pluginState.map(oldTableState => {
const 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(rowState => rowState.pos !== action.pos);
}
return pluginState;
};
const {
createPluginState,
createCommand
} = pluginFactory(pluginKey, reducer, {
mapping: (tr, pluginState) => {
if (tr.docChanged) {
return pluginState.map(rowInfo => {
const remapped = tr.mapping.mapResult(rowInfo.pos);
return remapped ? {
...rowInfo,
pos: remapped.pos
} : undefined;
}).filter(f => f !== undefined);
}
return pluginState;
}
});
export { createPluginState, createCommand };