@mintjamsinc/ichigojs 0.1.10 → 0.1.11

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.
@@ -5,7 +5,96 @@
5
5
  })(this, (function (exports) { 'use strict';
6
6
 
7
7
  // Copyright (c) 2025 MintJams Inc. Licensed under MIT License.
8
+ /**
9
+ * Represents a reusable component definition.
10
+ */
11
+ class VComponent {
12
+ /**
13
+ * The unique identifier for the component.
14
+ */
15
+ id;
16
+ /**
17
+ * The function that creates a new instance of the component.
18
+ */
19
+ createInstance;
20
+ /**
21
+ * The optional template ID for the component.
22
+ * If not specified, defaults to the component ID.
23
+ */
24
+ templateID;
25
+ /**
26
+ * Creates a new component definition.
27
+ * @param id The unique identifier for the component.
28
+ * @param createInstance The function that creates a new instance of the component.
29
+ * @param templateID The optional template ID for the component.
30
+ */
31
+ constructor(id, createInstance, templateID) {
32
+ if (!id || typeof id !== 'string') {
33
+ throw new Error('Component ID must be a non-empty string.');
34
+ }
35
+ if (typeof createInstance !== 'function') {
36
+ throw new Error('createInstance must be a function.');
37
+ }
38
+ this.id = id.trim();
39
+ this.createInstance = createInstance;
40
+ this.templateID = templateID?.trim() || this.id;
41
+ }
42
+ }
43
+
44
+ // Copyright (c) 2025 MintJams Inc. Licensed under MIT License.
45
+ /**
46
+ * A registry for managing component definitions.
47
+ */
8
48
  class VComponentRegistry {
49
+ /**
50
+ * Map of component ID to component definition.
51
+ */
52
+ #components = new Map();
53
+ /**
54
+ * Registers a new component.
55
+ * @param id The unique identifier for the component.
56
+ * @param createInstance The function that creates a new instance of the component.
57
+ * @param templateID The optional template ID for the component.
58
+ * @returns True if the component was registered, false if a component with the same ID already exists.
59
+ */
60
+ register(id, createInstance, templateID) {
61
+ if (this.has(id)) {
62
+ return false;
63
+ }
64
+ const component = new VComponent(id, createInstance, templateID);
65
+ this.#components.set(id, component);
66
+ return true;
67
+ }
68
+ /**
69
+ * Checks if a component with the given ID exists.
70
+ * @param id The component ID to check.
71
+ * @returns True if the component exists, false otherwise.
72
+ */
73
+ has(id) {
74
+ return this.#components.has(id);
75
+ }
76
+ /**
77
+ * Gets a component by its ID.
78
+ * @param id The component ID to retrieve.
79
+ * @returns The component definition, or undefined if not found.
80
+ */
81
+ get(id) {
82
+ return this.#components.get(id);
83
+ }
84
+ /**
85
+ * Removes a component from the registry.
86
+ * @param id The component ID to remove.
87
+ * @returns True if the component was removed, false if it didn't exist.
88
+ */
89
+ unregister(id) {
90
+ return this.#components.delete(id);
91
+ }
92
+ /**
93
+ * Clears all registered components.
94
+ */
95
+ clear() {
96
+ this.#components.clear();
97
+ }
9
98
  }
10
99
 
11
100
  // Copyright (c) 2025 MintJams Inc. Licensed under MIT License.
@@ -80,6 +169,8 @@
80
169
  StandardDirectiveName["V_INTERSECTION"] = "v-intersection";
81
170
  /** Performance observer directives. */
82
171
  StandardDirectiveName["V_PERFORMANCE"] = "v-performance";
172
+ /** Component directive. */
173
+ StandardDirectiveName["V_COMPONENT"] = "v-component";
83
174
  })(StandardDirectiveName || (StandardDirectiveName = {}));
84
175
 
85
176
  // This file was generated. Do not modify manually!
@@ -6646,10 +6737,16 @@
6646
6737
  const isArrowFunction = /^\s*(\([^)]*\)|[a-zA-Z_$][a-zA-Z0-9_$]*)\s*=>/.test(source);
6647
6738
  const isFunctionExpression = source.startsWith('function');
6648
6739
  const isAsyncFunction = source.startsWith('async');
6649
- // If it's a method shorthand (e.g., "methodName() { ... }"), convert to function expression
6650
- if (!isFunctionExpression && !isArrowFunction && !isAsyncFunction) {
6740
+ // If it's a method shorthand (e.g., "methodName() { ... }" or "async methodName() { ... }"), convert to function expression
6741
+ if (!isFunctionExpression && !isArrowFunction) {
6651
6742
  // It's likely a method shorthand, convert to function expression
6652
- source = `function ${source}`;
6743
+ if (isAsyncFunction) {
6744
+ // Remove 'async' prefix and add 'async function' prefix
6745
+ source = `async function ${source.substring(5).trim()}`;
6746
+ }
6747
+ else {
6748
+ source = `function ${source}`;
6749
+ }
6653
6750
  }
6654
6751
  // Wrap in parentheses for parsing
6655
6752
  source = `(${source})`;
@@ -7117,6 +7214,226 @@
7117
7214
  }
7118
7215
  }
7119
7216
 
7217
+ // Copyright (c) 2025 MintJams Inc. Licensed under MIT License.
7218
+ /**
7219
+ * Directive for rendering components.
7220
+ * Usage: <div v-component="componentId" :options="props"></div>
7221
+ *
7222
+ * The :options binding is used to pass properties to the component.
7223
+ * Example:
7224
+ * <div v-component="my-component" :options="{message: 'Hello'}"></div>
7225
+ */
7226
+ class VComponentDirective {
7227
+ /**
7228
+ * The virtual node to which this directive is applied.
7229
+ */
7230
+ #vNode;
7231
+ /**
7232
+ * The component ID expression.
7233
+ */
7234
+ #expression;
7235
+ /**
7236
+ * Whether the component ID is static (not reactive).
7237
+ */
7238
+ #isStatic = false;
7239
+ /**
7240
+ * The component ID to render.
7241
+ */
7242
+ #componentId;
7243
+ /**
7244
+ * The child application instance for the component.
7245
+ */
7246
+ #childApp;
7247
+ /**
7248
+ * Whether the component has been activated.
7249
+ */
7250
+ #isActivated = false;
7251
+ constructor(context) {
7252
+ this.#vNode = context.vNode;
7253
+ this.#expression = context.attribute.value;
7254
+ // Check for .static modifier
7255
+ const attrName = context.attribute.name;
7256
+ this.#isStatic = attrName.includes('.static');
7257
+ // Remove the directive attribute from the element
7258
+ this.#vNode.node.removeAttribute(context.attribute.name);
7259
+ }
7260
+ /**
7261
+ * @inheritdoc
7262
+ */
7263
+ get name() {
7264
+ return StandardDirectiveName.V_COMPONENT;
7265
+ }
7266
+ /**
7267
+ * @inheritdoc
7268
+ */
7269
+ get vNode() {
7270
+ return this.#vNode;
7271
+ }
7272
+ /**
7273
+ * @inheritdoc
7274
+ */
7275
+ get needsAnchor() {
7276
+ return false;
7277
+ }
7278
+ /**
7279
+ * @inheritdoc
7280
+ */
7281
+ get bindingsPreparer() {
7282
+ return undefined;
7283
+ }
7284
+ /**
7285
+ * @inheritdoc
7286
+ */
7287
+ get domUpdater() {
7288
+ // Component rendering is handled through onMounted lifecycle hook
7289
+ return undefined;
7290
+ }
7291
+ /**
7292
+ * @inheritdoc
7293
+ */
7294
+ get templatize() {
7295
+ return false;
7296
+ }
7297
+ /**
7298
+ * @inheritdoc
7299
+ */
7300
+ get dependentIdentifiers() {
7301
+ return [];
7302
+ }
7303
+ /**
7304
+ * @inheritdoc
7305
+ */
7306
+ get onMount() {
7307
+ return () => {
7308
+ this.renderComponent();
7309
+ };
7310
+ }
7311
+ /**
7312
+ * @inheritdoc
7313
+ */
7314
+ get onMounted() {
7315
+ return undefined;
7316
+ }
7317
+ /**
7318
+ * @inheritdoc
7319
+ */
7320
+ get onUpdate() {
7321
+ return undefined;
7322
+ }
7323
+ /**
7324
+ * @inheritdoc
7325
+ */
7326
+ get onUpdated() {
7327
+ return undefined;
7328
+ }
7329
+ /**
7330
+ * @inheritdoc
7331
+ */
7332
+ get onUnmount() {
7333
+ return () => this.cleanupComponent();
7334
+ }
7335
+ /**
7336
+ * @inheritdoc
7337
+ */
7338
+ get onUnmounted() {
7339
+ return undefined;
7340
+ }
7341
+ /**
7342
+ * @inheritdoc
7343
+ */
7344
+ destroy() {
7345
+ this.cleanupComponent();
7346
+ }
7347
+ /**
7348
+ * Renders the component.
7349
+ */
7350
+ renderComponent() {
7351
+ const element = this.#vNode.node;
7352
+ if (!element) {
7353
+ return;
7354
+ }
7355
+ // For now, only support static component IDs
7356
+ const componentId = this.#expression.trim();
7357
+ if (!componentId) {
7358
+ console.warn(`Component ID is empty for v-component directive`);
7359
+ return;
7360
+ }
7361
+ // Get properties from :options or :options.component directive
7362
+ let properties = {};
7363
+ const optionsDirective = this.#vNode.directiveManager?.optionsDirective('component');
7364
+ if (optionsDirective && optionsDirective.expression) {
7365
+ // Evaluate the options expression
7366
+ const identifiers = optionsDirective.dependentIdentifiers;
7367
+ const values = identifiers.map(id => this.#vNode.bindings?.get(id));
7368
+ const args = identifiers.join(", ");
7369
+ const funcBody = `return (${optionsDirective.expression});`;
7370
+ const func = new Function(args, funcBody);
7371
+ const result = func(...values);
7372
+ if (typeof result === 'object' && result !== null) {
7373
+ properties = result;
7374
+ }
7375
+ }
7376
+ // Store component ID
7377
+ this.#componentId = componentId;
7378
+ // Get component definition from the application's component registry
7379
+ const vApplication = this.#vNode.vApplication;
7380
+ if (!vApplication) {
7381
+ console.error('VApplication not found on VNode');
7382
+ return;
7383
+ }
7384
+ const component = vApplication.componentRegistry.get(componentId);
7385
+ if (!component) {
7386
+ console.error(`Component '${componentId}' not found in registry`);
7387
+ return;
7388
+ }
7389
+ // Get template element
7390
+ const finalTemplateID = component.templateID;
7391
+ const templateElement = document.querySelector(`#${finalTemplateID}`);
7392
+ if (!templateElement || !(templateElement instanceof HTMLTemplateElement)) {
7393
+ console.error(`Template element '#${finalTemplateID}' not found`);
7394
+ return;
7395
+ }
7396
+ // Clone template content
7397
+ const fragment = templateElement.content.cloneNode(true);
7398
+ const childNodes = Array.from(fragment.childNodes);
7399
+ // Find the first element node
7400
+ let componentElement;
7401
+ for (const node of childNodes) {
7402
+ if (node.nodeType === Node.ELEMENT_NODE) {
7403
+ componentElement = node;
7404
+ break;
7405
+ }
7406
+ }
7407
+ if (!componentElement) {
7408
+ console.error(`No element found in template '#${finalTemplateID}'`);
7409
+ return;
7410
+ }
7411
+ // Replace element with component element
7412
+ const parent = element.parentNode;
7413
+ if (parent) {
7414
+ parent.insertBefore(componentElement, element);
7415
+ parent.removeChild(element);
7416
+ }
7417
+ // Create component instance
7418
+ const instance = component.createInstance(properties);
7419
+ // Create and mount child application using the parent application's registries
7420
+ this.#childApp = vApplication.createChildApp(instance);
7421
+ this.#childApp.mount(componentElement);
7422
+ this.#isActivated = true;
7423
+ }
7424
+ /**
7425
+ * Cleans up the component.
7426
+ */
7427
+ cleanupComponent() {
7428
+ if (this.#childApp) {
7429
+ // TODO: Implement unmount when available in VApplication
7430
+ // this.#childApp.unmount();
7431
+ this.#childApp = undefined;
7432
+ }
7433
+ this.#isActivated = false;
7434
+ }
7435
+ }
7436
+
7120
7437
  // Copyright (c) 2025 MintJams Inc. Licensed under MIT License.
7121
7438
  /**
7122
7439
  * Utility class for creating reactive proxies that automatically track changes.
@@ -7595,7 +7912,11 @@
7595
7912
  }
7596
7913
  // If this is an options binding directive, cache it
7597
7914
  if (directive.name === StandardDirectiveName.V_BIND && directive.isOptions) {
7598
- this.#optionsDirectives[directive.name] = directive;
7915
+ const bindDirective = directive;
7916
+ const attrName = bindDirective.attributeName;
7917
+ if (attrName) {
7918
+ this.#optionsDirectives[attrName] = bindDirective;
7919
+ }
7599
7920
  }
7600
7921
  }
7601
7922
  }
@@ -10575,7 +10896,10 @@
10575
10896
  // v-intersection
10576
10897
  context.attribute.name === StandardDirectiveName.V_INTERSECTION ||
10577
10898
  // v-performance
10578
- context.attribute.name === StandardDirectiveName.V_PERFORMANCE) {
10899
+ context.attribute.name === StandardDirectiveName.V_PERFORMANCE ||
10900
+ // v-component, v-component.<modifier>
10901
+ context.attribute.name === StandardDirectiveName.V_COMPONENT ||
10902
+ context.attribute.name.startsWith(StandardDirectiveName.V_COMPONENT + ".")) {
10579
10903
  return true;
10580
10904
  }
10581
10905
  return false;
@@ -10626,6 +10950,11 @@
10626
10950
  if (context.attribute.name === StandardDirectiveName.V_PERFORMANCE) {
10627
10951
  return new VPerformanceDirective(context);
10628
10952
  }
10953
+ // v-component, v-component.<modifier>
10954
+ if (context.attribute.name === StandardDirectiveName.V_COMPONENT ||
10955
+ context.attribute.name.startsWith(StandardDirectiveName.V_COMPONENT + ".")) {
10956
+ return new VComponentDirective(context);
10957
+ }
10629
10958
  throw new Error(`The attribute "${context.attribute.name}" cannot be parsed by ${this.name}.`);
10630
10959
  }
10631
10960
  }
@@ -10859,12 +11188,18 @@
10859
11188
  }
10860
11189
  /**
10861
11190
  * Mounts the application.
10862
- * @param selectors The CSS selectors to identify the root element.
10863
- */
10864
- mount(selectors) {
10865
- const element = document.querySelector(selectors);
10866
- if (!element) {
10867
- throw new Error(`Element not found for selectors: ${selectors}`);
11191
+ * @param target The CSS selector string or HTMLElement to mount the application to.
11192
+ */
11193
+ mount(target) {
11194
+ let element;
11195
+ if (typeof target === 'string') {
11196
+ element = document.querySelector(target);
11197
+ if (!element) {
11198
+ throw new Error(`Element not found for selector: ${target}`);
11199
+ }
11200
+ }
11201
+ else {
11202
+ element = target;
10868
11203
  }
10869
11204
  // Clean the element by removing unnecessary whitespace text nodes
10870
11205
  this.#cleanElement(element);
@@ -10878,6 +11213,14 @@
10878
11213
  this.#vNode.update();
10879
11214
  this.#logger.info('Application mounted.');
10880
11215
  }
11216
+ /**
11217
+ * Creates a child application instance with the same registries.
11218
+ * @param options The application options for the child.
11219
+ * @returns The created child application instance.
11220
+ */
11221
+ createChildApp(options) {
11222
+ return new VApplication(options, this.#directiveParserRegistry, this.#componentRegistry);
11223
+ }
10881
11224
  /**
10882
11225
  * Cleans the element by removing unnecessary whitespace text nodes.
10883
11226
  * @param element The element to clean.
@@ -11114,6 +11457,8 @@
11114
11457
  }
11115
11458
  }
11116
11459
 
11460
+ exports.VComponent = VComponent;
11461
+ exports.VComponentRegistry = VComponentRegistry;
11117
11462
  exports.VDOM = VDOM;
11118
11463
 
11119
11464
  }));