2022-06-08 20:10:25 +08:00
|
|
|
|
import { Editor, Range } from '@tiptap/core'
|
2023-02-03 00:37:33 +08:00
|
|
|
|
import { EditorState, Plugin, PluginKey } from '@tiptap/pm/state'
|
|
|
|
|
import { Decoration, DecorationSet, EditorView } from '@tiptap/pm/view'
|
2022-05-27 18:31:46 +08:00
|
|
|
|
|
2023-10-10 02:46:14 +08:00
|
|
|
|
import { findSuggestionMatch as defaultFindSuggestionMatch } from './findSuggestionMatch.js'
|
2020-12-17 00:47:17 +08:00
|
|
|
|
|
fix: types for Suggestion `command`, allowing generic overrides (#4136)
* Fix typing for Suggestion `command` with new MentionAttrs generic
As of
https://github.com/ueberdosis/tiptap/commit/7cae9673f0086973b4d31e1343375ed5ad04ee0a,
new generics were added for Suggestion options and props. However,
there is a subtle bug in the current typing: the object selected with
the suggestion `command` need not have the same types as the `items` in
the suggestion options. For instance, in Tiptap's official demo
https://tiptap.dev/api/nodes/mention, the suggestion `items` are all
`string`s, but the selected Mention is of type `{id: string}` (which are
the attributes of the Mention node, as the Mention extension requires):
```ts
const selectItem = index => {
const item = props.items[index]
if (item) {
props.command({ id: item })
}
}
```
i.e., there should be no restriction that when you select something with
the suggestion `command`, it must use the identical structure as the
suggested items. When using the suggestion plugin with the Mention
extension, for instance, the value passed to the SuggestionProps
`props.command()` function must be a `Record<string, any>`, as it's
directly/exclusively used to set the `attrs` of a `Node` via
`insertContentAt` (and you need not use that shape for suggestion
options, as in the Tiptap example above):
https://github.com/ueberdosis/tiptap/blob/44996d60bebd80f3dcc897909f59d83a0eff6337/packages/extension-mention/src/mention.ts#L42
https://github.com/ueberdosis/tiptap/blob/f8695073968c5c6865ad8faf05351020abb2a3cc/packages/core/src/types.ts#L79
This fixes the typing so that suggestions can correctly refer separately
to their own items (of any type), while ensuring the `command`ed item be
of whatever type is necessary (and so in the Mention context, could be
restricted further).
* Add generics to override selected suggestion type
---------
Co-authored-by: Steven DeMartini <sjdemartini@users.noreply.github.com>
2024-05-17 11:12:04 +08:00
|
|
|
|
export interface SuggestionOptions<I = any, TSelected = any> {
|
2024-05-11 20:30:44 +08:00
|
|
|
|
/**
|
|
|
|
|
* The plugin key for the suggestion plugin.
|
|
|
|
|
* @default 'suggestion'
|
|
|
|
|
* @example 'mention'
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
pluginKey?: PluginKey
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The editor instance.
|
|
|
|
|
* @default null
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
editor: Editor
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The character that triggers the suggestion.
|
|
|
|
|
* @default '@'
|
|
|
|
|
* @example '#'
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
char?: string
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Allow spaces in the suggestion query.
|
|
|
|
|
* @default false
|
|
|
|
|
* @example true
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
allowSpaces?: boolean
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Allow prefixes in the suggestion query.
|
|
|
|
|
* @default [' ']
|
|
|
|
|
* @example [' ', '@']
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
allowedPrefixes?: string[] | null
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Only match suggestions at the start of the line.
|
|
|
|
|
* @default false
|
|
|
|
|
* @example true
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
startOfLine?: boolean
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The tag name of the decoration node.
|
|
|
|
|
* @default 'span'
|
|
|
|
|
* @example 'div'
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
decorationTag?: string
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The class name of the decoration node.
|
|
|
|
|
* @default 'suggestion'
|
|
|
|
|
* @example 'mention'
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
decorationClass?: string
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A function that is called when a suggestion is selected.
|
|
|
|
|
* @param props The props object.
|
|
|
|
|
* @param props.editor The editor instance.
|
|
|
|
|
* @param props.range The range of the suggestion.
|
|
|
|
|
* @param props.props The props of the selected suggestion.
|
|
|
|
|
* @returns void
|
|
|
|
|
* @example ({ editor, range, props }) => { props.command(props.props) }
|
|
|
|
|
*/
|
fix: types for Suggestion `command`, allowing generic overrides (#4136)
* Fix typing for Suggestion `command` with new MentionAttrs generic
As of
https://github.com/ueberdosis/tiptap/commit/7cae9673f0086973b4d31e1343375ed5ad04ee0a,
new generics were added for Suggestion options and props. However,
there is a subtle bug in the current typing: the object selected with
the suggestion `command` need not have the same types as the `items` in
the suggestion options. For instance, in Tiptap's official demo
https://tiptap.dev/api/nodes/mention, the suggestion `items` are all
`string`s, but the selected Mention is of type `{id: string}` (which are
the attributes of the Mention node, as the Mention extension requires):
```ts
const selectItem = index => {
const item = props.items[index]
if (item) {
props.command({ id: item })
}
}
```
i.e., there should be no restriction that when you select something with
the suggestion `command`, it must use the identical structure as the
suggested items. When using the suggestion plugin with the Mention
extension, for instance, the value passed to the SuggestionProps
`props.command()` function must be a `Record<string, any>`, as it's
directly/exclusively used to set the `attrs` of a `Node` via
`insertContentAt` (and you need not use that shape for suggestion
options, as in the Tiptap example above):
https://github.com/ueberdosis/tiptap/blob/44996d60bebd80f3dcc897909f59d83a0eff6337/packages/extension-mention/src/mention.ts#L42
https://github.com/ueberdosis/tiptap/blob/f8695073968c5c6865ad8faf05351020abb2a3cc/packages/core/src/types.ts#L79
This fixes the typing so that suggestions can correctly refer separately
to their own items (of any type), while ensuring the `command`ed item be
of whatever type is necessary (and so in the Mention context, could be
restricted further).
* Add generics to override selected suggestion type
---------
Co-authored-by: Steven DeMartini <sjdemartini@users.noreply.github.com>
2024-05-17 11:12:04 +08:00
|
|
|
|
command?: (props: { editor: Editor; range: Range; props: TSelected }) => void
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A function that returns the suggestion items in form of an array.
|
|
|
|
|
* @param props The props object.
|
|
|
|
|
* @param props.editor The editor instance.
|
|
|
|
|
* @param props.query The current suggestion query.
|
|
|
|
|
* @returns An array of suggestion items.
|
|
|
|
|
* @example ({ editor, query }) => [{ id: 1, label: 'John Doe' }]
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
items?: (props: { query: string; editor: Editor }) => I[] | Promise<I[]>
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The render function for the suggestion.
|
|
|
|
|
* @returns An object with render functions.
|
|
|
|
|
*/
|
2021-01-19 06:41:38 +08:00
|
|
|
|
render?: () => {
|
fix: types for Suggestion `command`, allowing generic overrides (#4136)
* Fix typing for Suggestion `command` with new MentionAttrs generic
As of
https://github.com/ueberdosis/tiptap/commit/7cae9673f0086973b4d31e1343375ed5ad04ee0a,
new generics were added for Suggestion options and props. However,
there is a subtle bug in the current typing: the object selected with
the suggestion `command` need not have the same types as the `items` in
the suggestion options. For instance, in Tiptap's official demo
https://tiptap.dev/api/nodes/mention, the suggestion `items` are all
`string`s, but the selected Mention is of type `{id: string}` (which are
the attributes of the Mention node, as the Mention extension requires):
```ts
const selectItem = index => {
const item = props.items[index]
if (item) {
props.command({ id: item })
}
}
```
i.e., there should be no restriction that when you select something with
the suggestion `command`, it must use the identical structure as the
suggested items. When using the suggestion plugin with the Mention
extension, for instance, the value passed to the SuggestionProps
`props.command()` function must be a `Record<string, any>`, as it's
directly/exclusively used to set the `attrs` of a `Node` via
`insertContentAt` (and you need not use that shape for suggestion
options, as in the Tiptap example above):
https://github.com/ueberdosis/tiptap/blob/44996d60bebd80f3dcc897909f59d83a0eff6337/packages/extension-mention/src/mention.ts#L42
https://github.com/ueberdosis/tiptap/blob/f8695073968c5c6865ad8faf05351020abb2a3cc/packages/core/src/types.ts#L79
This fixes the typing so that suggestions can correctly refer separately
to their own items (of any type), while ensuring the `command`ed item be
of whatever type is necessary (and so in the Mention context, could be
restricted further).
* Add generics to override selected suggestion type
---------
Co-authored-by: Steven DeMartini <sjdemartini@users.noreply.github.com>
2024-05-17 11:12:04 +08:00
|
|
|
|
onBeforeStart?: (props: SuggestionProps<I, TSelected>) => void;
|
|
|
|
|
onStart?: (props: SuggestionProps<I, TSelected>) => void;
|
|
|
|
|
onBeforeUpdate?: (props: SuggestionProps<I, TSelected>) => void;
|
|
|
|
|
onUpdate?: (props: SuggestionProps<I, TSelected>) => void;
|
|
|
|
|
onExit?: (props: SuggestionProps<I, TSelected>) => void;
|
|
|
|
|
onKeyDown?: (props: SuggestionKeyDownProps) => boolean;
|
2023-02-03 00:37:33 +08:00
|
|
|
|
}
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A function that returns a boolean to indicate if the suggestion should be active.
|
|
|
|
|
* @param props The props object.
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
allow?: (props: { editor: Editor; state: EditorState; range: Range }) => boolean
|
2023-10-10 02:46:14 +08:00
|
|
|
|
findSuggestionMatch?: typeof defaultFindSuggestionMatch
|
2021-01-15 22:58:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
fix: types for Suggestion `command`, allowing generic overrides (#4136)
* Fix typing for Suggestion `command` with new MentionAttrs generic
As of
https://github.com/ueberdosis/tiptap/commit/7cae9673f0086973b4d31e1343375ed5ad04ee0a,
new generics were added for Suggestion options and props. However,
there is a subtle bug in the current typing: the object selected with
the suggestion `command` need not have the same types as the `items` in
the suggestion options. For instance, in Tiptap's official demo
https://tiptap.dev/api/nodes/mention, the suggestion `items` are all
`string`s, but the selected Mention is of type `{id: string}` (which are
the attributes of the Mention node, as the Mention extension requires):
```ts
const selectItem = index => {
const item = props.items[index]
if (item) {
props.command({ id: item })
}
}
```
i.e., there should be no restriction that when you select something with
the suggestion `command`, it must use the identical structure as the
suggested items. When using the suggestion plugin with the Mention
extension, for instance, the value passed to the SuggestionProps
`props.command()` function must be a `Record<string, any>`, as it's
directly/exclusively used to set the `attrs` of a `Node` via
`insertContentAt` (and you need not use that shape for suggestion
options, as in the Tiptap example above):
https://github.com/ueberdosis/tiptap/blob/44996d60bebd80f3dcc897909f59d83a0eff6337/packages/extension-mention/src/mention.ts#L42
https://github.com/ueberdosis/tiptap/blob/f8695073968c5c6865ad8faf05351020abb2a3cc/packages/core/src/types.ts#L79
This fixes the typing so that suggestions can correctly refer separately
to their own items (of any type), while ensuring the `command`ed item be
of whatever type is necessary (and so in the Mention context, could be
restricted further).
* Add generics to override selected suggestion type
---------
Co-authored-by: Steven DeMartini <sjdemartini@users.noreply.github.com>
2024-05-17 11:12:04 +08:00
|
|
|
|
export interface SuggestionProps<I = any, TSelected = any> {
|
2024-05-11 20:30:44 +08:00
|
|
|
|
/**
|
|
|
|
|
* The editor instance.
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
editor: Editor
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The range of the suggestion.
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
range: Range
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The current suggestion query.
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
query: string
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The current suggestion text.
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
text: string
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The suggestion items array.
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
items: I[]
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A function that is called when a suggestion is selected.
|
|
|
|
|
* @param props The props object.
|
|
|
|
|
* @returns void
|
|
|
|
|
*/
|
fix: types for Suggestion `command`, allowing generic overrides (#4136)
* Fix typing for Suggestion `command` with new MentionAttrs generic
As of
https://github.com/ueberdosis/tiptap/commit/7cae9673f0086973b4d31e1343375ed5ad04ee0a,
new generics were added for Suggestion options and props. However,
there is a subtle bug in the current typing: the object selected with
the suggestion `command` need not have the same types as the `items` in
the suggestion options. For instance, in Tiptap's official demo
https://tiptap.dev/api/nodes/mention, the suggestion `items` are all
`string`s, but the selected Mention is of type `{id: string}` (which are
the attributes of the Mention node, as the Mention extension requires):
```ts
const selectItem = index => {
const item = props.items[index]
if (item) {
props.command({ id: item })
}
}
```
i.e., there should be no restriction that when you select something with
the suggestion `command`, it must use the identical structure as the
suggested items. When using the suggestion plugin with the Mention
extension, for instance, the value passed to the SuggestionProps
`props.command()` function must be a `Record<string, any>`, as it's
directly/exclusively used to set the `attrs` of a `Node` via
`insertContentAt` (and you need not use that shape for suggestion
options, as in the Tiptap example above):
https://github.com/ueberdosis/tiptap/blob/44996d60bebd80f3dcc897909f59d83a0eff6337/packages/extension-mention/src/mention.ts#L42
https://github.com/ueberdosis/tiptap/blob/f8695073968c5c6865ad8faf05351020abb2a3cc/packages/core/src/types.ts#L79
This fixes the typing so that suggestions can correctly refer separately
to their own items (of any type), while ensuring the `command`ed item be
of whatever type is necessary (and so in the Mention context, could be
restricted further).
* Add generics to override selected suggestion type
---------
Co-authored-by: Steven DeMartini <sjdemartini@users.noreply.github.com>
2024-05-17 11:12:04 +08:00
|
|
|
|
command: (props: TSelected) => void
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The decoration node HTML element
|
|
|
|
|
* @default null
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
decorationNode: Element | null
|
2024-05-11 20:30:44 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The function that returns the client rect
|
|
|
|
|
* @default null
|
|
|
|
|
* @example () => new DOMRect(0, 0, 0, 0)
|
|
|
|
|
*/
|
2023-02-03 00:37:33 +08:00
|
|
|
|
clientRect?: (() => DOMRect | null) | null
|
2021-01-19 17:09:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface SuggestionKeyDownProps {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
view: EditorView
|
|
|
|
|
event: KeyboardEvent
|
|
|
|
|
range: Range
|
2021-01-19 17:09:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-13 19:14:54 +08:00
|
|
|
|
export const SuggestionPluginKey = new PluginKey('suggestion')
|
|
|
|
|
|
2024-05-11 20:30:44 +08:00
|
|
|
|
/**
|
|
|
|
|
* This utility allows you to create suggestions.
|
|
|
|
|
* @see https://tiptap.dev/api/utilities/suggestion
|
|
|
|
|
*/
|
fix: types for Suggestion `command`, allowing generic overrides (#4136)
* Fix typing for Suggestion `command` with new MentionAttrs generic
As of
https://github.com/ueberdosis/tiptap/commit/7cae9673f0086973b4d31e1343375ed5ad04ee0a,
new generics were added for Suggestion options and props. However,
there is a subtle bug in the current typing: the object selected with
the suggestion `command` need not have the same types as the `items` in
the suggestion options. For instance, in Tiptap's official demo
https://tiptap.dev/api/nodes/mention, the suggestion `items` are all
`string`s, but the selected Mention is of type `{id: string}` (which are
the attributes of the Mention node, as the Mention extension requires):
```ts
const selectItem = index => {
const item = props.items[index]
if (item) {
props.command({ id: item })
}
}
```
i.e., there should be no restriction that when you select something with
the suggestion `command`, it must use the identical structure as the
suggested items. When using the suggestion plugin with the Mention
extension, for instance, the value passed to the SuggestionProps
`props.command()` function must be a `Record<string, any>`, as it's
directly/exclusively used to set the `attrs` of a `Node` via
`insertContentAt` (and you need not use that shape for suggestion
options, as in the Tiptap example above):
https://github.com/ueberdosis/tiptap/blob/44996d60bebd80f3dcc897909f59d83a0eff6337/packages/extension-mention/src/mention.ts#L42
https://github.com/ueberdosis/tiptap/blob/f8695073968c5c6865ad8faf05351020abb2a3cc/packages/core/src/types.ts#L79
This fixes the typing so that suggestions can correctly refer separately
to their own items (of any type), while ensuring the `command`ed item be
of whatever type is necessary (and so in the Mention context, could be
restricted further).
* Add generics to override selected suggestion type
---------
Co-authored-by: Steven DeMartini <sjdemartini@users.noreply.github.com>
2024-05-17 11:12:04 +08:00
|
|
|
|
export function Suggestion<I = any, TSelected = any>({
|
2021-08-13 19:14:54 +08:00
|
|
|
|
pluginKey = SuggestionPluginKey,
|
2021-01-19 17:09:32 +08:00
|
|
|
|
editor,
|
2021-01-15 16:25:50 +08:00
|
|
|
|
char = '@',
|
|
|
|
|
allowSpaces = false,
|
2022-11-04 19:32:01 +08:00
|
|
|
|
allowedPrefixes = [' '],
|
2021-01-15 16:25:50 +08:00
|
|
|
|
startOfLine = false,
|
2021-01-20 17:47:31 +08:00
|
|
|
|
decorationTag = 'span',
|
|
|
|
|
decorationClass = 'suggestion',
|
2021-01-15 22:58:39 +08:00
|
|
|
|
command = () => null,
|
|
|
|
|
items = () => [],
|
2021-01-19 06:41:38 +08:00
|
|
|
|
render = () => ({}),
|
2021-02-08 03:38:33 +08:00
|
|
|
|
allow = () => true,
|
2023-10-10 02:46:14 +08:00
|
|
|
|
findSuggestionMatch = defaultFindSuggestionMatch,
|
fix: types for Suggestion `command`, allowing generic overrides (#4136)
* Fix typing for Suggestion `command` with new MentionAttrs generic
As of
https://github.com/ueberdosis/tiptap/commit/7cae9673f0086973b4d31e1343375ed5ad04ee0a,
new generics were added for Suggestion options and props. However,
there is a subtle bug in the current typing: the object selected with
the suggestion `command` need not have the same types as the `items` in
the suggestion options. For instance, in Tiptap's official demo
https://tiptap.dev/api/nodes/mention, the suggestion `items` are all
`string`s, but the selected Mention is of type `{id: string}` (which are
the attributes of the Mention node, as the Mention extension requires):
```ts
const selectItem = index => {
const item = props.items[index]
if (item) {
props.command({ id: item })
}
}
```
i.e., there should be no restriction that when you select something with
the suggestion `command`, it must use the identical structure as the
suggested items. When using the suggestion plugin with the Mention
extension, for instance, the value passed to the SuggestionProps
`props.command()` function must be a `Record<string, any>`, as it's
directly/exclusively used to set the `attrs` of a `Node` via
`insertContentAt` (and you need not use that shape for suggestion
options, as in the Tiptap example above):
https://github.com/ueberdosis/tiptap/blob/44996d60bebd80f3dcc897909f59d83a0eff6337/packages/extension-mention/src/mention.ts#L42
https://github.com/ueberdosis/tiptap/blob/f8695073968c5c6865ad8faf05351020abb2a3cc/packages/core/src/types.ts#L79
This fixes the typing so that suggestions can correctly refer separately
to their own items (of any type), while ensuring the `command`ed item be
of whatever type is necessary (and so in the Mention context, could be
restricted further).
* Add generics to override selected suggestion type
---------
Co-authored-by: Steven DeMartini <sjdemartini@users.noreply.github.com>
2024-05-17 11:12:04 +08:00
|
|
|
|
}: SuggestionOptions<I, TSelected>) {
|
|
|
|
|
let props: SuggestionProps<I, TSelected> | undefined
|
2021-01-19 06:41:38 +08:00
|
|
|
|
const renderer = render?.()
|
2020-12-17 00:47:17 +08:00
|
|
|
|
|
2022-06-20 17:45:37 +08:00
|
|
|
|
const plugin: Plugin<any> = new Plugin({
|
2021-08-13 19:14:54 +08:00
|
|
|
|
key: pluginKey,
|
2020-12-17 00:47:17 +08:00
|
|
|
|
|
|
|
|
|
view() {
|
|
|
|
|
return {
|
|
|
|
|
update: async (view, prevState) => {
|
|
|
|
|
const prev = this.key?.getState(prevState)
|
|
|
|
|
const next = this.key?.getState(view.state)
|
|
|
|
|
|
|
|
|
|
// See how the state changed
|
|
|
|
|
const moved = prev.active && next.active && prev.range.from !== next.range.from
|
|
|
|
|
const started = !prev.active && next.active
|
|
|
|
|
const stopped = prev.active && !next.active
|
|
|
|
|
const changed = !started && !stopped && prev.query !== next.query
|
|
|
|
|
const handleStart = started || moved
|
|
|
|
|
const handleChange = changed && !moved
|
|
|
|
|
const handleExit = stopped || moved
|
|
|
|
|
|
|
|
|
|
// Cancel when suggestion isn't active
|
|
|
|
|
if (!handleStart && !handleChange && !handleExit) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 00:37:33 +08:00
|
|
|
|
const state = handleExit && !handleStart ? prev : next
|
|
|
|
|
const decorationNode = view.dom.querySelector(
|
|
|
|
|
`[data-decoration-id="${state.decorationId}"]`,
|
|
|
|
|
)
|
2021-12-05 06:24:21 +08:00
|
|
|
|
|
|
|
|
|
props = {
|
2021-01-19 17:09:32 +08:00
|
|
|
|
editor,
|
2020-12-17 00:47:17 +08:00
|
|
|
|
range: state.range,
|
|
|
|
|
query: state.query,
|
|
|
|
|
text: state.text,
|
2022-03-11 21:20:44 +08:00
|
|
|
|
items: [],
|
2021-01-21 19:28:58 +08:00
|
|
|
|
command: commandProps => {
|
2024-04-07 01:02:55 +08:00
|
|
|
|
return command({
|
2021-01-20 16:23:44 +08:00
|
|
|
|
editor,
|
|
|
|
|
range: state.range,
|
2021-01-21 19:28:58 +08:00
|
|
|
|
props: commandProps,
|
2021-01-20 16:23:44 +08:00
|
|
|
|
})
|
2021-01-19 06:41:38 +08:00
|
|
|
|
},
|
2020-12-17 00:47:17 +08:00
|
|
|
|
decorationNode,
|
2021-01-19 19:28:33 +08:00
|
|
|
|
// virtual node for popper.js or tippy.js
|
2021-01-15 21:49:28 +08:00
|
|
|
|
// this can be used for building popups without a DOM node
|
2021-04-17 05:33:30 +08:00
|
|
|
|
clientRect: decorationNode
|
2021-08-24 04:13:51 +08:00
|
|
|
|
? () => {
|
2022-11-05 06:18:56 +08:00
|
|
|
|
// because of `items` can be asynchrounous we’ll search for the current decoration node
|
2023-02-03 00:37:33 +08:00
|
|
|
|
const { decorationId } = this.key?.getState(editor.state) // eslint-disable-line
|
|
|
|
|
const currentDecorationNode = view.dom.querySelector(
|
|
|
|
|
`[data-decoration-id="${decorationId}"]`,
|
|
|
|
|
)
|
2021-08-24 04:13:51 +08:00
|
|
|
|
|
2022-05-27 18:31:46 +08:00
|
|
|
|
return currentDecorationNode?.getBoundingClientRect() || null
|
2021-08-24 04:13:51 +08:00
|
|
|
|
}
|
2021-04-17 05:33:30 +08:00
|
|
|
|
: null,
|
2020-12-17 00:47:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-11 21:20:44 +08:00
|
|
|
|
if (handleStart) {
|
|
|
|
|
renderer?.onBeforeStart?.(props)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (handleChange) {
|
|
|
|
|
renderer?.onBeforeUpdate?.(props)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (handleChange || handleStart) {
|
|
|
|
|
props.items = await items({
|
|
|
|
|
editor,
|
|
|
|
|
query: state.query,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-08 03:00:13 +08:00
|
|
|
|
if (handleExit) {
|
|
|
|
|
renderer?.onExit?.(props)
|
2020-12-17 00:47:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (handleChange) {
|
2021-01-19 06:41:38 +08:00
|
|
|
|
renderer?.onUpdate?.(props)
|
2020-12-17 00:47:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-08 03:00:13 +08:00
|
|
|
|
if (handleStart) {
|
|
|
|
|
renderer?.onStart?.(props)
|
2020-12-17 00:47:17 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
2021-12-05 06:24:21 +08:00
|
|
|
|
|
|
|
|
|
destroy: () => {
|
|
|
|
|
if (!props) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderer?.onExit?.(props)
|
|
|
|
|
},
|
2020-12-17 00:47:17 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
state: {
|
|
|
|
|
// Initialize the plugin's internal state.
|
|
|
|
|
init() {
|
2022-06-21 06:17:10 +08:00
|
|
|
|
const state: {
|
2023-02-03 00:37:33 +08:00
|
|
|
|
active: boolean
|
|
|
|
|
range: Range
|
2022-06-21 06:17:10 +08:00
|
|
|
|
query: null | string
|
|
|
|
|
text: null | string
|
|
|
|
|
composing: boolean
|
|
|
|
|
decorationId?: string | null
|
|
|
|
|
} = {
|
2020-12-17 00:47:17 +08:00
|
|
|
|
active: false,
|
2022-06-21 06:17:10 +08:00
|
|
|
|
range: {
|
|
|
|
|
from: 0,
|
|
|
|
|
to: 0,
|
|
|
|
|
},
|
2020-12-17 00:47:17 +08:00
|
|
|
|
query: null,
|
|
|
|
|
text: null,
|
2021-08-13 21:13:19 +08:00
|
|
|
|
composing: false,
|
2020-12-17 00:47:17 +08:00
|
|
|
|
}
|
2022-06-21 06:17:10 +08:00
|
|
|
|
|
|
|
|
|
return state
|
2020-12-17 00:47:17 +08:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Apply changes to the plugin state from a view transaction.
|
2024-05-24 04:20:39 +08:00
|
|
|
|
apply(transaction, prev, _oldState, state) {
|
2022-04-08 12:50:50 +08:00
|
|
|
|
const { isEditable } = editor
|
2021-08-13 21:13:19 +08:00
|
|
|
|
const { composing } = editor.view
|
2021-01-19 06:41:38 +08:00
|
|
|
|
const { selection } = transaction
|
2021-08-13 21:13:19 +08:00
|
|
|
|
const { empty, from } = selection
|
2020-12-17 00:47:17 +08:00
|
|
|
|
const next = { ...prev }
|
|
|
|
|
|
2021-08-13 21:13:19 +08:00
|
|
|
|
next.composing = composing
|
|
|
|
|
|
2022-04-08 12:50:50 +08:00
|
|
|
|
// We can only be suggesting if the view is editable, and:
|
|
|
|
|
// * there is no selection, or
|
|
|
|
|
// * a composition is active (see: https://github.com/ueberdosis/tiptap/issues/1449)
|
|
|
|
|
if (isEditable && (empty || editor.view.composing)) {
|
2020-12-17 00:47:17 +08:00
|
|
|
|
// Reset active state if we just left the previous suggestion range
|
2023-02-03 00:37:33 +08:00
|
|
|
|
if ((from < prev.range.from || from > prev.range.to) && !composing && !prev.composing) {
|
2020-12-17 00:47:17 +08:00
|
|
|
|
next.active = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Try to match against where our cursor currently is
|
2021-01-15 21:49:28 +08:00
|
|
|
|
const match = findSuggestionMatch({
|
2021-01-15 16:25:50 +08:00
|
|
|
|
char,
|
|
|
|
|
allowSpaces,
|
2022-06-22 05:17:26 +08:00
|
|
|
|
allowedPrefixes,
|
2021-01-15 16:25:50 +08:00
|
|
|
|
startOfLine,
|
2021-01-15 16:55:15 +08:00
|
|
|
|
$position: selection.$from,
|
2021-01-15 16:25:50 +08:00
|
|
|
|
})
|
2023-02-03 00:37:33 +08:00
|
|
|
|
const decorationId = `id_${Math.floor(Math.random() * 0xffffffff)}`
|
2021-01-15 16:25:50 +08:00
|
|
|
|
|
2020-12-17 00:47:17 +08:00
|
|
|
|
// If we found a match, update the current state to show it
|
2022-01-21 16:29:36 +08:00
|
|
|
|
if (match && allow({ editor, state, range: match.range })) {
|
2020-12-17 00:47:17 +08:00
|
|
|
|
next.active = true
|
|
|
|
|
next.decorationId = prev.decorationId ? prev.decorationId : decorationId
|
|
|
|
|
next.range = match.range
|
|
|
|
|
next.query = match.query
|
|
|
|
|
next.text = match.text
|
|
|
|
|
} else {
|
|
|
|
|
next.active = false
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
next.active = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure to empty the range if suggestion is inactive
|
|
|
|
|
if (!next.active) {
|
|
|
|
|
next.decorationId = null
|
2022-06-21 06:17:10 +08:00
|
|
|
|
next.range = { from: 0, to: 0 }
|
2020-12-17 00:47:17 +08:00
|
|
|
|
next.query = null
|
|
|
|
|
next.text = null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return next
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
props: {
|
|
|
|
|
// Call the keydown hook if suggestion is active.
|
|
|
|
|
handleKeyDown(view, event) {
|
2022-06-20 17:45:37 +08:00
|
|
|
|
const { active, range } = plugin.getState(view.state)
|
2020-12-17 00:47:17 +08:00
|
|
|
|
|
2021-01-15 21:49:28 +08:00
|
|
|
|
if (!active) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2020-12-17 00:47:17 +08:00
|
|
|
|
|
2021-01-19 06:41:38 +08:00
|
|
|
|
return renderer?.onKeyDown?.({ view, event, range }) || false
|
2020-12-17 00:47:17 +08:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Setup decorator on the currently active suggestion.
|
2021-01-15 21:49:28 +08:00
|
|
|
|
decorations(state) {
|
2022-06-20 17:45:37 +08:00
|
|
|
|
const { active, range, decorationId } = plugin.getState(state)
|
2020-12-17 00:47:17 +08:00
|
|
|
|
|
2021-01-15 21:49:28 +08:00
|
|
|
|
if (!active) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
2020-12-17 00:47:17 +08:00
|
|
|
|
|
2021-01-15 21:49:28 +08:00
|
|
|
|
return DecorationSet.create(state.doc, [
|
2020-12-17 00:47:17 +08:00
|
|
|
|
Decoration.inline(range.from, range.to, {
|
2021-01-20 17:47:31 +08:00
|
|
|
|
nodeName: decorationTag,
|
|
|
|
|
class: decorationClass,
|
2020-12-17 00:47:17 +08:00
|
|
|
|
'data-decoration-id': decorationId,
|
|
|
|
|
}),
|
|
|
|
|
])
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
2022-06-20 17:45:37 +08:00
|
|
|
|
|
|
|
|
|
return plugin
|
2020-12-17 00:47:17 +08:00
|
|
|
|
}
|