78 lines
6.1 KiB
JavaScript
78 lines
6.1 KiB
JavaScript
// The "New…" hub: every create GUI of the IDE in one categorised palette with search. Each entry only calls the existing action or command
|
|
// of its window; an entry is hidden when the project lacks what it needs. Reached by the + New button of the tool bar (Alt+N), the
|
|
// project tree's context menu (New > ...) and Search Everywhere ("New: Controller…"). The windows keep their own create buttons.
|
|
import { chooser, t } from './ui.js';
|
|
|
|
export const GROUPS = ['Project', 'Symfony', 'Doctrine', 'Security', 'API and HTTP', 'Scripts and tests', 'Translations'];
|
|
|
|
const later = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
|
|
/** The entries available in this project: [{id, group, label, icon, run()}] (labels are English keys, translated when shown). */
|
|
export function hubEntries(app) {
|
|
const caps = app.project?.capabilities || {};
|
|
const symfony = caps.console === 'bin/console';
|
|
const writable = !caps.readOnly && !!app.tree;
|
|
// a bin/console maker: the Console window with the command selected, its form ready to fill
|
|
const maker = (name) => async () => {
|
|
app.showTool('console', true);
|
|
await app.symfonyConsole.load();
|
|
app.symfonyConsole.select(name);
|
|
};
|
|
// a window that loads its data when first shown: shown, then called
|
|
const via = (tool, fn) => async () => { app.showTool(tool, true); await later(300); await fn(); };
|
|
const dir = () => app.tree.targetDir();
|
|
const list = [
|
|
[writable, 'Project', 'File…', 'file', () => app.tree.newFile(dir())],
|
|
[writable, 'Project', 'Folder…', 'folder', () => app.tree.newDirectory(dir())],
|
|
[writable, 'Project', 'PHP class…', 'file', () => app.tree.newFile(dir(), '.php', 'Class name (Hero, Scene/PlayScene, Entity/Coin…)')],
|
|
[writable && symfony, 'Symfony', 'Twig template…', 'file', () => app.tree.newFile('templates', '.html.twig', 'Template name (home/index)')],
|
|
[symfony && !!app.symfonyConsole, 'Symfony', 'Controller…', 'console', maker('make:controller')],
|
|
[symfony && !!app.symfonyConsole, 'Symfony', 'Command…', 'console', () => app.actions.run('newSymfonyCommand')],
|
|
[!!app.symfonyForms, 'Symfony', 'Form type…', 'design', () => app.actions.run('symfonyNewFormType')],
|
|
[symfony && !!app.symfonyConsole, 'Symfony', 'Event subscriber…', 'bell', maker('make:subscriber')],
|
|
[symfony && !!app.symfonyConsole, 'Symfony', 'Message and handler…', 'cast', maker('make:message')],
|
|
[!!app.validationPanel, 'Symfony', 'Validation constraint…', 'check', via('validation', () => app.validationPanel.newConstraint())],
|
|
[!!app.doctrine, 'Doctrine', 'Entity…', 'hierarchy', () => app.actions.run('doctrineNewEntity')],
|
|
[!!app.doctrine, 'Doctrine', 'Migration (diff)', 'history', () => app.actions.run('doctrineDiff')],
|
|
[!!caps.fixtures, 'Doctrine', 'Fixtures…', 'hierarchy', () => app.actions.run('newFixtures')],
|
|
[!!caps.fixtures, 'Doctrine', 'Load fixtures…', 'play', () => app.actions.run('loadFixtures')],
|
|
[symfony && !!app.symfonyConsole, 'Security', 'User…', 'user', maker('make:user')],
|
|
[symfony && !!app.symfonyConsole, 'Security', 'Login (authenticator)…', 'user', maker('make:security:form-login')],
|
|
[symfony && !!app.symfonyConsole, 'Security', 'Voter…', 'user', maker('make:voter')],
|
|
[symfony && !app.apiPlatform && !!app.composer, 'API and HTTP', 'Install API Platform…', 'structure', () => import('./api-install.js').then((m) => m.installApiPlatform(app))],
|
|
[!!app.apiPlatform, 'API and HTTP', 'API resource…', 'structure', via('apiplatform', () => app.apiPlatform.addResource())],
|
|
[!!app.apiPlatform, 'API and HTTP', 'State provider…', 'structure', via('apiplatform', () => app.apiPlatform.newState('provider'))],
|
|
[!!app.apiPlatform, 'API and HTTP', 'State processor…', 'structure', via('apiplatform', () => app.apiPlatform.newState('processor'))],
|
|
[!!app.httpClient, 'API and HTTP', 'HTTP request…', 'globe', via('httpClient', () => app.httpClient.newRequest())],
|
|
[!!app.httpClient, 'API and HTTP', 'HTTP collection…', 'globe', via('httpClient', () => app.httpClient.newCollection())],
|
|
[!!app.scripts, 'Scripts and tests', 'Script…', 'zap', () => app.actions.run('newScript')],
|
|
[!!app.scripts, 'Scripts and tests', 'Castor task…', 'zap', () => app.actions.run('newCastorTask')],
|
|
[!!caps.testWizard, 'Scripts and tests', 'PHPUnit test…', 'test', () => app.actions.run('newTestWizard')],
|
|
[symfony && !!app.symfonyConsole, 'Scripts and tests', 'Test…', 'test', maker('make:test')],
|
|
[!!app.translations, 'Translations', 'Translation key…', 'globe', via('translations', () => app.translations.addKey())],
|
|
];
|
|
return list.filter(([ok]) => ok).map(([, group, label, ico, run]) => ({ id: `${group}/${label}`, group, label, icon: ico, run }));
|
|
}
|
|
|
|
/** The palette: grouped, filtered as you type. `at` is the button it opens under. */
|
|
export function openNewHub(app, at) {
|
|
const entries = hubEntries(app);
|
|
chooser((query) => {
|
|
const q = query.trim().toLowerCase();
|
|
return entries
|
|
.filter((e) => !q || `${e.label} ${t(e.label)} ${e.group} ${t(e.group)}`.toLowerCase().includes(q))
|
|
.sort((a, b) => GROUPS.indexOf(a.group) - GROUPS.indexOf(b.group))
|
|
.map((e) => ({ label: e.label, group: t(e.group), icon: e.icon, action: () => e.run(), tour: 'new-' + e.label.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '') }));
|
|
}, { at, placeholder: 'What do you want to create?', width: 380 });
|
|
}
|
|
|
|
/** One action per entry for Search Everywhere ("New: Controller…"): the labels are already in the user's language. */
|
|
export function registerHubActions(app, a) {
|
|
a.add('newHub', 'New…', () => openNewHub(app, document.querySelector('[data-tour="new-button"]') || undefined), { kbd: 'Alt+N', icon: 'plus' });
|
|
for (const e of hubEntries(app)) a.add('new:' + e.id, `${t('New')}: ${t(e.group)} ▸ ${t(e.label)}`, () => e.run(), { icon: e.icon });
|
|
}
|
|
|
|
/** The tree's context menu: New > the entries of the hub (the tree keeps its own file kinds above them). */
|
|
export function hubMenuItems(app) {
|
|
return hubEntries(app).filter((e) => e.group !== 'Project').map((e) => ({ label: `${t(e.group)} ▸ ${t(e.label)}`, icon: e.icon, action: () => e.run() }));
|
|
}
|