yummies 5.15.0 → 6.0.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/README.md +10 -4
- package/async.cjs +45 -1
- package/async.cjs.map +1 -1
- package/async.d.ts +7 -6
- package/common.cjs +11 -1
- package/common.cjs.map +1 -1
- package/common.d.ts +8 -5
- package/common.js.map +1 -1
- package/complex.cjs +88 -1
- package/complex.cjs.map +1 -1
- package/{complex/modules-factory.d.ts → complex.d.ts} +31 -4
- package/complex.js.map +1 -1
- package/cookie.cjs +9 -1
- package/cookie.cjs.map +1 -1
- package/cookie.d.ts +5 -3
- package/cookie.js.map +1 -1
- package/css.cjs +45 -1
- package/css.cjs.map +1 -1
- package/css.d.ts +11 -8
- package/css.js.map +1 -1
- package/data.cjs +43 -1
- package/data.cjs.map +1 -1
- package/data.d.ts +5 -4
- package/data.js.map +1 -1
- package/date-time.cjs +395 -1
- package/date-time.cjs.map +1 -1
- package/date-time.d.ts +12 -10
- package/date-time.js.map +1 -1
- package/device.cjs +32 -1
- package/device.cjs.map +1 -1
- package/device.d.ts +9 -8
- package/encodings.cjs +270 -1
- package/encodings.cjs.map +1 -1
- package/encodings.d.ts +3 -2
- package/encodings.js.map +1 -1
- package/errors.cjs +23 -1
- package/errors.cjs.map +1 -1
- package/errors.d.ts +5 -4
- package/errors.js.map +1 -1
- package/file.cjs +28 -1
- package/file.cjs.map +1 -1
- package/file.d.ts +4 -3
- package/format.cjs +75 -1
- package/format.cjs.map +1 -1
- package/format.d.ts +74 -0
- package/format.js.map +1 -1
- package/html.cjs +167 -1
- package/html.cjs.map +1 -1
- package/html.d.ts +19 -17
- package/html.js.map +1 -1
- package/id.cjs +25 -1
- package/id.cjs.map +1 -1
- package/id.d.ts +9 -8
- package/imports.cjs +33 -1
- package/imports.cjs.map +1 -1
- package/imports.d.ts +6 -4
- package/math.cjs +14 -1
- package/math.cjs.map +1 -1
- package/math.d.ts +7 -5
- package/math.js.map +1 -1
- package/media.cjs +105 -1
- package/media.cjs.map +1 -1
- package/media.d.ts +11 -11
- package/mobx.cjs +184 -1
- package/mobx.cjs.map +1 -1
- package/mobx.d.ts +66 -0
- package/mobx.js +16 -12
- package/mobx.js.map +1 -1
- package/ms.cjs +13 -1
- package/ms.cjs.map +1 -1
- package/ms.d.ts +4 -3
- package/number.cjs +10 -1
- package/number.cjs.map +1 -1
- package/number.d.ts +3 -2
- package/package.json +39 -113
- package/parser.cjs +68 -1
- package/parser.cjs.map +1 -1
- package/parser.d.ts +41 -0
- package/parser.js.map +1 -1
- package/price.cjs +20 -1
- package/price.cjs.map +1 -1
- package/price.d.ts +5 -3
- package/random.cjs +24 -1
- package/random.cjs.map +1 -1
- package/random.d.ts +10 -9
- package/react.cjs +216 -1
- package/react.cjs.map +1 -1
- package/react.d.ts +113 -0
- package/react.js.map +1 -1
- package/sound.cjs +11 -1
- package/sound.cjs.map +1 -1
- package/sound.d.ts +3 -2
- package/text.cjs +37 -1
- package/text.cjs.map +1 -1
- package/text.d.ts +4 -3
- package/type-guard.cjs +75 -1
- package/type-guard.cjs.map +1 -1
- package/type-guard.d.ts +121 -0
- package/type-guard.js.map +1 -1
- package/{utils/types.d.ts → types.d.ts} +54 -53
- package/types.global.cjs +2 -0
- package/types.global.cjs.map +1 -0
- package/{utility-types.d.ts → types.global.d.ts} +1 -1
- package/types.global.js +2 -0
- package/types.global.js.map +1 -0
- package/vibrate.cjs +8 -1
- package/vibrate.cjs.map +1 -1
- package/vibrate.d.ts +3 -2
- package/async.d.ts.map +0 -1
- package/common.d.ts.map +0 -1
- package/complex/counter.d.ts +0 -15
- package/complex/counter.d.ts.map +0 -1
- package/complex/global-config.d.ts +0 -11
- package/complex/global-config.d.ts.map +0 -1
- package/complex/index.d.ts +0 -4
- package/complex/index.d.ts.map +0 -1
- package/complex/modules-factory.d.ts.map +0 -1
- package/cookie.d.ts.map +0 -1
- package/css.d.ts.map +0 -1
- package/data.d.ts.map +0 -1
- package/date-time.d.ts.map +0 -1
- package/device.d.ts.map +0 -1
- package/encodings.d.ts.map +0 -1
- package/errors.d.ts.map +0 -1
- package/file.d.ts.map +0 -1
- package/format/_exports.d.ts +0 -5
- package/format/_exports.d.ts.map +0 -1
- package/format/constants.d.ts +0 -4
- package/format/constants.d.ts.map +0 -1
- package/format/index.d.ts +0 -3
- package/format/index.d.ts.map +0 -1
- package/format/number.d.ts +0 -36
- package/format/number.d.ts.map +0 -1
- package/format/percent.d.ts +0 -15
- package/format/percent.d.ts.map +0 -1
- package/format/skip-spaces.d.ts +0 -5
- package/format/skip-spaces.d.ts.map +0 -1
- package/html.d.ts.map +0 -1
- package/id.d.ts.map +0 -1
- package/imports.d.ts.map +0 -1
- package/math.d.ts.map +0 -1
- package/media.d.ts.map +0 -1
- package/mobx/apply-observable.d.ts +0 -4
- package/mobx/apply-observable.d.ts.map +0 -1
- package/mobx/create-enhanced-atom.d.ts +0 -11
- package/mobx/create-enhanced-atom.d.ts.map +0 -1
- package/mobx/create-ref.d.ts +0 -25
- package/mobx/create-ref.d.ts.map +0 -1
- package/mobx/deep-observable-struct.d.ts +0 -7
- package/mobx/deep-observable-struct.d.ts.map +0 -1
- package/mobx/get-mobx-administration.d.ts +0 -6
- package/mobx/get-mobx-administration.d.ts.map +0 -1
- package/mobx/index.d.ts +0 -7
- package/mobx/index.d.ts.map +0 -1
- package/mobx/lazy-observe.d.ts +0 -14
- package/mobx/lazy-observe.d.ts.map +0 -1
- package/ms.d.ts.map +0 -1
- package/number.d.ts.map +0 -1
- package/parser/_exports.d.ts +0 -4
- package/parser/_exports.d.ts.map +0 -1
- package/parser/index.d.ts +0 -3
- package/parser/index.d.ts.map +0 -1
- package/parser/number.d.ts +0 -21
- package/parser/number.d.ts.map +0 -1
- package/parser/percent.d.ts +0 -4
- package/parser/percent.d.ts.map +0 -1
- package/parser/string.d.ts +0 -7
- package/parser/string.d.ts.map +0 -1
- package/price.d.ts.map +0 -1
- package/random.d.ts.map +0 -1
- package/react/hooks/index.d.ts +0 -22
- package/react/hooks/index.d.ts.map +0 -1
- package/react/hooks/use-abort-controller.d.ts +0 -2
- package/react/hooks/use-abort-controller.d.ts.map +0 -1
- package/react/hooks/use-abort-signal.d.ts +0 -2
- package/react/hooks/use-abort-signal.d.ts.map +0 -1
- package/react/hooks/use-click-outside.d.ts +0 -9
- package/react/hooks/use-click-outside.d.ts.map +0 -1
- package/react/hooks/use-constant.d.ts +0 -9
- package/react/hooks/use-constant.d.ts.map +0 -1
- package/react/hooks/use-define-ref.d.ts +0 -10
- package/react/hooks/use-define-ref.d.ts.map +0 -1
- package/react/hooks/use-element-ref.d.ts +0 -2
- package/react/hooks/use-element-ref.d.ts.map +0 -1
- package/react/hooks/use-event-listener.d.ts +0 -8
- package/react/hooks/use-event-listener.d.ts.map +0 -1
- package/react/hooks/use-event.d.ts +0 -3
- package/react/hooks/use-event.d.ts.map +0 -1
- package/react/hooks/use-flag.d.ts +0 -8
- package/react/hooks/use-flag.d.ts.map +0 -1
- package/react/hooks/use-force-update.d.ts +0 -2
- package/react/hooks/use-force-update.d.ts.map +0 -1
- package/react/hooks/use-initial-height.d.ts +0 -5
- package/react/hooks/use-initial-height.d.ts.map +0 -1
- package/react/hooks/use-instance.d.ts +0 -27
- package/react/hooks/use-instance.d.ts.map +0 -1
- package/react/hooks/use-intersection-observer.d.ts +0 -2
- package/react/hooks/use-intersection-observer.d.ts.map +0 -1
- package/react/hooks/use-last-defined-value.d.ts +0 -2
- package/react/hooks/use-last-defined-value.d.ts.map +0 -1
- package/react/hooks/use-last-value-ref.d.ts +0 -2
- package/react/hooks/use-last-value-ref.d.ts.map +0 -1
- package/react/hooks/use-life-cycle.d.ts +0 -5
- package/react/hooks/use-life-cycle.d.ts.map +0 -1
- package/react/hooks/use-resize-observer.d.ts +0 -2
- package/react/hooks/use-resize-observer.d.ts.map +0 -1
- package/react/hooks/use-sync-ref.d.ts +0 -2
- package/react/hooks/use-sync-ref.d.ts.map +0 -1
- package/react/hooks/use-toggle.d.ts +0 -2
- package/react/hooks/use-toggle.d.ts.map +0 -1
- package/react/hooks/use-value.d.ts +0 -5
- package/react/hooks/use-value.d.ts.map +0 -1
- package/react/hooks/use-visibility-state.d.ts +0 -2
- package/react/hooks/use-visibility-state.d.ts.map +0 -1
- package/react/index.d.ts +0 -2
- package/react/index.d.ts.map +0 -1
- package/sound.d.ts.map +0 -1
- package/storage.d.ts +0 -39
- package/storage.d.ts.map +0 -1
- package/text.d.ts.map +0 -1
- package/type-guard/_exports.d.ts +0 -86
- package/type-guard/_exports.d.ts.map +0 -1
- package/type-guard/index.d.ts +0 -3
- package/type-guard/index.d.ts.map +0 -1
- package/utility-types.cjs +0 -2
- package/utility-types.cjs.map +0 -1
- package/utility-types.d.ts.map +0 -1
- package/utility-types.js +0 -2
- package/utility-types.js.map +0 -1
- package/utils/types.d.ts.map +0 -1
- package/vibrate.d.ts.map +0 -1
- /package/{utils/types.cjs → types.cjs} +0 -0
- /package/{utils/types.cjs.map → types.cjs.map} +0 -0
- /package/{utils/types.js → types.js} +0 -0
- /package/{utils/types.js.map → types.js.map} +0 -0
package/math.cjs
CHANGED
|
@@ -1,2 +1,15 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
function degToRad(deg) {
|
|
4
|
+
return deg * (Math.PI / 180);
|
|
5
|
+
}
|
|
6
|
+
function radToDeg(rad) {
|
|
7
|
+
return rad * (180 / Math.PI);
|
|
8
|
+
}
|
|
9
|
+
const percentFrom = (value, from) => {
|
|
10
|
+
return (value ?? 0) / (from ?? 0) * 100 || 0;
|
|
11
|
+
};
|
|
12
|
+
exports.degToRad = degToRad;
|
|
13
|
+
exports.percentFrom = percentFrom;
|
|
14
|
+
exports.radToDeg = radToDeg;
|
|
2
15
|
//# sourceMappingURL=math.cjs.map
|
package/math.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"math.cjs","sources":["../src/math.ts"],"sourcesContent":["import type { Maybe } from 'yummies/
|
|
1
|
+
{"version":3,"file":"math.cjs","sources":["../src/math.ts"],"sourcesContent":["import type { Maybe } from 'yummies/types';\n\nexport function degToRad(deg: number) {\n return deg * (Math.PI / 180);\n}\nexport function radToDeg(rad: number) {\n return rad * (180 / Math.PI);\n}\n\n/**\n * Получить процент от числа\n * @example\n * ```ts\n * percentFrom(500, 2000) // 25\n * percentFrom(1000, 2000) // 50\n * ```\n */\nexport const percentFrom = (value: Maybe<number>, from: Maybe<number>) => {\n return ((value ?? 0) / (from ?? 0)) * 100 || 0;\n};\n"],"names":[],"mappings":";;AAEO,SAAS,SAAS,KAAa;AACpC,SAAO,OAAO,KAAK,KAAK;AAC1B;AACO,SAAS,SAAS,KAAa;AACpC,SAAO,OAAO,MAAM,KAAK;AAC3B;AAUO,MAAM,cAAc,CAAC,OAAsB,SAAwB;AACxE,UAAS,SAAS,MAAM,QAAQ,KAAM,OAAO;AAC/C;;;;"}
|
package/math.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { Maybe } from '
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { Maybe } from 'yummies/types';
|
|
2
|
+
|
|
3
|
+
declare function degToRad(deg: number): number;
|
|
4
|
+
declare function radToDeg(rad: number): number;
|
|
4
5
|
/**
|
|
5
6
|
* Получить процент от числа
|
|
6
7
|
* @example
|
|
@@ -9,5 +10,6 @@ export declare function radToDeg(rad: number): number;
|
|
|
9
10
|
* percentFrom(1000, 2000) // 50
|
|
10
11
|
* ```
|
|
11
12
|
*/
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
declare const percentFrom: (value: Maybe<number>, from: Maybe<number>) => number;
|
|
14
|
+
|
|
15
|
+
export { degToRad, percentFrom, radToDeg };
|
package/math.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"math.js","sources":["../src/math.ts"],"sourcesContent":["import type { Maybe } from 'yummies/
|
|
1
|
+
{"version":3,"file":"math.js","sources":["../src/math.ts"],"sourcesContent":["import type { Maybe } from 'yummies/types';\n\nexport function degToRad(deg: number) {\n return deg * (Math.PI / 180);\n}\nexport function radToDeg(rad: number) {\n return rad * (180 / Math.PI);\n}\n\n/**\n * Получить процент от числа\n * @example\n * ```ts\n * percentFrom(500, 2000) // 25\n * percentFrom(1000, 2000) // 50\n * ```\n */\nexport const percentFrom = (value: Maybe<number>, from: Maybe<number>) => {\n return ((value ?? 0) / (from ?? 0)) * 100 || 0;\n};\n"],"names":[],"mappings":"AAEO,SAAS,SAAS,KAAa;AACpC,SAAO,OAAO,KAAK,KAAK;AAC1B;AACO,SAAS,SAAS,KAAa;AACpC,SAAO,OAAO,MAAM,KAAK;AAC3B;AAUO,MAAM,cAAc,CAAC,OAAsB,SAAwB;AACxE,UAAS,SAAS,MAAM,QAAQ,KAAM,OAAO;AAC/C;"}
|
package/media.cjs
CHANGED
|
@@ -1,2 +1,106 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const math = require("yummies/math");
|
|
4
|
+
function blobToBase64(blob) {
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
const reader = new FileReader();
|
|
7
|
+
reader.onloadend = () => resolve(reader.result);
|
|
8
|
+
reader.onerror = reject;
|
|
9
|
+
reader.readAsDataURL(blob);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
const blobToUrl = (urlOrBlob) => urlOrBlob instanceof Blob ? URL.createObjectURL(urlOrBlob) : urlOrBlob;
|
|
13
|
+
const fileToBlob = (file) => {
|
|
14
|
+
return new Blob([file], { type: file.type });
|
|
15
|
+
};
|
|
16
|
+
const imageToBlob = (imageElement, mimeType = "image/png") => {
|
|
17
|
+
const canvas = document.createElement("canvas");
|
|
18
|
+
canvas.width = imageElement.naturalWidth || 300;
|
|
19
|
+
canvas.height = imageElement.naturalHeight || 300;
|
|
20
|
+
canvas.getContext("2d").drawImage(imageElement, 0, 0);
|
|
21
|
+
const dataUri = canvas.toDataURL(mimeType, 1);
|
|
22
|
+
const base64data = dataUri.split(",")[1];
|
|
23
|
+
const base64MimeType = dataUri.split(";")[0].slice(5);
|
|
24
|
+
const bytes = globalThis.atob(base64data);
|
|
25
|
+
const buf = new ArrayBuffer(bytes.length);
|
|
26
|
+
const array = new Uint8Array(buf);
|
|
27
|
+
for (let index = 0; index < bytes.length; index++) {
|
|
28
|
+
array[index] = bytes.charCodeAt(index);
|
|
29
|
+
}
|
|
30
|
+
const blob = new Blob([array], { type: base64MimeType });
|
|
31
|
+
return blob;
|
|
32
|
+
};
|
|
33
|
+
const renderImage = (urlOrBlob) => new Promise((resolve, reject) => {
|
|
34
|
+
const image = new Image();
|
|
35
|
+
image.src = blobToUrl(urlOrBlob);
|
|
36
|
+
image.onload = () => resolve(image);
|
|
37
|
+
image.onerror = () => reject();
|
|
38
|
+
});
|
|
39
|
+
function cropImageFromCanvas(context) {
|
|
40
|
+
const canvas = context.canvas;
|
|
41
|
+
let w = canvas.width;
|
|
42
|
+
let h = canvas.height;
|
|
43
|
+
const pix = { x: [], y: [] };
|
|
44
|
+
const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
|
|
45
|
+
let x;
|
|
46
|
+
let y;
|
|
47
|
+
let index;
|
|
48
|
+
for (y = 0; y < h; y++) {
|
|
49
|
+
for (x = 0; x < w; x++) {
|
|
50
|
+
index = (y * w + x) * 4;
|
|
51
|
+
if (imageData.data[index + 3] > 0) {
|
|
52
|
+
pix.x.push(x);
|
|
53
|
+
pix.y.push(y);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
pix.x.sort((a, b) => a - b);
|
|
58
|
+
pix.y.sort((a, b) => a - b);
|
|
59
|
+
const n = pix.x.length - 1;
|
|
60
|
+
w = 1 + pix.x[n] - pix.x[0];
|
|
61
|
+
h = 1 + pix.y[n] - pix.y[0];
|
|
62
|
+
const cut = context.getImageData(pix.x[0], pix.y[0], w, h);
|
|
63
|
+
canvas.width = w;
|
|
64
|
+
canvas.height = h;
|
|
65
|
+
context.putImageData(cut, 0, 0);
|
|
66
|
+
return canvas;
|
|
67
|
+
}
|
|
68
|
+
const rotateImage = (image, angle) => {
|
|
69
|
+
const maxSize = Math.max(image.width, image.height);
|
|
70
|
+
const canvas = document.createElement("canvas");
|
|
71
|
+
canvas.width = maxSize;
|
|
72
|
+
canvas.height = maxSize;
|
|
73
|
+
const context = canvas.getContext("2d");
|
|
74
|
+
context.save();
|
|
75
|
+
context.translate(canvas.width / 2, canvas.height / 2);
|
|
76
|
+
context.rotate(math.degToRad(angle));
|
|
77
|
+
context.drawImage(image, -image.width / 2, -image.height / 2);
|
|
78
|
+
context.restore();
|
|
79
|
+
cropImageFromCanvas(context);
|
|
80
|
+
return renderImage(canvas.toDataURL("image/png"));
|
|
81
|
+
};
|
|
82
|
+
function decodeDataUrl(url) {
|
|
83
|
+
const regex = /^data:(.*);base64,\s?(.*)$/;
|
|
84
|
+
const matches = new RegExp(regex).exec(url);
|
|
85
|
+
return {
|
|
86
|
+
mimeType: matches?.[1],
|
|
87
|
+
data: matches?.[2]
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
const isHttpUrl = (url) => {
|
|
91
|
+
return url.startsWith("https://") || url.startsWith("http://");
|
|
92
|
+
};
|
|
93
|
+
const isBase64Image = (str) => {
|
|
94
|
+
const { mimeType, data } = decodeDataUrl(str);
|
|
95
|
+
return !!(data && mimeType?.startsWith("image/"));
|
|
96
|
+
};
|
|
97
|
+
exports.blobToBase64 = blobToBase64;
|
|
98
|
+
exports.blobToUrl = blobToUrl;
|
|
99
|
+
exports.decodeDataUrl = decodeDataUrl;
|
|
100
|
+
exports.fileToBlob = fileToBlob;
|
|
101
|
+
exports.imageToBlob = imageToBlob;
|
|
102
|
+
exports.isBase64Image = isBase64Image;
|
|
103
|
+
exports.isHttpUrl = isHttpUrl;
|
|
104
|
+
exports.renderImage = renderImage;
|
|
105
|
+
exports.rotateImage = rotateImage;
|
|
2
106
|
//# sourceMappingURL=media.cjs.map
|
package/media.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"media.cjs","sources":["../src/media.ts"],"sourcesContent":["import { degToRad } from 'yummies/math';\n\nexport function blobToBase64(blob: Blob): Promise<string> {\n return new Promise((resolve, reject) => {\n const reader = new FileReader();\n reader.onloadend = () => resolve(reader.result as string);\n reader.onerror = reject;\n reader.readAsDataURL(blob);\n });\n}\n\nexport const blobToUrl = (urlOrBlob: string | Blob) =>\n urlOrBlob instanceof Blob ? URL.createObjectURL(urlOrBlob) : urlOrBlob;\n\nexport const fileToBlob = (file: File) => {\n return new Blob([file], { type: file.type });\n};\n\nexport const imageToBlob = (\n imageElement: HTMLImageElement,\n mimeType: string = 'image/png',\n) => {\n const canvas = document.createElement('canvas');\n\n canvas.width = imageElement.naturalWidth || 300;\n canvas.height = imageElement.naturalHeight || 300;\n\n canvas.getContext('2d')!.drawImage(imageElement, 0, 0);\n\n const dataUri = canvas.toDataURL(mimeType, 1);\n const base64data = dataUri.split(',')[1];\n const base64MimeType = dataUri.split(';')[0].slice(5);\n\n const bytes = globalThis.atob(base64data);\n const buf = new ArrayBuffer(bytes.length);\n const array = new Uint8Array(buf);\n\n for (let index = 0; index < bytes.length; index++) {\n array[index] = bytes.charCodeAt(index);\n }\n\n const blob = new Blob([array], { type: base64MimeType });\n\n return blob;\n};\n\n/**\n * Загружает и отрисовывает изображение с использованием Image\n *\n * @returns {Promise<HTMLImageElement>}\n */\nexport const renderImage = (urlOrBlob: Blob | string) =>\n new Promise<HTMLImageElement>((resolve, reject) => {\n const image = new Image();\n image.src = blobToUrl(urlOrBlob);\n image.onload = () => resolve(image);\n image.onerror = () => reject();\n });\n\nfunction cropImageFromCanvas(context: CanvasRenderingContext2D) {\n const canvas = context.canvas;\n let w = canvas.width;\n let h = canvas.height;\n const pix: { x: number[]; y: number[] } = { x: [], y: [] };\n const imageData = context.getImageData(0, 0, canvas.width, canvas.height);\n let x: number;\n let y: number;\n let index: number;\n\n for (y = 0; y < h; y++) {\n for (x = 0; x < w; x++) {\n index = (y * w + x) * 4;\n if (imageData.data[index + 3] > 0) {\n pix.x.push(x);\n pix.y.push(y);\n }\n }\n }\n pix.x.sort((a, b) => a - b);\n pix.y.sort((a, b) => a - b);\n const n = pix.x.length - 1;\n\n w = 1 + pix.x[n] - pix.x[0];\n h = 1 + pix.y[n] - pix.y[0];\n const cut = context.getImageData(pix.x[0], pix.y[0], w, h);\n\n canvas.width = w;\n canvas.height = h;\n context.putImageData(cut, 0, 0);\n return canvas;\n}\n\n// TODO: ломает iphone с огромными изображениями\nexport const rotateImage = (image: HTMLImageElement, angle: number) => {\n const maxSize = Math.max(image.width, image.height);\n const canvas = document.createElement('canvas');\n canvas.width = maxSize;\n canvas.height = maxSize;\n const context = canvas.getContext('2d')!;\n context.save();\n context.translate(canvas.width / 2, canvas.height / 2);\n context.rotate(degToRad(angle));\n context.drawImage(image, -image.width / 2, -image.height / 2);\n context.restore();\n cropImageFromCanvas(context);\n return renderImage(canvas.toDataURL('image/png'));\n};\n\ninterface DecodedDataUrl {\n mimeType?: string;\n data?: string;\n}\n\n/*\n * Returning object which contains base64 data and mime type of passed data url string.\n * */\nexport function decodeDataUrl(url: string): DecodedDataUrl {\n const regex = /^data:(.*);base64,\\s?(.*)$/;\n const matches = new RegExp(regex).exec(url);\n\n return {\n mimeType: matches?.[1],\n data: matches?.[2],\n };\n}\n\nexport const isHttpUrl = (url: string): boolean => {\n return url.startsWith('https://') || url.startsWith('http://');\n};\n\nexport const isBase64Image = (str: string): boolean => {\n const { mimeType, data } = decodeDataUrl(str);\n return !!(data && mimeType?.startsWith('image/'));\n};\n"],"names":["
|
|
1
|
+
{"version":3,"file":"media.cjs","sources":["../src/media.ts"],"sourcesContent":["import { degToRad } from 'yummies/math';\n\nexport function blobToBase64(blob: Blob): Promise<string> {\n return new Promise((resolve, reject) => {\n const reader = new FileReader();\n reader.onloadend = () => resolve(reader.result as string);\n reader.onerror = reject;\n reader.readAsDataURL(blob);\n });\n}\n\nexport const blobToUrl = (urlOrBlob: string | Blob) =>\n urlOrBlob instanceof Blob ? URL.createObjectURL(urlOrBlob) : urlOrBlob;\n\nexport const fileToBlob = (file: File) => {\n return new Blob([file], { type: file.type });\n};\n\nexport const imageToBlob = (\n imageElement: HTMLImageElement,\n mimeType: string = 'image/png',\n) => {\n const canvas = document.createElement('canvas');\n\n canvas.width = imageElement.naturalWidth || 300;\n canvas.height = imageElement.naturalHeight || 300;\n\n canvas.getContext('2d')!.drawImage(imageElement, 0, 0);\n\n const dataUri = canvas.toDataURL(mimeType, 1);\n const base64data = dataUri.split(',')[1];\n const base64MimeType = dataUri.split(';')[0].slice(5);\n\n const bytes = globalThis.atob(base64data);\n const buf = new ArrayBuffer(bytes.length);\n const array = new Uint8Array(buf);\n\n for (let index = 0; index < bytes.length; index++) {\n array[index] = bytes.charCodeAt(index);\n }\n\n const blob = new Blob([array], { type: base64MimeType });\n\n return blob;\n};\n\n/**\n * Загружает и отрисовывает изображение с использованием Image\n *\n * @returns {Promise<HTMLImageElement>}\n */\nexport const renderImage = (urlOrBlob: Blob | string) =>\n new Promise<HTMLImageElement>((resolve, reject) => {\n const image = new Image();\n image.src = blobToUrl(urlOrBlob);\n image.onload = () => resolve(image);\n image.onerror = () => reject();\n });\n\nfunction cropImageFromCanvas(context: CanvasRenderingContext2D) {\n const canvas = context.canvas;\n let w = canvas.width;\n let h = canvas.height;\n const pix: { x: number[]; y: number[] } = { x: [], y: [] };\n const imageData = context.getImageData(0, 0, canvas.width, canvas.height);\n let x: number;\n let y: number;\n let index: number;\n\n for (y = 0; y < h; y++) {\n for (x = 0; x < w; x++) {\n index = (y * w + x) * 4;\n if (imageData.data[index + 3] > 0) {\n pix.x.push(x);\n pix.y.push(y);\n }\n }\n }\n pix.x.sort((a, b) => a - b);\n pix.y.sort((a, b) => a - b);\n const n = pix.x.length - 1;\n\n w = 1 + pix.x[n] - pix.x[0];\n h = 1 + pix.y[n] - pix.y[0];\n const cut = context.getImageData(pix.x[0], pix.y[0], w, h);\n\n canvas.width = w;\n canvas.height = h;\n context.putImageData(cut, 0, 0);\n return canvas;\n}\n\n// TODO: ломает iphone с огромными изображениями\nexport const rotateImage = (image: HTMLImageElement, angle: number) => {\n const maxSize = Math.max(image.width, image.height);\n const canvas = document.createElement('canvas');\n canvas.width = maxSize;\n canvas.height = maxSize;\n const context = canvas.getContext('2d')!;\n context.save();\n context.translate(canvas.width / 2, canvas.height / 2);\n context.rotate(degToRad(angle));\n context.drawImage(image, -image.width / 2, -image.height / 2);\n context.restore();\n cropImageFromCanvas(context);\n return renderImage(canvas.toDataURL('image/png'));\n};\n\ninterface DecodedDataUrl {\n mimeType?: string;\n data?: string;\n}\n\n/*\n * Returning object which contains base64 data and mime type of passed data url string.\n * */\nexport function decodeDataUrl(url: string): DecodedDataUrl {\n const regex = /^data:(.*);base64,\\s?(.*)$/;\n const matches = new RegExp(regex).exec(url);\n\n return {\n mimeType: matches?.[1],\n data: matches?.[2],\n };\n}\n\nexport const isHttpUrl = (url: string): boolean => {\n return url.startsWith('https://') || url.startsWith('http://');\n};\n\nexport const isBase64Image = (str: string): boolean => {\n const { mimeType, data } = decodeDataUrl(str);\n return !!(data && mimeType?.startsWith('image/'));\n};\n"],"names":["degToRad"],"mappings":";;;AAEO,SAAS,aAAa,MAA6B;AACxD,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,UAAM,SAAS,IAAI,WAAA;AACnB,WAAO,YAAY,MAAM,QAAQ,OAAO,MAAgB;AACxD,WAAO,UAAU;AACjB,WAAO,cAAc,IAAI;AAAA,EAC3B,CAAC;AACH;AAEO,MAAM,YAAY,CAAC,cACxB,qBAAqB,OAAO,IAAI,gBAAgB,SAAS,IAAI;AAExD,MAAM,aAAa,CAAC,SAAe;AACxC,SAAO,IAAI,KAAK,CAAC,IAAI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AAK,GAAG;AAE1C,SAAO;AAAA,IACL,UAAU,UAAU,CAAC;AAAA,IACrB,MAAM,UAAU,CAAC;AAAA,EAAA;AAErB;AAEO,MAAM,YAAY,CAAC,QAAyB;AACjD,SAAO,IAAI,WAAW,UAAU,KAAK,IAAI,WAAW,SAAS;AAC/D;AAEO,MAAM,gBAAgB,CAAC,QAAyB;AACrD,QAAM,EAAE,UAAU,SAAS,cAAc,GAAG;AAC5C,SAAO,CAAC,EAAE,QAAQ,UAAU,WAAW,QAAQ;AACjD;;;;;;;;;;"}
|
package/media.d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
declare function blobToBase64(blob: Blob): Promise<string>;
|
|
2
|
+
declare const blobToUrl: (urlOrBlob: string | Blob) => string;
|
|
3
|
+
declare const fileToBlob: (file: File) => Blob;
|
|
4
|
+
declare const imageToBlob: (imageElement: HTMLImageElement, mimeType?: string) => Blob;
|
|
5
5
|
/**
|
|
6
6
|
* Загружает и отрисовывает изображение с использованием Image
|
|
7
7
|
*
|
|
8
8
|
* @returns {Promise<HTMLImageElement>}
|
|
9
9
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
declare const renderImage: (urlOrBlob: Blob | string) => Promise<HTMLImageElement>;
|
|
11
|
+
declare const rotateImage: (image: HTMLImageElement, angle: number) => Promise<HTMLImageElement>;
|
|
12
12
|
interface DecodedDataUrl {
|
|
13
13
|
mimeType?: string;
|
|
14
14
|
data?: string;
|
|
15
15
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
declare function decodeDataUrl(url: string): DecodedDataUrl;
|
|
17
|
+
declare const isHttpUrl: (url: string) => boolean;
|
|
18
|
+
declare const isBase64Image: (str: string) => boolean;
|
|
19
|
+
|
|
20
|
+
export { blobToBase64, blobToUrl, decodeDataUrl, fileToBlob, imageToBlob, isBase64Image, isHttpUrl, renderImage, rotateImage };
|
package/mobx.cjs
CHANGED
|
@@ -1,2 +1,185 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const mobx = require("mobx");
|
|
4
|
+
const typeGuard = require("yummies/type-guard");
|
|
5
|
+
const applyObservable = (context, annotationsArray, useDecorators) => {
|
|
6
|
+
if (useDecorators) {
|
|
7
|
+
annotationsArray.forEach(([field, annotation]) => {
|
|
8
|
+
annotation(context, field);
|
|
9
|
+
});
|
|
10
|
+
mobx.makeObservable(context);
|
|
11
|
+
} else {
|
|
12
|
+
mobx.makeObservable(context, Object.fromEntries(annotationsArray));
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
const createEnhancedAtom = (name, onBecomeObservedHandler, onBecomeUnobservedHandler, meta) => {
|
|
16
|
+
const atom = mobx.createAtom(
|
|
17
|
+
name,
|
|
18
|
+
onBecomeObservedHandler && (() => onBecomeObservedHandler(atom)),
|
|
19
|
+
onBecomeUnobservedHandler && (() => onBecomeUnobservedHandler(atom))
|
|
20
|
+
);
|
|
21
|
+
atom.meta = meta ?? {};
|
|
22
|
+
atom.reportChanged = atom.reportChanged.bind(atom);
|
|
23
|
+
atom.reportObserved = atom.reportObserved.bind(atom);
|
|
24
|
+
return atom;
|
|
25
|
+
};
|
|
26
|
+
const createRef = (cfg) => {
|
|
27
|
+
const comparer = cfg?.comparer ?? mobx.comparer.default;
|
|
28
|
+
const ref = (value) => {
|
|
29
|
+
const nextValue = value ?? null;
|
|
30
|
+
if (comparer(ref.current, nextValue)) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
mobx.runInAction(() => {
|
|
34
|
+
ref.current = nextValue;
|
|
35
|
+
ref.listeners.forEach((listener) => {
|
|
36
|
+
listener(ref.current);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
ref.listeners = new Set(cfg?.onChange ? [cfg.onChange] : []);
|
|
41
|
+
if (cfg?.onSet || cfg?.onUnset) {
|
|
42
|
+
ref.listeners.add((value) => {
|
|
43
|
+
if (value) {
|
|
44
|
+
cfg.onSet?.(value);
|
|
45
|
+
} else {
|
|
46
|
+
cfg.onUnset?.();
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
ref.current = cfg?.initial ?? null;
|
|
51
|
+
ref.meta = cfg?.meta ?? {};
|
|
52
|
+
mobx.makeObservable(ref, {
|
|
53
|
+
current: mobx.observable.ref,
|
|
54
|
+
meta: mobx.observable
|
|
55
|
+
});
|
|
56
|
+
return ref;
|
|
57
|
+
};
|
|
58
|
+
const isRef = (value) => {
|
|
59
|
+
return typeof value === "function" && "current" in value;
|
|
60
|
+
};
|
|
61
|
+
const toRef = (value, cfg) => {
|
|
62
|
+
return isRef(value) ? value : createRef({ initial: value, ...cfg });
|
|
63
|
+
};
|
|
64
|
+
class DeepObservableStruct {
|
|
65
|
+
data;
|
|
66
|
+
constructor(data) {
|
|
67
|
+
this.data = data;
|
|
68
|
+
mobx.makeObservable(this, {
|
|
69
|
+
data: mobx.observable.deep,
|
|
70
|
+
set: mobx.action
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
set(newData) {
|
|
74
|
+
const stack = Object.keys(this.data).map((key) => [
|
|
75
|
+
key,
|
|
76
|
+
this.data,
|
|
77
|
+
newData
|
|
78
|
+
]);
|
|
79
|
+
let currentIndex = 0;
|
|
80
|
+
let stackLength = stack.length;
|
|
81
|
+
while (currentIndex < stackLength) {
|
|
82
|
+
const [key, currObservableData, newData2] = stack[currentIndex];
|
|
83
|
+
const newValue = newData2[key];
|
|
84
|
+
const currValue = currObservableData[key];
|
|
85
|
+
currentIndex++;
|
|
86
|
+
if (key in newData2) {
|
|
87
|
+
if (typeGuard.typeGuard.isObject(newValue) && typeGuard.typeGuard.isObject(currValue)) {
|
|
88
|
+
const newValueKeys = Object.keys(newValue);
|
|
89
|
+
Object.keys(currValue).forEach((childKey) => {
|
|
90
|
+
if (!(childKey in newValue)) {
|
|
91
|
+
delete currObservableData[key][childKey];
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
newValueKeys.forEach((childKey) => {
|
|
95
|
+
const length = stack.push([
|
|
96
|
+
childKey,
|
|
97
|
+
currObservableData[key],
|
|
98
|
+
newValue
|
|
99
|
+
]);
|
|
100
|
+
stackLength = length;
|
|
101
|
+
});
|
|
102
|
+
} else if (newValue !== currValue) {
|
|
103
|
+
currObservableData[key] = newValue;
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
delete currObservableData[key];
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
Object.keys(newData).forEach((newDataKey) => {
|
|
110
|
+
if (!this.data[newDataKey]) {
|
|
111
|
+
this.data[newDataKey] = newData[newDataKey];
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const getMobxAdministration = (context) => context[mobx.$mobx];
|
|
117
|
+
const lazyObserve = ({
|
|
118
|
+
context,
|
|
119
|
+
property,
|
|
120
|
+
onStart,
|
|
121
|
+
onEnd,
|
|
122
|
+
endDelay = false
|
|
123
|
+
}) => {
|
|
124
|
+
let timeoutId;
|
|
125
|
+
let metaData;
|
|
126
|
+
const observingProps = /* @__PURE__ */ new Set();
|
|
127
|
+
const properties = Array.isArray(property) ? property : [property];
|
|
128
|
+
const cleanup = () => {
|
|
129
|
+
observingProps.clear();
|
|
130
|
+
if (endDelay === false) {
|
|
131
|
+
onEnd?.(metaData, cleanup);
|
|
132
|
+
metaData = void 0;
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (timeoutId) {
|
|
136
|
+
clearTimeout(timeoutId);
|
|
137
|
+
timeoutId = void 0;
|
|
138
|
+
}
|
|
139
|
+
timeoutId = setTimeout(() => {
|
|
140
|
+
onEnd?.(metaData, cleanup);
|
|
141
|
+
timeoutId = void 0;
|
|
142
|
+
metaData = void 0;
|
|
143
|
+
}, endDelay);
|
|
144
|
+
};
|
|
145
|
+
const start = (property2) => {
|
|
146
|
+
const isAlreadyObserving = observingProps.size > 0;
|
|
147
|
+
observingProps.add(property2);
|
|
148
|
+
if (isAlreadyObserving) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
if (timeoutId) {
|
|
152
|
+
clearTimeout(timeoutId);
|
|
153
|
+
timeoutId = void 0;
|
|
154
|
+
}
|
|
155
|
+
metaData = onStart?.();
|
|
156
|
+
};
|
|
157
|
+
const stop = (property2) => {
|
|
158
|
+
const isAlreadyNotObserving = !observingProps.size;
|
|
159
|
+
observingProps.delete(property2);
|
|
160
|
+
const isObserving = observingProps.size > 0;
|
|
161
|
+
if (isAlreadyNotObserving || isObserving) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
cleanup();
|
|
165
|
+
};
|
|
166
|
+
properties.forEach((property2) => {
|
|
167
|
+
if (context) {
|
|
168
|
+
mobx.onBecomeObserved(context, property2, () => start(property2));
|
|
169
|
+
mobx.onBecomeUnobserved(context, property2, () => stop(property2));
|
|
170
|
+
} else {
|
|
171
|
+
mobx.onBecomeObserved(property2, () => start(property2));
|
|
172
|
+
mobx.onBecomeUnobserved(property2, () => stop(property2));
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
return cleanup;
|
|
176
|
+
};
|
|
177
|
+
exports.DeepObservableStruct = DeepObservableStruct;
|
|
178
|
+
exports.applyObservable = applyObservable;
|
|
179
|
+
exports.createEnhancedAtom = createEnhancedAtom;
|
|
180
|
+
exports.createRef = createRef;
|
|
181
|
+
exports.getMobxAdministration = getMobxAdministration;
|
|
182
|
+
exports.isRef = isRef;
|
|
183
|
+
exports.lazyObserve = lazyObserve;
|
|
184
|
+
exports.toRef = toRef;
|
|
2
185
|
//# sourceMappingURL=mobx.cjs.map
|
package/mobx.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mobx.cjs","sources":["../src/mobx/deep-observable-struct.ts","../src/mobx/apply-observable.ts","../src/mobx/create-enhanced-atom.ts","../src/mobx/create-ref.ts","../src/mobx/get-mobx-administration.ts","../src/mobx/lazy-observe.ts"],"sourcesContent":["import { action, makeObservable, observable } from 'mobx';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport class DeepObservableStruct<TData extends AnyObject> {\n data: TData;\n\n constructor(data: TData) {\n this.data = data;\n\n makeObservable(this, {\n data: observable.deep,\n set: action,\n });\n }\n\n set(newData: Partial<TData>) {\n type StackItem = [key: string, currObservable: AnyObject, new: AnyObject];\n\n const stack: StackItem[] = Object.keys(this.data).map((key) => [\n key,\n this.data,\n newData,\n ]);\n\n let currentIndex = 0;\n let stackLength = stack.length;\n\n while (currentIndex < stackLength) {\n const [key, currObservableData, newData] = stack[currentIndex];\n const newValue = newData[key];\n const currValue = currObservableData[key];\n\n currentIndex++;\n\n if (key in newData) {\n if (typeGuard.isObject(newValue) && typeGuard.isObject(currValue)) {\n const newValueKeys = Object.keys(newValue);\n\n Object.keys(currValue).forEach((childKey) => {\n if (!(childKey in newValue)) {\n delete currObservableData[key][childKey];\n }\n });\n\n newValueKeys.forEach((childKey) => {\n const length = stack.push([\n childKey,\n currObservableData[key],\n newValue,\n ]);\n stackLength = length;\n });\n } else if (newValue !== currValue) {\n currObservableData[key] = newValue;\n }\n } else {\n delete currObservableData[key];\n }\n }\n\n Object.keys(newData).forEach((newDataKey) => {\n if (!this.data[newDataKey]) {\n // @ts-expect-error\n this.data[newDataKey] = newData[newDataKey];\n }\n });\n }\n}\n","import { makeObservable } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport type ObservableAnnotationsArray = [string, any][];\n\nexport const applyObservable = (\n context: AnyObject,\n annotationsArray: ObservableAnnotationsArray,\n useDecorators?: boolean,\n) => {\n if (useDecorators) {\n annotationsArray.forEach(([field, annotation]) => {\n annotation(context, field);\n });\n\n makeObservable(context);\n } else {\n makeObservable(context, Object.fromEntries(annotationsArray));\n }\n};\n","import { createAtom, type IAtom } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\nexport interface IEnhancedAtom<TMeta extends AnyObject = AnyObject>\n extends IAtom {\n meta: TMeta;\n}\n\n/**\n * Creates an enhanced atom with meta data\n * And bind `reportChanged` and `reportObserved` method to the atom\n */\nexport const createEnhancedAtom = <TMeta extends AnyObject>(\n name: string,\n onBecomeObservedHandler?: (atom: IEnhancedAtom<TMeta>) => void,\n onBecomeUnobservedHandler?: (atom: IEnhancedAtom<TMeta>) => void,\n meta?: TMeta,\n): IEnhancedAtom<TMeta> => {\n const atom = createAtom(\n name,\n onBecomeObservedHandler && (() => onBecomeObservedHandler(atom)),\n onBecomeUnobservedHandler && (() => onBecomeUnobservedHandler(atom)),\n ) as IEnhancedAtom<TMeta>;\n atom.meta = meta ?? ({} as TMeta);\n atom.reportChanged = atom.reportChanged.bind(atom);\n atom.reportObserved = atom.reportObserved.bind(atom);\n return atom;\n};\n","import {\n type IEqualsComparer,\n makeObservable,\n comparer as mobxComparer,\n observable,\n runInAction,\n} from 'mobx';\nimport type { AnyObject, Maybe } from 'yummies/utils/types';\n\nexport type RefChangeListener<T> = (value: T | null) => void;\n\n/**\n * Alternative to React.createRef but works in MobX world.\n * Typically it the should be the same React.LegacyRef (fn style)\n */\nexport type Ref<T = any, TMeta = AnyObject> = ((element: Maybe<T>) => void) & {\n listeners: Set<RefChangeListener<T>>;\n current: T | null;\n meta: TMeta;\n};\n\n/**\n * Creates ref thing to attach HTMLElements in React and all other\n */\nexport const createRef = <T = HTMLElement, TMeta = AnyObject>(cfg?: {\n onSet?: (node: T) => void;\n onUnset?: () => void;\n onChange?: RefChangeListener<T>;\n meta?: TMeta;\n initial?: Maybe<T>;\n comparer?: IEqualsComparer<T | null>;\n}): Ref<T, TMeta> => {\n const comparer = cfg?.comparer ?? mobxComparer.default;\n\n const actionFn = ((value: Maybe<T>) => {\n const nextValue = value ?? null;\n\n if (comparer(actionFn.current, nextValue)) {\n return;\n }\n\n runInAction(() => {\n actionFn.current = nextValue;\n\n actionFn.listeners.forEach((listener) => {\n listener(actionFn.current);\n });\n });\n }) as Ref<T, TMeta>;\n\n actionFn.listeners = new Set(cfg?.onChange ? [cfg.onChange] : []);\n\n if (cfg?.onSet || cfg?.onUnset) {\n actionFn.listeners.add((value) => {\n if (value) {\n cfg.onSet?.(value);\n } else {\n cfg.onUnset?.();\n }\n });\n }\n\n actionFn.current = cfg?.initial ?? null;\n actionFn.meta = cfg?.meta ?? ({} as TMeta);\n\n makeObservable(actionFn, {\n current: observable.ref,\n meta: observable,\n });\n\n return actionFn;\n};\n\nexport const isRef = <T, TMeta = AnyObject>(\n value: any,\n): value is Ref<T, TMeta> => {\n return typeof value === 'function' && 'current' in value;\n};\n","import { $mobx, type AnnotationMapEntry } from 'mobx';\nimport type { AnyObject } from 'yummies/utils/types';\n\ntype ObservableObjectAdministration = Parameters<\n Exclude<AnnotationMapEntry, boolean>['make_']\n>[0];\n\nexport const getMobxAdministration = (\n context: AnyObject,\n): ObservableObjectAdministration => context[$mobx];\n","import { onBecomeObserved, onBecomeUnobserved } from 'mobx';\n\n/**\n * When ONE OF the properties is becomes observed then `onStart` function is called.\n * WHen ALL properties are unobserved then `onEnd` function is called with the `metaData` that was returned by `onStart`.\n *\n * It uses `onBecomeObserved` and `onBecomeUnobserved` mobx hooks to perform lazy observation.\n */\nexport const lazyObserve = <TMetaData = void>({\n context,\n property,\n onStart,\n onEnd,\n endDelay = false,\n}: {\n context?: any;\n property: any | any[];\n onStart?: () => TMetaData;\n onEnd?: (metaData: TMetaData, cleanupFn: VoidFunction) => void;\n endDelay?: number | false;\n}) => {\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n let metaData: TMetaData | undefined;\n const observingProps = new Set<string>();\n const properties = Array.isArray(property) ? property : [property];\n\n const cleanup = () => {\n observingProps.clear();\n\n if (endDelay === false) {\n onEnd?.(metaData!, cleanup);\n metaData = undefined;\n return;\n }\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n\n timeoutId = setTimeout(() => {\n onEnd?.(metaData!, cleanup);\n timeoutId = undefined;\n metaData = undefined;\n }, endDelay);\n };\n\n const start = (property: string) => {\n const isAlreadyObserving = observingProps.size > 0;\n observingProps.add(property);\n\n if (isAlreadyObserving) {\n return;\n }\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n\n metaData = onStart?.();\n };\n\n const stop = (property: string) => {\n const isAlreadyNotObserving = !observingProps.size;\n\n observingProps.delete(property);\n\n const isObserving = observingProps.size > 0;\n\n if (isAlreadyNotObserving || isObserving) {\n return;\n }\n\n cleanup();\n };\n\n properties.forEach((property) => {\n if (context) {\n onBecomeObserved(context, property, () => start(property));\n onBecomeUnobserved(context, property, () => stop(property));\n } else {\n onBecomeObserved(property, () => start(property));\n onBecomeUnobserved(property, () => stop(property));\n }\n });\n\n return cleanup;\n};\n"],"names":["data","constructor","this","makeObservable","observable","deep","set","action","newData","stack","Object","keys","map","key","currentIndex","stackLength","length","currObservableData","newValue","currValue","typeGuard","isObject","newValueKeys","forEach","childKey","push","newDataKey","context","annotationsArray","useDecorators","field","annotation","fromEntries","name","onBecomeObservedHandler","onBecomeUnobservedHandler","meta","atom","createAtom","reportChanged","bind","reportObserved","cfg","comparer","mobxComparer","default","actionFn","value","nextValue","current","runInAction","listeners","listener","Set","onChange","onSet","onUnset","add","initial","ref","$mobx","property","onStart","onEnd","endDelay","timeoutId","metaData","observingProps","properties","Array","isArray","cleanup","clear","clearTimeout","setTimeout","start","isAlreadyObserving","size","stop","isAlreadyNotObserving","delete","isObserving","onBecomeObserved","onBecomeUnobserved"],"mappings":"qKAIO,MACLA,KAEA,WAAAC,CAAYD,GACVE,KAAKF,KAAOA,EAEZG,EAAAA,eAAeD,KAAM,CACnBF,KAAMI,EAAAA,WAAWC,KACjBC,IAAKC,EAAAA,QAET,CAEA,GAAAD,CAAIE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kC,EAAU,IAAMc,EAAMd,IAChDsB,EAAAA,mBAAmBxD,EAASkC,EAAU,IAAMiB,EAAKjB,MAEjDqB,EAAAA,iBAAiBrB,EAAU,IAAMc,EAAMd,IACvCsB,EAAAA,mBAAmBtB,EAAU,IAAMiB,EAAKjB,OAIrCU"}
|
|
1
|
+
{"version":3,"file":"mobx.cjs","sources":["../src/mobx/apply-observable.ts","../src/mobx/create-enhanced-atom.ts","../src/mobx/create-ref.ts","../src/mobx/deep-observable-struct.ts","../src/mobx/get-mobx-administration.ts","../src/mobx/lazy-observe.ts"],"sourcesContent":["import { makeObservable } from 'mobx';\nimport type { AnyObject } from 'yummies/types';\n\nexport type ObservableAnnotationsArray = [string, any][];\n\nexport const applyObservable = (\n context: AnyObject,\n annotationsArray: ObservableAnnotationsArray,\n useDecorators?: boolean,\n) => {\n if (useDecorators) {\n annotationsArray.forEach(([field, annotation]) => {\n annotation(context, field);\n });\n\n makeObservable(context);\n } else {\n makeObservable(context, Object.fromEntries(annotationsArray));\n }\n};\n","import { createAtom, type IAtom } from 'mobx';\nimport type { AnyObject } from 'yummies/types';\n\nexport interface IEnhancedAtom<TMeta extends AnyObject = AnyObject>\n extends IAtom {\n meta: TMeta;\n}\n\n/**\n * Creates an enhanced atom with meta data\n * And bind `reportChanged` and `reportObserved` method to the atom\n */\nexport const createEnhancedAtom = <TMeta extends AnyObject>(\n name: string,\n onBecomeObservedHandler?: (atom: IEnhancedAtom<TMeta>) => void,\n onBecomeUnobservedHandler?: (atom: IEnhancedAtom<TMeta>) => void,\n meta?: TMeta,\n): IEnhancedAtom<TMeta> => {\n const atom = createAtom(\n name,\n onBecomeObservedHandler && (() => onBecomeObservedHandler(atom)),\n onBecomeUnobservedHandler && (() => onBecomeUnobservedHandler(atom)),\n ) as IEnhancedAtom<TMeta>;\n atom.meta = meta ?? ({} as TMeta);\n atom.reportChanged = atom.reportChanged.bind(atom);\n atom.reportObserved = atom.reportObserved.bind(atom);\n return atom;\n};\n","import {\n type IEqualsComparer,\n makeObservable,\n comparer as mobxComparer,\n observable,\n runInAction,\n} from 'mobx';\nimport type { AnyObject, Maybe } from 'yummies/types';\n\nexport type RefChangeListener<T> = (value: T | null) => void;\n\n/**\n * Alternative to React.createRef but works in MobX world.\n * Typically it the should be the same React.LegacyRef (fn style)\n */\nexport interface Ref<T = any, TMeta = AnyObject> {\n (element: Maybe<T>): void;\n\n listeners: Set<RefChangeListener<NoInfer<T>>>;\n current: NoInfer<T> | null;\n meta: TMeta;\n}\n\nexport interface CreateRefConfig<T = any, TMeta = AnyObject> {\n onSet?: (node: T) => void;\n onUnset?: () => void;\n onChange?: RefChangeListener<T>;\n meta?: TMeta;\n initial?: Maybe<T>;\n comparer?: IEqualsComparer<T | null>;\n}\n\n/**\n * Creates ref thing to attach HTMLElements in React and all other\n */\nexport const createRef = <T = any, TMeta = AnyObject>(\n cfg?: CreateRefConfig<T, TMeta>,\n): Ref<T, TMeta> => {\n const comparer = cfg?.comparer ?? mobxComparer.default;\n\n const ref = ((value: Maybe<T>) => {\n const nextValue = value ?? null;\n\n if (comparer(ref.current, nextValue)) {\n return;\n }\n\n runInAction(() => {\n ref.current = nextValue;\n\n ref.listeners.forEach((listener) => {\n listener(ref.current);\n });\n });\n }) as Ref<T, TMeta>;\n\n ref.listeners = new Set(cfg?.onChange ? [cfg.onChange] : []);\n\n if (cfg?.onSet || cfg?.onUnset) {\n ref.listeners.add((value) => {\n if (value) {\n cfg.onSet?.(value);\n } else {\n cfg.onUnset?.();\n }\n });\n }\n\n ref.current = cfg?.initial ?? null;\n ref.meta = cfg?.meta ?? ({} as TMeta);\n\n makeObservable(ref, {\n current: observable.ref,\n meta: observable,\n });\n\n return ref;\n};\n\nexport const isRef = <T, TMeta = any>(\n value: T | Ref<T, TMeta>,\n): value is Ref<T, TMeta> => {\n return typeof value === 'function' && 'current' in value;\n};\n\nexport const toRef = <T, TMeta = any>(\n value: T | Ref<T, TMeta>,\n cfg?: Omit<CreateRefConfig<T, TMeta>, 'initial'>,\n): Ref<T, TMeta> => {\n return isRef(value) ? value : createRef({ initial: value, ...cfg });\n};\n","import { action, makeObservable, observable } from 'mobx';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { AnyObject } from 'yummies/types';\n\nexport class DeepObservableStruct<TData extends AnyObject> {\n data: TData;\n\n constructor(data: TData) {\n this.data = data;\n\n makeObservable(this, {\n data: observable.deep,\n set: action,\n });\n }\n\n set(newData: Partial<TData>) {\n type StackItem = [key: string, currObservable: AnyObject, new: AnyObject];\n\n const stack: StackItem[] = Object.keys(this.data).map((key) => [\n key,\n this.data,\n newData,\n ]);\n\n let currentIndex = 0;\n let stackLength = stack.length;\n\n while (currentIndex < stackLength) {\n const [key, currObservableData, newData] = stack[currentIndex];\n const newValue = newData[key];\n const currValue = currObservableData[key];\n\n currentIndex++;\n\n if (key in newData) {\n if (typeGuard.isObject(newValue) && typeGuard.isObject(currValue)) {\n const newValueKeys = Object.keys(newValue);\n\n Object.keys(currValue).forEach((childKey) => {\n if (!(childKey in newValue)) {\n delete currObservableData[key][childKey];\n }\n });\n\n newValueKeys.forEach((childKey) => {\n const length = stack.push([\n childKey,\n currObservableData[key],\n newValue,\n ]);\n stackLength = length;\n });\n } else if (newValue !== currValue) {\n currObservableData[key] = newValue;\n }\n } else {\n delete currObservableData[key];\n }\n }\n\n Object.keys(newData).forEach((newDataKey) => {\n if (!this.data[newDataKey]) {\n // @ts-expect-error\n this.data[newDataKey] = newData[newDataKey];\n }\n });\n }\n}\n","import { $mobx, type AnnotationMapEntry } from 'mobx';\nimport type { AnyObject } from 'yummies/types';\n\ntype ObservableObjectAdministration = Parameters<\n Exclude<AnnotationMapEntry, boolean>['make_']\n>[0];\n\nexport const getMobxAdministration = (\n context: AnyObject,\n): ObservableObjectAdministration => context[$mobx];\n","import { onBecomeObserved, onBecomeUnobserved } from 'mobx';\n\n/**\n * When ONE OF the properties is becomes observed then `onStart` function is called.\n * WHen ALL properties are unobserved then `onEnd` function is called with the `metaData` that was returned by `onStart`.\n *\n * It uses `onBecomeObserved` and `onBecomeUnobserved` mobx hooks to perform lazy observation.\n */\nexport const lazyObserve = <TMetaData = void>({\n context,\n property,\n onStart,\n onEnd,\n endDelay = false,\n}: {\n context?: any;\n property: any | any[];\n onStart?: () => TMetaData;\n onEnd?: (metaData: TMetaData, cleanupFn: VoidFunction) => void;\n endDelay?: number | false;\n}) => {\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n let metaData: TMetaData | undefined;\n const observingProps = new Set<string>();\n const properties = Array.isArray(property) ? property : [property];\n\n const cleanup = () => {\n observingProps.clear();\n\n if (endDelay === false) {\n onEnd?.(metaData!, cleanup);\n metaData = undefined;\n return;\n }\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n\n timeoutId = setTimeout(() => {\n onEnd?.(metaData!, cleanup);\n timeoutId = undefined;\n metaData = undefined;\n }, endDelay);\n };\n\n const start = (property: string) => {\n const isAlreadyObserving = observingProps.size > 0;\n observingProps.add(property);\n\n if (isAlreadyObserving) {\n return;\n }\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = undefined;\n }\n\n metaData = onStart?.();\n };\n\n const stop = (property: string) => {\n const isAlreadyNotObserving = !observingProps.size;\n\n observingProps.delete(property);\n\n const isObserving = observingProps.size > 0;\n\n if (isAlreadyNotObserving || isObserving) {\n return;\n }\n\n cleanup();\n };\n\n properties.forEach((property) => {\n if (context) {\n onBecomeObserved(context, property, () => start(property));\n onBecomeUnobserved(context, property, () => stop(property));\n } else {\n onBecomeObserved(property, () => start(property));\n onBecomeUnobserved(property, () => stop(property));\n }\n });\n\n return cleanup;\n};\n"],"names":["makeObservable","createAtom","mobxComparer","runInAction","observable","action","newData","typeGuard","$mobx","property","onBecomeObserved","onBecomeUnobserved"],"mappings":";;;;AAKO,MAAM,kBAAkB,CAC7B,SACA,kBACA,kBACG;AACH,MAAI,eAAe;AACjB,qBAAiB,QAAQ,CAAC,CAAC,OAAO,UAAU,MAAM;AA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AAI,yBAAyB,aAAa;AACxC;AAAA,IACF;AAEA,YAAA;AAAA,EACF;AAEA,aAAW,QAAQ,CAACA,cAAa;AAC/B,QAAI,SAAS;AACXC,WAAAA,iBAAiB,SAASD,WAAU,MAAM,MAAMA,SAAQ,CAAC;AACzDE,WAAAA,mBAAmB,SAASF,WAAU,MAAM,KAAKA,SAAQ,CAAC;AAAA,IAC5D,OAAO;AACLC,WAAAA,iBAAiBD,WAAU,MAAM,MAAMA,SAAQ,CAAC;AAChDE,WAAAA,mBAAmBF,WAAU,MAAM,KAAKA,SAAQ,CAAC;AAAA,IACnD;AAAA,EACF,CAAC;AAED,SAAO;AACT;;;;;;;;;"}
|
package/mobx.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { AnyObject, Maybe } from 'yummies/types';
|
|
2
|
+
import { IAtom, IEqualsComparer, AnnotationMapEntry } from 'mobx';
|
|
3
|
+
|
|
4
|
+
type ObservableAnnotationsArray = [string, any][];
|
|
5
|
+
declare const applyObservable: (context: AnyObject, annotationsArray: ObservableAnnotationsArray, useDecorators?: boolean) => void;
|
|
6
|
+
|
|
7
|
+
interface IEnhancedAtom<TMeta extends AnyObject = AnyObject> extends IAtom {
|
|
8
|
+
meta: TMeta;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Creates an enhanced atom with meta data
|
|
12
|
+
* And bind `reportChanged` and `reportObserved` method to the atom
|
|
13
|
+
*/
|
|
14
|
+
declare const createEnhancedAtom: <TMeta extends AnyObject>(name: string, onBecomeObservedHandler?: (atom: IEnhancedAtom<TMeta>) => void, onBecomeUnobservedHandler?: (atom: IEnhancedAtom<TMeta>) => void, meta?: TMeta) => IEnhancedAtom<TMeta>;
|
|
15
|
+
|
|
16
|
+
type RefChangeListener<T> = (value: T | null) => void;
|
|
17
|
+
/**
|
|
18
|
+
* Alternative to React.createRef but works in MobX world.
|
|
19
|
+
* Typically it the should be the same React.LegacyRef (fn style)
|
|
20
|
+
*/
|
|
21
|
+
interface Ref<T = any, TMeta = AnyObject> {
|
|
22
|
+
(element: Maybe<T>): void;
|
|
23
|
+
listeners: Set<RefChangeListener<NoInfer<T>>>;
|
|
24
|
+
current: NoInfer<T> | null;
|
|
25
|
+
meta: TMeta;
|
|
26
|
+
}
|
|
27
|
+
interface CreateRefConfig<T = any, TMeta = AnyObject> {
|
|
28
|
+
onSet?: (node: T) => void;
|
|
29
|
+
onUnset?: () => void;
|
|
30
|
+
onChange?: RefChangeListener<T>;
|
|
31
|
+
meta?: TMeta;
|
|
32
|
+
initial?: Maybe<T>;
|
|
33
|
+
comparer?: IEqualsComparer<T | null>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Creates ref thing to attach HTMLElements in React and all other
|
|
37
|
+
*/
|
|
38
|
+
declare const createRef: <T = any, TMeta = AnyObject>(cfg?: CreateRefConfig<T, TMeta>) => Ref<T, TMeta>;
|
|
39
|
+
declare const isRef: <T, TMeta = any>(value: T | Ref<T, TMeta>) => value is Ref<T, TMeta>;
|
|
40
|
+
declare const toRef: <T, TMeta = any>(value: T | Ref<T, TMeta>, cfg?: Omit<CreateRefConfig<T, TMeta>, "initial">) => Ref<T, TMeta>;
|
|
41
|
+
|
|
42
|
+
declare class DeepObservableStruct<TData extends AnyObject> {
|
|
43
|
+
data: TData;
|
|
44
|
+
constructor(data: TData);
|
|
45
|
+
set(newData: Partial<TData>): void;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
type ObservableObjectAdministration = Parameters<Exclude<AnnotationMapEntry, boolean>['make_']>[0];
|
|
49
|
+
declare const getMobxAdministration: (context: AnyObject) => ObservableObjectAdministration;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* When ONE OF the properties is becomes observed then `onStart` function is called.
|
|
53
|
+
* WHen ALL properties are unobserved then `onEnd` function is called with the `metaData` that was returned by `onStart`.
|
|
54
|
+
*
|
|
55
|
+
* It uses `onBecomeObserved` and `onBecomeUnobserved` mobx hooks to perform lazy observation.
|
|
56
|
+
*/
|
|
57
|
+
declare const lazyObserve: <TMetaData = void>({ context, property, onStart, onEnd, endDelay, }: {
|
|
58
|
+
context?: any;
|
|
59
|
+
property: any | any[];
|
|
60
|
+
onStart?: () => TMetaData;
|
|
61
|
+
onEnd?: (metaData: TMetaData, cleanupFn: VoidFunction) => void;
|
|
62
|
+
endDelay?: number | false;
|
|
63
|
+
}) => () => void;
|
|
64
|
+
|
|
65
|
+
export { DeepObservableStruct, applyObservable, createEnhancedAtom, createRef, getMobxAdministration, isRef, lazyObserve, toRef };
|
|
66
|
+
export type { CreateRefConfig, IEnhancedAtom, ObservableAnnotationsArray, Ref, RefChangeListener };
|
package/mobx.js
CHANGED
|
@@ -23,21 +23,21 @@ const createEnhancedAtom = (name, onBecomeObservedHandler, onBecomeUnobservedHan
|
|
|
23
23
|
};
|
|
24
24
|
const createRef = (cfg) => {
|
|
25
25
|
const comparer$1 = cfg?.comparer ?? comparer.default;
|
|
26
|
-
const
|
|
26
|
+
const ref = (value) => {
|
|
27
27
|
const nextValue = value ?? null;
|
|
28
|
-
if (comparer$1(
|
|
28
|
+
if (comparer$1(ref.current, nextValue)) {
|
|
29
29
|
return;
|
|
30
30
|
}
|
|
31
31
|
runInAction(() => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
listener(
|
|
32
|
+
ref.current = nextValue;
|
|
33
|
+
ref.listeners.forEach((listener) => {
|
|
34
|
+
listener(ref.current);
|
|
35
35
|
});
|
|
36
36
|
});
|
|
37
37
|
};
|
|
38
|
-
|
|
38
|
+
ref.listeners = new Set(cfg?.onChange ? [cfg.onChange] : []);
|
|
39
39
|
if (cfg?.onSet || cfg?.onUnset) {
|
|
40
|
-
|
|
40
|
+
ref.listeners.add((value) => {
|
|
41
41
|
if (value) {
|
|
42
42
|
cfg.onSet?.(value);
|
|
43
43
|
} else {
|
|
@@ -45,17 +45,20 @@ const createRef = (cfg) => {
|
|
|
45
45
|
}
|
|
46
46
|
});
|
|
47
47
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
makeObservable(
|
|
48
|
+
ref.current = cfg?.initial ?? null;
|
|
49
|
+
ref.meta = cfg?.meta ?? {};
|
|
50
|
+
makeObservable(ref, {
|
|
51
51
|
current: observable.ref,
|
|
52
52
|
meta: observable
|
|
53
53
|
});
|
|
54
|
-
return
|
|
54
|
+
return ref;
|
|
55
55
|
};
|
|
56
56
|
const isRef = (value) => {
|
|
57
57
|
return typeof value === "function" && "current" in value;
|
|
58
58
|
};
|
|
59
|
+
const toRef = (value, cfg) => {
|
|
60
|
+
return isRef(value) ? value : createRef({ initial: value, ...cfg });
|
|
61
|
+
};
|
|
59
62
|
class DeepObservableStruct {
|
|
60
63
|
data;
|
|
61
64
|
constructor(data) {
|
|
@@ -176,6 +179,7 @@ export {
|
|
|
176
179
|
createRef,
|
|
177
180
|
getMobxAdministration,
|
|
178
181
|
isRef,
|
|
179
|
-
lazyObserve
|
|
182
|
+
lazyObserve,
|
|
183
|
+
toRef
|
|
180
184
|
};
|
|
181
185
|
//# sourceMappingURL=mobx.js.map
|