@atlaskit/editor-plugin-type-ahead
Version:
Type-ahead plugin for @atlaskit/editor-core
34 lines (33 loc) • 1.25 kB
JavaScript
import { INPUT_METHOD } from '@atlaskit/editor-common/analytics';
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { createRule } from '@atlaskit/editor-common/utils';
import { createPlugin, leafNodeReplacementCharacter } from '@atlaskit/prosemirror-input-rules';
import { openTypeAheadAtCursor } from '../pm-plugins/commands/open-typeahead-at-cursor';
export function inputRulePlugin(schema, typeAheads, featureFlags) {
if (!typeAheads || typeAheads.length === 0) {
return;
}
const rules = typeAheads.reduce((acc, typeAhead) => {
const trigger = typeAhead.customRegex || typeAhead.trigger;
if (!trigger) {
return acc;
}
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
const regex = new RegExp(`(^|[.!?\\s${leafNodeReplacementCharacter}])(${trigger})$`);
acc.push(createRule(regex, (state, match) => {
return openTypeAheadAtCursor({
triggerHandler: typeAhead,
inputMethod: INPUT_METHOD.KEYBOARD
})({
tr: state.tr
});
}));
return acc;
}, []);
const plugin = new SafePlugin(createPlugin('type-ahead', rules, {
allowInsertTextOnDocument: false
}));
return plugin;
}
export default inputRulePlugin;