Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[prosemirror-autocomplete] add cancelOnSpace option #185

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/prosemirror-autocomplete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ Provide the trigger in the matched group and anything before in a non-capture gr

- `name: string`: the trigger is passed in the action, you can use this to descriminate handler calls
- `trigger: string | RegExp`: used to trigger an autocomplete suggestion - described above
- `cancelOnFirstSpace?: boolean`, cancels the auto complete on first space, default is true
- `cancelOnSpace?: boolean` (default `false`) When `true`, a space anywhere in the filter cancels autocompletion, and the value of `cancelOnFirstSpace` is ignored. Default is `false`.
- `cancelOnFirstSpace?: boolean` (default `true`) When `true`, only a space in the initial position of the filter will cancel autocompletion, while spaces elsewhere are allowed. Default is `true`.
- `allArrowKeys?: boolean`: Use left/right arrow keys, default is false
- `decorationAttrs?: DecorationAttrs`, passed to the `<span>` element directly through prosemirror

Expand Down
2 changes: 1 addition & 1 deletion packages/prosemirror-autocomplete/demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const options: Options = {
reducer,
triggers: [
// For demo purposes, make the `#` and `@` easier to create
{ name: 'hashtag', trigger: /(#)$/ },
{ name: 'hashtag', trigger: /(#)$/, cancelOnSpace: true },
{ name: 'mention', trigger: /(@)$/ },
{ name: 'emoji', trigger: ':' },
{ name: 'link', trigger: '[[', cancelOnFirstSpace: false },
Expand Down
7 changes: 5 additions & 2 deletions packages/prosemirror-autocomplete/src/decoration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,13 @@ export function getDecorationPlugin(reducer: Required<Options>['reducer']) {
// Be defensive, just in case the trigger doesn't exist
const filter = text.slice(trigger?.length ?? 1);

const checkCancelOnSpace = type?.cancelOnFirstSpace ?? true;
const cancelOnFirstSpace = type?.cancelOnFirstSpace ?? true;
const cancelOnSpace = type?.cancelOnSpace ?? false;
const checkCancelOnSpace = cancelOnFirstSpace || cancelOnSpace;

if (
checkCancelOnSpace &&
filter.length === 0 &&
(cancelOnSpace || filter.length === 0) &&
(event.key === ' ' || event.key === 'Spacebar')
) {
closeAutocomplete(view);
Expand Down
7 changes: 6 additions & 1 deletion packages/prosemirror-autocomplete/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ export type AutocompleteTrMeta = OpenAutocomplete | CloseAutocomplete;
export type Trigger = {
name: string;
trigger: string | RegExp;
cancelOnFirstSpace?: boolean; // Default is true
/** When `true`, a space anywhere in the filter cancels autocompletion, and
* the value of `cancelOnFirstSpace` is ignored. (default: `false`) */
cancelOnSpace?: boolean;
/** When `true`, a space in the initial position of the filter cancels
* autocompletion, while spaces elsewhere are allowed. (default: `true`) */
cancelOnFirstSpace?: boolean;
allArrowKeys?: boolean; // Default is false
decorationAttrs?: DecorationAttrs;
};
Expand Down