ddan-js 2.0.7 → 2.0.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.
@@ -40,7 +40,7 @@ class DStore extends event_1.default {
40
40
  result = obj_1.default.copy(val);
41
41
  }
42
42
  else {
43
- const [, data] = await hook_1.default.exec(() => this.dispatch(key), `${this.__eventId__}_${key}`);
43
+ const [, data] = await hook_1.default.exec(() => this.dispatch(key), `${this.__eventId}_${key}`);
44
44
  result = data;
45
45
  }
46
46
  return result;
@@ -2,19 +2,19 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const is_1 = require("../util/is");
4
4
  class DTask {
5
- __source__;
5
+ __source;
6
6
  constructor(source) {
7
- this.__source__ = source;
7
+ this.__source = source;
8
8
  }
9
9
  run() {
10
10
  let promise;
11
11
  try {
12
12
  let temp;
13
- if (is_1.default.isFunction(this.__source__)) {
14
- temp = this.__source__();
13
+ if (is_1.default.isFunction(this.__source)) {
14
+ temp = this.__source();
15
15
  }
16
16
  else {
17
- temp = this.__source__;
17
+ temp = this.__source;
18
18
  }
19
19
  promise = is_1.default.isPromise(temp) ? temp : Promise.resolve(temp);
20
20
  return promise.then(data => [null, data]).catch(err => [err, undefined]);
@@ -48,12 +48,12 @@ class DTrack {
48
48
  }
49
49
  exports.DTrack = DTrack;
50
50
  class DTracker {
51
- __map__ = new Map();
52
- __trackList__ = [];
51
+ __map = new Map();
52
+ __trackList = [];
53
53
  can = true;
54
54
  clear() {
55
- this.__map__.clear();
56
- this.__trackList__ = [];
55
+ this.__map.clear();
56
+ this.__trackList = [];
57
57
  return this;
58
58
  }
59
59
  add(name, { desc = "", type = "" } = {}) {
@@ -63,13 +63,13 @@ class DTracker {
63
63
  _track.name = name;
64
64
  _track.type = type;
65
65
  _track.desc = desc;
66
- this.__trackList__.push(_track);
66
+ this.__trackList.push(_track);
67
67
  return _track.id;
68
68
  }
69
69
  done(id) {
70
70
  if (!id || !this.can)
71
71
  return;
72
- const _track = this.__trackList__.find(e => e.id === id);
72
+ const _track = this.__trackList.find(e => e.id === id);
73
73
  if (!_track || _track.isDone)
74
74
  return;
75
75
  _track.done();
@@ -77,38 +77,38 @@ class DTracker {
77
77
  endTrack.id = this._endId(id);
78
78
  endTrack.flag = 1;
79
79
  endTrack.desc = `${_track.desc} | end`;
80
- this.__trackList__.push(endTrack);
80
+ this.__trackList.push(endTrack);
81
81
  }
82
82
  getTrack(id) {
83
- return this.__trackList__.find(e => e.id === id);
83
+ return this.__trackList.find(e => e.id === id);
84
84
  }
85
85
  getLine(id) {
86
86
  if (!id || id.endsWith("-end"))
87
87
  return [];
88
- const idx_s = this.__trackList__.findIndex(e => e.id === id);
88
+ const idx_s = this.__trackList.findIndex(e => e.id === id);
89
89
  if (idx_s < 0)
90
90
  return [];
91
91
  const endid = this._endId(id);
92
- const idx_e = this.__trackList__.findIndex(e => e.id === endid);
93
- const count = idx_e < 0 ? this.__trackList__.length - idx_s : idx_e - idx_s + 1;
94
- return list_1.default.take(this.__trackList__, count, idx_s);
92
+ const idx_e = this.__trackList.findIndex(e => e.id === endid);
93
+ const count = idx_e < 0 ? this.__trackList.length - idx_s : idx_e - idx_s + 1;
94
+ return list_1.default.take(this.__trackList, count, idx_s);
95
95
  }
96
96
  getListByName(name, id = "") {
97
97
  if (!name)
98
98
  return [];
99
- const _nameList = this.__trackList__.filter(e => e.name === name);
99
+ const _nameList = this.__trackList.filter(e => e.name === name);
100
100
  if (_nameList.length <= 0)
101
101
  return [];
102
102
  if (!id)
103
103
  return _nameList;
104
104
  const endid = this._endId(id);
105
- return this.__trackList__.filter(e => e.id === id || e.id === endid);
105
+ return this.__trackList.filter(e => e.id === id || e.id === endid);
106
106
  }
107
107
  getList() {
108
- return this.__trackList__;
108
+ return this.__trackList;
109
109
  }
110
110
  size() {
111
- return this.__trackList__.length;
111
+ return this.__trackList.length;
112
112
  }
113
113
  _endId = (id) => `${id}-end`;
114
114
  }
@@ -17,7 +17,7 @@ const stringify = (styleObj) => {
17
17
  });
18
18
  return result;
19
19
  };
20
- const parse = (styleStr, camel = false) => {
20
+ const parse = (styleStr, { camel = false, pure = true } = {}) => {
21
21
  if (!styleStr.trim())
22
22
  return undefined;
23
23
  const ret = qs_1.default.parse(styleStr, { sep: ';', eq: ':', uri: false });
@@ -25,7 +25,7 @@ const parse = (styleStr, camel = false) => {
25
25
  util_1.default.forof(ret, (k, v) => {
26
26
  if (!v)
27
27
  return;
28
- const _k = camel ? string_1.default.camelCase(k) : k;
28
+ const _k = camel ? ((pure && k.startsWith("_")) ? k : string_1.default.camelCase(k)) : k;
29
29
  if (rule_1.default.isFloat(v)) {
30
30
  result[_k] = +v;
31
31
  }
@@ -68,6 +68,10 @@ const commonFields = [
68
68
  "borderColor",
69
69
  "borderRadius",
70
70
  "background",
71
+ "backgroundImage",
72
+ "backgroundRepeat",
73
+ "backgroundSize",
74
+ "backgroundPosition",
71
75
  "color",
72
76
  "overflow",
73
77
  "opacity",
@@ -80,7 +84,8 @@ const commonFields = [
80
84
  "flex",
81
85
  "flexBasis",
82
86
  "flexFlow",
83
- "flexShrink"
87
+ "flexShrink",
88
+ "pointerEvents",
84
89
  ];
85
90
  const fixFields = [
86
91
  "top",
@@ -4,9 +4,9 @@ function mutex(func) {
4
4
  var lastThis = null;
5
5
  async function mutexed(...args) {
6
6
  lastThis = this || lastThis || {};
7
- if (lastThis.__ddMutex__)
7
+ if (lastThis.__ddMutex)
8
8
  return false;
9
- lastThis.__ddMutex__ = true;
9
+ lastThis.__ddMutex = true;
10
10
  try {
11
11
  func && (await Promise.resolve(func.apply(this, args)));
12
12
  }
@@ -14,7 +14,7 @@ function mutex(func) {
14
14
  console.log(err);
15
15
  }
16
16
  finally {
17
- lastThis.__ddMutex__ = false;
17
+ lastThis.__ddMutex = false;
18
18
  }
19
19
  }
20
20
  return mutexed;
@@ -36,11 +36,29 @@ const readAsDataURL = (file, cb) => {
36
36
  cb && cb(e.target?.result);
37
37
  };
38
38
  };
39
- const downloadFile = (fileName, dataUrl) => {
40
- const a = document.createElement("a");
41
- a.href = dataUrl;
42
- a.download = fileName;
43
- a.click();
39
+ const downloadUrl = (filename, url) => {
40
+ try {
41
+ const a = document.createElement("a");
42
+ a.href = url;
43
+ a.download = filename;
44
+ a.click();
45
+ }
46
+ catch (err) {
47
+ console.warn(`[ddan] downloadUrl`, err);
48
+ }
49
+ };
50
+ const download = (filename, obj) => {
51
+ if (!obj)
52
+ return;
53
+ if (typeof obj === "string") {
54
+ downloadUrl(filename, obj);
55
+ return;
56
+ }
57
+ const href = URL?.createObjectURL(obj);
58
+ if (!href)
59
+ return;
60
+ downloadUrl(filename, href);
61
+ URL?.revokeObjectURL(href);
44
62
  };
45
63
  const downloadImage = (url) => {
46
64
  try {
@@ -56,14 +74,37 @@ const downloadImage = (url) => {
56
74
  let context = canvas.getContext("2d");
57
75
  context && context.drawImage(img, 0, 0, img.width, img.height);
58
76
  let base64 = canvas.toDataURL(); //得到图片的base64编码数据
59
- downloadFile(name, base64);
77
+ downloadUrl(name, base64);
60
78
  };
61
79
  img.src = url; // 将canvas对象转换为图片的data url
62
80
  img.setAttribute("crossOrigin", "Anonymous");
63
81
  }
64
82
  catch (err) {
65
- console.log(`[ddan] downloadImageUrl`, err);
83
+ console.warn(`[ddan] downloadImage`, err);
66
84
  }
67
85
  return Promise.resolve();
68
86
  };
69
- exports.default = { dataURLtoFile, dataURLtoBlob, blobToFile, readAsDataURL, downloadFile, downloadImage };
87
+ const watermark = (text, { width = 400, height = 300, angle = 0, fillStyle = "rgba(0, 0, 0, 0.2)", font = "16px Arial", textAlign = "left", textBaseline = "middle" } = {}) => {
88
+ try {
89
+ if (!text)
90
+ return "";
91
+ const canvas = document.createElement("canvas");
92
+ canvas.width = width;
93
+ canvas.height = height;
94
+ const ctx = canvas.getContext("2d");
95
+ if (!ctx)
96
+ return "";
97
+ angle && ctx.rotate((angle * Math.PI) / 180);
98
+ fillStyle && (ctx.fillStyle = fillStyle);
99
+ font && (ctx.font = font);
100
+ textAlign && (ctx.textAlign = textAlign);
101
+ textBaseline && (ctx.textBaseline = textBaseline);
102
+ ctx.fillText(text, canvas.width / 10, canvas.height / 2);
103
+ return `url(${canvas.toDataURL()})`; // "image/png" 默认
104
+ }
105
+ catch (err) {
106
+ console.warn(`[ddan] watermark`, err);
107
+ return "";
108
+ }
109
+ };
110
+ exports.default = { dataURLtoFile, dataURLtoBlob, blobToFile, readAsDataURL, downloadUrl, download, downloadImage, watermark };
@@ -9,7 +9,7 @@ const copy = (source, options = {}) => {
9
9
  return newData;
10
10
  if (Array.isArray(newData))
11
11
  return newData;
12
- const { fields = [], camel = false, pure = false } = options;
12
+ const { fields = [], camel = false, pure = true } = options;
13
13
  if ((!fields || fields.length <= 0) && !camel)
14
14
  return newData;
15
15
  const result = {};
@@ -4,9 +4,9 @@ export interface IDEvent {
4
4
  tag: string;
5
5
  }
6
6
  export default class DEvent {
7
- __map__: Map<string, Set<Function | any>>;
8
- __tagList__: IDEvent[];
9
- __eventId__: string;
7
+ __map: Map<string, Set<Function | any>>;
8
+ __tagList: IDEvent[];
9
+ __eventId: string;
10
10
  constructor();
11
11
  clear(): this;
12
12
  /**
@@ -1,7 +1,7 @@
1
1
  export type TSVG = "back" | "right" | "home" | "close" | "close_circle" | "close_circle_fill" | "warning" | "play" | "lock" | "lock_fill" | "plus" | "plus_circle" | "plus_circle_fill" | "star" | "star_fill" | "share" | "love" | "love_fill";
2
2
  export declare class DIcon {
3
- __defStyle__: string;
4
- __map__: Record<TSVG, string>;
3
+ __defStyle: string;
4
+ __map: Record<TSVG, string>;
5
5
  getBase64: (svgType: TSVG) => string;
6
6
  getSVG: (svgType: TSVG) => {
7
7
  url: string;
@@ -1,5 +1,5 @@
1
1
  export default class DPipeTask {
2
- __list__: any[];
2
+ __list: any[];
3
3
  constructor(_task?: any);
4
4
  push(_task?: any): this;
5
5
  clear(): this;
@@ -1,7 +1,7 @@
1
1
  export default class DPipeline {
2
- __list__: any[];
3
- __running__: boolean;
4
- __waitting__: boolean;
2
+ __list: any[];
3
+ __running: boolean;
4
+ __waitting: boolean;
5
5
  constructor();
6
6
  pipe(_task?: any): this;
7
7
  delay(t?: number): this;
@@ -1,5 +1,5 @@
1
1
  export default class DPipeparallel {
2
- __list__: any[];
2
+ __list: any[];
3
3
  constructor();
4
4
  add(_task?: any): this;
5
5
  clear(): this;
@@ -1,5 +1,5 @@
1
1
  export default class DTask {
2
- __source__: any;
2
+ __source: any;
3
3
  constructor(source?: any);
4
4
  run(): Promise<any[]>;
5
5
  static _lockedMap: Map<string, Promise<any[]>>;
@@ -25,8 +25,8 @@ export declare class DTrack {
25
25
  done: (t?: number) => void;
26
26
  }
27
27
  export declare class DTracker {
28
- __map__: Map<string, DTrack>;
29
- __trackList__: DTrack[];
28
+ __map: Map<string, DTrack>;
29
+ __trackList: DTrack[];
30
30
  can: boolean;
31
31
  clear(): this;
32
32
  add(name: string, { desc, type }?: {
@@ -195,7 +195,10 @@ declare const _default: {
195
195
  };
196
196
  css: {
197
197
  stringify: (styleObj: Record<string, string | number>) => string;
198
- parse: (styleStr: string, camel?: boolean) => {} | undefined;
198
+ parse: (styleStr: string, { camel, pure }?: {
199
+ camel?: boolean | undefined;
200
+ pure?: boolean | undefined;
201
+ }) => {} | undefined;
199
202
  fixValue: (value: string | number, unit?: string) => string | number;
200
203
  style: (cssKV: Record<string, any>, unit?: string) => string;
201
204
  commonFields: string[];
@@ -221,8 +224,18 @@ declare const _default: {
221
224
  dataURLtoBlob: (dataurl: any) => Blob;
222
225
  blobToFile: (theBlob: any, fileName: any) => any;
223
226
  readAsDataURL: (file: any, cb: any) => void;
224
- downloadFile: (fileName: any, dataUrl: any) => void;
227
+ downloadUrl: (filename: string, url: string) => void;
228
+ download: (filename: string, obj: string | Blob | MediaSource) => void;
225
229
  downloadImage: (url: string) => Promise<void> | undefined;
230
+ watermark: (text: string, { width, height, angle, fillStyle, font, textAlign, textBaseline }?: {
231
+ width?: number | undefined;
232
+ height?: number | undefined;
233
+ angle?: number | undefined;
234
+ fillStyle?: string | undefined;
235
+ font?: string | undefined;
236
+ textAlign?: "center" | "end" | "left" | "right" | "start" | undefined;
237
+ textBaseline?: "alphabetic" | "bottom" | "hanging" | "ideographic" | "middle" | "top" | undefined;
238
+ }) => string;
226
239
  };
227
240
  icon: import("./class/icon").DIcon;
228
241
  rule: {
@@ -1,6 +1,9 @@
1
1
  declare const _default: {
2
2
  stringify: (styleObj: Record<string, string | number>) => string;
3
- parse: (styleStr: string, camel?: boolean) => {} | undefined;
3
+ parse: (styleStr: string, { camel, pure }?: {
4
+ camel?: boolean | undefined;
5
+ pure?: boolean | undefined;
6
+ }) => {} | undefined;
4
7
  fixValue: (value: string | number, unit?: string) => string | number;
5
8
  style: (cssKV: Record<string, any>, unit?: string) => string;
6
9
  commonFields: string[];
@@ -3,7 +3,17 @@ declare const _default: {
3
3
  dataURLtoBlob: (dataurl: any) => Blob;
4
4
  blobToFile: (theBlob: any, fileName: any) => any;
5
5
  readAsDataURL: (file: any, cb: any) => void;
6
- downloadFile: (fileName: any, dataUrl: any) => void;
6
+ downloadUrl: (filename: string, url: string) => void;
7
+ download: (filename: string, obj: string | Blob | MediaSource) => void;
7
8
  downloadImage: (url: string) => Promise<void> | undefined;
9
+ watermark: (text: string, { width, height, angle, fillStyle, font, textAlign, textBaseline }?: {
10
+ width?: number | undefined;
11
+ height?: number | undefined;
12
+ angle?: number | undefined;
13
+ fillStyle?: string | undefined;
14
+ font?: string | undefined;
15
+ textAlign?: "center" | "end" | "left" | "right" | "start" | undefined;
16
+ textBaseline?: "alphabetic" | "bottom" | "hanging" | "ideographic" | "middle" | "top" | undefined;
17
+ }) => string;
8
18
  };
9
19
  export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ddan-js",
3
- "version": "2.0.7",
3
+ "version": "2.0.9",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "main": "bin/ddan-js.js",