@codady/utils 0.0.22 → 0.0.24
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.md +4 -4
- package/dist/utils.cjs.js +174 -9
- package/dist/utils.cjs.min.js +3 -3
- package/dist/utils.esm.js +174 -9
- package/dist/utils.esm.min.js +3 -3
- package/dist/utils.umd.js +174 -9
- package/dist/utils.umd.min.js +3 -3
- package/dist.zip +0 -0
- package/modules.js +25 -3
- package/modules.ts +26 -3
- package/package.json +1 -1
- package/src/addClasses.js +41 -0
- package/src/addClasses.ts +42 -0
- package/src/alias.js +2 -0
- package/src/alias.ts +2 -0
- package/src/classes.js +114 -0
- package/src/classes.ts +140 -0
- package/src/comma.js +2 -0
- package/src/comma.ts +2 -0
- package/src/createTools.js +79 -0
- package/src/createTools.ts +129 -0
- package/src/fileToBase64.js +28 -0
- package/src/fileToBase64.ts +31 -0
- package/src/getClasses.js +16 -0
- package/src/getClasses.ts +20 -0
- package/src/getSvgUri.js +32 -0
- package/src/getSvgUri.ts +35 -0
- package/src/hasClasses.js +36 -0
- package/src/hasClasses.ts +39 -0
- package/src/namespace.js +2 -0
- package/src/namespace.ts +2 -0
- package/src/parseClasses.js +34 -0
- package/src/parseClasses.ts +39 -0
- package/src/removeClasses.js +44 -0
- package/src/removeClasses.ts +47 -0
- package/src/space.js +2 -0
- package/src/space.ts +2 -0
- package/src/trim.js +41 -0
- package/src/trim.ts +44 -0
- package/src/svgToBase64.js +0 -22
- package/src/svgToBase64.ts +0 -22
package/src/classes.ts
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/07 11:46:20
|
|
3
|
+
* @namespace classes
|
|
4
|
+
* @description A utility module for batch manipulation of CSS classes on DOM elements.
|
|
5
|
+
*/
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
import parseClasses from './parseClasses';
|
|
9
|
+
import getEl from './getEl';
|
|
10
|
+
import isEmpty from './isEmpty';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {Function} T_fn
|
|
14
|
+
* @description Type for a function that can be used for intercepting class operations
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @typedef {Object} T_classes
|
|
19
|
+
* @description Interface for class manipulation methods
|
|
20
|
+
* @property {T_fn} get - Method to retrieve current classes of the element
|
|
21
|
+
* @property {T_fn} add - Method to add classes to the element
|
|
22
|
+
* @property {T_fn} remove - Method to remove classes from the element
|
|
23
|
+
* @property {T_fn} replace - Method to replace an old class with a new one
|
|
24
|
+
* @property {T_fn} [has] - Method to check if element has specified classes
|
|
25
|
+
*/
|
|
26
|
+
type T_classes = {
|
|
27
|
+
get: () => string[],
|
|
28
|
+
add: (classes: string | string[], intercept?: (className: string) => string | boolean | void) => T_classes,
|
|
29
|
+
remove: (classes: string | string[], intercept?: (className: string) => string | boolean | void) => T_classes,
|
|
30
|
+
replace: (oldClass: string, newClass: string) => T_classes,
|
|
31
|
+
has: (classes: string | string[]) => boolean,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @description Creates an object with methods for manipulating CSS classes on a DOM element
|
|
36
|
+
* @param {string | Node} target - The target DOM element or selector (can be a CSS selector or an actual DOM element)
|
|
37
|
+
* @returns {T_classes} An object containing methods for class manipulation
|
|
38
|
+
*/
|
|
39
|
+
const classes = (target: string | Node): T_classes => {
|
|
40
|
+
let el = getEl(target);
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
/**
|
|
44
|
+
* @memberOf classes
|
|
45
|
+
* @description Retrieves an array of CSS classes currently applied to the element
|
|
46
|
+
* @returns {string[]} Array of class names
|
|
47
|
+
* @example
|
|
48
|
+
* ax.classes('#demo').get();
|
|
49
|
+
*/
|
|
50
|
+
get: (): string[] => el ? parseClasses((el as Element).getAttribute('class') || '') : [],
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @memberOf classes
|
|
54
|
+
* @description Adds one or more CSS classes to the element
|
|
55
|
+
* @param {string | string[]} classes - Class names to add (space/comma separated string or array)
|
|
56
|
+
* @param {Function} [intercept] - Optional callback to intercept class addition
|
|
57
|
+
* @returns {T_classes} The classes instance for chaining
|
|
58
|
+
* @example
|
|
59
|
+
* ax.classes('#demo').add('demo01,demo02');
|
|
60
|
+
*/
|
|
61
|
+
add: function (classes: string | string[], intercept?: (className: string) => string | boolean | void): T_classes {
|
|
62
|
+
let arr = parseClasses(classes);
|
|
63
|
+
if (!el || arr.length === 0) { return this; }
|
|
64
|
+
|
|
65
|
+
arr.forEach((k: string) => {
|
|
66
|
+
let tmp: any;
|
|
67
|
+
if (intercept) {
|
|
68
|
+
tmp = intercept(k);
|
|
69
|
+
tmp === true ? (el as Element).classList.add(k) :
|
|
70
|
+
(typeof tmp === 'string' && tmp) ? (el as Element).classList.add(tmp) : null;
|
|
71
|
+
} else {
|
|
72
|
+
(el as Element).classList.add(k);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
return this;
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @memberOf classes
|
|
80
|
+
* @description Removes one or more CSS classes from the element
|
|
81
|
+
* @param {string | string[]} classes - Class names to remove (space/comma separated string or array)
|
|
82
|
+
* @param {Function} [intercept] - Optional callback to intercept class removal
|
|
83
|
+
* @returns {T_classes} The classes instance for chaining
|
|
84
|
+
* @example
|
|
85
|
+
* ax.classes('#demo').remove('demo01,demo02');
|
|
86
|
+
*/
|
|
87
|
+
remove: function (classes: string | string[], intercept?: (className: string) => string | boolean | void): T_classes {
|
|
88
|
+
let arr = parseClasses(classes);
|
|
89
|
+
if (!el || arr.length === 0) return this;
|
|
90
|
+
|
|
91
|
+
arr.forEach((k: string) => {
|
|
92
|
+
let tmp: any;
|
|
93
|
+
if (intercept) {
|
|
94
|
+
tmp = intercept(k);
|
|
95
|
+
tmp === true ? (el as Element).classList.remove(k) :
|
|
96
|
+
(typeof tmp === 'string' && tmp) ? (el as Element).classList.remove(tmp) : null;
|
|
97
|
+
} else {
|
|
98
|
+
(el as Element).classList.remove(k);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
return this;
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* @memberOf classes
|
|
106
|
+
* @description Replaces an existing CSS class with a new one
|
|
107
|
+
* @param {string} oldClass - The class name to be replaced
|
|
108
|
+
* @param {string} newClass - The new class name to apply
|
|
109
|
+
* @returns {T_classes} The classes instance for chaining
|
|
110
|
+
*/
|
|
111
|
+
replace: function (oldClass: string, newClass: string): T_classes {
|
|
112
|
+
if (!el || !oldClass) return this;
|
|
113
|
+
if (el && newClass && oldClass) {
|
|
114
|
+
(el as Element).classList.remove(oldClass);
|
|
115
|
+
(el as Element).classList.add(newClass);
|
|
116
|
+
}
|
|
117
|
+
return this;
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @memberOf classes
|
|
122
|
+
* @description Checks if the element has all the specified CSS classes
|
|
123
|
+
* @param {string | string[]} classes - Class names to check (space/comma separated string or array)
|
|
124
|
+
* @returns {boolean} True if the element has all specified classes, false otherwise
|
|
125
|
+
*/
|
|
126
|
+
has: function (classes: string | string[]): boolean {
|
|
127
|
+
if (!el || isEmpty(classes)) return false;
|
|
128
|
+
|
|
129
|
+
let arr = parseClasses(classes);
|
|
130
|
+
for (let k of arr) {
|
|
131
|
+
if (!(el as Element).classList.contains(k)) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export default classes;
|
package/src/comma.js
ADDED
package/src/comma.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/07 13:37:57
|
|
3
|
+
* @function createTools
|
|
4
|
+
* @description Creates a group of icon button tools and appends them to a parent element.
|
|
5
|
+
* The `data` array can contain tool definitions, where each tool can be defined either as an object or a string shorthand.
|
|
6
|
+
* @param {toolsItem[]} data - The data representing the tools. Each item in the array can be an object with properties for the tool or a string representing a shorthand tool.
|
|
7
|
+
* @param {Element | string} parent - The DOM element or selector where the tools will be appended.
|
|
8
|
+
* @returns {HTMLElement} The container element that holds the tools.
|
|
9
|
+
* @example
|
|
10
|
+
* ax.createTools([{},{},'toggle','close']);
|
|
11
|
+
* <!-- Returns a node containing the tool buttons -->
|
|
12
|
+
*/
|
|
13
|
+
'use strict';
|
|
14
|
+
import createEl from './createEl';
|
|
15
|
+
import isEmpty from './isEmpty';
|
|
16
|
+
import ALIAS from './alias';
|
|
17
|
+
import NAMESPACE from './namespace';
|
|
18
|
+
import addClasses from './addClasses';
|
|
19
|
+
/**
|
|
20
|
+
* @description Creates a set of icon button tools and appends them to the specified parent element.
|
|
21
|
+
* @param {toolsItem[]} data - The tool data, each tool can be either an object or a shorthand string.
|
|
22
|
+
* @returns {HTMLElement} The container DOM element that holds the tools.
|
|
23
|
+
*/
|
|
24
|
+
const createTools = (data) => {
|
|
25
|
+
/*! data = [
|
|
26
|
+
{
|
|
27
|
+
name: 'close',
|
|
28
|
+
nodeName: 'span',
|
|
29
|
+
icon:'',
|
|
30
|
+
disk:'',
|
|
31
|
+
cube:'',
|
|
32
|
+
image:'',
|
|
33
|
+
attrs: {},
|
|
34
|
+
classes:'',
|
|
35
|
+
styles:'',
|
|
36
|
+
label:'',
|
|
37
|
+
expandable:false,
|
|
38
|
+
focusable:true,
|
|
39
|
+
action: null,
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
name: 'max',
|
|
43
|
+
nodeName: 'span',
|
|
44
|
+
icon:'',
|
|
45
|
+
attrs: {},
|
|
46
|
+
classes:'',
|
|
47
|
+
styles:'',
|
|
48
|
+
label:'',
|
|
49
|
+
expandable:false,
|
|
50
|
+
action: null,
|
|
51
|
+
},
|
|
52
|
+
] */
|
|
53
|
+
const toolsEl = createEl('span', { class: `${NAMESPACE}-box-tools` }), renderFn = (props) => {
|
|
54
|
+
const dftAttrs = {}, arrow = props.extendable ? `<i ${ALIAS}="arrow"></i>` : '', iconStr = props.icon ? `<i ${ALIAS}="icon">${props.icon}</i>` : '', diskStr = props.disk ? `<i ${ALIAS}="disk"><img src="${props.disk}"/></i>` : '', cubeStr = props.cube ? `<i ${ALIAS}="cube"><img src="${props.cube}"/></i>` : '', imageStr = props.image ? `<i ${ALIAS}="image"><img src="${props.image}"/></i>` : '', label = props.label ? `<i ${ALIAS}="label">${props.label}</i>` : '', html = iconStr + diskStr + cubeStr + imageStr + label + arrow;
|
|
55
|
+
//使用title提示
|
|
56
|
+
props.title && (dftAttrs.title = props.title);
|
|
57
|
+
//可聚焦,增加tabindex=1
|
|
58
|
+
props.focusable && (dftAttrs.tabindex = 1);
|
|
59
|
+
//attrs是其他属性,可能会覆盖title、tabindex
|
|
60
|
+
props.wrapEl = createEl(props.nodeName || 'span', Object.assign(dftAttrs, props.attrs), html);
|
|
61
|
+
props.iconEl = props.wrapEl.querySelector(`[${ALIAS}="icon"]`);
|
|
62
|
+
props.cubeEl = props.wrapEl.querySelector(`[${ALIAS}="cube"]`);
|
|
63
|
+
props.diskEl = props.wrapEl.querySelector(`[${ALIAS}="disk"]`);
|
|
64
|
+
props.imageEl = props.wrapEl.querySelector(`[${ALIAS}="image"]`);
|
|
65
|
+
props.labelEl = props.wrapEl.querySelector(`[${ALIAS}="label"]`);
|
|
66
|
+
//增加classes和styles
|
|
67
|
+
!isEmpty(props.classes) && addClasses(props.wrapEl, props.classes);
|
|
68
|
+
!isEmpty(props.styles) && (props.wrapEl.style.cssText += props.styles);
|
|
69
|
+
};
|
|
70
|
+
//此处不用map方法,是避免改变原data的内存地址指向
|
|
71
|
+
for (let item of data) {
|
|
72
|
+
//data=[{},{},'toggle','close']
|
|
73
|
+
renderFn(item);
|
|
74
|
+
toolsEl.appendChild(item.wrapEl);
|
|
75
|
+
item?.action?.(item);
|
|
76
|
+
}
|
|
77
|
+
return toolsEl;
|
|
78
|
+
};
|
|
79
|
+
export default createTools;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/07 13:37:57
|
|
3
|
+
* @function createTools
|
|
4
|
+
* @description Creates a group of icon button tools and appends them to a parent element.
|
|
5
|
+
* The `data` array can contain tool definitions, where each tool can be defined either as an object or a string shorthand.
|
|
6
|
+
* @param {toolsItem[]} data - The data representing the tools. Each item in the array can be an object with properties for the tool or a string representing a shorthand tool.
|
|
7
|
+
* @param {Element | string} parent - The DOM element or selector where the tools will be appended.
|
|
8
|
+
* @returns {HTMLElement} The container element that holds the tools.
|
|
9
|
+
* @example
|
|
10
|
+
* ax.createTools([{},{},'toggle','close']);
|
|
11
|
+
* <!-- Returns a node containing the tool buttons -->
|
|
12
|
+
*/
|
|
13
|
+
'use strict';
|
|
14
|
+
|
|
15
|
+
import classes from './classes';
|
|
16
|
+
import createEl from './createEl';
|
|
17
|
+
import isEmpty from './isEmpty';
|
|
18
|
+
import ALIAS from './alias';
|
|
19
|
+
import NAMESPACE from './namespace';
|
|
20
|
+
import addClasses from './addClasses';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @typedef {Object} toolsItem
|
|
24
|
+
* @description Represents a single tool item with properties that define its behavior and appearance.
|
|
25
|
+
* @property {string} name - The name of the tool (required).
|
|
26
|
+
* @property {string} [nodeName='span'] - The HTML tag to use for the tool element (optional, default is 'span').
|
|
27
|
+
* @property {string} [icon] - The icon to display for the tool (optional).
|
|
28
|
+
* @property {string} [disk] - The URL of the disk image for the tool (optional).
|
|
29
|
+
* @property {string} [cube] - The URL of the cube image for the tool (optional).
|
|
30
|
+
* @property {string} [image] - The URL of the image to display for the tool (optional).
|
|
31
|
+
* @property {string | string[]} [classes] - CSS class names to apply to the tool (optional).
|
|
32
|
+
* @property {string} [styles] - Inline styles to apply to the tool (optional).
|
|
33
|
+
* @property {string} [label] - A label to display next to the tool (optional).
|
|
34
|
+
* @property {boolean} [expandable=false] - Whether the tool is expandable (optional, default is false).
|
|
35
|
+
* @property {string} [title] - A title to show as a tooltip when the user hovers over the tool (optional).
|
|
36
|
+
* @property {boolean} [focusable=false] - Whether the tool is focusable via keyboard (optional, default is false).
|
|
37
|
+
* @property {Function | null} [action=null] - A callback function to be executed when the tool is activated (optional).
|
|
38
|
+
* @property {Record<string, string>} [attrs] - Additional attributes to add to the tool element (optional).
|
|
39
|
+
* @property {any} [key: string] - Other unspecified properties that can be added dynamically.
|
|
40
|
+
*/
|
|
41
|
+
export type toolsItem = {
|
|
42
|
+
name: string;
|
|
43
|
+
nodeName?: string;
|
|
44
|
+
icon?: string;
|
|
45
|
+
disk?: string;
|
|
46
|
+
cube?: string;
|
|
47
|
+
image?: string;
|
|
48
|
+
classes?: string | string[];
|
|
49
|
+
styles?: string;
|
|
50
|
+
label?: string;
|
|
51
|
+
expandable?: boolean;
|
|
52
|
+
title?: string;
|
|
53
|
+
focusable?: boolean;
|
|
54
|
+
action?: null | Function;
|
|
55
|
+
attrs?: Record<string, string>;
|
|
56
|
+
[key: string]: any;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @description Creates a set of icon button tools and appends them to the specified parent element.
|
|
61
|
+
* @param {toolsItem[]} data - The tool data, each tool can be either an object or a shorthand string.
|
|
62
|
+
* @returns {HTMLElement} The container DOM element that holds the tools.
|
|
63
|
+
*/
|
|
64
|
+
const createTools = (data: toolsItem[]):HTMLElement => {
|
|
65
|
+
/*! data = [
|
|
66
|
+
{
|
|
67
|
+
name: 'close',
|
|
68
|
+
nodeName: 'span',
|
|
69
|
+
icon:'',
|
|
70
|
+
disk:'',
|
|
71
|
+
cube:'',
|
|
72
|
+
image:'',
|
|
73
|
+
attrs: {},
|
|
74
|
+
classes:'',
|
|
75
|
+
styles:'',
|
|
76
|
+
label:'',
|
|
77
|
+
expandable:false,
|
|
78
|
+
focusable:true,
|
|
79
|
+
action: null,
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
name: 'max',
|
|
83
|
+
nodeName: 'span',
|
|
84
|
+
icon:'',
|
|
85
|
+
attrs: {},
|
|
86
|
+
classes:'',
|
|
87
|
+
styles:'',
|
|
88
|
+
label:'',
|
|
89
|
+
expandable:false,
|
|
90
|
+
action: null,
|
|
91
|
+
},
|
|
92
|
+
] */
|
|
93
|
+
const toolsEl = createEl('span', { class: `${NAMESPACE}-box-tools` }),
|
|
94
|
+
renderFn = (props: any) => {
|
|
95
|
+
const dftAttrs: any = {},
|
|
96
|
+
arrow = props.extendable ? `<i ${ALIAS}="arrow"></i>` : '',
|
|
97
|
+
iconStr = props.icon ? `<i ${ALIAS}="icon">${props.icon}</i>` : '',
|
|
98
|
+
diskStr = props.disk ? `<i ${ALIAS}="disk"><img src="${props.disk}"/></i>` : '',
|
|
99
|
+
cubeStr = props.cube ? `<i ${ALIAS}="cube"><img src="${props.cube}"/></i>` : '',
|
|
100
|
+
imageStr = props.image ? `<i ${ALIAS}="image"><img src="${props.image}"/></i>` : '',
|
|
101
|
+
label = props.label ? `<i ${ALIAS}="label">${props.label}</i>` : '',
|
|
102
|
+
html = iconStr + diskStr + cubeStr + imageStr + label + arrow;
|
|
103
|
+
//使用title提示
|
|
104
|
+
props.title && (dftAttrs.title = props.title);
|
|
105
|
+
//可聚焦,增加tabindex=1
|
|
106
|
+
props.focusable && (dftAttrs.tabindex = 1);
|
|
107
|
+
//attrs是其他属性,可能会覆盖title、tabindex
|
|
108
|
+
props.wrapEl = createEl(props.nodeName || 'span', Object.assign(dftAttrs, props.attrs), html);
|
|
109
|
+
props.iconEl = props.wrapEl.querySelector(`[${ALIAS}="icon"]`);
|
|
110
|
+
props.cubeEl = props.wrapEl.querySelector(`[${ALIAS}="cube"]`);
|
|
111
|
+
props.diskEl = props.wrapEl.querySelector(`[${ALIAS}="disk"]`);
|
|
112
|
+
props.imageEl = props.wrapEl.querySelector(`[${ALIAS}="image"]`);
|
|
113
|
+
props.labelEl = props.wrapEl.querySelector(`[${ALIAS}="label"]`);
|
|
114
|
+
//增加classes和styles
|
|
115
|
+
!isEmpty(props.classes) && addClasses(props.wrapEl,props.classes);
|
|
116
|
+
!isEmpty(props.styles) && (props.wrapEl.style.cssText += props.styles);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
//此处不用map方法,是避免改变原data的内存地址指向
|
|
120
|
+
for (let item of data) {
|
|
121
|
+
//data=[{},{},'toggle','close']
|
|
122
|
+
renderFn(item);
|
|
123
|
+
toolsEl.appendChild(item.wrapEl);
|
|
124
|
+
item?.action?.(item);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return toolsEl;
|
|
128
|
+
}
|
|
129
|
+
export default createTools;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/07 08:54:36
|
|
3
|
+
* Converts a File or Blob object into a Base64 encoded string (Data URI format).
|
|
4
|
+
* * @param file - The File or Blob object to be converted.
|
|
5
|
+
* @returns A promise that resolves to the Data URI string (e.g., "data:image/png;base64,...").
|
|
6
|
+
*/
|
|
7
|
+
const fileToBase64 = (file) => {
|
|
8
|
+
return new Promise((resolve, reject) => {
|
|
9
|
+
const reader = new FileReader();
|
|
10
|
+
// Triggered when the read operation is successfully completed
|
|
11
|
+
reader.onload = () => {
|
|
12
|
+
// When using readAsDataURL, the result is guaranteed to be a string
|
|
13
|
+
if (typeof reader.result === 'string') {
|
|
14
|
+
resolve(reader.result);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
reject(new Error('FileReader result is not a string'));
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
// Triggered when the read operation encounters an error
|
|
21
|
+
reader.onerror = () => {
|
|
22
|
+
reject(reader.error || new Error('Unknown error occurred during file reading'));
|
|
23
|
+
};
|
|
24
|
+
// Initiates the reading process to convert the file into a Data URL
|
|
25
|
+
reader.readAsDataURL(file);
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
export default fileToBase64;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/07 08:54:36
|
|
3
|
+
* Converts a File or Blob object into a Base64 encoded string (Data URI format).
|
|
4
|
+
* * @param file - The File or Blob object to be converted.
|
|
5
|
+
* @returns A promise that resolves to the Data URI string (e.g., "data:image/png;base64,...").
|
|
6
|
+
*/
|
|
7
|
+
const fileToBase64 = (file: File | Blob): Promise<string> => {
|
|
8
|
+
return new Promise((resolve, reject) => {
|
|
9
|
+
const reader = new FileReader();
|
|
10
|
+
|
|
11
|
+
// Triggered when the read operation is successfully completed
|
|
12
|
+
reader.onload = () => {
|
|
13
|
+
// When using readAsDataURL, the result is guaranteed to be a string
|
|
14
|
+
if (typeof reader.result === 'string') {
|
|
15
|
+
resolve(reader.result);
|
|
16
|
+
} else {
|
|
17
|
+
reject(new Error('FileReader result is not a string'));
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// Triggered when the read operation encounters an error
|
|
22
|
+
reader.onerror = () => {
|
|
23
|
+
reject(reader.error || new Error('Unknown error occurred during file reading'));
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// Initiates the reading process to convert the file into a Data URL
|
|
27
|
+
reader.readAsDataURL(file);
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export default fileToBase64;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/07 12:05:04
|
|
3
|
+
* @namespace getClasses
|
|
4
|
+
* @description Utility function to retrieve the list of CSS classes applied to a DOM element.
|
|
5
|
+
* This function will fetch the `class` attribute from a DOM element and parse it into an array of class names.
|
|
6
|
+
*/
|
|
7
|
+
'use strict';
|
|
8
|
+
import getEl from "./getEl";
|
|
9
|
+
import parseClasses from "./parseClasses";
|
|
10
|
+
const getClasses = (target) => {
|
|
11
|
+
// Get the DOM element from the selector or Node
|
|
12
|
+
let el = getEl(target);
|
|
13
|
+
// If the element is found, parse and return its classes; otherwise, return an empty array
|
|
14
|
+
return el ? parseClasses(el.getAttribute('class') || '') : [];
|
|
15
|
+
};
|
|
16
|
+
export default getClasses;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/07 12:05:04
|
|
3
|
+
* @namespace getClasses
|
|
4
|
+
* @description Utility function to retrieve the list of CSS classes applied to a DOM element.
|
|
5
|
+
* This function will fetch the `class` attribute from a DOM element and parse it into an array of class names.
|
|
6
|
+
*/
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
import getEl from "./getEl";
|
|
10
|
+
import parseClasses from "./parseClasses";
|
|
11
|
+
|
|
12
|
+
const getClasses = (target: string | Node | null): string[] => {
|
|
13
|
+
// Get the DOM element from the selector or Node
|
|
14
|
+
let el = getEl(target);
|
|
15
|
+
|
|
16
|
+
// If the element is found, parse and return its classes; otherwise, return an empty array
|
|
17
|
+
return el ? parseClasses((el as Element).getAttribute('class') || '') : [];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default getClasses;
|
package/src/getSvgUri.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/07 08:53:59
|
|
3
|
+
* Converts a raw SVG string into a UTF-8 encoded Data URI.
|
|
4
|
+
* * Unlike Base64, this format keeps the SVG human-readable and
|
|
5
|
+
* is typically more space-efficient for text-based graphics.
|
|
6
|
+
* * @param svgString - The raw SVG source code to convert.
|
|
7
|
+
* @returns A formatted Data URI string (e.g., "data:image/svg+xml;utf8,...")
|
|
8
|
+
*/
|
|
9
|
+
const getSvgUri = (svgString) => {
|
|
10
|
+
// 1. Minify the SVG: Remove line breaks and collapse multiple spaces
|
|
11
|
+
// to ensure the resulting URI is a single, clean line.
|
|
12
|
+
const cleanedSvg = svgString
|
|
13
|
+
.replace(/\n/g, '')
|
|
14
|
+
.replace(/\s+/g, ' ')
|
|
15
|
+
.trim();
|
|
16
|
+
// 2. Character Encoding:
|
|
17
|
+
// We escape specific characters that have special meanings in URLs or
|
|
18
|
+
// can break CSS parser logic (especially when used in background-image: url()).
|
|
19
|
+
// - '#' (Hash) is escaped to '%23' because it's a URL fragment identifier.
|
|
20
|
+
// - '%' (Percent) is escaped to '%25' as it is the escape character itself.
|
|
21
|
+
// - '<' and '>' are escaped to '%3C' and '%3E' for URL safety.
|
|
22
|
+
// - '{' and '}' are escaped to '%7B' and '%7D' to avoid conflicts with CSS block syntax.
|
|
23
|
+
const encoded = cleanedSvg
|
|
24
|
+
.replace(/%/g, '%25')
|
|
25
|
+
.replace(/#/g, '%23')
|
|
26
|
+
.replace(/{/g, '%7B')
|
|
27
|
+
.replace(/}/g, '%7D')
|
|
28
|
+
.replace(/</g, '%3C')
|
|
29
|
+
.replace(/>/g, '%3E');
|
|
30
|
+
return `data:image/svg+xml;utf8,${encoded}`;
|
|
31
|
+
};
|
|
32
|
+
export default getSvgUri;
|
package/src/getSvgUri.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/07 08:53:59
|
|
3
|
+
* Converts a raw SVG string into a UTF-8 encoded Data URI.
|
|
4
|
+
* * Unlike Base64, this format keeps the SVG human-readable and
|
|
5
|
+
* is typically more space-efficient for text-based graphics.
|
|
6
|
+
* * @param svgString - The raw SVG source code to convert.
|
|
7
|
+
* @returns A formatted Data URI string (e.g., "data:image/svg+xml;utf8,...")
|
|
8
|
+
*/
|
|
9
|
+
const getSvgUri = (svgString: string): string => {
|
|
10
|
+
// 1. Minify the SVG: Remove line breaks and collapse multiple spaces
|
|
11
|
+
// to ensure the resulting URI is a single, clean line.
|
|
12
|
+
const cleanedSvg = svgString
|
|
13
|
+
.replace(/\n/g, '')
|
|
14
|
+
.replace(/\s+/g, ' ')
|
|
15
|
+
.trim();
|
|
16
|
+
|
|
17
|
+
// 2. Character Encoding:
|
|
18
|
+
// We escape specific characters that have special meanings in URLs or
|
|
19
|
+
// can break CSS parser logic (especially when used in background-image: url()).
|
|
20
|
+
// - '#' (Hash) is escaped to '%23' because it's a URL fragment identifier.
|
|
21
|
+
// - '%' (Percent) is escaped to '%25' as it is the escape character itself.
|
|
22
|
+
// - '<' and '>' are escaped to '%3C' and '%3E' for URL safety.
|
|
23
|
+
// - '{' and '}' are escaped to '%7B' and '%7D' to avoid conflicts with CSS block syntax.
|
|
24
|
+
const encoded = cleanedSvg
|
|
25
|
+
.replace(/%/g, '%25')
|
|
26
|
+
.replace(/#/g, '%23')
|
|
27
|
+
.replace(/{/g, '%7B')
|
|
28
|
+
.replace(/}/g, '%7D')
|
|
29
|
+
.replace(/</g, '%3C')
|
|
30
|
+
.replace(/>/g, '%3E');
|
|
31
|
+
|
|
32
|
+
return `data:image/svg+xml;utf8,${encoded}`;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export default getSvgUri;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/07 12:10:08
|
|
3
|
+
* @namespace hasClasses
|
|
4
|
+
* @description Checks if the given DOM element has all of the specified CSS classes.
|
|
5
|
+
*
|
|
6
|
+
* This function takes one or more class names and checks if the target DOM element contains **all** the specified classes.
|
|
7
|
+
* It returns `true` if the element has all the specified classes, otherwise `false`.
|
|
8
|
+
*
|
|
9
|
+
* @param {string | Node | null} target - The target DOM element, Node, or CSS selector to check for the classes.
|
|
10
|
+
* - If a string, it is interpreted as a CSS selector (e.g., `#elementId`).
|
|
11
|
+
* - If a DOM node or element is passed, it is used directly.
|
|
12
|
+
* - If `null` is passed, the function will return `false` because there is no element to check.
|
|
13
|
+
* @param {string | string[]} classes - A string or an array of class names to check.
|
|
14
|
+
* - A space/comma-separated string of class names (e.g., `'class1 class2'`).
|
|
15
|
+
* - An array of class names (e.g., `['class1', 'class2']`).
|
|
16
|
+
* @returns {boolean} Returns `true` if the element has all of the specified classes, otherwise `false`.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ax.hasClasses('#demo', 'demo01 demo02'); // Returns true if element with id 'demo' has both 'demo01' and 'demo02'
|
|
20
|
+
* ax.hasClasses('#demo', ['demo01', 'demo02']); // Returns true if element with id 'demo' has both 'demo01' and 'demo02'
|
|
21
|
+
*/
|
|
22
|
+
'use strict';
|
|
23
|
+
import getEl from "./getEl";
|
|
24
|
+
import parseClasses from "./parseClasses";
|
|
25
|
+
const hasClasses = (target, classes) => {
|
|
26
|
+
const el = getEl(target), arr = parseClasses(classes);
|
|
27
|
+
if (!el || arr.length === 0)
|
|
28
|
+
return false;
|
|
29
|
+
for (let k of arr) {
|
|
30
|
+
if (!el.classList.contains(k)) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
};
|
|
36
|
+
export default hasClasses;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/07 12:10:08
|
|
3
|
+
* @namespace hasClasses
|
|
4
|
+
* @description Checks if the given DOM element has all of the specified CSS classes.
|
|
5
|
+
*
|
|
6
|
+
* This function takes one or more class names and checks if the target DOM element contains **all** the specified classes.
|
|
7
|
+
* It returns `true` if the element has all the specified classes, otherwise `false`.
|
|
8
|
+
*
|
|
9
|
+
* @param {string | Node | null} target - The target DOM element, Node, or CSS selector to check for the classes.
|
|
10
|
+
* - If a string, it is interpreted as a CSS selector (e.g., `#elementId`).
|
|
11
|
+
* - If a DOM node or element is passed, it is used directly.
|
|
12
|
+
* - If `null` is passed, the function will return `false` because there is no element to check.
|
|
13
|
+
* @param {string | string[]} classes - A string or an array of class names to check.
|
|
14
|
+
* - A space/comma-separated string of class names (e.g., `'class1 class2'`).
|
|
15
|
+
* - An array of class names (e.g., `['class1', 'class2']`).
|
|
16
|
+
* @returns {boolean} Returns `true` if the element has all of the specified classes, otherwise `false`.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ax.hasClasses('#demo', 'demo01 demo02'); // Returns true if element with id 'demo' has both 'demo01' and 'demo02'
|
|
20
|
+
* ax.hasClasses('#demo', ['demo01', 'demo02']); // Returns true if element with id 'demo' has both 'demo01' and 'demo02'
|
|
21
|
+
*/
|
|
22
|
+
'use strict';
|
|
23
|
+
|
|
24
|
+
import getEl from "./getEl";
|
|
25
|
+
import parseClasses from "./parseClasses";
|
|
26
|
+
|
|
27
|
+
const hasClasses = (target: string | Node | null, classes: string | string[]): boolean => {
|
|
28
|
+
const el = getEl(target),
|
|
29
|
+
arr = parseClasses(classes);
|
|
30
|
+
if (!el || arr.length === 0) return false;
|
|
31
|
+
|
|
32
|
+
for (let k of arr) {
|
|
33
|
+
if (!(el as Element).classList.contains(k)) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
export default hasClasses;
|
package/src/namespace.js
ADDED
package/src/namespace.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since Last modified: 2026/01/07 11:45:59
|
|
3
|
+
* @function parseClasses
|
|
4
|
+
* @description Converts a string or an array of class names into an array of class names.
|
|
5
|
+
* If a string is provided, it can be a comma-separated or space-separated list of class names.
|
|
6
|
+
* The function cleans up any unnecessary spaces and ensures the result is an array of strings.
|
|
7
|
+
* @param {string | string[]} data - A string containing class names separated by commas or spaces, or an array of class names.
|
|
8
|
+
* @returns {string[]} - Returns an array of class names as strings.
|
|
9
|
+
* @example
|
|
10
|
+
* parseClasses('demo01,demo02'); // Returns ['demo01', 'demo02']
|
|
11
|
+
* parseClasses('demo01 demo02'); // Returns ['demo01', 'demo02']
|
|
12
|
+
* parseClasses(['demo01', 'demo02']); // Returns ['demo01', 'demo02']
|
|
13
|
+
*/
|
|
14
|
+
'use strict';
|
|
15
|
+
import COMMA from "./comma";
|
|
16
|
+
import SPACE from "./space";
|
|
17
|
+
import trim from "./trim";
|
|
18
|
+
const parseClasses = (data) => {
|
|
19
|
+
let separator, result = [];
|
|
20
|
+
if (Array.isArray(data)) {
|
|
21
|
+
// If data is already an array, filter out invalid values
|
|
22
|
+
result = data.filter((k) => k && typeof k === 'string');
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
// Trim the input string and handle multiple spaces
|
|
26
|
+
data = trim(data);
|
|
27
|
+
// Use comma as the separator if present, otherwise use space
|
|
28
|
+
separator = data.includes(COMMA) ? COMMA : SPACE;
|
|
29
|
+
result = data.split(separator);
|
|
30
|
+
}
|
|
31
|
+
// Trim each item globally and filter out any empty strings
|
|
32
|
+
return result.map((k) => trim(k, 'global')).filter(Boolean);
|
|
33
|
+
};
|
|
34
|
+
export default parseClasses;
|