@atlaskit/editor-plugin-interaction
Version:
Interaction plugin for @atlaskit/editor-core
45 lines • 1.42 kB
JavaScript
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { PluginKey } from '@atlaskit/editor-prosemirror/state';
export const key = new PluginKey('interactionPluginHandler');
const handleInteraction = view => {
const interactionState = key.getState(view.state);
if (!(interactionState !== null && interactionState !== void 0 && interactionState.hasHadInteraction)) {
view.dispatch(view.state.tr.setMeta(key, {
hasHadInteraction: true
}));
}
return false;
};
export const createPlugin = () => new SafePlugin({
key,
state: {
init() {
return {
hasHadInteraction: false
};
},
apply(tr, oldPluginState) {
const meta = tr.getMeta(key);
if (typeof meta === 'object') {
if (meta.hasHadInteraction !== oldPluginState.hasHadInteraction) {
return {
hasHadInteraction: meta.hasHadInteraction
};
}
}
return oldPluginState;
}
},
props: {
handleDOMEvents: {
// Handle all pointer click events (includes drag inside editor)
mousedown: handleInteraction,
// Handle keyboard events. Must be keyup to handle tabbing into editor (keyup occurs
// on the "next focused" element)
keyup: handleInteraction,
// Handle drag and drop _into_ the editor from outside. Eg image DnD
drop: handleInteraction,
focus: handleInteraction
}
}
});