@widgetic/creator 0.4.36 → 0.4.38
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/CreatorApp.svelte +241 -27
- package/dist/components/BrandKitPanel.svelte +585 -0
- package/dist/components/BrandKitPanel.svelte.d.ts +34 -0
- package/dist/components/FileUrlSourceControl.svelte.d.ts +1 -1
- package/dist/components/VersionDropdownRow.svelte +1 -19
- package/dist/components/WidgetDetails.svelte.d.ts +1 -1
- package/dist/components/WidgetElementSelector.svelte +149 -33
- package/dist/components/WidgetElementSelector.svelte.d.ts +1 -0
- package/dist/components/WidgetShapeLivePreviews.svelte +9 -2
- package/dist/components/WidgetShapeLivePreviews.svelte.d.ts +4 -0
- package/dist/generation/brandKitForRequest.d.ts +17 -0
- package/dist/generation/brandKitForRequest.js +18 -1
- package/dist/host/creatorHost.d.ts +7 -0
- package/dist/host/creatorHost.js +9 -0
- package/dist/i18n/language.d.ts +174 -5
- package/dist/i18n/messages.d.ts +27 -1
- package/dist/i18n/messages.js +27 -1
- package/dist/i18n/statusText.js +4 -0
- package/dist/pageHelpers.d.ts +2 -0
- package/dist/pageHelpers.js +10 -0
- package/dist/stores/websocketStore.d.ts +9 -0
- package/dist/stores/websocketStore.js +20 -0
- package/dist/utils/versionRatings.d.ts +65 -0
- package/dist/utils/versionRatings.js +97 -0
- package/dist/utils/widgetElementSelector.d.ts +6 -0
- package/dist/utils/widgetElementSelector.js +23 -0
- package/package.json +3 -3
- package/dist/components/GenerationOptionsPanel.svelte +0 -216
- package/dist/components/GenerationOptionsPanel.svelte.d.ts +0 -21
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* versionRatings — the thumbs a person gives to the versions of a widget in the creator's chat.
|
|
3
|
+
*
|
|
4
|
+
* The chat (`@widgetic/chat`) draws two thumbs next to each version and tells the creator what the person pressed (`onRateVersion`).
|
|
5
|
+
* The creator does two things with it:
|
|
6
|
+
*
|
|
7
|
+
* 1. It tells the API (`PUT /conversations/{conversationId}/messages/{messageId}/rating`, the generated `rateMessageVersionRaw`),
|
|
8
|
+
* which keeps the word for Widget Lessons (what the code generator learns across people). The API takes three words: `up`, `down`
|
|
9
|
+
* and `none` (a rating taken back).
|
|
10
|
+
* 2. It remembers the thumbs here, in the creator's own storage, so the thumbs is still showing when the chat opens again: the API
|
|
11
|
+
* has no call that lists ratings, so the chat is handed what the creator remembers (`versionRatings`).
|
|
12
|
+
*
|
|
13
|
+
* Nothing here throws into the chat except the call to the API: when it fails the chat puts the thumbs back as it was, and nothing is
|
|
14
|
+
* remembered. A storage that is full or blocked is never a reason to fail a press.
|
|
15
|
+
*/
|
|
16
|
+
/** What a person can say about a version: a thumbs up, a thumbs down, or nothing (a rating taken back). */
|
|
17
|
+
export type VersionRating = 'up' | 'down' | 'none';
|
|
18
|
+
/** Where the creator remembers the thumbs: one record of the id of the message that announced a version, to `up` or `down`. */
|
|
19
|
+
export declare const VERSION_RATINGS_STORAGE_KEY = "widgetCreator_versionRatings_v1";
|
|
20
|
+
/** How many thumbs are remembered: the newest ones, because an old version is rarely looked at again. */
|
|
21
|
+
export declare const MAX_REMEMBERED_VERSION_RATINGS = 500;
|
|
22
|
+
/** What the creator asks of the API client: the generated `MessagesApi` has this call once the SDK is regenerated. */
|
|
23
|
+
export interface VersionRatingClient {
|
|
24
|
+
rateMessageVersionRaw(requestParameters: {
|
|
25
|
+
conversationId: string;
|
|
26
|
+
messageId: string;
|
|
27
|
+
rateMessageVersionRequest: {
|
|
28
|
+
rating: VersionRating;
|
|
29
|
+
};
|
|
30
|
+
}): Promise<{
|
|
31
|
+
raw: {
|
|
32
|
+
json(): Promise<unknown>;
|
|
33
|
+
};
|
|
34
|
+
}>;
|
|
35
|
+
}
|
|
36
|
+
/** Which version of which conversation is rated, and how. */
|
|
37
|
+
export interface VersionRatingRequest {
|
|
38
|
+
conversationId: string;
|
|
39
|
+
messageId: string;
|
|
40
|
+
rating: VersionRating;
|
|
41
|
+
}
|
|
42
|
+
/** What the API said about the word it was sent. */
|
|
43
|
+
export interface VersionRatingOutcome {
|
|
44
|
+
/** Whether the API kept the word: `null` when its answer could not be read. */
|
|
45
|
+
recorded: boolean | null;
|
|
46
|
+
/** Why it did not (`switched_off`, `no_record`, `unavailable`), when it said. */
|
|
47
|
+
reason: string | null;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Tells the API what the person pressed. The API answers 200 for anyone who may rate, and says in the answer when it could not keep the
|
|
51
|
+
* word (`recorded: false`: the version is older than the tracing, or the learning is switched off); that is no failure for the person,
|
|
52
|
+
* so it is handed back for the caller to note and nothing more. A call that fails (offline, refused) throws, and the chat undoes the press.
|
|
53
|
+
*/
|
|
54
|
+
export declare function rateVersionThroughApi(client: VersionRatingClient, request: VersionRatingRequest): Promise<VersionRatingOutcome>;
|
|
55
|
+
/** The thumbs found in what the storage held: a record of message id to `up` or `down`, and nothing else. Anything unreadable is nothing. */
|
|
56
|
+
export declare function parseRememberedVersionRatings(stored: string | null | undefined): Record<string, VersionRating>;
|
|
57
|
+
/**
|
|
58
|
+
* The thumbs after a version was rated: the rating replaces the one the version had and is put last, `none` takes it away, and past
|
|
59
|
+
* `limit` the oldest are dropped. The record that was given is not changed.
|
|
60
|
+
*/
|
|
61
|
+
export declare function rememberVersionRating(ratings: Readonly<Record<string, VersionRating>>, messageId: string, rating: VersionRating, limit?: number): Record<string, VersionRating>;
|
|
62
|
+
/** The thumbs the creator remembers. A storage that cannot be read is no thumbs. */
|
|
63
|
+
export declare function loadRememberedVersionRatings(): Record<string, VersionRating>;
|
|
64
|
+
/** Remembers the thumbs. A storage that is full or blocked is never a reason to fail a press. */
|
|
65
|
+
export declare function saveRememberedVersionRatings(ratings: Readonly<Record<string, VersionRating>>): void;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* versionRatings — the thumbs a person gives to the versions of a widget in the creator's chat.
|
|
3
|
+
*
|
|
4
|
+
* The chat (`@widgetic/chat`) draws two thumbs next to each version and tells the creator what the person pressed (`onRateVersion`).
|
|
5
|
+
* The creator does two things with it:
|
|
6
|
+
*
|
|
7
|
+
* 1. It tells the API (`PUT /conversations/{conversationId}/messages/{messageId}/rating`, the generated `rateMessageVersionRaw`),
|
|
8
|
+
* which keeps the word for Widget Lessons (what the code generator learns across people). The API takes three words: `up`, `down`
|
|
9
|
+
* and `none` (a rating taken back).
|
|
10
|
+
* 2. It remembers the thumbs here, in the creator's own storage, so the thumbs is still showing when the chat opens again: the API
|
|
11
|
+
* has no call that lists ratings, so the chat is handed what the creator remembers (`versionRatings`).
|
|
12
|
+
*
|
|
13
|
+
* Nothing here throws into the chat except the call to the API: when it fails the chat puts the thumbs back as it was, and nothing is
|
|
14
|
+
* remembered. A storage that is full or blocked is never a reason to fail a press.
|
|
15
|
+
*/
|
|
16
|
+
import { creatorStorage } from '../host/creatorHost.js';
|
|
17
|
+
/** Where the creator remembers the thumbs: one record of the id of the message that announced a version, to `up` or `down`. */
|
|
18
|
+
export const VERSION_RATINGS_STORAGE_KEY = 'widgetCreator_versionRatings_v1';
|
|
19
|
+
/** How many thumbs are remembered: the newest ones, because an old version is rarely looked at again. */
|
|
20
|
+
export const MAX_REMEMBERED_VERSION_RATINGS = 500;
|
|
21
|
+
/**
|
|
22
|
+
* Tells the API what the person pressed. The API answers 200 for anyone who may rate, and says in the answer when it could not keep the
|
|
23
|
+
* word (`recorded: false`: the version is older than the tracing, or the learning is switched off); that is no failure for the person,
|
|
24
|
+
* so it is handed back for the caller to note and nothing more. A call that fails (offline, refused) throws, and the chat undoes the press.
|
|
25
|
+
*/
|
|
26
|
+
export async function rateVersionThroughApi(client, request) {
|
|
27
|
+
const { conversationId, messageId, rating } = request;
|
|
28
|
+
const response = await client.rateMessageVersionRaw({ conversationId, messageId, rateMessageVersionRequest: { rating } });
|
|
29
|
+
try {
|
|
30
|
+
const body = (await response.raw.json());
|
|
31
|
+
const recorded = typeof body?.data?.recorded === 'boolean' ? body.data.recorded : null;
|
|
32
|
+
const reason = typeof body?.data?.reason === 'string' ? body.data.reason : null;
|
|
33
|
+
return { recorded, reason };
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
/* an answer that cannot be read changes nothing for the person: the word was sent */
|
|
37
|
+
return { recorded: null, reason: null };
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** The thumbs found in what the storage held: a record of message id to `up` or `down`, and nothing else. Anything unreadable is nothing. */
|
|
41
|
+
export function parseRememberedVersionRatings(stored) {
|
|
42
|
+
if (!stored)
|
|
43
|
+
return {};
|
|
44
|
+
try {
|
|
45
|
+
const parsed = JSON.parse(stored);
|
|
46
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed))
|
|
47
|
+
return {};
|
|
48
|
+
const remembered = {};
|
|
49
|
+
for (const [messageId, rating] of Object.entries(parsed)) {
|
|
50
|
+
if (rating === 'up' || rating === 'down')
|
|
51
|
+
remembered[messageId] = rating;
|
|
52
|
+
}
|
|
53
|
+
return remembered;
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return {};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The thumbs after a version was rated: the rating replaces the one the version had and is put last, `none` takes it away, and past
|
|
61
|
+
* `limit` the oldest are dropped. The record that was given is not changed.
|
|
62
|
+
*/
|
|
63
|
+
export function rememberVersionRating(ratings, messageId, rating, limit = MAX_REMEMBERED_VERSION_RATINGS) {
|
|
64
|
+
const others = {};
|
|
65
|
+
for (const [otherId, otherRating] of Object.entries(ratings)) {
|
|
66
|
+
if (otherId !== messageId)
|
|
67
|
+
others[otherId] = otherRating;
|
|
68
|
+
}
|
|
69
|
+
if (rating === 'none')
|
|
70
|
+
return others;
|
|
71
|
+
const next = { ...others, [messageId]: rating };
|
|
72
|
+
const ids = Object.keys(next);
|
|
73
|
+
if (ids.length <= limit)
|
|
74
|
+
return next;
|
|
75
|
+
const kept = {};
|
|
76
|
+
for (const id of ids.slice(ids.length - Math.max(1, limit)))
|
|
77
|
+
kept[id] = next[id];
|
|
78
|
+
return kept;
|
|
79
|
+
}
|
|
80
|
+
/** The thumbs the creator remembers. A storage that cannot be read is no thumbs. */
|
|
81
|
+
export function loadRememberedVersionRatings() {
|
|
82
|
+
try {
|
|
83
|
+
return parseRememberedVersionRatings(creatorStorage.getItem(VERSION_RATINGS_STORAGE_KEY));
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return {};
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/** Remembers the thumbs. A storage that is full or blocked is never a reason to fail a press. */
|
|
90
|
+
export function saveRememberedVersionRatings(ratings) {
|
|
91
|
+
try {
|
|
92
|
+
creatorStorage.setItem(VERSION_RATINGS_STORAGE_KEY, JSON.stringify(ratings));
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
/* a full or blocked store must never break a press */
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -43,6 +43,8 @@ export interface SelectedWidgetElementReference {
|
|
|
43
43
|
};
|
|
44
44
|
/** Compact human-readable label (`<em> · to shared company work.`) */
|
|
45
45
|
label: string;
|
|
46
|
+
/** A note typed on the preview, sent with this part. */
|
|
47
|
+
note?: string;
|
|
46
48
|
}
|
|
47
49
|
/**
|
|
48
50
|
* Whether the element may be picked. A plain boolean on purpose: a type guard (`element is Element`) would make the
|
|
@@ -79,4 +81,8 @@ export declare function computeChipPlacement(rect: SelectedElementRect, viewport
|
|
|
79
81
|
* Inline script injected into fallback `srcdoc` mirror iframes when a widget preview
|
|
80
82
|
* was cached before `PICKER_BRIDGE_SCRIPT` was added to the Cloudflare builder worker.
|
|
81
83
|
*/
|
|
84
|
+
/** The page CSP nonce, so a srcdoc mirror can run under the same policy as the creator. */
|
|
85
|
+
export declare function pageScriptNonce(): string;
|
|
86
|
+
/** Puts the page nonce on every script so the strict policy does not block the mirror. */
|
|
87
|
+
export declare function stampScriptsWithNonce(html: string, nonce: string): string;
|
|
82
88
|
export declare const FALLBACK_PICKER_BRIDGE_SCRIPT = "<script>(function(){\n if(window.__widgeticPickerBridge)return;\n window.__widgeticPickerBridge=true;\n var NEVER_PICKED={html:1,body:1,head:1,script:1,style:1,link:1,meta:1};\n var SVG_NS='http://www.w3.org/2000/svg';\n var hovered=null,current=null,descendants=[];\n function isPickable(el){\n if(!el||!el.tagName)return false;\n return !NEVER_PICKED[el.tagName.toLowerCase()];\n }\n function pickTarget(el){\n var node=el;\n while(node&&node.namespaceURI===SVG_NS&&node.tagName.toLowerCase()!=='svg'){\n if(!node.parentElement)break;\n node=node.parentElement;\n }\n return isPickable(node)?node:null;\n }\n function parentToPick(el){\n var next=el.parentElement||(el.getRootNode&&el.getRootNode()instanceof ShadowRoot?el.getRootNode().host:null);\n while(next&&!isPickable(next)){\n next=next.parentElement||(next.getRootNode&&next.getRootNode()instanceof ShadowRoot?next.getRootNode().host:null);\n }\n if(next&&next.id==='app'&&next.parentElement===document.body)return null;\n return next;\n }\n function deepElementAt(root,x,y){\n var cur=root,hit=null;\n for(var d=0;d<24;d++){\n var list=cur.elementsFromPoint?cur.elementsFromPoint(x,y):[];\n var found=null;\n for(var i=0;i<list.length;i++){\n var c=list[i];\n if(c.id==='app'&&list.length>i+1&&!NEVER_PICKED[list[i+1].tagName.toLowerCase()])continue;\n found=c;break;\n }\n if(!found)break;\n hit=found;\n if(!found.shadowRoot||found.shadowRoot===cur)break;\n cur=found.shadowRoot;\n }\n return hit;\n }\n function buildSelector(el){\n var parts=[],node=el;\n while(node&&node.nodeType===1&&node!==document.body&&node.id!=='app'&&parts.length<6){\n var tag=node.tagName.toLowerCase();\n if(node.id){parts.unshift(tag+'#'+node.id);break;}\n var cls=Array.from(node.classList||[]).filter(function(c){return /^[A-Za-z_][\\w-]{0,31}$/.test(c)}).slice(0,2);\n var seg=tag+(cls.length?'.'+cls.join('.'):'');\n parts.unshift(seg);\n node=node.parentElement;\n }\n return parts.join(' > ')||el.tagName.toLowerCase();\n }\n function serialize(el){\n if(!el)return null;\n var r=el.getBoundingClientRect();\n var tag=el.tagName.toLowerCase();\n var classes=Array.from(el.classList||[]);\n var rawText=(el.innerText||el.textContent||el.getAttribute('aria-label')||el.getAttribute('placeholder')||el.getAttribute('alt')||'').replace(/\\s+/g,' ').trim().slice(0,120);\n var dc=el.getAttribute('data-content')||el.getAttribute('data-content-item')||null;\n var p=el.parentElement;\n var parentTag=p&&p!==document.body&&p.id!=='app'?(p.tagName.toLowerCase()+(p.classList&&p.classList[0]?'.'+p.classList[0]:'')):null;\n var attrs={};\n ['role','type','href','src','aria-label','placeholder','name'].forEach(function(k){\n var v=el.getAttribute(k);if(v)attrs[k]=v.slice(0,160);\n });\n var labelClass=classes.find(function(c){return /^[A-Za-z_][\\w-]{0,31}$/.test(c)&&!/^(flex|grid|block|inline|relative|absolute|w-|h-|p-|m-|text-|bg-|border|rounded)/.test(c)})||classes[0]||'';\n var tagBadge='<'+tag+(el.id?'#'+el.id:labelClass?'.'+labelClass:'')+'>';\n var label=rawText?(tagBadge+' \\u00b7 '+rawText.slice(0,48)):(tagBadge+' \\u00b7 '+Math.round(r.width)+' \\u00d7 '+Math.round(r.height));\n return {\n tagName:tag,\n selector:buildSelector(el),\n idAttr:el.id||undefined,\n classList:classes.slice(0,16),\n textSnippet:rawText||undefined,\n dataContent:dc||undefined,\n parentTag:parentTag||undefined,\n outerHtmlSnippet:(el.outerHTML||'').slice(0,800),\n attributes:attrs,\n rect:{x:r.left,y:r.top,width:r.width,height:r.height},\n label:label\n };\n }\n window.addEventListener('message',function(e){\n var d=e.data;if(!d||typeof d.type!=='string'||!d.type.startsWith('widgetic:picker-'))return;\n var wid=d.widgetId||null;\n if(d.type==='widgetic:picker-ping'){\n try{window.parent.postMessage({type:'widgetic:picker-pong',widgetId:wid},'*')}catch(err){}\n return;\n }\n if(d.type==='widgetic:picker-hit-test'){\n var raw=deepElementAt(document,Number(d.x)||0,Number(d.y)||0);\n var picked=raw?pickTarget(raw):null;\n if(picked!==hovered){hovered=picked;current=picked;descendants=[];}\n try{window.parent.postMessage({type:'widgetic:picker-hover-result',widgetId:wid,element:serialize(current)},'*')}catch(err){}\n return;\n }\n if(d.type==='widgetic:picker-walk'){\n if(!current)return;\n if(d.direction==='up'){\n var prev=current,cand=parentToPick(current);\n while(cand){\n var cr=cand.getBoundingClientRect();\n if(cr.width>0&&cr.height>0)break;\n cand=parentToPick(cand);\n }\n if(cand){descendants.push(prev);current=cand;}\n }else if(d.direction==='down'){\n var child=descendants.pop();\n if(child)current=child;\n }\n try{window.parent.postMessage({type:'widgetic:picker-hover-result',widgetId:wid,element:serialize(current)},'*')}catch(err){}\n return;\n }\n if(d.type==='widgetic:picker-select'){\n var target=current;\n if(!target&&typeof d.x==='number'&&typeof d.y==='number'){\n var hit=deepElementAt(document,d.x,d.y);\n target=hit?pickTarget(hit):null;\n }\n try{window.parent.postMessage({type:'widgetic:picker-select-result',widgetId:wid,element:serialize(target)},'*')}catch(err){}\n }\n });\n})();</script>";
|
|
@@ -192,6 +192,29 @@ export function computeChipPlacement(rect, viewportWidth, viewportHeight, chipHe
|
|
|
192
192
|
* Inline script injected into fallback `srcdoc` mirror iframes when a widget preview
|
|
193
193
|
* was cached before `PICKER_BRIDGE_SCRIPT` was added to the Cloudflare builder worker.
|
|
194
194
|
*/
|
|
195
|
+
/** The page CSP nonce, so a srcdoc mirror can run under the same policy as the creator. */
|
|
196
|
+
export function pageScriptNonce() {
|
|
197
|
+
if (typeof document === 'undefined')
|
|
198
|
+
return '';
|
|
199
|
+
const scripts = document.querySelectorAll('script');
|
|
200
|
+
for (const script of scripts) {
|
|
201
|
+
const value = script.nonce || script.getAttribute('nonce') || '';
|
|
202
|
+
if (value)
|
|
203
|
+
return value;
|
|
204
|
+
}
|
|
205
|
+
return '';
|
|
206
|
+
}
|
|
207
|
+
/** Puts the page nonce on every script so the strict policy does not block the mirror. */
|
|
208
|
+
export function stampScriptsWithNonce(html, nonce) {
|
|
209
|
+
if (!nonce)
|
|
210
|
+
return html;
|
|
211
|
+
const safe = nonce.replace(/"/g, '');
|
|
212
|
+
return html.replace(/<script\b([^>]*)>/gi, (match, attrs) => {
|
|
213
|
+
if (/\bnonce\s*=/i.test(attrs))
|
|
214
|
+
return match;
|
|
215
|
+
return `<script nonce="${safe}"${attrs}>`;
|
|
216
|
+
});
|
|
217
|
+
}
|
|
195
218
|
export const FALLBACK_PICKER_BRIDGE_SCRIPT = `<script>(function(){
|
|
196
219
|
if(window.__widgeticPickerBridge)return;
|
|
197
220
|
window.__widgeticPickerBridge=true;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@widgetic/creator",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.38",
|
|
4
4
|
"license": "SEE LICENSE IN LICENSE",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"peerDependencies": {
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"@widgetic/api-sdk": "^2.0.0",
|
|
9
9
|
"@widgetic/cache-layer": "^0.1.2",
|
|
10
10
|
"@widgetic/canvas": "^0.5.27",
|
|
11
|
-
"@widgetic/chat": "^0.1.
|
|
11
|
+
"@widgetic/chat": "^0.1.34",
|
|
12
12
|
"@widgetic/design-system": "^0.5.15",
|
|
13
13
|
"@widgetic/editor": "^4.0.10",
|
|
14
14
|
"@widgetic/file-browser": "^0.5.24",
|
|
@@ -75,7 +75,7 @@
|
|
|
75
75
|
"@widgetic/api-sdk": "^2.0.0",
|
|
76
76
|
"@widgetic/cache-layer": "^0.1.2",
|
|
77
77
|
"@widgetic/canvas": "^0.5.27",
|
|
78
|
-
"@widgetic/chat": "^0.1.
|
|
78
|
+
"@widgetic/chat": "^0.1.34",
|
|
79
79
|
"@widgetic/design-system": "^0.5.15",
|
|
80
80
|
"@widgetic/dev-tools": "^0.3.0",
|
|
81
81
|
"@widgetic/editor": "^4.0.10",
|
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
<!--
|
|
2
|
-
Generation options of the chat: the language of a new widget and, in debug mode, the preset and uploaded libraries.
|
|
3
|
-
The brand kit (colors, fonts, icons, spacing, corner radius and style notes) is not here: it belongs to the account
|
|
4
|
-
and is edited in the account settings, so every widget of that account uses it. (The `creator.brandKit.*` message
|
|
5
|
-
keys keep the name they had when this panel held the brand kit; they hold the texts of these options.)
|
|
6
|
-
-->
|
|
7
|
-
|
|
8
|
-
<script lang="ts">
|
|
9
|
-
import { DropdownMenu } from '@widgetic/design-system';
|
|
10
|
-
import FileUrlSourceControl from './FileUrlSourceControl.svelte';
|
|
11
|
-
import HoverTooltip from './HoverTooltip.svelte';
|
|
12
|
-
import { useCreatorLanguage } from '../i18n/index.js';
|
|
13
|
-
|
|
14
|
-
const { t } = useCreatorLanguage();
|
|
15
|
-
|
|
16
|
-
interface Props {
|
|
17
|
-
class?: string;
|
|
18
|
-
style?: string;
|
|
19
|
-
part?: 'checks' | 'panel';
|
|
20
|
-
librariesEnabled?: boolean;
|
|
21
|
-
showPresetLibraries?: boolean;
|
|
22
|
-
libraries?: string[];
|
|
23
|
-
libraryGitUrl?: string;
|
|
24
|
-
libraryFileName?: string;
|
|
25
|
-
libraryUploads?: { name: string; content: string; encoding: string }[];
|
|
26
|
-
showLanguage?: boolean;
|
|
27
|
-
language?: string;
|
|
28
|
-
languageLocked?: boolean;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
let {
|
|
32
|
-
class: className = '',
|
|
33
|
-
style = '',
|
|
34
|
-
part = 'checks',
|
|
35
|
-
librariesEnabled = $bindable(false),
|
|
36
|
-
showPresetLibraries = false,
|
|
37
|
-
libraries = $bindable([] as string[]),
|
|
38
|
-
libraryGitUrl = $bindable(''),
|
|
39
|
-
libraryFileName = $bindable(''),
|
|
40
|
-
libraryUploads = $bindable([] as { name: string; content: string; encoding: string }[]),
|
|
41
|
-
showLanguage = false,
|
|
42
|
-
language = $bindable('react'),
|
|
43
|
-
languageLocked = false,
|
|
44
|
-
}: Props = $props();
|
|
45
|
-
|
|
46
|
-
const libraryChoices = [
|
|
47
|
-
{ id: 'chart.js/auto', label: 'Chart.js' },
|
|
48
|
-
{ id: 'd3', label: 'D3' },
|
|
49
|
-
{ id: 'gsap', label: 'GSAP' },
|
|
50
|
-
{ id: 'three', label: 'Three.js' },
|
|
51
|
-
];
|
|
52
|
-
|
|
53
|
-
function toggleLibrary(id: string) {
|
|
54
|
-
const next = libraries.includes(id)
|
|
55
|
-
? libraries.filter((entry) => entry !== id)
|
|
56
|
-
: [...libraries, id];
|
|
57
|
-
libraries = next;
|
|
58
|
-
if (next.length > 0) librariesEnabled = true;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
type SourceRow = { id: string; fileName: string; url: string; content: string; encoding: string };
|
|
62
|
-
const emptySource = (): SourceRow => ({
|
|
63
|
-
id: crypto.randomUUID(),
|
|
64
|
-
fileName: '',
|
|
65
|
-
url: '',
|
|
66
|
-
content: '',
|
|
67
|
-
encoding: 'utf8',
|
|
68
|
-
});
|
|
69
|
-
let libraryRows = $state<SourceRow[]>([emptySource()]);
|
|
70
|
-
|
|
71
|
-
function addSourceRow() {
|
|
72
|
-
if (libraryRows.length >= 4) return;
|
|
73
|
-
libraryRows = [emptySource(), ...libraryRows];
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function removeSourceRow(id: string) {
|
|
77
|
-
const next = libraryRows.filter((row) => row.id !== id);
|
|
78
|
-
libraryRows = next.length > 0 ? next : [emptySource()];
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function onLibrariesChange(event: Event) {
|
|
82
|
-
librariesEnabled = (event.currentTarget as HTMLInputElement).checked;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function componentExtension() {
|
|
86
|
-
return language === 'react' ? '.jsx' : '.svelte';
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function sourceAccept() {
|
|
90
|
-
return ['.js', '.ts', componentExtension(), '.zip', '.tar', '.tgz', '.gz'].join(',');
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
$effect(() => {
|
|
94
|
-
libraryFileName = libraryRows[0]?.fileName || '';
|
|
95
|
-
libraryGitUrl = libraryRows[0]?.url || '';
|
|
96
|
-
libraryUploads = libraryRows
|
|
97
|
-
.filter((row) => row.fileName && row.content)
|
|
98
|
-
.map((row) => ({ name: row.fileName, content: row.content, encoding: row.encoding }));
|
|
99
|
-
});
|
|
100
|
-
</script>
|
|
101
|
-
|
|
102
|
-
{#if part === 'checks'}
|
|
103
|
-
<div class="generation-options-checks contents {className}" {style}>
|
|
104
|
-
{#if showLanguage || showPresetLibraries}
|
|
105
|
-
<div class="generation-libraries-row ms-auto flex w-full flex-col gap-2">
|
|
106
|
-
<div class="generation-libraries-checks flex flex-nowrap items-center gap-1">
|
|
107
|
-
{#if showLanguage}
|
|
108
|
-
{#if languageLocked}
|
|
109
|
-
<HoverTooltip class="inline-flex shrink-0" text={$t('creator.brandKit.languageIsFixedAfterThe')}>
|
|
110
|
-
<span
|
|
111
|
-
class="generation-language-select flex h-10 w-[4.75rem] shrink-0 cursor-default items-center justify-between gap-0.5 rounded-lg border border-gray-300 bg-white px-1.5 text-sm text-gray-900 opacity-60"
|
|
112
|
-
aria-disabled="true"
|
|
113
|
-
>
|
|
114
|
-
<span class="truncate">{language === 'react' ? 'React' : 'Svelte'}</span>
|
|
115
|
-
</span>
|
|
116
|
-
</HoverTooltip>
|
|
117
|
-
{:else}
|
|
118
|
-
<HoverTooltip class="inline-flex shrink-0" text={$t('creator.brandKit.selectLanguage')}>
|
|
119
|
-
<DropdownMenu.Root>
|
|
120
|
-
<DropdownMenu.Trigger
|
|
121
|
-
class="generation-language-select flex h-10 w-[4.75rem] shrink-0 cursor-pointer items-center justify-between gap-0.5 rounded-lg border border-gray-300 bg-white px-1.5 text-sm text-gray-900"
|
|
122
|
-
>
|
|
123
|
-
<span class="truncate">{language === 'react' ? 'React' : 'Svelte'}</span>
|
|
124
|
-
<svg class="h-3 w-3 shrink-0 text-gray-500" viewBox="0 0 12 12" fill="none" aria-hidden="true">
|
|
125
|
-
<path d="M3 4.5 6 7.5 9 4.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" />
|
|
126
|
-
</svg>
|
|
127
|
-
</DropdownMenu.Trigger>
|
|
128
|
-
<DropdownMenu.Content class="w-[4.75rem] min-w-0">
|
|
129
|
-
<DropdownMenu.Item
|
|
130
|
-
class="cursor-pointer {language === 'svelte' ? 'bg-[#314F2F]/15 text-[#314F2F]' : ''}"
|
|
131
|
-
onSelect={() => (language = 'svelte')}
|
|
132
|
-
>Svelte</DropdownMenu.Item>
|
|
133
|
-
<DropdownMenu.Item
|
|
134
|
-
class="cursor-pointer {language === 'react' ? 'bg-[#314F2F]/15 text-[#314F2F]' : ''}"
|
|
135
|
-
onSelect={() => (language = 'react')}
|
|
136
|
-
>React</DropdownMenu.Item>
|
|
137
|
-
</DropdownMenu.Content>
|
|
138
|
-
</DropdownMenu.Root>
|
|
139
|
-
</HoverTooltip>
|
|
140
|
-
{/if}
|
|
141
|
-
{/if}
|
|
142
|
-
{#if showPresetLibraries}
|
|
143
|
-
<label class="generation-option-toggle flex h-10 shrink-0 cursor-pointer items-center gap-1.5 rounded-lg border px-2 text-sm {librariesEnabled ? 'border-[#314F2F] bg-[#314F2F] text-white' : 'border-gray-300 bg-white text-gray-800'}">
|
|
144
|
-
<input class="generation-option-check cursor-pointer" type="checkbox" checked={librariesEnabled} onchange={onLibrariesChange} />
|
|
145
|
-
{$t('creator.brandKit.libraries')}
|
|
146
|
-
</label>
|
|
147
|
-
{/if}
|
|
148
|
-
{#if librariesEnabled && showPresetLibraries}
|
|
149
|
-
{#each libraryChoices as choice (choice.id)}
|
|
150
|
-
<label class="generation-library flex items-center gap-1 text-xs text-gray-700">
|
|
151
|
-
<input
|
|
152
|
-
class="generation-library-check cursor-pointer"
|
|
153
|
-
type="checkbox"
|
|
154
|
-
checked={libraries.includes(choice.id)}
|
|
155
|
-
onchange={() => toggleLibrary(choice.id)}
|
|
156
|
-
/>
|
|
157
|
-
{choice.label}
|
|
158
|
-
</label>
|
|
159
|
-
{/each}
|
|
160
|
-
{/if}
|
|
161
|
-
{#if librariesEnabled && showPresetLibraries}
|
|
162
|
-
<HoverTooltip class="inline-flex shrink-0" text={$t('creator.brandKit.addLibrary')}>
|
|
163
|
-
<button
|
|
164
|
-
type="button"
|
|
165
|
-
class="generation-library-add flex h-7 w-7 shrink-0 cursor-pointer items-center justify-center rounded-md border border-gray-300 bg-white text-sm text-gray-800"
|
|
166
|
-
aria-label={$t('creator.brandKit.addLibrary')}
|
|
167
|
-
onclick={addSourceRow}
|
|
168
|
-
>+</button>
|
|
169
|
-
</HoverTooltip>
|
|
170
|
-
{/if}
|
|
171
|
-
</div>
|
|
172
|
-
{#if librariesEnabled && showPresetLibraries}
|
|
173
|
-
{#each libraryRows as row (row.id)}
|
|
174
|
-
<div class="generation-source-row flex items-center gap-1">
|
|
175
|
-
<FileUrlSourceControl
|
|
176
|
-
class="min-w-0 flex-1"
|
|
177
|
-
bind:fileName={row.fileName}
|
|
178
|
-
bind:fileContent={row.content}
|
|
179
|
-
bind:fileEncoding={row.encoding}
|
|
180
|
-
bind:url={row.url}
|
|
181
|
-
fileLabel={$t('creator.brandKit.uploadLibrary')}
|
|
182
|
-
fileTitle={$t('creator.brandKit.uploadCustomLibraryTooltip')}
|
|
183
|
-
urlPlaceholder={$t('creator.brandKit.urlLibraries', { extension: componentExtension() })}
|
|
184
|
-
accept={sourceAccept()}
|
|
185
|
-
/>
|
|
186
|
-
{#if libraryRows.length > 1}
|
|
187
|
-
<button
|
|
188
|
-
type="button"
|
|
189
|
-
class="generation-source-remove flex h-7 w-7 shrink-0 cursor-pointer items-center justify-center rounded-md border border-gray-300 text-gray-500"
|
|
190
|
-
aria-label={$t('creator.brandKit.removeLibrary')}
|
|
191
|
-
onclick={() => removeSourceRow(row.id)}
|
|
192
|
-
>
|
|
193
|
-
<svg class="h-3.5 w-3.5" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">
|
|
194
|
-
<path d="M4 7h16" />
|
|
195
|
-
<path d="M9 7V5h6v2" />
|
|
196
|
-
<path d="M8 7l1 13h6l1-13" />
|
|
197
|
-
</svg>
|
|
198
|
-
</button>
|
|
199
|
-
{/if}
|
|
200
|
-
</div>
|
|
201
|
-
{/each}
|
|
202
|
-
{/if}
|
|
203
|
-
</div>
|
|
204
|
-
{/if}
|
|
205
|
-
</div>
|
|
206
|
-
{/if}
|
|
207
|
-
|
|
208
|
-
<style>
|
|
209
|
-
.generation-options-checks input:focus,
|
|
210
|
-
.generation-options-checks input:focus-visible,
|
|
211
|
-
.generation-language-select:focus,
|
|
212
|
-
.generation-language-select:focus-visible {
|
|
213
|
-
outline: none;
|
|
214
|
-
box-shadow: none;
|
|
215
|
-
}
|
|
216
|
-
</style>
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
interface Props {
|
|
2
|
-
class?: string;
|
|
3
|
-
style?: string;
|
|
4
|
-
part?: 'checks' | 'panel';
|
|
5
|
-
librariesEnabled?: boolean;
|
|
6
|
-
showPresetLibraries?: boolean;
|
|
7
|
-
libraries?: string[];
|
|
8
|
-
libraryGitUrl?: string;
|
|
9
|
-
libraryFileName?: string;
|
|
10
|
-
libraryUploads?: {
|
|
11
|
-
name: string;
|
|
12
|
-
content: string;
|
|
13
|
-
encoding: string;
|
|
14
|
-
}[];
|
|
15
|
-
showLanguage?: boolean;
|
|
16
|
-
language?: string;
|
|
17
|
-
languageLocked?: boolean;
|
|
18
|
-
}
|
|
19
|
-
declare const GenerationOptionsPanel: import("svelte").Component<Props, {}, "librariesEnabled" | "libraries" | "libraryGitUrl" | "libraryFileName" | "libraryUploads" | "language">;
|
|
20
|
-
type GenerationOptionsPanel = ReturnType<typeof GenerationOptionsPanel>;
|
|
21
|
-
export default GenerationOptionsPanel;
|