@orago/dodo 0.0.1

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.
Files changed (78) hide show
  1. package/.editorconfig +12 -0
  2. package/.eslintrc.cjs +23 -0
  3. package/.gitattributes +2 -0
  4. package/dist/_virtual/_rolldown/runtime.cjs +1 -0
  5. package/dist/_virtual/_rolldown/runtime.mjs +1 -0
  6. package/dist/dodo-events.cjs +2 -0
  7. package/dist/dodo-events.cjs.map +1 -0
  8. package/dist/dodo-events.d.ts +44 -0
  9. package/dist/dodo-events.mjs +2 -0
  10. package/dist/dodo-events.mjs.map +1 -0
  11. package/dist/dodo.cjs +2 -0
  12. package/dist/dodo.cjs.map +1 -0
  13. package/dist/dodo.d.ts +304 -0
  14. package/dist/dodo.mjs +2 -0
  15. package/dist/dodo.mjs.map +1 -0
  16. package/dist/index.cjs +1 -0
  17. package/dist/index.d.ts +3 -0
  18. package/dist/index.mjs +1 -0
  19. package/dist/plugins/index.cjs +1 -0
  20. package/dist/plugins/index.d.ts +2 -0
  21. package/dist/plugins/index.mjs +1 -0
  22. package/dist/plugins/plugin-tracking.cjs +2 -0
  23. package/dist/plugins/plugin-tracking.cjs.map +1 -0
  24. package/dist/plugins/plugin-tracking.d.ts +40 -0
  25. package/dist/plugins/plugin-tracking.mjs +2 -0
  26. package/dist/plugins/plugin-tracking.mjs.map +1 -0
  27. package/dist/server/copydir.cjs +2 -0
  28. package/dist/server/copydir.cjs.map +1 -0
  29. package/dist/server/copydir.d.ts +12 -0
  30. package/dist/server/copydir.mjs +2 -0
  31. package/dist/server/copydir.mjs.map +1 -0
  32. package/dist/server/index.cjs +1 -0
  33. package/dist/server/index.d.ts +3 -0
  34. package/dist/server/index.mjs +1 -0
  35. package/dist/server/util.cjs +2 -0
  36. package/dist/server/util.cjs.map +1 -0
  37. package/dist/server/util.d.ts +31 -0
  38. package/dist/server/util.mjs +2 -0
  39. package/dist/server/util.mjs.map +1 -0
  40. package/dist/util/class.util.cjs +2 -0
  41. package/dist/util/class.util.cjs.map +1 -0
  42. package/dist/util/class.util.mjs +2 -0
  43. package/dist/util/class.util.mjs.map +1 -0
  44. package/dist/util/plugins-support.cjs +2 -0
  45. package/dist/util/plugins-support.cjs.map +1 -0
  46. package/dist/util/plugins-support.d.ts +19 -0
  47. package/dist/util/plugins-support.mjs +2 -0
  48. package/dist/util/plugins-support.mjs.map +1 -0
  49. package/dist/util/submap.cjs +2 -0
  50. package/dist/util/submap.cjs.map +1 -0
  51. package/dist/util/submap.d.ts +14 -0
  52. package/dist/util/submap.mjs +2 -0
  53. package/dist/util/submap.mjs.map +1 -0
  54. package/dist/util/utilities.cjs +2 -0
  55. package/dist/util/utilities.cjs.map +1 -0
  56. package/dist/util/utilities.d.ts +14 -0
  57. package/dist/util/utilities.mjs +2 -0
  58. package/dist/util/utilities.mjs.map +1 -0
  59. package/package.json +61 -0
  60. package/pnpm-workspace.yaml +4 -0
  61. package/readme.md +0 -0
  62. package/src/dodo-events.ts +245 -0
  63. package/src/dodo.ts +384 -0
  64. package/src/index.ts +2 -0
  65. package/src/plugins/index.ts +5 -0
  66. package/src/plugins/plugin-schema.ts +58 -0
  67. package/src/plugins/plugin-style.ts +11 -0
  68. package/src/plugins/plugin-tracking.ts +227 -0
  69. package/src/server/copydir.ts +119 -0
  70. package/src/server/index.ts +7 -0
  71. package/src/server/util.ts +82 -0
  72. package/src/util/class.util.ts +42 -0
  73. package/src/util/jsx-runtime.ts +53 -0
  74. package/src/util/plugins-support.ts +59 -0
  75. package/src/util/submap.ts +60 -0
  76. package/src/util/utilities.ts +51 -0
  77. package/tsconfig.json +31 -0
  78. package/tsdown.config.ts +22 -0
@@ -0,0 +1,227 @@
1
+ import { Dodo } from "../dodo.js";
2
+ import { DodoEvents } from "../dodo-events.js";
3
+ import { DodoGlobalPlugin } from "../util/plugins-support.js";
4
+
5
+ function getAllRemovedNodes(node: Node): Node[] {
6
+ const nodes: Node[] = [node];
7
+ node.childNodes.forEach((child) => {
8
+ nodes.push(...getAllRemovedNodes(child));
9
+ });
10
+ return nodes;
11
+ }
12
+
13
+ export class DodoRef {
14
+ public static flag(name: string) {
15
+ return `__dodo_${name}`;
16
+ }
17
+ public static ref_prop = "__vnode";
18
+
19
+ public static bindRef(instance: Dodo, query_flag?: string) {
20
+ (instance.element as any)[this.ref_prop] = new WeakRef(instance);
21
+
22
+ if (query_flag != undefined) {
23
+ instance.attr({ [DodoRef.flag(query_flag)]: "" });
24
+ }
25
+ }
26
+
27
+ public static queryFlag(flag: string, alias: boolean = true): Dodo[] {
28
+ if (alias == true) flag = DodoRef.flag(flag);
29
+
30
+ const found = document.querySelectorAll(`[${flag}]`);
31
+
32
+ return Array.from(found)
33
+ .map((e: any) => e?.[this.ref_prop]?.deref())
34
+ .filter((e) => e instanceof Dodo);
35
+ }
36
+
37
+ public static filterQuery(list: any[]): Dodo[] {
38
+ return list
39
+ .map((e: any) => e?.[this.ref_prop]?.deref())
40
+ .filter((e) => e instanceof Dodo);
41
+ }
42
+ }
43
+
44
+ class StateObserver {
45
+ private inDom(element: HTMLElement) {
46
+ return this.tracked_in_dom.get(element) == true;
47
+ }
48
+ private tracked_in_dom: Map<HTMLElement, boolean> = new Map();
49
+ observer: MutationObserver;
50
+
51
+ constructor() {
52
+ this.observer = new MutationObserver((mutations: MutationRecord[]) => {
53
+ const queried = DodoRef.queryFlag(StateTrackingPlugin.plugin_id);
54
+ const blah: HTMLElement[] = [];
55
+
56
+ for (const mutation of mutations) {
57
+ let tmp: HTMLElement[] = [];
58
+ for (const removed of Array.from(mutation.removedNodes)) {
59
+ tmp.push(...(getAllRemovedNodes(removed) as any));
60
+ blah.push(removed as HTMLElement);
61
+ }
62
+ const removed_query = DodoRef.filterQuery(tmp);
63
+ queried.push(...removed_query);
64
+ }
65
+
66
+ for (const node of queried) {
67
+ const element = node.element;
68
+ blah.push(element);
69
+ }
70
+
71
+ for (const element of blah) {
72
+ if (document.body.contains(element)) {
73
+ if (this.inDom(element) != true) {
74
+ DodoEvents.emitTo(element, "connected");
75
+ }
76
+
77
+ this.tracked_in_dom.set(element, true);
78
+ } else if (this.inDom(element)) {
79
+ /* Was in dom but removed */
80
+ DodoEvents.emitTo(element, "disconnected");
81
+ this.tracked_in_dom.set(element, false);
82
+ }
83
+ }
84
+ });
85
+
86
+ this.observer.observe(document.body, {
87
+ childList: true,
88
+ subtree: true,
89
+ });
90
+ }
91
+ }
92
+
93
+ export class StateTrackingPlugin {
94
+ public static plugin_id = "state-tracking";
95
+
96
+ public static initNodeTracking(node: Dodo) {
97
+ DodoRef.bindRef(node, this.plugin_id);
98
+ }
99
+
100
+ public static init(options?: { all?: boolean }) {
101
+ const init_cb = (node: Dodo) => this.initNodeTracking(node);
102
+
103
+ if (options?.all == true) {
104
+ Dodo.events.on("init", init_cb);
105
+ }
106
+
107
+ const observer = new StateObserver();
108
+
109
+ return {
110
+ observer,
111
+ destroy() {
112
+ if (options?.all == true) {
113
+ Dodo.events.off("init", init_cb);
114
+ }
115
+ observer.observer.disconnect();
116
+ },
117
+ };
118
+ }
119
+
120
+ public static plugin(): DodoGlobalPlugin {
121
+ let ref: ReturnType<typeof StateTrackingPlugin.init> | undefined;
122
+ let clearIfExists = () => {
123
+ if (ref == undefined) return;
124
+ ref.destroy();
125
+ ref = undefined;
126
+ };
127
+
128
+ return {
129
+ id: StateTrackingPlugin.plugin_id,
130
+ add() {
131
+ clearIfExists();
132
+ ref = StateTrackingPlugin.init();
133
+ },
134
+ remove: () => clearIfExists(),
135
+ events: [
136
+ [
137
+ "connected",
138
+ (instance) => {
139
+ StateTrackingPlugin.initNodeTracking(instance);
140
+ console.log("listening connected!", instance);
141
+ },
142
+ ],
143
+ [
144
+ "disconnected",
145
+ (instance) =>
146
+ StateTrackingPlugin.initNodeTracking(instance),
147
+ ],
148
+ ],
149
+ };
150
+ }
151
+ }
152
+
153
+ class SizeObserver {
154
+ observer: ResizeObserver;
155
+
156
+ constructor() {
157
+ this.observer = new ResizeObserver(() => {
158
+ const queried = DodoRef.queryFlag(SizeTrackingPlugin.plugin_id);
159
+
160
+ for (const instance of queried) {
161
+ SizeTrackingPlugin.handleInstance(instance);
162
+ }
163
+ });
164
+
165
+ this.observer.observe(document.body, {});
166
+ }
167
+ }
168
+
169
+ export class SizeTrackingPlugin {
170
+ public static plugin_id = "size-tracking";
171
+
172
+ public static sizes: WeakMap<
173
+ HTMLElement,
174
+ { width: number; height: number }
175
+ > = new WeakMap();
176
+ public static handleInstance(instance: Dodo) {
177
+ const cache_size = SizeTrackingPlugin.sizes.get(instance.element);
178
+ const bounds = instance.getBounds();
179
+ const current_size = {
180
+ width: bounds.width,
181
+ height: bounds.height,
182
+ };
183
+
184
+ if (
185
+ cache_size == undefined ||
186
+ cache_size.width != current_size.width ||
187
+ cache_size.height != current_size.height
188
+ ) {
189
+ SizeTrackingPlugin.sizes.set(instance.element, current_size);
190
+ DodoEvents.emitTo(instance.element, "resize");
191
+ }
192
+ }
193
+
194
+ public static initNodeTracking(node: Dodo) {
195
+ DodoRef.bindRef(node, this.plugin_id);
196
+ const bounds = node.getBounds();
197
+ this.sizes.set(node.element, {
198
+ width: bounds.width,
199
+ height: bounds.height,
200
+ });
201
+ }
202
+
203
+ public static plugin(): DodoGlobalPlugin {
204
+ let ref: SizeObserver | undefined;
205
+ let clearIfExists = () => {
206
+ if (ref == undefined) return;
207
+ ref.observer.disconnect();
208
+ ref = undefined;
209
+ };
210
+
211
+ return {
212
+ id: SizeTrackingPlugin.plugin_id,
213
+ add() {
214
+ clearIfExists();
215
+ ref = new SizeObserver();
216
+ },
217
+ remove: () => clearIfExists(),
218
+
219
+ events: [
220
+ [
221
+ "resize",
222
+ (instance) => SizeTrackingPlugin.initNodeTracking(instance),
223
+ ],
224
+ ],
225
+ };
226
+ }
227
+ }
@@ -0,0 +1,119 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+
4
+ interface CopyDirOptions {
5
+ utimes?: boolean;
6
+ mode?: boolean; // keep mode if true
7
+ cover?: boolean; // cover if file exists
8
+ filter?: boolean | Function; // file filter
9
+ debug?: boolean;
10
+ }
11
+
12
+ function copyDirSync(from: string, to: string, options: CopyDirOptions) {
13
+ if (typeof options === "function") {
14
+ options = {
15
+ filter: options,
16
+ };
17
+ }
18
+ if (typeof options === "undefined") options = {};
19
+ if (typeof options.cover === "undefined") {
20
+ options.cover = true;
21
+ }
22
+ options.filter =
23
+ typeof options.filter === "function"
24
+ ? options.filter
25
+ : function (state: any, filepath: any, filename: any) {
26
+ return options.filter;
27
+ };
28
+ let stats = fs.lstatSync(from);
29
+ let statsname = stats.isDirectory()
30
+ ? "directory"
31
+ : stats.isFile()
32
+ ? "file"
33
+ : stats.isSymbolicLink()
34
+ ? "symbolicLink"
35
+ : "";
36
+ let valid = options.filter(statsname, from, path.basename(from));
37
+ if (statsname === "directory" || statsname === "symbolicLink") {
38
+ // Directory or SymbolicLink
39
+ if (valid) {
40
+ try {
41
+ fs.statSync(to);
42
+ } catch (err: any) {
43
+ if (err.code === "ENOENT") {
44
+ fs.mkdirSync(to);
45
+ options.debug && console.log(">> " + to);
46
+ } else {
47
+ throw err;
48
+ }
49
+ }
50
+ rewriteSync(to, options, stats);
51
+ listDirectorySync(from, to, options);
52
+ }
53
+ } else if (stats.isFile()) {
54
+ // File
55
+ if (valid) {
56
+ if (options.cover) {
57
+ writeFileSync(from, to, options, stats);
58
+ } else {
59
+ try {
60
+ fs.statSync(to);
61
+ } catch (err: any) {
62
+ if (err.code === "ENOENT") {
63
+ writeFileSync(from, to, options, stats);
64
+ } else {
65
+ throw err;
66
+ }
67
+ }
68
+ }
69
+ }
70
+ } else {
71
+ throw new Error("stats invalid: " + from);
72
+ }
73
+ }
74
+
75
+ function listDirectorySync(from: string, to: string, options: CopyDirOptions) {
76
+ let files = fs.readdirSync(from);
77
+ copyFromArraySync(files, from, to, options);
78
+ }
79
+
80
+ function copyFromArraySync(
81
+ files: string[],
82
+ from: string,
83
+ to: string,
84
+ options: CopyDirOptions,
85
+ ) {
86
+ if (files.length === 0) return true;
87
+ let f = files.shift()!;
88
+ copyDirSync(path.join(from, f), path.join(to, f), options);
89
+ copyFromArraySync(files, from, to, options);
90
+ }
91
+
92
+ function writeFileSync(
93
+ from: string,
94
+ to: string,
95
+ options: CopyDirOptions,
96
+ stats: any,
97
+ ) {
98
+ fs.writeFileSync(to, fs.readFileSync(from, "binary"), "binary");
99
+ options.debug && console.log(">> " + to);
100
+ rewriteSync(to, options, stats);
101
+ }
102
+
103
+ function rewriteSync(f: fs.PathLike, options: CopyDirOptions, stats: any) {
104
+ if (options.cover) {
105
+ let mode = options.mode === true ? stats.mode : options.mode;
106
+ let utimes =
107
+ options.utimes === true
108
+ ? {
109
+ atime: stats.atime,
110
+ mtime: stats.mtime,
111
+ }
112
+ : options.utimes;
113
+ mode && fs.chmodSync(f, mode);
114
+ utimes && fs.utimesSync(f, utimes.atime, utimes.mtime);
115
+ }
116
+ return true;
117
+ }
118
+
119
+ export { copyDirSync };
@@ -0,0 +1,7 @@
1
+ export { copyDirSync } from "./copydir.js";
2
+ export {
3
+ buildProject,
4
+ includeFolder,
5
+ renderPages,
6
+ type ProjectOptions,
7
+ } from "./util.js";
@@ -0,0 +1,82 @@
1
+ import { KiwiItem } from "@orago/kiwi";
2
+ import { itemToXml } from "@orago/kiwi/plugins";
3
+ import fs from "fs";
4
+ import path from "path";
5
+ import { copyDirSync } from "./copydir.js";
6
+
7
+ export interface ProjectConfig {
8
+ cwd: string;
9
+ root: string;
10
+ dist: string;
11
+ }
12
+
13
+ export interface ProjectOptions {
14
+ pages: [name: string, KiwiItem][];
15
+ include?: [relative_from: string, root_to: string][];
16
+ config: ProjectConfig;
17
+ }
18
+
19
+ export function resolveDist(cwd: string, dist: string) {
20
+ return path.join(cwd, "../", dist);
21
+ }
22
+
23
+ /**
24
+ * init / setup project, currently used for making sure folder exists
25
+ */
26
+ export function init(cwd: string, dist: string) {
27
+ const _dist = resolveDist(cwd, dist);
28
+ if (fs.existsSync(_dist) != true) {
29
+ fs.mkdirSync(_dist, { recursive: true });
30
+ }
31
+ }
32
+
33
+ /**
34
+ * Renders pages as <name.html> to the provided dist folder
35
+ * @param cwd - current working directory
36
+ * @param dist - dist folder path
37
+ * @param pages
38
+ */
39
+ export function renderPages(
40
+ cwd: string,
41
+ dist: string,
42
+ pages: [name: string, KiwiItem][],
43
+ ) {
44
+ for (const [name, data] of pages) {
45
+ fs.writeFileSync(
46
+ path.join(resolveDist(cwd, dist), `./${name}`),
47
+ itemToXml(data),
48
+ "utf8",
49
+ );
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Copy folder contents to dist
55
+ * @param cwd - current working directory
56
+ * @param dist - dist folder path
57
+ * @param relative_from - included path relative to your cwd
58
+ * @param root_to - path relative to dist
59
+ */
60
+ export function includeFolder(
61
+ cwd: string,
62
+ dist: string,
63
+ relative_from: string,
64
+ root_to: string,
65
+ ) {
66
+ copyDirSync(
67
+ path.join(cwd, relative_from),
68
+ path.join(resolveDist(cwd, dist), root_to),
69
+ {},
70
+ );
71
+ }
72
+
73
+ export function buildProject(options: ProjectOptions) {
74
+ const { config } = options;
75
+ const { cwd, dist } = config;
76
+ init(cwd, dist);
77
+ renderPages(cwd, dist, options.pages);
78
+
79
+ for (const [from, to] of options.include ?? []) {
80
+ includeFolder(cwd, dist, from, to);
81
+ }
82
+ }
@@ -0,0 +1,42 @@
1
+ type OverloadedCallable<T> = T extends (...args: any[]) => any
2
+ ? T
3
+ : T extends { (...args: any[]): any }
4
+ ? T
5
+ : never;
6
+
7
+ type CallableInstance<
8
+ TClass extends InstanceType<new (...args: any[]) => any>,
9
+ K extends keyof TClass,
10
+ > = TClass extends { [P in K]: infer F }
11
+ ? OverloadedCallable<F> & TClass
12
+ : never;
13
+
14
+ type CallableInstanceFn<
15
+ TClass extends InstanceType<new (...args: any[]) => any>,
16
+ FN extends (...args: any[]) => any,
17
+ > = OverloadedCallable<FN> & TClass;
18
+
19
+ function withCalling<
20
+ TClass extends new (...args: any[]) => any,
21
+ TInstance extends InstanceType<TClass>,
22
+ FN extends (...args: any[]) => any,
23
+ >(instance: TInstance, callback: FN): CallableInstanceFn<TInstance, FN> {
24
+ const callable = callback as CallableInstanceFn<TInstance, FN>;
25
+ Object.setPrototypeOf(callable, instance.constructor.prototype);
26
+ Object.assign(callable, instance);
27
+ return callable;
28
+ }
29
+
30
+ function makeCallableInstance<
31
+ TClass extends new (...args: any[]) => any,
32
+ K extends keyof InstanceType<TClass>,
33
+ >(
34
+ Class: TClass,
35
+ args: ConstructorParameters<TClass>,
36
+ invoke: K,
37
+ ): CallableInstance<InstanceType<TClass>, K> {
38
+ const instance = new Class(...args);
39
+ return withCalling(instance, instance[invoke].bind(instance));
40
+ }
41
+
42
+ export { makeCallableInstance, withCalling };
@@ -0,0 +1,53 @@
1
+ // polyfill for jsx
2
+ import type { ElementName, KiwiUpdate } from "@orago/kiwi";
3
+ import { ChildList, DodoUtil, oo } from "../dodo.js";
4
+
5
+ export function JsxFragment(...children: ChildList): DocumentFragment {
6
+ const frag = document.createDocumentFragment();
7
+ const items = DodoUtil.flattenElements(children);
8
+ for (const item of items) frag.appendChild(item);
9
+ return frag;
10
+ }
11
+
12
+ declare global {
13
+ namespace JSX {
14
+ export type Element = ReturnType<typeof oo>;
15
+ export type IntrinsicElements = {
16
+ [K in keyof HTMLElementTagNameMap]: ElementName;
17
+ };
18
+ }
19
+ }
20
+
21
+ interface JsxKiwiUpdate {
22
+ id?: KiwiUpdate["id"];
23
+ attributes?: KiwiUpdate["attributes"];
24
+ classes?: KiwiUpdate["classes"];
25
+ styles?: KiwiUpdate["styles"];
26
+ }
27
+
28
+ type JsxProps = (JsxKiwiUpdate & { children?: any[] }) | null | undefined;
29
+
30
+ export function jsx(type: any, props: JsxProps, key: any) {
31
+ if (typeof type === "function") {
32
+ if (props?.children != undefined) {
33
+ props.children = DodoUtil.flattenElements([props.children ?? []]);
34
+ }
35
+ return new type(props);
36
+ }
37
+
38
+ return oo(type, props, DodoUtil.flattenElements([props?.children ?? []]));
39
+ }
40
+
41
+ export const jsxs = jsx;
42
+ export const Fragment = JsxFragment;
43
+
44
+ export const jsxDEV = (
45
+ type: any,
46
+ props: JsxProps,
47
+ key: any,
48
+ isStatic: any,
49
+ source: any,
50
+ self: any,
51
+ ) => {
52
+ return jsx(type, props, key);
53
+ };
@@ -0,0 +1,59 @@
1
+ import type { Dodo } from "../dodo.js";
2
+
3
+ export interface DodoGlobalPlugin {
4
+ id: string;
5
+ add?: () => void;
6
+ remove?: () => void;
7
+ events?: [string, (instance: Dodo) => void][];
8
+ }
9
+
10
+ export class DodoGlobalPlugins {
11
+ plugins: Set<DodoGlobalPlugin> = new Set();
12
+ event_callbacks: Partial<Record<string, ((instance: Dodo) => void)[]>> = {};
13
+
14
+ constructor() {}
15
+
16
+ private rebuild(): this {
17
+ // this.event_callbacks.clear();
18
+ this.event_callbacks = {};
19
+
20
+ for (const plugin of this.plugins) {
21
+ if (plugin.events != undefined) {
22
+ for (const [event, cb] of plugin.events) {
23
+ this.event_callbacks[event] ??= [];
24
+ this.event_callbacks[event].push(cb);
25
+ }
26
+ }
27
+ }
28
+
29
+ return this;
30
+ }
31
+
32
+ add(plugins: DodoGlobalPlugin | DodoGlobalPlugin[]): this {
33
+ const _plugins = Array.isArray(plugins) ? plugins : [plugins];
34
+
35
+ for (const plugin of _plugins) {
36
+ this.plugins.add(plugin);
37
+ if (plugin.add != undefined) plugin.add();
38
+ }
39
+ this.rebuild();
40
+ return this;
41
+ }
42
+
43
+ remove(ids: string | string[]) {
44
+ const _ids = Array.isArray(ids) ? ids : [ids];
45
+
46
+ for (const id of _ids) {
47
+ const plugin = Array.from(this.plugins).find(
48
+ (plugin) => plugin.id == id,
49
+ );
50
+
51
+ if (plugin != undefined) {
52
+ if (plugin.remove != undefined) plugin.remove();
53
+ this.plugins.delete(plugin);
54
+ }
55
+ }
56
+
57
+ this.rebuild();
58
+ }
59
+ }
@@ -0,0 +1,60 @@
1
+ type EmitterEvents = Record<string, any>;
2
+ type Evt<K, Strict extends boolean> = Strict extends true
3
+ ? keyof K
4
+ : keyof K | (string & {});
5
+ type CallbackEmit<T extends EmitterEvents, K> = K extends keyof T
6
+ ? T[K]
7
+ : any[];
8
+
9
+ export class SubMap<
10
+ T extends EmitterEvents & {} = {},
11
+ Strict extends boolean = false,
12
+ > {
13
+ public readonly all: Map<Evt<T, Strict>, any[]> = new Map();
14
+
15
+ public get<K extends Evt<T, Strict>>(event: K) {
16
+ return this.all.get(event) ?? [];
17
+ }
18
+
19
+ public add<K extends Evt<T, Strict>>(
20
+ event: K,
21
+ ...items: CallbackEmit<T, K>
22
+ ): this {
23
+ let list = this.all.get(event);
24
+
25
+ if (list) {
26
+ list.push(
27
+ ...(items.filter((e: any) => list?.includes(e) != true) as any),
28
+ );
29
+ } else {
30
+ this.all.set(event, [...items]);
31
+ }
32
+
33
+ return this;
34
+ }
35
+
36
+ public remove<K extends Evt<T, Strict>>(
37
+ event: K,
38
+ ...items: [CallbackEmit<T, K>[0], ...CallbackEmit<T, K>[]]
39
+ ): this {
40
+ const list = this.all.get(event);
41
+
42
+ if (list) {
43
+ for (const item of items) {
44
+ const index = list.indexOf(item);
45
+
46
+ if (index !== -1) {
47
+ list.splice(index, 1);
48
+ }
49
+ }
50
+ }
51
+
52
+ return this;
53
+ }
54
+
55
+ public removeAll<K extends Evt<T, Strict>>(event: K): this {
56
+ this.all.delete(event);
57
+
58
+ return this;
59
+ }
60
+ }
@@ -0,0 +1,51 @@
1
+ import { Util as KiwiUtil } from "@orago/kiwi";
2
+ import { Dodo } from "../dodo.js";
3
+
4
+ export function whereString(options: KiwiUtil.WhereOptions): string {
5
+ const class_str = (options.classes ?? [])?.map((e) => "." + e)?.join("");
6
+ const attr_str = Object.entries(options.attributes ?? {}).map(([k, v]) => {
7
+ k = KiwiUtil.formatAttributeName("kebab", k);
8
+ return `[${k}='${v}']`;
9
+ });
10
+ const data_str = Object.entries(options.data ?? {}).map(([k, v]) => {
11
+ k = KiwiUtil.formatAttributeName("kebab", k);
12
+ return `[data-${k}='${v}']`;
13
+ });
14
+
15
+ return `${options.tag ?? ""}${class_str}${attr_str}${data_str}`;
16
+ }
17
+
18
+ export class Query {
19
+ public static qs(
20
+ selector: string,
21
+ element: HTMLElement | Document = document,
22
+ ): Dodo | null {
23
+ const current = element.querySelector(selector);
24
+
25
+ return current ? new Dodo(current as HTMLElement) : null;
26
+ }
27
+
28
+ public static qsAll(
29
+ selector: string,
30
+ element: HTMLElement | Document = document,
31
+ ): Dodo[] {
32
+ return Array.from(element.querySelectorAll(selector)).map((current) => {
33
+ return new Dodo(current as HTMLElement);
34
+ });
35
+ }
36
+ static where(
37
+ options: KiwiUtil.WhereOptions,
38
+ element: HTMLElement | Document = document,
39
+ ): Dodo[] {
40
+ const found = Query.qsAll(whereString(options), element);
41
+
42
+ if (options.text != undefined) {
43
+ return KiwiUtil.textFind(
44
+ options.text,
45
+ found.map((e) => [e.element.textContent, e] as [string, Dodo]),
46
+ ).map((vec) => vec[1]);
47
+ } else {
48
+ return found;
49
+ }
50
+ }
51
+ }