@atlaskit/editor-plugin-focus
Version:
Focus plugin for @atlaskit/editor-core
50 lines (49 loc) • 1.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.key = exports.createPlugin = void 0;
var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
var _state = require("@atlaskit/editor-prosemirror/state");
var key = exports.key = new _state.PluginKey('focusPluginHandler');
var createPlugin = exports.createPlugin = function createPlugin() {
return new _safePlugin.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;
}
}
}
});
};