rob-revisited/ide/ui/js/wizard-preview.js

95 lines
4.7 KiB
JavaScript

// What the generator forms (fixtures.js, test-wizard.js) share: the file diff pane (Monaco's inline diff, a button per file touched), the
// install notice for the packages a form needs (composer require --dev through the Composer window's backend, output shown live) and the
// stylesheet. The PHP side of the changes is wizard.apply (ide/src/Wizard/WizardCommands.php: written only if the files did not change).
import { backend } from './backend.js';
import { el, clear, notify, t } from './ui.js';
import { monaco } from './editor.js';
import { requireWithComposer } from './api-install.js';
import { installPackages } from './wizard-install.js';
const cssHref = new URL('../css/wizard-gen.css', import.meta.url).href;
if (![...document.querySelectorAll('link[rel=stylesheet]')].some((l) => l.href === cssHref)) document.head.append(el('link', { rel: 'stylesheet', href: cssHref }));
/** The diff pane: `.node` to place, `.show(changes, error)` to fill, `.dispose()` when the dialog closes. */
export function diffPane() {
const node = el('div.wg-diff');
const files = el('div.wg-files');
const code = el('div.wg-code');
node.append(files, code);
let diff = null;
let models = [];
let changes = [];
let current = 0;
const draw = () => {
clear(files);
if (!changes.length) return;
for (const [i, c] of changes.entries()) {
files.append(el('button.wg-file' + (i === current ? '.on' : ''), { onclick: () => { current = i; draw(); } }, c.before === null ? t('{file} (new)', { file: c.file }) : c.file));
}
const main = changes[current];
if (!monaco) { clear(code); code.append(el('pre.wg-pre', {}, main.after)); return; }
if (!diff) {
diff = monaco.editor.createDiffEditor(code, { readOnly: true, originalEditable: false, renderSideBySide: false, automaticLayout: true, minimap: { enabled: false },
scrollBeyondLastLine: false, fontSize: 12, renderOverviewRuler: false, ignoreTrimWhitespace: false,
theme: document.documentElement.dataset.theme === 'light' ? 'jumphp-light' : 'jumphp-dark' });
models = [monaco.editor.createModel('', 'php'), monaco.editor.createModel('', 'php')];
diff.setModel({ original: models[0], modified: models[1] });
}
models[0].setValue(main.before ?? '');
models[1].setValue(main.after);
};
return {
node,
show(list, error = null) {
changes = list || [];
current = Math.min(current, Math.max(0, changes.length - 1));
if (error) { clear(files); files.append(el('div.wg-error', {}, error)); return; }
draw();
},
dispose() { diff?.dispose(); models.forEach((m) => m.dispose()); },
};
}
/** Writes the changes that differ (wizard.apply refuses a file that changed since the preview). Resolves with the files written. */
export async function writeChanges(app, changes) {
const todo = changes.filter((c) => c.before !== c.after);
if (!todo.length) { notify(t('Nothing changes.'), 'info'); return []; }
for (const c of todo) {
if (app.editors?.isDirty?.(c.file)) throw new Error(t('{file} has unsaved changes: save it first', { file: c.file }));
}
const r = await backend.invoke('wizard.apply', { changes: todo });
await app.editors?.reloadFromDisk?.(r.files);
app.tree?.refresh?.();
return r.files;
}
/**
* The notice of the packages a form needs. `missing` = [{package, command}]; "Install" runs `composer require --dev` for each, one after
* the other, in the project's Composer runner, its output shown live; `onDone()` is called when all are installed.
*/
export function installNotice(app, title, missing, onDone) {
const log = el('pre.wg-log', { hidden: true });
const text = el('span', {}, title);
const button = el('button.btn.small.primary', { type: 'button', onclick: () => run() }, t('Install'));
const node = el('div.wg-notice', { hidden: !missing.length }, el('div.wg-notice-row', {}, el('span.wg-ico', {}, '!'), text, el('code', {}, missing.map((m) => m.command).join(' && ')), button), log);
async function run() {
button.disabled = true;
log.hidden = false;
log.textContent = '';
try {
const r = await installPackages(missing, {
run: (params, onOutput) => requireWithComposer(app, params, onOutput),
onOutput: (chunk) => { log.textContent = (log.textContent + chunk.replace(/\x1b\[[0-9;?]*[A-Za-z]/g, '')).slice(-1500); log.scrollTop = log.scrollHeight; },
});
if (r.status === 'failed') throw new Error(t('The installation failed: see the output below, or the Run window.'));
text.textContent = t('Installed: {package}', { package: r.done.join(', ') });
node.classList.add('ok');
button.hidden = true;
await onDone?.();
} catch (e) {
text.textContent = e.message || String(e);
button.disabled = false;
}
}
return node;
}