@opkle/ui-core 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,286 @@
1
+ type StyleValue =
2
+ | string
3
+ | number
4
+ | null
5
+ | undefined
6
+ | [string | number, string | number];
7
+
8
+ type DomStyle = Partial<CSSStyleDeclaration> & Record<string, StyleValue>;
9
+
10
+ type DomEventHandler = (event: any) => any;
11
+ type DomEventMap = Record<string, DomEventHandler>;
12
+
13
+ type DomCommand = {
14
+ mother?: HTMLElement | Element | SVGElement;
15
+ style?: DomStyle;
16
+ mode?: string;
17
+ source?: string;
18
+ text?: string | string[];
19
+ class?: string[];
20
+ id?: string;
21
+ attribute?: Record<string, string>;
22
+ event?: DomEventMap;
23
+ child?: DomCommand;
24
+ children?: DomCommand[];
25
+ bold?: DomStyle;
26
+ strike?: DomStyle;
27
+ under?: DomStyle;
28
+ special?: DomStyle;
29
+ code?: DomStyle;
30
+ italic?: DomStyle;
31
+ reference?: DomStyle;
32
+ anchor?: DomStyle;
33
+ next?: DomCommand;
34
+ previous?: DomCommand;
35
+ pass?: boolean;
36
+ unshift?: boolean;
37
+ position?: number;
38
+ }
39
+
40
+ const mimeTypes: Record<string, string> = {
41
+ aac: "audio/aac",
42
+ abw: "application/x-abiword",
43
+ arc: "application/x-freearc",
44
+ mkv: "video/x-matroska",
45
+ avi: "video/x-msvideo",
46
+ flac: "audio/flac",
47
+ azw: "application/vnd.amazon.ebook",
48
+ bin: "application/octet-stream",
49
+ bz: "application/x-bzip",
50
+ bz2: "application/x-bzip2",
51
+ csh: "application/x-csh",
52
+ css: "text/css",
53
+ csv: "text/csv",
54
+ doc: "application/msword",
55
+ epub: "application/epub+zip",
56
+ gif: "image/gif",
57
+ html: "text/html",
58
+ htm: "text/html",
59
+ ts: "video/mp2t",
60
+ ico: "image/vnd.microsoft.icon",
61
+ ics: "text/calendar",
62
+ jar: "application/java-archive",
63
+ jpg: "image/jpeg",
64
+ jpeg: "image/jpeg",
65
+ mjs: "text/javascript",
66
+ js: "text/javascript",
67
+ json: "application/json",
68
+ mid: "audio/midi",
69
+ midi: "audio/midi",
70
+ mpeg: "video/mpeg",
71
+ mpkg: "application/vnd.apple.installer+xml",
72
+ odp: "application/vnd.oasis.opendocument.presentation",
73
+ ods: "application/vnd.oasis.opendocument.spreadsheet",
74
+ odt: "application/vnd.oasis.opendocument.text",
75
+ oga: "audio/ogg",
76
+ ogv: "video/ogg",
77
+ ogx: "application/ogg",
78
+ pdf: "application/pdf",
79
+ ppt: "application/vnd.ms-powerpoint",
80
+ rar: "application/vnd.rar",
81
+ rtf: "application/rtf",
82
+ sh: "application/x-sh",
83
+ svg: "image/svg+xml",
84
+ swf: "application/x-shockwave-flash",
85
+ tar: "application/x-tar",
86
+ tif: "image/tiff",
87
+ tiff: "image/tiff",
88
+ ttf: "font/ttf",
89
+ vsd: "application/vnd.visio",
90
+ wav: "audio/wav",
91
+ weba: "audio/webm",
92
+ webm: "video/webm",
93
+ webp: "image/webp",
94
+ woff: "font/woff",
95
+ xhtml: "application/xhtml+xml",
96
+ xls: "application/vnd.ms-excel",
97
+ xml: "application/xml",
98
+ xul: "application/vnd.mozilla.xul+xml",
99
+ zip: "application/zip",
100
+ "3gp": "video/3gpp",
101
+ "3g2": "video/3gpp2",
102
+ "7z": "application/x-7z-compressed",
103
+ "opf": "application/oebps-package+xml",
104
+ "txt": "text/plain",
105
+ "png": "image/png",
106
+ "apng": "image/apng",
107
+ "jfif": "image/jpeg",
108
+ "pjpeg": "image/jpeg",
109
+ "psd": "image/vnd.adobe.photoshop",
110
+ "pjp": "image/jpeg",
111
+ "cur": "image/x-icon",
112
+ "bmp": "image/bmp",
113
+ "avif": "image/avif",
114
+ "woff2": "font/woff2",
115
+ "otf": "font/otf",
116
+ "eot": "application/vnd.ms-fontobject",
117
+ "gz": "application/gzip",
118
+ "mp3": "audio/mpeg",
119
+ "ogg": "audio/ogg",
120
+ "mp4": "video/mp4",
121
+ "md": "text/markdown",
122
+ "heic": "image/heic",
123
+ "heif": "image/heif",
124
+ "opus": "audio/opus",
125
+ "jsonld": "application/ld+json",
126
+ "php": "application/x-httpd-php",
127
+ "docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
128
+ "pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
129
+ "xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
130
+ "hwp": "application/x-hwp",
131
+ "hwpx": "application/vnd.hancom.hwpx",
132
+ "indd": "application/x-indesign",
133
+ "ncx": "application/x-dtbncx+xml",
134
+ };
135
+
136
+ class SvgTongListClass {
137
+ public list: string[];
138
+ constructor() {
139
+ this.list = [];
140
+ }
141
+ add = (str: string): void => {
142
+ this.list.push(str);
143
+ }
144
+ }
145
+
146
+ class SvgTongHtmlParsingClass {
147
+ public attribute: Record<string, string>;
148
+ public src: string;
149
+ public id: string;
150
+ public style: Record<string, string>;
151
+ public events: Array<{ name: string; callback: (event: Event) => void }>;
152
+ public classList: SvgTongListClass;
153
+
154
+ constructor() {
155
+ this.attribute = {};
156
+ this.src = "";
157
+ this.id = "";
158
+ this.style = {};
159
+ this.events = [];
160
+ this.classList = new SvgTongListClass();
161
+ }
162
+
163
+ public setAttribute = (key: string, value: string): void => {
164
+ this.attribute[ key ] = value;
165
+ }
166
+
167
+ public getAttribute = (key: string): string => {
168
+ return this.attribute[ key ];
169
+ }
170
+
171
+ public addEventListener = (eventName: string, callback: (event: Event) => void): void => {
172
+ const obj: { name: string; callback: (event: Event) => void } = {
173
+ name: eventName,
174
+ callback: callback,
175
+ };
176
+ this.events.push(obj);
177
+ }
178
+ }
179
+
180
+ class SvgDom {
181
+
182
+ public source: SVGElement;
183
+ public classList: SvgDomClassList;
184
+ public stringSource: string;
185
+
186
+ constructor(str: string) {
187
+ const resultDom = new DOMParser().parseFromString(str, "image/svg+xml");
188
+ const children: HTMLCollection = resultDom.children;
189
+ const target = children[ 0 ] as SVGElement;
190
+ this.source = target;
191
+ this.classList = new SvgDomClassList(target);
192
+ this.stringSource = str;
193
+ }
194
+
195
+ public getAttribute(key: string) {
196
+ return this.source.getAttribute(key);
197
+ }
198
+
199
+ public setAttribute(key: string, value: string) {
200
+ this.source.setAttribute(key, value);
201
+ }
202
+
203
+ public setId(id: string) {
204
+ this.source.id = id;
205
+ }
206
+
207
+ public getRatio(): number {
208
+ let viewBoxString: string;
209
+ let viewBoxArr: Array<string>;
210
+
211
+ viewBoxString = String(this.source.getAttribute("viewBox"));
212
+ if (viewBoxString === "null") {
213
+ return -1;
214
+ }
215
+ viewBoxArr = viewBoxString.split(' ');
216
+
217
+ return (Number(viewBoxArr[ 2 ]) / Number(viewBoxArr[ 3 ]));
218
+ }
219
+
220
+ public setStyle(key: string, value: string) {
221
+ let thisStyleKey: any;
222
+ thisStyleKey = key;
223
+ this.source.style[ thisStyleKey ] = value;
224
+ }
225
+
226
+ public addEventListener(eventName: string, eventFunction: (e: any) => any) {
227
+ this.source.addEventListener(eventName, eventFunction);
228
+ }
229
+
230
+ }
231
+
232
+ class SvgDomClassList extends Array {
233
+
234
+ public source: SVGElement;
235
+
236
+ constructor(source: SVGElement) {
237
+ super();
238
+ this.source = source;
239
+ }
240
+
241
+ add(str: string) {
242
+ this.push(str);
243
+ this.source.classList.add(str);
244
+ }
245
+
246
+ }
247
+
248
+ class SvgTong {
249
+ constructor() { }
250
+
251
+ public static tongMaker(): SvgTongHtmlParsingClass {
252
+ let obj: SvgTongHtmlParsingClass = new SvgTongHtmlParsingClass();
253
+ return obj;
254
+ }
255
+
256
+ public static stringParsing(str: string): SvgDom {
257
+ return new SvgDom(str);
258
+ }
259
+
260
+ public static getRatio(svgString: string): number {
261
+ let svgDom: SvgDom;
262
+ let viewBoxString: string;
263
+ let viewBoxArr: Array<string>;
264
+
265
+ svgDom = SvgTong.stringParsing(svgString);
266
+
267
+ viewBoxString = String(svgDom.getAttribute("viewBox"));
268
+ if (viewBoxString === "null") {
269
+ return -1;
270
+ }
271
+ viewBoxArr = viewBoxString.split(' ');
272
+
273
+ return (Number(viewBoxArr[ 2 ]) / Number(viewBoxArr[ 3 ]));
274
+ }
275
+
276
+ }
277
+
278
+ export {
279
+ DomStyle,
280
+ DomEventHandler,
281
+ DomEventMap,
282
+ DomCommand,
283
+ mimeTypes,
284
+ SvgTong,
285
+ SvgDom
286
+ };