@stacksjs/stx 0.2.194 → 0.2.196
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/dist/chunk-0ae6chzd.js +4 -0
- package/dist/chunk-0w0mrtme.js +2 -0
- package/dist/chunk-0y46fbkx.js +3 -0
- package/dist/chunk-118a77bs.js +4471 -0
- package/dist/chunk-12nvcvtn.js +3 -0
- package/dist/chunk-14ynv5ah.js +2 -0
- package/dist/chunk-1e25ksp6.js +3 -0
- package/dist/chunk-1mhc4g86.js +43 -0
- package/dist/chunk-1thssz8x.js +2 -0
- package/dist/chunk-1yk07x7b.js +2 -0
- package/dist/chunk-2zkj69dg.js +13 -0
- package/dist/chunk-338316c2.js +2 -0
- package/dist/chunk-3952ykhb.js +1655 -0
- package/dist/chunk-3es366z4.js +3 -0
- package/dist/chunk-3m49eb4v.js +2 -0
- package/dist/chunk-3nmpfsw5.js +2 -0
- package/dist/chunk-3vy1q9da.js +2 -0
- package/dist/chunk-4a4h28pp.js +3022 -0
- package/dist/chunk-4pf61rcb.js +2 -0
- package/dist/chunk-5anpcnde.js +2 -0
- package/dist/chunk-5mkkre22.js +505 -0
- package/dist/chunk-5n0dt838.js +2 -0
- package/dist/chunk-5s2y052b.js +2 -0
- package/dist/chunk-5v64c8tr.js +2 -0
- package/dist/chunk-5xd7vge2.js +2 -0
- package/dist/chunk-6bxcjdp4.js +2 -0
- package/dist/chunk-6msvxnbr.js +2 -0
- package/dist/chunk-7kger0se.js +1280 -0
- package/dist/chunk-7ry55nms.js +3 -0
- package/dist/chunk-7s0wfa4e.js +24 -0
- package/dist/chunk-7w4c5w2b.js +2 -0
- package/dist/chunk-8src5smc.js +15 -0
- package/dist/chunk-937effkz.js +2 -0
- package/dist/chunk-9b82d4g5.js +3 -0
- package/dist/chunk-9xh78snm.js +3 -0
- package/dist/chunk-a2899x2t.js +2 -0
- package/dist/chunk-abpw3b39.js +2 -0
- package/dist/chunk-avwj12at.js +194 -0
- package/dist/chunk-b6jhznkh.js +71 -0
- package/dist/chunk-bpa2jeyw.js +3 -0
- package/dist/chunk-cbc56wpd.js +3 -0
- package/dist/chunk-dhpy0h9y.js +2 -0
- package/dist/chunk-dtmwr674.js +302 -0
- package/dist/chunk-dxfcyj8t.js +2 -0
- package/dist/chunk-e0460y7f.js +29 -0
- package/dist/chunk-ede7zpjf.js +2 -0
- package/dist/chunk-ehfzp7xe.js +2 -0
- package/dist/chunk-fd77g6t6.js +2 -0
- package/dist/chunk-fnkxbnka.js +30 -0
- package/dist/chunk-fvjh7rx0.js +84 -0
- package/dist/chunk-g19skgmw.js +18 -0
- package/dist/chunk-gcnrdgrq.js +3 -0
- package/dist/chunk-h2wpnxrh.js +8 -0
- package/dist/chunk-hb83d0qc.js +2 -0
- package/dist/chunk-hncp9gz6.js +8 -0
- package/dist/chunk-ht51fz7w.js +4 -0
- package/dist/chunk-hyrjade7.js +321 -0
- package/dist/chunk-hyyzp99b.js +2 -0
- package/dist/chunk-jpbk42j7.js +2 -0
- package/dist/chunk-jr5tp4ks.js +2 -0
- package/dist/chunk-k03b9z9e.js +898 -0
- package/dist/chunk-kjr22neh.js +3 -0
- package/dist/chunk-mjt5e2jy.js +2 -0
- package/dist/chunk-mkm91b6w.js +2 -0
- package/dist/chunk-mtr2f82n.js +2 -0
- package/dist/chunk-n8zpm7xb.js +397 -0
- package/dist/chunk-ndnc7k2n.js +22 -0
- package/dist/chunk-nenge2m2.js +2 -0
- package/dist/chunk-nngpktr2.js +2 -0
- package/dist/chunk-nsjyn31z.js +2 -0
- package/dist/chunk-nye9brje.js +147 -0
- package/dist/chunk-p5kp35dy.js +2 -0
- package/dist/chunk-pw0rzgpv.js +3 -0
- package/dist/chunk-q020t182.js +2 -0
- package/dist/chunk-q4gp9egt.js +4 -0
- package/dist/chunk-q8szpd6w.js +3 -0
- package/dist/chunk-q9n5swx7.js +3 -0
- package/dist/chunk-qpcwahse.js +2 -0
- package/dist/chunk-qz75kx6n.js +231 -0
- package/dist/chunk-r41nsgym.js +116 -0
- package/dist/chunk-r4qnvxhv.js +18 -0
- package/dist/chunk-r6p4ws0a.js +3809 -0
- package/dist/chunk-rdf7d42n.js +2 -0
- package/dist/chunk-rs2fa3g1.js +2 -0
- package/dist/chunk-ryjpp51z.js +499 -0
- package/dist/chunk-s018d32j.js +30 -0
- package/dist/chunk-spn8e3b3.js +2 -0
- package/dist/chunk-sry9atvv.js +2 -0
- package/dist/chunk-st5qggks.js +3 -0
- package/dist/chunk-tywvd1zc.js +11 -0
- package/dist/chunk-v3ryxw4v.js +2 -0
- package/dist/chunk-va9k88da.js +2 -0
- package/dist/chunk-vgk5v31k.js +216 -0
- package/dist/chunk-vj320kyr.js +2 -0
- package/dist/chunk-vr3qtsj4.js +2 -0
- package/dist/chunk-w0ng9e1m.js +341 -0
- package/dist/chunk-w29sv5jf.js +2 -0
- package/dist/chunk-whq99ytc.js +7987 -0
- package/dist/chunk-wnaddtv3.js +288 -0
- package/dist/chunk-wtengj6h.js +2 -0
- package/dist/chunk-x0ks1fer.js +1194 -0
- package/dist/chunk-x0zh76yv.js +4 -0
- package/dist/chunk-x13f5bbe.js +358 -0
- package/dist/chunk-xfp10a23.js +2 -0
- package/dist/chunk-xp3fp266.js +11 -0
- package/dist/chunk-xz019t7t.js +2 -0
- package/dist/chunk-y35rktea.js +12 -0
- package/dist/chunk-y4hkqsgw.js +4 -0
- package/dist/chunk-yab6d6kb.js +28 -0
- package/dist/chunk-yckd6ks5.js +3 -0
- package/dist/chunk-ycn1vnv2.js +5 -0
- package/dist/chunk-yf3bmmkk.js +8 -0
- package/dist/chunk-ygr11tgz.js +3 -0
- package/dist/chunk-ymccpdg6.js +351 -0
- package/dist/chunk-yr2wc2pa.js +3 -0
- package/dist/chunk-yv340gxj.js +2 -0
- package/dist/chunk-z2sp0k8d.js +3 -0
- package/dist/chunk-z5cm0bwn.js +11 -0
- package/dist/chunk-zda3hvde.js +14 -0
- package/dist/chunk-zgvga8rc.js +5 -0
- package/dist/chunk-zqbw8zqq.js +3 -0
- package/dist/cli.js +108 -27481
- package/dist/craft-entry.js +9 -5
- package/dist/craft.js +7 -18503
- package/dist/database.js +2 -2
- package/dist/pwa.js +8 -16603
- package/dist/ssg.js +52 -15512
- package/dist/variable-extractor.js +12 -2
- package/dist/visual-testing.js +1 -1
- package/package.json +5 -5
|
@@ -0,0 +1,1280 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import{a as x,b as u,c as b,d as Az,e as G,f as d,h as y,i as I,j as Uz,k as v}from"./chunk-0y46fbkx.js";import{l as h,m as f,n as g}from"./chunk-q9n5swx7.js";import{Mo as V}from"./chunk-r6p4ws0a.js";import"./chunk-8src5smc.js";import"./chunk-gcnrdgrq.js";import"./chunk-xp3fp266.js";import"./chunk-nye9brje.js";import"./chunk-tywvd1zc.js";import"./chunk-cbc56wpd.js";import"./chunk-x0zh76yv.js";import"./chunk-0ae6chzd.js";import"./chunk-kjr22neh.js";import"./chunk-3952ykhb.js";import"./chunk-dtmwr674.js";import{hu as F}from"./chunk-pw0rzgpv.js";import l from"fs";import R from"path";function m(z,J){let Z=[R.join(z.root,"stx.config.ts"),R.join(z.root,"stx.config.js"),R.join(z.root,"stx.config.mjs")],K=J.debounceMs??300,$=null,X=[];for(let B of Z)try{if(!l.existsSync(B))continue;let Y=l.watch(B,(W)=>{if(W==="change"){if($)clearTimeout($);$=setTimeout(async()=>{d("Config file changed, restarting...");try{await J.onChange()}catch(j){G(`Failed to restart: ${j}`)}},K)}});X.push(Y)}catch{}return{stop(){if($)clearTimeout($);for(let B of X)B.close()}}}import E from"fs";import i from"path";var Gz=".stx/story/analytics.json",Nz=1,c=1e4;class M{storage;storagePath;currentSession=null;dirty=!1;viewStartTimes=new Map;constructor(z){this.storagePath=i.join(z,Gz),this.storage=this.loadStorage(),this.startSession()}loadStorage(){try{let z=E.readFileSync(this.storagePath,"utf-8");return JSON.parse(z)}catch{return{version:Nz,events:[],sessions:[]}}}async save(){if(!this.dirty)return;let z=i.dirname(this.storagePath);if(await E.promises.mkdir(z,{recursive:!0}),this.storage.events.length>c)this.storage.events=this.storage.events.slice(-c);await E.promises.writeFile(this.storagePath,JSON.stringify(this.storage,null,2)),this.dirty=!1}startSession(){this.endSession(),this.currentSession={id:Hz(),startTime:Date.now(),componentViews:[]},this.storage.sessions.push(this.currentSession),this.dirty=!0}endSession(){if(this.currentSession)this.currentSession.endTime=Date.now(),this.currentSession=null}track(z){this.storage.events.push({...z,timestamp:Date.now()}),this.dirty=!0}trackView(z,J){if(this.endViewTiming(z),this.viewStartTimes.set(z,Date.now()),this.track({type:"view",componentId:z,variantId:J}),this.currentSession&&!this.currentSession.componentViews.includes(z))this.currentSession.componentViews.push(z)}endViewTiming(z){let J=this.viewStartTimes.get(z);if(J){let Z=Date.now()-J;this.track({type:"view",componentId:z,metadata:{duration:Z,endView:!0}}),this.viewStartTimes.delete(z)}}trackPropChange(z,J,Z){this.track({type:"prop_change",componentId:z,propName:J,metadata:{value:Z}})}trackVariantSwitch(z,J){this.track({type:"variant_switch",componentId:z,variantId:J})}trackSearch(z,J){this.track({type:"search",query:z,metadata:{resultCount:J}})}getComponentAnalytics(z,J){let Z=this.storage.events.filter((Q)=>Q.componentId===z),K=Z.filter((Q)=>Q.type==="view"&&!Q.metadata?.endView),$=Z.filter((Q)=>Q.type==="prop_change"),X=Z.filter((Q)=>Q.type==="variant_switch"),B={};for(let Q of $)if(Q.propName)B[Q.propName]=(B[Q.propName]||0)+1;let Y={};for(let Q of X)if(Q.variantId)Y[Q.variantId]=(Y[Q.variantId]||0)+1;let W=new Set(this.storage.sessions.filter((Q)=>Q.componentViews.includes(z)).map((Q)=>Q.id)),j=Z.filter((Q)=>Q.metadata?.duration).map((Q)=>Q.metadata.duration),U=j.length>0?j.reduce((Q,D)=>Q+D,0)/j.length:0,_=Object.entries(B).sort((Q,D)=>D[1]-Q[1]).slice(0,5).map(([Q])=>Q);return{componentId:z,componentName:J,viewCount:K.length,uniqueViews:W.size,lastViewed:K[K.length-1]?.timestamp,propChanges:B,topChangedProps:_,variantViews:Y,avgTimeSpent:U}}getMostViewedComponents(z=10){let J=new Map;for(let Z of this.storage.events)if(Z.type==="view"&&Z.componentId&&!Z.metadata?.endView)J.set(Z.componentId,(J.get(Z.componentId)||0)+1);return Array.from(J.entries()).map(([Z,K])=>({componentId:Z,viewCount:K})).sort((Z,K)=>K.viewCount-Z.viewCount).slice(0,z)}getMostChangedProps(z=10){let J=new Map;for(let Z of this.storage.events)if(Z.type==="prop_change"&&Z.propName)J.set(Z.propName,(J.get(Z.propName)||0)+1);return Array.from(J.entries()).map(([Z,K])=>({propName:Z,changeCount:K})).sort((Z,K)=>K.changeCount-Z.changeCount).slice(0,z)}getSearchAnalytics(){let z=this.storage.events.filter(($)=>$.type==="search"),J=new Map,Z=0;for(let $ of z){if($.query)J.set($.query,(J.get($.query)||0)+1);if($.metadata?.resultCount!==void 0)Z+=$.metadata.resultCount}let K=Array.from(J.entries()).map(([$,X])=>({query:$,count:X})).sort(($,X)=>X.count-$.count).slice(0,10);return{totalSearches:z.length,topQueries:K,avgResultCount:z.length>0?Z/z.length:0}}clear(){this.storage.events=[],this.storage.sessions=[],this.dirty=!0,this.startSession()}exportAnalytics(){return JSON.stringify({exportedAt:Date.now(),...this.storage},null,2)}}function Hz(){return`session-${Date.now()}-${Math.random().toString(36).slice(2,8)}`}import P from"fs";import p from"path";var Fz=".stx/story/bookmarks.json",Vz=1;class T{storage;storagePath;dirty=!1;constructor(z){this.storagePath=p.join(z,Fz),this.storage=this.loadStorage()}loadStorage(){try{let z=P.readFileSync(this.storagePath,"utf-8");return JSON.parse(z)}catch{return{version:Vz,bookmarks:[]}}}async save(){if(!this.dirty)return;let z=p.dirname(this.storagePath);await P.promises.mkdir(z,{recursive:!0}),await P.promises.writeFile(this.storagePath,JSON.stringify(this.storage,null,2)),this.dirty=!1}addBookmark(z,J,Z,K={}){let $={id:Rz(),name:K.name||`${z}/${J}`,storyId:z,variantId:J,props:Z,created:Date.now(),notes:K.notes,color:K.color};return this.storage.bookmarks.push($),this.dirty=!0,$}getAllBookmarks(){return[...this.storage.bookmarks]}getBookmarksForStory(z){return this.storage.bookmarks.filter((J)=>J.storyId===z)}getBookmark(z){return this.storage.bookmarks.find((J)=>J.id===z)}isBookmarked(z,J){return this.storage.bookmarks.some((Z)=>Z.storyId===z&&Z.variantId===J)}updateBookmark(z,J){let Z=this.getBookmark(z);if(!Z)return;return Object.assign(Z,J),this.dirty=!0,Z}deleteBookmark(z){let J=this.storage.bookmarks.findIndex((Z)=>Z.id===z);if(J===-1)return!1;return this.storage.bookmarks.splice(J,1),this.dirty=!0,!0}toggleBookmark(z,J,Z){let K=this.storage.bookmarks.find((X)=>X.storyId===z&&X.variantId===J);if(K)return this.deleteBookmark(K.id),{added:!1};return{added:!0,bookmark:this.addBookmark(z,J,Z)}}exportBookmarks(){return JSON.stringify(this.storage,null,2)}importBookmarks(z,J=!0){let Z=JSON.parse(z),K=0;for(let $ of Z.bookmarks){if(J){let X=this.storage.bookmarks.find((B)=>B.id===$.id);if(X)Object.assign(X,$);else this.storage.bookmarks.push($)}else this.storage.bookmarks.push($);K++}if(K>0)this.dirty=!0;return K}}function Rz(){return`bm-${Date.now()}-${Math.random().toString(36).slice(2,8)}`}var N=new Set;function s(){return{open(z){N.add(z),z.send(JSON.stringify({type:"connected",timestamp:Date.now()}))},close(z){N.delete(z)},message(z,J){}}}function q(z){let J=JSON.stringify({...z,timestamp:z.timestamp||Date.now()});for(let Z of N)try{Z.send(J)}catch{N.delete(Z)}}function n(z,J){q({type:"story-update",data:{storyId:z,...J}})}function a(z,J){q({type:"story-add",data:{storyId:z,...J}})}function o(z){q({type:"story-remove",data:{storyId:z}})}function r(z){return`
|
|
3
|
+
<script>
|
|
4
|
+
(function() {
|
|
5
|
+
const ws = new WebSocket('${z}');
|
|
6
|
+
|
|
7
|
+
ws.onopen = function() {
|
|
8
|
+
console.log('[STX Story] HMR connected');
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
ws.onmessage = function(event) {
|
|
12
|
+
const message = JSON.parse(event.data);
|
|
13
|
+
|
|
14
|
+
switch (message.type) {
|
|
15
|
+
case 'connected':
|
|
16
|
+
console.log('[STX Story] Connected to HMR server');
|
|
17
|
+
break;
|
|
18
|
+
|
|
19
|
+
case 'story-update':
|
|
20
|
+
console.log('[STX Story] Story updated:', message.data.storyId);
|
|
21
|
+
// Reload the current story if it matches
|
|
22
|
+
if (window.__stxStory && window.__stxStory.currentStoryId === message.data.storyId) {
|
|
23
|
+
window.__stxStory.reloadCurrentStory();
|
|
24
|
+
}
|
|
25
|
+
break;
|
|
26
|
+
|
|
27
|
+
case 'story-add':
|
|
28
|
+
console.log('[STX Story] Story added:', message.data.storyId);
|
|
29
|
+
window.__stxStory && window.__stxStory.refreshStoryList();
|
|
30
|
+
break;
|
|
31
|
+
|
|
32
|
+
case 'story-remove':
|
|
33
|
+
console.log('[STX Story] Story removed:', message.data.storyId);
|
|
34
|
+
window.__stxStory && window.__stxStory.refreshStoryList();
|
|
35
|
+
break;
|
|
36
|
+
|
|
37
|
+
case 'config-change':
|
|
38
|
+
console.log('[STX Story] Config changed, reloading...');
|
|
39
|
+
location.reload();
|
|
40
|
+
break;
|
|
41
|
+
|
|
42
|
+
case 'full-reload':
|
|
43
|
+
console.log('[STX Story] Full reload requested');
|
|
44
|
+
location.reload();
|
|
45
|
+
break;
|
|
46
|
+
|
|
47
|
+
case 'error':
|
|
48
|
+
console.error('[STX Story] Error:', message.data.error);
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
ws.onclose = function() {
|
|
54
|
+
console.log('[STX Story] HMR disconnected, attempting reconnect...');
|
|
55
|
+
setTimeout(function() {
|
|
56
|
+
location.reload();
|
|
57
|
+
}, 1000);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
ws.onerror = function(error) {
|
|
61
|
+
console.error('[STX Story] WebSocket error:', error);
|
|
62
|
+
};
|
|
63
|
+
})();
|
|
64
|
+
</script>
|
|
65
|
+
`}function e(){return`
|
|
66
|
+
<script>
|
|
67
|
+
(function() {
|
|
68
|
+
// Hot swap state
|
|
69
|
+
window.__stxHotSwap = {
|
|
70
|
+
storyId: null,
|
|
71
|
+
variantId: null,
|
|
72
|
+
props: {},
|
|
73
|
+
iframe: null,
|
|
74
|
+
|
|
75
|
+
// Initialize hot swap
|
|
76
|
+
init: function(iframeSelector) {
|
|
77
|
+
this.iframe = document.querySelector(iframeSelector);
|
|
78
|
+
if (!this.iframe) {
|
|
79
|
+
console.warn('[STX Story] Hot swap iframe not found');
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Listen for variant changes
|
|
84
|
+
window.addEventListener('stx:variant-change', this.handleVariantChange.bind(this));
|
|
85
|
+
window.addEventListener('stx:props-change', this.handlePropsChange.bind(this));
|
|
86
|
+
|
|
87
|
+
console.log('[STX Story] Hot swap initialized');
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
// Handle variant change
|
|
91
|
+
handleVariantChange: function(event) {
|
|
92
|
+
const { storyId, variantId } = event.detail;
|
|
93
|
+
|
|
94
|
+
if (this.storyId === storyId && this.variantId === variantId) {
|
|
95
|
+
return; // No change
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
this.storyId = storyId;
|
|
99
|
+
this.variantId = variantId;
|
|
100
|
+
|
|
101
|
+
this.swapContent();
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
// Handle props change
|
|
105
|
+
handlePropsChange: function(event) {
|
|
106
|
+
const { props } = event.detail;
|
|
107
|
+
this.props = { ...this.props, ...props };
|
|
108
|
+
this.updateProps();
|
|
109
|
+
},
|
|
110
|
+
|
|
111
|
+
// Swap iframe content without reload
|
|
112
|
+
swapContent: async function() {
|
|
113
|
+
if (!this.iframe || !this.storyId || !this.variantId) return;
|
|
114
|
+
|
|
115
|
+
// Save scroll position
|
|
116
|
+
const scrollTop = this.iframe.contentWindow?.scrollY || 0;
|
|
117
|
+
const scrollLeft = this.iframe.contentWindow?.scrollX || 0;
|
|
118
|
+
|
|
119
|
+
// Fetch new content
|
|
120
|
+
try {
|
|
121
|
+
const response = await fetch('/api/render/' + this.storyId + '/' + this.variantId);
|
|
122
|
+
const data = await response.json();
|
|
123
|
+
|
|
124
|
+
if (data.html) {
|
|
125
|
+
// Fade out
|
|
126
|
+
this.iframe.style.opacity = '0.5';
|
|
127
|
+
this.iframe.style.transition = 'opacity 150ms ease';
|
|
128
|
+
|
|
129
|
+
// Update content
|
|
130
|
+
const doc = this.iframe.contentDocument;
|
|
131
|
+
if (doc) {
|
|
132
|
+
// Find body or create one
|
|
133
|
+
let body = doc.body;
|
|
134
|
+
if (!body) {
|
|
135
|
+
doc.write('<html><head></head><body></body></html>');
|
|
136
|
+
body = doc.body;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Update body content
|
|
140
|
+
body.innerHTML = data.html;
|
|
141
|
+
|
|
142
|
+
// Update styles if provided
|
|
143
|
+
if (data.css) {
|
|
144
|
+
let styleEl = doc.getElementById('stx-component-styles');
|
|
145
|
+
if (!styleEl) {
|
|
146
|
+
styleEl = doc.createElement('style');
|
|
147
|
+
styleEl.id = 'stx-component-styles';
|
|
148
|
+
doc.head.appendChild(styleEl);
|
|
149
|
+
}
|
|
150
|
+
styleEl.textContent = data.css;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Restore scroll position
|
|
154
|
+
this.iframe.contentWindow?.scrollTo(scrollLeft, scrollTop);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Fade in
|
|
158
|
+
setTimeout(() => {
|
|
159
|
+
this.iframe.style.opacity = '1';
|
|
160
|
+
}, 50);
|
|
161
|
+
|
|
162
|
+
// Dispatch event
|
|
163
|
+
window.dispatchEvent(new CustomEvent('stx:swap-complete', {
|
|
164
|
+
detail: { storyId: this.storyId, variantId: this.variantId }
|
|
165
|
+
}));
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
console.error('[STX Story] Hot swap failed:', error);
|
|
170
|
+
// Fallback to full reload
|
|
171
|
+
this.iframe.src = '/preview/' + this.storyId + '/' + this.variantId;
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
|
|
175
|
+
// Update props in iframe without content swap
|
|
176
|
+
updateProps: function() {
|
|
177
|
+
if (!this.iframe) return;
|
|
178
|
+
|
|
179
|
+
const win = this.iframe.contentWindow;
|
|
180
|
+
if (win && win.__stxProps) {
|
|
181
|
+
Object.assign(win.__stxProps, this.props);
|
|
182
|
+
|
|
183
|
+
// Trigger re-render if component supports it
|
|
184
|
+
if (win.__stxRerender) {
|
|
185
|
+
win.__stxRerender(this.props);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
window.dispatchEvent(new CustomEvent('stx:props-updated', {
|
|
189
|
+
detail: { props: this.props }
|
|
190
|
+
}));
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
|
|
194
|
+
// Select a variant
|
|
195
|
+
selectVariant: function(storyId, variantId) {
|
|
196
|
+
window.dispatchEvent(new CustomEvent('stx:variant-change', {
|
|
197
|
+
detail: { storyId, variantId }
|
|
198
|
+
}));
|
|
199
|
+
},
|
|
200
|
+
|
|
201
|
+
// Update a prop
|
|
202
|
+
updateProp: function(name, value) {
|
|
203
|
+
window.dispatchEvent(new CustomEvent('stx:props-change', {
|
|
204
|
+
detail: { props: { [name]: value } }
|
|
205
|
+
}));
|
|
206
|
+
},
|
|
207
|
+
|
|
208
|
+
// Reset props to defaults
|
|
209
|
+
resetProps: function() {
|
|
210
|
+
this.props = {};
|
|
211
|
+
this.swapContent();
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
// Auto-initialize when DOM is ready
|
|
216
|
+
if (document.readyState === 'loading') {
|
|
217
|
+
document.addEventListener('DOMContentLoaded', function() {
|
|
218
|
+
window.__stxHotSwap.init('#preview-iframe');
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
window.__stxHotSwap.init('#preview-iframe');
|
|
223
|
+
}
|
|
224
|
+
})();
|
|
225
|
+
</script>
|
|
226
|
+
`}var t=[{key:"k",modifiers:["meta"],description:"Open search",category:"Navigation",action:"openSearch"},{key:"ArrowUp",description:"Previous story",category:"Navigation",action:"prevStory"},{key:"ArrowDown",description:"Next story",category:"Navigation",action:"nextStory"},{key:"ArrowLeft",description:"Previous variant",category:"Navigation",action:"prevVariant"},{key:"ArrowRight",description:"Next variant",category:"Navigation",action:"nextVariant"},{key:"Enter",description:"Select story",category:"Navigation",action:"selectStory"},{key:"Escape",description:"Close panel/modal",category:"Navigation",action:"closePanel"},{key:"d",description:"Toggle dark mode",category:"View",action:"toggleDarkMode"},{key:"f",description:"Toggle fullscreen",category:"View",action:"toggleFullscreen"},{key:"s",description:"Toggle sidebar",category:"View",action:"toggleSidebar"},{key:"a",description:"Toggle addons panel",category:"View",action:"toggleAddons"},{key:"z",description:"Zoom in",category:"View",action:"zoomIn"},{key:"x",description:"Zoom out",category:"View",action:"zoomOut"},{key:"0",description:"Reset zoom",category:"View",action:"resetZoom"},{key:"r",description:"Reload story",category:"Actions",action:"reloadStory"},{key:"c",description:"Copy story code",category:"Actions",action:"copyCode"},{key:"p",description:"Open props panel",category:"Actions",action:"openProps"},{key:"b",description:"Toggle bookmark",category:"Actions",action:"toggleBookmark"},{key:"t",description:"Run tests",category:"Actions",action:"runTests"},{key:"?",description:"Show shortcuts",category:"Help",action:"showShortcuts"}];function Ez(z){let J=[];if(z.modifiers)for(let K of z.modifiers)switch(K){case"meta":J.push("\u2318");break;case"ctrl":J.push("Ctrl");break;case"alt":J.push("Alt");break;case"shift":J.push("\u21E7");break}let Z=z.key;switch(Z){case"ArrowUp":Z="\u2191";break;case"ArrowDown":Z="\u2193";break;case"ArrowLeft":Z="\u2190";break;case"ArrowRight":Z="\u2192";break;case"Enter":Z="\u21B5";break;case"Escape":Z="Esc";break;default:Z=Z.toUpperCase()}return J.push(Z),J.join(" + ")}function zz(z=t){let J=new Map;for(let K of z){let $=J.get(K.category)||[];$.push(K),J.set(K.category,$)}return`
|
|
227
|
+
<div class="shortcuts-modal" id="shortcuts-modal" style="display: none;">
|
|
228
|
+
<div class="shortcuts-backdrop" onclick="closeShortcutsModal()"></div>
|
|
229
|
+
<div class="shortcuts-content">
|
|
230
|
+
<div class="shortcuts-header">
|
|
231
|
+
<h3>Keyboard Shortcuts</h3>
|
|
232
|
+
<button class="shortcuts-close" onclick="closeShortcutsModal()">\xD7</button>
|
|
233
|
+
</div>
|
|
234
|
+
<div class="shortcuts-body">
|
|
235
|
+
${Array.from(J.entries()).map(([K,$])=>`
|
|
236
|
+
<div class="shortcuts-category">
|
|
237
|
+
<h4>${K}</h4>
|
|
238
|
+
<div class="shortcuts-list">
|
|
239
|
+
${$.map((X)=>`
|
|
240
|
+
<div class="shortcut-item">
|
|
241
|
+
<span class="shortcut-desc">${X.description}</span>
|
|
242
|
+
<kbd class="shortcut-key">${Ez(X)}</kbd>
|
|
243
|
+
</div>
|
|
244
|
+
`).join("")}
|
|
245
|
+
</div>
|
|
246
|
+
</div>
|
|
247
|
+
`).join("")}
|
|
248
|
+
</div>
|
|
249
|
+
</div>
|
|
250
|
+
</div>
|
|
251
|
+
`}function Jz(){return`
|
|
252
|
+
.shortcuts-modal {
|
|
253
|
+
position: fixed;
|
|
254
|
+
inset: 0;
|
|
255
|
+
z-index: 1000;
|
|
256
|
+
display: flex;
|
|
257
|
+
align-items: center;
|
|
258
|
+
justify-content: center;
|
|
259
|
+
}
|
|
260
|
+
.shortcuts-backdrop {
|
|
261
|
+
position: absolute;
|
|
262
|
+
inset: 0;
|
|
263
|
+
background: rgba(0, 0, 0, 0.5);
|
|
264
|
+
}
|
|
265
|
+
.shortcuts-content {
|
|
266
|
+
position: relative;
|
|
267
|
+
background: var(--bg);
|
|
268
|
+
border-radius: 12px;
|
|
269
|
+
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
|
|
270
|
+
max-width: 600px;
|
|
271
|
+
max-height: 80vh;
|
|
272
|
+
overflow: hidden;
|
|
273
|
+
display: flex;
|
|
274
|
+
flex-direction: column;
|
|
275
|
+
}
|
|
276
|
+
.shortcuts-header {
|
|
277
|
+
display: flex;
|
|
278
|
+
justify-content: space-between;
|
|
279
|
+
align-items: center;
|
|
280
|
+
padding: 16px 20px;
|
|
281
|
+
border-bottom: 1px solid var(--border);
|
|
282
|
+
}
|
|
283
|
+
.shortcuts-header h3 {
|
|
284
|
+
margin: 0;
|
|
285
|
+
font-size: 16px;
|
|
286
|
+
}
|
|
287
|
+
.shortcuts-close {
|
|
288
|
+
background: none;
|
|
289
|
+
border: none;
|
|
290
|
+
font-size: 24px;
|
|
291
|
+
cursor: pointer;
|
|
292
|
+
color: var(--text-secondary);
|
|
293
|
+
padding: 0;
|
|
294
|
+
line-height: 1;
|
|
295
|
+
}
|
|
296
|
+
.shortcuts-body {
|
|
297
|
+
padding: 20px;
|
|
298
|
+
overflow-y: auto;
|
|
299
|
+
display: grid;
|
|
300
|
+
grid-template-columns: repeat(2, 1fr);
|
|
301
|
+
gap: 24px;
|
|
302
|
+
}
|
|
303
|
+
.shortcuts-category h4 {
|
|
304
|
+
margin: 0 0 12px;
|
|
305
|
+
font-size: 12px;
|
|
306
|
+
text-transform: uppercase;
|
|
307
|
+
color: var(--text-secondary);
|
|
308
|
+
letter-spacing: 0.5px;
|
|
309
|
+
}
|
|
310
|
+
.shortcuts-list {
|
|
311
|
+
display: flex;
|
|
312
|
+
flex-direction: column;
|
|
313
|
+
gap: 8px;
|
|
314
|
+
}
|
|
315
|
+
.shortcut-item {
|
|
316
|
+
display: flex;
|
|
317
|
+
justify-content: space-between;
|
|
318
|
+
align-items: center;
|
|
319
|
+
gap: 12px;
|
|
320
|
+
}
|
|
321
|
+
.shortcut-desc {
|
|
322
|
+
font-size: 13px;
|
|
323
|
+
color: var(--text);
|
|
324
|
+
}
|
|
325
|
+
.shortcut-key {
|
|
326
|
+
font-family: monospace;
|
|
327
|
+
font-size: 11px;
|
|
328
|
+
padding: 4px 8px;
|
|
329
|
+
background: var(--bg-secondary);
|
|
330
|
+
border: 1px solid var(--border);
|
|
331
|
+
border-radius: 4px;
|
|
332
|
+
color: var(--text-secondary);
|
|
333
|
+
white-space: nowrap;
|
|
334
|
+
}
|
|
335
|
+
`}function Zz(){return`
|
|
336
|
+
<script>
|
|
337
|
+
window.__stxShortcuts = {
|
|
338
|
+
shortcuts: ${JSON.stringify(t)},
|
|
339
|
+
|
|
340
|
+
init() {
|
|
341
|
+
document.addEventListener('keydown', this.handleKeydown.bind(this));
|
|
342
|
+
},
|
|
343
|
+
|
|
344
|
+
handleKeydown(event) {
|
|
345
|
+
// Don't handle if typing in input
|
|
346
|
+
if (event.target.tagName === 'INPUT' || event.target.tagName === 'TEXTAREA') {
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const shortcut = this.findShortcut(event);
|
|
351
|
+
if (shortcut) {
|
|
352
|
+
event.preventDefault();
|
|
353
|
+
this.executeAction(shortcut.action);
|
|
354
|
+
}
|
|
355
|
+
},
|
|
356
|
+
|
|
357
|
+
findShortcut(event) {
|
|
358
|
+
return this.shortcuts.find(s => {
|
|
359
|
+
if (s.key.toLowerCase() !== event.key.toLowerCase()) return false;
|
|
360
|
+
|
|
361
|
+
const mods = s.modifiers || [];
|
|
362
|
+
if (mods.includes('meta') !== event.metaKey) return false;
|
|
363
|
+
if (mods.includes('ctrl') !== event.ctrlKey) return false;
|
|
364
|
+
if (mods.includes('alt') !== event.altKey) return false;
|
|
365
|
+
if (mods.includes('shift') !== event.shiftKey) return false;
|
|
366
|
+
|
|
367
|
+
return true;
|
|
368
|
+
});
|
|
369
|
+
},
|
|
370
|
+
|
|
371
|
+
executeAction(action) {
|
|
372
|
+
switch (action) {
|
|
373
|
+
case 'openSearch':
|
|
374
|
+
document.getElementById('search-input')?.focus();
|
|
375
|
+
break;
|
|
376
|
+
case 'prevStory':
|
|
377
|
+
this.navigateStory(-1);
|
|
378
|
+
break;
|
|
379
|
+
case 'nextStory':
|
|
380
|
+
this.navigateStory(1);
|
|
381
|
+
break;
|
|
382
|
+
case 'prevVariant':
|
|
383
|
+
this.navigateVariant(-1);
|
|
384
|
+
break;
|
|
385
|
+
case 'nextVariant':
|
|
386
|
+
this.navigateVariant(1);
|
|
387
|
+
break;
|
|
388
|
+
case 'selectStory':
|
|
389
|
+
document.querySelector('.story-item.focused')?.click();
|
|
390
|
+
break;
|
|
391
|
+
case 'closePanel':
|
|
392
|
+
this.closeActivePanel();
|
|
393
|
+
break;
|
|
394
|
+
case 'toggleDarkMode':
|
|
395
|
+
document.documentElement.classList.toggle('dark');
|
|
396
|
+
break;
|
|
397
|
+
case 'toggleFullscreen':
|
|
398
|
+
this.toggleFullscreen();
|
|
399
|
+
break;
|
|
400
|
+
case 'toggleSidebar':
|
|
401
|
+
document.querySelector('.sidebar')?.classList.toggle('hidden');
|
|
402
|
+
break;
|
|
403
|
+
case 'toggleAddons':
|
|
404
|
+
document.querySelector('.addons-panel')?.classList.toggle('hidden');
|
|
405
|
+
break;
|
|
406
|
+
case 'zoomIn':
|
|
407
|
+
window.__stxStory?.zoomIn?.();
|
|
408
|
+
break;
|
|
409
|
+
case 'zoomOut':
|
|
410
|
+
window.__stxStory?.zoomOut?.();
|
|
411
|
+
break;
|
|
412
|
+
case 'resetZoom':
|
|
413
|
+
window.__stxStory?.resetZoom?.();
|
|
414
|
+
break;
|
|
415
|
+
case 'reloadStory':
|
|
416
|
+
window.__stxStory?.reloadCurrentStory?.();
|
|
417
|
+
break;
|
|
418
|
+
case 'copyCode':
|
|
419
|
+
window.__stxStory?.copyCode?.();
|
|
420
|
+
break;
|
|
421
|
+
case 'openProps':
|
|
422
|
+
document.querySelector('[data-panel="props"]')?.click();
|
|
423
|
+
break;
|
|
424
|
+
case 'toggleBookmark':
|
|
425
|
+
window.__stxBookmarks?.toggleCurrent?.();
|
|
426
|
+
break;
|
|
427
|
+
case 'runTests':
|
|
428
|
+
window.__stxStory?.runTests?.();
|
|
429
|
+
break;
|
|
430
|
+
case 'showShortcuts':
|
|
431
|
+
this.showModal();
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
},
|
|
435
|
+
|
|
436
|
+
navigateStory(direction) {
|
|
437
|
+
const items = Array.from(document.querySelectorAll('.story-item'));
|
|
438
|
+
const current = document.querySelector('.story-item.active');
|
|
439
|
+
const currentIndex = current ? items.indexOf(current) : -1;
|
|
440
|
+
const nextIndex = Math.max(0, Math.min(items.length - 1, currentIndex + direction));
|
|
441
|
+
items[nextIndex]?.click();
|
|
442
|
+
},
|
|
443
|
+
|
|
444
|
+
navigateVariant(direction) {
|
|
445
|
+
const buttons = Array.from(document.querySelectorAll('.variant-btn'));
|
|
446
|
+
const current = document.querySelector('.variant-btn.active');
|
|
447
|
+
const currentIndex = current ? buttons.indexOf(current) : -1;
|
|
448
|
+
const nextIndex = Math.max(0, Math.min(buttons.length - 1, currentIndex + direction));
|
|
449
|
+
buttons[nextIndex]?.click();
|
|
450
|
+
},
|
|
451
|
+
|
|
452
|
+
closeActivePanel() {
|
|
453
|
+
const modal = document.querySelector('.shortcuts-modal[style*="flex"]');
|
|
454
|
+
if (modal) {
|
|
455
|
+
closeShortcutsModal();
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
// Close other panels
|
|
459
|
+
document.querySelector('.panel.open')?.classList.remove('open');
|
|
460
|
+
},
|
|
461
|
+
|
|
462
|
+
toggleFullscreen() {
|
|
463
|
+
if (document.fullscreenElement) {
|
|
464
|
+
document.exitFullscreen();
|
|
465
|
+
}
|
|
466
|
+
else {
|
|
467
|
+
document.documentElement.requestFullscreen();
|
|
468
|
+
}
|
|
469
|
+
},
|
|
470
|
+
|
|
471
|
+
showModal() {
|
|
472
|
+
const modal = document.getElementById('shortcuts-modal');
|
|
473
|
+
if (modal) modal.style.display = 'flex';
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
function closeShortcutsModal() {
|
|
478
|
+
const modal = document.getElementById('shortcuts-modal');
|
|
479
|
+
if (modal) modal.style.display = 'none';
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
// Initialize on load
|
|
483
|
+
if (document.readyState === 'loading') {
|
|
484
|
+
document.addEventListener('DOMContentLoaded', () => window.__stxShortcuts.init());
|
|
485
|
+
}
|
|
486
|
+
else {
|
|
487
|
+
window.__stxShortcuts.init();
|
|
488
|
+
}
|
|
489
|
+
</script>
|
|
490
|
+
`}class $z{metrics=new Map;maxHistorySize=100;enabled=!0;setEnabled(z){this.enabled=z}recordRender(z,J,Z,K){if(!this.enabled)return;let $=this.metrics.get(z);if(!$)$={componentId:z,componentName:J,renderCount:0,totalRenderTime:0,avgRenderTime:0,minRenderTime:1/0,maxRenderTime:0,lastRenderTime:0,history:[]},this.metrics.set(z,$);$.renderCount++,$.totalRenderTime+=Z,$.avgRenderTime=$.totalRenderTime/$.renderCount,$.minRenderTime=Math.min($.minRenderTime,Z),$.maxRenderTime=Math.max($.maxRenderTime,Z),$.lastRenderTime=Z;let X=this.getMemoryUsage();if(X!==void 0)$.memoryUsage=X;if($.history.push({timestamp:Date.now(),duration:Z,props:K,memory:X}),$.history.length>this.maxHistorySize)$.history=$.history.slice(-this.maxHistorySize)}getMetrics(z){return this.metrics.get(z)}getAllMetrics(){return Array.from(this.metrics.values())}getSlowestComponents(z=10){return this.getAllMetrics().sort((J,Z)=>Z.avgRenderTime-J.avgRenderTime).slice(0,z)}getMostRenderedComponents(z=10){return this.getAllMetrics().sort((J,Z)=>Z.renderCount-J.renderCount).slice(0,z)}clear(){this.metrics.clear()}clearComponent(z){this.metrics.delete(z)}getMemoryUsage(){if(typeof performance<"u"&&"memory"in performance)return performance.memory?.usedJSHeapSize;return}exportMetrics(){return JSON.stringify({timestamp:Date.now(),metrics:this.getAllMetrics()},null,2)}createTimer(z,J){return new Yz(this,z,J)}}class Yz{profiler;componentId;componentName;startTime;props;constructor(z,J,Z){this.profiler=z;this.componentId=J;this.componentName=Z;this.startTime=performance.now()}setProps(z){return this.props=z,this}end(){let z=performance.now()-this.startTime;return this.profiler.recordRender(this.componentId,this.componentName,z,this.props),z}}var tz=new $z;function Kz(){return`
|
|
491
|
+
<script>
|
|
492
|
+
window.__stxPerformance = {
|
|
493
|
+
metrics: new Map(),
|
|
494
|
+
|
|
495
|
+
recordRender(componentId, componentName, duration, props) {
|
|
496
|
+
let m = this.metrics.get(componentId);
|
|
497
|
+
if (!m) {
|
|
498
|
+
m = {
|
|
499
|
+
componentId,
|
|
500
|
+
componentName,
|
|
501
|
+
renderCount: 0,
|
|
502
|
+
totalRenderTime: 0,
|
|
503
|
+
avgRenderTime: 0,
|
|
504
|
+
minRenderTime: Infinity,
|
|
505
|
+
maxRenderTime: 0,
|
|
506
|
+
lastRenderTime: 0,
|
|
507
|
+
history: []
|
|
508
|
+
};
|
|
509
|
+
this.metrics.set(componentId, m);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
m.renderCount++;
|
|
513
|
+
m.totalRenderTime += duration;
|
|
514
|
+
m.avgRenderTime = m.totalRenderTime / m.renderCount;
|
|
515
|
+
m.minRenderTime = Math.min(m.minRenderTime, duration);
|
|
516
|
+
m.maxRenderTime = Math.max(m.maxRenderTime, duration);
|
|
517
|
+
m.lastRenderTime = duration;
|
|
518
|
+
|
|
519
|
+
m.history.push({ timestamp: Date.now(), duration, props });
|
|
520
|
+
if (m.history.length > 100) m.history.shift();
|
|
521
|
+
|
|
522
|
+
this.updatePanel();
|
|
523
|
+
},
|
|
524
|
+
|
|
525
|
+
getMetrics() {
|
|
526
|
+
return Array.from(this.metrics.values());
|
|
527
|
+
},
|
|
528
|
+
|
|
529
|
+
clear() {
|
|
530
|
+
this.metrics.clear();
|
|
531
|
+
this.updatePanel();
|
|
532
|
+
},
|
|
533
|
+
|
|
534
|
+
updatePanel() {
|
|
535
|
+
const panel = document.getElementById('performance-panel');
|
|
536
|
+
if (panel) {
|
|
537
|
+
// Update panel content
|
|
538
|
+
window.dispatchEvent(new CustomEvent('stx:performance-update', {
|
|
539
|
+
detail: { metrics: this.getMetrics() }
|
|
540
|
+
}));
|
|
541
|
+
}
|
|
542
|
+
},
|
|
543
|
+
|
|
544
|
+
// Timer helper
|
|
545
|
+
startTimer(componentId, componentName) {
|
|
546
|
+
const start = performance.now();
|
|
547
|
+
return {
|
|
548
|
+
end: (props) => {
|
|
549
|
+
const duration = performance.now() - start;
|
|
550
|
+
this.recordRender(componentId, componentName, duration, props);
|
|
551
|
+
return duration;
|
|
552
|
+
}
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
};
|
|
556
|
+
|
|
557
|
+
window.clearPerformanceMetrics = () => window.__stxPerformance.clear();
|
|
558
|
+
</script>
|
|
559
|
+
`}import w from"fs";import Xz from"path";var Mz=".stx/story/presets.json",C=1,Pz=10;class k{storage;storagePath;dirty=!1;constructor(z){this.storagePath=Xz.join(z,Mz),this.storage=this.loadStorage()}loadStorage(){try{let z=w.readFileSync(this.storagePath,"utf-8"),J=JSON.parse(z);if(J.version!==C)return this.migrateStorage(J);return J}catch{return{version:C,presets:[],recentIds:[]}}}migrateStorage(z){return{version:C,presets:z.presets||[],recentIds:z.recentIds||[]}}async save(){if(!this.dirty)return;let z=Xz.dirname(this.storagePath);await w.promises.mkdir(z,{recursive:!0}),await w.promises.writeFile(this.storagePath,JSON.stringify(this.storage,null,2)),this.dirty=!1}createPreset(z,J,Z={}){let K={id:Tz(),name:z,description:Z.description,props:J,componentId:Z.componentId,created:Date.now(),usageCount:0,tags:Z.tags};return this.storage.presets.push(K),this.dirty=!0,K}getPreset(z){return this.storage.presets.find((J)=>J.id===z)}getAllPresets(){return[...this.storage.presets]}getPresetsForComponent(z){return this.storage.presets.filter((J)=>!J.componentId||J.componentId===z)}getRecentPresets(){return this.storage.recentIds.map((z)=>this.getPreset(z)).filter((z)=>z!==void 0)}usePreset(z){let J=this.getPreset(z);if(!J)return;return J.lastUsed=Date.now(),J.usageCount++,this.storage.recentIds=[z,...this.storage.recentIds.filter((Z)=>Z!==z)].slice(0,Pz),this.dirty=!0,J}updatePreset(z,J){let Z=this.getPreset(z);if(!Z)return;return Object.assign(Z,J),this.dirty=!0,Z}deletePreset(z){let J=this.storage.presets.findIndex((Z)=>Z.id===z);if(J===-1)return!1;return this.storage.presets.splice(J,1),this.storage.recentIds=this.storage.recentIds.filter((Z)=>Z!==z),this.dirty=!0,!0}searchPresets(z){let J=z.toLowerCase();return this.storage.presets.filter((Z)=>{if(Z.name.toLowerCase().includes(J))return!0;if(Z.description?.toLowerCase().includes(J))return!0;if(Z.tags?.some((K)=>K.toLowerCase().includes(J)))return!0;return!1})}getMostUsedPresets(z=5){return[...this.storage.presets].sort((J,Z)=>Z.usageCount-J.usageCount).slice(0,z)}exportPresets(z){let J=z?this.storage.presets.filter((Z)=>z.includes(Z.id)):this.storage.presets;return JSON.stringify({presets:J},null,2)}importPresets(z,J=!1){let Z=JSON.parse(z),K=0;for(let $ of Z.presets){let X=this.storage.presets.find((B)=>B.id===$.id);if(X&&!J)continue;if(X)Object.assign(X,$);else this.storage.presets.push($);K++}if(K>0)this.dirty=!0;return K}}function Tz(){return`preset-${Date.now()}-${Math.random().toString(36).slice(2,8)}`}import S from"fs";var Qz=new Map;async function H(z,J,Z={}){let K=performance.now(),$=[];try{let X=await S.promises.stat(J),B=Qz.get(J),Y;if(B&&B.mtime===X.mtimeMs)Y=B.content;else Y=await S.promises.readFile(J,"utf-8"),Qz.set(J,{content:Y,mtime:X.mtimeMs,analyzed:null});let{processDirectives:W}=await import("./chunk-338316c2.js"),j={},U=A(Y);if(U)try{await V(U,j,J)}catch(O){$.push(`Warning: Could not extract script variables: ${O instanceof Error?O.message:String(O)}`)}if(Z.props)Object.assign(j,Z.props);let Q=await W(Y,j,J,{},new Set),D=Wz(Y),L=A(Y);return Q=jz(Q),Q=Dz(Q,j),{html:Q,css:D,js:L,errors:$,duration:performance.now()-K}}catch(X){return $.push(X instanceof Error?X.message:String(X)),{html:`<div class="stx-render-error">${Lz($[0])}</div>`,css:"",js:"",errors:$,duration:performance.now()-K}}}async function qz(z){try{let J=await S.promises.readFile(z,"utf-8"),Z=A(J);if(!Z)return null;let K={};if(await V(Z,K,z),Array.isArray(K.variants))return K.variants;return null}catch{return null}}async function Bz(z,J,Z,K){let $=J.story?.variants;if(!$){let Y=await qz(J.path);if(Y)$=Y}if(!$&&Z==="default")return H(z,J.path,{props:K||{}});if(!$)return H(z,J.path,{props:K||{}});let X=$.find((Y)=>Y.id===Z);if(!X&&Z==="default")return H(z,J.path,{props:K||{}});if(!X)return{html:'<div class="stx-render-error">Variant not found</div>',css:"",js:"",errors:[`Variant '${Z}' not found`],duration:0};let B={...X.state,...K};if(X.source)return wz(z,X.source,B);return H(z,J.path,{props:B})}async function wz(z,J,Z={}){let K=performance.now(),$=[];try{let{processDirectives:X}=await import("./chunk-338316c2.js"),B={...Z},Y=A(J);if(Y)try{await V(Y,B,"inline-template.stx")}catch{}let j=await X(J,B,"inline-template.stx",{},new Set);return j=jz(j),j=Dz(j,B),{html:j,css:Wz(J),js:A(J),errors:$,duration:performance.now()-K}}catch(X){return $.push(X instanceof Error?X.message:String(X)),{html:`<div class="stx-render-error">${Lz($[0])}</div>`,css:"",js:"",errors:$,duration:performance.now()-K}}}function Wz(z){let J=[],Z=/<style\b[^>]*>([\s\S]*?)<\/style>/gi,K=Z.exec(z);while(K!==null)J.push(K[1].trim()),K=Z.exec(z);return J.join(`
|
|
560
|
+
`)}function jz(z){return z.replace(/<!--[\s\S]*?-->/g,"").replace(/<style\b[^>]*>[\s\S]*?<\/style>/gi,"").replace(/<script\b[^>]*>[\s\S]*?<\/script>/gi,"").replace(/^\s*\n/gm,"").trim()}function A(z){let J=[],Z=/<script\b[^>]*>([\s\S]*?)<\/script>/gi,K=Z.exec(z);while(K!==null)J.push(K[1].trim()),K=Z.exec(z);return J.join(`
|
|
561
|
+
`)}function Dz(z,J){let Z=`<script>window.__stxProps = ${JSON.stringify(J)};</script>`;return z+Z}function Lz(z){return z.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""")}async function Oz(z,J){let Z=await I(z);f(z,Z);let K=v(z.config,Z);g(z,K),console.log(` Found ${Z.length} story files`);let $=typeof J.host==="string"?J.host:J.host===!0?"0.0.0.0":"localhost",X=new k(z.root),B=new T(z.root),Y=new M(z.root),W=s(),j=Bun.serve({port:J.port,hostname:$,fetch:async(Q,D)=>{let O=new URL(Q.url).pathname;if(O==="/__hmr"){if(D.upgrade(Q,{data:{isHmr:!0}}))return;return new Response("WebSocket upgrade failed",{status:400})}if(O.startsWith("/api/"))return Cz(z,O,Q,{presetsManager:X,bookmarksManager:B,analyticsTracker:Y});return kz(z,O,J.port)},websocket:{open(Q){W.open(Q)},close(Q){W.close(Q)},message(Q,D){W.message(Q,D)}}}),U=`http://${$}:${J.port}`,_=Uz(z,async(Q,D)=>{console.log(` [${Q}] ${D.relativePath}`);let L=await I(z);f(z,L);let O=v(z.config,L);if(g(z,O),Q==="add")a(D.id,{fileName:D.fileName});else if(Q==="unlink")o(D.id);else n(D.id,{fileName:D.fileName})});if(J.open){let Q=U;try{let{exec:D}=await import("child_process"),L=process.platform;D(`${L==="darwin"?"open":L==="win32"?"start":"xdg-open"} ${Q}`)}catch{}}return{server:j,url:U,close:async()=>{_(),j.stop()}}}async function Cz(z,J,Z,K){let{presetsManager:$,bookmarksManager:X,analyticsTracker:B}=K;if(J==="/api/stories")return Response.json({files:z.storyFiles.map((Y)=>({id:Y.id,fileName:Y.fileName,relativePath:Y.relativePath,treePath:Y.treePath})),tree:z.tree});if(J.startsWith("/api/story/")){let Y=J.slice(11),W=z.storyFiles.find((j)=>j.id===Y);if(!W)return new Response("Story not found",{status:404});try{let j=await Bun.file(W.path).text();return Response.json({id:W.id,fileName:W.fileName,relativePath:W.relativePath,content:j})}catch{return new Response("Error reading story",{status:500})}}if(J==="/api/config")return Response.json({theme:z.config.theme,responsivePresets:z.config.responsivePresets,backgroundPresets:z.config.backgroundPresets});if(J.startsWith("/api/render/")){let Y=new URL(Z.url),W=J.slice(12).split("/"),j=W[0],U=W[1]||"default",_,Q=Y.searchParams.get("props");if(Q)try{_=JSON.parse(Q)}catch{}let D=z.storyFiles.find((L)=>L.id===j);if(!D)return new Response("Story not found",{status:404});try{let L=await Bz(z,D,U,_);return Response.json({html:L.html,css:L.css,js:L.js,errors:L.errors,duration:L.duration})}catch(L){return Response.json({html:"",css:"",js:"",errors:[L instanceof Error?L.message:String(L)],duration:0},{status:500})}}if(J==="/api/presets"&&Z.method==="GET")return Response.json({presets:$.getAllPresets()});if(J==="/api/presets"&&Z.method==="POST")try{let Y=await Z.json(),W=$.createPreset(Y.name,Y.props,{componentId:Y.componentId,description:Y.description});return await $.save(),Response.json(W)}catch(Y){return Response.json({error:String(Y)},{status:400})}if(J.startsWith("/api/presets/")&&!J.includes("/use")&&Z.method==="GET"){let Y=J.slice(13),W=$.getPreset(Y);if(!W)return new Response("Preset not found",{status:404});return Response.json(W)}if(J.endsWith("/use")&&Z.method==="POST"){let Y=J.slice(13,-4),W=$.usePreset(Y);if(!W)return new Response("Preset not found",{status:404});return await $.save(),Response.json(W)}if(J.startsWith("/api/presets/")&&Z.method==="DELETE"){let Y=J.slice(13);if(!$.deletePreset(Y))return new Response("Preset not found",{status:404});return await $.save(),Response.json({success:!0})}if(J==="/api/bookmarks"&&Z.method==="GET")return Response.json({bookmarks:X.getAllBookmarks()});if(J==="/api/bookmarks"&&Z.method==="POST")try{let Y=await Z.json(),W=X.addBookmark(Y.storyId,Y.variantId,Y.props,{name:Y.name,notes:Y.notes,color:Y.color});return await X.save(),Response.json(W)}catch(Y){return Response.json({error:String(Y)},{status:400})}if(J==="/api/bookmarks/toggle"&&Z.method==="POST")try{let Y=await Z.json(),W=X.toggleBookmark(Y.storyId,Y.variantId,Y.props);return await X.save(),Response.json(W)}catch(Y){return Response.json({error:String(Y)},{status:400})}if(J.startsWith("/api/bookmarks/")&&J!=="/api/bookmarks/toggle"&&Z.method==="GET"){let Y=J.slice(15),W=X.getBookmark(Y);if(!W)return new Response("Bookmark not found",{status:404});return Response.json(W)}if(J.startsWith("/api/bookmarks/")&&Z.method==="DELETE"){let Y=J.slice(15);if(!X.deleteBookmark(Y))return new Response("Bookmark not found",{status:404});return await X.save(),Response.json({success:!0})}if(J==="/api/analytics/track"&&Z.method==="POST")try{let Y=await Z.json();if(Y.type==="view"&&Y.componentId)B.trackView(Y.componentId,Y.variantId);else if(Y.type==="prop_change"&&Y.componentId&&Y.propName)B.trackPropChange(Y.componentId,Y.propName,null);else if(Y.type==="variant_switch"&&Y.componentId&&Y.variantId)B.trackVariantSwitch(Y.componentId,Y.variantId);else if(Y.type==="search"&&Y.query)B.trackSearch(Y.query,0);return await B.save(),Response.json({success:!0})}catch(Y){return Response.json({error:String(Y)},{status:400})}if(J==="/api/analytics/summary"&&Z.method==="GET")return Response.json({mostViewed:B.getMostViewedComponents(10),mostChangedProps:B.getMostChangedProps(10),searchAnalytics:B.getSearchAnalytics()});if(J.startsWith("/api/analytics/component/")&&Z.method==="GET"){let Y=J.slice(25),W=z.storyFiles.find((U)=>U.id===Y),j=B.getComponentAnalytics(Y,W?.fileName||Y);return Response.json(j)}return new Response("Not found",{status:404})}function kz(z,J,Z){let K=Sz(z,Z);return new Response(K,{headers:{"Content-Type":"text/html; charset=utf-8"}})}function Sz(z,J){let{theme:Z}=z.config,K=Z.title||"STX Story",$=`ws://localhost:${J}/__hmr`,X=r($);return`<!DOCTYPE html>
|
|
562
|
+
<html lang="en" class="${Z.darkClass||"dark"}">
|
|
563
|
+
<head>
|
|
564
|
+
<meta charset="UTF-8">
|
|
565
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
566
|
+
<title>${K}</title>
|
|
567
|
+
<style>
|
|
568
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
569
|
+
:root {
|
|
570
|
+
--bg: #ffffff;
|
|
571
|
+
--bg-secondary: #f8f9fa;
|
|
572
|
+
--bg-tertiary: #e9ecef;
|
|
573
|
+
--text: #212529;
|
|
574
|
+
--text-secondary: #6c757d;
|
|
575
|
+
--border: #dee2e6;
|
|
576
|
+
--primary: #3b82f6;
|
|
577
|
+
--primary-hover: #2563eb;
|
|
578
|
+
--success: #10b981;
|
|
579
|
+
--warning: #f59e0b;
|
|
580
|
+
--danger: #ef4444;
|
|
581
|
+
}
|
|
582
|
+
.dark {
|
|
583
|
+
--bg: #0d1117;
|
|
584
|
+
--bg-secondary: #161b22;
|
|
585
|
+
--bg-tertiary: #21262d;
|
|
586
|
+
--text: #f0f6fc;
|
|
587
|
+
--text-secondary: #8b949e;
|
|
588
|
+
--border: #30363d;
|
|
589
|
+
}
|
|
590
|
+
body {
|
|
591
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
592
|
+
background: var(--bg);
|
|
593
|
+
color: var(--text);
|
|
594
|
+
display: flex;
|
|
595
|
+
height: 100vh;
|
|
596
|
+
overflow: hidden;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/* Sidebar */
|
|
600
|
+
.sidebar {
|
|
601
|
+
width: 260px;
|
|
602
|
+
min-width: 260px;
|
|
603
|
+
background: var(--bg-secondary);
|
|
604
|
+
border-right: 1px solid var(--border);
|
|
605
|
+
display: flex;
|
|
606
|
+
flex-direction: column;
|
|
607
|
+
overflow: hidden;
|
|
608
|
+
}
|
|
609
|
+
.sidebar-header {
|
|
610
|
+
padding: 16px;
|
|
611
|
+
border-bottom: 1px solid var(--border);
|
|
612
|
+
display: flex;
|
|
613
|
+
align-items: center;
|
|
614
|
+
gap: 8px;
|
|
615
|
+
}
|
|
616
|
+
.sidebar-header h1 {
|
|
617
|
+
font-size: 16px;
|
|
618
|
+
font-weight: 600;
|
|
619
|
+
}
|
|
620
|
+
.sidebar-search {
|
|
621
|
+
padding: 12px 16px;
|
|
622
|
+
border-bottom: 1px solid var(--border);
|
|
623
|
+
}
|
|
624
|
+
.sidebar-search input {
|
|
625
|
+
width: 100%;
|
|
626
|
+
padding: 8px 12px;
|
|
627
|
+
border: 1px solid var(--border);
|
|
628
|
+
border-radius: 6px;
|
|
629
|
+
background: var(--bg);
|
|
630
|
+
color: var(--text);
|
|
631
|
+
font-size: 13px;
|
|
632
|
+
}
|
|
633
|
+
.sidebar-search input:focus {
|
|
634
|
+
outline: none;
|
|
635
|
+
border-color: var(--primary);
|
|
636
|
+
}
|
|
637
|
+
.story-list {
|
|
638
|
+
flex: 1;
|
|
639
|
+
overflow-y: auto;
|
|
640
|
+
padding: 8px;
|
|
641
|
+
list-style: none;
|
|
642
|
+
}
|
|
643
|
+
.story-item {
|
|
644
|
+
padding: 8px 12px;
|
|
645
|
+
border-radius: 6px;
|
|
646
|
+
cursor: pointer;
|
|
647
|
+
margin-bottom: 2px;
|
|
648
|
+
font-size: 13px;
|
|
649
|
+
display: flex;
|
|
650
|
+
align-items: center;
|
|
651
|
+
gap: 8px;
|
|
652
|
+
}
|
|
653
|
+
.story-item:hover { background: var(--bg-tertiary); }
|
|
654
|
+
.story-item.active {
|
|
655
|
+
background: var(--primary);
|
|
656
|
+
color: white;
|
|
657
|
+
}
|
|
658
|
+
.story-icon { font-size: 14px; }
|
|
659
|
+
|
|
660
|
+
/* Main content */
|
|
661
|
+
.main {
|
|
662
|
+
flex: 1;
|
|
663
|
+
display: flex;
|
|
664
|
+
flex-direction: column;
|
|
665
|
+
overflow: hidden;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
/* Toolbar */
|
|
669
|
+
.toolbar {
|
|
670
|
+
padding: 12px 16px;
|
|
671
|
+
border-bottom: 1px solid var(--border);
|
|
672
|
+
display: flex;
|
|
673
|
+
align-items: center;
|
|
674
|
+
gap: 12px;
|
|
675
|
+
background: var(--bg-secondary);
|
|
676
|
+
}
|
|
677
|
+
.toolbar-group {
|
|
678
|
+
display: flex;
|
|
679
|
+
align-items: center;
|
|
680
|
+
gap: 8px;
|
|
681
|
+
}
|
|
682
|
+
.toolbar-divider {
|
|
683
|
+
width: 1px;
|
|
684
|
+
height: 24px;
|
|
685
|
+
background: var(--border);
|
|
686
|
+
}
|
|
687
|
+
.toolbar select, .toolbar button {
|
|
688
|
+
padding: 6px 12px;
|
|
689
|
+
border: 1px solid var(--border);
|
|
690
|
+
border-radius: 6px;
|
|
691
|
+
background: var(--bg);
|
|
692
|
+
color: var(--text);
|
|
693
|
+
font-size: 12px;
|
|
694
|
+
cursor: pointer;
|
|
695
|
+
}
|
|
696
|
+
.toolbar select:hover, .toolbar button:hover {
|
|
697
|
+
background: var(--bg-tertiary);
|
|
698
|
+
}
|
|
699
|
+
.toolbar-spacer { flex: 1; }
|
|
700
|
+
|
|
701
|
+
/* Variant tabs */
|
|
702
|
+
.variant-tabs {
|
|
703
|
+
display: flex;
|
|
704
|
+
gap: 4px;
|
|
705
|
+
padding: 12px 16px;
|
|
706
|
+
border-bottom: 1px solid var(--border);
|
|
707
|
+
background: var(--bg);
|
|
708
|
+
overflow-x: auto;
|
|
709
|
+
}
|
|
710
|
+
.variant-tab {
|
|
711
|
+
padding: 6px 14px;
|
|
712
|
+
border: 1px solid var(--border);
|
|
713
|
+
border-radius: 6px;
|
|
714
|
+
background: var(--bg-secondary);
|
|
715
|
+
color: var(--text);
|
|
716
|
+
font-size: 12px;
|
|
717
|
+
cursor: pointer;
|
|
718
|
+
white-space: nowrap;
|
|
719
|
+
transition: all 0.15s;
|
|
720
|
+
}
|
|
721
|
+
.variant-tab:hover { background: var(--bg-tertiary); }
|
|
722
|
+
.variant-tab.active {
|
|
723
|
+
background: var(--primary);
|
|
724
|
+
border-color: var(--primary);
|
|
725
|
+
color: white;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/* Content area */
|
|
729
|
+
.content-wrapper {
|
|
730
|
+
flex: 1;
|
|
731
|
+
display: flex;
|
|
732
|
+
overflow: hidden;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
/* Canvas */
|
|
736
|
+
.canvas-container {
|
|
737
|
+
flex: 1;
|
|
738
|
+
display: flex;
|
|
739
|
+
flex-direction: column;
|
|
740
|
+
overflow: hidden;
|
|
741
|
+
background: var(--bg-tertiary);
|
|
742
|
+
}
|
|
743
|
+
.canvas {
|
|
744
|
+
flex: 1;
|
|
745
|
+
padding: 24px;
|
|
746
|
+
overflow: auto;
|
|
747
|
+
display: flex;
|
|
748
|
+
justify-content: center;
|
|
749
|
+
align-items: flex-start;
|
|
750
|
+
}
|
|
751
|
+
.canvas-frame {
|
|
752
|
+
background: white;
|
|
753
|
+
border-radius: 8px;
|
|
754
|
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
|
|
755
|
+
overflow: hidden;
|
|
756
|
+
width: 100%;
|
|
757
|
+
max-width: 100%;
|
|
758
|
+
}
|
|
759
|
+
.canvas-frame iframe {
|
|
760
|
+
width: 100%;
|
|
761
|
+
height: 400px;
|
|
762
|
+
border: none;
|
|
763
|
+
display: block;
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
/* Controls Panel */
|
|
767
|
+
.controls-panel {
|
|
768
|
+
width: 300px;
|
|
769
|
+
min-width: 300px;
|
|
770
|
+
background: var(--bg-secondary);
|
|
771
|
+
border-left: 1px solid var(--border);
|
|
772
|
+
display: flex;
|
|
773
|
+
flex-direction: column;
|
|
774
|
+
overflow: hidden;
|
|
775
|
+
}
|
|
776
|
+
.controls-header {
|
|
777
|
+
padding: 12px 16px;
|
|
778
|
+
border-bottom: 1px solid var(--border);
|
|
779
|
+
font-weight: 600;
|
|
780
|
+
font-size: 13px;
|
|
781
|
+
display: flex;
|
|
782
|
+
justify-content: space-between;
|
|
783
|
+
align-items: center;
|
|
784
|
+
}
|
|
785
|
+
.controls-body {
|
|
786
|
+
flex: 1;
|
|
787
|
+
overflow-y: auto;
|
|
788
|
+
padding: 16px;
|
|
789
|
+
}
|
|
790
|
+
.control-group {
|
|
791
|
+
margin-bottom: 16px;
|
|
792
|
+
}
|
|
793
|
+
.control-label {
|
|
794
|
+
display: block;
|
|
795
|
+
font-size: 12px;
|
|
796
|
+
font-weight: 500;
|
|
797
|
+
margin-bottom: 6px;
|
|
798
|
+
color: var(--text-secondary);
|
|
799
|
+
}
|
|
800
|
+
.control-input {
|
|
801
|
+
width: 100%;
|
|
802
|
+
padding: 8px 12px;
|
|
803
|
+
border: 1px solid var(--border);
|
|
804
|
+
border-radius: 6px;
|
|
805
|
+
background: var(--bg);
|
|
806
|
+
color: var(--text);
|
|
807
|
+
font-size: 13px;
|
|
808
|
+
}
|
|
809
|
+
.control-input:focus {
|
|
810
|
+
outline: none;
|
|
811
|
+
border-color: var(--primary);
|
|
812
|
+
}
|
|
813
|
+
.control-checkbox {
|
|
814
|
+
display: flex;
|
|
815
|
+
align-items: center;
|
|
816
|
+
gap: 8px;
|
|
817
|
+
}
|
|
818
|
+
.control-checkbox input {
|
|
819
|
+
width: 16px;
|
|
820
|
+
height: 16px;
|
|
821
|
+
}
|
|
822
|
+
.controls-actions {
|
|
823
|
+
padding: 12px 16px;
|
|
824
|
+
border-top: 1px solid var(--border);
|
|
825
|
+
display: flex;
|
|
826
|
+
gap: 8px;
|
|
827
|
+
}
|
|
828
|
+
.controls-actions button {
|
|
829
|
+
flex: 1;
|
|
830
|
+
padding: 8px;
|
|
831
|
+
border: 1px solid var(--border);
|
|
832
|
+
border-radius: 6px;
|
|
833
|
+
background: var(--bg);
|
|
834
|
+
color: var(--text);
|
|
835
|
+
font-size: 12px;
|
|
836
|
+
cursor: pointer;
|
|
837
|
+
}
|
|
838
|
+
.controls-actions button:hover {
|
|
839
|
+
background: var(--bg-tertiary);
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
/* States */
|
|
843
|
+
.empty-state {
|
|
844
|
+
display: flex;
|
|
845
|
+
flex-direction: column;
|
|
846
|
+
align-items: center;
|
|
847
|
+
justify-content: center;
|
|
848
|
+
height: 100%;
|
|
849
|
+
color: var(--text-secondary);
|
|
850
|
+
font-size: 14px;
|
|
851
|
+
gap: 8px;
|
|
852
|
+
}
|
|
853
|
+
.loading {
|
|
854
|
+
display: flex;
|
|
855
|
+
align-items: center;
|
|
856
|
+
justify-content: center;
|
|
857
|
+
height: 100%;
|
|
858
|
+
color: var(--text-secondary);
|
|
859
|
+
}
|
|
860
|
+
.loading::after {
|
|
861
|
+
content: '';
|
|
862
|
+
width: 20px;
|
|
863
|
+
height: 20px;
|
|
864
|
+
border: 2px solid var(--border);
|
|
865
|
+
border-top-color: var(--primary);
|
|
866
|
+
border-radius: 50%;
|
|
867
|
+
animation: spin 1s linear infinite;
|
|
868
|
+
margin-left: 8px;
|
|
869
|
+
}
|
|
870
|
+
@keyframes spin {
|
|
871
|
+
to { transform: rotate(360deg); }
|
|
872
|
+
}
|
|
873
|
+
.render-error {
|
|
874
|
+
padding: 16px;
|
|
875
|
+
background: #fef2f2;
|
|
876
|
+
border: 1px solid #fecaca;
|
|
877
|
+
border-radius: 8px;
|
|
878
|
+
color: #dc2626;
|
|
879
|
+
}
|
|
880
|
+
.dark .render-error {
|
|
881
|
+
background: #450a0a;
|
|
882
|
+
border-color: #7f1d1d;
|
|
883
|
+
color: #fca5a5;
|
|
884
|
+
}
|
|
885
|
+
.render-error pre {
|
|
886
|
+
margin-top: 8px;
|
|
887
|
+
white-space: pre-wrap;
|
|
888
|
+
font-size: 12px;
|
|
889
|
+
font-family: monospace;
|
|
890
|
+
}
|
|
891
|
+
</style>
|
|
892
|
+
</head>
|
|
893
|
+
<body>
|
|
894
|
+
<aside class="sidebar">
|
|
895
|
+
<div class="sidebar-header">
|
|
896
|
+
<span style="font-size: 20px;">\uD83D\uDCD6</span>
|
|
897
|
+
<h1>${K}</h1>
|
|
898
|
+
</div>
|
|
899
|
+
<div class="sidebar-search">
|
|
900
|
+
<input type="text" placeholder="Search components..." id="search-input" oninput="filterStories(this.value)">
|
|
901
|
+
</div>
|
|
902
|
+
<ul class="story-list" id="story-list">
|
|
903
|
+
<li class="empty-state">Loading stories...</li>
|
|
904
|
+
</ul>
|
|
905
|
+
</aside>
|
|
906
|
+
|
|
907
|
+
<main class="main">
|
|
908
|
+
<div class="toolbar">
|
|
909
|
+
<div class="toolbar-group">
|
|
910
|
+
<button onclick="toggleTheme()" title="Toggle theme">\uD83C\uDF13</button>
|
|
911
|
+
<button onclick="reloadStory()" title="Reload">\u21BB</button>
|
|
912
|
+
</div>
|
|
913
|
+
<div class="toolbar-divider"></div>
|
|
914
|
+
<div class="toolbar-group">
|
|
915
|
+
<select id="viewport" onchange="changeViewport(this.value)">
|
|
916
|
+
<option value="">Responsive</option>
|
|
917
|
+
${z.config.responsivePresets.map((B)=>`<option value="${B.width}">${B.label}</option>`).join("")}
|
|
918
|
+
</select>
|
|
919
|
+
<select id="background" onchange="changeBackground(this.value)">
|
|
920
|
+
${z.config.backgroundPresets.map((B)=>`<option value="${B.color}">${B.label}</option>`).join("")}
|
|
921
|
+
</select>
|
|
922
|
+
</div>
|
|
923
|
+
<div class="toolbar-spacer"></div>
|
|
924
|
+
<div class="toolbar-group">
|
|
925
|
+
<button onclick="toggleControls()" id="controls-toggle">Controls \u25B6</button>
|
|
926
|
+
</div>
|
|
927
|
+
</div>
|
|
928
|
+
|
|
929
|
+
<div class="variant-tabs" id="variant-tabs" style="display: none;">
|
|
930
|
+
<!-- Variant tabs will be populated here -->
|
|
931
|
+
</div>
|
|
932
|
+
|
|
933
|
+
<div class="content-wrapper">
|
|
934
|
+
<div class="canvas-container">
|
|
935
|
+
<div class="canvas" id="canvas">
|
|
936
|
+
<div class="empty-state">
|
|
937
|
+
<span style="font-size: 32px;">\uD83C\uDFA8</span>
|
|
938
|
+
<span>Select a component from the sidebar</span>
|
|
939
|
+
</div>
|
|
940
|
+
</div>
|
|
941
|
+
</div>
|
|
942
|
+
|
|
943
|
+
<div class="controls-panel" id="controls-panel" style="display: none;">
|
|
944
|
+
<div class="controls-header">
|
|
945
|
+
<span>Controls</span>
|
|
946
|
+
<button onclick="resetProps()" style="font-size: 11px; padding: 4px 8px;">Reset</button>
|
|
947
|
+
</div>
|
|
948
|
+
<div class="controls-body" id="controls-body">
|
|
949
|
+
<div class="empty-state">Select a story to edit props</div>
|
|
950
|
+
</div>
|
|
951
|
+
<div class="controls-actions">
|
|
952
|
+
<button onclick="copyProps()">Copy JSON</button>
|
|
953
|
+
<button onclick="applyProps()">Apply</button>
|
|
954
|
+
</div>
|
|
955
|
+
</div>
|
|
956
|
+
</div>
|
|
957
|
+
</main>
|
|
958
|
+
|
|
959
|
+
<script>
|
|
960
|
+
let currentStory = null;
|
|
961
|
+
let currentVariant = 'default';
|
|
962
|
+
let currentStoryData = null;
|
|
963
|
+
let currentProps = {};
|
|
964
|
+
let allStories = [];
|
|
965
|
+
|
|
966
|
+
// Load stories
|
|
967
|
+
fetch('/api/stories')
|
|
968
|
+
.then(r => r.json())
|
|
969
|
+
.then(data => {
|
|
970
|
+
allStories = data.files;
|
|
971
|
+
renderStoryList(allStories);
|
|
972
|
+
});
|
|
973
|
+
|
|
974
|
+
function renderStoryList(stories) {
|
|
975
|
+
const list = document.getElementById('story-list');
|
|
976
|
+
if (stories.length === 0) {
|
|
977
|
+
list.innerHTML = '<li class="empty-state">No stories found</li>';
|
|
978
|
+
return;
|
|
979
|
+
}
|
|
980
|
+
list.innerHTML = stories.map(f =>
|
|
981
|
+
'<li class="story-item" data-id="' + f.id + '" onclick="selectStory(\\'' + f.id + '\\', this)">' +
|
|
982
|
+
'<span class="story-icon">\uD83D\uDCE6</span>' + f.fileName + '</li>'
|
|
983
|
+
).join('');
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
function filterStories(query) {
|
|
987
|
+
const filtered = allStories.filter(f =>
|
|
988
|
+
f.fileName.toLowerCase().includes(query.toLowerCase())
|
|
989
|
+
);
|
|
990
|
+
renderStoryList(filtered);
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
function selectStory(id, element) {
|
|
994
|
+
currentStory = id;
|
|
995
|
+
currentVariant = 'default';
|
|
996
|
+
|
|
997
|
+
document.querySelectorAll('.story-item').forEach(el => el.classList.remove('active'));
|
|
998
|
+
if (element) element.classList.add('active');
|
|
999
|
+
|
|
1000
|
+
// Fetch story details with variants
|
|
1001
|
+
fetch('/api/story/' + id)
|
|
1002
|
+
.then(r => r.json())
|
|
1003
|
+
.then(data => {
|
|
1004
|
+
currentStoryData = data;
|
|
1005
|
+
currentProps = {};
|
|
1006
|
+
|
|
1007
|
+
// Parse variants from content
|
|
1008
|
+
const variants = parseVariants(data.content);
|
|
1009
|
+
renderVariantTabs(variants);
|
|
1010
|
+
|
|
1011
|
+
// Parse props from content
|
|
1012
|
+
const props = parseProps(data.content);
|
|
1013
|
+
renderControls(props);
|
|
1014
|
+
|
|
1015
|
+
// Render the default variant
|
|
1016
|
+
renderVariant('default');
|
|
1017
|
+
})
|
|
1018
|
+
.catch(err => {
|
|
1019
|
+
showError('Failed to load story: ' + err.message);
|
|
1020
|
+
});
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
function parseVariants(content) {
|
|
1024
|
+
const match = content.match(/const\\s+variants\\s*=\\s*\\[([\\s\\S]*?)\\]/);
|
|
1025
|
+
if (!match) return [{ id: 'default', title: 'Default' }];
|
|
1026
|
+
|
|
1027
|
+
try {
|
|
1028
|
+
const variantsStr = '[' + match[1] + ']';
|
|
1029
|
+
const cleaned = variantsStr
|
|
1030
|
+
.replace(/'/g, '"')
|
|
1031
|
+
.replace(/(\\w+):/g, '"$1":')
|
|
1032
|
+
.replace(/,\\s*}/g, '}')
|
|
1033
|
+
.replace(/,\\s*]/g, ']');
|
|
1034
|
+
return JSON.parse(cleaned);
|
|
1035
|
+
}
|
|
1036
|
+
catch {
|
|
1037
|
+
return [{ id: 'default', title: 'Default' }];
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
function parseProps(content) {
|
|
1042
|
+
const props = [];
|
|
1043
|
+
const scriptMatch = content.match(/<script>([\\s\\S]*?)<\\/script>/);
|
|
1044
|
+
if (!scriptMatch) return props;
|
|
1045
|
+
|
|
1046
|
+
const script = scriptMatch[1];
|
|
1047
|
+
const propMatches = script.matchAll(/const\\s+(\\w+)\\s*=\\s*([^\\n]+)/g);
|
|
1048
|
+
|
|
1049
|
+
for (const m of propMatches) {
|
|
1050
|
+
const name = m[1];
|
|
1051
|
+
const valueStr = m[2].trim();
|
|
1052
|
+
|
|
1053
|
+
// Skip variants and title
|
|
1054
|
+
if (name === 'variants' || name === 'title') continue;
|
|
1055
|
+
|
|
1056
|
+
let type = 'text';
|
|
1057
|
+
let value = valueStr;
|
|
1058
|
+
|
|
1059
|
+
if (valueStr === 'true' || valueStr === 'false') {
|
|
1060
|
+
type = 'boolean';
|
|
1061
|
+
value = valueStr === 'true';
|
|
1062
|
+
}
|
|
1063
|
+
else if (valueStr.match(/^\\d+$/)) {
|
|
1064
|
+
type = 'number';
|
|
1065
|
+
value = parseInt(valueStr);
|
|
1066
|
+
}
|
|
1067
|
+
else if (valueStr.startsWith("'") || valueStr.startsWith('"')) {
|
|
1068
|
+
value = valueStr.slice(1, -1);
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
props.push({ name, type, value, default: value });
|
|
1072
|
+
currentProps[name] = value;
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
return props;
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
function renderVariantTabs(variants) {
|
|
1079
|
+
const container = document.getElementById('variant-tabs');
|
|
1080
|
+
if (variants.length <= 1) {
|
|
1081
|
+
container.style.display = 'none';
|
|
1082
|
+
return;
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
container.style.display = 'flex';
|
|
1086
|
+
container.innerHTML = variants.map(v =>
|
|
1087
|
+
'<button class="variant-tab' + (v.id === currentVariant ? ' active' : '') +
|
|
1088
|
+
'" onclick="selectVariant(\\'' + v.id + '\\')">' + v.title + '</button>'
|
|
1089
|
+
).join('');
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
function renderControls(props) {
|
|
1093
|
+
const body = document.getElementById('controls-body');
|
|
1094
|
+
if (props.length === 0) {
|
|
1095
|
+
body.innerHTML = '<div class="empty-state">No props available</div>';
|
|
1096
|
+
return;
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
body.innerHTML = props.map(p => {
|
|
1100
|
+
if (p.type === 'boolean') {
|
|
1101
|
+
return '<div class="control-group">' +
|
|
1102
|
+
'<label class="control-checkbox">' +
|
|
1103
|
+
'<input type="checkbox" ' + (currentProps[p.name] ? 'checked' : '') +
|
|
1104
|
+
' onchange="updateProp(\\'' + p.name + '\\', this.checked)">' +
|
|
1105
|
+
'<span>' + p.name + '</span>' +
|
|
1106
|
+
'</label></div>';
|
|
1107
|
+
}
|
|
1108
|
+
return '<div class="control-group">' +
|
|
1109
|
+
'<label class="control-label">' + p.name + '</label>' +
|
|
1110
|
+
'<input type="' + (p.type === 'number' ? 'number' : 'text') + '" ' +
|
|
1111
|
+
'class="control-input" value="' + escapeHtml(String(currentProps[p.name] || '')) + '" ' +
|
|
1112
|
+
'onchange="updateProp(\\'' + p.name + '\\', this.value)">' +
|
|
1113
|
+
'</div>';
|
|
1114
|
+
}).join('');
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
function selectVariant(variantId) {
|
|
1118
|
+
currentVariant = variantId;
|
|
1119
|
+
|
|
1120
|
+
document.querySelectorAll('.variant-tab').forEach(el => el.classList.remove('active'));
|
|
1121
|
+
event.target.classList.add('active');
|
|
1122
|
+
|
|
1123
|
+
// Get variant state and apply to props
|
|
1124
|
+
const variants = parseVariants(currentStoryData.content);
|
|
1125
|
+
const variant = variants.find(v => v.id === variantId);
|
|
1126
|
+
if (variant && variant.state) {
|
|
1127
|
+
Object.assign(currentProps, variant.state);
|
|
1128
|
+
renderControls(parseProps(currentStoryData.content));
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
renderVariant(variantId);
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
function renderVariant(variantId) {
|
|
1135
|
+
const canvas = document.getElementById('canvas');
|
|
1136
|
+
canvas.innerHTML = '<div class="loading">Rendering</div>';
|
|
1137
|
+
|
|
1138
|
+
// Build URL with props
|
|
1139
|
+
const propsParam = Object.keys(currentProps).length > 0
|
|
1140
|
+
? '?props=' + encodeURIComponent(JSON.stringify(currentProps))
|
|
1141
|
+
: '';
|
|
1142
|
+
|
|
1143
|
+
fetch('/api/render/' + currentStory + '/' + variantId + propsParam)
|
|
1144
|
+
.then(r => r.json())
|
|
1145
|
+
.then(data => {
|
|
1146
|
+
if (data.errors && data.errors.length > 0 && !data.html) {
|
|
1147
|
+
canvas.innerHTML = '<div class="render-error"><strong>Render Error:</strong><pre>' +
|
|
1148
|
+
escapeHtml(data.errors.join('\\n')) + '</pre></div>';
|
|
1149
|
+
return;
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
canvas.innerHTML = '<div class="canvas-frame"><iframe id="preview-iframe"></iframe></div>';
|
|
1153
|
+
const iframe = document.getElementById('preview-iframe');
|
|
1154
|
+
const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
|
|
1155
|
+
|
|
1156
|
+
iframeDoc.open();
|
|
1157
|
+
iframeDoc.write('<!DOCTYPE html>' +
|
|
1158
|
+
'<html>' +
|
|
1159
|
+
'<head>' +
|
|
1160
|
+
'<meta charset="UTF-8">' +
|
|
1161
|
+
'<meta name="viewport" content="width=device-width, initial-scale=1.0">' +
|
|
1162
|
+
'<style>' +
|
|
1163
|
+
'* { box-sizing: border-box; }' +
|
|
1164
|
+
'body { margin: 0; padding: 20px; font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, sans-serif; }' +
|
|
1165
|
+
(data.css || '') +
|
|
1166
|
+
'</style>' +
|
|
1167
|
+
'</head>' +
|
|
1168
|
+
'<body>' +
|
|
1169
|
+
(data.html || '') +
|
|
1170
|
+
'</body>' +
|
|
1171
|
+
'</html>');
|
|
1172
|
+
iframeDoc.close();
|
|
1173
|
+
|
|
1174
|
+
// Auto-resize iframe
|
|
1175
|
+
setTimeout(() => {
|
|
1176
|
+
const height = iframeDoc.body.scrollHeight + 40;
|
|
1177
|
+
iframe.style.height = Math.max(200, height) + 'px';
|
|
1178
|
+
}, 100);
|
|
1179
|
+
})
|
|
1180
|
+
.catch(err => {
|
|
1181
|
+
canvas.innerHTML = '<div class="render-error"><strong>Error:</strong> ' + escapeHtml(err.message) + '</div>';
|
|
1182
|
+
});
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
function updateProp(name, value) {
|
|
1186
|
+
currentProps[name] = value;
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
function applyProps() {
|
|
1190
|
+
if (currentStory) {
|
|
1191
|
+
renderVariant(currentVariant);
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
function resetProps() {
|
|
1196
|
+
if (currentStoryData) {
|
|
1197
|
+
currentProps = {};
|
|
1198
|
+
const props = parseProps(currentStoryData.content);
|
|
1199
|
+
props.forEach(p => { currentProps[p.name] = p.default; });
|
|
1200
|
+
renderControls(props);
|
|
1201
|
+
renderVariant(currentVariant);
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
function copyProps() {
|
|
1206
|
+
navigator.clipboard.writeText(JSON.stringify(currentProps, null, 2))
|
|
1207
|
+
.then(() => alert('Props copied to clipboard!'));
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
function reloadStory() {
|
|
1211
|
+
if (currentStory) {
|
|
1212
|
+
renderVariant(currentVariant);
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
|
|
1216
|
+
function showError(message) {
|
|
1217
|
+
document.getElementById('canvas').innerHTML =
|
|
1218
|
+
'<div class="render-error">' + escapeHtml(message) + '</div>';
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
function escapeHtml(text) {
|
|
1222
|
+
const div = document.createElement('div');
|
|
1223
|
+
div.textContent = text;
|
|
1224
|
+
return div.innerHTML;
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
function toggleTheme() {
|
|
1228
|
+
document.documentElement.classList.toggle('dark');
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
function toggleControls() {
|
|
1232
|
+
const panel = document.getElementById('controls-panel');
|
|
1233
|
+
const btn = document.getElementById('controls-toggle');
|
|
1234
|
+
if (panel.style.display === 'none') {
|
|
1235
|
+
panel.style.display = 'flex';
|
|
1236
|
+
btn.textContent = 'Controls \u25C0';
|
|
1237
|
+
}
|
|
1238
|
+
else {
|
|
1239
|
+
panel.style.display = 'none';
|
|
1240
|
+
btn.textContent = 'Controls \u25B6';
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
function changeViewport(width) {
|
|
1245
|
+
const frame = document.querySelector('.canvas-frame');
|
|
1246
|
+
if (frame) {
|
|
1247
|
+
frame.style.maxWidth = width ? width + 'px' : '';
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
function changeBackground(color) {
|
|
1252
|
+
const canvas = document.getElementById('canvas');
|
|
1253
|
+
canvas.style.background = color || '';
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
// STX Story global object for HMR
|
|
1257
|
+
window.__stxStory = {
|
|
1258
|
+
currentStoryId: null,
|
|
1259
|
+
reloadCurrentStory: function() {
|
|
1260
|
+
if (currentStory) renderVariant(currentVariant);
|
|
1261
|
+
},
|
|
1262
|
+
refreshStoryList: function() {
|
|
1263
|
+
fetch('/api/stories')
|
|
1264
|
+
.then(r => r.json())
|
|
1265
|
+
.then(data => {
|
|
1266
|
+
allStories = data.files;
|
|
1267
|
+
renderStoryList(allStories);
|
|
1268
|
+
});
|
|
1269
|
+
}
|
|
1270
|
+
};
|
|
1271
|
+
</script>
|
|
1272
|
+
${X}
|
|
1273
|
+
${e()}
|
|
1274
|
+
${Zz()}
|
|
1275
|
+
${Kz()}
|
|
1276
|
+
<style>${Jz()}</style>
|
|
1277
|
+
${zz()}
|
|
1278
|
+
</body>
|
|
1279
|
+
</html>`}import fz from"fs";import gz from"path";var Iz=new Map,vz=[],xz=[],uz=new Map;async function _z(z){let J=z.config.setupFile;if(!J)return;let Z=gz.resolve(z.root,J);try{await fz.promises.access(Z)}catch{return}let K={storyContext:z,registerComponent:($,X)=>{Iz.set($,X)},addCSS:($)=>{vz.push($)},addScript:($)=>{xz.push($)},setState:($,X)=>{uz.set($,X)}};try{let $=await import(Z);if(typeof $.default==="function")await $.default(K);else if(typeof $.setupStory==="function")await $.setupStory(K)}catch($){console.warn(`Warning: Failed to load setup file: ${$}`)}}async function bz(z={}){x();let J=await h({mode:"dev"});if(!J.config.enabled){G("Story feature is disabled in config.");return}await _z(J);let Z=z.port??J.config.port,K,$=async()=>{K=await Oz(J,{port:Z,open:z.open,host:z.host}),b(J.storyFiles.length),u(K.url),y()};await $();let X=m(J,{onChange:async()=>{await K.close();let Y=await h({mode:"dev"});Object.assign(J,Y),await $()}}),B=async()=>{console.log(`
|
|
1280
|
+
`),X.stop(),await K.close(),process.exit(0)};if(process.on("SIGINT",B),process.on("SIGTERM",B),process.stdin.isTTY)process.stdin.setRawMode(!0),process.stdin.resume(),process.stdin.on("data",async(Y)=>{switch(Y.toString()){case"h":y();break;case"o":try{let{exec:j}=await import("child_process"),U=process.platform==="darwin"?"open":process.platform==="win32"?"start":"xdg-open";j(`${U} ${K.url}`)}catch{Az("Failed to open browser")}break;case"r":await K.close(),await $();break;case"c":console.clear(),x(),b(J.storyFiles.length),u(K.url);break;case"q":case"\x03":await B();break}});await new Promise(()=>{})}var qJ=bz;export{bz as devCommand,qJ as dev};
|