@js-toolkit/web-utils 1.66.0 → 1.67.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/package.json +10 -10
- package/responsive/MediaQuery.d.ts +18 -0
- package/responsive/MediaQuery.js +1 -0
- package/responsive/MediaQueryListener.d.ts +19 -0
- package/responsive/MediaQueryListener.js +1 -0
- package/responsive/ViewSize.d.ts +45 -0
- package/responsive/ViewSize.js +1 -0
- package/responsive/getViewSizeQueryMap.d.ts +2 -0
- package/responsive/getViewSizeQueryMap.js +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@js-toolkit/web-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.67.0",
|
|
4
4
|
"description": "Web utils",
|
|
5
5
|
"author": "VZH",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,11 +20,11 @@
|
|
|
20
20
|
"@js-toolkit/node-utils": "1.2.6"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@eslint/compat": "2.0.
|
|
23
|
+
"@eslint/compat": "2.0.1",
|
|
24
24
|
"@eslint/eslintrc": "3.3.3",
|
|
25
25
|
"@eslint/js": "9.39.2",
|
|
26
|
-
"@js-toolkit/configs": "3.99.
|
|
27
|
-
"@js-toolkit/utils": "1.
|
|
26
|
+
"@js-toolkit/configs": "3.99.1",
|
|
27
|
+
"@js-toolkit/utils": "1.61.0",
|
|
28
28
|
"@types/eslint": "9.6.1",
|
|
29
29
|
"@types/lodash.throttle": "4.1.9",
|
|
30
30
|
"@types/uuid": "11.0.0",
|
|
@@ -33,17 +33,17 @@
|
|
|
33
33
|
"eslint-config-prettier": "10.1.8",
|
|
34
34
|
"eslint-import-resolver-typescript": "4.4.4",
|
|
35
35
|
"eslint-plugin-import-x": "4.16.1",
|
|
36
|
-
"eslint-plugin-prettier": "5.5.
|
|
36
|
+
"eslint-plugin-prettier": "5.5.5",
|
|
37
37
|
"lodash.throttle": "4.1.1",
|
|
38
|
-
"prettier": "3.
|
|
38
|
+
"prettier": "3.8.0",
|
|
39
39
|
"reconnecting-websocket": "4.4.0",
|
|
40
40
|
"rimraf": "6.1.2",
|
|
41
|
-
"terser": "5.
|
|
41
|
+
"terser": "5.46.0",
|
|
42
42
|
"typescript": "5.9.3",
|
|
43
|
-
"typescript-eslint": "8.
|
|
44
|
-
"ua-parser-js": "2.0.
|
|
43
|
+
"typescript-eslint": "8.53.1",
|
|
44
|
+
"ua-parser-js": "2.0.8",
|
|
45
45
|
"uuid": "13.0.0",
|
|
46
|
-
"webpack": "5.104.
|
|
46
|
+
"webpack": "5.104.1",
|
|
47
47
|
"yargs": "18.0.0"
|
|
48
48
|
}
|
|
49
49
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { MediaQueryListener, type MediaQueryEventHandler } from './MediaQueryListener';
|
|
2
|
+
import type { ViewSize } from './ViewSize';
|
|
3
|
+
export type { MediaQueryEvent, MediaQueryEventHandler } from './MediaQueryListener';
|
|
4
|
+
export declare abstract class MediaQuery {
|
|
5
|
+
private static _listener;
|
|
6
|
+
static get listener(): MediaQueryListener;
|
|
7
|
+
static get isInitialized(): boolean;
|
|
8
|
+
static get currentViewSize(): ViewSize;
|
|
9
|
+
static get currentViewSizeValues(): ViewSize.Values;
|
|
10
|
+
static addListener(listener: MediaQueryEventHandler): void;
|
|
11
|
+
static removeListener(listener: MediaQueryEventHandler): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Init all media queries for handle changes.
|
|
14
|
+
* Safe for multiple calls.
|
|
15
|
+
*/
|
|
16
|
+
static init(): ViewSize;
|
|
17
|
+
static destroy(): void;
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{MediaQueryListener}from"./MediaQueryListener";export class MediaQuery{static _listener;static get listener(){if(!this._listener)throw new Error("Media queries is not initialized. You should initialize it before by call `init` method.");return this._listener}static get isInitialized(){return null!=this._listener}static get currentViewSize(){return this.listener.currentViewSize}static get currentViewSizeValues(){return this.listener.currentViewSizeQuery}static addListener(e){this.listener.addListener(e)}static removeListener(e){return this.listener.removeListener(e)}static init(){return this._listener??=new MediaQueryListener,this.currentViewSize}static destroy(){this._listener&&this._listener.destroy()}}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ViewSize } from './ViewSize';
|
|
2
|
+
export interface MediaQueryEvent extends Pick<MediaQueryListEvent, 'matches'> {
|
|
3
|
+
readonly viewSize: ViewSize;
|
|
4
|
+
}
|
|
5
|
+
export type MediaQueryEventHandler = (event: MediaQueryEvent) => void;
|
|
6
|
+
export declare class MediaQueryListener implements Disposable {
|
|
7
|
+
private _currentViewSize;
|
|
8
|
+
private readonly mediaQueries;
|
|
9
|
+
private readonly listeners;
|
|
10
|
+
readonly queries: Readonly<Record<ViewSize, string>>;
|
|
11
|
+
get currentViewSize(): ViewSize;
|
|
12
|
+
get currentViewSizeQuery(): ViewSize.Values;
|
|
13
|
+
/** Init all media queries for handle changes. */
|
|
14
|
+
constructor();
|
|
15
|
+
addListener(listener: MediaQueryEventHandler): void;
|
|
16
|
+
removeListener(listener: MediaQueryEventHandler): boolean;
|
|
17
|
+
destroy(): void;
|
|
18
|
+
[Symbol.dispose](): void;
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ViewSize}from"./ViewSize";import{getViewSizeQueryMap}from"./getViewSizeQueryMap";export class MediaQueryListener{_currentViewSize;mediaQueries=[];listeners=new Set;queries;get currentViewSize(){if(!this._currentViewSize)throw new Error(`Media queries is not initialized properly. Current view size is ${String(this._currentViewSize)}.`);return this._currentViewSize}get currentViewSizeQuery(){return ViewSize.values[this.currentViewSize]}constructor(){this.queries=getViewSizeQueryMap(),Object.entries(this.queries).forEach(([e,i])=>{const r=ViewSize.of(e),t=window.matchMedia(i);this.mediaQueries.push(t),t.onchange=({matches:e})=>{e&&(this._currentViewSize=r),this.listeners.forEach(i=>i({viewSize:r,matches:e}))},t.matches&&(this._currentViewSize=r)})}addListener(e){this.listeners.add(e)}removeListener(e){return this.listeners.delete(e)}destroy(){this.mediaQueries.splice(0,this.mediaQueries.length).forEach(e=>{e.onchange=null}),this.listeners.clear()}[Symbol.dispose](){return this.destroy()}}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export declare enum ViewSize {
|
|
2
|
+
/** minWidth=0, maxWidth=319 */
|
|
3
|
+
xxxxs = 1,
|
|
4
|
+
/** minWidth=320, maxWidth=479 */
|
|
5
|
+
xxxs = 2,
|
|
6
|
+
/** minWidth=480, maxWidth=639 */
|
|
7
|
+
xxs = 3,
|
|
8
|
+
/** minWidth=640, maxWidth=827 */
|
|
9
|
+
xs = 4,
|
|
10
|
+
/** minWidth=828, maxWidth=1023 */
|
|
11
|
+
s = 5,
|
|
12
|
+
/** minWidth=1024, maxWidth=1365 */
|
|
13
|
+
m = 6,
|
|
14
|
+
/** minWidth=1366, maxWidth=1599 */
|
|
15
|
+
l = 7,
|
|
16
|
+
/** minWidth=1600, maxWidth=1919 */
|
|
17
|
+
xl = 8,
|
|
18
|
+
/** minWidth=1920, maxWidth=2559 */
|
|
19
|
+
xxl = 9,
|
|
20
|
+
/** minWidth=2560, maxWidth=3839 */
|
|
21
|
+
xxxl = 10,
|
|
22
|
+
/** minWidth=3840, maxWidth=7679 */
|
|
23
|
+
xxxxl = 11,
|
|
24
|
+
/** minWidth=7680, maxWidth=Number.MAX_SAFE_INTEGER */
|
|
25
|
+
xxxxxl = 12
|
|
26
|
+
}
|
|
27
|
+
export declare namespace ViewSize {
|
|
28
|
+
type Type = ExtractKeysOfType<typeof ViewSize, number>;
|
|
29
|
+
type Keys = keyof Type;
|
|
30
|
+
interface Values {
|
|
31
|
+
readonly minWidth: number;
|
|
32
|
+
readonly maxWidth: number;
|
|
33
|
+
}
|
|
34
|
+
/** All values are unique. */
|
|
35
|
+
const values: Readonly<Record<ViewSize, Values>>;
|
|
36
|
+
function of(viewSizeNumber: string): ViewSize;
|
|
37
|
+
function keyOf(viewSize: ViewSize): Keys;
|
|
38
|
+
/** Sorted values. */
|
|
39
|
+
const valueList: readonly (readonly [ViewSize, Values])[];
|
|
40
|
+
function get(width: number): ViewSize;
|
|
41
|
+
function lt(size: ViewSize, than: ViewSize): boolean;
|
|
42
|
+
function lte(size: ViewSize, than: ViewSize): boolean;
|
|
43
|
+
function gt(size: ViewSize, than: ViewSize): boolean;
|
|
44
|
+
function gte(size: ViewSize, than: ViewSize): boolean;
|
|
45
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export var ViewSize;!function(x){x[x.xxxxs=1]="xxxxs",x[x.xxxs=2]="xxxs",x[x.xxs=3]="xxs",x[x.xs=4]="xs",x[x.s=5]="s",x[x.m=6]="m",x[x.l=7]="l",x[x.xl=8]="xl",x[x.xxl=9]="xxl",x[x.xxxl=10]="xxxl",x[x.xxxxl=11]="xxxxl",x[x.xxxxxl=12]="xxxxxl"}(ViewSize||(ViewSize={})),function(x){function i(i){const t=+i,n=Number.isFinite(t)?x[t]:i;return x[n]}x.values={[x.xxxxs]:{minWidth:0,maxWidth:319},[x.xxxs]:{minWidth:320,maxWidth:479},[x.xxs]:{minWidth:480,maxWidth:639},[x.xs]:{minWidth:640,maxWidth:827},[x.s]:{minWidth:828,maxWidth:1023},[x.m]:{minWidth:1024,maxWidth:1365},[x.l]:{minWidth:1366,maxWidth:1599},[x.xl]:{minWidth:1600,maxWidth:1919},[x.xxl]:{minWidth:1920,maxWidth:2559},[x.xxxl]:{minWidth:2560,maxWidth:3839},[x.xxxxl]:{minWidth:3840,maxWidth:7679},[x.xxxxxl]:{minWidth:7680,maxWidth:Number.MAX_SAFE_INTEGER}},x.of=i,x.keyOf=function(i){return x[i]},x.valueList=Object.entries(x.values).map(([x,t])=>[i(x),t]).sort(([,x],[,i])=>x.minWidth-i.minWidth),x.get=function(i){return x.valueList.find(([,x])=>i>=x.minWidth&&i<=x.maxWidth)?.[0]??x.valueList.at(-1)[0]},x.lt=function(x,i){return x<i},x.lte=function(x,i){return x<=i},x.gt=function(x,i){return x>i},x.gte=function(x,i){return x>=i}}(ViewSize||(ViewSize={}));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ViewSize}from"./ViewSize";export function getViewSizeQueryMap(){return ViewSize.valueList.reduce((e,[i,{minWidth:t,maxWidth:n}])=>(e[i]=`only screen and (min-width: ${t}px) and (max-width: ${n}px)`,e),{})}
|