@twobirds/microcomponents 0.11.0 → 0.12.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.
- package/README.md +13 -3
- package/dist/cli/create.d.ts.map +1 -1
- package/dist/cli/dev.d.ts +3 -1
- package/dist/cli/dev.d.ts.map +1 -1
- package/dist/cli/dispatch.d.ts +2 -0
- package/dist/cli/dispatch.d.ts.map +1 -1
- package/dist/cli/index.js +110 -18
- package/dist/cli/preview.d.ts +3 -1
- package/dist/cli/preview.d.ts.map +1 -1
- package/dist/docs/cli.d.ts +1 -1
- package/dist/helpers.min.js +1 -0
- package/dist/mcp/api.json +28 -2
- package/dist/mcp/concepts/mc-types.md +15 -8
- package/dist/microcomponents.d.ts +1 -0
- package/dist/microcomponents.d.ts.map +1 -1
- package/dist/microcomponents.js +1 -1
- package/dist/microcomponents.min.js +1 -1
- package/dist/placeholders.min.js +1 -0
- package/dist/state/index.d.ts +2 -0
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/placeholders.js +1980 -0
- package/dist/utils/autoload.js +1451 -0
- package/dist/utils/events.d.ts +1 -1
- package/dist/utils/events.d.ts.map +1 -1
- package/dist/utils/helpers.js +693 -0
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/package.json +21 -5
package/README.md
CHANGED
|
@@ -100,18 +100,23 @@ pnpm run cli -- new my-card --target-html --target-css --dry-run
|
|
|
100
100
|
```bash
|
|
101
101
|
pnpm run cli -- create --namespace mc --dir mc-app
|
|
102
102
|
# created mc-app/package.json
|
|
103
|
+
# created mc-app/tsconfig.json
|
|
104
|
+
# created mc-app/tsconfig.test.json
|
|
105
|
+
# created mc-app/vitest.config.ts
|
|
103
106
|
# created mc-app/opencode.jsonc
|
|
107
|
+
# created mc-app/.opencode/skills/components/SKILL.md
|
|
104
108
|
# created mc-app/src/index.html
|
|
105
109
|
# created mc-app/src/index-dev.html
|
|
106
110
|
# created mc-app/src/mc/app.ts
|
|
107
|
-
# created mc-app/src/mc/app.
|
|
111
|
+
# created mc-app/src/mc/app.html
|
|
108
112
|
# created mc-app/src/mc/app.target.css
|
|
109
113
|
# created mc-app/src/mc/bundle.ts
|
|
114
|
+
# created mc-app/test/mc/app.spec.ts
|
|
110
115
|
# ...
|
|
111
116
|
# project "mc" created in mc-app/ — run: cd mc-app && pnpm install && pnpm dev
|
|
112
117
|
```
|
|
113
118
|
|
|
114
|
-
`pnpm dev` runs `mc dev`: `tsc -w` plus a `node:http` server serving `build/`, so a saved component reaches the browser through `autoload()` with nothing bundled; companion assets are mirrored into `build/` next to the compiled component, and the library is resolved through an import map to the served `microcomponents.min.js`. `pnpm build` runs `tsc` then `mc bundle`, which emits one `bundle.js` into `public/` with every companion asset inlined by the CLI bundler, the concatenated `bundle.css`, and the production shell — a single script tag, no autoloader. `pnpm preview` serves `public/`. `opencode.jsonc` registers the MCP server, so opencode picks it up when you open the project.
|
|
119
|
+
`pnpm dev` runs `mc dev`: `tsc -w` plus a `node:http` server serving `build/`, so a saved component reaches the browser through `autoload()` with nothing bundled; companion assets are mirrored into `build/` next to the compiled component, and the library is resolved through an import map to the served `microcomponents.min.js`. `pnpm build` runs `tsc` then `mc bundle`, which emits one `bundle.js` into `public/` with every companion asset inlined by the CLI bundler, the concatenated `bundle.css`, and the production shell — a single script tag, no autoloader. `pnpm preview` serves `public/`. `pnpm typecheck` checks `src/` and `test/` in two contexts, and `pnpm test` runs the specs under vitest in jsdom, so component class code executes against a real DOM. `opencode.jsonc` registers the MCP server, so opencode picks it up when you open the project.
|
|
115
120
|
|
|
116
121
|
The namespace (1 to 4 lowercase letters) becomes the `src/<namespace>/` folder and the first segment of every component tag, so `mc` is the home of tags like `<mc-app>`. Because the dev shell is the autoload base, `import.meta.url` + the tag-to-path rule resolves `<mc-app>` to `build/mc/app.js` — the compiled starter. New components are served automatically by the autoloader, but the bundle walks one entry, so add each one to `src/<namespace>/bundle.ts`. `--dir` defaults to the namespace, `--dry-run` prints the plan without touching disk.
|
|
117
122
|
|
|
@@ -121,7 +126,7 @@ Existing files are never overwritten silently: every target is checked before th
|
|
|
121
126
|
|
|
122
127
|
```bash
|
|
123
128
|
pnpm run cli -- create --namespace mc
|
|
124
|
-
# refusing to overwrite
|
|
129
|
+
# refusing to overwrite 16 existing file(s):
|
|
125
130
|
# mc/package.json
|
|
126
131
|
# mc/src/mc/app.ts
|
|
127
132
|
# ...
|
|
@@ -134,11 +139,16 @@ Once installed from npm the same command is available as `mc` (and as `microcomp
|
|
|
134
139
|
```bash
|
|
135
140
|
mc new my-card [--css] [--html] [--target-html] [--shadow-html] [--target-css] [--shadow-css] [--js] [--dir <path>] [--force] [--dry-run]
|
|
136
141
|
mc create --namespace <ns> [--dir <path>] [--force] [--dry-run]
|
|
142
|
+
mc dev [--port <n>] [--host <addr>]
|
|
143
|
+
mc bundle
|
|
144
|
+
mc preview [--port <n>] [--host <addr>]
|
|
137
145
|
mc clean
|
|
138
146
|
mc check
|
|
139
147
|
mc --version
|
|
140
148
|
```
|
|
141
149
|
|
|
150
|
+
`dev` serves `build/` with `tsc -w` running, and `preview` serves `public/`. Both bind to `127.0.0.1` by default, so the served files are not reachable off the machine; pass `--host 0.0.0.0` (or an interface address) when you want the LAN to reach them.
|
|
151
|
+
|
|
142
152
|
`clean` and `check` are thin wrappers around the package.json scripts of the same name, so the scripts remain the single source of truth for what they do — `mc check` type-checks every project context (lib, cli, mcp, test, demo, node) and exits with the script's exit code. `mc --version` (`-v`) prints the version of the package the CLI ships in, read from its own package.json, so it reports the installed library, not the version of the project the command runs in.
|
|
143
153
|
|
|
144
154
|
Nothing has to be installed to run it once — `npx` fetches the package into its own cache and runs the bin from there:
|
package/dist/cli/create.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create.d.ts","sourceRoot":"","sources":["../../src/cli/create.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;EAkBE;AAUF;;;;;;;;;;EAUE;AACF,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAEpD;AAED,yCAAyC;AACzC,MAAM,WAAW,aAAa;IAC7B,kEAAkE;IAClE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9B,+BAA+B;IAC/B,SAAS,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;;;;;EAYE;AACF,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,aAAa,
|
|
1
|
+
{"version":3,"file":"create.d.ts","sourceRoot":"","sources":["../../src/cli/create.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;EAkBE;AAUF;;;;;;;;;;EAUE;AACF,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAEpD;AAED,yCAAyC;AACzC,MAAM,WAAW,aAAa;IAC7B,kEAAkE;IAClE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9B,+BAA+B;IAC/B,SAAS,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;;;;;EAYE;AACF,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,aAAa,CA6BhE"}
|
package/dist/cli/dev.d.ts
CHANGED
|
@@ -81,7 +81,9 @@ export declare function createDevServer(projectRoot: string, options: {
|
|
|
81
81
|
* @param projectRoot Project root (defaults to the current directory).
|
|
82
82
|
* @param minJs Path of `microcomponents.min.js` (defaults to this package's `dist/`).
|
|
83
83
|
* @param port Dev-server port.
|
|
84
|
+
* @param host Bind address. Defaults to loopback, so a dev server serving source files is
|
|
85
|
+
* not reachable off the machine. Pass an interface address to serve the LAN.
|
|
84
86
|
* @returns The process exit code (the server keeps running until killed).
|
|
85
87
|
*/
|
|
86
|
-
export declare function runDev(projectRoot?: string, minJs?: string, port?: number): Promise<number>;
|
|
88
|
+
export declare function runDev(projectRoot?: string, minJs?: string, port?: number, host?: string): Promise<number>;
|
|
87
89
|
//# sourceMappingURL=dev.d.ts.map
|
package/dist/cli/dev.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dev.d.ts","sourceRoot":"","sources":["../../src/cli/dev.ts"],"names":[],"mappings":"AACA;;;;;;;;;;;;;;;GAeG;AAGH,OAAO,EAAsC,KAAK,MAAM,EAAuB,MAAM,WAAW,CAAC;AASjG;;;;;;;;;;;GAWG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAO5C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAO/F;AA8BD;;;;;;;;;;;GAWG;AACH,wBAAsB,UAAU,CAAC,UAAU,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,SAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAW/F;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAC9B,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACtC,MAAM,CAKR;AAED
|
|
1
|
+
{"version":3,"file":"dev.d.ts","sourceRoot":"","sources":["../../src/cli/dev.ts"],"names":[],"mappings":"AACA;;;;;;;;;;;;;;;GAeG;AAGH,OAAO,EAAsC,KAAK,MAAM,EAAuB,MAAM,WAAW,CAAC;AASjG;;;;;;;;;;;GAWG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAO5C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAO/F;AA8BD;;;;;;;;;;;GAWG;AACH,wBAAsB,UAAU,CAAC,UAAU,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,SAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAW/F;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAC9B,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACtC,MAAM,CAKR;AAED;;;;;;;;;;GAUG;AAEH,wBAAsB,MAAM,CAC3B,WAAW,GAAE,MAAsB,EACnC,KAAK,GAAE,MAAuE,EAC9E,IAAI,GAAE,MAAa,EACnB,IAAI,GAAE,MAAoB,GACxB,OAAO,CAAC,MAAM,CAAC,CAiCjB"}
|
package/dist/cli/dispatch.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ so the command works in any consumer project.
|
|
|
13
13
|
*/
|
|
14
14
|
export type Runner = (script: string, options?: {
|
|
15
15
|
port?: number;
|
|
16
|
+
host?: string;
|
|
16
17
|
}) => Promise<number>;
|
|
17
18
|
/**
|
|
18
19
|
Runs a package.json script through pnpm and resolves with its exit code.
|
|
@@ -29,6 +30,7 @@ const code = await runScript('check');
|
|
|
29
30
|
*/
|
|
30
31
|
export declare function runScript(script: string, options?: {
|
|
31
32
|
port?: number;
|
|
33
|
+
host?: string;
|
|
32
34
|
}): Promise<number>;
|
|
33
35
|
/**
|
|
34
36
|
Parses `argv` and routes it to the matching command, returning a process exit
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dispatch.d.ts","sourceRoot":"","sources":["../../src/cli/dispatch.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;EAYE;
|
|
1
|
+
{"version":3,"file":"dispatch.d.ts","sourceRoot":"","sources":["../../src/cli/dispatch.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;EAYE;AAsEF,MAAM,MAAM,MAAM,GAAG,CACpB,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,KACtC,OAAO,CAAC,MAAM,CAAC,CAAC;AAwHrB;;;;;;;;;;;;EAYE;AACF,wBAAsB,SAAS,CAC9B,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,GACxC,OAAO,CAAC,MAAM,CAAC,CAajB;AAED;;;;;;;;;;;;;;;;EAgBE;AACF,wBAAsB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,GAAG,GAAE,MAAkB,GAAG,OAAO,CAAC,MAAM,CAAC,CA8HnF"}
|
package/dist/cli/index.js
CHANGED
|
@@ -215,6 +215,8 @@ function scaffoldProject(namespace) {
|
|
|
215
215
|
const files = {};
|
|
216
216
|
files["package.json"] = packageJson(namespace);
|
|
217
217
|
files["tsconfig.json"] = tsconfig();
|
|
218
|
+
files["tsconfig.test.json"] = tsconfigTest();
|
|
219
|
+
files["vitest.config.ts"] = vitestConfig();
|
|
218
220
|
files[".gitignore"] = gitignore();
|
|
219
221
|
files["opencode.jsonc"] = opencodeConfig();
|
|
220
222
|
files[".opencode/skills/components/SKILL.md"] = componentSkill(namespace, tag);
|
|
@@ -226,6 +228,7 @@ function scaffoldProject(namespace) {
|
|
|
226
228
|
files[`src/${base}.target.css`] = appCss(tag);
|
|
227
229
|
files[`src/${namespace}/bundle.ts`] = bundle();
|
|
228
230
|
files["src/bundle.json"] = bundleConfig(namespace);
|
|
231
|
+
files[`test/${base}.spec.ts`] = appSpec(tag);
|
|
229
232
|
return {
|
|
230
233
|
files,
|
|
231
234
|
namespace
|
|
@@ -240,10 +243,15 @@ function packageJson(namespace) {
|
|
|
240
243
|
dev: "mc dev",
|
|
241
244
|
build: "tsc -p tsconfig.json && mc bundle",
|
|
242
245
|
preview: "mc preview",
|
|
243
|
-
typecheck: "tsc --noEmit -p tsconfig.json"
|
|
246
|
+
typecheck: "tsc --noEmit -p tsconfig.json && tsc --noEmit -p tsconfig.test.json",
|
|
247
|
+
test: "vitest run"
|
|
244
248
|
},
|
|
245
|
-
dependencies: { "@twobirds/microcomponents": "^0.
|
|
246
|
-
devDependencies: {
|
|
249
|
+
dependencies: { "@twobirds/microcomponents": "^0.12.1" },
|
|
250
|
+
devDependencies: {
|
|
251
|
+
typescript: "^6.0.3",
|
|
252
|
+
vitest: "^4.1.10",
|
|
253
|
+
jsdom: "^29.1.1"
|
|
254
|
+
}
|
|
247
255
|
}, null, " ");
|
|
248
256
|
}
|
|
249
257
|
function tsconfig() {
|
|
@@ -267,6 +275,60 @@ function tsconfig() {
|
|
|
267
275
|
include: ["src/**/*.ts"]
|
|
268
276
|
}, null, " ");
|
|
269
277
|
}
|
|
278
|
+
function tsconfigTest() {
|
|
279
|
+
return JSON.stringify({
|
|
280
|
+
compilerOptions: {
|
|
281
|
+
target: "ESNext",
|
|
282
|
+
module: "ESNext",
|
|
283
|
+
moduleResolution: "Bundler",
|
|
284
|
+
lib: [
|
|
285
|
+
"ESNext",
|
|
286
|
+
"DOM",
|
|
287
|
+
"DOM.Iterable"
|
|
288
|
+
],
|
|
289
|
+
strict: true,
|
|
290
|
+
esModuleInterop: true,
|
|
291
|
+
skipLibCheck: true,
|
|
292
|
+
noEmit: true
|
|
293
|
+
},
|
|
294
|
+
include: ["test/**/*.ts"]
|
|
295
|
+
}, null, " ");
|
|
296
|
+
}
|
|
297
|
+
function vitestConfig() {
|
|
298
|
+
return `import { defineConfig } from 'vitest/config';
|
|
299
|
+
|
|
300
|
+
export default defineConfig({
|
|
301
|
+
test: {
|
|
302
|
+
// components need a DOM: jsdom gives \`customElements\`, \`document\`, and the
|
|
303
|
+
// \`_mc\` contract the library writes, so a spec runs the real class code
|
|
304
|
+
environment: 'jsdom',
|
|
305
|
+
include: ['test/**/*.spec.ts'],
|
|
306
|
+
},
|
|
307
|
+
});
|
|
308
|
+
`;
|
|
309
|
+
}
|
|
310
|
+
function appSpec(tag) {
|
|
311
|
+
const className = kebabToPascal(tag);
|
|
312
|
+
return `import { describe, expect, it } from 'vitest';
|
|
313
|
+
import { DC, parse } from '@twobirds/microcomponents';
|
|
314
|
+
import { ${className} } from '../../src/${tagPath(tag)}.js';
|
|
315
|
+
|
|
316
|
+
describe('${tag}', () => {
|
|
317
|
+
it('registers the starter tag in the custom element registry', () => {
|
|
318
|
+
// importing the module runs defineCE, so the tag is in the registry, and the
|
|
319
|
+
// exported class is the DC subclass behind it
|
|
320
|
+
expect(customElements.get('${tag}')).toBeDefined();
|
|
321
|
+
expect(${className}.prototype instanceof DC).toBe(true);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it('renders the title from data through parse()', () => {
|
|
325
|
+
const html = parse('<h1>{title}</h1>', { title: 'Micro Components' });
|
|
326
|
+
|
|
327
|
+
expect(html).toContain('<h1>Micro Components</h1>');
|
|
328
|
+
});
|
|
329
|
+
});
|
|
330
|
+
`;
|
|
331
|
+
}
|
|
270
332
|
function gitignore() {
|
|
271
333
|
return `node_modules/
|
|
272
334
|
build/
|
|
@@ -359,11 +421,14 @@ Bubble up or down the tree only when the receiver cannot be named.
|
|
|
359
421
|
|
|
360
422
|
\`\`\`sh
|
|
361
423
|
pnpm typecheck
|
|
424
|
+
pnpm test
|
|
362
425
|
pnpm dev
|
|
363
426
|
pnpm build
|
|
364
427
|
pnpm preview
|
|
365
428
|
\`\`\`
|
|
366
429
|
|
|
430
|
+
Specs live in \`test/\` and run under vitest in jsdom, so component class code executes against a real DOM. Import the component from \`src/\` with a \`.js\` specifier; vitest resolves it to the \`.ts\` source.
|
|
431
|
+
|
|
367
432
|
\`mc clean\` removes \`build/\`, \`public/\` and \`coverage/\`.
|
|
368
433
|
|
|
369
434
|
## API reference
|
|
@@ -391,7 +456,8 @@ pnpm dev
|
|
|
391
456
|
The dev server runs \`tsc -w\` and serves \`build/\`, so a saved component reaches the
|
|
392
457
|
browser through \`autoload()\` with no bundling. Companion \`.css\`, \`.html\` and \`.json\`
|
|
393
458
|
files are mirrored into \`build/\` next to the compiled component, which is where the
|
|
394
|
-
autoloader looks for them.
|
|
459
|
+
autoloader looks for them. The server binds loopback, so only this machine reaches it;
|
|
460
|
+
\`pnpm dev -- --host 0.0.0.0\` widens it to the LAN.
|
|
395
461
|
|
|
396
462
|
## Build
|
|
397
463
|
|
|
@@ -410,13 +476,26 @@ The bundle is driven by \`src/bundle.json\`: \`entry\` names the barrel to bundl
|
|
|
410
476
|
pnpm preview
|
|
411
477
|
\`\`\`
|
|
412
478
|
|
|
479
|
+
## Test
|
|
480
|
+
|
|
481
|
+
\`\`\`sh
|
|
482
|
+
pnpm test
|
|
483
|
+
\`\`\`
|
|
484
|
+
|
|
485
|
+
Specs live in \`test/\` and run under vitest in jsdom, so component class code executes
|
|
486
|
+
against a real DOM: \`customElements\`, \`document\`, and the \`_mc\` property the library
|
|
487
|
+
writes. \`test/${namespace}/app.spec.ts\` is the starter spec — it asserts the tag is
|
|
488
|
+
registered and that \`parse()\` renders the title from \`data\`.
|
|
489
|
+
|
|
413
490
|
## Type-check
|
|
414
491
|
|
|
415
492
|
\`\`\`sh
|
|
416
493
|
pnpm typecheck
|
|
417
494
|
\`\`\`
|
|
418
495
|
|
|
419
|
-
\`tsconfig.json\` compiles \`src/\` into \`build
|
|
496
|
+
\`tsconfig.json\` compiles \`src/\` into \`build/\`; \`tsconfig.test.json\` type-checks
|
|
497
|
+
\`test/\` without emitting, so a spec that imports a component is checked against the
|
|
498
|
+
component's real types.
|
|
420
499
|
|
|
421
500
|
## Components
|
|
422
501
|
|
|
@@ -499,7 +578,7 @@ function appSource(namespace, tag) {
|
|
|
499
578
|
<${tag}></${tag}>
|
|
500
579
|
\`\`\`
|
|
501
580
|
*/
|
|
502
|
-
class ${className} extends DC {
|
|
581
|
+
export class ${className} extends DC {
|
|
503
582
|
static assets = ['html', 'targetCss'];
|
|
504
583
|
|
|
505
584
|
data = { title: 'Micro Components' };
|
|
@@ -1341,10 +1420,12 @@ function createDevServer(projectRoot, options) {
|
|
|
1341
1420
|
* @param projectRoot Project root (defaults to the current directory).
|
|
1342
1421
|
* @param minJs Path of `microcomponents.min.js` (defaults to this package's `dist/`).
|
|
1343
1422
|
* @param port Dev-server port.
|
|
1423
|
+
* @param host Bind address. Defaults to loopback, so a dev server serving source files is
|
|
1424
|
+
* not reachable off the machine. Pass an interface address to serve the LAN.
|
|
1344
1425
|
* @returns The process exit code (the server keeps running until killed).
|
|
1345
1426
|
*/
|
|
1346
1427
|
/* v8 ignore start — the long-running server + tsc watch is exercised by hand, not in the suite */
|
|
1347
|
-
async function runDev(projectRoot = process.cwd(), minJs = path.join(import.meta.dirname, "..", "microcomponents.min.js"), port = 5173) {
|
|
1428
|
+
async function runDev(projectRoot = process.cwd(), minJs = path.join(import.meta.dirname, "..", "microcomponents.min.js"), port = 5173, host = "127.0.0.1") {
|
|
1348
1429
|
const sourceRoot = path.join(projectRoot, "src");
|
|
1349
1430
|
const buildRoot = path.join(projectRoot, "build");
|
|
1350
1431
|
await mirrorInto(sourceRoot, buildRoot);
|
|
@@ -1369,8 +1450,8 @@ async function runDev(projectRoot = process.cwd(), minJs = path.join(import.meta
|
|
|
1369
1450
|
minJs,
|
|
1370
1451
|
port
|
|
1371
1452
|
});
|
|
1372
|
-
server.listen(port);
|
|
1373
|
-
console.log(`dev server on http
|
|
1453
|
+
server.listen(port, host);
|
|
1454
|
+
console.log(`dev server on http://${host === "0.0.0.0" ? "localhost" : host}:${port}/ (serving build/, tsc -w running)`);
|
|
1374
1455
|
return new Promise((resolve) => {
|
|
1375
1456
|
tsc.on("exit", () => {
|
|
1376
1457
|
server.close();
|
|
@@ -1454,13 +1535,15 @@ function createPreviewServer(projectRoot, port, outDir = "public") {
|
|
|
1454
1535
|
*
|
|
1455
1536
|
* @param projectRoot Project root (defaults to the current directory).
|
|
1456
1537
|
* @param port Preview-server port.
|
|
1538
|
+
* @param host Bind address. Defaults to loopback, so a preview server is not reachable off
|
|
1539
|
+
* the machine. Pass an interface address to serve the LAN.
|
|
1457
1540
|
* @returns The process exit code (the server keeps running until killed).
|
|
1458
1541
|
*/
|
|
1459
1542
|
/* v8 ignore start — the long-running server is exercised by hand, not in the suite */
|
|
1460
|
-
async function runPreview(projectRoot = process.cwd(), port = 5174) {
|
|
1543
|
+
async function runPreview(projectRoot = process.cwd(), port = 5174, host = "127.0.0.1") {
|
|
1461
1544
|
const server = createPreviewServer(projectRoot, port);
|
|
1462
|
-
server.listen(port);
|
|
1463
|
-
console.log(`preview server on http
|
|
1545
|
+
server.listen(port, host);
|
|
1546
|
+
console.log(`preview server on http://${host === "0.0.0.0" ? "localhost" : host}:${port}/ (serving public/)`);
|
|
1464
1547
|
return new Promise((resolve) => {
|
|
1465
1548
|
server.on("close", () => resolve(0));
|
|
1466
1549
|
});
|
|
@@ -1486,9 +1569,9 @@ var USAGE = `mc — Micro Components build tools
|
|
|
1486
1569
|
Usage:
|
|
1487
1570
|
mc new <tag> [--css] [--html] [--target-html] [--shadow-html] [--target-css] [--shadow-css] [--js] [--dir <path>] [--force] [--dry-run]
|
|
1488
1571
|
mc create --namespace <ns> [--dir <path>] [--force] [--dry-run]
|
|
1489
|
-
mc dev [--port <n>]
|
|
1572
|
+
mc dev [--port <n>] [--host <addr>]
|
|
1490
1573
|
mc bundle
|
|
1491
|
-
mc preview [--port <n>]
|
|
1574
|
+
mc preview [--port <n>] [--host <addr>]
|
|
1492
1575
|
mc clean
|
|
1493
1576
|
mc check
|
|
1494
1577
|
mc --version
|
|
@@ -1515,6 +1598,8 @@ Other options:
|
|
|
1515
1598
|
--js emit plain JavaScript instead of TypeScript
|
|
1516
1599
|
--dir <path> scaffold root (default: src)
|
|
1517
1600
|
--port <n> dev / preview server port (default: 5173 / 5174)
|
|
1601
|
+
--host <addr> dev / preview bind address (default: 127.0.0.1 — loopback only; pass
|
|
1602
|
+
0.0.0.0 or a interface address to serve the LAN)
|
|
1518
1603
|
--force overwrite files that already exist
|
|
1519
1604
|
--dry-run print what would be written; touch nothing on disk
|
|
1520
1605
|
--version print the version of the package the CLI ships in (-v)
|
|
@@ -1670,9 +1755,9 @@ async function runScript(script, options) {
|
|
|
1670
1755
|
if (await hasScript(script)) return spawnProcess("pnpm", ["run", script]);
|
|
1671
1756
|
if (script === "clean") return cleanSelf();
|
|
1672
1757
|
if (script === "check") return checkSelf();
|
|
1673
|
-
if (script === "dev") return runDev(process.cwd(), void 0, options?.port ?? 5173);
|
|
1758
|
+
if (script === "dev") return runDev(process.cwd(), void 0, options?.port ?? 5173, options?.host ?? "127.0.0.1");
|
|
1674
1759
|
if (script === "bundle") return runBundle();
|
|
1675
|
-
if (script === "preview") return runPreview(process.cwd(), options?.port ?? 5174);
|
|
1760
|
+
if (script === "preview") return runPreview(process.cwd(), options?.port ?? 5174, options?.host ?? "127.0.0.1");
|
|
1676
1761
|
console.error(`mc: no "${script}" script in the current project`);
|
|
1677
1762
|
return 1;
|
|
1678
1763
|
}
|
|
@@ -1708,6 +1793,7 @@ async function main(argv, run = runScript) {
|
|
|
1708
1793
|
dir: { type: "string" },
|
|
1709
1794
|
namespace: { type: "string" },
|
|
1710
1795
|
port: { type: "string" },
|
|
1796
|
+
host: { type: "string" },
|
|
1711
1797
|
force: { type: "boolean" },
|
|
1712
1798
|
"dry-run": { type: "boolean" },
|
|
1713
1799
|
help: {
|
|
@@ -1791,9 +1877,15 @@ async function main(argv, run = runScript) {
|
|
|
1791
1877
|
console.log(`\nproject "${namespace}" created in ${root}/ — run: cd ${root} && pnpm install && pnpm dev`);
|
|
1792
1878
|
return 0;
|
|
1793
1879
|
}
|
|
1794
|
-
if (command === "dev") return run("dev", {
|
|
1880
|
+
if (command === "dev") return run("dev", {
|
|
1881
|
+
port: values.port ? Number(values.port) : 5173,
|
|
1882
|
+
host: values.host
|
|
1883
|
+
});
|
|
1795
1884
|
if (command === "bundle") return run("bundle");
|
|
1796
|
-
if (command === "preview") return run("preview", {
|
|
1885
|
+
if (command === "preview") return run("preview", {
|
|
1886
|
+
port: values.port ? Number(values.port) : 5174,
|
|
1887
|
+
host: values.host
|
|
1888
|
+
});
|
|
1797
1889
|
if (command === "clean") return run("clean");
|
|
1798
1890
|
if (command === "check") return run("check");
|
|
1799
1891
|
console.error(`mc: unknown command "${command}"\n\n${USAGE}`);
|
package/dist/cli/preview.d.ts
CHANGED
|
@@ -42,7 +42,9 @@ export declare function createPreviewServer(projectRoot: string, port: number, o
|
|
|
42
42
|
*
|
|
43
43
|
* @param projectRoot Project root (defaults to the current directory).
|
|
44
44
|
* @param port Preview-server port.
|
|
45
|
+
* @param host Bind address. Defaults to loopback, so a preview server is not reachable off
|
|
46
|
+
* the machine. Pass an interface address to serve the LAN.
|
|
45
47
|
* @returns The process exit code (the server keeps running until killed).
|
|
46
48
|
*/
|
|
47
|
-
export declare function runPreview(projectRoot?: string, port?: number): Promise<number>;
|
|
49
|
+
export declare function runPreview(projectRoot?: string, port?: number, host?: string): Promise<number>;
|
|
48
50
|
//# sourceMappingURL=preview.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"preview.d.ts","sourceRoot":"","sources":["../../src/cli/preview.ts"],"names":[],"mappings":"AACA;;;;;;;;;GASG;AAEH,OAAO,EAAsC,KAAK,MAAM,EAAuB,MAAM,WAAW,CAAC;AAMjG;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAKvF;AA6BD;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CAAC,WAAW,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,SAAW,GAAG,MAAM,CAKhG;AAED
|
|
1
|
+
{"version":3,"file":"preview.d.ts","sourceRoot":"","sources":["../../src/cli/preview.ts"],"names":[],"mappings":"AACA;;;;;;;;;GASG;AAEH,OAAO,EAAsC,KAAK,MAAM,EAAuB,MAAM,WAAW,CAAC;AAMjG;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAKvF;AA6BD;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CAAC,WAAW,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,SAAW,GAAG,MAAM,CAKhG;AAED;;;;;;;;GAQG;AAEH,wBAAsB,UAAU,CAC/B,WAAW,GAAE,MAAsB,EACnC,IAAI,GAAE,MAAa,EACnB,IAAI,GAAE,MAAoB,GACxB,OAAO,CAAC,MAAM,CAAC,CAUjB"}
|
package/dist/docs/cli.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(e,t){let n=t.toLowerCase();return`on`+n in e?n:`_mc`+t[0].toUpperCase()+t.substring(1)}function t(n,r,i,a=!1){if(n){if(typeof n==`string`){document.querySelectorAll(n).forEach(e=>t(e,r,i));return}return n.addEventListener(e(n,r),i,{capture:!1,once:a}),i}}var n=class{stopped=!1;constructor(){}stop(){this.stopped=!0}};function r(t,i,a={},o=`l`,s){if(!t)return;if(typeof t==`string`){document.querySelectorAll(t).forEach(e=>r(e,i,a,o));return}let c=/^_mc/.test(i)?i:e(t,i),l=s||new n,u=new CustomEvent(c,{detail:{data:a,control:l},bubbles:!1});l.stopped||/l/.test(o)&&(t.dispatchEvent(u),l.stopped)||/[ud]/.test(o)&&queueMicrotask(()=>{if(o.indexOf(`u`)>-1&&r(t.parentElement,c,a,`lu`,l),o.indexOf(`d`)>-1)for(let e of t.children)r(e,c,a,`ld`)})}function i(){return typeof window<`u`&&window.document!==void 0}function a(e){return typeof e==`object`&&!!e&&e.constructor===Object}function o(e){let t={},n=Object.keys(e).sort();for(let r=0;r<n.length;r++){let i=n[r],s=e[i];a(s)?t[i]=o(s):t[i]=s}return t}function s(e,t=!0){let n={},r=e,i=Object.getOwnPropertyNames(e);for(let e=0;e<i.length;e++){let t=i[e],a=r[t];if(a instanceof Object){let e=s(a,!1);for(let r in e)n[t+`.`+r]=e[r]}else n[t]=a}return t?o(n):n}function c(e,t){if(e===t||e!==e&&t!==t)return!0;if(typeof e!=`object`||!e||typeof t!=`object`||!t||e.constructor!==t.constructor)return!1;if(e instanceof Date)return e.getTime()===t.getTime();if(e instanceof RegExp)return e.toString()===t.toString();if(Array.isArray(e)){let n=t;if(e.length!==n.length)return!1;for(let t=0;t<e.length;t++)if(!c(e[t],n[t]))return!1;return!0}let n=e,r=t,i=Object.keys(n);if(i.length!==Object.keys(r).length)return!1;for(let e=0;e<i.length;e++){let t=i[e];if(!Object.prototype.hasOwnProperty.call(r,t)||!c(n[t],r[t]))return!1}return!0}function l(e,t={}){let n=t,r=typeof e==`string`?e.split(`.`):e;if(!n||!r[0]||!(r[0]in n))return;let i=n[r[0]];return r.length>1?a(i)?l(r.slice(1),i):void 0:i}function u(e,t,...n){let r=[e,t,...n];if(r.length>2){for(;r.length>1;)r[0]=u(r[0],r[1]),r.splice(1,1);return r[0]}if(typeof e==`string`){let n=e;return e.match(/\{[^\{\}]*\}/g)?.forEach(e=>{let r=l(e.substring(1,e.length-1),t);r!==void 0&&(n=n.replace(e,String(r)))}),n}else if(Array.isArray(e))return e.map(e=>u(e,t));else if(a(e)){let n=e;Object.keys(n).forEach(e=>{Object.prototype.hasOwnProperty.call(n,e)&&(n[e]=u(n[e],t))})}return e}function d(e){return e.split(`-`).map(e=>e.charAt(0).toUpperCase()+e.slice(1)).join(``)}function f(e){return e.replace(/[A-Z]/g,(e,t)=>(t?`-`:``)+e.toLowerCase())}function p(e,t){let n;return(...r)=>{clearTimeout(n),n=setTimeout(()=>{e(...r)},t)}}function m(e){let t=document.createElement(`template`);return e=e.replace(/<([A-Za-z0-9\-]*)([^>\/]*)(\/>)/gi,`<$1$2></$1>`),t.innerHTML=e,t.content.normalize(),Array.from(t.content.childNodes)}function h(e,t){let n=Object.getOwnPropertyDescriptors(e);for(let[e,r]of Object.entries(n))(`get`in r||`set`in r)&&Object.defineProperty(t,e,r)}var g=new WeakMap;function _(e,t){let n=g.get(e)??new Set;if(n.has(t))return e.adoptedStyleSheets[0];n.add(t),g.set(e,n);let r=new CSSStyleSheet;return r.replaceSync(t),e.adoptedStyleSheets.push(r),r}function v(e,t){let n=e.target,r=n.shadowRoot;!r&&Object.keys(t).some(e=>e.indexOf(`shadow`)===0)&&(r=n.attachShadow({mode:`open`})),Object.keys(t).forEach(i=>{switch(i){case`css`:_(document,t[i]);break;case`targetCss`:_(document,t[i]);break;case`shadowCss`:_(r,t[i]);break;case`shadowHtml`:r.innerHTML=t[i],b(r,e);break;case`targetHtml`:n.innerHTML=t[i],b(n,e);break}})}function y(e,t){let n=t=>{let n=t.match(/^([a-z]+)([A-Z][a-z]*)$/);return n?`./${e.replace(/-/g,`/`)}.${n[1]}.${n[2].toLowerCase()}`:`./${e.replace(/-/g,`/`)}.${t.toLowerCase()}`};return Promise.all(t.map(async e=>{let t=n(e);try{let n=await fetch(t);if(!n.ok)return`file not found: ${t}`;if(t.endsWith(`json`)){let r=await n.text();try{return JSON.parse(r)}catch{return console.error(`could not JSON.parse() asset '${e}' from ${t}:\n`,r),null}}return n.text()}catch{return`file not found: ${t}`}}))}function b(e,n){let i=[...e.querySelectorAll(`*`)];for(let e of i){if(!(e instanceof HTMLElement))continue;let i=[...e.attributes];for(let a of i){let i=a.name.toLowerCase();if(!(`on`+i in e))continue;let o=a.value.trim().split(`|`);if(o.length<2)continue;let[s,c,l]=o;e.removeAttribute(a.name);let u;if(l)try{u=JSON.parse(l)}catch{u=l}s===`this`||s===``?t(e,i,()=>n.trigger(c,u)):t(e,i,()=>r(s,c,u))}}}export{b as bindTemplateEvents,h as copyGettersSetters,p as debounce,c as deepEqual,s as flattenObject,m as htmlToElements,_ as injectCSS,i as isBrowser,a as isNativeObject,d as kebabToPascal,y as loadAssets,l as nameSpace,u as parse,f as pascalToKebab,v as processAssets,o as sortObject};
|
package/dist/mcp/api.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
|
-
"libraryVersion": "0.
|
|
3
|
-
"exportedCount":
|
|
2
|
+
"libraryVersion": "0.12.1",
|
|
3
|
+
"exportedCount": 59,
|
|
4
4
|
"entries": {
|
|
5
5
|
"MC": {
|
|
6
6
|
"name": "MC",
|
|
@@ -358,6 +358,16 @@
|
|
|
358
358
|
}
|
|
359
359
|
]
|
|
360
360
|
},
|
|
361
|
+
"makePlaceholders": {
|
|
362
|
+
"name": "makePlaceholders",
|
|
363
|
+
"kind": "function",
|
|
364
|
+
"group": "State",
|
|
365
|
+
"file": "src/state/placeholders.ts",
|
|
366
|
+
"signatures": [
|
|
367
|
+
"export function makePlaceholders(element: HTMLElement | DocumentFragment | ShadowRoot): void {"
|
|
368
|
+
],
|
|
369
|
+
"description": "Applies placeholder support to an element and its descendants.\n\nThis function scans the provided target for elements containing placeholders\nand attaches a `Placeholders` controller instance to each one if not already present."
|
|
370
|
+
},
|
|
361
371
|
"getMcEventName": {
|
|
362
372
|
"name": "getMcEventName",
|
|
363
373
|
"kind": "function",
|
|
@@ -614,6 +624,22 @@
|
|
|
614
624
|
}
|
|
615
625
|
]
|
|
616
626
|
},
|
|
627
|
+
"pascalToKebab": {
|
|
628
|
+
"name": "pascalToKebab",
|
|
629
|
+
"kind": "function",
|
|
630
|
+
"group": "Helpers",
|
|
631
|
+
"file": "src/utils/helpers.ts",
|
|
632
|
+
"signatures": [
|
|
633
|
+
"export function pascalToKebab(str: string) {"
|
|
634
|
+
],
|
|
635
|
+
"description": "Converts a PascalCase string to kebab-case.\nExample: 'MyComponentName' -> 'my-component-name'",
|
|
636
|
+
"examples": [
|
|
637
|
+
{
|
|
638
|
+
"title": "",
|
|
639
|
+
"code": "console.log(pascalToKebab('MyElement'));"
|
|
640
|
+
}
|
|
641
|
+
]
|
|
642
|
+
},
|
|
617
643
|
"debounce": {
|
|
618
644
|
"name": "debounce",
|
|
619
645
|
"kind": "function",
|
|
@@ -124,14 +124,15 @@ the namespaced variables of their composed parent.
|
|
|
124
124
|
Each custom element (`.ts`/`.js`) ships with optional companion files sharing the same
|
|
125
125
|
base name. The suffix encodes the structural sub-pattern (§1a–1b) and the styling target.
|
|
126
126
|
|
|
127
|
-
| Suffix | Purpose
|
|
128
|
-
| -------------- |
|
|
129
|
-
| `.css` | CSS custom properties (variable defaults) only — no structural styles
|
|
130
|
-
| `.target.css` | Structural style rules for target DOM (`innerHTML`)
|
|
131
|
-
| `.shadow.css` | Structural style rules for shadow DOM
|
|
132
|
-
| `.target.html` | HTML template injected into `this.target.innerHTML`
|
|
133
|
-
| `.shadow.html` | HTML template injected into `shadowRoot.innerHTML`
|
|
134
|
-
| `.
|
|
127
|
+
| Suffix | Purpose | Styling Target |
|
|
128
|
+
| -------------- | ----------------------------------------------------------------------- | ---------------------------------------------------- |
|
|
129
|
+
| `.css` | CSS custom properties (variable defaults) only — no structural styles | N/A (token definitions) |
|
|
130
|
+
| `.target.css` | Structural style rules for target DOM (`innerHTML`) | Global document scope via `injectCSS(document, ...)` |
|
|
131
|
+
| `.shadow.css` | Structural style rules for shadow DOM | Shadow root via `injectCSS(shadowRoot, ...)` |
|
|
132
|
+
| `.target.html` | HTML template injected into `this.target.innerHTML` | N/A (markup) |
|
|
133
|
+
| `.shadow.html` | HTML template injected into `shadowRoot.innerHTML` | N/A (markup) |
|
|
134
|
+
| `.html` | HTML template loaded as a plain string; the component injects it itself | N/A (markup) |
|
|
135
|
+
| `.json` | Static data files imported by the component | N/A (data) |
|
|
135
136
|
|
|
136
137
|
Companion files follow the same directory structure as their source file. A component at
|
|
137
138
|
`src/shop/sidebar.ts` has companions at `src/shop/sidebar.target.html`,
|
|
@@ -139,6 +140,12 @@ Companion files follow the same directory structure as their source file. A comp
|
|
|
139
140
|
`loadAssets()` / `processAssets()`; keep the `.ts` source focused on logic only —
|
|
140
141
|
constructor, lifecycle, event handlers, state.
|
|
141
142
|
|
|
143
|
+
**Declarative attributes are bound by the asset pipeline for `targetHtml` and `shadowHtml`
|
|
144
|
+
only.** `processAssets()` calls `bindTemplateEvents()` after injecting those two. A plain
|
|
145
|
+
`html` asset arrives as a string and is injected by the component, so its `click=` / `change=`
|
|
146
|
+
attributes are inert until the component binds them itself — `oneInit()` calls
|
|
147
|
+
`bindTemplateEvents(this.target, this)` right after the parse, as `<todo-item>` does.
|
|
148
|
+
|
|
142
149
|
### Inline Templates — Escape Hatch
|
|
143
150
|
|
|
144
151
|
A simple `const template` string constant is allowed **only** when the component has no
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"microcomponents.d.ts","sourceRoot":"","sources":["../src/microcomponents.ts"],"names":[],"mappings":"AACA,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,cAAc,CAAC;AAC7B,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC"}
|
|
1
|
+
{"version":3,"file":"microcomponents.d.ts","sourceRoot":"","sources":["../src/microcomponents.ts"],"names":[],"mappings":"AACA,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,cAAc,CAAC;AAC7B,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC"}
|
package/dist/microcomponents.js
CHANGED
|
@@ -2987,4 +2987,4 @@ formValues: function(target, propName) {
|
|
|
2987
2987
|
} });
|
|
2988
2988
|
}
|
|
2989
2989
|
//#endregion
|
|
2990
|
-
export { BubbleController, DC, MC, McBase, autoload, bindTemplateEvents, copyGettersSetters, debounce, deepEqual, defineCE, flattenObject, formValuePlugin, formValues, getCE, getMcEventName, htmlToElements, injectCSS, isAlreadyObserved, isBrowser, isMcHost, isNativeObject, kebabToPascal, listCE, loadAssets, nameSpace, observe, observePlugin, off, on, one, parse, pascalToKebab, processAssets, sortObject, trigger, trueEventName };
|
|
2990
|
+
export { BubbleController, DC, MC, McBase, autoload, bindTemplateEvents, copyGettersSetters, debounce, deepEqual, defineCE, flattenObject, formValuePlugin, formValues, getCE, getMcEventName, htmlToElements, injectCSS, isAlreadyObserved, isBrowser, isMcHost, isNativeObject, kebabToPascal, listCE, loadAssets, makePlaceholders, nameSpace, observe, observePlugin, off, on, one, parse, pascalToKebab, processAssets, sortObject, trigger, trueEventName };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
function e(e){return`_mc`+e[0].toUpperCase()+e.substring(1)}function t(e,t){let n=t.toLowerCase();return`on`+n in e?n:`_mc`+t[0].toUpperCase()+t.substring(1)}function n(e,r,i){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>n(e,r,i));return}e.removeEventListener(t(e,r),i)}}function r(e,n,i,a=!1){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>r(e,n,i));return}return e.addEventListener(t(e,n),i,{capture:!1,once:a}),i}}function i(e,t,n){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>i(e,t,n));return}return r(e,t,n,!0),n}}var a=class{stopped=!1;constructor(){}stop(){this.stopped=!0}};function o(e,n,r={},i=`l`,s){if(!e)return;if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>o(e,n,r,i));return}let c=/^_mc/.test(n)?n:t(e,n),l=s||new a,u=new CustomEvent(c,{detail:{data:r,control:l},bubbles:!1});l.stopped||/l/.test(i)&&(e.dispatchEvent(u),l.stopped)||/[ud]/.test(i)&&queueMicrotask(()=>{if(i.indexOf(`u`)>-1&&o(e.parentElement,c,r,`lu`,l),i.indexOf(`d`)>-1)for(let t of e.children)o(t,c,r,`ld`)})}function s(){return typeof window<`u`&&window.document!==void 0}function c(e){return typeof e==`object`&&!!e&&e.constructor===Object}function l(e){let t={},n=Object.keys(e).sort();for(let r=0;r<n.length;r++){let i=n[r],a=e[i];c(a)?t[i]=l(a):t[i]=a}return t}function u(e,t=!0){let n={},r=e,i=Object.getOwnPropertyNames(e);for(let e=0;e<i.length;e++){let t=i[e],a=r[t];if(a instanceof Object){let e=u(a,!1);for(let r in e)n[t+`.`+r]=e[r]}else n[t]=a}return t?l(n):n}function d(e,t){if(e===t||e!==e&&t!==t)return!0;if(typeof e!=`object`||!e||typeof t!=`object`||!t||e.constructor!==t.constructor)return!1;if(e instanceof Date)return e.getTime()===t.getTime();if(e instanceof RegExp)return e.toString()===t.toString();if(Array.isArray(e)){let n=t;if(e.length!==n.length)return!1;for(let t=0;t<e.length;t++)if(!d(e[t],n[t]))return!1;return!0}let n=e,r=t,i=Object.keys(n);if(i.length!==Object.keys(r).length)return!1;for(let e=0;e<i.length;e++){let t=i[e];if(!Object.prototype.hasOwnProperty.call(r,t)||!d(n[t],r[t]))return!1}return!0}function f(e,t={}){let n=t,r=typeof e==`string`?e.split(`.`):e;if(!n||!r[0]||!(r[0]in n))return;let i=n[r[0]];return r.length>1?c(i)?f(r.slice(1),i):void 0:i}function p(e,t,...n){let r=[e,t,...n];if(r.length>2){for(;r.length>1;)r[0]=p(r[0],r[1]),r.splice(1,1);return r[0]}if(typeof e==`string`){let n=e;return e.match(/\{[^\{\}]*\}/g)?.forEach(e=>{let r=f(e.substring(1,e.length-1),t);r!==void 0&&(n=n.replace(e,String(r)))}),n}else if(Array.isArray(e))return e.map(e=>p(e,t));else if(c(e)){let n=e;Object.keys(n).forEach(e=>{Object.prototype.hasOwnProperty.call(n,e)&&(n[e]=p(n[e],t))})}return e}function m(e){return e.split(`-`).map(e=>e.charAt(0).toUpperCase()+e.slice(1)).join(``)}function h(e){return e.replace(/[A-Z]/g,(e,t)=>(t?`-`:``)+e.toLowerCase())}function g(e,t){let n;return(...r)=>{clearTimeout(n),n=setTimeout(()=>{e(...r)},t)}}function ee(e){let t=document.createElement(`template`);return e=e.replace(/<([A-Za-z0-9\-]*)([^>\/]*)(\/>)/gi,`<$1$2></$1>`),t.innerHTML=e,t.content.normalize(),Array.from(t.content.childNodes)}function _(e,t){let n=Object.getOwnPropertyDescriptors(e);for(let[e,r]of Object.entries(n))(`get`in r||`set`in r)&&Object.defineProperty(t,e,r)}var v=new WeakMap;function y(e,t){let n=v.get(e)??new Set;if(n.has(t))return e.adoptedStyleSheets[0];n.add(t),v.set(e,n);let r=new CSSStyleSheet;return r.replaceSync(t),e.adoptedStyleSheets.push(r),r}function b(e,t){let n=e.target,r=n.shadowRoot;!r&&Object.keys(t).some(e=>e.indexOf(`shadow`)===0)&&(r=n.attachShadow({mode:`open`})),Object.keys(t).forEach(i=>{switch(i){case`css`:y(document,t[i]);break;case`targetCss`:y(document,t[i]);break;case`shadowCss`:y(r,t[i]);break;case`shadowHtml`:r.innerHTML=t[i],S(r,e);break;case`targetHtml`:n.innerHTML=t[i],S(n,e);break}})}function x(e,t){let n=t=>{let n=t.match(/^([a-z]+)([A-Z][a-z]*)$/);return n?`./${e.replace(/-/g,`/`)}.${n[1]}.${n[2].toLowerCase()}`:`./${e.replace(/-/g,`/`)}.${t.toLowerCase()}`};return Promise.all(t.map(async e=>{let t=n(e);try{let n=await fetch(t);if(!n.ok)return`file not found: ${t}`;if(t.endsWith(`json`)){let r=await n.text();try{return JSON.parse(r)}catch{return console.error(`could not JSON.parse() asset '${e}' from ${t}:\n`,r),null}}return n.text()}catch{return`file not found: ${t}`}}))}function S(e,t){let n=[...e.querySelectorAll(`*`)];for(let e of n){if(!(e instanceof HTMLElement))continue;let n=[...e.attributes];for(let i of n){let n=i.name.toLowerCase();if(!(`on`+n in e))continue;let a=i.value.trim().split(`|`);if(a.length<2)continue;let[s,c,l]=a;e.removeAttribute(i.name);let u;if(l)try{u=JSON.parse(l)}catch{u=l}s===`this`||s===``?r(e,n,()=>t.trigger(c,u)):r(e,n,()=>o(s,c,u))}}}function C(e,t){if(typeof HTMLElement>`u`||!(e instanceof HTMLElement)||!(`_mc`in e))return!1;if(t===void 0)return!0;let n=e._mc;return typeof n!=`object`||!n?!1:t in n}function te(e,t,n={}){let r=e;`_mc`in e||(r._mc=new O(e));let i=r._mc;return t.length&&(i[t]=n,e instanceof HTMLElement&&!e.getAttribute(`data-mc`)&&e.setAttribute(`data-mc`,``)),r}function ne(e,t){if(!t)return;let n=e._mc;n&&(n[t]&&delete n[t],!Object.keys(n).length&&e instanceof HTMLElement&&e.hasAttribute(`data-mc`)&&e.removeAttribute(`data-mc`))}function w(e){queueMicrotask(()=>{e.trigger(`Init`,null)})}function T(e){let t=e.target,n=Object.getPrototypeOf(e),i=e,a=Object.getOwnPropertyNames(n).filter(e=>/^(on[A-Z]|one[A-Z])/.test(e)&&typeof n[e]==`function`);for(let e of a){let n=/^one/.test(e);r(t,e.replace(/^one?/,``),t=>i[e](t),n)}}function re(e){return Array.from(e.querySelectorAll(`[data-mc]:not(:scope [data-mc] [data-mc])`)).filter(e=>e instanceof HTMLElement&&`_mc`in e).map(e=>e._mc)}function E(e,t){let n=e.parentElement?.closest(`[data-mc]`);for(;n instanceof HTMLElement;){if(`_mc`in n){let e=n._mc;if(t===void 0||t in e)return e}n=n.parentElement?.closest(`[data-mc]`)}return null}var D=class{#e;static _assets={};static assets=[];static _assetsPromise;constructor(e){this.#e=new WeakRef(e||{});let t=this.constructor;this instanceof O&&this.constructor!==O&&T(this);let n=(t.assets??[]).filter(e=>!t._assets[e]);n.length&&!t._assetsPromise&&(t._assetsPromise=x(t._originalTag||(this instanceof O?this.target.tagName.toLowerCase():h(t.name)),n).then(e=>{let r={};return n.forEach((t,n)=>{r[t]=e[n]}),t._assets=Object.assign({},t._assets,r),r}))}get target(){return this.#e.deref()}get assets(){return this.constructor._assets||{}}},ie=class extends D{constructor(e){super(e)}},O=class e extends D{static tag;static attributes={};constructor(t){super(t);let n=this.constructor;if(n===e)return;let r=this.target.tagName.toLowerCase();if(n.tag!==void 0&&r!==n.tag&&n.tag!==``){let t=document.createElement(n.tag);t.classList.add(r),n._originalTag=r,e.add(t,n.name,new n(t)),delete n._originalTag,r.includes(`-`)&&!this.target.isConnected?this.on(`connected`,()=>this.target.replaceWith(t)):this.target.replaceWith(t);return}let i=n.attributes;if(n.attributes&&Object.keys(n.attributes).forEach(e=>{this.target.setAttribute(e,i[e])}),Object.keys(n._assets).length){b(this,n._assets);for(let e of[`css`,`targetCss`])delete n._assets[e];n.assets=n.assets.filter(e=>e!==`css`&&e!==`targetCss`)}if(n._assetsPromise?n._assetsPromise.then(e=>{b(this,e);for(let e of[`css`,`targetCss`])delete n._assets[e];n.assets=n.assets.filter(e=>e!==`css`&&e!==`targetCss`),w(this)}):w(this),n.tag===``){this.target.remove();return}}static add(e,t,n={}){return te(e,t,n)}static remove(e,t){ne(e,t)}static get(e,t,n){if(!C(e))return;let r=e._mc[t];return r instanceof n?r:void 0}off(e,t){n(this.target,e,t)}on(e,t){return r(this.target,e,t)}one(e,t){return i(this.target,e,t)}trigger(e,t={},n=`l`,r){o(this.target,e,t,n,r)}ancestors(e){let t=[],n=E(this.target,e);for(;n;)t.push(n),n=E(n.target,e);return t}closest(e){return E(this.target,e)}children(e){let t=re(this.target);return e===void 0?t:t.filter(t=>e in t)}descendants(e){return Array.from(this.target.querySelectorAll(`[data-mc]`)).filter(e=>e instanceof HTMLElement&&`_mc`in e).map(e=>e._mc).filter(t=>e===void 0||e in t)}};function k(e){return e.split(`-`).join(`/`)}function A(e){return`./`+k(e)+`.js`}var j=new Set;function M(e){return()=>{let t=Array.from(document.querySelectorAll(`[data-mc~="${e}"]`)).filter(e=>e instanceof HTMLElement);if(t.length===0)return;let n=customElements.get(e);for(let r of t){try{O.add(r,n.name,new n(r))}catch{if(`create`in n&&typeof n.create==`function`){let e=n.create(r);if(!C(e))throw`create() of Class ${n.name} did not return a McHost.`;e.setAttribute(`data-mc`,r.getAttribute(`data-mc`)??``),r.replaceWith(e),r=e}else throw`Class ${n.name} doesn't have a callable constructor AND class doesn't have a static .create() method.`}let t=r.getAttribute(`data-mc`).split(` `).filter(t=>t!==e&&t!==``).join(` `).trim();r.setAttribute(`data-mc`,t)}}}function N(e){if(j.has(e))return;j.add(e);let t=A(e),n=document.createElement(`script`);n.setAttribute(`src`,t),n.setAttribute(`blocking`,`render`),n.async=!0,n.setAttribute(`type`,`module`),n.setAttribute(`data-name`,e),n.setAttribute(`loading`,``),customElements.whenDefined(e).then(M(e)),n.addEventListener(`load`,t=>{j.delete(e),document.head.querySelector(`script[data-name="${e}"]`)?.remove()}),n.addEventListener(`error`,n=>{console.error(`could not load DC code from`,t),j.delete(e)}),document.head.append(n)}function P(){[...document.querySelectorAll(`:not(:defined)`)].filter(e=>!!e&&!customElements.get(e.tagName.toLowerCase())&&!j.has(e.tagName.toLowerCase())).forEach(e=>{N(e.tagName.toLowerCase())}),[...document.querySelectorAll(`[data-mc]:not([data-mc=""]`)].filter(e=>!!e).forEach(e=>{(e.getAttribute(`data-mc`)?.split(` `).filter(e=>e!==``))?.forEach(e=>{let t=customElements.get(e);!j.has(e)&&!t?N(e):t&&M(e)()})})}var F=null,I=null;function L(e=!0){return e?F?!0:(F=new MutationObserver(P),I=setTimeout(()=>{I=null,F&&(F.observe(document.body,{childList:!0,subtree:!0}),P())},5),window.autoload??=L,!0):(I&&=(clearTimeout(I),null),F&&=(F.disconnect(),null),j.clear(),!1)}L.state=j;function ae(e,t){return Object.defineProperty(t,"name",{value:e})}function oe(e){return class extends e{constructor(...e){super(...e)}}}function R(e,t,n=[],r){if(customElements.get(e)){console.warn(`IGNORED: Custom element "${e}" is already defined!`);return}if(!e.includes(`-`))throw Error(`createCustomElement: tagName must contain at least one hyphen ("-").`);let i=HTMLElement;r&&(i=document.createElement(r).constructor);let a=m(e);if(!(t.prototype instanceof D))return customElements.define(e,t),t;let o=ae(a,class extends oe(i){constructor(e){super(),r||this.attachInternals(),O.add(this,a,new t(this,e))}connectedCallback(){this.dispatchEvent(new CustomEvent(`_mcConnected`))}connectedMoveCallback(){this.dispatchEvent(new CustomEvent(`_mcConnectedMove`))}disconnectedCallback(){this.dispatchEvent(new CustomEvent(`_mcDisconnected`))}adoptedCallback(){this.dispatchEvent(new CustomEvent(`_mcAdopted`))}attributeChangedCallback(e,t,n){this.dispatchEvent(new CustomEvent(`_mcAttributeChanged`,{detail:{name:e,oldValue:t,newValue:n}}))}formResetCallback(){this.dispatchEvent(new CustomEvent(`_mcFormReset`))}static get observedAttributes(){return[...n]}});return r||(o.formAssociated=!0),customElements.define(e,o,{extends:r}),R.list||=[],R.list.push(e),o}function z(e){return customElements.get(e)}function B(){return R.list??[]}function V(e){let t=new Set,n=/\{[^\{\}]*\}/g;return(e.getAttributeNames?e.getAttributeNames():[]).filter(t=>e.getAttribute(t)?.match(n)).forEach(r=>{new Set(e.getAttribute(r)?.match(n)).forEach(e=>t.add(e.replace(/[\{\}]/g,``)))}),[...e.childNodes].filter(e=>e.nodeType===3&&(e.nodeValue||``).match(n)).forEach(e=>{(e?.nodeValue||``).match(n).forEach(e=>t.add(e.replace(/[\{\}]/g,``)))}),[...t]}var H=[`INPUT`,`SELECT`,`TEXTAREA`],U=class extends O{data={};#e=``;#t=!1;constructor(e){super(e),V(e).forEach(e=>this.data[e]=``),X(this),this.#r()}#n(e,t,n){t.replace(/[\{\}]/g,``);let r=t.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`);return e.replace(new RegExp(r,`g`),n)}#r(){let e=this.target;(e.getAttributeNames?e.getAttributeNames():[]).filter(t=>e.getAttribute(t)?.match(/\{[^\{\}]*\}/g)).forEach(t=>{let n=new Set(e.getAttribute(t)?.match(/\{[^\{\}]*\}/g)),r=e.getAttribute(t)??``;this.data.subscribe((r=>i=>{let a=r;n.forEach(e=>{a=this.#n(a,e,String(i[e.replace(/[\{\}]/g,``)]))}),e.setAttribute(t,a)})(r))});let t=[...e.childNodes].filter(e=>e.nodeType===3&&(e.nodeValue||``).match(/\{[^\{\}]*\}/g));t.length&&(this.#e=e.getAttribute(`style`)||``,t.forEach(t=>{let n=t.nodeValue??``,r=new Set(n.match(/\{[^\{\}]*\}/g));this.data.subscribe((n=>i=>{let a=n;r.forEach(e=>{a=this.#n(a,e,String(i[e.replace(/[\{\}]/g,``)]))}),this.#t||(this.#t=!0,this.#e?e.style=this.#e:e.removeAttribute(`style`)),t.nodeValue=a})(n)),!H.includes(e.tagName)&&e.tagName!==`FIELDSET`&&(e.style.display=`none`)}))}onData(e){let t={};Object.keys(e.detail.data).forEach(n=>{this.data.hasOwnProperty(n)&&(t[n]=e.detail.data[n])}),JSON.stringify(t)!==JSON.stringify(this.data)&&Object.assign(this.data,t)}};function W(e){return[e,...e.querySelectorAll(`*`)].filter(e=>!!V(e).length).filter(e=>e?.tagName!==`STYLE`).reverse()}function G(e){W(e).forEach(e=>{e?._mc?._placeholders||O.add(e,`_placeholders`,new U(e))})}function K(e){return typeof e==`object`&&!!e}var q={};function se(e){let t=e.constructor,n=t.name,r=class extends t{#e=!0;#t=[];constructor(e){super(e);let t=this.constructor;t.callbacks=[],K(e)&&Object.getOwnPropertyNames(e).forEach(t=>{this[t]=e[t]})}get callbacks(){return this.#t}set callbacks(e){this.#t=e}[Symbol.toPrimitive](e){let t=this.valueOf();return e===`string`?String(t):e===`number`?Number(t):t}subscribe(e){return this.#t.push(e),this}unsubscribe(e){return this.#t=this.#t.filter(t=>t!==e),this}bind(e){if(!s())return this;if(K(this.valueOf())){G(e);let t=[e,...e.querySelectorAll(`[data-mc]`)].filter(e=>!!e?._mc?._placeholders);this.#t.push(function(e){let n=u(e);t.forEach(e=>{e&&o(e,`data`,n)})}),this.notify()}else console.warn(`IGNORED - cannot bind a non-Object to the dom, IS:`,typeof this.valueOf(),this);return this}notify(e){let t=K(this.valueOf())&&!(this.valueOf()instanceof Array)?structuredClone(Object.assign({},this)):this.valueOf();return e!==void 0&&(this.#e=e),this.#e&&this.#t.forEach(e=>{e(t)}),this}};return q[n]=r,r}function J(e){return q[e.constructor.name]||se(e)}function Y(e){return(typeof e==`object`||typeof e==`function`)&&e!==null&&`subscribe`in e}function X(e,t){let n=e;return(t?[t]:[...Object.getOwnPropertyNames(e)]).forEach(t=>{let r=n[t];if(Object.prototype.hasOwnProperty.call(e,t)&&typeof r!=`function`&&!Y(r)){if(t[0]===`_`||r===void 0)return;let i=r,a=new(J(i))(i),o=!1,s=function(){o||(o=!0,queueMicrotask(function(){o=!1,a.notify()}))};Object.defineProperty(e,t,{enumerable:!0,get(){if(K(a.valueOf())){let e=structuredClone(Object.assign({},a));queueMicrotask(function(){d(structuredClone(Object.assign({},a)),e)||s()})}return a},set(e){if(typeof e==typeof a.valueOf())if(K(a.valueOf())&&!(a.valueOf()instanceof Array))Y(a)||(a=new(J(e))(structuredClone(Object.assign({},e))),_(e,a)),Object.assign(a,e),a.notify();else{let t=a.valueOf(),n=a.callbacks;e!==t&&(a=new(J(e))(e),a.callbacks=n,s())}else console.warn(`IGNORED - cannot change observable types, IS:`,typeof a.valueOf(),a,`- CANNOT BECOME:`,typeof e)}}),n[t]=i}}),e}function Z(e){let t=Object.getPrototypeOf(e.prototype);function n(e,t){let n=e[t];Y(n)||X(e,t)}Object.hasOwn(t,`notify`)||Object.assign(t,{notify:function(e){return n(this,e),this[e].notify(),this},subscribe:function(e,t){return n(this,e),this[e].subscribe(t),this},bind:function(e,t){return n(this,e),this[e].bind(t),this}})}Z(O);var ce=e=>e.tagName.toLowerCase(),Q=class{constructor(e){}},le=class extends Q{#e;constructor(e){super(e),this.#e=e}get val(){return this.#e.form?new FormData(this.#e.form).get(this.#e.name)??null:null}set val(e){let t=[...this.#e.form?.querySelectorAll(`input[type="radio"][name="${this.#e.name}"]`)??[]].filter(t=>t.getAttribute(`value`)===e).shift();t&&(t.checked=!0)}},ue=class extends Q{#e;constructor(e){super(e),this.#e=e}get val(){return this.#e.checked}set val(e){this.#e.checked=e}},de=class extends Q{#e;constructor(e){super(e),this.#e=e}get val(){return this.#e.value}set val(e){this.#e.value=e}},fe=class extends Q{#e;#t=!1;constructor(e){super(e),this.#e=e,this.#t=e.multiple}get val(){let e=[];return[...this.#e.querySelectorAll(`option`)].filter(e=>e.selected).forEach(t=>{e.push(t.value)}),this.#t?e:e[0]}set val(e){e=typeof e==`string`?[e]:e,[...this.#e.querySelectorAll(`option`)].forEach(t=>{e.indexOf(t.value)>-1?t.selected=!0:t.selected=!1})}},pe=class extends Q{#e;constructor(e){super(e),this.#e=e}get val(){return this.#e.value||``}set val(e){this.#e.value=e}},me=e=>{let t=[...(e||document).querySelectorAll(`input,select,textarea`)],n={};return t.forEach(e=>{let t=e.getAttribute(`name`);if(t){let r=!1;switch(ce(e)){case`input`:switch(e.type){case`radio`:r=new le(e);break;case`checkbox`:r=new ue(e);break;default:r=new de(e)}break;case`select`:r=new fe(e);break;case`textarea`:r=new pe(e);break}if(r){let i=r;O.add(e,t,i),Object.defineProperty(n,t,{enumerable:!0,configurable:!0,get:()=>i.val,set:e=>{i.val!==e&&(i.val=e)}})}}}),n};function $(e,t,n){if(!s())return;let r=me(n),i=e;i[t]=Object.assign({},r),X(e,t);let a=i[t];_(r,a),[...n.querySelectorAll(`input, textarea, select`)].forEach(e=>{e.addEventListener(`input`,()=>{a.notify()})})}function he(e){let t=Object.getPrototypeOf(e.prototype);Object.hasOwn(t,`formValues`)||Object.assign(t,{formValues:function(e,t){return $(this,t,e),this}})}export{a as BubbleController,O as DC,ie as MC,D as McBase,L as autoload,S as bindTemplateEvents,_ as copyGettersSetters,g as debounce,d as deepEqual,R as defineCE,u as flattenObject,he as formValuePlugin,$ as formValues,z as getCE,e as getMcEventName,ee as htmlToElements,y as injectCSS,Y as isAlreadyObserved,s as isBrowser,C as isMcHost,c as isNativeObject,m as kebabToPascal,B as listCE,x as loadAssets,f as nameSpace,X as observe,Z as observePlugin,n as off,r as on,i as one,p as parse,h as pascalToKebab,b as processAssets,l as sortObject,o as trigger,t as trueEventName};
|
|
1
|
+
function e(e){return`_mc`+e[0].toUpperCase()+e.substring(1)}function t(e,t){let n=t.toLowerCase();return`on`+n in e?n:`_mc`+t[0].toUpperCase()+t.substring(1)}function n(e,r,i){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>n(e,r,i));return}e.removeEventListener(t(e,r),i)}}function r(e,n,i,a=!1){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>r(e,n,i));return}return e.addEventListener(t(e,n),i,{capture:!1,once:a}),i}}function i(e,t,n){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>i(e,t,n));return}return r(e,t,n,!0),n}}var a=class{stopped=!1;constructor(){}stop(){this.stopped=!0}};function o(e,n,r={},i=`l`,s){if(!e)return;if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>o(e,n,r,i));return}let c=/^_mc/.test(n)?n:t(e,n),l=s||new a,u=new CustomEvent(c,{detail:{data:r,control:l},bubbles:!1});l.stopped||/l/.test(i)&&(e.dispatchEvent(u),l.stopped)||/[ud]/.test(i)&&queueMicrotask(()=>{if(i.indexOf(`u`)>-1&&o(e.parentElement,c,r,`lu`,l),i.indexOf(`d`)>-1)for(let t of e.children)o(t,c,r,`ld`)})}function s(){return typeof window<`u`&&window.document!==void 0}function c(e){return typeof e==`object`&&!!e&&e.constructor===Object}function l(e){let t={},n=Object.keys(e).sort();for(let r=0;r<n.length;r++){let i=n[r],a=e[i];c(a)?t[i]=l(a):t[i]=a}return t}function u(e,t=!0){let n={},r=e,i=Object.getOwnPropertyNames(e);for(let e=0;e<i.length;e++){let t=i[e],a=r[t];if(a instanceof Object){let e=u(a,!1);for(let r in e)n[t+`.`+r]=e[r]}else n[t]=a}return t?l(n):n}function d(e,t){if(e===t||e!==e&&t!==t)return!0;if(typeof e!=`object`||!e||typeof t!=`object`||!t||e.constructor!==t.constructor)return!1;if(e instanceof Date)return e.getTime()===t.getTime();if(e instanceof RegExp)return e.toString()===t.toString();if(Array.isArray(e)){let n=t;if(e.length!==n.length)return!1;for(let t=0;t<e.length;t++)if(!d(e[t],n[t]))return!1;return!0}let n=e,r=t,i=Object.keys(n);if(i.length!==Object.keys(r).length)return!1;for(let e=0;e<i.length;e++){let t=i[e];if(!Object.prototype.hasOwnProperty.call(r,t)||!d(n[t],r[t]))return!1}return!0}function f(e,t={}){let n=t,r=typeof e==`string`?e.split(`.`):e;if(!n||!r[0]||!(r[0]in n))return;let i=n[r[0]];return r.length>1?c(i)?f(r.slice(1),i):void 0:i}function p(e,t,...n){let r=[e,t,...n];if(r.length>2){for(;r.length>1;)r[0]=p(r[0],r[1]),r.splice(1,1);return r[0]}if(typeof e==`string`){let n=e;return e.match(/\{[^\{\}]*\}/g)?.forEach(e=>{let r=f(e.substring(1,e.length-1),t);r!==void 0&&(n=n.replace(e,String(r)))}),n}else if(Array.isArray(e))return e.map(e=>p(e,t));else if(c(e)){let n=e;Object.keys(n).forEach(e=>{Object.prototype.hasOwnProperty.call(n,e)&&(n[e]=p(n[e],t))})}return e}function m(e){return e.split(`-`).map(e=>e.charAt(0).toUpperCase()+e.slice(1)).join(``)}function h(e){return e.replace(/[A-Z]/g,(e,t)=>(t?`-`:``)+e.toLowerCase())}function g(e,t){let n;return(...r)=>{clearTimeout(n),n=setTimeout(()=>{e(...r)},t)}}function ee(e){let t=document.createElement(`template`);return e=e.replace(/<([A-Za-z0-9\-]*)([^>\/]*)(\/>)/gi,`<$1$2></$1>`),t.innerHTML=e,t.content.normalize(),Array.from(t.content.childNodes)}function _(e,t){let n=Object.getOwnPropertyDescriptors(e);for(let[e,r]of Object.entries(n))(`get`in r||`set`in r)&&Object.defineProperty(t,e,r)}var v=new WeakMap;function y(e,t){let n=v.get(e)??new Set;if(n.has(t))return e.adoptedStyleSheets[0];n.add(t),v.set(e,n);let r=new CSSStyleSheet;return r.replaceSync(t),e.adoptedStyleSheets.push(r),r}function b(e,t){let n=e.target,r=n.shadowRoot;!r&&Object.keys(t).some(e=>e.indexOf(`shadow`)===0)&&(r=n.attachShadow({mode:`open`})),Object.keys(t).forEach(i=>{switch(i){case`css`:y(document,t[i]);break;case`targetCss`:y(document,t[i]);break;case`shadowCss`:y(r,t[i]);break;case`shadowHtml`:r.innerHTML=t[i],S(r,e);break;case`targetHtml`:n.innerHTML=t[i],S(n,e);break}})}function x(e,t){let n=t=>{let n=t.match(/^([a-z]+)([A-Z][a-z]*)$/);return n?`./${e.replace(/-/g,`/`)}.${n[1]}.${n[2].toLowerCase()}`:`./${e.replace(/-/g,`/`)}.${t.toLowerCase()}`};return Promise.all(t.map(async e=>{let t=n(e);try{let n=await fetch(t);if(!n.ok)return`file not found: ${t}`;if(t.endsWith(`json`)){let r=await n.text();try{return JSON.parse(r)}catch{return console.error(`could not JSON.parse() asset '${e}' from ${t}:\n`,r),null}}return n.text()}catch{return`file not found: ${t}`}}))}function S(e,t){let n=[...e.querySelectorAll(`*`)];for(let e of n){if(!(e instanceof HTMLElement))continue;let n=[...e.attributes];for(let i of n){let n=i.name.toLowerCase();if(!(`on`+n in e))continue;let a=i.value.trim().split(`|`);if(a.length<2)continue;let[s,c,l]=a;e.removeAttribute(i.name);let u;if(l)try{u=JSON.parse(l)}catch{u=l}s===`this`||s===``?r(e,n,()=>t.trigger(c,u)):r(e,n,()=>o(s,c,u))}}}function C(e,t){if(typeof HTMLElement>`u`||!(e instanceof HTMLElement)||!(`_mc`in e))return!1;if(t===void 0)return!0;let n=e._mc;return typeof n!=`object`||!n?!1:t in n}function w(e,t,n={}){let r=e;`_mc`in e||(r._mc=new k(e));let i=r._mc;return t.length&&(i[t]=n,e instanceof HTMLElement&&!e.getAttribute(`data-mc`)&&e.setAttribute(`data-mc`,``)),r}function te(e,t){if(!t)return;let n=e._mc;n&&(n[t]&&delete n[t],!Object.keys(n).length&&e instanceof HTMLElement&&e.hasAttribute(`data-mc`)&&e.removeAttribute(`data-mc`))}function T(e){queueMicrotask(()=>{e.trigger(`Init`,null)})}function E(e){let t=e.target,n=Object.getPrototypeOf(e),i=e,a=Object.getOwnPropertyNames(n).filter(e=>/^(on[A-Z]|one[A-Z])/.test(e)&&typeof n[e]==`function`);for(let e of a){let n=/^one/.test(e);r(t,e.replace(/^one?/,``),t=>i[e](t),n)}}function ne(e){return Array.from(e.querySelectorAll(`[data-mc]:not(:scope [data-mc] [data-mc])`)).filter(e=>e instanceof HTMLElement&&`_mc`in e).map(e=>e._mc)}function D(e,t){let n=e.parentElement?.closest(`[data-mc]`);for(;n instanceof HTMLElement;){if(`_mc`in n){let e=n._mc;if(t===void 0||t in e)return e}n=n.parentElement?.closest(`[data-mc]`)}return null}var O=class{#e;static _assets={};static assets=[];static _assetsPromise;constructor(e){this.#e=new WeakRef(e||{});let t=this.constructor;this instanceof k&&this.constructor!==k&&E(this);let n=(t.assets??[]).filter(e=>!t._assets[e]);n.length&&!t._assetsPromise&&(t._assetsPromise=x(t._originalTag||(this instanceof k?this.target.tagName.toLowerCase():h(t.name)),n).then(e=>{let r={};return n.forEach((t,n)=>{r[t]=e[n]}),t._assets=Object.assign({},t._assets,r),r}))}get target(){return this.#e.deref()}get assets(){return this.constructor._assets||{}}},re=class extends O{constructor(e){super(e)}},k=class e extends O{static tag;static attributes={};constructor(t){super(t);let n=this.constructor;if(n===e)return;let r=this.target.tagName.toLowerCase();if(n.tag!==void 0&&r!==n.tag&&n.tag!==``){let t=document.createElement(n.tag);t.classList.add(r),n._originalTag=r,e.add(t,n.name,new n(t)),delete n._originalTag,r.includes(`-`)&&!this.target.isConnected?this.on(`connected`,()=>this.target.replaceWith(t)):this.target.replaceWith(t);return}let i=n.attributes;if(n.attributes&&Object.keys(n.attributes).forEach(e=>{this.target.setAttribute(e,i[e])}),Object.keys(n._assets).length){b(this,n._assets);for(let e of[`css`,`targetCss`])delete n._assets[e];n.assets=n.assets.filter(e=>e!==`css`&&e!==`targetCss`)}if(n._assetsPromise?n._assetsPromise.then(e=>{b(this,e);for(let e of[`css`,`targetCss`])delete n._assets[e];n.assets=n.assets.filter(e=>e!==`css`&&e!==`targetCss`),T(this)}):T(this),n.tag===``){this.target.remove();return}}static add(e,t,n={}){return w(e,t,n)}static remove(e,t){te(e,t)}static get(e,t,n){if(!C(e))return;let r=e._mc[t];return r instanceof n?r:void 0}off(e,t){n(this.target,e,t)}on(e,t){return r(this.target,e,t)}one(e,t){return i(this.target,e,t)}trigger(e,t={},n=`l`,r){o(this.target,e,t,n,r)}ancestors(e){let t=[],n=D(this.target,e);for(;n;)t.push(n),n=D(n.target,e);return t}closest(e){return D(this.target,e)}children(e){let t=ne(this.target);return e===void 0?t:t.filter(t=>e in t)}descendants(e){return Array.from(this.target.querySelectorAll(`[data-mc]`)).filter(e=>e instanceof HTMLElement&&`_mc`in e).map(e=>e._mc).filter(t=>e===void 0||e in t)}};function A(e){return e.split(`-`).join(`/`)}function j(e){return`./`+A(e)+`.js`}var M=new Set;function N(e){return()=>{let t=Array.from(document.querySelectorAll(`[data-mc~="${e}"]`)).filter(e=>e instanceof HTMLElement);if(t.length===0)return;let n=customElements.get(e);for(let r of t){try{k.add(r,n.name,new n(r))}catch{if(`create`in n&&typeof n.create==`function`){let e=n.create(r);if(!C(e))throw`create() of Class ${n.name} did not return a McHost.`;e.setAttribute(`data-mc`,r.getAttribute(`data-mc`)??``),r.replaceWith(e),r=e}else throw`Class ${n.name} doesn't have a callable constructor AND class doesn't have a static .create() method.`}let t=r.getAttribute(`data-mc`).split(` `).filter(t=>t!==e&&t!==``).join(` `).trim();r.setAttribute(`data-mc`,t)}}}function P(e){if(M.has(e))return;M.add(e);let t=j(e),n=document.createElement(`script`);n.setAttribute(`src`,t),n.setAttribute(`blocking`,`render`),n.async=!0,n.setAttribute(`type`,`module`),n.setAttribute(`data-name`,e),n.setAttribute(`loading`,``),customElements.whenDefined(e).then(N(e)),n.addEventListener(`load`,t=>{M.delete(e),document.head.querySelector(`script[data-name="${e}"]`)?.remove()}),n.addEventListener(`error`,n=>{console.error(`could not load DC code from`,t),M.delete(e)}),document.head.append(n)}function F(){[...document.querySelectorAll(`:not(:defined)`)].filter(e=>!!e&&!customElements.get(e.tagName.toLowerCase())&&!M.has(e.tagName.toLowerCase())).forEach(e=>{P(e.tagName.toLowerCase())}),[...document.querySelectorAll(`[data-mc]:not([data-mc=""]`)].filter(e=>!!e).forEach(e=>{(e.getAttribute(`data-mc`)?.split(` `).filter(e=>e!==``))?.forEach(e=>{let t=customElements.get(e);!M.has(e)&&!t?P(e):t&&N(e)()})})}var I=null,L=null;function R(e=!0){return e?I?!0:(I=new MutationObserver(F),L=setTimeout(()=>{L=null,I&&(I.observe(document.body,{childList:!0,subtree:!0}),F())},5),window.autoload??=R,!0):(L&&=(clearTimeout(L),null),I&&=(I.disconnect(),null),M.clear(),!1)}R.state=M;function ie(e,t){return Object.defineProperty(t,"name",{value:e})}function ae(e){return class extends e{constructor(...e){super(...e)}}}function z(e,t,n=[],r){if(customElements.get(e)){console.warn(`IGNORED: Custom element "${e}" is already defined!`);return}if(!e.includes(`-`))throw Error(`createCustomElement: tagName must contain at least one hyphen ("-").`);let i=HTMLElement;r&&(i=document.createElement(r).constructor);let a=m(e);if(!(t.prototype instanceof O))return customElements.define(e,t),t;let o=ie(a,class extends ae(i){constructor(e){super(),r||this.attachInternals(),k.add(this,a,new t(this,e))}connectedCallback(){this.dispatchEvent(new CustomEvent(`_mcConnected`))}connectedMoveCallback(){this.dispatchEvent(new CustomEvent(`_mcConnectedMove`))}disconnectedCallback(){this.dispatchEvent(new CustomEvent(`_mcDisconnected`))}adoptedCallback(){this.dispatchEvent(new CustomEvent(`_mcAdopted`))}attributeChangedCallback(e,t,n){this.dispatchEvent(new CustomEvent(`_mcAttributeChanged`,{detail:{name:e,oldValue:t,newValue:n}}))}formResetCallback(){this.dispatchEvent(new CustomEvent(`_mcFormReset`))}static get observedAttributes(){return[...n]}});return r||(o.formAssociated=!0),customElements.define(e,o,{extends:r}),z.list||=[],z.list.push(e),o}function oe(e){return customElements.get(e)}function B(){return z.list??[]}function V(e){let t=new Set,n=/\{[^\{\}]*\}/g;return(e.getAttributeNames?e.getAttributeNames():[]).filter(t=>e.getAttribute(t)?.match(n)).forEach(r=>{new Set(e.getAttribute(r)?.match(n)).forEach(e=>t.add(e.replace(/[\{\}]/g,``)))}),[...e.childNodes].filter(e=>e.nodeType===3&&(e.nodeValue||``).match(n)).forEach(e=>{(e?.nodeValue||``).match(n).forEach(e=>t.add(e.replace(/[\{\}]/g,``)))}),[...t]}var H=[`INPUT`,`SELECT`,`TEXTAREA`],U=class extends k{data={};#e=``;#t=!1;constructor(e){super(e),V(e).forEach(e=>this.data[e]=``),X(this),this.#r()}#n(e,t,n){t.replace(/[\{\}]/g,``);let r=t.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`);return e.replace(new RegExp(r,`g`),n)}#r(){let e=this.target;(e.getAttributeNames?e.getAttributeNames():[]).filter(t=>e.getAttribute(t)?.match(/\{[^\{\}]*\}/g)).forEach(t=>{let n=new Set(e.getAttribute(t)?.match(/\{[^\{\}]*\}/g)),r=e.getAttribute(t)??``;this.data.subscribe((r=>i=>{let a=r;n.forEach(e=>{a=this.#n(a,e,String(i[e.replace(/[\{\}]/g,``)]))}),e.setAttribute(t,a)})(r))});let t=[...e.childNodes].filter(e=>e.nodeType===3&&(e.nodeValue||``).match(/\{[^\{\}]*\}/g));t.length&&(this.#e=e.getAttribute(`style`)||``,t.forEach(t=>{let n=t.nodeValue??``,r=new Set(n.match(/\{[^\{\}]*\}/g));this.data.subscribe((n=>i=>{let a=n;r.forEach(e=>{a=this.#n(a,e,String(i[e.replace(/[\{\}]/g,``)]))}),this.#t||(this.#t=!0,this.#e?e.style=this.#e:e.removeAttribute(`style`)),t.nodeValue=a})(n)),!H.includes(e.tagName)&&e.tagName!==`FIELDSET`&&(e.style.display=`none`)}))}onData(e){let t={};Object.keys(e.detail.data).forEach(n=>{this.data.hasOwnProperty(n)&&(t[n]=e.detail.data[n])}),JSON.stringify(t)!==JSON.stringify(this.data)&&Object.assign(this.data,t)}};function W(e){return[e,...e.querySelectorAll(`*`)].filter(e=>!!V(e).length).filter(e=>e?.tagName!==`STYLE`).reverse()}function G(e){W(e).forEach(e=>{e?._mc?._placeholders||k.add(e,`_placeholders`,new U(e))})}function K(e){return typeof e==`object`&&!!e}var q={};function se(e){let t=e.constructor,n=t.name,r=class extends t{#e=!0;#t=[];constructor(e){super(e);let t=this.constructor;t.callbacks=[],K(e)&&Object.getOwnPropertyNames(e).forEach(t=>{this[t]=e[t]})}get callbacks(){return this.#t}set callbacks(e){this.#t=e}[Symbol.toPrimitive](e){let t=this.valueOf();return e===`string`?String(t):e===`number`?Number(t):t}subscribe(e){return this.#t.push(e),this}unsubscribe(e){return this.#t=this.#t.filter(t=>t!==e),this}bind(e){if(!s())return this;if(K(this.valueOf())){G(e);let t=[e,...e.querySelectorAll(`[data-mc]`)].filter(e=>!!e?._mc?._placeholders);this.#t.push(function(e){let n=u(e);t.forEach(e=>{e&&o(e,`data`,n)})}),this.notify()}else console.warn(`IGNORED - cannot bind a non-Object to the dom, IS:`,typeof this.valueOf(),this);return this}notify(e){let t=K(this.valueOf())&&!(this.valueOf()instanceof Array)?structuredClone(Object.assign({},this)):this.valueOf();return e!==void 0&&(this.#e=e),this.#e&&this.#t.forEach(e=>{e(t)}),this}};return q[n]=r,r}function J(e){return q[e.constructor.name]||se(e)}function Y(e){return(typeof e==`object`||typeof e==`function`)&&e!==null&&`subscribe`in e}function X(e,t){let n=e;return(t?[t]:[...Object.getOwnPropertyNames(e)]).forEach(t=>{let r=n[t];if(Object.prototype.hasOwnProperty.call(e,t)&&typeof r!=`function`&&!Y(r)){if(t[0]===`_`||r===void 0)return;let i=r,a=new(J(i))(i),o=!1,s=function(){o||(o=!0,queueMicrotask(function(){o=!1,a.notify()}))};Object.defineProperty(e,t,{enumerable:!0,get(){if(K(a.valueOf())){let e=structuredClone(Object.assign({},a));queueMicrotask(function(){d(structuredClone(Object.assign({},a)),e)||s()})}return a},set(e){if(typeof e==typeof a.valueOf())if(K(a.valueOf())&&!(a.valueOf()instanceof Array))Y(a)||(a=new(J(e))(structuredClone(Object.assign({},e))),_(e,a)),Object.assign(a,e),a.notify();else{let t=a.valueOf(),n=a.callbacks;e!==t&&(a=new(J(e))(e),a.callbacks=n,s())}else console.warn(`IGNORED - cannot change observable types, IS:`,typeof a.valueOf(),a,`- CANNOT BECOME:`,typeof e)}}),n[t]=i}}),e}function Z(e){let t=Object.getPrototypeOf(e.prototype);function n(e,t){let n=e[t];Y(n)||X(e,t)}Object.hasOwn(t,`notify`)||Object.assign(t,{notify:function(e){return n(this,e),this[e].notify(),this},subscribe:function(e,t){return n(this,e),this[e].subscribe(t),this},bind:function(e,t){return n(this,e),this[e].bind(t),this}})}Z(k);var ce=e=>e.tagName.toLowerCase(),Q=class{constructor(e){}},le=class extends Q{#e;constructor(e){super(e),this.#e=e}get val(){return this.#e.form?new FormData(this.#e.form).get(this.#e.name)??null:null}set val(e){let t=[...this.#e.form?.querySelectorAll(`input[type="radio"][name="${this.#e.name}"]`)??[]].filter(t=>t.getAttribute(`value`)===e).shift();t&&(t.checked=!0)}},ue=class extends Q{#e;constructor(e){super(e),this.#e=e}get val(){return this.#e.checked}set val(e){this.#e.checked=e}},de=class extends Q{#e;constructor(e){super(e),this.#e=e}get val(){return this.#e.value}set val(e){this.#e.value=e}},fe=class extends Q{#e;#t=!1;constructor(e){super(e),this.#e=e,this.#t=e.multiple}get val(){let e=[];return[...this.#e.querySelectorAll(`option`)].filter(e=>e.selected).forEach(t=>{e.push(t.value)}),this.#t?e:e[0]}set val(e){e=typeof e==`string`?[e]:e,[...this.#e.querySelectorAll(`option`)].forEach(t=>{e.indexOf(t.value)>-1?t.selected=!0:t.selected=!1})}},pe=class extends Q{#e;constructor(e){super(e),this.#e=e}get val(){return this.#e.value||``}set val(e){this.#e.value=e}},me=e=>{let t=[...(e||document).querySelectorAll(`input,select,textarea`)],n={};return t.forEach(e=>{let t=e.getAttribute(`name`);if(t){let r=!1;switch(ce(e)){case`input`:switch(e.type){case`radio`:r=new le(e);break;case`checkbox`:r=new ue(e);break;default:r=new de(e)}break;case`select`:r=new fe(e);break;case`textarea`:r=new pe(e);break}if(r){let i=r;k.add(e,t,i),Object.defineProperty(n,t,{enumerable:!0,configurable:!0,get:()=>i.val,set:e=>{i.val!==e&&(i.val=e)}})}}}),n};function $(e,t,n){if(!s())return;let r=me(n),i=e;i[t]=Object.assign({},r),X(e,t);let a=i[t];_(r,a),[...n.querySelectorAll(`input, textarea, select`)].forEach(e=>{e.addEventListener(`input`,()=>{a.notify()})})}function he(e){let t=Object.getPrototypeOf(e.prototype);Object.hasOwn(t,`formValues`)||Object.assign(t,{formValues:function(e,t){return $(this,t,e),this}})}export{a as BubbleController,k as DC,re as MC,O as McBase,R as autoload,S as bindTemplateEvents,_ as copyGettersSetters,g as debounce,d as deepEqual,z as defineCE,u as flattenObject,he as formValuePlugin,$ as formValues,oe as getCE,e as getMcEventName,ee as htmlToElements,y as injectCSS,Y as isAlreadyObserved,s as isBrowser,C as isMcHost,c as isNativeObject,m as kebabToPascal,B as listCE,x as loadAssets,G as makePlaceholders,f as nameSpace,X as observe,Z as observePlugin,n as off,r as on,i as one,p as parse,h as pascalToKebab,b as processAssets,l as sortObject,o as trigger,t as trueEventName};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(e,t){let n=t.toLowerCase();return`on`+n in e?n:`_mc`+t[0].toUpperCase()+t.substring(1)}function t(n,r,i){if(n){if(typeof n==`string`){document.querySelectorAll(n).forEach(e=>t(e,r,i));return}n.removeEventListener(e(n,r),i)}}function n(t,r,i,a=!1){if(t){if(typeof t==`string`){document.querySelectorAll(t).forEach(e=>n(e,r,i));return}return t.addEventListener(e(t,r),i,{capture:!1,once:a}),i}}function r(e,t,i){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>r(e,t,i));return}return n(e,t,i,!0),i}}var i=class{stopped=!1;constructor(){}stop(){this.stopped=!0}};function a(t,n,r={},o=`l`,s){if(!t)return;if(typeof t==`string`){document.querySelectorAll(t).forEach(e=>a(e,n,r,o));return}let c=/^_mc/.test(n)?n:e(t,n),l=s||new i,u=new CustomEvent(c,{detail:{data:r,control:l},bubbles:!1});l.stopped||/l/.test(o)&&(t.dispatchEvent(u),l.stopped)||/[ud]/.test(o)&&queueMicrotask(()=>{if(o.indexOf(`u`)>-1&&a(t.parentElement,c,r,`lu`,l),o.indexOf(`d`)>-1)for(let e of t.children)a(e,c,r,`ld`)})}function o(){return typeof window<`u`&&window.document!==void 0}function s(e){return typeof e==`object`&&!!e&&e.constructor===Object}function c(e){let t={},n=Object.keys(e).sort();for(let r=0;r<n.length;r++){let i=n[r],a=e[i];s(a)?t[i]=c(a):t[i]=a}return t}function l(e,t=!0){let n={},r=e,i=Object.getOwnPropertyNames(e);for(let e=0;e<i.length;e++){let t=i[e],a=r[t];if(a instanceof Object){let e=l(a,!1);for(let r in e)n[t+`.`+r]=e[r]}else n[t]=a}return t?c(n):n}function u(e,t){if(e===t||e!==e&&t!==t)return!0;if(typeof e!=`object`||!e||typeof t!=`object`||!t||e.constructor!==t.constructor)return!1;if(e instanceof Date)return e.getTime()===t.getTime();if(e instanceof RegExp)return e.toString()===t.toString();if(Array.isArray(e)){let n=t;if(e.length!==n.length)return!1;for(let t=0;t<e.length;t++)if(!u(e[t],n[t]))return!1;return!0}let n=e,r=t,i=Object.keys(n);if(i.length!==Object.keys(r).length)return!1;for(let e=0;e<i.length;e++){let t=i[e];if(!Object.prototype.hasOwnProperty.call(r,t)||!u(n[t],r[t]))return!1}return!0}function d(e){return e.replace(/[A-Z]/g,(e,t)=>(t?`-`:``)+e.toLowerCase())}function f(e,t){let n=Object.getOwnPropertyDescriptors(e);for(let[e,r]of Object.entries(n))(`get`in r||`set`in r)&&Object.defineProperty(t,e,r)}var p=new WeakMap;function m(e,t){let n=p.get(e)??new Set;if(n.has(t))return e.adoptedStyleSheets[0];n.add(t),p.set(e,n);let r=new CSSStyleSheet;return r.replaceSync(t),e.adoptedStyleSheets.push(r),r}function h(e,t){let n=e.target,r=n.shadowRoot;!r&&Object.keys(t).some(e=>e.indexOf(`shadow`)===0)&&(r=n.attachShadow({mode:`open`})),Object.keys(t).forEach(i=>{switch(i){case`css`:m(document,t[i]);break;case`targetCss`:m(document,t[i]);break;case`shadowCss`:m(r,t[i]);break;case`shadowHtml`:r.innerHTML=t[i],_(r,e);break;case`targetHtml`:n.innerHTML=t[i],_(n,e);break}})}function g(e,t){let n=t=>{let n=t.match(/^([a-z]+)([A-Z][a-z]*)$/);return n?`./${e.replace(/-/g,`/`)}.${n[1]}.${n[2].toLowerCase()}`:`./${e.replace(/-/g,`/`)}.${t.toLowerCase()}`};return Promise.all(t.map(async e=>{let t=n(e);try{let n=await fetch(t);if(!n.ok)return`file not found: ${t}`;if(t.endsWith(`json`)){let r=await n.text();try{return JSON.parse(r)}catch{return console.error(`could not JSON.parse() asset '${e}' from ${t}:\n`,r),null}}return n.text()}catch{return`file not found: ${t}`}}))}function _(e,t){let r=[...e.querySelectorAll(`*`)];for(let e of r){if(!(e instanceof HTMLElement))continue;let r=[...e.attributes];for(let i of r){let r=i.name.toLowerCase();if(!(`on`+r in e))continue;let o=i.value.trim().split(`|`);if(o.length<2)continue;let[s,c,l]=o;e.removeAttribute(i.name);let u;if(l)try{u=JSON.parse(l)}catch{u=l}s===`this`||s===``?n(e,r,()=>t.trigger(c,u)):n(e,r,()=>a(s,c,u))}}}function v(e,t){if(typeof HTMLElement>`u`||!(e instanceof HTMLElement)||!(`_mc`in e))return!1;if(t===void 0)return!0;let n=e._mc;return typeof n!=`object`||!n?!1:t in n}function y(e,t,n={}){let r=e;`_mc`in e||(r._mc=new E(e));let i=r._mc;return t.length&&(i[t]=n,e instanceof HTMLElement&&!e.getAttribute(`data-mc`)&&e.setAttribute(`data-mc`,``)),r}function b(e,t){if(!t)return;let n=e._mc;n&&(n[t]&&delete n[t],!Object.keys(n).length&&e instanceof HTMLElement&&e.hasAttribute(`data-mc`)&&e.removeAttribute(`data-mc`))}function x(e){queueMicrotask(()=>{e.trigger(`Init`,null)})}function S(e){let t=e.target,r=Object.getPrototypeOf(e),i=e,a=Object.getOwnPropertyNames(r).filter(e=>/^(on[A-Z]|one[A-Z])/.test(e)&&typeof r[e]==`function`);for(let e of a){let r=/^one/.test(e);n(t,e.replace(/^one?/,``),t=>i[e](t),r)}}function C(e){return Array.from(e.querySelectorAll(`[data-mc]:not(:scope [data-mc] [data-mc])`)).filter(e=>e instanceof HTMLElement&&`_mc`in e).map(e=>e._mc)}function w(e,t){let n=e.parentElement?.closest(`[data-mc]`);for(;n instanceof HTMLElement;){if(`_mc`in n){let e=n._mc;if(t===void 0||t in e)return e}n=n.parentElement?.closest(`[data-mc]`)}return null}var T=class{#e;static _assets={};static assets=[];static _assetsPromise;constructor(e){this.#e=new WeakRef(e||{});let t=this.constructor;this instanceof E&&this.constructor!==E&&S(this);let n=(t.assets??[]).filter(e=>!t._assets[e]);n.length&&!t._assetsPromise&&(t._assetsPromise=g(t._originalTag||(this instanceof E?this.target.tagName.toLowerCase():d(t.name)),n).then(e=>{let r={};return n.forEach((t,n)=>{r[t]=e[n]}),t._assets=Object.assign({},t._assets,r),r}))}get target(){return this.#e.deref()}get assets(){return this.constructor._assets||{}}},E=class e extends T{static tag;static attributes={};constructor(t){super(t);let n=this.constructor;if(n===e)return;let r=this.target.tagName.toLowerCase();if(n.tag!==void 0&&r!==n.tag&&n.tag!==``){let t=document.createElement(n.tag);t.classList.add(r),n._originalTag=r,e.add(t,n.name,new n(t)),delete n._originalTag,r.includes(`-`)&&!this.target.isConnected?this.on(`connected`,()=>this.target.replaceWith(t)):this.target.replaceWith(t);return}let i=n.attributes;if(n.attributes&&Object.keys(n.attributes).forEach(e=>{this.target.setAttribute(e,i[e])}),Object.keys(n._assets).length){h(this,n._assets);for(let e of[`css`,`targetCss`])delete n._assets[e];n.assets=n.assets.filter(e=>e!==`css`&&e!==`targetCss`)}if(n._assetsPromise?n._assetsPromise.then(e=>{h(this,e);for(let e of[`css`,`targetCss`])delete n._assets[e];n.assets=n.assets.filter(e=>e!==`css`&&e!==`targetCss`),x(this)}):x(this),n.tag===``){this.target.remove();return}}static add(e,t,n={}){return y(e,t,n)}static remove(e,t){b(e,t)}static get(e,t,n){if(!v(e))return;let r=e._mc[t];return r instanceof n?r:void 0}off(e,n){t(this.target,e,n)}on(e,t){return n(this.target,e,t)}one(e,t){return r(this.target,e,t)}trigger(e,t={},n=`l`,r){a(this.target,e,t,n,r)}ancestors(e){let t=[],n=w(this.target,e);for(;n;)t.push(n),n=w(n.target,e);return t}closest(e){return w(this.target,e)}children(e){let t=C(this.target);return e===void 0?t:t.filter(t=>e in t)}descendants(e){return Array.from(this.target.querySelectorAll(`[data-mc]`)).filter(e=>e instanceof HTMLElement&&`_mc`in e).map(e=>e._mc).filter(t=>e===void 0||e in t)}};function D(e){return typeof e==`object`&&!!e}var O={};function k(e){let t=e.constructor,n=t.name,r=class extends t{#e=!0;#t=[];constructor(e){super(e);let t=this.constructor;t.callbacks=[],D(e)&&Object.getOwnPropertyNames(e).forEach(t=>{this[t]=e[t]})}get callbacks(){return this.#t}set callbacks(e){this.#t=e}[Symbol.toPrimitive](e){let t=this.valueOf();return e===`string`?String(t):e===`number`?Number(t):t}subscribe(e){return this.#t.push(e),this}unsubscribe(e){return this.#t=this.#t.filter(t=>t!==e),this}bind(e){if(!o())return this;if(D(this.valueOf())){L(e);let t=[e,...e.querySelectorAll(`[data-mc]`)].filter(e=>!!e?._mc?._placeholders);this.#t.push(function(e){let n=l(e);t.forEach(e=>{e&&a(e,`data`,n)})}),this.notify()}else console.warn(`IGNORED - cannot bind a non-Object to the dom, IS:`,typeof this.valueOf(),this);return this}notify(e){let t=D(this.valueOf())&&!(this.valueOf()instanceof Array)?structuredClone(Object.assign({},this)):this.valueOf();return e!==void 0&&(this.#e=e),this.#e&&this.#t.forEach(e=>{e(t)}),this}};return O[n]=r,r}function A(e){return O[e.constructor.name]||k(e)}function j(e){return(typeof e==`object`||typeof e==`function`)&&e!==null&&`subscribe`in e}function M(e,t){let n=e;return(t?[t]:[...Object.getOwnPropertyNames(e)]).forEach(t=>{let r=n[t];if(Object.prototype.hasOwnProperty.call(e,t)&&typeof r!=`function`&&!j(r)){if(t[0]===`_`||r===void 0)return;let i=r,a=new(A(i))(i),o=!1,s=function(){o||(o=!0,queueMicrotask(function(){o=!1,a.notify()}))};Object.defineProperty(e,t,{enumerable:!0,get(){if(D(a.valueOf())){let e=structuredClone(Object.assign({},a));queueMicrotask(function(){u(structuredClone(Object.assign({},a)),e)||s()})}return a},set(e){if(typeof e==typeof a.valueOf())if(D(a.valueOf())&&!(a.valueOf()instanceof Array))j(a)||(a=new(A(e))(structuredClone(Object.assign({},e))),f(e,a)),Object.assign(a,e),a.notify();else{let t=a.valueOf(),n=a.callbacks;e!==t&&(a=new(A(e))(e),a.callbacks=n,s())}else console.warn(`IGNORED - cannot change observable types, IS:`,typeof a.valueOf(),a,`- CANNOT BECOME:`,typeof e)}}),n[t]=i}}),e}function N(e){let t=new Set,n=/\{[^\{\}]*\}/g;return(e.getAttributeNames?e.getAttributeNames():[]).filter(t=>e.getAttribute(t)?.match(n)).forEach(r=>{new Set(e.getAttribute(r)?.match(n)).forEach(e=>t.add(e.replace(/[\{\}]/g,``)))}),[...e.childNodes].filter(e=>e.nodeType===3&&(e.nodeValue||``).match(n)).forEach(e=>{(e?.nodeValue||``).match(n).forEach(e=>t.add(e.replace(/[\{\}]/g,``)))}),[...t]}var P=[`INPUT`,`SELECT`,`TEXTAREA`],F=class extends E{data={};#e=``;#t=!1;constructor(e){super(e),N(e).forEach(e=>this.data[e]=``),M(this),this.#r()}#n(e,t,n){t.replace(/[\{\}]/g,``);let r=t.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`);return e.replace(new RegExp(r,`g`),n)}#r(){let e=this.target;(e.getAttributeNames?e.getAttributeNames():[]).filter(t=>e.getAttribute(t)?.match(/\{[^\{\}]*\}/g)).forEach(t=>{let n=new Set(e.getAttribute(t)?.match(/\{[^\{\}]*\}/g)),r=e.getAttribute(t)??``;this.data.subscribe((r=>i=>{let a=r;n.forEach(e=>{a=this.#n(a,e,String(i[e.replace(/[\{\}]/g,``)]))}),e.setAttribute(t,a)})(r))});let t=[...e.childNodes].filter(e=>e.nodeType===3&&(e.nodeValue||``).match(/\{[^\{\}]*\}/g));t.length&&(this.#e=e.getAttribute(`style`)||``,t.forEach(t=>{let n=t.nodeValue??``,r=new Set(n.match(/\{[^\{\}]*\}/g));this.data.subscribe((n=>i=>{let a=n;r.forEach(e=>{a=this.#n(a,e,String(i[e.replace(/[\{\}]/g,``)]))}),this.#t||(this.#t=!0,this.#e?e.style=this.#e:e.removeAttribute(`style`)),t.nodeValue=a})(n)),!P.includes(e.tagName)&&e.tagName!==`FIELDSET`&&(e.style.display=`none`)}))}onData(e){let t={};Object.keys(e.detail.data).forEach(n=>{this.data.hasOwnProperty(n)&&(t[n]=e.detail.data[n])}),JSON.stringify(t)!==JSON.stringify(this.data)&&Object.assign(this.data,t)}};function I(e){return[e,...e.querySelectorAll(`*`)].filter(e=>!!N(e).length).filter(e=>e?.tagName!==`STYLE`).reverse()}function L(e){I(e).forEach(e=>{e?._mc?._placeholders||E.add(e,`_placeholders`,new F(e))})}export{L as makePlaceholders};
|