Total Complexity | 7 |
Complexity/F | 1.4 |
Lines of Code | 31 |
Function Count | 5 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { Plugin } from 'prosemirror-state'; |
||
2 | |||
3 | /** |
||
4 | * Disable the browser's default behavior on tab, which is required to support indentation using Tab/Shift-Tab |
||
5 | */ |
||
6 | export function disableDefaultTabBehavior() { |
||
7 | function preventTab(e) { |
||
8 | if (e.key === 'Tab') { |
||
9 | e.preventDefault(); |
||
10 | e.stopPropagation(); |
||
11 | } |
||
12 | } |
||
13 | |||
14 | let setupComplete = false; |
||
15 | |||
16 | return new Plugin({ |
||
17 | view: () => ({ |
||
18 | update: () => { |
||
19 | if (setupComplete) return; |
||
|
|||
20 | |||
21 | window.addEventListener('keydown', preventTab); |
||
22 | setupComplete = true; |
||
23 | }, |
||
24 | destroy: () => { |
||
25 | window.removeEventListener('keydown', preventTab); |
||
26 | }, |
||
27 | }), |
||
28 | }); |
||
29 | } |
||
30 | |||
31 | export default disableDefaultTabBehavior; |
||
32 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.