aleman 1.7.1 → 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 CHANGED
@@ -1,3 +1,8 @@
1
+ 2025.09.14, v1.8.0
2
+
3
+ feature:
4
+ - c338550 aleman: menu: vim: built-in
5
+
1
6
  2025.09.13, v1.7.1
2
7
 
3
8
  feature:
package/README.md CHANGED
@@ -20,13 +20,36 @@
20
20
  bun i aleman
21
21
  ```
22
22
 
23
- ## Usage Example
23
+ ## Rules and Addons
24
24
 
25
- Addon:
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
@@ -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
+ };
@@ -1,6 +1,9 @@
1
- import * as up from '../up.js';
1
+ import * as up from './up.js';
2
2
 
3
- export function gg({state, options}) {
3
+ export const {filter} = up;
4
+ export const commands = ['gg'];
5
+
6
+ export function listener({state, options}) {
4
7
  const {
5
8
  insideSubmenu,
6
9
  index,
@@ -18,3 +21,4 @@ export function gg({state, options}) {
18
21
  options,
19
22
  });
20
23
  }
24
+
@@ -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 {createVim} from './vim/index.js';
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
- createVim(),
22
+ gg,
23
+ j,
24
+ k,
21
25
  ];
@@ -1,7 +1,10 @@
1
- import * as down from '../down.js';
2
- import {getSubmenu} from '../submenu/index.js';
1
+ import * as down from './down.js';
2
+ import {getSubmenu} from './submenu/index.js';
3
3
 
4
- export function j({count, state, options}) {
4
+ export const {filter} = down;
5
+ export const commands = ['j'];
6
+
7
+ export function listener({count, state, options}) {
5
8
  const {
6
9
  index,
7
10
  insideSubmenu,
@@ -34,3 +37,4 @@ export function j({count, state, options}) {
34
37
  options,
35
38
  });
36
39
  }
40
+
@@ -1,6 +1,9 @@
1
- import * as down from '../down.js';
1
+ import * as down from './down.js';
2
2
 
3
- export function k({count, state, options}) {
3
+ export const {filter} = down;
4
+ export const commands = ['k'];
5
+
6
+ export function listener({count, state, options}) {
4
7
  const {
5
8
  insideSubmenu,
6
9
  index,
@@ -27,3 +30,4 @@ export function k({count, state, options}) {
27
30
  options,
28
31
  });
29
32
  }
33
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aleman",
3
- "version": "1.7.1",
3
+ "version": "1.8.0",
4
4
  "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout-based framework for web",
@@ -1,69 +0,0 @@
1
- import {k} from './k.js';
2
- import {gg} from './gg.js';
3
- import {j} from './j.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
-
24
- if (!buffer.length && key === 'g') {
25
- buffer.push('g');
26
- return null;
27
- }
28
-
29
- if (!buffer.length && /\d/.test(key)) {
30
- buffer.push(Number(key));
31
- return null;
32
- }
33
-
34
- if (!buffer.length && key === 'j')
35
- buffer.push(1);
36
-
37
- if (!buffer.length && key === 'k')
38
- buffer.push(1);
39
-
40
- const [count] = buffer;
41
-
42
- if (isNumber(count) && key === 'j') {
43
- buffer = [];
44
- return j({
45
- count,
46
- state,
47
- options,
48
- });
49
- }
50
-
51
- if (isNumber(count) && key === 'k') {
52
- buffer = [];
53
- return k({
54
- count,
55
- state,
56
- options,
57
- });
58
- }
59
-
60
- if (count === 'g' && key === 'g') {
61
- buffer = [];
62
- return gg({
63
- state,
64
- options,
65
- });
66
- }
67
-
68
- buffer = [];
69
- };