add startOfLine option

This commit is contained in:
Philipp Kühn
2018-09-04 23:26:51 +02:00
parent 0e3810bca0
commit 39072b217d
2 changed files with 9 additions and 4 deletions

View File

@@ -45,7 +45,10 @@ export default class BlockquoteNode extends Node {
return [ return [
suggestionsPlugin({ suggestionsPlugin({
debug: true, debug: true,
matcher: triggerCharacter('@', { allowSpaces: false }), matcher: triggerCharacter('@', {
allowSpaces: true,
startOfLine: false,
}),
onEnter(args) { onEnter(args) {
console.log('start', args); console.log('start', args);
}, },

View File

@@ -8,16 +8,18 @@ import { Decoration, DecorationSet } from 'prosemirror-view';
* @param {Boolean} allowSpaces * @param {Boolean} allowSpaces
* @returns {function(*)} * @returns {function(*)}
*/ */
export function triggerCharacter(char, { allowSpaces = false }) { export function triggerCharacter(char, { allowSpaces = false, startOfLine = false }) {
/** /**
* @param {ResolvedPos} $position * @param {ResolvedPos} $position
*/ */
return $position => { return $position => {
// Matching expressions used for later // Matching expressions used for later
const suffix = new RegExp(`\\s${char}$`); const suffix = new RegExp(`\\s${char}$`);
const prefix = startOfLine ? '^' : ''
const regexp = allowSpaces const regexp = allowSpaces
? new RegExp(`${char}.*?(?=\\s${char}|$)`, 'g') ? new RegExp(`${prefix}${char}.*?(?=\\s${char}|$)`, 'g')
: new RegExp(`(?:^)?${char}[^\\s${char}]*`, 'g'); : new RegExp(`${prefix}(?:^)?${char}[^\\s${char}]*`, 'g');
// Lookup the boundaries of the current node // Lookup the boundaries of the current node
const textFrom = $position.before(); const textFrom = $position.before();