@aliens-syndicate/cli 0.1.9 → 0.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.es-ES.md +9 -11
- package/README.md +9 -11
- package/README.pt-BR.md +9 -11
- package/README.zh-CN.md +10 -12
- package/package.json +3 -2
- package/tools/build.mjs +15 -7
- package/tools/cli-shared.mjs +11 -2
- package/tools/cli.mjs +1 -0
- package/tools/create.mjs +14 -4
- package/tools/doctor.mjs +10 -5
- package/tools/playground/client.mjs +500 -0
- package/tools/playground/examples/computed/app.markup +3 -0
- package/tools/playground/examples/computed/cart.logic +22 -0
- package/tools/playground/examples/computed/cart.markup +10 -0
- package/tools/playground/examples/computed/cart.style +13 -0
- package/tools/playground/examples/counter/app.markup +3 -0
- package/tools/playground/examples/counter/counter.logic +19 -0
- package/tools/playground/examples/counter/counter.markup +7 -0
- package/tools/playground/examples/counter/counter.style +11 -0
- package/tools/playground/examples/effect-cleanup/app.markup +3 -0
- package/tools/playground/examples/effect-cleanup/stopwatch.logic +28 -0
- package/tools/playground/examples/effect-cleanup/stopwatch.markup +6 -0
- package/tools/playground/examples/effect-cleanup/stopwatch.style +11 -0
- package/tools/playground/examples/element-properties/app.markup +5 -0
- package/tools/playground/examples/element-properties/app.style +3 -0
- package/tools/playground/examples/element-properties/greeting.markup +4 -0
- package/tools/playground/examples/element-properties/greeting.style +7 -0
- package/tools/playground/examples/fix-the-error/app.markup +3 -0
- package/tools/playground/examples/fix-the-error/light.logic +10 -0
- package/tools/playground/examples/fix-the-error/light.markup +6 -0
- package/tools/playground/examples/fix-the-error/light.style +7 -0
- package/tools/playground/examples/hello-world/app.markup +4 -0
- package/tools/playground/examples/hello-world/app.style +7 -0
- package/tools/playground/examples/icons/app.markup +7 -0
- package/tools/playground/examples/icons/app.style +5 -0
- package/tools/playground/examples/list-for/app.markup +3 -0
- package/tools/playground/examples/list-for/planets.logic +15 -0
- package/tools/playground/examples/list-for/planets.markup +6 -0
- package/tools/playground/examples/list-for/planets.style +7 -0
- package/tools/playground/examples/ready-elements/app.markup +3 -0
- package/tools/playground/examples/ready-elements/app.style +3 -0
- package/tools/playground/examples/ready-elements/mission.logic +9 -0
- package/tools/playground/examples/ready-elements/mission.markup +7 -0
- package/tools/playground/examples/ready-elements/mission.style +5 -0
- package/tools/playground/examples/streams/app.markup +3 -0
- package/tools/playground/examples/streams/planet-search.logic +24 -0
- package/tools/playground/examples/streams/planet-search.markup +9 -0
- package/tools/playground/examples/streams/planet-search.style +9 -0
- package/tools/playground/examples/styles-modifiers/app.markup +5 -0
- package/tools/playground/examples/styles-modifiers/app.style +10 -0
- package/tools/playground/examples/watch/app.markup +3 -0
- package/tools/playground/examples/watch/volume.logic +20 -0
- package/tools/playground/examples/watch/volume.markup +9 -0
- package/tools/playground/examples/watch/volume.style +11 -0
- package/tools/playground.mjs +222 -0
- package/tools/project-layout.mjs +1 -1
- package/tools/runtime.mjs +16 -6
- package/tools/serve.mjs +15 -2
- package/tools/starter/app/pages/home/home.markup +19 -0
- package/tools/starter/app/pages/home/home.style +51 -0
- package/tools/starter/language/.en-US +10 -73
- package/tools/starter/language/.es-ES +10 -73
- package/tools/starter/language/.pt-BR +10 -73
- package/tools/starter/language/.zh-CN +10 -73
- package/tools/starter/styles/generic/ui-base.style +4 -5
- package/tools/test.mjs +20 -0
- package/tools/update.mjs +3 -3
- package/tools/utilities-docs.mjs +0 -5
- package/tools/watch.mjs +3 -2
- package/tools/starter/app/elements/atoms/kit-nav-link/kit-nav-link.markup +0 -2
- package/tools/starter/app/elements/molecules/kit-button-demo/kit-button-demo.logic +0 -14
- package/tools/starter/app/elements/molecules/kit-button-demo/kit-button-demo.markup +0 -8
- package/tools/starter/app/elements/molecules/kit-card-link/kit-card-link.markup +0 -4
- package/tools/starter/app/elements/organisms/kit-navigation/kit-navigation.markup +0 -19
- package/tools/starter/app/pages/kit-border/kit-border.markup +0 -38
- package/tools/starter/app/pages/kit-color/kit-color.markup +0 -58
- package/tools/starter/app/pages/kit-components/kit-components.markup +0 -95
- package/tools/starter/app/pages/kit-overview/kit-overview.markup +0 -25
- package/tools/starter/app/pages/kit-radius/kit-radius.markup +0 -34
- package/tools/starter/app/pages/kit-space/kit-space.markup +0 -61
- package/tools/starter/app/pages/kit-type/kit-type.markup +0 -48
- package/tools/starter/app/pages/style-guide/style-guide.markup +0 -10
- package/tools/starter/app/pages/style-guide/style-guide.style +0 -252
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
// The Playground page: edit Space files and see the result as you type. The runtime imports this module the first
|
|
2
|
+
// time the page opens. Each edit runs the real build (compiler.mjs, the browser copy of tools/build.mjs) on an
|
|
3
|
+
// in-memory project, shows the page in a sandboxed frame and reports build errors with file and line, and runtime
|
|
4
|
+
// logs and errors in the console below. Edits stay in this browser, per example, until Reset.
|
|
5
|
+
import { build } from './compiler.mjs';
|
|
6
|
+
import { files as disk, packageEntries } from './shims.mjs';
|
|
7
|
+
import spaceSyntax from './syntax.mjs';
|
|
8
|
+
|
|
9
|
+
const { analyzeSpace } = spaceSyntax;
|
|
10
|
+
const base = new URL('./', import.meta.url);
|
|
11
|
+
const root = document.querySelector('[data-playground]');
|
|
12
|
+
const view = root.closest('[data-view]');
|
|
13
|
+
const part = name => root.querySelector(`[data-playground-${name}]`);
|
|
14
|
+
const text = (key, values = {}) => Object.entries(values).reduce((message, [name, value]) => message.replaceAll(`{${name}}`, value),
|
|
15
|
+
view.querySelector(`[data-playground-text="${key}"]`)?.textContent ?? key);
|
|
16
|
+
const storageKey = 'aliens-playground';
|
|
17
|
+
const fileName = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*\.(?:markup|logic|style)$/;
|
|
18
|
+
const kinds = { markup: 'Markup', logic: 'Logic', style: 'Style' };
|
|
19
|
+
const pascal = name => name.split('-').map(word => word[0].toUpperCase() + word.slice(1)).join('');
|
|
20
|
+
|
|
21
|
+
// The look of what this module creates: the editor, the file tabs and the console lines. It reads the page tokens.
|
|
22
|
+
document.head.insertAdjacentHTML('beforeend', `<style>
|
|
23
|
+
.pg-editor { display: grid; grid-template-columns: auto 1fr; min-height: 100%; font: 0.82rem/1.6 var(--mono); tab-size: 3; }
|
|
24
|
+
.pg-gutter { position: sticky; left: 0; z-index: 1; padding: 12px 10px 12px 12px; text-align: right; color: var(--muted); background: var(--page); border-right: 1px solid var(--line); user-select: none; }
|
|
25
|
+
.pg-gutter span { display: block; }
|
|
26
|
+
.pg-gutter .pg-error-number { color: var(--color-danger); font-weight: 700; }
|
|
27
|
+
.pg-code { position: relative; min-width: 0; }
|
|
28
|
+
.pg-code pre, .pg-code textarea { margin: 0; padding: 12px 16px; font: inherit; letter-spacing: normal; white-space: pre; tab-size: 3; border: 0; }
|
|
29
|
+
.pg-code pre { min-height: 100%; pointer-events: none; color: var(--ink); }
|
|
30
|
+
.pg-code textarea { position: absolute; inset: 0; width: 100%; height: 100%; resize: none; overflow: hidden; color: transparent; background: transparent; caret-color: var(--ink); outline: none; }
|
|
31
|
+
.pg-code textarea::selection { background: color-mix(in srgb, var(--accent) 28%, transparent); color: transparent; }
|
|
32
|
+
.pg-error-line { position: absolute; left: 0; right: 0; height: 1.6em; background: color-mix(in srgb, var(--color-danger) 14%, transparent); border-left: 3px solid var(--color-danger); pointer-events: none; }
|
|
33
|
+
.pg-tab { display: inline-flex; align-items: center; gap: 6px; min-height: 36px; padding: 4px 10px; border: 0; border-radius: 6px 6px 0 0; background: transparent; color: var(--muted); font: 0.78rem var(--mono); white-space: nowrap; cursor: pointer; }
|
|
34
|
+
.pg-tab:hover { color: var(--ink); }
|
|
35
|
+
.pg-tab[aria-selected="true"] { background: var(--page); color: var(--ink); box-shadow: inset 0 -2px 0 var(--accent); }
|
|
36
|
+
.pg-tab .pg-edited { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); }
|
|
37
|
+
.pg-tab .pg-close { display: inline-grid; place-items: center; width: 16px; height: 16px; border-radius: 4px; color: var(--muted); }
|
|
38
|
+
.pg-tab .pg-close:hover { background: var(--surface); color: var(--color-danger); }
|
|
39
|
+
.pg-tab.pg-has-error { color: var(--color-danger); }
|
|
40
|
+
.pg-frame { width: 100%; height: 100%; border: 0; background: var(--page); }
|
|
41
|
+
.pg-line { display: grid; grid-template-columns: 4.2em 1fr; gap: 8px; padding: 4px 12px; border-bottom: 1px solid var(--line); white-space: pre-wrap; word-break: break-word; }
|
|
42
|
+
.pg-line time { color: var(--muted); }
|
|
43
|
+
.pg-line.pg-warn { background: var(--color-warning-bg); color: var(--color-warning); }
|
|
44
|
+
.pg-line.pg-error { background: var(--color-danger-bg); color: var(--color-danger); }
|
|
45
|
+
.pg-loading { display: grid; place-items: center; height: 100%; color: var(--muted); font-size: 0.85rem; }
|
|
46
|
+
</style>`);
|
|
47
|
+
|
|
48
|
+
const [{ versions, files: preload }, examples] = await Promise.all([
|
|
49
|
+
fetch(new URL('files.json', base)).then(response => response.json()),
|
|
50
|
+
fetch(new URL('examples.json', base)).then(response => response.json())
|
|
51
|
+
]);
|
|
52
|
+
const baseFiles = new Map(Object.entries(preload).map(([key, value]) => [key.startsWith('/') ? key : new URL(key, base).pathname, value]));
|
|
53
|
+
for (const short of Object.keys(versions)) packageEntries.set(`@aliens-syndicate/${short}`, new URL(`packages/${short}/index.mjs`, base).pathname);
|
|
54
|
+
const config = `project:
|
|
55
|
+
name: Playground
|
|
56
|
+
version: 0.1.0
|
|
57
|
+
|
|
58
|
+
files:
|
|
59
|
+
entry: project/app/app.logic
|
|
60
|
+
assets: project/assets
|
|
61
|
+
styles: project/styles/main.style
|
|
62
|
+
environment: project/.env
|
|
63
|
+
agent: ai/.map
|
|
64
|
+
|
|
65
|
+
build:
|
|
66
|
+
target: web
|
|
67
|
+
environment: production
|
|
68
|
+
|
|
69
|
+
console: false
|
|
70
|
+
|
|
71
|
+
dependencies:
|
|
72
|
+
icons:
|
|
73
|
+
@aliens-syndicate/icons/stroke-rounded: ${versions.icons}
|
|
74
|
+
elements:
|
|
75
|
+
@aliens-syndicate/elements: ${versions.elements}
|
|
76
|
+
reactive:
|
|
77
|
+
@aliens-syndicate/reactive: ${versions.reactive}
|
|
78
|
+
|
|
79
|
+
devDependencies:
|
|
80
|
+
utilities:
|
|
81
|
+
@aliens-syndicate/utilities: ${versions.utilities}
|
|
82
|
+
|
|
83
|
+
language:
|
|
84
|
+
directory: project/language
|
|
85
|
+
default: en-US
|
|
86
|
+
`;
|
|
87
|
+
const language = 'site:\n title: Aliens Playground\n\nhome:\n description: A page built in the Aliens Playground.\n';
|
|
88
|
+
|
|
89
|
+
// State: the example, its files in tab order and the open file. Edits are saved per example.
|
|
90
|
+
let saved = {};
|
|
91
|
+
try { saved = JSON.parse(localStorage.getItem(storageKey)) ?? {}; } catch { saved = {}; }
|
|
92
|
+
const exampleButtons = [...root.querySelectorAll('[data-playground-example]')];
|
|
93
|
+
// #playground-counter opens the counter example; otherwise the last example opened in this browser comes back.
|
|
94
|
+
const linkedExample = () => {
|
|
95
|
+
const id = /^#playground-([a-z0-9-]+)$/.exec(location.hash)?.[1];
|
|
96
|
+
return id && examples[id] ? id : null;
|
|
97
|
+
};
|
|
98
|
+
let example = linkedExample() ?? (examples[saved.example] ? saved.example : exampleButtons[0]?.dataset.playgroundExample);
|
|
99
|
+
let files = new Map();
|
|
100
|
+
let active = null;
|
|
101
|
+
let outputs = null;
|
|
102
|
+
let failure = null;
|
|
103
|
+
let view_ = 'result';
|
|
104
|
+
let consoleFilter = 'all';
|
|
105
|
+
const consoleLines = [];
|
|
106
|
+
|
|
107
|
+
function persist() {
|
|
108
|
+
const original = examples[example];
|
|
109
|
+
const edited = [...files].some(([name, source]) => original[name] !== source) || Object.keys(original).some(name => !files.has(name));
|
|
110
|
+
saved.edits = saved.edits ?? {};
|
|
111
|
+
if (edited) saved.edits[example] = Object.fromEntries(files);
|
|
112
|
+
else delete saved.edits[example];
|
|
113
|
+
saved.example = example;
|
|
114
|
+
try { localStorage.setItem(storageKey, JSON.stringify(saved)); } catch { /* Storage is optional. */ }
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Opens a file in the order app.markup, then the other Elements, each as markup, logic, style.
|
|
118
|
+
function orderedFiles(entries) {
|
|
119
|
+
const rank = name => [name.startsWith('app.') ? 0 : 1, name.replace(/\.\w+$/, ''), ['markup', 'logic', 'style'].indexOf(name.split('.').pop())];
|
|
120
|
+
return new Map(entries.sort(([left], [right]) => {
|
|
121
|
+
const [a, b] = [rank(left), rank(right)];
|
|
122
|
+
return a[0] - b[0] || a[1].localeCompare(b[1]) || a[2] - b[2];
|
|
123
|
+
}));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function loadExample(id, { fresh = false } = {}) {
|
|
127
|
+
example = id;
|
|
128
|
+
const source = !fresh && saved.edits?.[id] ? saved.edits[id] : examples[id];
|
|
129
|
+
files = orderedFiles(Object.entries(source));
|
|
130
|
+
active = [...files.keys()].find(name => !name.startsWith('app.')) ?? [...files.keys()][0];
|
|
131
|
+
for (const button of exampleButtons) button.setAttribute('aria-current', String(button.dataset.playgroundExample === id));
|
|
132
|
+
showTitle();
|
|
133
|
+
persist();
|
|
134
|
+
renderTabs();
|
|
135
|
+
showFile();
|
|
136
|
+
compile();
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function showTitle() {
|
|
140
|
+
const button = exampleButtons.find(entry => entry.dataset.playgroundExample === example);
|
|
141
|
+
part('title').textContent = button?.querySelector('strong')?.textContent ?? example;
|
|
142
|
+
part('summary').textContent = button?.querySelector('span')?.textContent ?? '';
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// ---- Editor: a transparent textarea over the highlighted code, with line numbers and the error line.
|
|
146
|
+
const editorHost = part('editor');
|
|
147
|
+
editorHost.innerHTML = `<div class="pg-editor"><div class="pg-gutter" aria-hidden="true"></div><div class="pg-code"><pre aria-hidden="true"><code></code></pre><textarea spellcheck="false" autocapitalize="off" autocomplete="off" autocorrect="off" wrap="off"></textarea></div></div>`;
|
|
148
|
+
const gutter = editorHost.querySelector('.pg-gutter');
|
|
149
|
+
const highlight = editorHost.querySelector('code');
|
|
150
|
+
const input = editorHost.querySelector('textarea');
|
|
151
|
+
const codeBox = editorHost.querySelector('.pg-code');
|
|
152
|
+
const escape = value => value.replace(/[&<>"]/g, character => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[character]);
|
|
153
|
+
|
|
154
|
+
function paint() {
|
|
155
|
+
const source = input.value;
|
|
156
|
+
const extension = '.' + (active?.split('.').pop() ?? 'markup');
|
|
157
|
+
let tokens = [];
|
|
158
|
+
try { tokens = analyzeSpace(source, extension).tokens; } catch { tokens = []; }
|
|
159
|
+
let index = 0;
|
|
160
|
+
highlight.innerHTML = source.split('\n').map((line, number) => {
|
|
161
|
+
let output = '';
|
|
162
|
+
let start = 0;
|
|
163
|
+
while (tokens[index]?.line < number) index++;
|
|
164
|
+
while (tokens[index]?.line === number) {
|
|
165
|
+
const token = tokens[index++];
|
|
166
|
+
output += escape(line.slice(start, token.start)) + `<span class="space-${token.role}">${escape(line.slice(token.start, token.start + token.length))}</span>`;
|
|
167
|
+
start = token.start + token.length;
|
|
168
|
+
}
|
|
169
|
+
return output + escape(line.slice(start));
|
|
170
|
+
}).join('\n') + '\n ';
|
|
171
|
+
const errorLine = failure?.file === active ? failure.line : null;
|
|
172
|
+
gutter.innerHTML = source.split('\n').map((_, number) => `<span${number + 1 === errorLine ? ' class="pg-error-number"' : ''}>${number + 1}</span>`).join('');
|
|
173
|
+
codeBox.querySelector('.pg-error-line')?.remove();
|
|
174
|
+
if (errorLine) codeBox.insertAdjacentHTML('afterbegin', `<div class="pg-error-line" style="top: calc(12px + ${errorLine - 1} * 1.6em)"></div>`);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function showFile() {
|
|
178
|
+
input.value = files.get(active) ?? '';
|
|
179
|
+
input.setAttribute('aria-label', active ?? '');
|
|
180
|
+
paint();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Typing keeps the native undo: insertText goes through the browser's edit history.
|
|
184
|
+
function insert(textToInsert) {
|
|
185
|
+
if (!document.execCommand('insertText', false, textToInsert)) input.setRangeText(textToInsert, input.selectionStart, input.selectionEnd, 'end');
|
|
186
|
+
}
|
|
187
|
+
input.addEventListener('keydown', event => {
|
|
188
|
+
const { selectionStart: start, selectionEnd: end, value } = input;
|
|
189
|
+
if (event.key === 'Tab') {
|
|
190
|
+
event.preventDefault();
|
|
191
|
+
const lineStart = value.lastIndexOf('\n', start - 1) + 1;
|
|
192
|
+
if (start === end && !event.shiftKey) return insert(' ');
|
|
193
|
+
// With a selection, or with Shift, every selected line moves three spaces in or out.
|
|
194
|
+
const lineEnd = value.indexOf('\n', end - 1) === -1 ? value.length : value.indexOf('\n', end - (end > start && value[end - 1] === '\n' ? 1 : 0));
|
|
195
|
+
const block = value.slice(lineStart, lineEnd === -1 ? value.length : lineEnd);
|
|
196
|
+
const moved = block.split('\n').map(line => event.shiftKey ? line.replace(/^ {1,3}/, '') : ' ' + line).join('\n');
|
|
197
|
+
input.setSelectionRange(lineStart, lineStart + block.length);
|
|
198
|
+
insert(moved);
|
|
199
|
+
input.setSelectionRange(lineStart, lineStart + moved.length);
|
|
200
|
+
} else if (event.key === 'Enter' && !event.metaKey && !event.ctrlKey) {
|
|
201
|
+
// A new line keeps the indentation of the current one.
|
|
202
|
+
event.preventDefault();
|
|
203
|
+
const lineStart = value.lastIndexOf('\n', start - 1) + 1;
|
|
204
|
+
insert('\n' + /^ */.exec(value.slice(lineStart, start))[0]);
|
|
205
|
+
} else if (event.key === 'Enter') {
|
|
206
|
+
event.preventDefault();
|
|
207
|
+
compile();
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
let timer;
|
|
211
|
+
input.addEventListener('input', () => {
|
|
212
|
+
files.set(active, input.value);
|
|
213
|
+
paint();
|
|
214
|
+
persist();
|
|
215
|
+
renderTabs();
|
|
216
|
+
setStatus('busy', text('compiling'));
|
|
217
|
+
clearTimeout(timer);
|
|
218
|
+
timer = setTimeout(compile, 300);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
// ---- File tabs: open, add, rename (double click) and remove files.
|
|
222
|
+
const tabs = part('tabs');
|
|
223
|
+
function renderTabs() {
|
|
224
|
+
const original = examples[example];
|
|
225
|
+
tabs.innerHTML = '';
|
|
226
|
+
for (const name of files.keys()) {
|
|
227
|
+
const tab = document.createElement('button');
|
|
228
|
+
tab.type = 'button';
|
|
229
|
+
tab.className = 'pg-tab' + (failure?.file === name ? ' pg-has-error' : '');
|
|
230
|
+
tab.setAttribute('role', 'tab');
|
|
231
|
+
tab.setAttribute('aria-selected', String(name === active));
|
|
232
|
+
tab.title = text('renameFile');
|
|
233
|
+
tab.innerHTML = `<span>${escape(name)}</span>${original[name] !== files.get(name) ? '<span class="pg-edited" aria-hidden="true"></span>' : ''}` +
|
|
234
|
+
(name === 'app.markup' ? '' : `<span class="pg-close" role="button" aria-label="${escape(text('deleteFile', { file: name }))}">×</span>`);
|
|
235
|
+
tab.addEventListener('click', event => {
|
|
236
|
+
if (event.target.closest('.pg-close')) {
|
|
237
|
+
if (!confirm(text('deleteFile', { file: name }))) return;
|
|
238
|
+
files.delete(name);
|
|
239
|
+
if (active === name) active = [...files.keys()][0];
|
|
240
|
+
persist();
|
|
241
|
+
renderTabs();
|
|
242
|
+
showFile();
|
|
243
|
+
return compile();
|
|
244
|
+
}
|
|
245
|
+
active = name;
|
|
246
|
+
renderTabs();
|
|
247
|
+
showFile();
|
|
248
|
+
input.focus();
|
|
249
|
+
});
|
|
250
|
+
tab.addEventListener('dblclick', () => {
|
|
251
|
+
if (name === 'app.markup') return;
|
|
252
|
+
const next = askName(name);
|
|
253
|
+
if (!next || next === name) return;
|
|
254
|
+
files = orderedFiles([...files].map(([key, value]) => key === name ? [next, value] : [key, value]));
|
|
255
|
+
active = next;
|
|
256
|
+
persist();
|
|
257
|
+
renderTabs();
|
|
258
|
+
showFile();
|
|
259
|
+
compile();
|
|
260
|
+
});
|
|
261
|
+
tabs.append(tab);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function askName(current = '') {
|
|
266
|
+
const answer = prompt(text('newFile'), current)?.trim().toLowerCase();
|
|
267
|
+
if (!answer) return null;
|
|
268
|
+
if (!fileName.test(answer)) {
|
|
269
|
+
alert(text('badName'));
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
if (answer !== current && files.has(answer)) {
|
|
273
|
+
alert(text('exists', { file: answer }));
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
return answer;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
part('add').addEventListener('click', () => {
|
|
280
|
+
const name = askName();
|
|
281
|
+
if (!name) return;
|
|
282
|
+
const [element, kind] = name.split('.');
|
|
283
|
+
const header = `${kinds[kind]} ${pascal(element)}\n`;
|
|
284
|
+
const starter = { markup: `${header} section id="${element}"\n p={${pascal(element)}}\n`, logic: header, style: `${header} ${element}\n p-4\n` }[kind];
|
|
285
|
+
files = orderedFiles([...files, [name, starter]]);
|
|
286
|
+
active = name;
|
|
287
|
+
persist();
|
|
288
|
+
renderTabs();
|
|
289
|
+
showFile();
|
|
290
|
+
compile();
|
|
291
|
+
input.focus();
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
// ---- Compiling: the files become a project in memory, the build runs and writes dist/ there.
|
|
295
|
+
const status = part('status');
|
|
296
|
+
function setStatus(state, message) {
|
|
297
|
+
status.dataset.state = state;
|
|
298
|
+
status.textContent = message;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function projectPath(name) {
|
|
302
|
+
const [element, kind] = name.split('.');
|
|
303
|
+
return element === 'app' ? `/project/app/app.${kind}` : `/project/app/elements/${element}/${element}.${kind}`;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// A build error starts with path:line. The path of a Playground file becomes its tab name.
|
|
307
|
+
function locate(message) {
|
|
308
|
+
const match = /^\/?project\/app\/(?:elements\/[a-z0-9-]+\/)?([a-z0-9-]+\.(?:markup|logic|style)):(\d+):\s*/.exec(message);
|
|
309
|
+
if (!match || !files.has(match[1])) return { file: null, line: null, message };
|
|
310
|
+
return { file: match[1], line: Number(match[2]), message: message.slice(match[0].length) };
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
let generation = 0;
|
|
314
|
+
async function compile() {
|
|
315
|
+
clearTimeout(timer);
|
|
316
|
+
const run = ++generation;
|
|
317
|
+
disk.clear();
|
|
318
|
+
for (const [path, source] of baseFiles) disk.set(path, source);
|
|
319
|
+
disk.set('/.config', config);
|
|
320
|
+
disk.set('/project/language/.en-US', language);
|
|
321
|
+
disk.set('/project/app/app.logic', 'Logic App\n');
|
|
322
|
+
disk.set('/project/assets/.keep', '');
|
|
323
|
+
disk.set('/ai/.map', '');
|
|
324
|
+
disk.set('/project/.env', '# The Playground has no environment values.\n');
|
|
325
|
+
for (const [name, source] of files) disk.set(projectPath(name), source);
|
|
326
|
+
const started = performance.now();
|
|
327
|
+
try {
|
|
328
|
+
if (!files.has('app.markup')) throw new Error('project/app/app.markup:1: the Playground needs app.markup, the page it shows.');
|
|
329
|
+
await build();
|
|
330
|
+
if (run !== generation) return;
|
|
331
|
+
failure = null;
|
|
332
|
+
outputs = { html: disk.get('/dist/index.html'), css: disk.get('/dist/main.css'), js: disk.get('/dist/app.js') };
|
|
333
|
+
setStatus('ok', text('compiled', { ms: Math.max(1, Math.round(performance.now() - started)) }));
|
|
334
|
+
clearConsole();
|
|
335
|
+
renderResult();
|
|
336
|
+
} catch (error) {
|
|
337
|
+
if (run !== generation) return;
|
|
338
|
+
failure = locate(String(error?.message ?? error));
|
|
339
|
+
setStatus('error', text('failed'));
|
|
340
|
+
}
|
|
341
|
+
showFailure();
|
|
342
|
+
renderTabs();
|
|
343
|
+
paint();
|
|
344
|
+
showView();
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// ---- Result: the page in a sandboxed frame, with a bridge that sends its console to the panel below.
|
|
348
|
+
const resultHost = part('result');
|
|
349
|
+
resultHost.innerHTML = `<div class="pg-loading">${escape(text('loading'))}</div>`;
|
|
350
|
+
let frame = null;
|
|
351
|
+
function renderResult() {
|
|
352
|
+
if (!outputs) return;
|
|
353
|
+
const theme = document.documentElement.dataset.theme === 'dark' ? 'dark' : 'light';
|
|
354
|
+
const bridge = `<script>(() => {
|
|
355
|
+
const format = value => value instanceof Error ? (value.stack || value.message) : typeof value === 'string' ? value : (() => { try { return JSON.stringify(value) ?? String(value); } catch { return String(value); } })();
|
|
356
|
+
const send = (level, values) => parent.postMessage({ aliensPlayground: true, level, text: values.map(format).join(' ') }, '*');
|
|
357
|
+
for (const level of ['log', 'info', 'debug', 'warn', 'error']) {
|
|
358
|
+
const original = console[level].bind(console);
|
|
359
|
+
console[level] = (...values) => { send(level === 'warn' || level === 'error' ? level : 'log', values); original(...values); };
|
|
360
|
+
}
|
|
361
|
+
addEventListener('error', event => send('error', [event.error ?? event.message]));
|
|
362
|
+
addEventListener('unhandledrejection', event => send('error', [event.reason]));
|
|
363
|
+
try { Object.defineProperty(window, 'localStorage', { value: { getItem: key => key === 'aliens-docs-theme' ? ${JSON.stringify(theme)} : null, setItem() {}, removeItem() {} } }); } catch {}
|
|
364
|
+
})();<\/script>`;
|
|
365
|
+
const script = outputs.js.replace(/<\/script/gi, '<\\/script');
|
|
366
|
+
const page = outputs.html
|
|
367
|
+
.replace('<head>', `<head>${bridge}`)
|
|
368
|
+
.replace('<link rel="stylesheet" href="./main.css">', () => `<style>${outputs.css}</style>`)
|
|
369
|
+
.replace('<script defer src="./app.js"></script>', '')
|
|
370
|
+
.replace('</body>', () => `<script>${script}<\/script></body>`);
|
|
371
|
+
const next = document.createElement('iframe');
|
|
372
|
+
next.className = 'pg-frame';
|
|
373
|
+
next.title = part('title').textContent;
|
|
374
|
+
next.setAttribute('sandbox', 'allow-scripts allow-modals allow-forms');
|
|
375
|
+
next.srcdoc = page;
|
|
376
|
+
resultHost.replaceChildren(next);
|
|
377
|
+
frame = next;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
window.addEventListener('message', event => {
|
|
381
|
+
if (!frame || event.source !== frame.contentWindow || !event.data?.aliensPlayground) return;
|
|
382
|
+
addConsole(event.data.level, event.data.text);
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
// ---- Error panel: the message, where it is and a button that opens that line.
|
|
386
|
+
const errorPanel = part('error');
|
|
387
|
+
function showFailure() {
|
|
388
|
+
errorPanel.hidden = !failure;
|
|
389
|
+
if (!failure) return;
|
|
390
|
+
part('error-where').textContent = failure.file ? `${failure.file} · ${text('line', { line: failure.line })}` : '';
|
|
391
|
+
part('error-where').hidden = !failure.file;
|
|
392
|
+
part('error-message').textContent = failure.message;
|
|
393
|
+
part('error-jump').hidden = !failure.file;
|
|
394
|
+
}
|
|
395
|
+
part('error-jump').addEventListener('click', () => jumpTo(failure?.file, failure?.line));
|
|
396
|
+
function jumpTo(file, line) {
|
|
397
|
+
if (!file || !files.has(file)) return;
|
|
398
|
+
active = file;
|
|
399
|
+
renderTabs();
|
|
400
|
+
showFile();
|
|
401
|
+
const lines = input.value.split('\n');
|
|
402
|
+
const offset = lines.slice(0, line - 1).reduce((total, current) => total + current.length + 1, 0);
|
|
403
|
+
input.focus();
|
|
404
|
+
input.setSelectionRange(offset, offset + (lines[line - 1]?.length ?? 0));
|
|
405
|
+
const top = (line - 4) * parseFloat(getComputedStyle(input).lineHeight);
|
|
406
|
+
editorHost.scrollTo({ top: Math.max(0, top), behavior: 'smooth' });
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// ---- Output tabs: the result, or the HTML, CSS and JavaScript the build wrote.
|
|
410
|
+
const compiled = part('compiled');
|
|
411
|
+
const viewButtons = [...root.querySelectorAll('[data-playground-view]')];
|
|
412
|
+
for (const button of viewButtons) button.addEventListener('click', () => { view_ = button.dataset.playgroundView; showView(); });
|
|
413
|
+
function userCss(css) {
|
|
414
|
+
// The starter styles come first; the files of the example are at the end, from their first selector on.
|
|
415
|
+
const names = [...files.keys()].map(name => name.split('.')[0]).filter(name => name !== 'app');
|
|
416
|
+
const marks = [...names, 'app'].flatMap(name => [`\n.${name}`, `\n#${name}`]).map(mark => css.indexOf(mark)).filter(index => index >= 0);
|
|
417
|
+
return marks.length ? css.slice(Math.min(...marks) + 1) : css;
|
|
418
|
+
}
|
|
419
|
+
function showView() {
|
|
420
|
+
for (const button of viewButtons) button.setAttribute('aria-selected', String(button.dataset.playgroundView === view_));
|
|
421
|
+
const code = view_ !== 'result';
|
|
422
|
+
resultHost.hidden = code;
|
|
423
|
+
compiled.hidden = !code;
|
|
424
|
+
if (!code || !outputs) return;
|
|
425
|
+
const body = /<body>([\s\S]*)<\/body>/.exec(outputs.html)?.[1] ?? outputs.html;
|
|
426
|
+
compiled.textContent = view_ === 'html' ? body.replace(/></g, '>\n<').trim()
|
|
427
|
+
: view_ === 'css' ? userCss(outputs.css).trim()
|
|
428
|
+
: (outputs.js.split('\nconst languages')[0] ?? '').trim();
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
// ---- Console: what the page logs, filtered by level.
|
|
432
|
+
const consoleList = part('console-list');
|
|
433
|
+
const consoleEmpty = part('console-empty');
|
|
434
|
+
const consoleCount = part('console-count');
|
|
435
|
+
function addConsole(level, message) {
|
|
436
|
+
consoleLines.push({ level, message, time: new Date() });
|
|
437
|
+
renderConsole();
|
|
438
|
+
}
|
|
439
|
+
function clearConsole() {
|
|
440
|
+
consoleLines.length = 0;
|
|
441
|
+
renderConsole();
|
|
442
|
+
}
|
|
443
|
+
function renderConsole() {
|
|
444
|
+
const shown = consoleLines.filter(line => consoleFilter === 'all' || line.level === consoleFilter);
|
|
445
|
+
consoleList.innerHTML = shown.map(({ level, message, time }) =>
|
|
446
|
+
`<li class="pg-line pg-${level}"><time>${time.toTimeString().slice(0, 8)}</time><span>${escape(message)}</span></li>`).join('');
|
|
447
|
+
consoleEmpty.hidden = shown.length > 0;
|
|
448
|
+
consoleCount.textContent = String(consoleLines.length);
|
|
449
|
+
consoleList.scrollTop = consoleList.scrollHeight;
|
|
450
|
+
}
|
|
451
|
+
for (const button of root.querySelectorAll('[data-playground-console-filter]')) {
|
|
452
|
+
button.addEventListener('click', () => {
|
|
453
|
+
consoleFilter = button.dataset.playgroundConsoleFilter;
|
|
454
|
+
for (const other of root.querySelectorAll('[data-playground-console-filter]')) other.setAttribute('aria-pressed', String(other === button));
|
|
455
|
+
renderConsole();
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
part('console-clear').addEventListener('click', clearConsole);
|
|
459
|
+
|
|
460
|
+
// ---- Examples menu, Copy and Reset.
|
|
461
|
+
const picker = part('picker');
|
|
462
|
+
const panel = part('examples');
|
|
463
|
+
function openExamples(open) {
|
|
464
|
+
panel.hidden = !open;
|
|
465
|
+
picker.setAttribute('aria-expanded', String(open));
|
|
466
|
+
if (open) panel.querySelector('input')?.focus();
|
|
467
|
+
}
|
|
468
|
+
picker.addEventListener('click', () => openExamples(panel.hidden));
|
|
469
|
+
for (const button of exampleButtons) {
|
|
470
|
+
button.addEventListener('click', () => {
|
|
471
|
+
openExamples(false);
|
|
472
|
+
loadExample(button.dataset.playgroundExample);
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
document.addEventListener('click', event => {
|
|
476
|
+
if (!panel.hidden && !panel.contains(event.target) && !picker.contains(event.target)) openExamples(false);
|
|
477
|
+
});
|
|
478
|
+
document.addEventListener('keydown', event => {
|
|
479
|
+
if (event.key === 'Escape' && !panel.hidden) {
|
|
480
|
+
openExamples(false);
|
|
481
|
+
picker.focus();
|
|
482
|
+
}
|
|
483
|
+
});
|
|
484
|
+
part('reset').addEventListener('click', () => loadExample(example, { fresh: true }));
|
|
485
|
+
part('copy').addEventListener('click', async () => {
|
|
486
|
+
try {
|
|
487
|
+
await navigator.clipboard.writeText(files.get(active) ?? '');
|
|
488
|
+
setStatus('ok', text('copied', { file: active }));
|
|
489
|
+
} catch { /* The clipboard is optional. */ }
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
// The frame follows the theme of the page, and the titles follow its language.
|
|
493
|
+
new MutationObserver(() => { renderResult(); showTitle(); }).observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme', 'lang'] });
|
|
494
|
+
|
|
495
|
+
window.addEventListener('hashchange', () => {
|
|
496
|
+
const id = linkedExample();
|
|
497
|
+
if (id && id !== example) loadExample(id);
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
loadExample(example);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Logic Cart
|
|
2
|
+
# total and freeShipping derive from quantity. The actions only change quantity.
|
|
3
|
+
signal quantity: number = 1
|
|
4
|
+
|
|
5
|
+
computed total: string = (this.quantity * 24.9).toFixed(2)
|
|
6
|
+
|
|
7
|
+
computed freeShipping: boolean = this.quantity * 24.9 >= 100
|
|
8
|
+
|
|
9
|
+
bind:
|
|
10
|
+
cart-free:
|
|
11
|
+
show: this.freeShipping
|
|
12
|
+
|
|
13
|
+
actions:
|
|
14
|
+
remove:
|
|
15
|
+
returns: void
|
|
16
|
+
body:
|
|
17
|
+
if (this.quantity > 1) this.quantity -= 1
|
|
18
|
+
|
|
19
|
+
add:
|
|
20
|
+
returns: void
|
|
21
|
+
body:
|
|
22
|
+
this.quantity += 1
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Markup Cart
|
|
2
|
+
section id="cart"
|
|
3
|
+
div id="cart-row"
|
|
4
|
+
button text="−" variant="cart-step" action="remove" label="Remove one"
|
|
5
|
+
output={1} id="cart-quantity"
|
|
6
|
+
button text="+" variant="cart-step" action="add" label="Add one"
|
|
7
|
+
p id="cart-summary"
|
|
8
|
+
span text="Total"
|
|
9
|
+
output={24.90} id="cart-total"
|
|
10
|
+
p={Free shipping} id="cart-free" hidden
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Style Cart
|
|
2
|
+
cart
|
|
3
|
+
grid justify-items-center gap-2 p-8
|
|
4
|
+
cart-row
|
|
5
|
+
flex items-center gap-4
|
|
6
|
+
cart-step
|
|
7
|
+
w-11 h-11 rounded-lg border border-outline bg-surface text-xl
|
|
8
|
+
cart-quantity
|
|
9
|
+
text-4xl font-bold text-brand
|
|
10
|
+
cart-summary
|
|
11
|
+
flex gap-2 font-semibold
|
|
12
|
+
cart-free
|
|
13
|
+
py-1 px-3 rounded-full bg-success-bg text-success text-sm font-semibold
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Logic Counter
|
|
2
|
+
# The id counter-value shows the value signal; no bind: needed.
|
|
3
|
+
signal value: number = 0
|
|
4
|
+
|
|
5
|
+
actions:
|
|
6
|
+
decrement:
|
|
7
|
+
returns: void
|
|
8
|
+
body:
|
|
9
|
+
this.value -= 1
|
|
10
|
+
|
|
11
|
+
increment:
|
|
12
|
+
returns: void
|
|
13
|
+
body:
|
|
14
|
+
this.value += 1
|
|
15
|
+
|
|
16
|
+
reset:
|
|
17
|
+
returns: void
|
|
18
|
+
body:
|
|
19
|
+
this.value = 0
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Markup Counter
|
|
2
|
+
section id="counter"
|
|
3
|
+
output={0} id="counter-value"
|
|
4
|
+
div id="counter-controls"
|
|
5
|
+
button text="−" variant="counter-button" action="decrement" label="Decrease"
|
|
6
|
+
button text="+" variant="counter-button" action="increment" label="Increase"
|
|
7
|
+
button text="Reset" variant="counter-reset" action="reset"
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Style Counter
|
|
2
|
+
counter
|
|
3
|
+
grid justify-items-center gap-3 p-8
|
|
4
|
+
counter-value
|
|
5
|
+
text-5xl font-bold text-brand
|
|
6
|
+
counter-controls
|
|
7
|
+
flex gap-2
|
|
8
|
+
counter-button
|
|
9
|
+
w-11 h-11 rounded-lg border border-outline bg-surface text-xl
|
|
10
|
+
counter-reset
|
|
11
|
+
text-brand underline bg-transparent border-0
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
Logic Stopwatch
|
|
2
|
+
# The effect starts an interval while running is true; cleanup stops it before the next run.
|
|
3
|
+
signal running: boolean = false
|
|
4
|
+
signal tenths: number = 0
|
|
5
|
+
|
|
6
|
+
computed time: string = (this.tenths / 10).toFixed(1) + "s"
|
|
7
|
+
|
|
8
|
+
effect tick:
|
|
9
|
+
body:
|
|
10
|
+
if (this.running) this.timer = setInterval(() => { this.tenths += 1 }, 100)
|
|
11
|
+
cleanup:
|
|
12
|
+
clearInterval(this.timer)
|
|
13
|
+
|
|
14
|
+
bind:
|
|
15
|
+
stopwatch-toggle:
|
|
16
|
+
pressed: this.running
|
|
17
|
+
|
|
18
|
+
actions:
|
|
19
|
+
toggle:
|
|
20
|
+
returns: void
|
|
21
|
+
body:
|
|
22
|
+
this.running = !this.running
|
|
23
|
+
|
|
24
|
+
reset:
|
|
25
|
+
returns: void
|
|
26
|
+
body:
|
|
27
|
+
this.running = false
|
|
28
|
+
this.tenths = 0
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Style Stopwatch
|
|
2
|
+
stopwatch
|
|
3
|
+
grid justify-items-center gap-3 p-8
|
|
4
|
+
stopwatch-time
|
|
5
|
+
text-5xl font-bold text-brand
|
|
6
|
+
stopwatch-controls
|
|
7
|
+
flex gap-2
|
|
8
|
+
stopwatch-button
|
|
9
|
+
py-2 px-4 rounded-lg border border-outline bg-surface font-semibold
|
|
10
|
+
stopwatch-button[aria-pressed="true"]
|
|
11
|
+
bg-brand text-on-brand
|