misaki-studio-internal 0.1.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.
@@ -0,0 +1,251 @@
1
+ import { useEffect, useMemo, useRef } from "react";
2
+ import { deepEqual } from "fast-equals";
3
+ let count = 0;
4
+
5
+ const getCount = () => {
6
+ return useMemo(() => `C${++count}`, []);
7
+ };
8
+
9
+ const useDidUpdateEffect = (props: {
10
+ values: any[];
11
+ option: {
12
+ debounce: boolean;
13
+ delay: number;
14
+ runOnInit: boolean;
15
+ };
16
+ on: () => void;
17
+ }) => {
18
+ const isMountingRef = useRef(false);
19
+
20
+ useEffect(() => {
21
+ isMountingRef.current = true;
22
+ }, []);
23
+
24
+ useEffect(() => {
25
+ if (!isMountingRef.current || props.option.runOnInit) {
26
+ return props.on();
27
+ } else {
28
+ isMountingRef.current = false;
29
+ }
30
+ }, props.values);
31
+ };
32
+
33
+ const loopOnRange = <T,>(
34
+ from: number,
35
+ to: number,
36
+ fn: (index: number) => T,
37
+ ) => {
38
+ const list: T[] = [];
39
+ const length = to < from ? to - from : from - to;
40
+ list.length = length;
41
+ for (let index = 0; index < length; index++) {
42
+ list[index] = fn(index);
43
+ }
44
+ return list;
45
+ };
46
+
47
+ const loopOnString = <T,>(
48
+ str: string,
49
+ fn: (character: string, index: number) => T,
50
+ ) => {
51
+ const list: T[] = [];
52
+ const length = str.length;
53
+ list.length = length;
54
+ for (let index = 0; index < length; index++) {
55
+ list[index] = fn(str[index], index);
56
+ }
57
+ return list;
58
+ };
59
+
60
+ const loopOnObject = <T,>(
61
+ object: any,
62
+ fn: (key: string, value: any, index: number) => T,
63
+ ) => {
64
+ const list: T[] = [];
65
+ const entries = Object.entries(object);
66
+ const length = entries.length;
67
+ list.length = length;
68
+ for (let index = 0; index < length; index++) {
69
+ list[index] = fn(entries[index][0], entries[index][1], index);
70
+ }
71
+ return list;
72
+ };
73
+
74
+ const loopOnArray = <T, A>(
75
+ array: A[],
76
+ fn: (value: A, index: number, isLastStep: boolean) => T,
77
+ ) => {
78
+ const list: T[] = [];
79
+ const length = array.length;
80
+ list.length = length;
81
+ for (let index = 0; index < length; index++) {
82
+ list[index] = fn(array[index], index, length === index + 1);
83
+ }
84
+ return list;
85
+ };
86
+
87
+ const toText = (value: string | number | boolean | null | undefined) => {
88
+ if (typeof value === "string") {
89
+ return value;
90
+ } else if (typeof value === "number") {
91
+ return value.toString();
92
+ } else if (typeof value === "boolean") {
93
+ return value.toString();
94
+ }
95
+ return undefined;
96
+ };
97
+
98
+ const toStyle = (value?: {
99
+ width?: { value: undefined; type: string };
100
+ height?: { value: undefined; type: string };
101
+ fill?: string;
102
+ image?: string;
103
+ }) => {
104
+ return {
105
+ width: value?.width ? value.width.value + value.width.type : undefined,
106
+ height: value?.height ? value.height.value + value.height.type : undefined,
107
+ backgroundColor: value?.fill,
108
+ backgroundImage: value?.image,
109
+ };
110
+ };
111
+
112
+ export enum ScrollElementConfig {
113
+ TOP = 1,
114
+ LEFT = 2,
115
+ RIGHT = 3,
116
+ BOTTOM = 4,
117
+ }
118
+
119
+ export enum ScrollElementOrator {
120
+ PLUS = 1,
121
+ MINUS = 2,
122
+ }
123
+
124
+ const scrollTo = (param: {
125
+ container?: string;
126
+ target?: string;
127
+ config?: ScrollElementConfig;
128
+ operator?: ScrollElementOrator;
129
+ value?: number;
130
+ }) => {
131
+ const container = param.container
132
+ ? document.querySelector(param.container)
133
+ : document.querySelector("#__next");
134
+ const value = param.value || 0;
135
+ let targetElement = param.target
136
+ ? document.querySelector(param.target)
137
+ : undefined;
138
+ if (container) {
139
+ let targetValue = 0;
140
+ const containerRect = container.getBoundingClientRect();
141
+ if (targetElement) {
142
+ const targetRect = targetElement.getBoundingClientRect();
143
+ if (param.config === ScrollElementConfig.TOP) {
144
+ targetValue = Number(
145
+ container.scrollTop + (targetRect.top - containerRect.top),
146
+ );
147
+ } else if (param.config === ScrollElementConfig.BOTTOM) {
148
+ targetValue = Number(
149
+ container.scrollTop + (targetRect.bottom - containerRect.top),
150
+ );
151
+ } else if (param.config === ScrollElementConfig.LEFT) {
152
+ targetValue = Number(
153
+ container.scrollLeft + (targetRect.left - containerRect.left),
154
+ );
155
+ } else if (param.config === ScrollElementConfig.RIGHT) {
156
+ targetValue = Number(
157
+ container.scrollLeft + (targetRect.right - containerRect.left),
158
+ );
159
+ }
160
+ if (param.operator === ScrollElementOrator.PLUS) {
161
+ targetValue += value;
162
+ } else if (param.operator === ScrollElementOrator.MINUS) {
163
+ targetValue -= value;
164
+ }
165
+ } else {
166
+ if (param.config === ScrollElementConfig.TOP) {
167
+ targetValue = 0;
168
+ } else if (param.config === ScrollElementConfig.BOTTOM) {
169
+ targetValue = -container.scrollHeight;
170
+ } else if (param.config === ScrollElementConfig.LEFT) {
171
+ targetValue = 0;
172
+ } else if (param.config === ScrollElementConfig.RIGHT) {
173
+ targetValue = -container.scrollWidth;
174
+ }
175
+ if (param.operator === ScrollElementOrator.PLUS) {
176
+ targetValue += value;
177
+ } else if (param.operator === ScrollElementOrator.MINUS) {
178
+ targetValue -= value;
179
+ }
180
+ }
181
+
182
+ if (
183
+ param.config === ScrollElementConfig.TOP ||
184
+ param.config === ScrollElementConfig.BOTTOM
185
+ ) {
186
+ container.scrollTo({
187
+ behavior: "instant",
188
+ top: targetValue,
189
+ });
190
+ } else {
191
+ container.scrollTo({
192
+ behavior: "instant",
193
+ left: targetValue,
194
+ });
195
+ }
196
+ }
197
+ };
198
+
199
+ const prepareMouseEvent = (
200
+ event: React.MouseEvent<HTMLElement, MouseEvent>,
201
+ ) => {
202
+ return {
203
+ text: ((event.currentTarget as any).value || "") as string,
204
+ element: event.currentTarget,
205
+ };
206
+ };
207
+ const prepareInputChangeEvent = (
208
+ event: React.ChangeEvent<HTMLInputElement>,
209
+ ) => {
210
+ return {
211
+ text: ((event.currentTarget as any).value || "") as string,
212
+ element: event.currentTarget,
213
+ };
214
+ };
215
+ const prepareKeyboardEvent = (event: React.KeyboardEvent<HTMLDivElement>) => {
216
+ return {
217
+ element: event.currentTarget,
218
+ };
219
+ };
220
+ const prepareFocusEvent = (
221
+ event: React.FocusEvent<HTMLDivElement, Element>,
222
+ ) => {
223
+ return {
224
+ element: event.currentTarget,
225
+ };
226
+ };
227
+
228
+ const prepareUIEvent = (event: React.UIEvent<HTMLDivElement, UIEvent>) => {
229
+ return {
230
+ element: event.currentTarget,
231
+ };
232
+ };
233
+
234
+ export default {
235
+ ScrollElementOrator,
236
+ ScrollElementConfig,
237
+ loopOnRange,
238
+ loopOnString,
239
+ loopOnObject,
240
+ loopOnArray,
241
+ toText,
242
+ toStyle,
243
+ useDidUpdateEffect,
244
+ prepareMouseEvent,
245
+ prepareInputChangeEvent,
246
+ prepareKeyboardEvent,
247
+ prepareFocusEvent,
248
+ prepareUIEvent,
249
+ getCount,
250
+ scrollTo,
251
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "outDir": "./dist",
4
+ "target": "es5",
5
+ "module": "esnext",
6
+ "jsx": "react-jsx",
7
+ "declaration": true,
8
+ "strict": true,
9
+ "moduleResolution": "node",
10
+ "esModuleInterop": true,
11
+ "forceConsistentCasingInFileNames": true,
12
+ "lib": ["dom", "esnext"],
13
+ },
14
+ "include": ["src/**/*.ts", "src/**/*.tsx", "src/styles.d.ts"],
15
+ "exclude": ["node_modules", "dist"],
16
+ }
@@ -0,0 +1,39 @@
1
+ const path = require("path");
2
+
3
+ module.exports = {
4
+ mode: "production",
5
+ entry: "./src/index.ts", // or .tsx if your main entry is a React component
6
+ output: {
7
+ filename: "index.js",
8
+ path: path.resolve(__dirname, "dist"),
9
+ libraryTarget: "umd",
10
+ globalObject: "this",
11
+ },
12
+ resolve: {
13
+ extensions: [".ts", ".tsx", ".js", ".jsx", ".json"],
14
+ },
15
+ module: {
16
+ rules: [
17
+ {
18
+ test: /\.(ts|tsx)$/,
19
+ exclude: /node_modules/,
20
+ use: "ts-loader",
21
+ },
22
+ {
23
+ test: /\.(module.scss)$/, // Matches .scss and .sass files
24
+ use: [
25
+ "style-loader",
26
+ "css-loader",
27
+ {
28
+ loader: "sass-loader",
29
+ },
30
+ ],
31
+ },
32
+ ],
33
+ },
34
+ externals: {
35
+ react: "react",
36
+ "react-dom": "react-dom",
37
+ "next/router": "next/router",
38
+ },
39
+ };