From 48aa5401e73becbc599711040a9e95b46d49f653 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Wed, 10 Jul 2024 17:15:28 -0400 Subject: [PATCH] Improving the label of the admin menu item --- composer.json | 2 +- services/commands/adminMenu.ts | 80 +++++++++++++++++++++++++++++----- 2 files changed, 70 insertions(+), 12 deletions(-) diff --git a/composer.json b/composer.json index 03a9b8e..166cbd8 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,7 @@ }, "autoload-dev": { "psr-4": { - "Alley\WP\Command_Palette\\Tests\\": "tests" + "Alley\\WP\\Command_Palette\\Tests\\": "tests" } }, "extra": { diff --git a/services/commands/adminMenu.ts b/services/commands/adminMenu.ts index 513f03b..d5f50a4 100644 --- a/services/commands/adminMenu.ts +++ b/services/commands/adminMenu.ts @@ -1,36 +1,94 @@ import { arrowRight } from '@wordpress/icons'; import type { Command } from '@wordpress/commands'; +const slugify = (text: string) => text.toLowerCase().replace(/: /g, '-').replace(/\s+/g, '-'); + /** * Collect all admin menu links as possible commands. */ const adminMenu = (): Command[] => { - const links = document.querySelectorAll('#adminmenu a'); + const menus = document.querySelectorAll('ul#adminmenu > li'); + // const links = document.querySelectorAll('#adminmenu .menu-top a'); const index: Command[] = []; - Array.from(links).forEach((link) => { - const url = link.getAttribute('href'); + Array.from(menus).forEach((menu) => { + const parentMenuLink = menu.querySelector('a'); + + if (!parentMenuLink) { + return; + } + + const href = parentMenuLink.getAttribute('href'); - if (!url || url === '#') { + if (!href || href === '#') { return; } - let label = link.textContent; + index.push({ + label: `Go to ${parentMenuLink.textContent}`, + name: `wp-command-palette-${slugify(parentMenuLink.textContent || href)}`, + icon: arrowRight, + callback: () => { + window.location.href = href; + }, + }); - if (url.endsWith('edit-comments.php')) { - label = 'Comments'; + if (!menu.classList.contains('wp-has-submenu')) { + return; } - if (url && label) { + const submenuItems = menu.querySelectorAll('.wp-submenu li a'); + + if (!submenuItems.length) { + return; + } + + Array.from(submenuItems).forEach((submenuItem) => { + const url = submenuItem.getAttribute('href'); + + if (!url || url === '#') { + return; + } + index.push({ - label: `Go to Settings: ${label}`, - name: url, + label: `Go to: ${parentMenuLink.textContent} – ${submenuItem.textContent}`, + name: `wp-command-palette-${slugify(`${parentMenuLink.textContent}: ${submenuItem.textContent}`)}`, icon: arrowRight, callback: () => { window.location.href = url; }, }); - } + }); + + // Add the parent menu + // const url = link.getAttribute('href'); + + // if (!url || url === '#') { + // return; + // } + + // let label = link.textContent; + + // if (url.endsWith('edit-comments.php')) { + // label = 'Comments'; + // } + + // // Check if the link is a + + // if (url && label) { + // index.push({ + // label: `Go to Settings: ${label}`, + // name: url, + // icon: arrowRight, + // callback: () => { + // window.location.href = url; + // }, + // }); + // } + + + // Collect all available submenu items from the parent menu. + // const submenus = menus.querySelectorAll('.wp-submenu li:not(.wp-first-item) a'); }); return index;