@atlaskit/editor-plugin-max-content-size
Version:
max-content-size plugin for @atlaskit/editor-core
71 lines (70 loc) • 2.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createPlugin = createPlugin;
exports.pluginKey = exports.default = void 0;
var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
var _state = require("@atlaskit/editor-prosemirror/state");
var _isExperimentEnabled = require("@atlaskit/platform-feature-experiments/is-experiment-enabled");
var pluginKey = exports.pluginKey = new _state.PluginKey('maxContentSizePlugin');
function createPlugin(dispatch, maxContentSize) {
if (!maxContentSize) {
return;
}
var maxContentSizeReached = false;
return new _safePlugin.SafePlugin({
state: {
init: function init() {
return {
maxContentSizeReached: false
};
},
apply: function apply(tr, state) {
var result = tr.doc && tr.doc.nodeSize > maxContentSize - 1;
return {
maxContentSizeReached: result
};
}
},
key: pluginKey,
filterTransaction: function filterTransaction(tr) {
var result = tr.doc && tr.doc.nodeSize > maxContentSize;
if (result || result !== maxContentSizeReached) {
dispatch(pluginKey, {
maxContentSizeReached: result
});
}
maxContentSizeReached = result;
if ((0, _isExperimentEnabled.isExperimentEnabled)('platform_editor_max_content_size_allow_delete')) {
// A document can load already over the limit, so only block transactions that grow it
// further — otherwise there is no way to edit it back under the limit.
return !result || tr.doc.nodeSize <= tr.before.nodeSize;
}
return !result;
}
});
}
var maxContentSizePlugin = function maxContentSizePlugin(_ref) {
var maxContentSize = _ref.config,
api = _ref.api;
return {
name: 'maxContentSize',
getSharedState: function getSharedState(editorState) {
if (!editorState) {
return undefined;
}
return pluginKey.getState(editorState);
},
pmPlugins: function pmPlugins() {
return [{
name: 'maxContentSize',
plugin: function plugin(_ref2) {
var dispatch = _ref2.dispatch;
return createPlugin(dispatch, maxContentSize);
}
}];
}
};
};
var _default = exports.default = maxContentSizePlugin;