@atlaskit/editor-core
Version:
A package contains Atlassian editor core functionality
79 lines • 3.17 kB
JavaScript
import { Plugin, PluginKey, } from '../../prosemirror';
import { clearFormatting } from './commands';
import keymapPlugin from './keymap';
var ClearFormattingState = (function () {
function ClearFormattingState(state) {
var _this = this;
this.formattingIsPresent = false;
this.markTypes = [
'em', 'code', 'strike', 'strong', 'underline', 'link', 'textColor'
];
this.changeHandlers = [];
this.blockStylingIsPresent = function () {
var state = _this.state;
var _a = state.selection, from = _a.from, to = _a.to;
var isBlockStyling = false;
state.doc.nodesBetween(from, to, function (node, pos) {
if (node.isBlock && node.type !== state.schema.nodes.paragraph) {
isBlockStyling = true;
}
});
return isBlockStyling;
};
this.changeHandlers = [];
this.update(state);
}
ClearFormattingState.prototype.subscribe = function (cb) {
this.changeHandlers.push(cb);
cb(this);
};
ClearFormattingState.prototype.unsubscribe = function (cb) {
this.changeHandlers = this.changeHandlers.filter(function (ch) { return ch !== cb; });
};
ClearFormattingState.prototype.update = function (newEditorState) {
var _this = this;
this.state = newEditorState;
var state = this.state;
this.activeMarkTypes = this.markTypes.filter(function (mark) { return state.schema.marks[mark] && _this.markIsActive(state.schema.marks[mark]); });
var formattingIsPresent = this.activeMarkTypes.length > 0 || this.blockStylingIsPresent();
if (formattingIsPresent !== this.formattingIsPresent) {
this.formattingIsPresent = formattingIsPresent;
this.triggerOnChange();
}
};
ClearFormattingState.prototype.clearFormatting = function (view) {
clearFormatting(this.markTypes)(view.state, view.dispatch);
};
ClearFormattingState.prototype.triggerOnChange = function () {
var _this = this;
this.changeHandlers.forEach(function (cb) { return cb(_this); });
};
ClearFormattingState.prototype.markIsActive = function (mark) {
var state = this.state;
var _a = state.selection, from = _a.from, to = _a.to, empty = _a.empty;
if (empty) {
return !!mark.isInSet(state.selection.$from.marks());
}
return state.doc.rangeHasMark(from, to, mark);
};
return ClearFormattingState;
}());
export { ClearFormattingState };
export var stateKey = new PluginKey('clearFormattingPlugin');
export var plugin = new Plugin({
state: {
init: function (config, state) {
return new ClearFormattingState(state);
},
apply: function (tr, pluginState, oldState, newState) {
pluginState.update(newState);
return pluginState;
}
},
key: stateKey
});
var plugins = function (schema) {
return [plugin, keymapPlugin(schema)].filter(function (plugin) { return !!plugin; });
};
export default plugins;
//# sourceMappingURL=index.js.map