-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathcommandStringExtraction.ts
More file actions
55 lines (46 loc) · 1.67 KB
/
commandStringExtraction.ts
File metadata and controls
55 lines (46 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import type { Middleware } from '../../../middleware';
import type { TextComposerMiddlewareExecutorState } from './TextComposerMiddlewareExecutor';
import { stripTriggerFromText } from './textMiddlewareUtils';
import type { CommandSuggestion } from './types';
export type ApplyCommandSettingsMiddleware = Middleware<
TextComposerMiddlewareExecutorState<CommandSuggestion>,
'onChange' | 'onSuggestionItemSelect'
>;
export const createCommandStringExtractionMiddleware =
(): ApplyCommandSettingsMiddleware => ({
handlers: {
onChange: ({ complete, forward, state }) => {
const { command } = state;
if (!command) {
return forward();
}
const triggerWithCommand = `/${command.name}`;
const newText = stripTriggerFromText(state.text, triggerWithCommand);
return complete({
...state,
selection: {
end: state.selection.end - triggerWithCommand.length,
start: state.selection.start - triggerWithCommand.length,
},
text: newText,
});
},
onSuggestionItemSelect: ({ next, forward, state }) => {
const { command } = state;
if (!command) {
return forward();
}
const triggerWithCommand = `/${command?.name} `;
const newText = state.text.slice(triggerWithCommand.length);
return next({
...state,
selection: {
end: state.selection.end - triggerWithCommand.length,
start: state.selection.start - triggerWithCommand.length,
},
text: newText,
});
},
},
id: 'stream-io/text-composer/command-string-extraction',
});