aleman 1.7.0 → 1.8.0
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.
- package/ChangeLog +10 -0
- package/README.md +83 -3
- package/aleman/add-listeners.js +10 -1
- package/aleman/vim.js +41 -0
- package/menu/addons/gg.js +24 -0
- package/menu/addons/index.js +6 -2
- package/menu/addons/j.js +40 -0
- package/menu/addons/k.js +33 -0
- package/package.json +1 -1
- package/menu/addons/vim.js +0 -124
package/ChangeLog
CHANGED
package/README.md
CHANGED
|
@@ -20,13 +20,36 @@
|
|
|
20
20
|
bun i aleman
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
##
|
|
23
|
+
## Rules and Addons
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
Aleman supports two main concepts:
|
|
26
|
+
- ✅addons - events;
|
|
27
|
+
- ✅rules - 🐊**Putout** rules that changes HTML;
|
|
28
|
+
|
|
29
|
+
All interaction with DOM made using rules, and we interact not with DOM directly, but with JSX AST.
|
|
30
|
+
It makes testing simple, states predictable and independent.
|
|
31
|
+
|
|
32
|
+
### Addons
|
|
33
|
+
|
|
34
|
+
Addon responsible for UI and interfaction with outer world: clicks, fetches and everything like this.
|
|
35
|
+
Aleman supports next types of addons:
|
|
36
|
+
|
|
37
|
+
- Global
|
|
38
|
+
- Events
|
|
39
|
+
- Keys
|
|
40
|
+
- Vim
|
|
41
|
+
|
|
42
|
+
When you need to filter out events according to `state` use `filter`:
|
|
26
43
|
|
|
27
44
|
```js
|
|
28
|
-
export const events = ['click'];
|
|
29
45
|
export const filter = ({state}) => state.command === 'show';
|
|
46
|
+
```
|
|
47
|
+
#### Global
|
|
48
|
+
|
|
49
|
+
Any browser event you need to listen globally:
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
export const events = ['click'];
|
|
30
53
|
export const listener = () => ({
|
|
31
54
|
command: 'hide',
|
|
32
55
|
index: -1,
|
|
@@ -35,6 +58,63 @@ export const listener = () => ({
|
|
|
35
58
|
});
|
|
36
59
|
```
|
|
37
60
|
|
|
61
|
+
#### Events
|
|
62
|
+
|
|
63
|
+
Any browser event you need to listen according to element with `data-name="hello":
|
|
64
|
+
|
|
65
|
+
```js
|
|
66
|
+
export const name = 'hello'
|
|
67
|
+
export const events = ['click'];
|
|
68
|
+
export const listener = () => ({
|
|
69
|
+
command: 'hide',
|
|
70
|
+
index: -1,
|
|
71
|
+
showSubmenu: false,
|
|
72
|
+
insideSubmenu: false,
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
#### Keys
|
|
77
|
+
|
|
78
|
+
```js
|
|
79
|
+
export const keys = ['Escape'];
|
|
80
|
+
|
|
81
|
+
export const listener = ({state, options}) => {
|
|
82
|
+
options.beforeHide?.(state);
|
|
83
|
+
return {
|
|
84
|
+
command: 'hide',
|
|
85
|
+
showSubmenu: false,
|
|
86
|
+
index: -1,
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Vim
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
import * as up from './up.js';
|
|
95
|
+
|
|
96
|
+
export const commands = ['gg'];
|
|
97
|
+
|
|
98
|
+
export function listener({state, options}) {
|
|
99
|
+
const {
|
|
100
|
+
insideSubmenu,
|
|
101
|
+
index,
|
|
102
|
+
submenuIndex,
|
|
103
|
+
} = state;
|
|
104
|
+
|
|
105
|
+
const newState = {
|
|
106
|
+
...state,
|
|
107
|
+
index: insideSubmenu ? index : 1,
|
|
108
|
+
submenuIndex: insideSubmenu ? 1 : submenuIndex,
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
return up.listener({
|
|
112
|
+
state: newState,
|
|
113
|
+
options,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
38
118
|
## Licence
|
|
39
119
|
|
|
40
120
|
MIT
|
package/aleman/add-listeners.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import {createVimParser} from './vim.js';
|
|
2
|
+
|
|
1
3
|
const queryElement = ({name}) => {
|
|
2
4
|
return document.querySelector(`[data-name="${name}"]`);
|
|
3
5
|
};
|
|
@@ -61,7 +63,7 @@ export const addGlobalListeners = ({globalAddons, options, readState, writeState
|
|
|
61
63
|
}
|
|
62
64
|
};
|
|
63
65
|
|
|
64
|
-
const createListener = ({options, addon, readState, writeState}) => (event) => {
|
|
66
|
+
const createListener = ({options, addon, readState, writeState, parseVim = createVimParser()}) => (event) => {
|
|
65
67
|
const {
|
|
66
68
|
keys,
|
|
67
69
|
listener,
|
|
@@ -70,11 +72,17 @@ const createListener = ({options, addon, readState, writeState}) => (event) => {
|
|
|
70
72
|
filter,
|
|
71
73
|
after,
|
|
72
74
|
afterIf,
|
|
75
|
+
commands,
|
|
73
76
|
} = addon;
|
|
74
77
|
|
|
75
78
|
if (keys && !keys.includes(event.key))
|
|
76
79
|
return;
|
|
77
80
|
|
|
81
|
+
const [command, count] = parseVim(event);
|
|
82
|
+
|
|
83
|
+
if (commands && !commands.includes(command))
|
|
84
|
+
return;
|
|
85
|
+
|
|
78
86
|
const state = readState();
|
|
79
87
|
|
|
80
88
|
const is = filter?.({
|
|
@@ -93,6 +101,7 @@ const createListener = ({options, addon, readState, writeState}) => (event) => {
|
|
|
93
101
|
event.stopPropagation();
|
|
94
102
|
|
|
95
103
|
const newState = listener({
|
|
104
|
+
count,
|
|
96
105
|
event,
|
|
97
106
|
state,
|
|
98
107
|
options,
|
package/aleman/vim.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const isNumber = (a) => !Number.isNaN(a) && typeof a === 'number';
|
|
2
|
+
|
|
3
|
+
export const createVimParser = (buffer = []) => (event) => {
|
|
4
|
+
const {key} = event;
|
|
5
|
+
|
|
6
|
+
if (!buffer.length && key === 'g') {
|
|
7
|
+
buffer.push('g');
|
|
8
|
+
return [''];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (!buffer.length && /\d/.test(key)) {
|
|
12
|
+
buffer.push(Number(key));
|
|
13
|
+
return [''];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (!buffer.length && key === 'j')
|
|
17
|
+
buffer.push(1);
|
|
18
|
+
|
|
19
|
+
if (!buffer.length && key === 'k')
|
|
20
|
+
buffer.push(1);
|
|
21
|
+
|
|
22
|
+
const [count] = buffer;
|
|
23
|
+
|
|
24
|
+
if (isNumber(count) && key === 'j') {
|
|
25
|
+
buffer = [];
|
|
26
|
+
return ['j', count];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (isNumber(count) && key === 'k') {
|
|
30
|
+
buffer = [];
|
|
31
|
+
return ['k', count];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (count === 'g' && key === 'g') {
|
|
35
|
+
buffer = [];
|
|
36
|
+
return ['gg'];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
buffer = [];
|
|
40
|
+
return [''];
|
|
41
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as up from './up.js';
|
|
2
|
+
|
|
3
|
+
export const {filter} = up;
|
|
4
|
+
export const commands = ['gg'];
|
|
5
|
+
|
|
6
|
+
export function listener({state, options}) {
|
|
7
|
+
const {
|
|
8
|
+
insideSubmenu,
|
|
9
|
+
index,
|
|
10
|
+
submenuIndex,
|
|
11
|
+
} = state;
|
|
12
|
+
|
|
13
|
+
const newState = {
|
|
14
|
+
...state,
|
|
15
|
+
index: insideSubmenu ? index : 1,
|
|
16
|
+
submenuIndex: insideSubmenu ? 1 : submenuIndex,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
return up.listener({
|
|
20
|
+
state: newState,
|
|
21
|
+
options,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
package/menu/addons/index.js
CHANGED
|
@@ -6,7 +6,9 @@ import * as enter from './enter.js';
|
|
|
6
6
|
import * as left from './left.js';
|
|
7
7
|
import * as right from './right.js';
|
|
8
8
|
import * as shiftG from './shift-g.js';
|
|
9
|
-
import
|
|
9
|
+
import * as gg from './gg.js';
|
|
10
|
+
import * as j from './j.js';
|
|
11
|
+
import * as k from './k.js';
|
|
10
12
|
|
|
11
13
|
export const addons = [
|
|
12
14
|
click,
|
|
@@ -17,5 +19,7 @@ export const addons = [
|
|
|
17
19
|
left,
|
|
18
20
|
right,
|
|
19
21
|
shiftG,
|
|
20
|
-
|
|
22
|
+
gg,
|
|
23
|
+
j,
|
|
24
|
+
k,
|
|
21
25
|
];
|
package/menu/addons/j.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as down from './down.js';
|
|
2
|
+
import {getSubmenu} from './submenu/index.js';
|
|
3
|
+
|
|
4
|
+
export const {filter} = down;
|
|
5
|
+
export const commands = ['j'];
|
|
6
|
+
|
|
7
|
+
export function listener({count, state, options}) {
|
|
8
|
+
const {
|
|
9
|
+
index,
|
|
10
|
+
insideSubmenu,
|
|
11
|
+
submenuIndex,
|
|
12
|
+
} = state;
|
|
13
|
+
|
|
14
|
+
const menuCount = Object.keys(options.menu).length;
|
|
15
|
+
const submenuCount = Object.keys(getSubmenu({
|
|
16
|
+
index,
|
|
17
|
+
options,
|
|
18
|
+
})).length;
|
|
19
|
+
|
|
20
|
+
let newIndex = insideSubmenu ? index : index + count - 1;
|
|
21
|
+
let newSubmenuIndex = insideSubmenu ? submenuIndex + count : submenuIndex;
|
|
22
|
+
|
|
23
|
+
if (newIndex > menuCount - 1)
|
|
24
|
+
newIndex = menuCount - 1;
|
|
25
|
+
|
|
26
|
+
if (newSubmenuIndex > submenuCount - 1)
|
|
27
|
+
newSubmenuIndex = submenuCount - 1;
|
|
28
|
+
|
|
29
|
+
const newState = {
|
|
30
|
+
...state,
|
|
31
|
+
index: newIndex,
|
|
32
|
+
submenuIndex: newSubmenuIndex,
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return down.listener({
|
|
36
|
+
state: newState,
|
|
37
|
+
options,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
package/menu/addons/k.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import * as down from './down.js';
|
|
2
|
+
|
|
3
|
+
export const {filter} = down;
|
|
4
|
+
export const commands = ['k'];
|
|
5
|
+
|
|
6
|
+
export function listener({count, state, options}) {
|
|
7
|
+
const {
|
|
8
|
+
insideSubmenu,
|
|
9
|
+
index,
|
|
10
|
+
submenuIndex,
|
|
11
|
+
} = state;
|
|
12
|
+
|
|
13
|
+
let newIndex = insideSubmenu ? index : index - count - 1;
|
|
14
|
+
let newSubmenuIndex = insideSubmenu ? submenuIndex - count - 1 : submenuIndex;
|
|
15
|
+
|
|
16
|
+
if (newIndex < -1)
|
|
17
|
+
newIndex = -1;
|
|
18
|
+
|
|
19
|
+
if (newSubmenuIndex < -1)
|
|
20
|
+
newSubmenuIndex = -1;
|
|
21
|
+
|
|
22
|
+
const newState = {
|
|
23
|
+
...state,
|
|
24
|
+
index: newIndex,
|
|
25
|
+
submenuIndex: newSubmenuIndex,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
return down.listener({
|
|
29
|
+
state: newState,
|
|
30
|
+
options,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
package/package.json
CHANGED
package/menu/addons/vim.js
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import * as up from './up.js';
|
|
2
|
-
import * as down from './down.js';
|
|
3
|
-
import {getSubmenu} from './submenu/index.js';
|
|
4
|
-
|
|
5
|
-
const isNumber = (a) => !Number.isNaN(a) && typeof a === 'number';
|
|
6
|
-
|
|
7
|
-
export const createVim = () => {
|
|
8
|
-
const buffer = [];
|
|
9
|
-
|
|
10
|
-
return {
|
|
11
|
-
events,
|
|
12
|
-
filter,
|
|
13
|
-
listener: createListener(buffer),
|
|
14
|
-
};
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const events = ['keydown'];
|
|
18
|
-
|
|
19
|
-
const filter = ({state}) => state.command === 'show';
|
|
20
|
-
|
|
21
|
-
const createListener = (buffer) => ({event, state, options}) => {
|
|
22
|
-
const {key} = event;
|
|
23
|
-
const {index} = state;
|
|
24
|
-
|
|
25
|
-
const menuCount = Object.keys(options.menu).length;
|
|
26
|
-
const submenuCount = Object.keys(getSubmenu({
|
|
27
|
-
index,
|
|
28
|
-
options,
|
|
29
|
-
})).length;
|
|
30
|
-
|
|
31
|
-
if (!buffer.length && key === 'g') {
|
|
32
|
-
buffer.push('g');
|
|
33
|
-
return null;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if (!buffer.length && /\d/.test(key)) {
|
|
37
|
-
buffer.push(Number(key));
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (!buffer.length && key === 'j')
|
|
42
|
-
buffer.push(1);
|
|
43
|
-
|
|
44
|
-
if (!buffer.length && key === 'k')
|
|
45
|
-
buffer.push(1);
|
|
46
|
-
|
|
47
|
-
const [first] = buffer;
|
|
48
|
-
|
|
49
|
-
if (isNumber(first) && key === 'j') {
|
|
50
|
-
buffer = [];
|
|
51
|
-
const {insideSubmenu, submenuIndex} = state;
|
|
52
|
-
|
|
53
|
-
let newIndex = insideSubmenu ? index : index + first - 1;
|
|
54
|
-
let newSubmenuIndex = insideSubmenu ? submenuIndex + first : submenuIndex;
|
|
55
|
-
|
|
56
|
-
if (newIndex > menuCount - 1)
|
|
57
|
-
newIndex = menuCount - 1;
|
|
58
|
-
|
|
59
|
-
if (newSubmenuIndex > submenuCount - 1)
|
|
60
|
-
newSubmenuIndex = submenuCount - 1;
|
|
61
|
-
|
|
62
|
-
const newState = {
|
|
63
|
-
...state,
|
|
64
|
-
index: newIndex,
|
|
65
|
-
submenuIndex: newSubmenuIndex,
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
return down.listener({
|
|
69
|
-
state: newState,
|
|
70
|
-
options,
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (isNumber(first) && key === 'k') {
|
|
75
|
-
buffer = [];
|
|
76
|
-
const {
|
|
77
|
-
insideSubmenu,
|
|
78
|
-
index,
|
|
79
|
-
submenuIndex,
|
|
80
|
-
} = state;
|
|
81
|
-
|
|
82
|
-
let newIndex = insideSubmenu ? index : index - first - 1;
|
|
83
|
-
let newSubmenuIndex = insideSubmenu ? submenuIndex - first - 1 : submenuIndex;
|
|
84
|
-
|
|
85
|
-
if (newIndex < -1)
|
|
86
|
-
newIndex = -1;
|
|
87
|
-
|
|
88
|
-
if (newSubmenuIndex < -1)
|
|
89
|
-
newSubmenuIndex = -1;
|
|
90
|
-
|
|
91
|
-
const newState = {
|
|
92
|
-
...state,
|
|
93
|
-
index: newIndex,
|
|
94
|
-
submenuIndex: newSubmenuIndex,
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
return down.listener({
|
|
98
|
-
state: newState,
|
|
99
|
-
options,
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (first === 'g' && key === 'g') {
|
|
104
|
-
buffer = [];
|
|
105
|
-
const {
|
|
106
|
-
insideSubmenu,
|
|
107
|
-
index,
|
|
108
|
-
submenuIndex,
|
|
109
|
-
} = state;
|
|
110
|
-
|
|
111
|
-
const newState = {
|
|
112
|
-
...state,
|
|
113
|
-
index: insideSubmenu ? index : 1,
|
|
114
|
-
submenuIndex: insideSubmenu ? 1 : submenuIndex,
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
return up.listener({
|
|
118
|
-
state: newState,
|
|
119
|
-
options,
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
buffer = [];
|
|
124
|
-
};
|