@atlaskit/editor-plugin-focus
Version:
Focus plugin for @atlaskit/editor-core
44 lines • 1.32 kB
JavaScript
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { PluginKey } from '@atlaskit/editor-prosemirror/state';
export var key = new PluginKey('focusPluginHandler');
export var createPlugin = function createPlugin() {
return new SafePlugin({
key: key,
state: {
init: function init() {
return {
hasFocus: false
};
},
apply: function apply(tr, oldPluginState) {
var meta = tr.getMeta(key);
if (typeof meta === 'boolean') {
if (meta !== oldPluginState.hasFocus) {
return {
hasFocus: meta
};
}
}
return oldPluginState;
}
},
props: {
handleDOMEvents: {
focus: function focus(view) {
var focusState = key.getState(view.state);
if (!(focusState !== null && focusState !== void 0 && focusState.hasFocus)) {
view.dispatch(view.state.tr.setMeta(key, true));
}
return false;
},
blur: function blur(view) {
var focusState = key.getState(view.state);
if (focusState !== null && focusState !== void 0 && focusState.hasFocus) {
view.dispatch(view.state.tr.setMeta(key, false));
}
return false;
}
}
}
});
};