@portel/photon-core 2.5.3 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +98 -0
- package/dist/collections/Collection.d.ts +146 -0
- package/dist/collections/Collection.d.ts.map +1 -0
- package/dist/collections/Collection.js +312 -0
- package/dist/collections/Collection.js.map +1 -0
- package/dist/collections/index.d.ts +1 -0
- package/dist/collections/index.d.ts.map +1 -1
- package/dist/collections/index.js +1 -0
- package/dist/collections/index.js.map +1 -1
- package/dist/compiler.d.ts.map +1 -1
- package/dist/compiler.js +61 -27
- package/dist/compiler.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/collections/Collection.ts +356 -0
- package/src/collections/index.ts +1 -0
- package/src/compiler.ts +75 -33
- package/src/index.ts +5 -0
package/src/compiler.ts
CHANGED
|
@@ -21,52 +21,94 @@ import * as crypto from 'crypto';
|
|
|
21
21
|
* @returns Absolute path to the compiled .mjs file
|
|
22
22
|
*/
|
|
23
23
|
/**
|
|
24
|
-
* Transform
|
|
24
|
+
* Transform arrays to reactive collections for PhotonMCP classes
|
|
25
25
|
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
26
|
+
* ZERO-EFFORT REACTIVITY: If a class extends PhotonMCP and has array properties,
|
|
27
|
+
* this transform automatically:
|
|
28
|
+
* 1. Injects `import { Array as ReactiveArray } from '@portel/photon-core'`
|
|
29
|
+
* 2. Transforms `= []` to `= new ReactiveArray()` for class properties
|
|
30
30
|
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
31
|
+
* Result: Developers write normal code, arrays are automatically reactive.
|
|
32
|
+
*
|
|
33
|
+
* ```typescript
|
|
34
|
+
* // Developer writes this (normal TypeScript):
|
|
35
|
+
* export default class TodoList extends PhotonMCP {
|
|
36
|
+
* items: Task[] = [];
|
|
37
|
+
* async add(text: string) { this.items.push({...}); }
|
|
38
|
+
* }
|
|
39
|
+
*
|
|
40
|
+
* // Compiler transforms to:
|
|
41
|
+
* import { Array as ReactiveArray } from '@portel/photon-core';
|
|
42
|
+
* export default class TodoList extends PhotonMCP {
|
|
43
|
+
* items = new ReactiveArray();
|
|
44
|
+
* async add(text: string) { this.items.push({...}); } // Auto-emits!
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
33
47
|
*/
|
|
34
48
|
function transformReactiveCollections(source: string): string {
|
|
35
|
-
// Check
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
);
|
|
39
|
-
if (!importMatch) return source;
|
|
49
|
+
// Check if this is a PhotonMCP class (extends PhotonMCP)
|
|
50
|
+
const isPhotonMCP = /class\s+\w+\s+extends\s+PhotonMCP\b/.test(source);
|
|
51
|
+
if (!isPhotonMCP) return source;
|
|
40
52
|
|
|
41
|
-
|
|
53
|
+
// Check if there are array properties with = [] that need transformation
|
|
54
|
+
// Look for patterns like: `items: Type[] = []` or `items = []` (class properties)
|
|
55
|
+
const hasArrayLiterals =
|
|
56
|
+
/\w+\s*:\s*[^=\n]+\[\]\s*=\s*\[\s*\]/.test(source) || // typed: Type[] = []
|
|
57
|
+
/^\s+\w+\s*=\s*\[\s*\](?=\s*[;\n])/m.test(source); // simple: prop = []
|
|
58
|
+
|
|
59
|
+
if (!hasArrayLiterals) return source;
|
|
42
60
|
|
|
43
61
|
let transformed = source;
|
|
44
62
|
|
|
45
|
-
//
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
//
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
63
|
+
// Check if Array is already imported from photon-core
|
|
64
|
+
const hasArrayImport = /import\s*\{[^}]*\bArray\b[^}]*\}\s*from\s*['"]@portel\/photon-core['"]/.test(source);
|
|
65
|
+
|
|
66
|
+
if (!hasArrayImport) {
|
|
67
|
+
// Inject ReactiveArray import (using alias to avoid shadowing issues)
|
|
68
|
+
// Find the photon-core import and add ReactiveArray to it, or add new import
|
|
69
|
+
const photonCoreImport = source.match(
|
|
70
|
+
/import\s*\{([^}]+)\}\s*from\s*['"]@portel\/photon-core['"]/
|
|
53
71
|
);
|
|
54
72
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
73
|
+
if (photonCoreImport) {
|
|
74
|
+
// Add to existing import
|
|
75
|
+
const existingImports = photonCoreImport[1];
|
|
76
|
+
transformed = transformed.replace(
|
|
77
|
+
photonCoreImport[0],
|
|
78
|
+
`import { ${existingImports}, Array as ReactiveArray } from '@portel/photon-core'`
|
|
79
|
+
);
|
|
80
|
+
} else {
|
|
81
|
+
// Add new import at the top (after any existing imports)
|
|
82
|
+
const lastImportMatch = source.match(/^import\s+.+$/gm);
|
|
83
|
+
if (lastImportMatch) {
|
|
84
|
+
const lastImport = lastImportMatch[lastImportMatch.length - 1];
|
|
85
|
+
transformed = transformed.replace(
|
|
86
|
+
lastImport,
|
|
87
|
+
`${lastImport}\nimport { Array as ReactiveArray } from '@portel/photon-core';`
|
|
88
|
+
);
|
|
89
|
+
} else {
|
|
90
|
+
// No imports, add at top
|
|
91
|
+
transformed = `import { Array as ReactiveArray } from '@portel/photon-core';\n${transformed}`;
|
|
64
92
|
}
|
|
65
|
-
|
|
93
|
+
}
|
|
66
94
|
}
|
|
67
95
|
|
|
68
|
-
//
|
|
69
|
-
|
|
96
|
+
// Determine the Array constructor name to use
|
|
97
|
+
const arrayConstructor = hasArrayImport ? 'Array' : 'ReactiveArray';
|
|
98
|
+
|
|
99
|
+
// Transform class property declarations with type annotation = []
|
|
100
|
+
// Handles: items: Task[] = []; | items: Array<T> = [];
|
|
101
|
+
transformed = transformed.replace(
|
|
102
|
+
/(\w+)\s*:\s*(?:Array<[^>]+>|[^=\n]+\[\])\s*=\s*\[\s*\]/g,
|
|
103
|
+
`$1 = new ${arrayConstructor}()`
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
// Transform class property without type annotation (indented, at start of line)
|
|
107
|
+
// Skip local variables (const/let/var)
|
|
108
|
+
transformed = transformed.replace(
|
|
109
|
+
/^(\s+)(\w+)\s*=\s*\[\s*\](?=\s*[;\n])/gm,
|
|
110
|
+
`$1$2 = new ${arrayConstructor}()`
|
|
111
|
+
);
|
|
70
112
|
|
|
71
113
|
return transformed;
|
|
72
114
|
}
|
package/src/index.ts
CHANGED
|
@@ -453,6 +453,11 @@ export {
|
|
|
453
453
|
ReactiveMap,
|
|
454
454
|
ReactiveSet,
|
|
455
455
|
type Emitter,
|
|
456
|
+
// Level 2+: Rich queryable collection with rendering hints
|
|
457
|
+
Collection,
|
|
458
|
+
type RenderHint,
|
|
459
|
+
type RenderFormat,
|
|
460
|
+
type CompareOp,
|
|
456
461
|
} from './collections/index.js';
|
|
457
462
|
|
|
458
463
|
// ===== PURPOSE-DRIVEN UI TYPES =====
|