aleman 1.4.0 → 1.4.2

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,17 @@
1
+ 2025.09.07, v1.4.2
2
+
3
+ feature:
4
+ - 1a1899e aleman: menu: item-click: no data-menu find on element
5
+
6
+ 2025.09.06, v1.4.1
7
+
8
+ feature:
9
+ - f745059 aleman: menu: build-menu: simplify
10
+ - 88f4f81 aleman: menu: rules: hide-submenu: siplify
11
+ - ffe78e2 aleman: menu: jsx-operator: containsClass
12
+ - 779f1e6 aleman: menu: rules: menu: simplify
13
+ - fe341ff menu: set-position: simplify
14
+
1
15
  2025.09.04, v1.4.0
2
16
 
3
17
  feature:
@@ -7,9 +7,8 @@ export const createItemClick = (name) => ({
7
7
  });
8
8
 
9
9
  const listener = ({event, options}) => {
10
- const menuItemElement = document.elementFromPoint(event.clientX, event.clientY);
11
- const {menuPath} = menuItemElement.dataset;
12
10
  const {menu} = options;
11
+ const menuPath = getMenuPath(event);
13
12
  const fn = jessy(menuPath, menu);
14
13
 
15
14
  setTimeout(fn);
@@ -21,3 +20,14 @@ const listener = ({event, options}) => {
21
20
  showSubmenu: false,
22
21
  };
23
22
  };
23
+
24
+ function getMenuPath(event) {
25
+ let menuItemElement = document.elementFromPoint(event.clientX, event.clientY);
26
+ const {menuPath} = menuItemElement.dataset;
27
+
28
+ if (!menuPath)
29
+ menuItemElement = menuItemElement.querySelector('[data-menu-path]');
30
+
31
+ return menuItemElement.dataset.menuPath;
32
+ }
33
+
@@ -1,7 +1,7 @@
1
1
  import {template} from 'putout';
2
- import {checkDataName} from '../check-data-name.js';
3
2
  import {
4
3
  appendAttributeValue,
4
+ hasDataName,
5
5
  setAttributeValue,
6
6
  } from '../jsx-operator.js';
7
7
 
@@ -26,7 +26,7 @@ const DefaultMenu = {
26
26
  };
27
27
 
28
28
  export const fix = ({path, menu, icon, name = ''}) => {
29
- const {children} = path.parentPath.node;
29
+ const {children} = path.node;
30
30
 
31
31
  for (const [key, value] of entries(menu)) {
32
32
  const menuItem = createMenuItem();
@@ -45,13 +45,13 @@ export const fix = ({path, menu, icon, name = ''}) => {
45
45
  setSubmenu(menuItem);
46
46
  menuItem.children.push(createMenu());
47
47
 
48
- const openingElement = path.parentPath
48
+ const elementPath = path
49
49
  .get('children')
50
50
  .at(-1)
51
- .get('children.1.openingElement');
51
+ .get('children.1');
52
52
 
53
53
  fix({
54
- path: openingElement,
54
+ path: elementPath,
55
55
  icon,
56
56
  menu: value,
57
57
  name: key,
@@ -61,17 +61,17 @@ export const fix = ({path, menu, icon, name = ''}) => {
61
61
  };
62
62
 
63
63
  export const traverse = ({options, push}) => ({
64
- JSXOpeningElement(path) {
64
+ JSXElement(path) {
65
65
  const {
66
66
  name = 'menu',
67
67
  menu = DefaultMenu,
68
68
  icon = false,
69
69
  } = options;
70
70
 
71
- if (!checkDataName(path, name))
71
+ if (!hasDataName(path, name))
72
72
  return;
73
73
 
74
- if (path.parentPath.node.children.length)
74
+ if (path.node.children.length)
75
75
  return;
76
76
 
77
77
  push({
@@ -0,0 +1,16 @@
1
+ <ul data-name="hello" className="menu menu-hidden">
2
+ <li data-menu-path="Upload" data-name="menu-item" className="menu-item icon icon-view">
3
+ <label data-menu-path="Upload">Upload</label>
4
+ </li>
5
+ <li data-menu-path="New" data-name="menu-item" className="menu-item icon icon-edit menu-item-selected menu-submenu-show">
6
+ <label data-menu-path="New">New</label>
7
+ <ul data-name="menu" className="menu menu-hidden">
8
+ <li data-menu-path="New.File" data-name="menu-item" className="menu-item icon icon-view">
9
+ <label data-menu-path="New.File">File</label>
10
+ </li>
11
+ <li data-menu-path="New.Directory" data-name="menu-item" className="menu-item icon icon-edit">
12
+ <label data-menu-path="New.Directory">Directory</label>
13
+ </li>
14
+ </ul>
15
+ </li>
16
+ </ul>;
@@ -1,41 +1,33 @@
1
- import {operator} from 'putout';
2
- import {checkDataName} from '../check-data-name.js';
3
- import {getAttributePath} from '../jsx-operator.js';
1
+ import {
2
+ hasTagName,
3
+ containsClassName,
4
+ removeClassName,
5
+ hasDataName,
6
+ } from '../jsx-operator.js';
4
7
 
5
- const {setLiteralValue} = operator;
8
+ const CLASS = 'menu-submenu-show';
6
9
 
7
10
  export const report = () => `Hide submenu`;
8
11
 
9
12
  export const fix = (path) => {
10
- const {value} = path.node;
11
- const newValue = value.value;
12
-
13
- setLiteralValue(value, newValue
14
- .replace('menu-submenu-show', '')
15
- .trim());
13
+ removeClassName(path, CLASS);
16
14
  };
17
15
 
18
16
  export const traverse = ({push, options}) => ({
19
- JSXOpeningElement(path) {
17
+ JSXElement(path) {
20
18
  const {name, showSubmenu} = options;
21
-
22
- if (path.node.name.name !== 'li')
23
- return;
19
+ const {parentPath} = path;
24
20
 
25
21
  if (showSubmenu)
26
22
  return;
27
23
 
28
- const openingElementPath = path.parentPath.parentPath.get('openingElement');
24
+ if (!hasTagName(path, 'li'))
25
+ return;
29
26
 
30
- if (!checkDataName(openingElementPath, name))
27
+ if (!hasDataName(parentPath, name))
31
28
  return false;
32
29
 
33
- const attributePath = getAttributePath(path, 'className');
34
-
35
- if (!attributePath)
36
- return;
37
-
38
- if (attributePath.node.value.value.includes('menu-submenu-show'))
39
- push(attributePath);
30
+ if (containsClassName(path, CLASS))
31
+ push(path);
40
32
  },
41
33
  });
@@ -1,8 +1,12 @@
1
- import {operator} from 'putout';
1
+ import {operator, types} from 'putout';
2
2
 
3
+ const {isJSXElement} = types;
3
4
  const {setLiteralValue} = operator;
4
5
 
5
6
  export function getAttributeValue(path, attributeName) {
7
+ if (isJSXElement(path))
8
+ path = path.get('openingElement');
9
+
6
10
  const {attributes} = path.node;
7
11
 
8
12
  for (const {name, value} of attributes) {
@@ -28,6 +32,9 @@ export function getAttributeNode(node, name) {
28
32
  }
29
33
 
30
34
  export function getAttributePath(path, name) {
35
+ if (isJSXElement(path))
36
+ path = path.get('openingElement');
37
+
31
38
  let result = null;
32
39
  const attributes = path.get('attributes');
33
40
 
@@ -45,9 +52,6 @@ export function appendAttributeValue(path, name, value) {
45
52
  const node = path.node || path;
46
53
  const attributeNode = getAttributeNode(node, name);
47
54
 
48
- if (!attributeNode)
49
- return;
50
-
51
55
  if (attributeNode.value.value.includes(value))
52
56
  return;
53
57
 
@@ -55,24 +59,44 @@ export function appendAttributeValue(path, name, value) {
55
59
  }
56
60
 
57
61
  export function setAttributeValue(node, name, value) {
62
+ node = node.node || node;
63
+
58
64
  const attributeNode = getAttributeNode(node, name);
59
65
 
60
66
  if (attributeNode)
61
67
  setLiteralValue(attributeNode.value, value);
62
68
  }
63
69
 
70
+ export function addClassName(path, name) {
71
+ appendAttributeValue(path, 'className', name);
72
+ }
73
+
74
+ export function removeClassName(path, name) {
75
+ removeAttributeValue(path, 'className', name);
76
+ }
77
+
78
+ export function containsClassName(path, className) {
79
+ const classNameValue = getAttributeValue(path, 'className');
80
+ return classNameValue.includes(className);
81
+ }
82
+
83
+ export const hasTagName = (path, name) => path.node.openingElement.name.name === name;
84
+
64
85
  export function removeAttributeValue(path, name, attributeValue) {
65
86
  if (!path)
66
87
  return;
67
88
 
68
- const node = path.node || path;
89
+ const {node} = path;
69
90
  const classAttribute = getAttributeNode(node, name);
70
91
 
71
- if (!classAttribute)
72
- return;
73
-
74
92
  const {value} = classAttribute.value;
75
93
 
76
94
  if (value.includes(attributeValue))
77
95
  setLiteralValue(classAttribute.value, value.replace(RegExp(`\\s?${attributeValue}`), ''));
78
96
  }
97
+
98
+ export function hasDataName(path, value = 'menu') {
99
+ const attribute = getAttributeValue(path, 'data-name');
100
+
101
+ return attribute === value;
102
+ }
@@ -1,7 +1,9 @@
1
- import {operator} from 'putout';
2
- import {checkDataName} from '../check-data-name.js';
3
-
4
- const {setLiteralValue} = operator;
1
+ import {
2
+ addClassName,
3
+ containsClassName,
4
+ hasDataName,
5
+ removeClassName,
6
+ } from '../jsx-operator.js';
5
7
 
6
8
  export const report = ({command}) => {
7
9
  const [first, ...rest] = command;
@@ -10,44 +12,27 @@ export const report = ({command}) => {
10
12
  };
11
13
 
12
14
  export const fix = ({path, command}) => {
13
- const {value} = path.node;
14
- let newValue;
15
-
16
- if (command === 'show')
17
- newValue = value.value.replace(/\s?menu-hidden/, '');
18
- else
19
- newValue = value.value + ' menu-hidden';
15
+ if (command === 'show') {
16
+ removeClassName(path, 'menu-hidden');
17
+ return;
18
+ }
20
19
 
21
- setLiteralValue(value, newValue);
20
+ addClassName(path, 'menu-hidden');
22
21
  };
23
22
 
24
23
  export const traverse = ({push, options}) => ({
25
- JSXOpeningElement(path) {
24
+ JSXElement(path) {
26
25
  const {name, command} = options;
27
- const attributes = path.get('attributes');
28
26
 
29
- const opengineElementPath = path.parentPath.get('openingElement');
30
-
31
- if (!checkDataName(opengineElementPath, name))
27
+ if (!hasDataName(path, name))
32
28
  return false;
33
29
 
34
- for (const attr of attributes) {
35
- const {name, value} = attr.node;
36
-
37
- if (name.name !== 'className')
38
- continue;
39
-
40
- if (command === 'show' && value.value.includes('menu-hidden'))
41
- push({
42
- command: 'show',
43
- path: attr,
44
- });
45
-
46
- if (command === 'hide' && !value.value.includes('menu-hidden'))
47
- push({
48
- command,
49
- path: attr,
50
- });
51
- }
30
+ const shown = !containsClassName(path, 'menu-hidden');
31
+
32
+ if (command === 'show' && !shown || command === 'hide' && shown)
33
+ push({
34
+ command,
35
+ path,
36
+ });
52
37
  },
53
38
  });
@@ -0,0 +1,8 @@
1
+ <ul data-name="menu" class="menu menu-hidden">
2
+ <li data-name="menu-item" className='menu-item icon icon-view'>
3
+ <label data-menu-path="View">View</label>
4
+ </li>
5
+ <li data-name="menu-item" className='menu-item icon icon-edit menu-item-selected menu-submenu-show'>
6
+ <label data-menu-path="Edit">Edit</label>
7
+ </li>
8
+ </ul>;
@@ -0,0 +1,8 @@
1
+ <ul data-name="menu" class="menu menu-hidden">
2
+ <li data-name="menu-item" className="menu-item icon icon-view menu-item-selected">
3
+ <label data-menu-path="View">View</label>
4
+ </li>
5
+ <li data-name="menu-item" className="menu-item icon icon-edit">
6
+ <label data-menu-path="Edit">Edit</label>
7
+ </li>
8
+ </ul>;
@@ -1,8 +1,8 @@
1
1
  import {operator, types} from 'putout';
2
- import {checkDataName} from '../check-data-name.js';
3
2
  import {
4
3
  appendAttributeValue,
5
4
  getAttributePath,
5
+ hasDataName,
6
6
  removeAttributeValue,
7
7
  } from '../jsx-operator.js';
8
8
 
@@ -38,18 +38,15 @@ export const traverse = ({options, push}) => ({
38
38
  if (path.node.name.name !== 'li')
39
39
  return;
40
40
 
41
- if (!isJSXElement(path.parentPath.parentPath))
42
- return;
41
+ const parentParentPath = path.parentPath.parentPath;
43
42
 
44
- const openingElementPath = path.parentPath.parentPath.get('openingElement');
43
+ if (!isJSXElement(parentParentPath))
44
+ return;
45
45
 
46
- if (!checkDataName(openingElementPath, name))
46
+ if (!hasDataName(parentParentPath, name))
47
47
  return;
48
48
 
49
- const children = path.parentPath
50
- .parentPath
51
- .get('children')
52
- .filter(isJSXElement);
49
+ const children = parentParentPath.get('children').filter(isJSXElement);
53
50
 
54
51
  const prev = children[index - 1];
55
52
  const current = children[index];
@@ -58,8 +55,7 @@ export const traverse = ({options, push}) => ({
58
55
  if (!current)
59
56
  return;
60
57
 
61
- const currentOpeningElement = current.get('openingElement');
62
- const attributePath = getAttributePath(currentOpeningElement, 'className');
58
+ const attributePath = getAttributePath(current, 'className');
63
59
 
64
60
  push({
65
61
  path: attributePath,
@@ -78,8 +74,6 @@ function unselect(path) {
78
74
  function addShowSubmenu(path, {showSubmenu}) {
79
75
  if (showSubmenu)
80
76
  return appendAttributeValue(path, 'className', 'menu-submenu-show');
81
-
82
- removeAttributeValue(path, 'className', 'menu-submenu-show');
83
77
  }
84
78
 
85
79
  function removeShowSubmenu(path) {
@@ -0,0 +1,2 @@
1
+ <ul data-name="menu" className="menu menu-hidden" style="left: 0; top: 20px;">
2
+ </ul>;
@@ -1,5 +1,10 @@
1
- import {operator, types} from 'putout';
2
- import {checkDataName} from '../check-data-name.js';
1
+ import {types} from 'putout';
2
+ import {
3
+ getAttributePath,
4
+ getAttributeValue,
5
+ hasDataName,
6
+ setAttributeValue,
7
+ } from '../jsx-operator.js';
3
8
 
4
9
  const {
5
10
  stringLiteral,
@@ -7,56 +12,51 @@ const {
7
12
  jsxAttribute,
8
13
  } = types;
9
14
 
10
- const {setLiteralValue} = operator;
11
-
12
15
  export const report = () => `Set position`;
13
16
 
14
17
  export const fix = ({path, attr, x, y}) => {
15
18
  const style = `left: ${x}px; top: ${y}px;`;
16
19
 
17
20
  if (attr) {
18
- setLiteralValue(attr.value, style);
19
-
21
+ setAttributeValue(path, 'style', style);
20
22
  return;
21
23
  }
22
24
 
23
- const {attributes} = path.node;
24
-
25
+ const {attributes} = path.node.openingElement;
25
26
  const attribute = jsxAttribute(jsxIdentifier('style'), stringLiteral(style));
26
27
 
27
28
  attributes.push(attribute);
28
29
  };
29
30
 
30
31
  export const traverse = ({options, push}) => ({
31
- JSXOpeningElement(path) {
32
+ JSXElement(path) {
32
33
  const {name = 'menu', position = {}} = options;
33
34
  const {x = 0, y = 20} = position;
34
35
 
35
- const openingElementPath = path.parentPath.get('openingElement');
36
-
37
- if (!checkDataName(openingElementPath, name))
36
+ if (!hasDataName(path, name))
38
37
  return;
39
38
 
40
- for (const attr of path.node.attributes) {
41
- if (attr.name.name !== 'style')
42
- continue;
43
-
44
- const [x1, y1] = parsePosition(attr.value.value);
45
-
46
- if (x === x1 && y === y1)
47
- return;
48
-
39
+ const styleAttributeValue = getAttributeValue(path, 'style');
40
+
41
+ if (!styleAttributeValue) {
49
42
  push({
50
43
  path,
51
- attr,
52
44
  x,
53
45
  y,
54
46
  });
55
47
  return;
56
48
  }
57
49
 
50
+ const [x1, y1] = parsePosition(styleAttributeValue);
51
+
52
+ if (x === x1 && y === y1)
53
+ return;
54
+
55
+ const styleAttributePath = getAttributePath(path, 'style');
56
+
58
57
  push({
59
58
  path,
59
+ attr: styleAttributePath,
60
60
  x,
61
61
  y,
62
62
  });
@@ -68,5 +68,8 @@ function parsePosition(str) {
68
68
  .split(/;|left|\s|:|top|px/)
69
69
  .filter(Boolean);
70
70
 
71
- return [x, y];
71
+ return [
72
+ Number(x),
73
+ Number(y),
74
+ ];
72
75
  }
@@ -1,8 +1,8 @@
1
1
  import {operator, types} from 'putout';
2
- import {checkDataName} from '../check-data-name.js';
3
2
  import {
4
3
  getAttributePath,
5
4
  getAttributeValue,
5
+ hasDataName,
6
6
  removeAttributeValue,
7
7
  } from '../jsx-operator.js';
8
8
 
@@ -48,7 +48,7 @@ export const traverse = ({options, push}) => ({
48
48
 
49
49
  const openingElementPath = path.parentPath.parentPath.get('openingElement');
50
50
 
51
- if (!checkDataName(openingElementPath))
51
+ if (!hasDataName(openingElementPath))
52
52
  return;
53
53
 
54
54
  const children = path.parentPath
@@ -1,5 +1,5 @@
1
1
  import {operator, types} from 'putout';
2
- import {checkDataName} from '../check-data-name.js';
2
+ import {hasDataName} from '../jsx-operator.js';
3
3
 
4
4
  const {isJSXElement} = types;
5
5
  const {setLiteralValue} = operator;
@@ -28,7 +28,7 @@ export const traverse = ({push, options}) => ({
28
28
 
29
29
  const openingElementPath = path.parentPath.parentPath.get('openingElement');
30
30
 
31
- if (!checkDataName(openingElementPath, name))
31
+ if (!hasDataName(openingElementPath, name))
32
32
  return;
33
33
 
34
34
  const children = path.parentPath
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aleman",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
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,7 +0,0 @@
1
- import {getAttributeValue} from './jsx-operator.js';
2
-
3
- export function checkDataName(path, value = 'menu') {
4
- const attribute = getAttributeValue(path, 'data-name');
5
-
6
- return attribute === value;
7
- }