74 lines
3.9 KiB
JavaScript
74 lines
3.9 KiB
JavaScript
// The changes the Validation and Security windows want to make (symfony-validation.js, symfony-security.js), shown before
|
|
// they are written: each file as a difference (Monaco's diff editor, new files against nothing), Apply writes them all.
|
|
// A file open with unsaved changes is not overwritten (save or revert it first); a file that changed on disk since the
|
|
// change was computed is refused (the window computes it again). Open tabs reload what was written.
|
|
// Also the windows' stylesheet (css/symfony-tools.css), loaded once.
|
|
import { backend } from './backend.js';
|
|
import { el, clear, dialog, icon, notify, t, basename } from './ui.js';
|
|
import { monaco, languageOf } from './editor.js';
|
|
|
|
let styled = false;
|
|
export function symfonyToolsStyle() {
|
|
if (styled) return;
|
|
styled = true;
|
|
document.head.append(el('link', { rel: 'stylesheet', href: 'css/symfony-tools.css' }));
|
|
}
|
|
|
|
/** files: [{path, before, after}]; resolves true when they were written */
|
|
export function reviewChanges(app, files, { title = 'Review the changes' } = {}) {
|
|
symfonyToolsStyle();
|
|
const changed = files.filter((f) => f.before !== f.after);
|
|
if (!changed.length) { notify(t('Nothing to change'), 'info'); return Promise.resolve(false); }
|
|
return new Promise((resolve) => {
|
|
let done = false;
|
|
let editor = null;
|
|
const models = [];
|
|
const view = el('div.sx-diff-view');
|
|
const list = el('div.sx-diff-files');
|
|
const show = (f) => {
|
|
for (const row of list.children) row.classList.toggle('on', row.dataset.path === f.path);
|
|
clear(view);
|
|
editor?.dispose();
|
|
if (!monaco) { view.append(el('pre.sx-pre', {}, f.after)); return; }
|
|
const lang = languageOf(f.path) || 'plaintext';
|
|
const a = monaco.editor.createModel(f.before, lang);
|
|
const b = monaco.editor.createModel(f.after, lang);
|
|
models.push(a, b);
|
|
editor = monaco.editor.createDiffEditor(view, { readOnly: true, originalEditable: false, renderSideBySide: false, automaticLayout: true,
|
|
minimap: { enabled: false }, scrollBeyondLastLine: false, ignoreTrimWhitespace: false, theme: document.documentElement.dataset.theme === 'light' ? 'jumphp-light' : 'jumphp-dark' });
|
|
editor.setModel({ original: a, modified: b });
|
|
};
|
|
for (const f of changed) {
|
|
list.append(el('div.sx-diff-file', { dataset: { path: f.path }, onclick: () => show(f), title: f.path },
|
|
icon(f.before === '' ? 'plus' : 'edit'), el('span', {}, basename(f.path)), el('small', {}, f.before === '' ? t('new') : t('changed'))));
|
|
}
|
|
const d = dialog({
|
|
title,
|
|
wide: true,
|
|
body: el('div.sx-diff', {}, list, view),
|
|
buttons: [{ label: 'Cancel' }, { label: changed.length > 1 ? t('Apply {n} files', { n: changed.length }) : 'Apply', primary: true, action: async () => {
|
|
await applyFiles(app, changed);
|
|
done = true;
|
|
resolve(true);
|
|
} }],
|
|
onClose: () => { editor?.dispose(); for (const m of models) m.dispose(); if (!done) resolve(false); },
|
|
});
|
|
d.box.classList.add('sx-diff-dialog');
|
|
show(changed[0]);
|
|
});
|
|
}
|
|
|
|
/** writes the files (after checking the disk and the open tabs), then reloads the tabs */
|
|
export async function applyFiles(app, files) {
|
|
for (const f of files) {
|
|
if (app.editors?.isDirty?.(f.path)) throw new Error(t('{file} has unsaved changes: save it first', { file: f.path }));
|
|
let current = '';
|
|
try { current = (await backend.invoke('file.read', { path: f.path })).content ?? ''; } catch { current = ''; }
|
|
if (current !== f.before) throw new Error(t('{file} changed meanwhile: try again', { file: f.path }));
|
|
}
|
|
for (const f of files) await backend.invoke('file.write', { path: f.path, content: f.after });
|
|
await app.editors?.reloadFromDisk?.(files.map((f) => f.path));
|
|
app.tree?.refresh?.();
|
|
app.git?.refreshSoon?.();
|
|
notify(files.length > 1 ? t('{n} files written', { n: files.length }) : t('{file} written', { file: files[0].path }), 'success');
|
|
}
|