@tiptap/suggestion 2.0.0-beta.95 → 2.0.0-beta.98

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@ import { ResolvedPos } from 'prosemirror-model';
3
3
  export interface Trigger {
4
4
  char: string;
5
5
  allowSpaces: boolean;
6
- prefixSpace: boolean;
6
+ allowedPrefixes: string[] | null;
7
7
  startOfLine: boolean;
8
8
  $position: ResolvedPos;
9
9
  }
@@ -6,8 +6,8 @@ export interface SuggestionOptions<I = any> {
6
6
  editor: Editor;
7
7
  char?: string;
8
8
  allowSpaces?: boolean;
9
+ allowedPrefixes?: string[] | null;
9
10
  startOfLine?: boolean;
10
- prefixSpace?: boolean;
11
11
  decorationTag?: string;
12
12
  decorationClass?: string;
13
13
  command?: (props: {
@@ -48,5 +48,5 @@ export interface SuggestionKeyDownProps {
48
48
  event: KeyboardEvent;
49
49
  range: Range;
50
50
  }
51
- export declare const SuggestionPluginKey: PluginKey<any, any>;
52
- export declare function Suggestion<I = any>({ pluginKey, editor, char, allowSpaces, prefixSpace, startOfLine, decorationTag, decorationClass, command, items, render, allow, }: SuggestionOptions<I>): Plugin<any, any>;
51
+ export declare const SuggestionPluginKey: PluginKey<any>;
52
+ export declare function Suggestion<I = any>({ pluginKey, editor, char, allowSpaces, allowedPrefixes, startOfLine, decorationTag, decorationClass, command, items, render, allow, }: SuggestionOptions<I>): Plugin<any>;
@@ -8,7 +8,7 @@ var core = require('@tiptap/core');
8
8
 
9
9
  function findSuggestionMatch(config) {
10
10
  var _a;
11
- const { char, allowSpaces, prefixSpace, startOfLine, $position, } = config;
11
+ const { char, allowSpaces, allowedPrefixes, startOfLine, $position, } = config;
12
12
  const escapedChar = core.escapeForRegEx(char);
13
13
  const suffix = new RegExp(`\\s${escapedChar}$`);
14
14
  const prefix = startOfLine ? '^' : '';
@@ -27,8 +27,8 @@ function findSuggestionMatch(config) {
27
27
  // JavaScript doesn't have lookbehinds. This hacks a check that first character
28
28
  // is a space or the start of the line
29
29
  const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index);
30
- const matchPrefixIsSpace = /^[\s\0]?$/.test(matchPrefix);
31
- if (prefixSpace && !matchPrefixIsSpace) {
30
+ const matchPrefixIsAllowed = new RegExp(`^[${allowedPrefixes === null || allowedPrefixes === void 0 ? void 0 : allowedPrefixes.join('')}\0]?$`).test(matchPrefix);
31
+ if (allowedPrefixes !== null && !matchPrefixIsAllowed) {
32
32
  return null;
33
33
  }
34
34
  // The absolute position of the match in the document
@@ -55,10 +55,10 @@ function findSuggestionMatch(config) {
55
55
  }
56
56
 
57
57
  const SuggestionPluginKey = new prosemirrorState.PluginKey('suggestion');
58
- function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allowSpaces = false, prefixSpace = true, startOfLine = false, decorationTag = 'span', decorationClass = 'suggestion', command = () => null, items = () => [], render = () => ({}), allow = () => true, }) {
58
+ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allowSpaces = false, allowedPrefixes = [' '], startOfLine = false, decorationTag = 'span', decorationClass = 'suggestion', command = () => null, items = () => [], render = () => ({}), allow = () => true, }) {
59
59
  let props;
60
60
  const renderer = render === null || render === void 0 ? void 0 : render();
61
- return new prosemirrorState.Plugin({
61
+ const plugin = new prosemirrorState.Plugin({
62
62
  key: pluginKey,
63
63
  view() {
64
64
  return {
@@ -81,7 +81,7 @@ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allow
81
81
  const state = handleExit && !handleStart
82
82
  ? prev
83
83
  : next;
84
- const decorationNode = document.querySelector(`[data-decoration-id="${state.decorationId}"]`);
84
+ const decorationNode = view.dom.querySelector(`[data-decoration-id="${state.decorationId}"]`);
85
85
  props = {
86
86
  editor,
87
87
  range: state.range,
@@ -103,7 +103,7 @@ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allow
103
103
  var _a;
104
104
  // because of `items` can be asynchrounous we’ll search for the current docoration node
105
105
  const { decorationId } = (_a = this.key) === null || _a === void 0 ? void 0 : _a.getState(editor.state); // eslint-disable-line
106
- const currentDecorationNode = document.querySelector(`[data-decoration-id="${decorationId}"]`);
106
+ const currentDecorationNode = view.dom.querySelector(`[data-decoration-id="${decorationId}"]`);
107
107
  return (currentDecorationNode === null || currentDecorationNode === void 0 ? void 0 : currentDecorationNode.getBoundingClientRect()) || null;
108
108
  }
109
109
  : null,
@@ -142,13 +142,17 @@ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allow
142
142
  state: {
143
143
  // Initialize the plugin's internal state.
144
144
  init() {
145
- return {
145
+ const state = {
146
146
  active: false,
147
- range: {},
147
+ range: {
148
+ from: 0,
149
+ to: 0,
150
+ },
148
151
  query: null,
149
152
  text: null,
150
153
  composing: false,
151
154
  };
155
+ return state;
152
156
  },
153
157
  // Apply changes to the plugin state from a view transaction.
154
158
  apply(transaction, prev, oldState, state) {
@@ -163,7 +167,7 @@ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allow
163
167
  // * a composition is active (see: https://github.com/ueberdosis/tiptap/issues/1449)
164
168
  if (isEditable && (empty || editor.view.composing)) {
165
169
  // Reset active state if we just left the previous suggestion range
166
- if ((from < prev.range.from || from > prev.range.to)
170
+ if (((from < prev.range.from) || from > prev.range.to)
167
171
  && !composing
168
172
  && !prev.composing) {
169
173
  next.active = false;
@@ -172,7 +176,7 @@ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allow
172
176
  const match = findSuggestionMatch({
173
177
  char,
174
178
  allowSpaces,
175
- prefixSpace,
179
+ allowedPrefixes,
176
180
  startOfLine,
177
181
  $position: selection.$from,
178
182
  });
@@ -195,7 +199,7 @@ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allow
195
199
  // Make sure to empty the range if suggestion is inactive
196
200
  if (!next.active) {
197
201
  next.decorationId = null;
198
- next.range = {};
202
+ next.range = { from: 0, to: 0 };
199
203
  next.query = null;
200
204
  next.text = null;
201
205
  }
@@ -206,7 +210,7 @@ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allow
206
210
  // Call the keydown hook if suggestion is active.
207
211
  handleKeyDown(view, event) {
208
212
  var _a;
209
- const { active, range } = this.getState(view.state);
213
+ const { active, range } = plugin.getState(view.state);
210
214
  if (!active) {
211
215
  return false;
212
216
  }
@@ -214,7 +218,7 @@ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allow
214
218
  },
215
219
  // Setup decorator on the currently active suggestion.
216
220
  decorations(state) {
217
- const { active, range, decorationId } = this.getState(state);
221
+ const { active, range, decorationId } = plugin.getState(state);
218
222
  if (!active) {
219
223
  return null;
220
224
  }
@@ -228,6 +232,7 @@ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allow
228
232
  },
229
233
  },
230
234
  });
235
+ return plugin;
231
236
  }
232
237
 
233
238
  exports.Suggestion = Suggestion;
@@ -1 +1 @@
1
- {"version":3,"file":"tiptap-suggestion.cjs.js","sources":["../src/findSuggestionMatch.ts","../src/suggestion.ts"],"sourcesContent":["import { escapeForRegEx, Range } from '@tiptap/core'\nimport { ResolvedPos } from 'prosemirror-model'\n\nexport interface Trigger {\n char: string,\n allowSpaces: boolean,\n prefixSpace: boolean,\n startOfLine: boolean,\n $position: ResolvedPos,\n}\n\nexport type SuggestionMatch = {\n range: Range,\n query: string,\n text: string,\n} | null\n\nexport function findSuggestionMatch(config: Trigger): SuggestionMatch {\n const {\n char,\n allowSpaces,\n prefixSpace,\n startOfLine,\n $position,\n } = config\n\n const escapedChar = escapeForRegEx(char)\n const suffix = new RegExp(`\\\\s${escapedChar}$`)\n const prefix = startOfLine ? '^' : ''\n const regexp = allowSpaces\n ? new RegExp(`${prefix}${escapedChar}.*?(?=\\\\s${escapedChar}|$)`, 'gm')\n : new RegExp(`${prefix}(?:^)?${escapedChar}[^\\\\s${escapedChar}]*`, 'gm')\n\n const text = $position.nodeBefore?.isText && $position.nodeBefore.text\n\n if (!text) {\n return null\n }\n\n const textFrom = $position.pos - text.length\n const match = Array.from(text.matchAll(regexp)).pop()\n\n if (!match || match.input === undefined || match.index === undefined) {\n return null\n }\n\n // JavaScript doesn't have lookbehinds. This hacks a check that first character\n // is a space or the start of the line\n const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index)\n const matchPrefixIsSpace = /^[\\s\\0]?$/.test(matchPrefix)\n\n if (prefixSpace && !matchPrefixIsSpace) {\n return null\n }\n\n // The absolute position of the match in the document\n const from = textFrom + match.index\n let to = from + match[0].length\n\n // Edge case handling; if spaces are allowed and we're directly in between\n // two triggers\n if (allowSpaces && suffix.test(text.slice(to - 1, to + 1))) {\n match[0] += ' '\n to += 1\n }\n\n // If the $position is located within the matched substring, return that range\n if (from < $position.pos && to >= $position.pos) {\n return {\n range: {\n from,\n to,\n },\n query: match[0].slice(char.length),\n text: match[0],\n }\n }\n\n return null\n}\n","import { Editor, Range } from '@tiptap/core'\nimport { EditorState, Plugin, PluginKey } from 'prosemirror-state'\nimport { Decoration, DecorationSet, EditorView } from 'prosemirror-view'\n\nimport { findSuggestionMatch } from './findSuggestionMatch'\n\nexport interface SuggestionOptions<I = any> {\n pluginKey?: PluginKey,\n editor: Editor,\n char?: string,\n allowSpaces?: boolean,\n startOfLine?: boolean,\n prefixSpace?: boolean,\n decorationTag?: string,\n decorationClass?: string,\n command?: (props: {\n editor: Editor,\n range: Range,\n props: I,\n }) => void,\n items?: (props: {\n query: string,\n editor: Editor,\n }) => I[] | Promise<I[]>,\n render?: () => {\n onBeforeStart?: (props: SuggestionProps<I>) => void\n onStart?: (props: SuggestionProps<I>) => void,\n onBeforeUpdate?: (props: SuggestionProps<I>) => void\n onUpdate?: (props: SuggestionProps<I>) => void,\n onExit?: (props: SuggestionProps<I>) => void,\n onKeyDown?: (props: SuggestionKeyDownProps) => boolean,\n },\n allow?: (props: {\n editor: Editor,\n state: EditorState,\n range: Range,\n }) => boolean,\n}\n\nexport interface SuggestionProps<I = any> {\n editor: Editor,\n range: Range,\n query: string,\n text: string,\n items: I[],\n command: (props: I) => void,\n decorationNode: Element | null,\n clientRect?: (() => DOMRect | null) | null,\n}\n\nexport interface SuggestionKeyDownProps {\n view: EditorView,\n event: KeyboardEvent,\n range: Range,\n}\n\nexport const SuggestionPluginKey = new PluginKey('suggestion')\n\nexport function Suggestion<I = any>({\n pluginKey = SuggestionPluginKey,\n editor,\n char = '@',\n allowSpaces = false,\n prefixSpace = true,\n startOfLine = false,\n decorationTag = 'span',\n decorationClass = 'suggestion',\n command = () => null,\n items = () => [],\n render = () => ({}),\n allow = () => true,\n}: SuggestionOptions<I>) {\n\n let props: SuggestionProps<I> | undefined\n const renderer = render?.()\n\n return new Plugin({\n key: pluginKey,\n\n view() {\n return {\n update: async (view, prevState) => {\n const prev = this.key?.getState(prevState)\n const next = this.key?.getState(view.state)\n\n // See how the state changed\n const moved = prev.active && next.active && prev.range.from !== next.range.from\n const started = !prev.active && next.active\n const stopped = prev.active && !next.active\n const changed = !started && !stopped && prev.query !== next.query\n const handleStart = started || moved\n const handleChange = changed && !moved\n const handleExit = stopped || moved\n\n // Cancel when suggestion isn't active\n if (!handleStart && !handleChange && !handleExit) {\n return\n }\n\n const state = handleExit && !handleStart\n ? prev\n : next\n const decorationNode = document.querySelector(`[data-decoration-id=\"${state.decorationId}\"]`)\n\n props = {\n editor,\n range: state.range,\n query: state.query,\n text: state.text,\n items: [],\n command: commandProps => {\n command({\n editor,\n range: state.range,\n props: commandProps,\n })\n },\n decorationNode,\n // virtual node for popper.js or tippy.js\n // this can be used for building popups without a DOM node\n clientRect: decorationNode\n ? () => {\n // because of `items` can be asynchrounous we’ll search for the current docoration node\n const { decorationId } = this.key?.getState(editor.state) // eslint-disable-line\n const currentDecorationNode = document.querySelector(`[data-decoration-id=\"${decorationId}\"]`)\n\n return currentDecorationNode?.getBoundingClientRect() || null\n }\n : null,\n }\n\n if (handleStart) {\n renderer?.onBeforeStart?.(props)\n }\n\n if (handleChange) {\n renderer?.onBeforeUpdate?.(props)\n }\n\n if (handleChange || handleStart) {\n props.items = await items({\n editor,\n query: state.query,\n })\n }\n\n if (handleExit) {\n renderer?.onExit?.(props)\n }\n\n if (handleChange) {\n renderer?.onUpdate?.(props)\n }\n\n if (handleStart) {\n renderer?.onStart?.(props)\n }\n },\n\n destroy: () => {\n if (!props) {\n return\n }\n\n renderer?.onExit?.(props)\n },\n }\n },\n\n state: {\n // Initialize the plugin's internal state.\n init() {\n return {\n active: false,\n range: {},\n query: null,\n text: null,\n composing: false,\n }\n },\n\n // Apply changes to the plugin state from a view transaction.\n apply(transaction, prev, oldState, state) {\n const { isEditable } = editor\n const { composing } = editor.view\n const { selection } = transaction\n const { empty, from } = selection\n const next = { ...prev }\n\n next.composing = composing\n\n // We can only be suggesting if the view is editable, and:\n // * there is no selection, or\n // * a composition is active (see: https://github.com/ueberdosis/tiptap/issues/1449)\n if (isEditable && (empty || editor.view.composing)) {\n // Reset active state if we just left the previous suggestion range\n if (\n (from < prev.range.from || from > prev.range.to)\n && !composing\n && !prev.composing\n ) {\n next.active = false\n }\n\n // Try to match against where our cursor currently is\n const match = findSuggestionMatch({\n char,\n allowSpaces,\n prefixSpace,\n startOfLine,\n $position: selection.$from,\n })\n const decorationId = `id_${Math.floor(Math.random() * 0xFFFFFFFF)}`\n\n // If we found a match, update the current state to show it\n if (match && allow({ editor, state, range: match.range })) {\n next.active = true\n next.decorationId = prev.decorationId ? prev.decorationId : decorationId\n next.range = match.range\n next.query = match.query\n next.text = match.text\n } else {\n next.active = false\n }\n } else {\n next.active = false\n }\n\n // Make sure to empty the range if suggestion is inactive\n if (!next.active) {\n next.decorationId = null\n next.range = {}\n next.query = null\n next.text = null\n }\n\n return next\n },\n },\n\n props: {\n // Call the keydown hook if suggestion is active.\n handleKeyDown(view, event) {\n const { active, range } = this.getState(view.state)\n\n if (!active) {\n return false\n }\n\n return renderer?.onKeyDown?.({ view, event, range }) || false\n },\n\n // Setup decorator on the currently active suggestion.\n decorations(state) {\n const { active, range, decorationId } = this.getState(state)\n\n if (!active) {\n return null\n }\n\n return DecorationSet.create(state.doc, [\n Decoration.inline(range.from, range.to, {\n nodeName: decorationTag,\n class: decorationClass,\n 'data-decoration-id': decorationId,\n }),\n ])\n },\n },\n })\n}\n"],"names":["escapeForRegEx","PluginKey","Plugin","DecorationSet","Decoration"],"mappings":";;;;;;;;AAiBM,SAAU,mBAAmB,CAAC,MAAe,EAAA;;AACjD,IAAA,MAAM,EACJ,IAAI,EACJ,WAAW,EACX,WAAW,EACX,WAAW,EACX,SAAS,GACV,GAAG,MAAM,CAAA;AAEV,IAAA,MAAM,WAAW,GAAGA,mBAAc,CAAC,IAAI,CAAC,CAAA;IACxC,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,CAAM,GAAA,EAAA,WAAW,CAAG,CAAA,CAAA,CAAC,CAAA;IAC/C,MAAM,MAAM,GAAG,WAAW,GAAG,GAAG,GAAG,EAAE,CAAA;IACrC,MAAM,MAAM,GAAG,WAAW;AACxB,UAAE,IAAI,MAAM,CAAC,CAAG,EAAA,MAAM,CAAG,EAAA,WAAW,CAAY,SAAA,EAAA,WAAW,CAAK,GAAA,CAAA,EAAE,IAAI,CAAC;AACvE,UAAE,IAAI,MAAM,CAAC,GAAG,MAAM,CAAA,MAAA,EAAS,WAAW,CAAA,KAAA,EAAQ,WAAW,CAAA,EAAA,CAAI,EAAE,IAAI,CAAC,CAAA;AAE1E,IAAA,MAAM,IAAI,GAAG,CAAA,CAAA,EAAA,GAAA,SAAS,CAAC,UAAU,MAAE,IAAA,IAAA,EAAA,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAA,MAAM,KAAI,SAAS,CAAC,UAAU,CAAC,IAAI,CAAA;IAEtE,IAAI,CAAC,IAAI,EAAE;AACT,QAAA,OAAO,IAAI,CAAA;AACZ,KAAA;IAED,MAAM,QAAQ,GAAG,SAAS,CAAC,GAAG,GAAG,IAAI,CAAC,MAAM,CAAA;AAC5C,IAAA,MAAM,KAAK,GAA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AAA,CAAA,GAAA,KAAA,CAAA,GAAA,QAAQ,CAAE,SAAS,MAAA,IAAA,IAAA,EAAA,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAA,IAAA,CAAA,QAAA,EAAG,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,KAAI,KAAK,CAAA;aAC9D;;AAGD,YAAA,WAAW,CAAC,KAAK,EAAA;AACf,gBAAA,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,EAAE,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;gBAE5D,IAAI,CAAC,MAAM,EAAE;AACX,oBAAA,OAAO,IAAI,CAAA;AACZ,iBAAA;AAED,gBAAA,OAAOC,6BAAa,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE;oBACrCC,0BAAU,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,EAAE;AACtC,wBAAA,QAAQ,EAAE,aAAa;AACvB,wBAAA,KAAK,EAAE,eAAe;AACtB,wBAAA,oBAAoB,EAAE,YAAY;qBACnC,CAAC;AACH,iBAAA,CAAC,CAAA;aACH;AACF,SAAA;AACF,KAAA,CAAC,CAAA;AACJ;;;;;;;"}
1
+ {"version":3,"file":"tiptap-suggestion.cjs.js","sources":["../src/findSuggestionMatch.ts","../src/suggestion.ts"],"sourcesContent":["import { escapeForRegEx, Range } from '@tiptap/core'\nimport { ResolvedPos } from 'prosemirror-model'\n\nexport interface Trigger {\n char: string,\n allowSpaces: boolean,\n allowedPrefixes: string[] | null,\n startOfLine: boolean,\n $position: ResolvedPos,\n}\n\nexport type SuggestionMatch = {\n range: Range,\n query: string,\n text: string,\n} | null\n\nexport function findSuggestionMatch(config: Trigger): SuggestionMatch {\n const {\n char,\n allowSpaces,\n allowedPrefixes,\n startOfLine,\n $position,\n } = config\n\n const escapedChar = escapeForRegEx(char)\n const suffix = new RegExp(`\\\\s${escapedChar}$`)\n const prefix = startOfLine ? '^' : ''\n const regexp = allowSpaces\n ? new RegExp(`${prefix}${escapedChar}.*?(?=\\\\s${escapedChar}|$)`, 'gm')\n : new RegExp(`${prefix}(?:^)?${escapedChar}[^\\\\s${escapedChar}]*`, 'gm')\n\n const text = $position.nodeBefore?.isText && $position.nodeBefore.text\n\n if (!text) {\n return null\n }\n\n const textFrom = $position.pos - text.length\n const match = Array.from(text.matchAll(regexp)).pop()\n\n if (!match || match.input === undefined || match.index === undefined) {\n return null\n }\n\n // JavaScript doesn't have lookbehinds. This hacks a check that first character\n // is a space or the start of the line\n const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index)\n const matchPrefixIsAllowed = new RegExp(`^[${allowedPrefixes?.join('')}\\0]?$`).test(matchPrefix)\n\n if (allowedPrefixes !== null && !matchPrefixIsAllowed) {\n return null\n }\n\n // The absolute position of the match in the document\n const from = textFrom + match.index\n let to = from + match[0].length\n\n // Edge case handling; if spaces are allowed and we're directly in between\n // two triggers\n if (allowSpaces && suffix.test(text.slice(to - 1, to + 1))) {\n match[0] += ' '\n to += 1\n }\n\n // If the $position is located within the matched substring, return that range\n if (from < $position.pos && to >= $position.pos) {\n return {\n range: {\n from,\n to,\n },\n query: match[0].slice(char.length),\n text: match[0],\n }\n }\n\n return null\n}\n","import { Editor, Range } from '@tiptap/core'\nimport { EditorState, Plugin, PluginKey } from 'prosemirror-state'\nimport { Decoration, DecorationSet, EditorView } from 'prosemirror-view'\n\nimport { findSuggestionMatch } from './findSuggestionMatch'\n\nexport interface SuggestionOptions<I = any> {\n pluginKey?: PluginKey,\n editor: Editor,\n char?: string,\n allowSpaces?: boolean,\n allowedPrefixes?: string[] | null,\n startOfLine?: boolean,\n decorationTag?: string,\n decorationClass?: string,\n command?: (props: {\n editor: Editor,\n range: Range,\n props: I,\n }) => void,\n items?: (props: {\n query: string,\n editor: Editor,\n }) => I[] | Promise<I[]>,\n render?: () => {\n onBeforeStart?: (props: SuggestionProps<I>) => void\n onStart?: (props: SuggestionProps<I>) => void,\n onBeforeUpdate?: (props: SuggestionProps<I>) => void\n onUpdate?: (props: SuggestionProps<I>) => void,\n onExit?: (props: SuggestionProps<I>) => void,\n onKeyDown?: (props: SuggestionKeyDownProps) => boolean,\n },\n allow?: (props: {\n editor: Editor,\n state: EditorState,\n range: Range,\n }) => boolean,\n}\n\nexport interface SuggestionProps<I = any> {\n editor: Editor,\n range: Range,\n query: string,\n text: string,\n items: I[],\n command: (props: I) => void,\n decorationNode: Element | null,\n clientRect?: (() => DOMRect | null) | null,\n}\n\nexport interface SuggestionKeyDownProps {\n view: EditorView,\n event: KeyboardEvent,\n range: Range,\n}\n\nexport const SuggestionPluginKey = new PluginKey('suggestion')\n\nexport function Suggestion<I = any>({\n pluginKey = SuggestionPluginKey,\n editor,\n char = '@',\n allowSpaces = false,\n allowedPrefixes = [' '],\n startOfLine = false,\n decorationTag = 'span',\n decorationClass = 'suggestion',\n command = () => null,\n items = () => [],\n render = () => ({}),\n allow = () => true,\n}: SuggestionOptions<I>) {\n\n let props: SuggestionProps<I> | undefined\n const renderer = render?.()\n\n const plugin: Plugin<any> = new Plugin({\n key: pluginKey,\n\n view() {\n return {\n update: async (view, prevState) => {\n const prev = this.key?.getState(prevState)\n const next = this.key?.getState(view.state)\n\n // See how the state changed\n const moved = prev.active && next.active && prev.range.from !== next.range.from\n const started = !prev.active && next.active\n const stopped = prev.active && !next.active\n const changed = !started && !stopped && prev.query !== next.query\n const handleStart = started || moved\n const handleChange = changed && !moved\n const handleExit = stopped || moved\n\n // Cancel when suggestion isn't active\n if (!handleStart && !handleChange && !handleExit) {\n return\n }\n\n const state = handleExit && !handleStart\n ? prev\n : next\n const decorationNode = view.dom.querySelector(`[data-decoration-id=\"${state.decorationId}\"]`)\n\n props = {\n editor,\n range: state.range,\n query: state.query,\n text: state.text,\n items: [],\n command: commandProps => {\n command({\n editor,\n range: state.range,\n props: commandProps,\n })\n },\n decorationNode,\n // virtual node for popper.js or tippy.js\n // this can be used for building popups without a DOM node\n clientRect: decorationNode\n ? () => {\n // because of `items` can be asynchrounous we’ll search for the current docoration node\n const { decorationId } = this.key?.getState(editor.state) // eslint-disable-line\n const currentDecorationNode = view.dom.querySelector(`[data-decoration-id=\"${decorationId}\"]`)\n\n return currentDecorationNode?.getBoundingClientRect() || null\n }\n : null,\n }\n\n if (handleStart) {\n renderer?.onBeforeStart?.(props)\n }\n\n if (handleChange) {\n renderer?.onBeforeUpdate?.(props)\n }\n\n if (handleChange || handleStart) {\n props.items = await items({\n editor,\n query: state.query,\n })\n }\n\n if (handleExit) {\n renderer?.onExit?.(props)\n }\n\n if (handleChange) {\n renderer?.onUpdate?.(props)\n }\n\n if (handleStart) {\n renderer?.onStart?.(props)\n }\n },\n\n destroy: () => {\n if (!props) {\n return\n }\n\n renderer?.onExit?.(props)\n },\n }\n },\n\n state: {\n // Initialize the plugin's internal state.\n init() {\n const state: {\n active: boolean,\n range: Range,\n query: null | string\n text: null | string\n composing: boolean\n decorationId?: string | null\n } = {\n active: false,\n range: {\n from: 0,\n to: 0,\n },\n query: null,\n text: null,\n composing: false,\n }\n\n return state\n },\n\n // Apply changes to the plugin state from a view transaction.\n apply(transaction, prev, oldState, state) {\n const { isEditable } = editor\n const { composing } = editor.view\n const { selection } = transaction\n const { empty, from } = selection\n const next = { ...prev }\n\n next.composing = composing\n\n // We can only be suggesting if the view is editable, and:\n // * there is no selection, or\n // * a composition is active (see: https://github.com/ueberdosis/tiptap/issues/1449)\n if (isEditable && (empty || editor.view.composing)) {\n // Reset active state if we just left the previous suggestion range\n if (\n ((from < prev.range.from) || from > prev.range.to)\n && !composing\n && !prev.composing\n ) {\n next.active = false\n }\n\n // Try to match against where our cursor currently is\n const match = findSuggestionMatch({\n char,\n allowSpaces,\n allowedPrefixes,\n startOfLine,\n $position: selection.$from,\n })\n const decorationId = `id_${Math.floor(Math.random() * 0xFFFFFFFF)}`\n\n // If we found a match, update the current state to show it\n if (match && allow({ editor, state, range: match.range })) {\n next.active = true\n next.decorationId = prev.decorationId ? prev.decorationId : decorationId\n next.range = match.range\n next.query = match.query\n next.text = match.text\n } else {\n next.active = false\n }\n } else {\n next.active = false\n }\n\n // Make sure to empty the range if suggestion is inactive\n if (!next.active) {\n next.decorationId = null\n next.range = { from: 0, to: 0 }\n next.query = null\n next.text = null\n }\n\n return next\n },\n },\n\n props: {\n // Call the keydown hook if suggestion is active.\n handleKeyDown(view, event) {\n const { active, range } = plugin.getState(view.state)\n\n if (!active) {\n return false\n }\n\n return renderer?.onKeyDown?.({ view, event, range }) || false\n },\n\n // Setup decorator on the currently active suggestion.\n decorations(state) {\n const { active, range, decorationId } = plugin.getState(state)\n\n if (!active) {\n return null\n }\n\n return DecorationSet.create(state.doc, [\n Decoration.inline(range.from, range.to, {\n nodeName: decorationTag,\n class: decorationClass,\n 'data-decoration-id': decorationId,\n }),\n ])\n },\n },\n })\n\n return plugin\n}\n"],"names":["escapeForRegEx","PluginKey","Plugin","DecorationSet","Decoration"],"mappings":";;;;;;;;AAiBM,SAAU,mBAAmB,CAAC,MAAe,EAAA;;AACjD,IAAA,MAAM,EACJ,IAAI,EACJ,WAAW,EACX,eAAe,EACf,WAAW,EACX,SAAS,GACV,GAAG,MAAM,CAAA;AAEV,IAAA,MAAM,WAAW,GAAGA,mBAAc,CAAC,IAAI,CAAC,CAAA;IACxC,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,CAAM,GAAA,EAAA,WAAW,CAAG,CAAA,CAAA,CAAC,CAAA;IAC/C,MAAM,MAAM,GAAG,WAAW,GAAG,GAAG,GAAG,EAAE,CAAA;IACrC,MAAM,MAAM,GAAG,WAAW;AACxB,UAAE,IAAI,MAAM,CAAC,CAAG,EAAA,MAAM,CAAG,EAAA,WAAW,CAAY,SAAA,EAAA,WAAW,CAAK,GAAA,CAAA,EAAE,IAAI,CAAC;AACvE,UAAE,IAAI,MAAM,CAAC,GAAG,MAAM,CAAA,MAAA,EAAS,WAAW,CAAA,KAAA,EAAQ,WAAW,CAA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AAA,OAAO,IAAI,CAAA;AACZ,iBAAA;AAED,gBAAA,OAAOC,6BAAa,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE;oBACrCC,0BAAU,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,EAAE;AACtC,wBAAA,QAAQ,EAAE,aAAa;AACvB,wBAAA,KAAK,EAAE,eAAe;AACtB,wBAAA,oBAAoB,EAAE,YAAY;qBACnC,CAAC;AACH,iBAAA,CAAC,CAAA;aACH;AACF,SAAA;AACF,KAAA,CAAC,CAAA;AAEF,IAAA,OAAO,MAAM,CAAA;AACf;;;;;;;"}
@@ -4,7 +4,7 @@ import { escapeForRegEx } from '@tiptap/core';
4
4
 
5
5
  function findSuggestionMatch(config) {
6
6
  var _a;
7
- const { char, allowSpaces, prefixSpace, startOfLine, $position, } = config;
7
+ const { char, allowSpaces, allowedPrefixes, startOfLine, $position, } = config;
8
8
  const escapedChar = escapeForRegEx(char);
9
9
  const suffix = new RegExp(`\\s${escapedChar}$`);
10
10
  const prefix = startOfLine ? '^' : '';
@@ -23,8 +23,8 @@ function findSuggestionMatch(config) {
23
23
  // JavaScript doesn't have lookbehinds. This hacks a check that first character
24
24
  // is a space or the start of the line
25
25
  const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index);
26
- const matchPrefixIsSpace = /^[\s\0]?$/.test(matchPrefix);
27
- if (prefixSpace && !matchPrefixIsSpace) {
26
+ const matchPrefixIsAllowed = new RegExp(`^[${allowedPrefixes === null || allowedPrefixes === void 0 ? void 0 : allowedPrefixes.join('')}\0]?$`).test(matchPrefix);
27
+ if (allowedPrefixes !== null && !matchPrefixIsAllowed) {
28
28
  return null;
29
29
  }
30
30
  // The absolute position of the match in the document
@@ -51,10 +51,10 @@ function findSuggestionMatch(config) {
51
51
  }
52
52
 
53
53
  const SuggestionPluginKey = new PluginKey('suggestion');
54
- function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allowSpaces = false, prefixSpace = true, startOfLine = false, decorationTag = 'span', decorationClass = 'suggestion', command = () => null, items = () => [], render = () => ({}), allow = () => true, }) {
54
+ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allowSpaces = false, allowedPrefixes = [' '], startOfLine = false, decorationTag = 'span', decorationClass = 'suggestion', command = () => null, items = () => [], render = () => ({}), allow = () => true, }) {
55
55
  let props;
56
56
  const renderer = render === null || render === void 0 ? void 0 : render();
57
- return new Plugin({
57
+ const plugin = new Plugin({
58
58
  key: pluginKey,
59
59
  view() {
60
60
  return {
@@ -77,7 +77,7 @@ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allow
77
77
  const state = handleExit && !handleStart
78
78
  ? prev
79
79
  : next;
80
- const decorationNode = document.querySelector(`[data-decoration-id="${state.decorationId}"]`);
80
+ const decorationNode = view.dom.querySelector(`[data-decoration-id="${state.decorationId}"]`);
81
81
  props = {
82
82
  editor,
83
83
  range: state.range,
@@ -99,7 +99,7 @@ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allow
99
99
  var _a;
100
100
  // because of `items` can be asynchrounous we’ll search for the current docoration node
101
101
  const { decorationId } = (_a = this.key) === null || _a === void 0 ? void 0 : _a.getState(editor.state); // eslint-disable-line
102
- const currentDecorationNode = document.querySelector(`[data-decoration-id="${decorationId}"]`);
102
+ const currentDecorationNode = view.dom.querySelector(`[data-decoration-id="${decorationId}"]`);
103
103
  return (currentDecorationNode === null || currentDecorationNode === void 0 ? void 0 : currentDecorationNode.getBoundingClientRect()) || null;
104
104
  }
105
105
  : null,
@@ -138,13 +138,17 @@ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allow
138
138
  state: {
139
139
  // Initialize the plugin's internal state.
140
140
  init() {
141
- return {
141
+ const state = {
142
142
  active: false,
143
- range: {},
143
+ range: {
144
+ from: 0,
145
+ to: 0,
146
+ },
144
147
  query: null,
145
148
  text: null,
146
149
  composing: false,
147
150
  };
151
+ return state;
148
152
  },
149
153
  // Apply changes to the plugin state from a view transaction.
150
154
  apply(transaction, prev, oldState, state) {
@@ -159,7 +163,7 @@ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allow
159
163
  // * a composition is active (see: https://github.com/ueberdosis/tiptap/issues/1449)
160
164
  if (isEditable && (empty || editor.view.composing)) {
161
165
  // Reset active state if we just left the previous suggestion range
162
- if ((from < prev.range.from || from > prev.range.to)
166
+ if (((from < prev.range.from) || from > prev.range.to)
163
167
  && !composing
164
168
  && !prev.composing) {
165
169
  next.active = false;
@@ -168,7 +172,7 @@ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allow
168
172
  const match = findSuggestionMatch({
169
173
  char,
170
174
  allowSpaces,
171
- prefixSpace,
175
+ allowedPrefixes,
172
176
  startOfLine,
173
177
  $position: selection.$from,
174
178
  });
@@ -191,7 +195,7 @@ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allow
191
195
  // Make sure to empty the range if suggestion is inactive
192
196
  if (!next.active) {
193
197
  next.decorationId = null;
194
- next.range = {};
198
+ next.range = { from: 0, to: 0 };
195
199
  next.query = null;
196
200
  next.text = null;
197
201
  }
@@ -202,7 +206,7 @@ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allow
202
206
  // Call the keydown hook if suggestion is active.
203
207
  handleKeyDown(view, event) {
204
208
  var _a;
205
- const { active, range } = this.getState(view.state);
209
+ const { active, range } = plugin.getState(view.state);
206
210
  if (!active) {
207
211
  return false;
208
212
  }
@@ -210,7 +214,7 @@ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allow
210
214
  },
211
215
  // Setup decorator on the currently active suggestion.
212
216
  decorations(state) {
213
- const { active, range, decorationId } = this.getState(state);
217
+ const { active, range, decorationId } = plugin.getState(state);
214
218
  if (!active) {
215
219
  return null;
216
220
  }
@@ -224,6 +228,7 @@ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allow
224
228
  },
225
229
  },
226
230
  });
231
+ return plugin;
227
232
  }
228
233
 
229
234
  export { Suggestion, SuggestionPluginKey, Suggestion as default, findSuggestionMatch };
@@ -1 +1 @@
1
- {"version":3,"file":"tiptap-suggestion.esm.js","sources":["../src/findSuggestionMatch.ts","../src/suggestion.ts"],"sourcesContent":["import { escapeForRegEx, Range } from '@tiptap/core'\nimport { ResolvedPos } from 'prosemirror-model'\n\nexport interface Trigger {\n char: string,\n allowSpaces: boolean,\n prefixSpace: boolean,\n startOfLine: boolean,\n $position: ResolvedPos,\n}\n\nexport type SuggestionMatch = {\n range: Range,\n query: string,\n text: string,\n} | null\n\nexport function findSuggestionMatch(config: Trigger): SuggestionMatch {\n const {\n char,\n allowSpaces,\n prefixSpace,\n startOfLine,\n $position,\n } = config\n\n const escapedChar = escapeForRegEx(char)\n const suffix = new RegExp(`\\\\s${escapedChar}$`)\n const prefix = startOfLine ? '^' : ''\n const regexp = allowSpaces\n ? new RegExp(`${prefix}${escapedChar}.*?(?=\\\\s${escapedChar}|$)`, 'gm')\n : new RegExp(`${prefix}(?:^)?${escapedChar}[^\\\\s${escapedChar}]*`, 'gm')\n\n const text = $position.nodeBefore?.isText && $position.nodeBefore.text\n\n if (!text) {\n return null\n }\n\n const textFrom = $position.pos - text.length\n const match = Array.from(text.matchAll(regexp)).pop()\n\n if (!match || match.input === undefined || match.index === undefined) {\n return null\n }\n\n // JavaScript doesn't have lookbehinds. This hacks a check that first character\n // is a space or the start of the line\n const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index)\n const matchPrefixIsSpace = /^[\\s\\0]?$/.test(matchPrefix)\n\n if (prefixSpace && !matchPrefixIsSpace) {\n return null\n }\n\n // The absolute position of the match in the document\n const from = textFrom + match.index\n let to = from + match[0].length\n\n // Edge case handling; if spaces are allowed and we're directly in between\n // two triggers\n if (allowSpaces && suffix.test(text.slice(to - 1, to + 1))) {\n match[0] += ' '\n to += 1\n }\n\n // If the $position is located within the matched substring, return that range\n if (from < $position.pos && to >= $position.pos) {\n return {\n range: {\n from,\n to,\n },\n query: match[0].slice(char.length),\n text: match[0],\n }\n }\n\n return null\n}\n","import { Editor, Range } from '@tiptap/core'\nimport { EditorState, Plugin, PluginKey } from 'prosemirror-state'\nimport { Decoration, DecorationSet, EditorView } from 'prosemirror-view'\n\nimport { findSuggestionMatch } from './findSuggestionMatch'\n\nexport interface SuggestionOptions<I = any> {\n pluginKey?: PluginKey,\n editor: Editor,\n char?: string,\n allowSpaces?: boolean,\n startOfLine?: boolean,\n prefixSpace?: boolean,\n decorationTag?: string,\n decorationClass?: string,\n command?: (props: {\n editor: Editor,\n range: Range,\n props: I,\n }) => void,\n items?: (props: {\n query: string,\n editor: Editor,\n }) => I[] | Promise<I[]>,\n render?: () => {\n onBeforeStart?: (props: SuggestionProps<I>) => void\n onStart?: (props: SuggestionProps<I>) => void,\n onBeforeUpdate?: (props: SuggestionProps<I>) => void\n onUpdate?: (props: SuggestionProps<I>) => void,\n onExit?: (props: SuggestionProps<I>) => void,\n onKeyDown?: (props: SuggestionKeyDownProps) => boolean,\n },\n allow?: (props: {\n editor: Editor,\n state: EditorState,\n range: Range,\n }) => boolean,\n}\n\nexport interface SuggestionProps<I = any> {\n editor: Editor,\n range: Range,\n query: string,\n text: string,\n items: I[],\n command: (props: I) => void,\n decorationNode: Element | null,\n clientRect?: (() => DOMRect | null) | null,\n}\n\nexport interface SuggestionKeyDownProps {\n view: EditorView,\n event: KeyboardEvent,\n range: Range,\n}\n\nexport const SuggestionPluginKey = new PluginKey('suggestion')\n\nexport function Suggestion<I = any>({\n pluginKey = SuggestionPluginKey,\n editor,\n char = '@',\n allowSpaces = false,\n prefixSpace = true,\n startOfLine = false,\n decorationTag = 'span',\n decorationClass = 'suggestion',\n command = () => null,\n items = () => [],\n render = () => ({}),\n allow = () => true,\n}: SuggestionOptions<I>) {\n\n let props: SuggestionProps<I> | undefined\n const renderer = render?.()\n\n return new Plugin({\n key: pluginKey,\n\n view() {\n return {\n update: async (view, prevState) => {\n const prev = this.key?.getState(prevState)\n const next = this.key?.getState(view.state)\n\n // See how the state changed\n const moved = prev.active && next.active && prev.range.from !== next.range.from\n const started = !prev.active && next.active\n const stopped = prev.active && !next.active\n const changed = !started && !stopped && prev.query !== next.query\n const handleStart = started || moved\n const handleChange = changed && !moved\n const handleExit = stopped || moved\n\n // Cancel when suggestion isn't active\n if (!handleStart && !handleChange && !handleExit) {\n return\n }\n\n const state = handleExit && !handleStart\n ? prev\n : next\n const decorationNode = document.querySelector(`[data-decoration-id=\"${state.decorationId}\"]`)\n\n props = {\n editor,\n range: state.range,\n query: state.query,\n text: state.text,\n items: [],\n command: commandProps => {\n command({\n editor,\n range: state.range,\n props: commandProps,\n })\n },\n decorationNode,\n // virtual node for popper.js or tippy.js\n // this can be used for building popups without a DOM node\n clientRect: decorationNode\n ? () => {\n // because of `items` can be asynchrounous we’ll search for the current docoration node\n const { decorationId } = this.key?.getState(editor.state) // eslint-disable-line\n const currentDecorationNode = document.querySelector(`[data-decoration-id=\"${decorationId}\"]`)\n\n return currentDecorationNode?.getBoundingClientRect() || null\n }\n : null,\n }\n\n if (handleStart) {\n renderer?.onBeforeStart?.(props)\n }\n\n if (handleChange) {\n renderer?.onBeforeUpdate?.(props)\n }\n\n if (handleChange || handleStart) {\n props.items = await items({\n editor,\n query: state.query,\n })\n }\n\n if (handleExit) {\n renderer?.onExit?.(props)\n }\n\n if (handleChange) {\n renderer?.onUpdate?.(props)\n }\n\n if (handleStart) {\n renderer?.onStart?.(props)\n }\n },\n\n destroy: () => {\n if (!props) {\n return\n }\n\n renderer?.onExit?.(props)\n },\n }\n },\n\n state: {\n // Initialize the plugin's internal state.\n init() {\n return {\n active: false,\n range: {},\n query: null,\n text: null,\n composing: false,\n }\n },\n\n // Apply changes to the plugin state from a view transaction.\n apply(transaction, prev, oldState, state) {\n const { isEditable } = editor\n const { composing } = editor.view\n const { selection } = transaction\n const { empty, from } = selection\n const next = { ...prev }\n\n next.composing = composing\n\n // We can only be suggesting if the view is editable, and:\n // * there is no selection, or\n // * a composition is active (see: https://github.com/ueberdosis/tiptap/issues/1449)\n if (isEditable && (empty || editor.view.composing)) {\n // Reset active state if we just left the previous suggestion range\n if (\n (from < prev.range.from || from > prev.range.to)\n && !composing\n && !prev.composing\n ) {\n next.active = false\n }\n\n // Try to match against where our cursor currently is\n const match = findSuggestionMatch({\n char,\n allowSpaces,\n prefixSpace,\n startOfLine,\n $position: selection.$from,\n })\n const decorationId = `id_${Math.floor(Math.random() * 0xFFFFFFFF)}`\n\n // If we found a match, update the current state to show it\n if (match && allow({ editor, state, range: match.range })) {\n next.active = true\n next.decorationId = prev.decorationId ? prev.decorationId : decorationId\n next.range = match.range\n next.query = match.query\n next.text = match.text\n } else {\n next.active = false\n }\n } else {\n next.active = false\n }\n\n // Make sure to empty the range if suggestion is inactive\n if (!next.active) {\n next.decorationId = null\n next.range = {}\n next.query = null\n next.text = null\n }\n\n return next\n },\n },\n\n props: {\n // Call the keydown hook if suggestion is active.\n handleKeyDown(view, event) {\n const { active, range } = this.getState(view.state)\n\n if (!active) {\n return false\n }\n\n return renderer?.onKeyDown?.({ view, event, range }) || false\n },\n\n // Setup decorator on the currently active suggestion.\n decorations(state) {\n const { active, range, decorationId } = this.getState(state)\n\n if (!active) {\n return null\n }\n\n return DecorationSet.create(state.doc, [\n Decoration.inline(range.from, range.to, {\n nodeName: decorationTag,\n class: decorationClass,\n 'data-decoration-id': decorationId,\n }),\n ])\n },\n },\n })\n}\n"],"names":[],"mappings":";;;;AAiBM,SAAU,mBAAmB,CAAC,MAAe,EAAA;;AACjD,IAAA,MAAM,EACJ,IAAI,EACJ,WAAW,EACX,WAAW,EACX,WAAW,EACX,SAAS,GACV,GAAG,MAAM,CAAA;AAEV,IAAA,MAAM,WAAW,GAAG,cAAc,CAAC,IAAI,CAAC,CAAA;IACxC,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,CAAM,GAAA,EAAA,WAAW,CAAG,CAAA,CAAA,CAAC,CAAA;IAC/C,MAAM,MAAM,GAAG,WAAW,G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AAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,EAAE;AACtC,wBAAA,QAAQ,EAAE,aAAa;AACvB,wBAAA,KAAK,EAAE,eAAe;AACtB,wBAAA,oBAAoB,EAAE,YAAY;qBACnC,CAAC;AACH,iBAAA,CAAC,CAAA;aACH;AACF,SAAA;AACF,KAAA,CAAC,CAAA;AACJ;;;;"}
1
+ {"version":3,"file":"tiptap-suggestion.esm.js","sources":["../src/findSuggestionMatch.ts","../src/suggestion.ts"],"sourcesContent":["import { escapeForRegEx, Range } from '@tiptap/core'\nimport { ResolvedPos } from 'prosemirror-model'\n\nexport interface Trigger {\n char: string,\n allowSpaces: boolean,\n allowedPrefixes: string[] | null,\n startOfLine: boolean,\n $position: ResolvedPos,\n}\n\nexport type SuggestionMatch = {\n range: Range,\n query: string,\n text: string,\n} | null\n\nexport function findSuggestionMatch(config: Trigger): SuggestionMatch {\n const {\n char,\n allowSpaces,\n allowedPrefixes,\n startOfLine,\n $position,\n } = config\n\n const escapedChar = escapeForRegEx(char)\n const suffix = new RegExp(`\\\\s${escapedChar}$`)\n const prefix = startOfLine ? '^' : ''\n const regexp = allowSpaces\n ? new RegExp(`${prefix}${escapedChar}.*?(?=\\\\s${escapedChar}|$)`, 'gm')\n : new RegExp(`${prefix}(?:^)?${escapedChar}[^\\\\s${escapedChar}]*`, 'gm')\n\n const text = $position.nodeBefore?.isText && $position.nodeBefore.text\n\n if (!text) {\n return null\n }\n\n const textFrom = $position.pos - text.length\n const match = Array.from(text.matchAll(regexp)).pop()\n\n if (!match || match.input === undefined || match.index === undefined) {\n return null\n }\n\n // JavaScript doesn't have lookbehinds. This hacks a check that first character\n // is a space or the start of the line\n const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index)\n const matchPrefixIsAllowed = new RegExp(`^[${allowedPrefixes?.join('')}\\0]?$`).test(matchPrefix)\n\n if (allowedPrefixes !== null && !matchPrefixIsAllowed) {\n return null\n }\n\n // The absolute position of the match in the document\n const from = textFrom + match.index\n let to = from + match[0].length\n\n // Edge case handling; if spaces are allowed and we're directly in between\n // two triggers\n if (allowSpaces && suffix.test(text.slice(to - 1, to + 1))) {\n match[0] += ' '\n to += 1\n }\n\n // If the $position is located within the matched substring, return that range\n if (from < $position.pos && to >= $position.pos) {\n return {\n range: {\n from,\n to,\n },\n query: match[0].slice(char.length),\n text: match[0],\n }\n }\n\n return null\n}\n","import { Editor, Range } from '@tiptap/core'\nimport { EditorState, Plugin, PluginKey } from 'prosemirror-state'\nimport { Decoration, DecorationSet, EditorView } from 'prosemirror-view'\n\nimport { findSuggestionMatch } from './findSuggestionMatch'\n\nexport interface SuggestionOptions<I = any> {\n pluginKey?: PluginKey,\n editor: Editor,\n char?: string,\n allowSpaces?: boolean,\n allowedPrefixes?: string[] | null,\n startOfLine?: boolean,\n decorationTag?: string,\n decorationClass?: string,\n command?: (props: {\n editor: Editor,\n range: Range,\n props: I,\n }) => void,\n items?: (props: {\n query: string,\n editor: Editor,\n }) => I[] | Promise<I[]>,\n render?: () => {\n onBeforeStart?: (props: SuggestionProps<I>) => void\n onStart?: (props: SuggestionProps<I>) => void,\n onBeforeUpdate?: (props: SuggestionProps<I>) => void\n onUpdate?: (props: SuggestionProps<I>) => void,\n onExit?: (props: SuggestionProps<I>) => void,\n onKeyDown?: (props: SuggestionKeyDownProps) => boolean,\n },\n allow?: (props: {\n editor: Editor,\n state: EditorState,\n range: Range,\n }) => boolean,\n}\n\nexport interface SuggestionProps<I = any> {\n editor: Editor,\n range: Range,\n query: string,\n text: string,\n items: I[],\n command: (props: I) => void,\n decorationNode: Element | null,\n clientRect?: (() => DOMRect | null) | null,\n}\n\nexport interface SuggestionKeyDownProps {\n view: EditorView,\n event: KeyboardEvent,\n range: Range,\n}\n\nexport const SuggestionPluginKey = new PluginKey('suggestion')\n\nexport function Suggestion<I = any>({\n pluginKey = SuggestionPluginKey,\n editor,\n char = '@',\n allowSpaces = false,\n allowedPrefixes = [' '],\n startOfLine = false,\n decorationTag = 'span',\n decorationClass = 'suggestion',\n command = () => null,\n items = () => [],\n render = () => ({}),\n allow = () => true,\n}: SuggestionOptions<I>) {\n\n let props: SuggestionProps<I> | undefined\n const renderer = render?.()\n\n const plugin: Plugin<any> = new Plugin({\n key: pluginKey,\n\n view() {\n return {\n update: async (view, prevState) => {\n const prev = this.key?.getState(prevState)\n const next = this.key?.getState(view.state)\n\n // See how the state changed\n const moved = prev.active && next.active && prev.range.from !== next.range.from\n const started = !prev.active && next.active\n const stopped = prev.active && !next.active\n const changed = !started && !stopped && prev.query !== next.query\n const handleStart = started || moved\n const handleChange = changed && !moved\n const handleExit = stopped || moved\n\n // Cancel when suggestion isn't active\n if (!handleStart && !handleChange && !handleExit) {\n return\n }\n\n const state = handleExit && !handleStart\n ? prev\n : next\n const decorationNode = view.dom.querySelector(`[data-decoration-id=\"${state.decorationId}\"]`)\n\n props = {\n editor,\n range: state.range,\n query: state.query,\n text: state.text,\n items: [],\n command: commandProps => {\n command({\n editor,\n range: state.range,\n props: commandProps,\n })\n },\n decorationNode,\n // virtual node for popper.js or tippy.js\n // this can be used for building popups without a DOM node\n clientRect: decorationNode\n ? () => {\n // because of `items` can be asynchrounous we’ll search for the current docoration node\n const { decorationId } = this.key?.getState(editor.state) // eslint-disable-line\n const currentDecorationNode = view.dom.querySelector(`[data-decoration-id=\"${decorationId}\"]`)\n\n return currentDecorationNode?.getBoundingClientRect() || null\n }\n : null,\n }\n\n if (handleStart) {\n renderer?.onBeforeStart?.(props)\n }\n\n if (handleChange) {\n renderer?.onBeforeUpdate?.(props)\n }\n\n if (handleChange || handleStart) {\n props.items = await items({\n editor,\n query: state.query,\n })\n }\n\n if (handleExit) {\n renderer?.onExit?.(props)\n }\n\n if (handleChange) {\n renderer?.onUpdate?.(props)\n }\n\n if (handleStart) {\n renderer?.onStart?.(props)\n }\n },\n\n destroy: () => {\n if (!props) {\n return\n }\n\n renderer?.onExit?.(props)\n },\n }\n },\n\n state: {\n // Initialize the plugin's internal state.\n init() {\n const state: {\n active: boolean,\n range: Range,\n query: null | string\n text: null | string\n composing: boolean\n decorationId?: string | null\n } = {\n active: false,\n range: {\n from: 0,\n to: 0,\n },\n query: null,\n text: null,\n composing: false,\n }\n\n return state\n },\n\n // Apply changes to the plugin state from a view transaction.\n apply(transaction, prev, oldState, state) {\n const { isEditable } = editor\n const { composing } = editor.view\n const { selection } = transaction\n const { empty, from } = selection\n const next = { ...prev }\n\n next.composing = composing\n\n // We can only be suggesting if the view is editable, and:\n // * there is no selection, or\n // * a composition is active (see: https://github.com/ueberdosis/tiptap/issues/1449)\n if (isEditable && (empty || editor.view.composing)) {\n // Reset active state if we just left the previous suggestion range\n if (\n ((from < prev.range.from) || from > prev.range.to)\n && !composing\n && !prev.composing\n ) {\n next.active = false\n }\n\n // Try to match against where our cursor currently is\n const match = findSuggestionMatch({\n char,\n allowSpaces,\n allowedPrefixes,\n startOfLine,\n $position: selection.$from,\n })\n const decorationId = `id_${Math.floor(Math.random() * 0xFFFFFFFF)}`\n\n // If we found a match, update the current state to show it\n if (match && allow({ editor, state, range: match.range })) {\n next.active = true\n next.decorationId = prev.decorationId ? prev.decorationId : decorationId\n next.range = match.range\n next.query = match.query\n next.text = match.text\n } else {\n next.active = false\n }\n } else {\n next.active = false\n }\n\n // Make sure to empty the range if suggestion is inactive\n if (!next.active) {\n next.decorationId = null\n next.range = { from: 0, to: 0 }\n next.query = null\n next.text = null\n }\n\n return next\n },\n },\n\n props: {\n // Call the keydown hook if suggestion is active.\n handleKeyDown(view, event) {\n const { active, range } = plugin.getState(view.state)\n\n if (!active) {\n return false\n }\n\n return renderer?.onKeyDown?.({ view, event, range }) || false\n },\n\n // Setup decorator on the currently active suggestion.\n decorations(state) {\n const { active, range, decorationId } = plugin.getState(state)\n\n if (!active) {\n return null\n }\n\n return DecorationSet.create(state.doc, [\n Decoration.inline(range.from, range.to, {\n nodeName: decorationTag,\n class: decorationClass,\n 'data-decoration-id': decorationId,\n }),\n ])\n },\n },\n })\n\n return plugin\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KAAK,EAAE,GAAG,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;gBAErD,IAAI,CAAC,MAAM,EAAE;AACX,oBAAA,OAAO,KAAK,CAAA;AACb,iBAAA;gBAED,OAAO,CAAA,MAAA,QAAQ,KAAA,IAAA,IAAR,QAAQ,KAAR,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,QAAQ,CAAE,SAAS,MAAA,IAAA,IAAA,EAAA,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAA,IAAA,CAAA,QAAA,EAAG,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,KAAI,KAAK,CAAA;aAC9D;;AAGD,YAAA,WAAW,CAAC,KAAK,EAAA;AACf,gBAAA,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,EAAE,GAAG,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;gBAE9D,IAAI,CAAC,MAAM,EAAE;AACX,oBAAA,OAAO,IAAI,CAAA;AACZ,iBAAA;AAED,gBAAA,OAAO,aAAa,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE;oBACrC,UAAU,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,EAAE;AACtC,wBAAA,QAAQ,EAAE,aAAa;AACvB,wBAAA,KAAK,EAAE,eAAe;AACtB,wBAAA,oBAAoB,EAAE,YAAY;qBACnC,CAAC;AACH,iBAAA,CAAC,CAAA;aACH;AACF,SAAA;AACF,KAAA,CAAC,CAAA;AAEF,IAAA,OAAO,MAAM,CAAA;AACf;;;;"}
@@ -6,7 +6,7 @@
6
6
 
7
7
  function findSuggestionMatch(config) {
8
8
  var _a;
9
- const { char, allowSpaces, prefixSpace, startOfLine, $position, } = config;
9
+ const { char, allowSpaces, allowedPrefixes, startOfLine, $position, } = config;
10
10
  const escapedChar = core.escapeForRegEx(char);
11
11
  const suffix = new RegExp(`\\s${escapedChar}$`);
12
12
  const prefix = startOfLine ? '^' : '';
@@ -25,8 +25,8 @@
25
25
  // JavaScript doesn't have lookbehinds. This hacks a check that first character
26
26
  // is a space or the start of the line
27
27
  const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index);
28
- const matchPrefixIsSpace = /^[\s\0]?$/.test(matchPrefix);
29
- if (prefixSpace && !matchPrefixIsSpace) {
28
+ const matchPrefixIsAllowed = new RegExp(`^[${allowedPrefixes === null || allowedPrefixes === void 0 ? void 0 : allowedPrefixes.join('')}\0]?$`).test(matchPrefix);
29
+ if (allowedPrefixes !== null && !matchPrefixIsAllowed) {
30
30
  return null;
31
31
  }
32
32
  // The absolute position of the match in the document
@@ -53,10 +53,10 @@
53
53
  }
54
54
 
55
55
  const SuggestionPluginKey = new prosemirrorState.PluginKey('suggestion');
56
- function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allowSpaces = false, prefixSpace = true, startOfLine = false, decorationTag = 'span', decorationClass = 'suggestion', command = () => null, items = () => [], render = () => ({}), allow = () => true, }) {
56
+ function Suggestion({ pluginKey = SuggestionPluginKey, editor, char = '@', allowSpaces = false, allowedPrefixes = [' '], startOfLine = false, decorationTag = 'span', decorationClass = 'suggestion', command = () => null, items = () => [], render = () => ({}), allow = () => true, }) {
57
57
  let props;
58
58
  const renderer = render === null || render === void 0 ? void 0 : render();
59
- return new prosemirrorState.Plugin({
59
+ const plugin = new prosemirrorState.Plugin({
60
60
  key: pluginKey,
61
61
  view() {
62
62
  return {
@@ -79,7 +79,7 @@
79
79
  const state = handleExit && !handleStart
80
80
  ? prev
81
81
  : next;
82
- const decorationNode = document.querySelector(`[data-decoration-id="${state.decorationId}"]`);
82
+ const decorationNode = view.dom.querySelector(`[data-decoration-id="${state.decorationId}"]`);
83
83
  props = {
84
84
  editor,
85
85
  range: state.range,
@@ -101,7 +101,7 @@
101
101
  var _a;
102
102
  // because of `items` can be asynchrounous we’ll search for the current docoration node
103
103
  const { decorationId } = (_a = this.key) === null || _a === void 0 ? void 0 : _a.getState(editor.state); // eslint-disable-line
104
- const currentDecorationNode = document.querySelector(`[data-decoration-id="${decorationId}"]`);
104
+ const currentDecorationNode = view.dom.querySelector(`[data-decoration-id="${decorationId}"]`);
105
105
  return (currentDecorationNode === null || currentDecorationNode === void 0 ? void 0 : currentDecorationNode.getBoundingClientRect()) || null;
106
106
  }
107
107
  : null,
@@ -140,13 +140,17 @@
140
140
  state: {
141
141
  // Initialize the plugin's internal state.
142
142
  init() {
143
- return {
143
+ const state = {
144
144
  active: false,
145
- range: {},
145
+ range: {
146
+ from: 0,
147
+ to: 0,
148
+ },
146
149
  query: null,
147
150
  text: null,
148
151
  composing: false,
149
152
  };
153
+ return state;
150
154
  },
151
155
  // Apply changes to the plugin state from a view transaction.
152
156
  apply(transaction, prev, oldState, state) {
@@ -161,7 +165,7 @@
161
165
  // * a composition is active (see: https://github.com/ueberdosis/tiptap/issues/1449)
162
166
  if (isEditable && (empty || editor.view.composing)) {
163
167
  // Reset active state if we just left the previous suggestion range
164
- if ((from < prev.range.from || from > prev.range.to)
168
+ if (((from < prev.range.from) || from > prev.range.to)
165
169
  && !composing
166
170
  && !prev.composing) {
167
171
  next.active = false;
@@ -170,7 +174,7 @@
170
174
  const match = findSuggestionMatch({
171
175
  char,
172
176
  allowSpaces,
173
- prefixSpace,
177
+ allowedPrefixes,
174
178
  startOfLine,
175
179
  $position: selection.$from,
176
180
  });
@@ -193,7 +197,7 @@
193
197
  // Make sure to empty the range if suggestion is inactive
194
198
  if (!next.active) {
195
199
  next.decorationId = null;
196
- next.range = {};
200
+ next.range = { from: 0, to: 0 };
197
201
  next.query = null;
198
202
  next.text = null;
199
203
  }
@@ -204,7 +208,7 @@
204
208
  // Call the keydown hook if suggestion is active.
205
209
  handleKeyDown(view, event) {
206
210
  var _a;
207
- const { active, range } = this.getState(view.state);
211
+ const { active, range } = plugin.getState(view.state);
208
212
  if (!active) {
209
213
  return false;
210
214
  }
@@ -212,7 +216,7 @@
212
216
  },
213
217
  // Setup decorator on the currently active suggestion.
214
218
  decorations(state) {
215
- const { active, range, decorationId } = this.getState(state);
219
+ const { active, range, decorationId } = plugin.getState(state);
216
220
  if (!active) {
217
221
  return null;
218
222
  }
@@ -226,6 +230,7 @@
226
230
  },
227
231
  },
228
232
  });
233
+ return plugin;
229
234
  }
230
235
 
231
236
  exports.Suggestion = Suggestion;
@@ -1 +1 @@
1
- {"version":3,"file":"tiptap-suggestion.umd.js","sources":["../src/findSuggestionMatch.ts","../src/suggestion.ts"],"sourcesContent":["import { escapeForRegEx, Range } from '@tiptap/core'\nimport { ResolvedPos } from 'prosemirror-model'\n\nexport interface Trigger {\n char: string,\n allowSpaces: boolean,\n prefixSpace: boolean,\n startOfLine: boolean,\n $position: ResolvedPos,\n}\n\nexport type SuggestionMatch = {\n range: Range,\n query: string,\n text: string,\n} | null\n\nexport function findSuggestionMatch(config: Trigger): SuggestionMatch {\n const {\n char,\n allowSpaces,\n prefixSpace,\n startOfLine,\n $position,\n } = config\n\n const escapedChar = escapeForRegEx(char)\n const suffix = new RegExp(`\\\\s${escapedChar}$`)\n const prefix = startOfLine ? '^' : ''\n const regexp = allowSpaces\n ? new RegExp(`${prefix}${escapedChar}.*?(?=\\\\s${escapedChar}|$)`, 'gm')\n : new RegExp(`${prefix}(?:^)?${escapedChar}[^\\\\s${escapedChar}]*`, 'gm')\n\n const text = $position.nodeBefore?.isText && $position.nodeBefore.text\n\n if (!text) {\n return null\n }\n\n const textFrom = $position.pos - text.length\n const match = Array.from(text.matchAll(regexp)).pop()\n\n if (!match || match.input === undefined || match.index === undefined) {\n return null\n }\n\n // JavaScript doesn't have lookbehinds. This hacks a check that first character\n // is a space or the start of the line\n const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index)\n const matchPrefixIsSpace = /^[\\s\\0]?$/.test(matchPrefix)\n\n if (prefixSpace && !matchPrefixIsSpace) {\n return null\n }\n\n // The absolute position of the match in the document\n const from = textFrom + match.index\n let to = from + match[0].length\n\n // Edge case handling; if spaces are allowed and we're directly in between\n // two triggers\n if (allowSpaces && suffix.test(text.slice(to - 1, to + 1))) {\n match[0] += ' '\n to += 1\n }\n\n // If the $position is located within the matched substring, return that range\n if (from < $position.pos && to >= $position.pos) {\n return {\n range: {\n from,\n to,\n },\n query: match[0].slice(char.length),\n text: match[0],\n }\n }\n\n return null\n}\n","import { Editor, Range } from '@tiptap/core'\nimport { EditorState, Plugin, PluginKey } from 'prosemirror-state'\nimport { Decoration, DecorationSet, EditorView } from 'prosemirror-view'\n\nimport { findSuggestionMatch } from './findSuggestionMatch'\n\nexport interface SuggestionOptions<I = any> {\n pluginKey?: PluginKey,\n editor: Editor,\n char?: string,\n allowSpaces?: boolean,\n startOfLine?: boolean,\n prefixSpace?: boolean,\n decorationTag?: string,\n decorationClass?: string,\n command?: (props: {\n editor: Editor,\n range: Range,\n props: I,\n }) => void,\n items?: (props: {\n query: string,\n editor: Editor,\n }) => I[] | Promise<I[]>,\n render?: () => {\n onBeforeStart?: (props: SuggestionProps<I>) => void\n onStart?: (props: SuggestionProps<I>) => void,\n onBeforeUpdate?: (props: SuggestionProps<I>) => void\n onUpdate?: (props: SuggestionProps<I>) => void,\n onExit?: (props: SuggestionProps<I>) => void,\n onKeyDown?: (props: SuggestionKeyDownProps) => boolean,\n },\n allow?: (props: {\n editor: Editor,\n state: EditorState,\n range: Range,\n }) => boolean,\n}\n\nexport interface SuggestionProps<I = any> {\n editor: Editor,\n range: Range,\n query: string,\n text: string,\n items: I[],\n command: (props: I) => void,\n decorationNode: Element | null,\n clientRect?: (() => DOMRect | null) | null,\n}\n\nexport interface SuggestionKeyDownProps {\n view: EditorView,\n event: KeyboardEvent,\n range: Range,\n}\n\nexport const SuggestionPluginKey = new PluginKey('suggestion')\n\nexport function Suggestion<I = any>({\n pluginKey = SuggestionPluginKey,\n editor,\n char = '@',\n allowSpaces = false,\n prefixSpace = true,\n startOfLine = false,\n decorationTag = 'span',\n decorationClass = 'suggestion',\n command = () => null,\n items = () => [],\n render = () => ({}),\n allow = () => true,\n}: SuggestionOptions<I>) {\n\n let props: SuggestionProps<I> | undefined\n const renderer = render?.()\n\n return new Plugin({\n key: pluginKey,\n\n view() {\n return {\n update: async (view, prevState) => {\n const prev = this.key?.getState(prevState)\n const next = this.key?.getState(view.state)\n\n // See how the state changed\n const moved = prev.active && next.active && prev.range.from !== next.range.from\n const started = !prev.active && next.active\n const stopped = prev.active && !next.active\n const changed = !started && !stopped && prev.query !== next.query\n const handleStart = started || moved\n const handleChange = changed && !moved\n const handleExit = stopped || moved\n\n // Cancel when suggestion isn't active\n if (!handleStart && !handleChange && !handleExit) {\n return\n }\n\n const state = handleExit && !handleStart\n ? prev\n : next\n const decorationNode = document.querySelector(`[data-decoration-id=\"${state.decorationId}\"]`)\n\n props = {\n editor,\n range: state.range,\n query: state.query,\n text: state.text,\n items: [],\n command: commandProps => {\n command({\n editor,\n range: state.range,\n props: commandProps,\n })\n },\n decorationNode,\n // virtual node for popper.js or tippy.js\n // this can be used for building popups without a DOM node\n clientRect: decorationNode\n ? () => {\n // because of `items` can be asynchrounous we’ll search for the current docoration node\n const { decorationId } = this.key?.getState(editor.state) // eslint-disable-line\n const currentDecorationNode = document.querySelector(`[data-decoration-id=\"${decorationId}\"]`)\n\n return currentDecorationNode?.getBoundingClientRect() || null\n }\n : null,\n }\n\n if (handleStart) {\n renderer?.onBeforeStart?.(props)\n }\n\n if (handleChange) {\n renderer?.onBeforeUpdate?.(props)\n }\n\n if (handleChange || handleStart) {\n props.items = await items({\n editor,\n query: state.query,\n })\n }\n\n if (handleExit) {\n renderer?.onExit?.(props)\n }\n\n if (handleChange) {\n renderer?.onUpdate?.(props)\n }\n\n if (handleStart) {\n renderer?.onStart?.(props)\n }\n },\n\n destroy: () => {\n if (!props) {\n return\n }\n\n renderer?.onExit?.(props)\n },\n }\n },\n\n state: {\n // Initialize the plugin's internal state.\n init() {\n return {\n active: false,\n range: {},\n query: null,\n text: null,\n composing: false,\n }\n },\n\n // Apply changes to the plugin state from a view transaction.\n apply(transaction, prev, oldState, state) {\n const { isEditable } = editor\n const { composing } = editor.view\n const { selection } = transaction\n const { empty, from } = selection\n const next = { ...prev }\n\n next.composing = composing\n\n // We can only be suggesting if the view is editable, and:\n // * there is no selection, or\n // * a composition is active (see: https://github.com/ueberdosis/tiptap/issues/1449)\n if (isEditable && (empty || editor.view.composing)) {\n // Reset active state if we just left the previous suggestion range\n if (\n (from < prev.range.from || from > prev.range.to)\n && !composing\n && !prev.composing\n ) {\n next.active = false\n }\n\n // Try to match against where our cursor currently is\n const match = findSuggestionMatch({\n char,\n allowSpaces,\n prefixSpace,\n startOfLine,\n $position: selection.$from,\n })\n const decorationId = `id_${Math.floor(Math.random() * 0xFFFFFFFF)}`\n\n // If we found a match, update the current state to show it\n if (match && allow({ editor, state, range: match.range })) {\n next.active = true\n next.decorationId = prev.decorationId ? prev.decorationId : decorationId\n next.range = match.range\n next.query = match.query\n next.text = match.text\n } else {\n next.active = false\n }\n } else {\n next.active = false\n }\n\n // Make sure to empty the range if suggestion is inactive\n if (!next.active) {\n next.decorationId = null\n next.range = {}\n next.query = null\n next.text = null\n }\n\n return next\n },\n },\n\n props: {\n // Call the keydown hook if suggestion is active.\n handleKeyDown(view, event) {\n const { active, range } = this.getState(view.state)\n\n if (!active) {\n return false\n }\n\n return renderer?.onKeyDown?.({ view, event, range }) || false\n },\n\n // Setup decorator on the currently active suggestion.\n decorations(state) {\n const { active, range, decorationId } = this.getState(state)\n\n if (!active) {\n return null\n }\n\n return DecorationSet.create(state.doc, [\n Decoration.inline(range.from, range.to, {\n nodeName: decorationTag,\n class: decorationClass,\n 'data-decoration-id': decorationId,\n }),\n ])\n },\n },\n })\n}\n"],"names":["escapeForRegEx","PluginKey","Plugin","DecorationSet","Decoration"],"mappings":";;;;;;EAiBM,SAAU,mBAAmB,CAAC,MAAe,EAAA;;EACjD,IAAA,MAAM,EACJ,IAAI,EACJ,WAAW,EACX,WAAW,EACX,WAAW,EACX,SAAS,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,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;kBAE5D,IAAI,CAAC,MAAM,EAAE;EACX,oBAAA,OAAO,IAAI,CAAA;EACZ,iBAAA;EAED,gBAAA,OAAOC,6BAAa,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE;sBACrCC,0BAAU,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,EAAE;EACtC,wBAAA,QAAQ,EAAE,aAAa;EACvB,wBAAA,KAAK,EAAE,eAAe;EACtB,wBAAA,oBAAoB,EAAE,YAAY;uBACnC,CAAC;EACH,iBAAA,CAAC,CAAA;eACH;EACF,SAAA;EACF,KAAA,CAAC,CAAA;EACJ;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"tiptap-suggestion.umd.js","sources":["../src/findSuggestionMatch.ts","../src/suggestion.ts"],"sourcesContent":["import { escapeForRegEx, Range } from '@tiptap/core'\nimport { ResolvedPos } from 'prosemirror-model'\n\nexport interface Trigger {\n char: string,\n allowSpaces: boolean,\n allowedPrefixes: string[] | null,\n startOfLine: boolean,\n $position: ResolvedPos,\n}\n\nexport type SuggestionMatch = {\n range: Range,\n query: string,\n text: string,\n} | null\n\nexport function findSuggestionMatch(config: Trigger): SuggestionMatch {\n const {\n char,\n allowSpaces,\n allowedPrefixes,\n startOfLine,\n $position,\n } = config\n\n const escapedChar = escapeForRegEx(char)\n const suffix = new RegExp(`\\\\s${escapedChar}$`)\n const prefix = startOfLine ? '^' : ''\n const regexp = allowSpaces\n ? new RegExp(`${prefix}${escapedChar}.*?(?=\\\\s${escapedChar}|$)`, 'gm')\n : new RegExp(`${prefix}(?:^)?${escapedChar}[^\\\\s${escapedChar}]*`, 'gm')\n\n const text = $position.nodeBefore?.isText && $position.nodeBefore.text\n\n if (!text) {\n return null\n }\n\n const textFrom = $position.pos - text.length\n const match = Array.from(text.matchAll(regexp)).pop()\n\n if (!match || match.input === undefined || match.index === undefined) {\n return null\n }\n\n // JavaScript doesn't have lookbehinds. This hacks a check that first character\n // is a space or the start of the line\n const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index)\n const matchPrefixIsAllowed = new RegExp(`^[${allowedPrefixes?.join('')}\\0]?$`).test(matchPrefix)\n\n if (allowedPrefixes !== null && !matchPrefixIsAllowed) {\n return null\n }\n\n // The absolute position of the match in the document\n const from = textFrom + match.index\n let to = from + match[0].length\n\n // Edge case handling; if spaces are allowed and we're directly in between\n // two triggers\n if (allowSpaces && suffix.test(text.slice(to - 1, to + 1))) {\n match[0] += ' '\n to += 1\n }\n\n // If the $position is located within the matched substring, return that range\n if (from < $position.pos && to >= $position.pos) {\n return {\n range: {\n from,\n to,\n },\n query: match[0].slice(char.length),\n text: match[0],\n }\n }\n\n return null\n}\n","import { Editor, Range } from '@tiptap/core'\nimport { EditorState, Plugin, PluginKey } from 'prosemirror-state'\nimport { Decoration, DecorationSet, EditorView } from 'prosemirror-view'\n\nimport { findSuggestionMatch } from './findSuggestionMatch'\n\nexport interface SuggestionOptions<I = any> {\n pluginKey?: PluginKey,\n editor: Editor,\n char?: string,\n allowSpaces?: boolean,\n allowedPrefixes?: string[] | null,\n startOfLine?: boolean,\n decorationTag?: string,\n decorationClass?: string,\n command?: (props: {\n editor: Editor,\n range: Range,\n props: I,\n }) => void,\n items?: (props: {\n query: string,\n editor: Editor,\n }) => I[] | Promise<I[]>,\n render?: () => {\n onBeforeStart?: (props: SuggestionProps<I>) => void\n onStart?: (props: SuggestionProps<I>) => void,\n onBeforeUpdate?: (props: SuggestionProps<I>) => void\n onUpdate?: (props: SuggestionProps<I>) => void,\n onExit?: (props: SuggestionProps<I>) => void,\n onKeyDown?: (props: SuggestionKeyDownProps) => boolean,\n },\n allow?: (props: {\n editor: Editor,\n state: EditorState,\n range: Range,\n }) => boolean,\n}\n\nexport interface SuggestionProps<I = any> {\n editor: Editor,\n range: Range,\n query: string,\n text: string,\n items: I[],\n command: (props: I) => void,\n decorationNode: Element | null,\n clientRect?: (() => DOMRect | null) | null,\n}\n\nexport interface SuggestionKeyDownProps {\n view: EditorView,\n event: KeyboardEvent,\n range: Range,\n}\n\nexport const SuggestionPluginKey = new PluginKey('suggestion')\n\nexport function Suggestion<I = any>({\n pluginKey = SuggestionPluginKey,\n editor,\n char = '@',\n allowSpaces = false,\n allowedPrefixes = [' '],\n startOfLine = false,\n decorationTag = 'span',\n decorationClass = 'suggestion',\n command = () => null,\n items = () => [],\n render = () => ({}),\n allow = () => true,\n}: SuggestionOptions<I>) {\n\n let props: SuggestionProps<I> | undefined\n const renderer = render?.()\n\n const plugin: Plugin<any> = new Plugin({\n key: pluginKey,\n\n view() {\n return {\n update: async (view, prevState) => {\n const prev = this.key?.getState(prevState)\n const next = this.key?.getState(view.state)\n\n // See how the state changed\n const moved = prev.active && next.active && prev.range.from !== next.range.from\n const started = !prev.active && next.active\n const stopped = prev.active && !next.active\n const changed = !started && !stopped && prev.query !== next.query\n const handleStart = started || moved\n const handleChange = changed && !moved\n const handleExit = stopped || moved\n\n // Cancel when suggestion isn't active\n if (!handleStart && !handleChange && !handleExit) {\n return\n }\n\n const state = handleExit && !handleStart\n ? prev\n : next\n const decorationNode = view.dom.querySelector(`[data-decoration-id=\"${state.decorationId}\"]`)\n\n props = {\n editor,\n range: state.range,\n query: state.query,\n text: state.text,\n items: [],\n command: commandProps => {\n command({\n editor,\n range: state.range,\n props: commandProps,\n })\n },\n decorationNode,\n // virtual node for popper.js or tippy.js\n // this can be used for building popups without a DOM node\n clientRect: decorationNode\n ? () => {\n // because of `items` can be asynchrounous we’ll search for the current docoration node\n const { decorationId } = this.key?.getState(editor.state) // eslint-disable-line\n const currentDecorationNode = view.dom.querySelector(`[data-decoration-id=\"${decorationId}\"]`)\n\n return currentDecorationNode?.getBoundingClientRect() || null\n }\n : null,\n }\n\n if (handleStart) {\n renderer?.onBeforeStart?.(props)\n }\n\n if (handleChange) {\n renderer?.onBeforeUpdate?.(props)\n }\n\n if (handleChange || handleStart) {\n props.items = await items({\n editor,\n query: state.query,\n })\n }\n\n if (handleExit) {\n renderer?.onExit?.(props)\n }\n\n if (handleChange) {\n renderer?.onUpdate?.(props)\n }\n\n if (handleStart) {\n renderer?.onStart?.(props)\n }\n },\n\n destroy: () => {\n if (!props) {\n return\n }\n\n renderer?.onExit?.(props)\n },\n }\n },\n\n state: {\n // Initialize the plugin's internal state.\n init() {\n const state: {\n active: boolean,\n range: Range,\n query: null | string\n text: null | string\n composing: boolean\n decorationId?: string | null\n } = {\n active: false,\n range: {\n from: 0,\n to: 0,\n },\n query: null,\n text: null,\n composing: false,\n }\n\n return state\n },\n\n // Apply changes to the plugin state from a view transaction.\n apply(transaction, prev, oldState, state) {\n const { isEditable } = editor\n const { composing } = editor.view\n const { selection } = transaction\n const { empty, from } = selection\n const next = { ...prev }\n\n next.composing = composing\n\n // We can only be suggesting if the view is editable, and:\n // * there is no selection, or\n // * a composition is active (see: https://github.com/ueberdosis/tiptap/issues/1449)\n if (isEditable && (empty || editor.view.composing)) {\n // Reset active state if we just left the previous suggestion range\n if (\n ((from < prev.range.from) || from > prev.range.to)\n && !composing\n && !prev.composing\n ) {\n next.active = false\n }\n\n // Try to match against where our cursor currently is\n const match = findSuggestionMatch({\n char,\n allowSpaces,\n allowedPrefixes,\n startOfLine,\n $position: selection.$from,\n })\n const decorationId = `id_${Math.floor(Math.random() * 0xFFFFFFFF)}`\n\n // If we found a match, update the current state to show it\n if (match && allow({ editor, state, range: match.range })) {\n next.active = true\n next.decorationId = prev.decorationId ? prev.decorationId : decorationId\n next.range = match.range\n next.query = match.query\n next.text = match.text\n } else {\n next.active = false\n }\n } else {\n next.active = false\n }\n\n // Make sure to empty the range if suggestion is inactive\n if (!next.active) {\n next.decorationId = null\n next.range = { from: 0, to: 0 }\n next.query = null\n next.text = null\n }\n\n return next\n },\n },\n\n props: {\n // Call the keydown hook if suggestion is active.\n handleKeyDown(view, event) {\n const { active, range } = plugin.getState(view.state)\n\n if (!active) {\n return false\n }\n\n return renderer?.onKeyDown?.({ view, event, range }) || false\n },\n\n // Setup decorator on the currently active suggestion.\n decorations(state) {\n const { active, range, decorationId } = plugin.getState(state)\n\n if (!active) {\n return null\n }\n\n return DecorationSet.create(state.doc, [\n Decoration.inline(range.from, range.to, {\n nodeName: decorationTag,\n class: decorationClass,\n 'data-decoration-id': decorationId,\n }),\n ])\n },\n },\n })\n\n return plugin\n}\n"],"names":["escapeForRegEx","PluginKey","Plugin","DecorationSet","Decoration"],"mappings":";;;;;;EAiBM,SAAU,mBAAmB,CAAC,MAAe,EAAA;;EACjD,IAAA,MAAM,EACJ,IAAI,EACJ,WAAW,EACX,eAAe,EACf,WAAW,EACX,SAAS,GACV,GAAG,MAAM,CAAA;EAEV,IAAA,MAAM,WAAW,GAAGA,mBAAc,CAAC,IAAI,CAAC,CAAA;MACxC,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,CAAM,GAAA,EAAA,WAAW,CAAG,CAAA,CAAA,CAAC,CAAA;MAC/C,MAAM,MAAM,GAAG,WAAW,GAAG,GAAG,GAAG,EAAE,CAAA;MACrC,MAAM,MAAM,GAAG,WAAW;EACxB,UAAE,IAAI,MAAM,CAAC,CAAG,EAAA,MAAM,CAAG,EAAA,WAAW,CAAY,SAAA,EAAA,WAAW,CAAK,GAAA,CAAA,EAAE,IAAI,CAAC;EACvE,UAAE,IAAI,MAAM,CAAC,GAAG,MAAM,CAAA,MAAA,EAAS,WAAW,CAAA,KAAA,EAAQ,WAAW,CAAA,EAAA,CAAI,EAAE,IAAI,CAAC,CAAA;EAE1E,IAAA,MAAM,IAAI,GAAG,CAAA,CAAA,EAAA,GAAA,SAAS,CAAC,UAAU,MAAE,IAAA,IAAA,EAAA,KAAA,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,EAAA,CAAA,MAAM,KAAI,SAAS,CAAC,UAAU,CAAC,IAAI,CAAA;MAEtE,IAAI,CAAC,IAAI,EAAE;EACT,QAAA,OAAO,IAAI,CAAA;EACZ,KAAA;MAED,MAAM,Q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ACH;EACF,SAAA;EACF,KAAA,CAAC,CAAA;EAEF,IAAA,OAAO,MAAM,CAAA;EACf;;;;;;;;;;;;;"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tiptap/suggestion",
3
3
  "description": "suggestion plugin for tiptap",
4
- "version": "2.0.0-beta.95",
4
+ "version": "2.0.0-beta.98",
5
5
  "homepage": "https://tiptap.dev",
6
6
  "keywords": [
7
7
  "tiptap",
@@ -20,23 +20,18 @@
20
20
  "src",
21
21
  "dist"
22
22
  ],
23
- "devDependencies": {
24
- "@types/prosemirror-model": "^1.16.1",
25
- "@types/prosemirror-state": "^1.2.8",
26
- "@types/prosemirror-view": "^1.23.1"
27
- },
28
23
  "peerDependencies": {
29
24
  "@tiptap/core": "^2.0.0-beta.1"
30
25
  },
31
26
  "dependencies": {
32
- "prosemirror-model": "^1.16.1",
33
- "prosemirror-state": "^1.3.4",
34
- "prosemirror-view": "^1.23.6"
27
+ "prosemirror-model": "1.18.1",
28
+ "prosemirror-state": "1.4.1",
29
+ "prosemirror-view": "1.26.2"
35
30
  },
36
31
  "repository": {
37
32
  "type": "git",
38
33
  "url": "https://github.com/ueberdosis/tiptap",
39
34
  "directory": "packages/suggestion"
40
35
  },
41
- "gitHead": "591c0807a2ab5c34b4b7fe12c12511fe4f493ebd"
36
+ "gitHead": "5bcfb322ceb30575430bd8797f2787d65de80f2c"
42
37
  }
@@ -4,7 +4,7 @@ import { ResolvedPos } from 'prosemirror-model'
4
4
  export interface Trigger {
5
5
  char: string,
6
6
  allowSpaces: boolean,
7
- prefixSpace: boolean,
7
+ allowedPrefixes: string[] | null,
8
8
  startOfLine: boolean,
9
9
  $position: ResolvedPos,
10
10
  }
@@ -19,7 +19,7 @@ export function findSuggestionMatch(config: Trigger): SuggestionMatch {
19
19
  const {
20
20
  char,
21
21
  allowSpaces,
22
- prefixSpace,
22
+ allowedPrefixes,
23
23
  startOfLine,
24
24
  $position,
25
25
  } = config
@@ -47,9 +47,9 @@ export function findSuggestionMatch(config: Trigger): SuggestionMatch {
47
47
  // JavaScript doesn't have lookbehinds. This hacks a check that first character
48
48
  // is a space or the start of the line
49
49
  const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index)
50
- const matchPrefixIsSpace = /^[\s\0]?$/.test(matchPrefix)
50
+ const matchPrefixIsAllowed = new RegExp(`^[${allowedPrefixes?.join('')}\0]?$`).test(matchPrefix)
51
51
 
52
- if (prefixSpace && !matchPrefixIsSpace) {
52
+ if (allowedPrefixes !== null && !matchPrefixIsAllowed) {
53
53
  return null
54
54
  }
55
55
 
package/src/suggestion.ts CHANGED
@@ -9,8 +9,8 @@ export interface SuggestionOptions<I = any> {
9
9
  editor: Editor,
10
10
  char?: string,
11
11
  allowSpaces?: boolean,
12
+ allowedPrefixes?: string[] | null,
12
13
  startOfLine?: boolean,
13
- prefixSpace?: boolean,
14
14
  decorationTag?: string,
15
15
  decorationClass?: string,
16
16
  command?: (props: {
@@ -61,7 +61,7 @@ export function Suggestion<I = any>({
61
61
  editor,
62
62
  char = '@',
63
63
  allowSpaces = false,
64
- prefixSpace = true,
64
+ allowedPrefixes = [' '],
65
65
  startOfLine = false,
66
66
  decorationTag = 'span',
67
67
  decorationClass = 'suggestion',
@@ -74,7 +74,7 @@ export function Suggestion<I = any>({
74
74
  let props: SuggestionProps<I> | undefined
75
75
  const renderer = render?.()
76
76
 
77
- return new Plugin({
77
+ const plugin: Plugin<any> = new Plugin({
78
78
  key: pluginKey,
79
79
 
80
80
  view() {
@@ -100,7 +100,7 @@ export function Suggestion<I = any>({
100
100
  const state = handleExit && !handleStart
101
101
  ? prev
102
102
  : next
103
- const decorationNode = document.querySelector(`[data-decoration-id="${state.decorationId}"]`)
103
+ const decorationNode = view.dom.querySelector(`[data-decoration-id="${state.decorationId}"]`)
104
104
 
105
105
  props = {
106
106
  editor,
@@ -122,7 +122,7 @@ export function Suggestion<I = any>({
122
122
  ? () => {
123
123
  // because of `items` can be asynchrounous we’ll search for the current docoration node
124
124
  const { decorationId } = this.key?.getState(editor.state) // eslint-disable-line
125
- const currentDecorationNode = document.querySelector(`[data-decoration-id="${decorationId}"]`)
125
+ const currentDecorationNode = view.dom.querySelector(`[data-decoration-id="${decorationId}"]`)
126
126
 
127
127
  return currentDecorationNode?.getBoundingClientRect() || null
128
128
  }
@@ -170,13 +170,25 @@ export function Suggestion<I = any>({
170
170
  state: {
171
171
  // Initialize the plugin's internal state.
172
172
  init() {
173
- return {
173
+ const state: {
174
+ active: boolean,
175
+ range: Range,
176
+ query: null | string
177
+ text: null | string
178
+ composing: boolean
179
+ decorationId?: string | null
180
+ } = {
174
181
  active: false,
175
- range: {},
182
+ range: {
183
+ from: 0,
184
+ to: 0,
185
+ },
176
186
  query: null,
177
187
  text: null,
178
188
  composing: false,
179
189
  }
190
+
191
+ return state
180
192
  },
181
193
 
182
194
  // Apply changes to the plugin state from a view transaction.
@@ -195,7 +207,7 @@ export function Suggestion<I = any>({
195
207
  if (isEditable && (empty || editor.view.composing)) {
196
208
  // Reset active state if we just left the previous suggestion range
197
209
  if (
198
- (from < prev.range.from || from > prev.range.to)
210
+ ((from < prev.range.from) || from > prev.range.to)
199
211
  && !composing
200
212
  && !prev.composing
201
213
  ) {
@@ -206,7 +218,7 @@ export function Suggestion<I = any>({
206
218
  const match = findSuggestionMatch({
207
219
  char,
208
220
  allowSpaces,
209
- prefixSpace,
221
+ allowedPrefixes,
210
222
  startOfLine,
211
223
  $position: selection.$from,
212
224
  })
@@ -229,7 +241,7 @@ export function Suggestion<I = any>({
229
241
  // Make sure to empty the range if suggestion is inactive
230
242
  if (!next.active) {
231
243
  next.decorationId = null
232
- next.range = {}
244
+ next.range = { from: 0, to: 0 }
233
245
  next.query = null
234
246
  next.text = null
235
247
  }
@@ -241,7 +253,7 @@ export function Suggestion<I = any>({
241
253
  props: {
242
254
  // Call the keydown hook if suggestion is active.
243
255
  handleKeyDown(view, event) {
244
- const { active, range } = this.getState(view.state)
256
+ const { active, range } = plugin.getState(view.state)
245
257
 
246
258
  if (!active) {
247
259
  return false
@@ -252,7 +264,7 @@ export function Suggestion<I = any>({
252
264
 
253
265
  // Setup decorator on the currently active suggestion.
254
266
  decorations(state) {
255
- const { active, range, decorationId } = this.getState(state)
267
+ const { active, range, decorationId } = plugin.getState(state)
256
268
 
257
269
  if (!active) {
258
270
  return null
@@ -268,4 +280,6 @@ export function Suggestion<I = any>({
268
280
  },
269
281
  },
270
282
  })
283
+
284
+ return plugin
271
285
  }