@atlaskit/editor-plugin-type-ahead
Version:
Type-ahead plugin for @atlaskit/editor-core
34 lines (33 loc) • 1.28 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;
}
var rules = typeAheads.reduce(function (acc, typeAhead) {
var trigger = typeAhead.customRegex || typeAhead.trigger;
if (!trigger) {
return acc;
}
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
var regex = new RegExp("(^|[.!?\\s".concat(leafNodeReplacementCharacter, "])(").concat(trigger, ")$"));
acc.push(createRule(regex, function (state, match) {
return openTypeAheadAtCursor({
triggerHandler: typeAhead,
inputMethod: INPUT_METHOD.KEYBOARD
})({
tr: state.tr
});
}));
return acc;
}, []);
var plugin = new SafePlugin(createPlugin('type-ahead', rules, {
allowInsertTextOnDocument: false
}));
return plugin;
}
export default inputRulePlugin;