@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.
@@ -1,5 +1,94 @@
1
1
  // Copyright (c) 2025 MintJams Inc. Licensed under MIT License.
2
+ /**
3
+ * Represents a reusable component definition.
4
+ */
5
+ class VComponent {
6
+ /**
7
+ * The unique identifier for the component.
8
+ */
9
+ id;
10
+ /**
11
+ * The function that creates a new instance of the component.
12
+ */
13
+ createInstance;
14
+ /**
15
+ * The optional template ID for the component.
16
+ * If not specified, defaults to the component ID.
17
+ */
18
+ templateID;
19
+ /**
20
+ * Creates a new component definition.
21
+ * @param id The unique identifier for the component.
22
+ * @param createInstance The function that creates a new instance of the component.
23
+ * @param templateID The optional template ID for the component.
24
+ */
25
+ constructor(id, createInstance, templateID) {
26
+ if (!id || typeof id !== 'string') {
27
+ throw new Error('Component ID must be a non-empty string.');
28
+ }
29
+ if (typeof createInstance !== 'function') {
30
+ throw new Error('createInstance must be a function.');
31
+ }
32
+ this.id = id.trim();
33
+ this.createInstance = createInstance;
34
+ this.templateID = templateID?.trim() || this.id;
35
+ }
36
+ }
37
+
38
+ // Copyright (c) 2025 MintJams Inc. Licensed under MIT License.
39
+ /**
40
+ * A registry for managing component definitions.
41
+ */
2
42
  class VComponentRegistry {
43
+ /**
44
+ * Map of component ID to component definition.
45
+ */
46
+ #components = new Map();
47
+ /**
48
+ * Registers a new component.
49
+ * @param id The unique identifier for the component.
50
+ * @param createInstance The function that creates a new instance of the component.
51
+ * @param templateID The optional template ID for the component.
52
+ * @returns True if the component was registered, false if a component with the same ID already exists.
53
+ */
54
+ register(id, createInstance, templateID) {
55
+ if (this.has(id)) {
56
+ return false;
57
+ }
58
+ const component = new VComponent(id, createInstance, templateID);
59
+ this.#components.set(id, component);
60
+ return true;
61
+ }
62
+ /**
63
+ * Checks if a component with the given ID exists.
64
+ * @param id The component ID to check.
65
+ * @returns True if the component exists, false otherwise.
66
+ */
67
+ has(id) {
68
+ return this.#components.has(id);
69
+ }
70
+ /**
71
+ * Gets a component by its ID.
72
+ * @param id The component ID to retrieve.
73
+ * @returns The component definition, or undefined if not found.
74
+ */
75
+ get(id) {
76
+ return this.#components.get(id);
77
+ }
78
+ /**
79
+ * Removes a component from the registry.
80
+ * @param id The component ID to remove.
81
+ * @returns True if the component was removed, false if it didn't exist.
82
+ */
83
+ unregister(id) {
84
+ return this.#components.delete(id);
85
+ }
86
+ /**
87
+ * Clears all registered components.
88
+ */
89
+ clear() {
90
+ this.#components.clear();
91
+ }
3
92
  }
4
93
 
5
94
  // Copyright (c) 2025 MintJams Inc. Licensed under MIT License.
@@ -74,6 +163,8 @@ var StandardDirectiveName;
74
163
  StandardDirectiveName["V_INTERSECTION"] = "v-intersection";
75
164
  /** Performance observer directives. */
76
165
  StandardDirectiveName["V_PERFORMANCE"] = "v-performance";
166
+ /** Component directive. */
167
+ StandardDirectiveName["V_COMPONENT"] = "v-component";
77
168
  })(StandardDirectiveName || (StandardDirectiveName = {}));
78
169
 
79
170
  // This file was generated. Do not modify manually!
@@ -6640,10 +6731,16 @@ class ExpressionUtils {
6640
6731
  const isArrowFunction = /^\s*(\([^)]*\)|[a-zA-Z_$][a-zA-Z0-9_$]*)\s*=>/.test(source);
6641
6732
  const isFunctionExpression = source.startsWith('function');
6642
6733
  const isAsyncFunction = source.startsWith('async');
6643
- // If it's a method shorthand (e.g., "methodName() { ... }"), convert to function expression
6644
- if (!isFunctionExpression && !isArrowFunction && !isAsyncFunction) {
6734
+ // If it's a method shorthand (e.g., "methodName() { ... }" or "async methodName() { ... }"), convert to function expression
6735
+ if (!isFunctionExpression && !isArrowFunction) {
6645
6736
  // It's likely a method shorthand, convert to function expression
6646
- source = `function ${source}`;
6737
+ if (isAsyncFunction) {
6738
+ // Remove 'async' prefix and add 'async function' prefix
6739
+ source = `async function ${source.substring(5).trim()}`;
6740
+ }
6741
+ else {
6742
+ source = `function ${source}`;
6743
+ }
6647
6744
  }
6648
6745
  // Wrap in parentheses for parsing
6649
6746
  source = `(${source})`;
@@ -7111,6 +7208,226 @@ class VBindDirective {
7111
7208
  }
7112
7209
  }
7113
7210
 
7211
+ // Copyright (c) 2025 MintJams Inc. Licensed under MIT License.
7212
+ /**
7213
+ * Directive for rendering components.
7214
+ * Usage: <div v-component="componentId" :options="props"></div>
7215
+ *
7216
+ * The :options binding is used to pass properties to the component.
7217
+ * Example:
7218
+ * <div v-component="my-component" :options="{message: 'Hello'}"></div>
7219
+ */
7220
+ class VComponentDirective {
7221
+ /**
7222
+ * The virtual node to which this directive is applied.
7223
+ */
7224
+ #vNode;
7225
+ /**
7226
+ * The component ID expression.
7227
+ */
7228
+ #expression;
7229
+ /**
7230
+ * Whether the component ID is static (not reactive).
7231
+ */
7232
+ #isStatic = false;
7233
+ /**
7234
+ * The component ID to render.
7235
+ */
7236
+ #componentId;
7237
+ /**
7238
+ * The child application instance for the component.
7239
+ */
7240
+ #childApp;
7241
+ /**
7242
+ * Whether the component has been activated.
7243
+ */
7244
+ #isActivated = false;
7245
+ constructor(context) {
7246
+ this.#vNode = context.vNode;
7247
+ this.#expression = context.attribute.value;
7248
+ // Check for .static modifier
7249
+ const attrName = context.attribute.name;
7250
+ this.#isStatic = attrName.includes('.static');
7251
+ // Remove the directive attribute from the element
7252
+ this.#vNode.node.removeAttribute(context.attribute.name);
7253
+ }
7254
+ /**
7255
+ * @inheritdoc
7256
+ */
7257
+ get name() {
7258
+ return StandardDirectiveName.V_COMPONENT;
7259
+ }
7260
+ /**
7261
+ * @inheritdoc
7262
+ */
7263
+ get vNode() {
7264
+ return this.#vNode;
7265
+ }
7266
+ /**
7267
+ * @inheritdoc
7268
+ */
7269
+ get needsAnchor() {
7270
+ return false;
7271
+ }
7272
+ /**
7273
+ * @inheritdoc
7274
+ */
7275
+ get bindingsPreparer() {
7276
+ return undefined;
7277
+ }
7278
+ /**
7279
+ * @inheritdoc
7280
+ */
7281
+ get domUpdater() {
7282
+ // Component rendering is handled through onMounted lifecycle hook
7283
+ return undefined;
7284
+ }
7285
+ /**
7286
+ * @inheritdoc
7287
+ */
7288
+ get templatize() {
7289
+ return false;
7290
+ }
7291
+ /**
7292
+ * @inheritdoc
7293
+ */
7294
+ get dependentIdentifiers() {
7295
+ return [];
7296
+ }
7297
+ /**
7298
+ * @inheritdoc
7299
+ */
7300
+ get onMount() {
7301
+ return () => {
7302
+ this.renderComponent();
7303
+ };
7304
+ }
7305
+ /**
7306
+ * @inheritdoc
7307
+ */
7308
+ get onMounted() {
7309
+ return undefined;
7310
+ }
7311
+ /**
7312
+ * @inheritdoc
7313
+ */
7314
+ get onUpdate() {
7315
+ return undefined;
7316
+ }
7317
+ /**
7318
+ * @inheritdoc
7319
+ */
7320
+ get onUpdated() {
7321
+ return undefined;
7322
+ }
7323
+ /**
7324
+ * @inheritdoc
7325
+ */
7326
+ get onUnmount() {
7327
+ return () => this.cleanupComponent();
7328
+ }
7329
+ /**
7330
+ * @inheritdoc
7331
+ */
7332
+ get onUnmounted() {
7333
+ return undefined;
7334
+ }
7335
+ /**
7336
+ * @inheritdoc
7337
+ */
7338
+ destroy() {
7339
+ this.cleanupComponent();
7340
+ }
7341
+ /**
7342
+ * Renders the component.
7343
+ */
7344
+ renderComponent() {
7345
+ const element = this.#vNode.node;
7346
+ if (!element) {
7347
+ return;
7348
+ }
7349
+ // For now, only support static component IDs
7350
+ const componentId = this.#expression.trim();
7351
+ if (!componentId) {
7352
+ console.warn(`Component ID is empty for v-component directive`);
7353
+ return;
7354
+ }
7355
+ // Get properties from :options or :options.component directive
7356
+ let properties = {};
7357
+ const optionsDirective = this.#vNode.directiveManager?.optionsDirective('component');
7358
+ if (optionsDirective && optionsDirective.expression) {
7359
+ // Evaluate the options expression
7360
+ const identifiers = optionsDirective.dependentIdentifiers;
7361
+ const values = identifiers.map(id => this.#vNode.bindings?.get(id));
7362
+ const args = identifiers.join(", ");
7363
+ const funcBody = `return (${optionsDirective.expression});`;
7364
+ const func = new Function(args, funcBody);
7365
+ const result = func(...values);
7366
+ if (typeof result === 'object' && result !== null) {
7367
+ properties = result;
7368
+ }
7369
+ }
7370
+ // Store component ID
7371
+ this.#componentId = componentId;
7372
+ // Get component definition from the application's component registry
7373
+ const vApplication = this.#vNode.vApplication;
7374
+ if (!vApplication) {
7375
+ console.error('VApplication not found on VNode');
7376
+ return;
7377
+ }
7378
+ const component = vApplication.componentRegistry.get(componentId);
7379
+ if (!component) {
7380
+ console.error(`Component '${componentId}' not found in registry`);
7381
+ return;
7382
+ }
7383
+ // Get template element
7384
+ const finalTemplateID = component.templateID;
7385
+ const templateElement = document.querySelector(`#${finalTemplateID}`);
7386
+ if (!templateElement || !(templateElement instanceof HTMLTemplateElement)) {
7387
+ console.error(`Template element '#${finalTemplateID}' not found`);
7388
+ return;
7389
+ }
7390
+ // Clone template content
7391
+ const fragment = templateElement.content.cloneNode(true);
7392
+ const childNodes = Array.from(fragment.childNodes);
7393
+ // Find the first element node
7394
+ let componentElement;
7395
+ for (const node of childNodes) {
7396
+ if (node.nodeType === Node.ELEMENT_NODE) {
7397
+ componentElement = node;
7398
+ break;
7399
+ }
7400
+ }
7401
+ if (!componentElement) {
7402
+ console.error(`No element found in template '#${finalTemplateID}'`);
7403
+ return;
7404
+ }
7405
+ // Replace element with component element
7406
+ const parent = element.parentNode;
7407
+ if (parent) {
7408
+ parent.insertBefore(componentElement, element);
7409
+ parent.removeChild(element);
7410
+ }
7411
+ // Create component instance
7412
+ const instance = component.createInstance(properties);
7413
+ // Create and mount child application using the parent application's registries
7414
+ this.#childApp = vApplication.createChildApp(instance);
7415
+ this.#childApp.mount(componentElement);
7416
+ this.#isActivated = true;
7417
+ }
7418
+ /**
7419
+ * Cleans up the component.
7420
+ */
7421
+ cleanupComponent() {
7422
+ if (this.#childApp) {
7423
+ // TODO: Implement unmount when available in VApplication
7424
+ // this.#childApp.unmount();
7425
+ this.#childApp = undefined;
7426
+ }
7427
+ this.#isActivated = false;
7428
+ }
7429
+ }
7430
+
7114
7431
  // Copyright (c) 2025 MintJams Inc. Licensed under MIT License.
7115
7432
  /**
7116
7433
  * Utility class for creating reactive proxies that automatically track changes.
@@ -7589,7 +7906,11 @@ class VDirectiveManager {
7589
7906
  }
7590
7907
  // If this is an options binding directive, cache it
7591
7908
  if (directive.name === StandardDirectiveName.V_BIND && directive.isOptions) {
7592
- this.#optionsDirectives[directive.name] = directive;
7909
+ const bindDirective = directive;
7910
+ const attrName = bindDirective.attributeName;
7911
+ if (attrName) {
7912
+ this.#optionsDirectives[attrName] = bindDirective;
7913
+ }
7593
7914
  }
7594
7915
  }
7595
7916
  }
@@ -10569,7 +10890,10 @@ class VStandardDirectiveParser {
10569
10890
  // v-intersection
10570
10891
  context.attribute.name === StandardDirectiveName.V_INTERSECTION ||
10571
10892
  // v-performance
10572
- context.attribute.name === StandardDirectiveName.V_PERFORMANCE) {
10893
+ context.attribute.name === StandardDirectiveName.V_PERFORMANCE ||
10894
+ // v-component, v-component.<modifier>
10895
+ context.attribute.name === StandardDirectiveName.V_COMPONENT ||
10896
+ context.attribute.name.startsWith(StandardDirectiveName.V_COMPONENT + ".")) {
10573
10897
  return true;
10574
10898
  }
10575
10899
  return false;
@@ -10620,6 +10944,11 @@ class VStandardDirectiveParser {
10620
10944
  if (context.attribute.name === StandardDirectiveName.V_PERFORMANCE) {
10621
10945
  return new VPerformanceDirective(context);
10622
10946
  }
10947
+ // v-component, v-component.<modifier>
10948
+ if (context.attribute.name === StandardDirectiveName.V_COMPONENT ||
10949
+ context.attribute.name.startsWith(StandardDirectiveName.V_COMPONENT + ".")) {
10950
+ return new VComponentDirective(context);
10951
+ }
10623
10952
  throw new Error(`The attribute "${context.attribute.name}" cannot be parsed by ${this.name}.`);
10624
10953
  }
10625
10954
  }
@@ -10853,12 +11182,18 @@ class VApplication {
10853
11182
  }
10854
11183
  /**
10855
11184
  * Mounts the application.
10856
- * @param selectors The CSS selectors to identify the root element.
10857
- */
10858
- mount(selectors) {
10859
- const element = document.querySelector(selectors);
10860
- if (!element) {
10861
- throw new Error(`Element not found for selectors: ${selectors}`);
11185
+ * @param target The CSS selector string or HTMLElement to mount the application to.
11186
+ */
11187
+ mount(target) {
11188
+ let element;
11189
+ if (typeof target === 'string') {
11190
+ element = document.querySelector(target);
11191
+ if (!element) {
11192
+ throw new Error(`Element not found for selector: ${target}`);
11193
+ }
11194
+ }
11195
+ else {
11196
+ element = target;
10862
11197
  }
10863
11198
  // Clean the element by removing unnecessary whitespace text nodes
10864
11199
  this.#cleanElement(element);
@@ -10872,6 +11207,14 @@ class VApplication {
10872
11207
  this.#vNode.update();
10873
11208
  this.#logger.info('Application mounted.');
10874
11209
  }
11210
+ /**
11211
+ * Creates a child application instance with the same registries.
11212
+ * @param options The application options for the child.
11213
+ * @returns The created child application instance.
11214
+ */
11215
+ createChildApp(options) {
11216
+ return new VApplication(options, this.#directiveParserRegistry, this.#componentRegistry);
11217
+ }
10875
11218
  /**
10876
11219
  * Cleans the element by removing unnecessary whitespace text nodes.
10877
11220
  * @param element The element to clean.
@@ -11108,5 +11451,5 @@ class VDOM {
11108
11451
  }
11109
11452
  }
11110
11453
 
11111
- export { VDOM };
11454
+ export { VComponent, VComponentRegistry, VDOM };
11112
11455
  //# sourceMappingURL=ichigo.esm.js.map