@aprovan/patchwork-image-shadcn 0.1.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/LICENSE ADDED
@@ -0,0 +1,359 @@
1
+ Copyright (c) 2026 Aprovan
2
+
3
+ This Source Code Form is subject to the terms of the Mozilla Public License, v.
4
+ 2.0. If a copy of the MPL was not distributed with this file, You can obtain one
5
+ at https://mozilla.org/MPL/2.0/.
6
+
7
+ ---
8
+
9
+ # Mozilla Public License Version 2.0
10
+
11
+ 1. Definitions
12
+
13
+ ---
14
+
15
+ 1.1. "Contributor" means each individual or legal entity that creates,
16
+ contributes to the creation of, or owns Covered Software.
17
+
18
+ 1.2. "Contributor Version" means the combination of the Contributions of others
19
+ (if any) used by a Contributor and that particular Contributor's Contribution.
20
+
21
+ 1.3. "Contribution" means Covered Software of a particular Contributor.
22
+
23
+ 1.4. "Covered Software" means Source Code Form to which the initial Contributor
24
+ has attached the notice in Exhibit A, the Executable Form of such Source Code
25
+ Form, and Modifications of such Source Code Form, in each case including
26
+ portions thereof.
27
+
28
+ 1.5. "Incompatible With Secondary Licenses" means
29
+
30
+ (a) that the initial Contributor has attached the notice described
31
+ in Exhibit B to the Covered Software; or
32
+
33
+ (b) that the Covered Software was made available under the terms of
34
+ version 1.1 or earlier of the License, but not also under the
35
+ terms of a Secondary License.
36
+
37
+ 1.6. "Executable Form" means any form of the work other than Source Code Form.
38
+
39
+ 1.7. "Larger Work" means a work that combines Covered Software with other
40
+ material, in a separate file or files, that is not Covered Software.
41
+
42
+ 1.8. "License" means this document.
43
+
44
+ 1.9. "Licensable" means having the right to grant, to the maximum extent
45
+ possible, whether at the time of the initial grant or subsequently, any and all
46
+ of the rights conveyed by this License.
47
+
48
+ 1.10. "Modifications" means any of the following:
49
+
50
+ (a) any file in Source Code Form that results from an addition to,
51
+ deletion from, or modification of the contents of Covered
52
+ Software; or
53
+
54
+ (b) any new file in Source Code Form that contains any Covered
55
+ Software.
56
+
57
+ 1.11. "Patent Claims" of a Contributor means any patent claim(s), including
58
+ without limitation, method, process, and apparatus claims, in any patent
59
+ Licensable by such Contributor that would be infringed, but for the grant of the
60
+ License, by the making, using, selling, offering for sale, having made, import,
61
+ or transfer of either its Contributions or its Contributor Version.
62
+
63
+ 1.12. "Secondary License" means either the GNU General Public License, Version
64
+ 2.0, the GNU Lesser General Public License, Version 2.1, the GNU Affero General
65
+ Public License, Version 3.0, or any later versions of those licenses.
66
+
67
+ 1.13. "Source Code Form" means the form of the work preferred for making
68
+ modifications.
69
+
70
+ 1.14. "You" (or "Your") means an individual or a legal entity exercising rights
71
+ under this License. For legal entities, "You" includes any entity that controls,
72
+ is controlled by, or is under common control with You. For purposes of this
73
+ definition, "control" means (a) the power, direct or indirect, to cause the
74
+ direction or management of such entity, whether by contract or otherwise, or (b)
75
+ ownership of more than fifty percent (50%) of the outstanding shares or
76
+ beneficial ownership of such entity.
77
+
78
+ 2. License Grants and Conditions
79
+
80
+ ---
81
+
82
+ 2.1. Grants
83
+
84
+ Each Contributor hereby grants You a world-wide, royalty-free, non-exclusive
85
+ license:
86
+
87
+ (a) under intellectual property rights (other than patent or trademark)
88
+ Licensable by such Contributor to use, reproduce, make available, modify,
89
+ display, perform, distribute, and otherwise exploit its Contributions, either on
90
+ an unmodified basis, with Modifications, or as part of a Larger Work; and
91
+
92
+ (b) under Patent Claims of such Contributor to make, use, sell, offer for sale,
93
+ have made, import, and otherwise transfer either its Contributions or its
94
+ Contributor Version.
95
+
96
+ 2.2. Effective Date
97
+
98
+ The licenses granted in Section 2.1 with respect to any Contribution become
99
+ effective for each Contribution on the date the Contributor first distributes
100
+ such Contribution.
101
+
102
+ 2.3. Limitations on Grant Scope
103
+
104
+ The licenses granted in this Section 2 are the only rights granted under this
105
+ License. No additional rights or licenses will be implied from the distribution
106
+ or licensing of Covered Software under this License. Notwithstanding Section
107
+ 2.1(b) above, no patent license is granted by a Contributor:
108
+
109
+ (a) for any code that a Contributor has removed from Covered Software; or
110
+
111
+ (b) for infringements caused by: (i) Your and any other third party's
112
+ modifications of Covered Software, or (ii) the combination of its Contributions
113
+ with other software (except as part of its Contributor Version); or
114
+
115
+ (c) under Patent Claims infringed by Covered Software in the absence of its
116
+ Contributions.
117
+
118
+ This License does not grant any rights in the trademarks, service marks, or
119
+ logos of any Contributor (except as may be necessary to comply with the notice
120
+ requirements in Section 3.4).
121
+
122
+ 2.4. Subsequent Licenses
123
+
124
+ No Contributor makes additional grants as a result of Your choice to distribute
125
+ the Covered Software under a subsequent version of this License (see Section
126
+ 10.2) or under the terms of a Secondary License (if permitted under the terms of
127
+ Section 3.3).
128
+
129
+ 2.5. Representation
130
+
131
+ Each Contributor represents that the Contributor believes its Contributions are
132
+ its original creation(s) or it has sufficient rights to grant the rights to its
133
+ Contributions conveyed by this License.
134
+
135
+ 2.6. Fair Use
136
+
137
+ This License is not intended to limit any rights You have under applicable
138
+ copyright doctrines of fair use, fair dealing, or other equivalents.
139
+
140
+ 2.7. Conditions
141
+
142
+ Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in
143
+ Section 2.1.
144
+
145
+ 3. Responsibilities
146
+
147
+ ---
148
+
149
+ 3.1. Distribution of Source Form
150
+
151
+ All distribution of Covered Software in Source Code Form, including any
152
+ Modifications that You create or to which You contribute, must be under the
153
+ terms of this License. You must inform recipients that the Source Code Form of
154
+ the Covered Software is governed by the terms of this License, and how they can
155
+ obtain a copy of this License. You may not attempt to alter or restrict the
156
+ recipients' rights in the Source Code Form.
157
+
158
+ 3.2. Distribution of Executable Form
159
+
160
+ If You distribute Covered Software in Executable Form then:
161
+
162
+ (a) such Covered Software must also be made available in Source Code Form, as
163
+ described in Section 3.1, and You must inform recipients of the Executable Form
164
+ how they can obtain a copy of such Source Code Form by reasonable means in a
165
+ timely manner, at a charge no more than the cost of distribution to the
166
+ recipient; and
167
+
168
+ (b) You may distribute such Executable Form under the terms of this License, or
169
+ sublicense it under different terms, provided that the license for the
170
+ Executable Form does not attempt to limit or alter the recipients' rights in the
171
+ Source Code Form under this License.
172
+
173
+ 3.3. Distribution of a Larger Work
174
+
175
+ You may create and distribute a Larger Work under terms of Your choice, provided
176
+ that You also comply with the requirements of this License for the Covered
177
+ Software. If the Larger Work is a combination of Covered Software with a work
178
+ governed by one or more Secondary Licenses, and the Covered Software is not
179
+ Incompatible With Secondary Licenses, this License permits You to additionally
180
+ distribute such Covered Software under the terms of such Secondary License(s),
181
+ so that the recipient of the Larger Work may, at their option, further
182
+ distribute the Covered Software under the terms of either this License or such
183
+ Secondary License(s).
184
+
185
+ 3.4. Notices
186
+
187
+ You may not remove or alter the substance of any license notices (including
188
+ copyright notices, patent notices, disclaimers of warranty, or limitations of
189
+ liability) contained within the Source Code Form of the Covered Software, except
190
+ that You may alter any license notices to the extent required to remedy known
191
+ factual inaccuracies.
192
+
193
+ 3.5. Application of Additional Terms
194
+
195
+ You may choose to offer, and to charge a fee for, warranty, support, indemnity
196
+ or liability obligations to one or more recipients of Covered Software. However,
197
+ You may do so only on Your own behalf, and not on behalf of any Contributor. You
198
+ must make it absolutely clear that any such warranty, support, indemnity, or
199
+ liability obligation is offered by You alone, and You hereby agree to indemnify
200
+ every Contributor for any liability incurred by such Contributor as a result of
201
+ warranty, support, indemnity or liability terms You offer. You may include
202
+ additional disclaimers of warranty and limitations of liability specific to any
203
+ jurisdiction.
204
+
205
+ 4. Inability to Comply Due to Statute or Regulation
206
+
207
+ ---
208
+
209
+ If it is impossible for You to comply with any of the terms of this License with
210
+ respect to some or all of the Covered Software due to statute, judicial order,
211
+ or regulation then You must: (a) comply with the terms of this License to the
212
+ maximum extent possible; and (b) describe the limitations and the code they
213
+ affect. Such description must be placed in a text file included with all
214
+ distributions of the Covered Software under this License. Except to the extent
215
+ prohibited by statute or regulation, such description must be sufficiently
216
+ detailed for a recipient of ordinary skill to be able to understand it.
217
+
218
+ 5. Termination
219
+
220
+ ---
221
+
222
+ 5.1. The rights granted under this License will terminate automatically if You
223
+ fail to comply with any of its terms. However, if You become compliant, then the
224
+ rights granted under this License from a particular Contributor are reinstated
225
+ (a) provisionally, unless and until such Contributor explicitly and finally
226
+ terminates Your grants, and (b) on an ongoing basis, if such Contributor fails
227
+ to notify You of the non-compliance by some reasonable means prior to 60 days
228
+ after You have come back into compliance. Moreover, Your grants from a
229
+ particular Contributor are reinstated on an ongoing basis if such Contributor
230
+ notifies You of the non-compliance by some reasonable means, this is the first
231
+ time You have received notice of non-compliance with this License from such
232
+ Contributor, and You become compliant prior to 30 days after Your receipt of the
233
+ notice.
234
+
235
+ 5.2. If You initiate litigation against any entity by asserting a patent
236
+ infringement claim (excluding declaratory judgment actions, counter-claims, and
237
+ cross-claims) alleging that a Contributor Version directly or indirectly
238
+ infringes any patent, then the rights granted to You by any and all Contributors
239
+ for the Covered Software under Section 2.1 of this License shall terminate.
240
+
241
+ 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user
242
+ license agreements (excluding distributors and resellers) which have been
243
+ validly granted by You or Your distributors under this License prior to
244
+ termination shall survive termination.
245
+
246
+ ---
247
+
248
+ - *
249
+ - 6. Disclaimer of Warranty \*
250
+ - ------------------------- \*
251
+ - *
252
+ - Covered Software is provided under this License on an "as is" \*
253
+ - basis, without warranty of any kind, either expressed, implied, or \*
254
+ - statutory, including, without limitation, warranties that the \*
255
+ - Covered Software is free of defects, merchantable, fit for a \*
256
+ - particular purpose or non-infringing. The entire risk as to the \*
257
+ - quality and performance of the Covered Software is with You. \*
258
+ - Should any Covered Software prove defective in any respect, You \*
259
+ - (not any Contributor) assume the cost of any necessary servicing, \*
260
+ - repair, or correction. This disclaimer of warranty constitutes an \*
261
+ - essential part of this License. No use of any Covered Software is \*
262
+ - authorized under this License except under this disclaimer. \*
263
+ - *
264
+
265
+ ---
266
+
267
+ ---
268
+
269
+ - *
270
+ - 7. Limitation of Liability \*
271
+ - -------------------------- \*
272
+ - *
273
+ - Under no circumstances and under no legal theory, whether tort \*
274
+ - (including negligence), contract, or otherwise, shall any \*
275
+ - Contributor, or anyone who distributes Covered Software as \*
276
+ - permitted above, be liable to You for any direct, indirect, \*
277
+ - special, incidental, or consequential damages of any character \*
278
+ - including, without limitation, damages for lost profits, loss of \*
279
+ - goodwill, work stoppage, computer failure or malfunction, or any \*
280
+ - and all other commercial damages or losses, even if such party \*
281
+ - shall have been informed of the possibility of such damages. This \*
282
+ - limitation of liability shall not apply to liability for death or \*
283
+ - personal injury resulting from such party's negligence to the \*
284
+ - extent applicable law prohibits such limitation. Some \*
285
+ - jurisdictions do not allow the exclusion or limitation of \*
286
+ - incidental or consequential damages, so this exclusion and \*
287
+ - limitation may not apply to You. \*
288
+ - *
289
+
290
+ ---
291
+
292
+ 8. Litigation
293
+
294
+ ---
295
+
296
+ Any litigation relating to this License may be brought only in the courts of a
297
+ jurisdiction where the defendant maintains its principal place of business and
298
+ such litigation shall be governed by laws of that jurisdiction, without
299
+ reference to its conflict-of-law provisions. Nothing in this Section shall
300
+ prevent a party's ability to bring cross-claims or counter-claims.
301
+
302
+ 9. Miscellaneous
303
+
304
+ ---
305
+
306
+ This License represents the complete agreement concerning the subject matter
307
+ hereof. If any provision of this License is held to be unenforceable, such
308
+ provision shall be reformed only to the extent necessary to make it enforceable.
309
+ Any law or regulation which provides that the language of a contract shall be
310
+ construed against the drafter shall not be used to construe this License against
311
+ a Contributor.
312
+
313
+ 10. Versions of the License
314
+
315
+ ---
316
+
317
+ 10.1. New Versions
318
+
319
+ Mozilla Foundation is the license steward. Except as provided in Section 10.3,
320
+ no one other than the license steward has the right to modify or publish new
321
+ versions of this License. Each version will be given a distinguishing version
322
+ number.
323
+
324
+ 10.2. Effect of New Versions
325
+
326
+ You may distribute the Covered Software under the terms of the version of the
327
+ License under which You originally received the Covered Software, or under the
328
+ terms of any subsequent version published by the license steward.
329
+
330
+ 10.3. Modified Versions
331
+
332
+ If you create software not governed by this License, and you want to create a
333
+ new license for such software, you may create and use a modified version of this
334
+ License if you rename the license and remove any references to the name of the
335
+ license steward (except to note that such modified license differs from this
336
+ License).
337
+
338
+ 10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses
339
+
340
+ If You choose to distribute Source Code Form that is Incompatible With Secondary
341
+ Licenses under the terms of this version of the License, the notice described in
342
+ Exhibit B of this License must be attached.
343
+
344
+ ## Exhibit A - Source Code Form License Notice
345
+
346
+ This Source Code Form is subject to the terms of the Mozilla Public License, v.
347
+ 2.0. If a copy of the MPL was not distributed with this file, You can obtain one
348
+ at https://mozilla.org/MPL/2.0/.
349
+
350
+ If it is not possible or desirable to put the notice in a particular file, then
351
+ You may include the notice in a location (such as a LICENSE file in a relevant
352
+ directory) where a recipient would be likely to look for such a notice.
353
+
354
+ You may add additional accurate notices of copyright ownership.
355
+
356
+ ## Exhibit B - "Incompatible With Secondary Licenses" Notice
357
+
358
+ This Source Code Form is "Incompatible With Secondary Licenses", as defined by
359
+ the Mozilla Public License, v. 2.0.
@@ -0,0 +1,265 @@
1
+ import {
2
+ DARK_CSS_VARIABLES,
3
+ DEFAULT_CSS_VARIABLES
4
+ } from "./chunk-7LFKP2EW.js";
5
+
6
+ // src/html.ts
7
+ function generateThemeCss(theme) {
8
+ const vars = theme === "dark" ? DARK_CSS_VARIABLES : DEFAULT_CSS_VARIABLES;
9
+ const cssVarsString = Object.entries(vars).map(([key, value]) => ` ${key}: ${value};`).join("\n");
10
+ return `
11
+ :root {
12
+ ${cssVarsString}
13
+ }
14
+
15
+ * {
16
+ margin: 0;
17
+ padding: 0;
18
+ box-sizing: border-box;
19
+ }
20
+
21
+ body {
22
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
23
+ line-height: 1.5;
24
+ color: hsl(var(--foreground));
25
+ background-color: hsl(var(--background));
26
+ }
27
+
28
+ *, *::before, *::after {
29
+ border-color: hsl(var(--border));
30
+ }
31
+ `;
32
+ }
33
+ function getTailwindCdnScript() {
34
+ return "https://cdn.tailwindcss.com";
35
+ }
36
+ function generateMountCode(propsJson) {
37
+ return `
38
+ import { createRoot } from "react-dom/client";
39
+ import React from "react";
40
+
41
+ // Widget code will set window.__PATCHWORK_WIDGET__
42
+ const Component = window.__PATCHWORK_WIDGET__;
43
+ const props = ${propsJson};
44
+
45
+ if (Component) {
46
+ const root = createRoot(document.getElementById('root'));
47
+ root.render(React.createElement(Component, props));
48
+ window.parent.postMessage({ type: 'patchwork:ready' }, '*');
49
+ } else {
50
+ const root = createRoot(document.getElementById('root'));
51
+ root.render(
52
+ React.createElement('div', { style: { color: 'red', padding: '20px' } },
53
+ React.createElement('h2', null, 'Error: No component found'),
54
+ React.createElement('p', null, 'Widget must export a default component.')
55
+ )
56
+ );
57
+ window.parent.postMessage({ type: 'patchwork:error', message: 'No component found' }, '*');
58
+ }
59
+
60
+ window.addEventListener('error', (e) => {
61
+ window.parent.postMessage({ type: 'patchwork:error', message: e.message }, '*');
62
+ });
63
+ `;
64
+ }
65
+ function transformWidgetCode(code) {
66
+ let result = code;
67
+ const namedMatch = result.match(/export\s*{\s*(\w+)\s+as\s+default\s*}/);
68
+ if (namedMatch) {
69
+ result = result.replace(
70
+ /export\s*{\s*\w+\s+as\s+default\s*};?/,
71
+ `window.__PATCHWORK_WIDGET__ = ${namedMatch[1]};`
72
+ );
73
+ }
74
+ const directMatch = result.match(/export\s+default\s+(?:function\s+)?(\w+)/);
75
+ if (directMatch && !namedMatch) {
76
+ result = result.replace(
77
+ /export\s+default\s+(?:function\s+)?(\w+)/,
78
+ `window.__PATCHWORK_WIDGET__ = $1`
79
+ );
80
+ }
81
+ return result.replace(/export\s*{[^}]*};?/g, "");
82
+ }
83
+ function generateServiceBridge(services) {
84
+ if (services.length === 0) return "";
85
+ const namespaceAssignments = services.map((ns) => `window.${ns} = createNamespaceProxy('${ns}');`).join("\n ");
86
+ return `
87
+ (function() {
88
+ var pendingCalls = new Map();
89
+
90
+ window.__patchwork_call__ = function(namespace, method, args) {
91
+ return new Promise(function(resolve, reject) {
92
+ var id = Math.random().toString(36).slice(2);
93
+ pendingCalls.set(id, { resolve: resolve, reject: reject });
94
+ window.parent.postMessage({
95
+ type: 'patchwork:call',
96
+ id: id,
97
+ service: namespace,
98
+ method: method,
99
+ args: args
100
+ }, '*');
101
+ setTimeout(function() {
102
+ if (pendingCalls.has(id)) {
103
+ pendingCalls.delete(id);
104
+ reject(new Error('Service call timeout: ' + namespace + '.' + method));
105
+ }
106
+ }, 30000);
107
+ });
108
+ };
109
+
110
+ window.addEventListener('message', function(e) {
111
+ if (e.data && e.data.type === 'patchwork:response') {
112
+ var pending = pendingCalls.get(e.data.id);
113
+ if (pending) {
114
+ pendingCalls.delete(e.data.id);
115
+ if (e.data.result.success) {
116
+ pending.resolve(e.data.result.data);
117
+ } else {
118
+ pending.reject(new Error(e.data.result.error || 'Service call failed'));
119
+ }
120
+ }
121
+ }
122
+ });
123
+
124
+ function createNamespaceProxy(namespace) {
125
+ function createNestedProxy(path) {
126
+ var fn = function() {
127
+ return window.__patchwork_call__(namespace, path, Array.prototype.slice.call(arguments));
128
+ };
129
+ return new Proxy(fn, {
130
+ get: function(_, nestedName) {
131
+ if (typeof nestedName === 'symbol') return undefined;
132
+ var newPath = path ? path + '.' + nestedName : nestedName;
133
+ return createNestedProxy(newPath);
134
+ }
135
+ });
136
+ }
137
+ return new Proxy({}, {
138
+ get: function(_, fieldName) {
139
+ if (typeof fieldName === 'symbol') return undefined;
140
+ return createNestedProxy(fieldName);
141
+ }
142
+ });
143
+ }
144
+
145
+ ${namespaceAssignments}
146
+ })();
147
+ `;
148
+ }
149
+ function generateHtml(compiledJs, importMap, options = {}) {
150
+ const {
151
+ title = "Patchwork Widget",
152
+ theme = "dark",
153
+ customCss = "",
154
+ props = {},
155
+ services = []
156
+ } = options;
157
+ const themeCss = generateThemeCss(theme);
158
+ const tailwindCdn = getTailwindCdnScript();
159
+ const widgetCode = transformWidgetCode(compiledJs);
160
+ const importMapJson = JSON.stringify({ imports: importMap }, null, 2);
161
+ const propsJson = JSON.stringify(props);
162
+ const mountCode = generateMountCode(propsJson);
163
+ const serviceBridge = generateServiceBridge(services);
164
+ return `<!DOCTYPE html>
165
+ <html lang="en" class="${theme}">
166
+ <head>
167
+ <meta charset="UTF-8">
168
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
169
+ <title>${title}</title>
170
+ <!-- Tailwind Play CDN - config must be set BEFORE script loads -->
171
+ <script>
172
+ window.tailwind = {
173
+ config: ${JSON.stringify({
174
+ darkMode: "class",
175
+ theme: {
176
+ extend: {
177
+ colors: {
178
+ border: "hsl(var(--border))",
179
+ input: "hsl(var(--input))",
180
+ ring: "hsl(var(--ring))",
181
+ background: "hsl(var(--background))",
182
+ foreground: "hsl(var(--foreground))",
183
+ primary: {
184
+ DEFAULT: "hsl(var(--primary))",
185
+ foreground: "hsl(var(--primary-foreground))"
186
+ },
187
+ secondary: {
188
+ DEFAULT: "hsl(var(--secondary))",
189
+ foreground: "hsl(var(--secondary-foreground))"
190
+ },
191
+ destructive: {
192
+ DEFAULT: "hsl(var(--destructive))",
193
+ foreground: "hsl(var(--destructive-foreground))"
194
+ },
195
+ muted: {
196
+ DEFAULT: "hsl(var(--muted))",
197
+ foreground: "hsl(var(--muted-foreground))"
198
+ },
199
+ accent: {
200
+ DEFAULT: "hsl(var(--accent))",
201
+ foreground: "hsl(var(--accent-foreground))"
202
+ },
203
+ popover: {
204
+ DEFAULT: "hsl(var(--popover))",
205
+ foreground: "hsl(var(--popover-foreground))"
206
+ },
207
+ card: {
208
+ DEFAULT: "hsl(var(--card))",
209
+ foreground: "hsl(var(--card-foreground))"
210
+ }
211
+ },
212
+ borderRadius: {
213
+ lg: "var(--radius)",
214
+ md: "calc(var(--radius) - 2px)",
215
+ sm: "calc(var(--radius) - 4px)"
216
+ }
217
+ }
218
+ }
219
+ })}
220
+ };
221
+ </script>
222
+ <script src="${tailwindCdn}" crossorigin></script>
223
+ <script type="importmap">${importMapJson}</script>
224
+ <style>${themeCss}${customCss}</style>
225
+ </head>
226
+ <body>
227
+ <div id="root"></div>
228
+ <script>
229
+ // Service bridge
230
+ ${serviceBridge}
231
+ </script>
232
+ <script type="module">
233
+ // Widget code
234
+ ${widgetCode}
235
+ </script>
236
+ <script type="module">
237
+ // Mount the widget
238
+ ${mountCode}
239
+ </script>
240
+ </body>
241
+ </html>`;
242
+ }
243
+ function getDefaultImportMap(cdn = "https://esm.sh") {
244
+ return {
245
+ react: `${cdn}/react@18`,
246
+ "react/": `${cdn}/react@18/`,
247
+ "react-dom": `${cdn}/react-dom@18`,
248
+ "react-dom/": `${cdn}/react-dom@18/`,
249
+ "react-dom/client": `${cdn}/react-dom@18/client`
250
+ };
251
+ }
252
+ function getFrameworkDependencies() {
253
+ return {
254
+ react: "^18.0.0",
255
+ "react-dom": "^18.0.0"
256
+ };
257
+ }
258
+
259
+ export {
260
+ transformWidgetCode,
261
+ generateHtml,
262
+ getDefaultImportMap,
263
+ getFrameworkDependencies
264
+ };
265
+ //# sourceMappingURL=chunk-4QNUSFQA.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/html.ts"],"sourcesContent":["/**\n * @aprovan/patchwork-image-shadcnshadcn - HTML Generation\n *\n * Generates complete HTML documents for browser widget rendering.\n * The image owns the full HTML template including CSS, import maps, and mounting code.\n *\n * Uses Tailwind Play CDN for runtime CSS generation.\n */\n\nimport { DEFAULT_CSS_VARIABLES, DARK_CSS_VARIABLES } from './setup.js';\n\nexport interface HtmlOptions {\n /** Document title */\n title?: string;\n /** Theme: 'light' | 'dark' (default: 'dark') */\n theme?: 'light' | 'dark';\n /** Custom CSS to inject */\n customCss?: string;\n /** Widget props to pass */\n props?: Record<string, unknown>;\n /** Service namespaces to expose (generates bridge code) */\n services?: string[];\n}\n\nexport interface ImportMapEntry {\n [pkg: string]: string;\n}\n\n/**\n * Generate CSS for the selected theme\n */\nfunction generateThemeCss(theme: 'light' | 'dark'): string {\n const vars = theme === 'dark' ? DARK_CSS_VARIABLES : DEFAULT_CSS_VARIABLES;\n const cssVarsString = Object.entries(vars)\n .map(([key, value]) => ` ${key}: ${value};`)\n .join('\\n');\n\n return `\n:root {\n${cssVarsString}\n}\n\n* {\n margin: 0;\n padding: 0;\n box-sizing: border-box;\n}\n\nbody {\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;\n line-height: 1.5;\n color: hsl(var(--foreground));\n background-color: hsl(var(--background));\n}\n\n*, *::before, *::after {\n border-color: hsl(var(--border));\n}\n`;\n}\n\n/**\n * Return the Tailwind Play CDN script URL.\n * Play CDN auto-initializes with MutationObserver, generating CSS on-demand.\n */\nfunction getTailwindCdnScript(): string {\n return 'https://cdn.tailwindcss.com';\n}\n\n/**\n * Generate the mounting code for React widgets\n */\nfunction generateMountCode(propsJson: string): string {\n return `\nimport { createRoot } from \"react-dom/client\";\nimport React from \"react\";\n\n// Widget code will set window.__PATCHWORK_WIDGET__\nconst Component = window.__PATCHWORK_WIDGET__;\nconst props = ${propsJson};\n\nif (Component) {\n const root = createRoot(document.getElementById('root'));\n root.render(React.createElement(Component, props));\n window.parent.postMessage({ type: 'patchwork:ready' }, '*');\n} else {\n const root = createRoot(document.getElementById('root'));\n root.render(\n React.createElement('div', { style: { color: 'red', padding: '20px' } },\n React.createElement('h2', null, 'Error: No component found'),\n React.createElement('p', null, 'Widget must export a default component.')\n )\n );\n window.parent.postMessage({ type: 'patchwork:error', message: 'No component found' }, '*');\n}\n\nwindow.addEventListener('error', (e) => {\n window.parent.postMessage({ type: 'patchwork:error', message: e.message }, '*');\n});\n`;\n}\n\n/**\n * Transform compiled widget code to set global export\n */\nexport function transformWidgetCode(code: string): string {\n let result = code;\n\n // Handle: export { X as default }\n const namedMatch = result.match(/export\\s*{\\s*(\\w+)\\s+as\\s+default\\s*}/);\n if (namedMatch) {\n result = result.replace(\n /export\\s*{\\s*\\w+\\s+as\\s+default\\s*};?/,\n `window.__PATCHWORK_WIDGET__ = ${namedMatch[1]};`,\n );\n }\n\n // Handle: export default X or export default function X\n const directMatch = result.match(/export\\s+default\\s+(?:function\\s+)?(\\w+)/);\n if (directMatch && !namedMatch) {\n result = result.replace(\n /export\\s+default\\s+(?:function\\s+)?(\\w+)/,\n `window.__PATCHWORK_WIDGET__ = $1`,\n );\n }\n\n // Remove remaining exports\n return result.replace(/export\\s*{[^}]*};?/g, '');\n}\n\n/**\n * Generate service bridge code for iframe communication\n *\n * Creates JavaScript that sets up:\n * 1. Message handling for service results from parent\n * 2. Dynamic proxy objects for each service namespace\n */\nfunction generateServiceBridge(services: string[]): string {\n if (services.length === 0) return '';\n\n const namespaceAssignments = services\n .map((ns) => `window.${ns} = createNamespaceProxy('${ns}');`)\n .join('\\n ');\n\n return `\n(function() {\n var pendingCalls = new Map();\n\n window.__patchwork_call__ = function(namespace, method, args) {\n return new Promise(function(resolve, reject) {\n var id = Math.random().toString(36).slice(2);\n pendingCalls.set(id, { resolve: resolve, reject: reject });\n window.parent.postMessage({\n type: 'patchwork:call',\n id: id,\n service: namespace,\n method: method,\n args: args\n }, '*');\n setTimeout(function() {\n if (pendingCalls.has(id)) {\n pendingCalls.delete(id);\n reject(new Error('Service call timeout: ' + namespace + '.' + method));\n }\n }, 30000);\n });\n };\n\n window.addEventListener('message', function(e) {\n if (e.data && e.data.type === 'patchwork:response') {\n var pending = pendingCalls.get(e.data.id);\n if (pending) {\n pendingCalls.delete(e.data.id);\n if (e.data.result.success) {\n pending.resolve(e.data.result.data);\n } else {\n pending.reject(new Error(e.data.result.error || 'Service call failed'));\n }\n }\n }\n });\n\n function createNamespaceProxy(namespace) {\n function createNestedProxy(path) {\n var fn = function() {\n return window.__patchwork_call__(namespace, path, Array.prototype.slice.call(arguments));\n };\n return new Proxy(fn, {\n get: function(_, nestedName) {\n if (typeof nestedName === 'symbol') return undefined;\n var newPath = path ? path + '.' + nestedName : nestedName;\n return createNestedProxy(newPath);\n }\n });\n }\n return new Proxy({}, {\n get: function(_, fieldName) {\n if (typeof fieldName === 'symbol') return undefined;\n return createNestedProxy(fieldName);\n }\n });\n }\n\n ${namespaceAssignments}\n})();\n`;\n}\n\n/**\n * Generate a complete HTML document for rendering a widget\n *\n * Uses Tailwind Play CDN which detects class usage via MutationObserver and\n * generates CSS on-demand.\n *\n * @param compiledJs - The compiled widget JavaScript code\n * @param importMap - Import map for dependencies\n * @param options - HTML generation options\n */\nexport function generateHtml(\n compiledJs: string,\n importMap: ImportMapEntry,\n options: HtmlOptions = {},\n): string {\n const {\n title = 'Patchwork Widget',\n theme = 'dark',\n customCss = '',\n props = {},\n services = [],\n } = options;\n\n const themeCss = generateThemeCss(theme);\n const tailwindCdn = getTailwindCdnScript();\n const widgetCode = transformWidgetCode(compiledJs);\n const importMapJson = JSON.stringify({ imports: importMap }, null, 2);\n const propsJson = JSON.stringify(props);\n const mountCode = generateMountCode(propsJson);\n const serviceBridge = generateServiceBridge(services);\n\n return `<!DOCTYPE html>\n<html lang=\"en\" class=\"${theme}\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>${title}</title>\n <!-- Tailwind Play CDN - config must be set BEFORE script loads -->\n <script>\n window.tailwind = {\n config: ${JSON.stringify({\n darkMode: 'class',\n theme: {\n extend: {\n colors: {\n border: 'hsl(var(--border))',\n input: 'hsl(var(--input))',\n ring: 'hsl(var(--ring))',\n background: 'hsl(var(--background))',\n foreground: 'hsl(var(--foreground))',\n primary: {\n DEFAULT: 'hsl(var(--primary))',\n foreground: 'hsl(var(--primary-foreground))',\n },\n secondary: {\n DEFAULT: 'hsl(var(--secondary))',\n foreground: 'hsl(var(--secondary-foreground))',\n },\n destructive: {\n DEFAULT: 'hsl(var(--destructive))',\n foreground: 'hsl(var(--destructive-foreground))',\n },\n muted: {\n DEFAULT: 'hsl(var(--muted))',\n foreground: 'hsl(var(--muted-foreground))',\n },\n accent: {\n DEFAULT: 'hsl(var(--accent))',\n foreground: 'hsl(var(--accent-foreground))',\n },\n popover: {\n DEFAULT: 'hsl(var(--popover))',\n foreground: 'hsl(var(--popover-foreground))',\n },\n card: {\n DEFAULT: 'hsl(var(--card))',\n foreground: 'hsl(var(--card-foreground))',\n },\n },\n borderRadius: {\n lg: 'var(--radius)',\n md: 'calc(var(--radius) - 2px)',\n sm: 'calc(var(--radius) - 4px)',\n },\n },\n },\n })}\n };\n </script>\n <script src=\"${tailwindCdn}\" crossorigin></script>\n <script type=\"importmap\">${importMapJson}</script>\n <style>${themeCss}${customCss}</style>\n</head>\n<body>\n <div id=\"root\"></div>\n <script>\n // Service bridge\n ${serviceBridge}\n </script>\n <script type=\"module\">\n // Widget code\n ${widgetCode}\n </script>\n <script type=\"module\">\n // Mount the widget\n ${mountCode}\n </script>\n</body>\n</html>`;\n}\n\n/**\n * Get the default import map for this image\n */\nexport function getDefaultImportMap(cdn = 'https://esm.sh'): ImportMapEntry {\n return {\n react: `${cdn}/react@18`,\n 'react/': `${cdn}/react@18/`,\n 'react-dom': `${cdn}/react-dom@18`,\n 'react-dom/': `${cdn}/react-dom@18/`,\n 'react-dom/client': `${cdn}/react-dom@18/client`,\n };\n}\n\n/**\n * Get framework dependencies declared by this image\n */\nexport function getFrameworkDependencies(): Record<string, string> {\n return {\n react: '^18.0.0',\n 'react-dom': '^18.0.0',\n };\n}\n"],"mappings":";;;;;;AA+BA,SAAS,iBAAiB,OAAiC;AACzD,QAAM,OAAO,UAAU,SAAS,qBAAqB;AACrD,QAAM,gBAAgB,OAAO,QAAQ,IAAI,EACtC,IAAI,CAAC,CAAC,KAAK,KAAK,MAAM,KAAK,GAAG,KAAK,KAAK,GAAG,EAC3C,KAAK,IAAI;AAEZ,SAAO;AAAA;AAAA,EAEP,aAAa;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAoBf;AAMA,SAAS,uBAA+B;AACtC,SAAO;AACT;AAKA,SAAS,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