Skip to content

Commit

Permalink
Improving the label of the admin menu item
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher committed Jul 10, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 69f93c4 commit 48aa540
Showing 2 changed files with 70 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@
},
"autoload-dev": {
"psr-4": {
"Alley\WP\Command_Palette\\Tests\\": "tests"
"Alley\\WP\\Command_Palette\\Tests\\": "tests"
}
},
"extra": {
80 changes: 69 additions & 11 deletions services/commands/adminMenu.ts
Original file line number Diff line number Diff line change
@@ -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;
// },
// });
// }


Check failure on line 89 in services/commands/adminMenu.ts

GitHub Actions / node-tests / Install, build, and test

More than 1 blank line not allowed
// Collect all available submenu items from the parent menu.
// const submenus = menus.querySelectorAll('.wp-submenu li:not(.wp-first-item) a');
});

return index;

0 comments on commit 48aa540

Please sign in to comment.