ddan-js 2.4.7 → 2.4.9

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.
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isAllowed = exports.queryPermission = exports.copyText = void 0;
3
4
  const qs_1 = require("./qs");
4
5
  const dataURLtoFile = (dataurl, filename) => {
5
6
  // const _dataurl = decodeURIComponent(dataurl);
6
- let arr = dataurl.split(",");
7
+ let arr = dataurl.split(',');
7
8
  let mime = arr[0].match(/:(.*?);/)[1];
8
9
  let bstr = window.atob(arr[1]);
9
10
  let n = bstr.length;
@@ -14,7 +15,7 @@ const dataURLtoFile = (dataurl, filename) => {
14
15
  return new File([u8arr], filename, { type: mime });
15
16
  };
16
17
  const dataURLtoBlob = (dataurl) => {
17
- let arr = dataurl.split(",");
18
+ let arr = dataurl.split(',');
18
19
  let mime = arr[0].match(/:(.*?);/)[1];
19
20
  let bstr = window.atob(arr[1]);
20
21
  let n = bstr.length;
@@ -39,7 +40,7 @@ const readAsDataURL = (file, cb) => {
39
40
  };
40
41
  const _downloadUrl = (url, filename) => {
41
42
  try {
42
- const ele = document.createElement("a");
43
+ const ele = document.createElement('a');
43
44
  ele.style.display = 'none';
44
45
  ele.href = url;
45
46
  ele.download = filename;
@@ -51,7 +52,7 @@ const _downloadUrl = (url, filename) => {
51
52
  console.error(`[ddan] _downloadUrl ${url}`, err);
52
53
  }
53
54
  };
54
- const downloadUrl = (url, filename = "", checkSomeOrigin = false) => {
55
+ const downloadUrl = (url, filename = '', checkSomeOrigin = false) => {
55
56
  try {
56
57
  if (!url)
57
58
  return;
@@ -66,8 +67,8 @@ const downloadUrl = (url, filename = "", checkSomeOrigin = false) => {
66
67
  params.t = Date.now();
67
68
  const _url = `${map.path}?${qs_1.default.stringify(params, { uri: false })}`;
68
69
  const xhr = new XMLHttpRequest();
69
- xhr.open("GET", _url, true);
70
- xhr.responseType = "blob";
70
+ xhr.open('GET', _url, true);
71
+ xhr.responseType = 'blob';
71
72
  xhr.onload = function () {
72
73
  downloadFile(xhr.response, _filename);
73
74
  };
@@ -94,7 +95,7 @@ const getBlob = (url) => {
94
95
  xhr.send();
95
96
  });
96
97
  };
97
- const downloadFile = (data, filename = "") => {
98
+ const downloadFile = (data, filename = '') => {
98
99
  try {
99
100
  if (!data)
100
101
  return;
@@ -108,10 +109,10 @@ const downloadFile = (data, filename = "") => {
108
109
  console.error(`[ddan] downloadFile`, error);
109
110
  }
110
111
  };
111
- const download = (urlOrFile, filename = "") => {
112
+ const download = (urlOrFile, filename = '') => {
112
113
  if (!urlOrFile)
113
114
  return;
114
- if (typeof urlOrFile === "string") {
115
+ if (typeof urlOrFile === 'string') {
115
116
  downloadUrl(urlOrFile, filename);
116
117
  return;
117
118
  }
@@ -125,32 +126,32 @@ const downloadImage = (url) => {
125
126
  //下载群二维码
126
127
  const img = new Image();
127
128
  img.onload = () => {
128
- let canvas = document.createElement("canvas");
129
+ let canvas = document.createElement('canvas');
129
130
  canvas.width = img.width;
130
131
  canvas.height = img.height;
131
- let context = canvas.getContext("2d");
132
+ let context = canvas.getContext('2d');
132
133
  context && context.drawImage(img, 0, 0, img.width, img.height);
133
134
  let base64 = canvas.toDataURL(); //得到图片的base64编码数据
134
135
  downloadUrl(base64, name);
135
136
  };
136
137
  img.src = url; // 将canvas对象转换为图片的data url
137
- img.setAttribute("crossOrigin", "Anonymous");
138
+ img.setAttribute('crossOrigin', 'Anonymous');
138
139
  }
139
140
  catch (err) {
140
141
  console.error(`[ddan] downloadImage`, err);
141
142
  }
142
143
  return Promise.resolve();
143
144
  };
144
- const watermark = (text, { width = 400, height = 300, angle = 0, fillStyle = "rgba(0, 0, 0, 0.2)", font = "16px Arial", textAlign = "left", textBaseline = "middle" } = {}) => {
145
+ const watermark = (text, { width = 400, height = 300, angle = 0, fillStyle = 'rgba(0, 0, 0, 0.2)', font = '16px Arial', textAlign = 'left', textBaseline = 'middle', } = {}) => {
145
146
  try {
146
147
  if (!text)
147
- return "";
148
- const canvas = document.createElement("canvas");
148
+ return '';
149
+ const canvas = document.createElement('canvas');
149
150
  canvas.width = width;
150
151
  canvas.height = height;
151
- const ctx = canvas.getContext("2d");
152
+ const ctx = canvas.getContext('2d');
152
153
  if (!ctx)
153
- return "";
154
+ return '';
154
155
  angle && ctx.rotate((angle * Math.PI) / 180);
155
156
  fillStyle && (ctx.fillStyle = fillStyle);
156
157
  font && (ctx.font = font);
@@ -161,9 +162,57 @@ const watermark = (text, { width = 400, height = 300, angle = 0, fillStyle = "rg
161
162
  }
162
163
  catch (err) {
163
164
  console.error(`[ddan] watermark`, err);
164
- return "";
165
+ return '';
165
166
  }
166
167
  };
168
+ const copyText = (text, legacy = false) => {
169
+ try {
170
+ if (!legacy && navigator.clipboard) {
171
+ // clipboard api 复制
172
+ navigator.clipboard.writeText(text);
173
+ }
174
+ else {
175
+ const _textarea = document.createElement('textarea');
176
+ document.body.appendChild(_textarea);
177
+ // 隐藏此输入框
178
+ _textarea.style.position = 'fixed';
179
+ _textarea.style.clip = 'rect(0 0 0 0)';
180
+ _textarea.style.top = '10px';
181
+ _textarea.style.opacity = '0';
182
+ // 赋值
183
+ _textarea.value = text ?? '';
184
+ // 选中
185
+ _textarea.select();
186
+ // 复制
187
+ document.execCommand('copy');
188
+ // 移除输入框
189
+ document.body.removeChild(_textarea);
190
+ }
191
+ }
192
+ catch (err) {
193
+ console.error(`[ddan] copyText`, err);
194
+ }
195
+ };
196
+ exports.copyText = copyText;
197
+ const queryPermission = (name, def = 'denied') => {
198
+ return new Promise((resolve) => {
199
+ const permissions = navigator?.permissions;
200
+ if (!permissions || !permissions.query)
201
+ return resolve(def);
202
+ const desc = { name };
203
+ permissions
204
+ .query(desc)
205
+ .then((res) => resolve(res.state))
206
+ .catch(() => resolve(def));
207
+ });
208
+ };
209
+ exports.queryPermission = queryPermission;
210
+ const isAllowed = (status, prompt = true) => {
211
+ if (prompt && status === 'prompt')
212
+ return true;
213
+ return status === 'granted';
214
+ };
215
+ exports.isAllowed = isAllowed;
167
216
  exports.default = {
168
217
  dataURLtoFile,
169
218
  dataURLtoBlob,
@@ -174,4 +223,7 @@ exports.default = {
174
223
  downloadFile,
175
224
  downloadImage,
176
225
  watermark,
226
+ copyText: exports.copyText,
227
+ queryPermission: exports.queryPermission,
228
+ isAllowed: exports.isAllowed,
177
229
  };
@@ -1,11 +1,11 @@
1
- import { Ddan } from "../typings";
1
+ import { Ddan } from '../typings';
2
2
  export interface IDEvent {
3
3
  name: string;
4
- handler: Function | any;
4
+ listener: Ddan.Function | any;
5
5
  tag: string;
6
6
  }
7
7
  export default class DEvent implements Ddan.IEvent {
8
- __map: Map<string, Set<Function | any>>;
8
+ __map: Map<string, Set<Ddan.Function | any>>;
9
9
  __tagList: IDEvent[];
10
10
  __eventId: string;
11
11
  constructor();
@@ -13,18 +13,18 @@ export default class DEvent implements Ddan.IEvent {
13
13
  /**
14
14
  * 监听
15
15
  * @param name
16
- * @param handler
16
+ * @param listener
17
17
  * @returns
18
18
  */
19
- on(name: string, handler: Function, tag?: string): this;
19
+ on(name: string, listener: Ddan.Function, tag?: string): this;
20
20
  emit(name: string, ...args: any[]): this;
21
21
  /**
22
22
  * 取消监听
23
23
  * @param name
24
- * @param handler
24
+ * @param listener
25
25
  * @returns
26
26
  */
27
- off(name: string, handler?: Function): this;
27
+ off(name: string, listener?: Ddan.Function): this;
28
28
  /**
29
29
  * 移除
30
30
  * @param name
@@ -35,9 +35,9 @@ export default class DEvent implements Ddan.IEvent {
35
35
  /**
36
36
  * 一次性监听
37
37
  * @param name
38
- * @param handler
38
+ * @param listener
39
39
  * @returns
40
40
  */
41
- once(name: string, handler: Function): this;
41
+ once(name: string, listener: Ddan.Function): this;
42
42
  has(name: string): boolean;
43
43
  }
@@ -186,15 +186,16 @@ declare const dUtil: {
186
186
  };
187
187
  };
188
188
  declare const dHook: {
189
+ delay: (ms?: number) => Promise<unknown>;
190
+ sleep: (ms?: number) => Promise<unknown>;
189
191
  run: <T = any>(task?: Function | Promise<T> | undefined, wait?: number) => Promise<[any, undefined] | [null, T]>;
192
+ to: <T_1 = any>(task?: Function | Promise<T_1> | undefined, wait?: number) => Promise<[any, undefined] | [null, T_1]>;
190
193
  exec: (func: Function, taskId?: string) => Promise<any[]>;
191
194
  debounce: typeof import("./modules/hook/debounce").default;
192
195
  throttle: typeof import("./modules/hook/throttle").default;
193
196
  task: (param?: import("./typings").Ddan.Func1<any, any> | undefined) => import("./class/pipeTask").default;
194
197
  mutex: typeof import("./modules/hook/mutex").default;
195
198
  polling: typeof import("./modules/hook/polling").default;
196
- to: <T_1 = any, U extends object = any>(promise: Promise<T_1>, errorExt?: object | undefined) => Promise<[null, T_1] | [U, undefined]>;
197
- delay: (time?: number) => Promise<unknown>;
198
199
  };
199
200
  declare const dMini: {
200
201
  mini: {
@@ -268,7 +269,7 @@ declare const dMini: {
268
269
  download: (urlOrFile: string | Blob | MediaSource, filename?: string) => void;
269
270
  downloadFile: (data: Blob | MediaSource, filename?: string) => void;
270
271
  downloadImage: (url: string) => Promise<void> | undefined;
271
- watermark: (text: string, { width, height, angle, fillStyle, font, textAlign, textBaseline }?: {
272
+ watermark: (text: string, { width, height, angle, fillStyle, font, textAlign, textBaseline, }?: {
272
273
  width?: number | undefined;
273
274
  height?: number | undefined;
274
275
  angle?: number | undefined;
@@ -277,6 +278,9 @@ declare const dMini: {
277
278
  textAlign?: "center" | "end" | "left" | "right" | "start" | undefined;
278
279
  textBaseline?: "alphabetic" | "bottom" | "hanging" | "ideographic" | "middle" | "top" | undefined;
279
280
  }) => string;
281
+ copyText: (text: string, legacy?: boolean) => void;
282
+ queryPermission: (name: string, def?: PermissionState) => Promise<PermissionState>;
283
+ isAllowed: (status: PermissionState | undefined, prompt?: boolean) => boolean;
280
284
  };
281
285
  };
282
286
  declare const dCdn: {
@@ -397,7 +401,7 @@ declare const dWeb: {
397
401
  download: (urlOrFile: string | Blob | MediaSource, filename?: string) => void;
398
402
  downloadFile: (data: Blob | MediaSource, filename?: string) => void;
399
403
  downloadImage: (url: string) => Promise<void> | undefined;
400
- watermark: (text: string, { width, height, angle, fillStyle, font, textAlign, textBaseline }?: {
404
+ watermark: (text: string, { width, height, angle, fillStyle, font, textAlign, textBaseline, }?: {
401
405
  width?: number | undefined;
402
406
  height?: number | undefined;
403
407
  angle?: number | undefined;
@@ -406,6 +410,9 @@ declare const dWeb: {
406
410
  textAlign?: "center" | "end" | "left" | "right" | "start" | undefined;
407
411
  textBaseline?: "alphabetic" | "bottom" | "hanging" | "ideographic" | "middle" | "top" | undefined;
408
412
  }) => string;
413
+ copyText: (text: string, legacy?: boolean) => void;
414
+ queryPermission: (name: string, def?: PermissionState) => Promise<PermissionState>;
415
+ isAllowed: (status: PermissionState | undefined, prompt?: boolean) => boolean;
409
416
  };
410
417
  };
411
418
  export { dUtil, dHook, dWeb, dMini, dCdn, dStore, dJoker, dTracker, dLogger };
@@ -436,15 +443,16 @@ declare const _default: {
436
443
  oneSecond: number;
437
444
  };
438
445
  hook: {
446
+ delay: (ms?: number) => Promise<unknown>;
447
+ sleep: (ms?: number) => Promise<unknown>;
439
448
  run: <T = any>(task?: Function | Promise<T> | undefined, wait?: number) => Promise<[any, undefined] | [null, T]>;
449
+ to: <T_1 = any>(task?: Function | Promise<T_1> | undefined, wait?: number) => Promise<[any, undefined] | [null, T_1]>;
440
450
  exec: (func: Function, taskId?: string) => Promise<any[]>;
441
451
  debounce: typeof import("./modules/hook/debounce").default;
442
452
  throttle: typeof import("./modules/hook/throttle").default;
443
453
  task: (param?: import("./typings").Ddan.Func1<any, any> | undefined) => import("./class/pipeTask").default;
444
454
  mutex: typeof import("./modules/hook/mutex").default;
445
455
  polling: typeof import("./modules/hook/polling").default;
446
- to: <T_1 = any, U extends object = any>(promise: Promise<T_1>, errorExt?: object | undefined) => Promise<[null, T_1] | [U, undefined]>;
447
- delay: (time?: number) => Promise<unknown>;
448
456
  };
449
457
  math: {
450
458
  random: (max: number) => number;
@@ -658,7 +666,7 @@ declare const _default: {
658
666
  download: (urlOrFile: string | Blob | MediaSource, filename?: string) => void;
659
667
  downloadFile: (data: Blob | MediaSource, filename?: string) => void;
660
668
  downloadImage: (url: string) => Promise<void> | undefined;
661
- watermark: (text: string, { width, height, angle, fillStyle, font, textAlign, textBaseline }?: {
669
+ watermark: (text: string, { width, height, angle, fillStyle, font, textAlign, textBaseline, }?: {
662
670
  width?: number | undefined;
663
671
  height?: number | undefined;
664
672
  angle?: number | undefined;
@@ -667,6 +675,9 @@ declare const _default: {
667
675
  textAlign?: "center" | "end" | "left" | "right" | "start" | undefined;
668
676
  textBaseline?: "alphabetic" | "bottom" | "hanging" | "ideographic" | "middle" | "top" | undefined;
669
677
  }) => string;
678
+ copyText: (text: string, legacy?: boolean) => void;
679
+ queryPermission: (name: string, def?: PermissionState) => Promise<PermissionState>;
680
+ isAllowed: (status: PermissionState | undefined, prompt?: boolean) => boolean;
670
681
  };
671
682
  icon: import("./class/icon").DIcon;
672
683
  rule: {
@@ -1,6 +1,7 @@
1
1
  declare function to<T = any, U extends object = any>(promise: Promise<T>, errorExt?: object): Promise<[null, T] | [U, undefined]>;
2
2
  declare const _default: {
3
3
  to: typeof to;
4
- delay: (time?: number) => Promise<unknown>;
4
+ delay: (ms?: number) => Promise<unknown>;
5
+ run: <T = any>(func: any) => Promise<[null, T] | [any, undefined]>;
5
6
  };
6
7
  export default _default;
@@ -1,9 +1,9 @@
1
- import debounce from "./debounce";
2
- import throttle from "./throttle";
3
- import mutex from "./mutex";
4
- import DPipeTask from "../../class/pipeTask";
5
- import { Ddan } from "../../typings";
6
- import polling from "./polling";
1
+ import debounce from './debounce';
2
+ import throttle from './throttle';
3
+ import mutex from './mutex';
4
+ import DPipeTask from '../../class/pipeTask';
5
+ import { Ddan } from '../../typings';
6
+ import polling from './polling';
7
7
  /**
8
8
  *
9
9
  * @param task 任务
@@ -12,14 +12,15 @@ import polling from "./polling";
12
12
  */
13
13
  declare function run<T = any>(task?: Promise<T> | Function, wait?: number): Promise<[any, undefined] | [null, T]>;
14
14
  declare const _default: {
15
+ delay: (ms?: number) => Promise<unknown>;
16
+ sleep: (ms?: number) => Promise<unknown>;
15
17
  run: typeof run;
18
+ to: <T = any>(task?: Function | Promise<T> | undefined, wait?: number) => Promise<[any, undefined] | [null, T]>;
16
19
  exec: (func: Function, taskId?: string) => Promise<any[]>;
17
20
  debounce: typeof debounce;
18
21
  throttle: typeof throttle;
19
22
  task: (param?: Ddan.Func1<any, any> | undefined) => DPipeTask;
20
23
  mutex: typeof mutex;
21
24
  polling: typeof polling;
22
- to: <T = any, U extends object = any>(promise: Promise<T>, errorExt?: object | undefined) => Promise<[null, T] | [U, undefined]>;
23
- delay: (time?: number) => Promise<unknown>;
24
25
  };
25
26
  export default _default;
@@ -1,3 +1,6 @@
1
+ export declare const copyText: (text: string, legacy?: boolean) => void;
2
+ export declare const queryPermission: (name: string, def?: PermissionState) => Promise<PermissionState>;
3
+ export declare const isAllowed: (status: PermissionState | undefined, prompt?: boolean) => boolean;
1
4
  declare const _default: {
2
5
  dataURLtoFile: (dataurl: any, filename: any) => File;
3
6
  dataURLtoBlob: (dataurl: any) => Blob;
@@ -7,7 +10,7 @@ declare const _default: {
7
10
  download: (urlOrFile: string | Blob | MediaSource, filename?: string) => void;
8
11
  downloadFile: (data: Blob | MediaSource, filename?: string) => void;
9
12
  downloadImage: (url: string) => Promise<void> | undefined;
10
- watermark: (text: string, { width, height, angle, fillStyle, font, textAlign, textBaseline }?: {
13
+ watermark: (text: string, { width, height, angle, fillStyle, font, textAlign, textBaseline, }?: {
11
14
  width?: number | undefined;
12
15
  height?: number | undefined;
13
16
  angle?: number | undefined;
@@ -16,5 +19,8 @@ declare const _default: {
16
19
  textAlign?: "center" | "end" | "left" | "right" | "start" | undefined;
17
20
  textBaseline?: "alphabetic" | "bottom" | "hanging" | "ideographic" | "middle" | "top" | undefined;
18
21
  }) => string;
22
+ copyText: (text: string, legacy?: boolean) => void;
23
+ queryPermission: (name: string, def?: PermissionState) => Promise<PermissionState>;
24
+ isAllowed: (status: PermissionState | undefined, prompt?: boolean) => boolean;
19
25
  };
20
26
  export default _default;
@@ -10,7 +10,7 @@ export default class DLoopFrame implements Ddan.ILoopFrame {
10
10
  start(): this;
11
11
  stop(): this;
12
12
  restart(): this;
13
- on(cb: Function): this;
14
- off(cb: Function | undefined): this;
13
+ on(cb: Ddan.Function): this;
14
+ off(cb: Ddan.Function | undefined): this;
15
15
  _handleLoop(): void;
16
16
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ddan-js",
3
- "version": "2.4.7",
3
+ "version": "2.4.9",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "ddan-js",