feat(structure): adjust
This commit is contained in:
215
ts_web/elements/dees-contextmenu/dees-contextmenu.demo.ts
Normal file
215
ts_web/elements/dees-contextmenu/dees-contextmenu.demo.ts
Normal file
@@ -0,0 +1,215 @@
|
||||
import { html } from '@design.estate/dees-element';
|
||||
import * as plugins from './00plugins.js';
|
||||
|
||||
import { DeesContextmenu } from './dees-contextmenu.js';
|
||||
|
||||
export const demoFunc = () => html`
|
||||
<style>
|
||||
.withMargin {
|
||||
display: block;
|
||||
margin: 20px;
|
||||
}
|
||||
.demo-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 20px;
|
||||
padding: 20px;
|
||||
min-height: 400px;
|
||||
}
|
||||
.demo-area {
|
||||
padding: 40px;
|
||||
border-radius: 8px;
|
||||
text-align: center;
|
||||
cursor: context-menu;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
.demo-area:hover {
|
||||
background: rgba(0, 0, 0, 0.02);
|
||||
}
|
||||
</style>
|
||||
<div class="demo-container">
|
||||
<dees-panel heading="Basic Context Menu with Nested Submenus">
|
||||
<div class="demo-area" @contextmenu=${(eventArg: MouseEvent) => {
|
||||
DeesContextmenu.openContextMenuWithOptions(eventArg, [
|
||||
{
|
||||
name: 'File',
|
||||
iconName: 'fileText',
|
||||
action: async () => {}, // Parent items with submenus still need an action
|
||||
submenu: [
|
||||
{ name: 'New', iconName: 'filePlus', shortcut: 'Cmd+N', action: async () => console.log('New file') },
|
||||
{ name: 'Open', iconName: 'folderOpen', shortcut: 'Cmd+O', action: async () => console.log('Open file') },
|
||||
{ name: 'Save', iconName: 'save', shortcut: 'Cmd+S', action: async () => console.log('Save') },
|
||||
{ divider: true },
|
||||
{ name: 'Export as PDF', iconName: 'download', action: async () => console.log('Export PDF') },
|
||||
{ name: 'Export as HTML', iconName: 'code', action: async () => console.log('Export HTML') },
|
||||
]
|
||||
},
|
||||
{
|
||||
name: 'Edit',
|
||||
iconName: 'edit3',
|
||||
action: async () => {}, // Parent items with submenus still need an action
|
||||
submenu: [
|
||||
{ name: 'Cut', iconName: 'scissors', shortcut: 'Cmd+X', action: async () => console.log('Cut') },
|
||||
{ name: 'Copy', iconName: 'copy', shortcut: 'Cmd+C', action: async () => console.log('Copy') },
|
||||
{ name: 'Paste', iconName: 'clipboard', shortcut: 'Cmd+V', action: async () => console.log('Paste') },
|
||||
{ divider: true },
|
||||
{ name: 'Find', iconName: 'search', shortcut: 'Cmd+F', action: async () => console.log('Find') },
|
||||
{ name: 'Replace', iconName: 'repeat', shortcut: 'Cmd+H', action: async () => console.log('Replace') },
|
||||
]
|
||||
},
|
||||
{
|
||||
name: 'View',
|
||||
iconName: 'eye',
|
||||
action: async () => {}, // Parent items with submenus still need an action
|
||||
submenu: [
|
||||
{ name: 'Zoom In', iconName: 'zoomIn', shortcut: 'Cmd++', action: async () => console.log('Zoom in') },
|
||||
{ name: 'Zoom Out', iconName: 'zoomOut', shortcut: 'Cmd+-', action: async () => console.log('Zoom out') },
|
||||
{ name: 'Reset Zoom', iconName: 'maximize2', shortcut: 'Cmd+0', action: async () => console.log('Reset zoom') },
|
||||
{ divider: true },
|
||||
{ name: 'Full Screen', iconName: 'maximize', shortcut: 'F11', action: async () => console.log('Full screen') },
|
||||
]
|
||||
},
|
||||
{ divider: true },
|
||||
{
|
||||
name: 'Settings',
|
||||
iconName: 'settings',
|
||||
action: async () => console.log('Settings')
|
||||
},
|
||||
{
|
||||
name: 'Help',
|
||||
iconName: 'helpCircle',
|
||||
action: async () => {}, // Parent items with submenus still need an action
|
||||
submenu: [
|
||||
{ name: 'Documentation', iconName: 'book', action: async () => console.log('Documentation') },
|
||||
{ name: 'Keyboard Shortcuts', iconName: 'keyboard', action: async () => console.log('Shortcuts') },
|
||||
{ divider: true },
|
||||
{ name: 'About', iconName: 'info', action: async () => console.log('About') },
|
||||
]
|
||||
}
|
||||
]);
|
||||
}}>
|
||||
<h3>Right-click anywhere in this area</h3>
|
||||
<p>A context menu with nested submenus will appear</p>
|
||||
</div>
|
||||
</dees-panel>
|
||||
<dees-panel heading="Component-Specific Context Menu">
|
||||
<dees-button style="margin: 20px;" @contextmenu=${(eventArg: MouseEvent) => {
|
||||
DeesContextmenu.openContextMenuWithOptions(eventArg, [
|
||||
{
|
||||
name: 'Button Actions',
|
||||
iconName: 'mousePointer',
|
||||
action: async () => {}, // Parent items with submenus still need an action
|
||||
submenu: [
|
||||
{ name: 'Click', iconName: 'mouse', action: async () => console.log('Click action') },
|
||||
{ name: 'Double Click', iconName: 'zap', action: async () => console.log('Double click') },
|
||||
{ name: 'Long Press', iconName: 'clock', action: async () => console.log('Long press') },
|
||||
]
|
||||
},
|
||||
{
|
||||
name: 'Button State',
|
||||
iconName: 'toggleLeft',
|
||||
action: async () => {}, // Parent items with submenus still need an action
|
||||
submenu: [
|
||||
{ name: 'Enable', iconName: 'checkCircle', action: async () => console.log('Enable') },
|
||||
{ name: 'Disable', iconName: 'xCircle', action: async () => console.log('Disable') },
|
||||
{ divider: true },
|
||||
{ name: 'Show', iconName: 'eye', action: async () => console.log('Show') },
|
||||
{ name: 'Hide', iconName: 'eyeOff', action: async () => console.log('Hide') },
|
||||
]
|
||||
},
|
||||
{ divider: true },
|
||||
{
|
||||
name: 'Disabled Action',
|
||||
iconName: 'ban',
|
||||
disabled: true,
|
||||
action: async () => console.log('This should not run'),
|
||||
},
|
||||
{
|
||||
name: 'Properties',
|
||||
iconName: 'settings',
|
||||
action: async () => console.log('Button properties'),
|
||||
},
|
||||
]);
|
||||
}}>Right-click on this button</dees-button>
|
||||
</dees-panel>
|
||||
|
||||
<dees-panel heading="Advanced Context Menu Example">
|
||||
<div class="demo-area" @contextmenu=${(eventArg: MouseEvent) => {
|
||||
DeesContextmenu.openContextMenuWithOptions(eventArg, [
|
||||
{
|
||||
name: 'Format',
|
||||
iconName: 'type',
|
||||
action: async () => {}, // Parent items with submenus still need an action
|
||||
submenu: [
|
||||
{ name: 'Bold', iconName: 'bold', shortcut: 'Cmd+B', action: async () => console.log('Bold') },
|
||||
{ name: 'Italic', iconName: 'italic', shortcut: 'Cmd+I', action: async () => console.log('Italic') },
|
||||
{ name: 'Underline', iconName: 'underline', shortcut: 'Cmd+U', action: async () => console.log('Underline') },
|
||||
{ divider: true },
|
||||
{ name: 'Font Size', iconName: 'type', action: async () => console.log('Font size menu') },
|
||||
{ name: 'Font Color', iconName: 'palette', action: async () => console.log('Font color menu') },
|
||||
]
|
||||
},
|
||||
{
|
||||
name: 'Transform',
|
||||
iconName: 'shuffle',
|
||||
action: async () => {}, // Parent items with submenus still need an action
|
||||
submenu: [
|
||||
{ name: 'To Uppercase', iconName: 'arrowUp', action: async () => console.log('Uppercase') },
|
||||
{ name: 'To Lowercase', iconName: 'arrowDown', action: async () => console.log('Lowercase') },
|
||||
{ name: 'Capitalize', iconName: 'type', action: async () => console.log('Capitalize') },
|
||||
]
|
||||
},
|
||||
{ divider: true },
|
||||
{
|
||||
name: 'Delete',
|
||||
iconName: 'trash2',
|
||||
action: async () => console.log('Delete')
|
||||
}
|
||||
]);
|
||||
}}>
|
||||
<h3>Advanced Nested Menu Example</h3>
|
||||
<p>This shows deeply nested submenus and various formatting options</p>
|
||||
</div>
|
||||
</dees-panel>
|
||||
|
||||
<dees-panel heading="Static Context Menu (Always Visible)">
|
||||
<dees-contextmenu
|
||||
class="withMargin"
|
||||
.menuItems=${[
|
||||
{
|
||||
name: 'Project',
|
||||
iconName: 'folder',
|
||||
action: async () => {}, // Parent items with submenus still need an action
|
||||
submenu: [
|
||||
{ name: 'New Project', iconName: 'folderPlus', shortcut: 'Cmd+Shift+N', action: async () => console.log('New project') },
|
||||
{ name: 'Open Project', iconName: 'folderOpen', shortcut: 'Cmd+Shift+O', action: async () => console.log('Open project') },
|
||||
{ divider: true },
|
||||
{ name: 'Recent Projects', iconName: 'clock', action: async () => {}, submenu: [
|
||||
{ name: 'Project Alpha', action: async () => console.log('Open Alpha') },
|
||||
{ name: 'Project Beta', action: async () => console.log('Open Beta') },
|
||||
{ name: 'Project Gamma', action: async () => console.log('Open Gamma') },
|
||||
]},
|
||||
]
|
||||
},
|
||||
{
|
||||
name: 'Tools',
|
||||
iconName: 'tool',
|
||||
action: async () => {}, // Parent items with submenus still need an action
|
||||
submenu: [
|
||||
{ name: 'Terminal', iconName: 'terminal', shortcut: 'Cmd+T', action: async () => console.log('Terminal') },
|
||||
{ name: 'Console', iconName: 'monitor', shortcut: 'Cmd+K', action: async () => console.log('Console') },
|
||||
{ divider: true },
|
||||
{ name: 'Extensions', iconName: 'package', action: async () => console.log('Extensions') },
|
||||
]
|
||||
},
|
||||
{ divider: true },
|
||||
{
|
||||
name: 'Preferences',
|
||||
iconName: 'sliders',
|
||||
action: async () => console.log('Preferences'),
|
||||
},
|
||||
]}
|
||||
></dees-contextmenu>
|
||||
</dees-panel>
|
||||
</div>
|
||||
`;
|
||||
456
ts_web/elements/dees-contextmenu/dees-contextmenu.ts
Normal file
456
ts_web/elements/dees-contextmenu/dees-contextmenu.ts
Normal file
@@ -0,0 +1,456 @@
|
||||
import * as plugins from './00plugins.js';
|
||||
import { demoFunc } from './dees-contextmenu.demo.js';
|
||||
import {
|
||||
customElement,
|
||||
html,
|
||||
DeesElement,
|
||||
property,
|
||||
type TemplateResult,
|
||||
cssManager,
|
||||
css,
|
||||
type CSSResult,
|
||||
unsafeCSS,
|
||||
} from '@design.estate/dees-element';
|
||||
|
||||
import * as domtools from '@design.estate/dees-domtools';
|
||||
import { DeesWindowLayer } from './dees-windowlayer.js';
|
||||
import { zIndexLayers } from './00zindex.js';
|
||||
import './dees-icon.js';
|
||||
|
||||
declare global {
|
||||
interface HTMLElementTagNameMap {
|
||||
'dees-contextmenu': DeesContextmenu;
|
||||
}
|
||||
}
|
||||
|
||||
@customElement('dees-contextmenu')
|
||||
export class DeesContextmenu extends DeesElement {
|
||||
// DEMO
|
||||
public static demo = demoFunc
|
||||
|
||||
// STATIC
|
||||
// This will store all the accumulated menu items
|
||||
public static contextMenuDeactivated = false;
|
||||
public static accumulatedMenuItems: (plugins.tsclass.website.IMenuItem & { shortcut?: string; disabled?: boolean; submenu?: (plugins.tsclass.website.IMenuItem & { shortcut?: string; disabled?: boolean } | { divider: true })[] } | { divider: true })[] = [];
|
||||
|
||||
// Add a global event listener for the right-click context menu
|
||||
public static initializeGlobalListener() {
|
||||
document.addEventListener('contextmenu', (event: MouseEvent) => {
|
||||
if (this.contextMenuDeactivated) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
|
||||
// Clear previously accumulated items
|
||||
DeesContextmenu.accumulatedMenuItems = [];
|
||||
|
||||
// Use composedPath to properly traverse shadow DOM boundaries
|
||||
const path = event.composedPath();
|
||||
|
||||
// Traverse the composed path to accumulate menu items
|
||||
for (const element of path) {
|
||||
if ((element as any).getContextMenuItems) {
|
||||
const items = (element as any).getContextMenuItems();
|
||||
if (items && items.length > 0) {
|
||||
if (DeesContextmenu.accumulatedMenuItems.length > 0) {
|
||||
DeesContextmenu.accumulatedMenuItems.push({ divider: true });
|
||||
}
|
||||
DeesContextmenu.accumulatedMenuItems.push(...items);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Open the context menu with the accumulated items
|
||||
DeesContextmenu.openContextMenuWithOptions(event, DeesContextmenu.accumulatedMenuItems);
|
||||
});
|
||||
}
|
||||
|
||||
// allows opening of a contextmenu with options
|
||||
public static async openContextMenuWithOptions(eventArg: MouseEvent, menuItemsArg: (plugins.tsclass.website.IMenuItem & { shortcut?: string; disabled?: boolean; submenu?: (plugins.tsclass.website.IMenuItem & { shortcut?: string; disabled?: boolean } | { divider: true })[] } | { divider: true })[]) {
|
||||
if (this.contextMenuDeactivated) {
|
||||
return;
|
||||
}
|
||||
eventArg.preventDefault();
|
||||
eventArg.stopPropagation();
|
||||
const contextMenu = new DeesContextmenu();
|
||||
contextMenu.style.position = 'fixed';
|
||||
contextMenu.style.zIndex = String(zIndexLayers.overlay.contextMenu);
|
||||
contextMenu.style.opacity = '0';
|
||||
contextMenu.style.transform = 'scale(0.95) translateY(-10px)';
|
||||
contextMenu.menuItems = menuItemsArg;
|
||||
contextMenu.windowLayer = await DeesWindowLayer.createAndShow();
|
||||
contextMenu.windowLayer.addEventListener('click', async (event) => {
|
||||
// Check if click is on the context menu or its submenus
|
||||
const clickedElement = event.target as HTMLElement;
|
||||
const isContextMenu = clickedElement.closest('dees-contextmenu');
|
||||
if (!isContextMenu) {
|
||||
await contextMenu.destroy();
|
||||
}
|
||||
})
|
||||
document.body.append(contextMenu);
|
||||
|
||||
// Get dimensions after adding to DOM
|
||||
await domtools.plugins.smartdelay.delayFor(0);
|
||||
const rect = contextMenu.getBoundingClientRect();
|
||||
const windowWidth = window.innerWidth;
|
||||
const windowHeight = window.innerHeight;
|
||||
|
||||
// Calculate position
|
||||
let top = eventArg.clientY;
|
||||
let left = eventArg.clientX;
|
||||
|
||||
// Adjust if menu would go off right edge
|
||||
if (left + rect.width > windowWidth) {
|
||||
left = windowWidth - rect.width - 10;
|
||||
}
|
||||
|
||||
// Adjust if menu would go off bottom edge
|
||||
if (top + rect.height > windowHeight) {
|
||||
top = windowHeight - rect.height - 10;
|
||||
}
|
||||
|
||||
// Ensure menu doesn't go off left or top edge
|
||||
if (left < 10) left = 10;
|
||||
if (top < 10) top = 10;
|
||||
|
||||
contextMenu.style.top = `${top}px`;
|
||||
contextMenu.style.left = `${left}px`;
|
||||
contextMenu.style.transformOrigin = 'top left';
|
||||
|
||||
// Animate in
|
||||
await domtools.plugins.smartdelay.delayFor(0);
|
||||
contextMenu.style.opacity = '1';
|
||||
contextMenu.style.transform = 'scale(1) translateY(0)';
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
@property({
|
||||
type: Array,
|
||||
})
|
||||
accessor menuItems: (plugins.tsclass.website.IMenuItem & { shortcut?: string; disabled?: boolean; submenu?: (plugins.tsclass.website.IMenuItem & { shortcut?: string; disabled?: boolean } | { divider: true })[]; divider?: never } | { divider: true })[] = [];
|
||||
windowLayer: DeesWindowLayer;
|
||||
|
||||
private submenu: DeesContextmenu | null = null;
|
||||
private submenuTimeout: any = null;
|
||||
private parentMenu: DeesContextmenu | null = null;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.tabIndex = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* STATIC STYLES
|
||||
*/
|
||||
public static styles = [
|
||||
cssManager.defaultStyles,
|
||||
css`
|
||||
:host {
|
||||
display: block;
|
||||
transition: opacity 0.2s, transform 0.2s;
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.mainbox {
|
||||
min-width: 200px;
|
||||
max-width: 280px;
|
||||
background: ${cssManager.bdTheme('#ffffff', '#000000')};
|
||||
border: 1px solid ${cssManager.bdTheme('#e0e0e0', '#202020')};
|
||||
border-radius: 4px;
|
||||
box-shadow: ${cssManager.bdTheme(
|
||||
'0 4px 12px rgba(0, 0, 0, 0.15)',
|
||||
'0 4px 12px rgba(0, 0, 0, 0.3)'
|
||||
)};
|
||||
user-select: none;
|
||||
padding: 4px 0;
|
||||
font-size: 12px;
|
||||
color: ${cssManager.bdTheme('#333', '#ccc')};
|
||||
}
|
||||
|
||||
.menuitem {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 8px 12px;
|
||||
cursor: default;
|
||||
transition: background 0.1s;
|
||||
line-height: 1;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.menuitem:hover {
|
||||
background: ${cssManager.bdTheme('rgba(0, 0, 0, 0.04)', 'rgba(255, 255, 255, 0.08)')};
|
||||
}
|
||||
|
||||
.menuitem.has-submenu::after {
|
||||
content: '›';
|
||||
position: absolute;
|
||||
right: 8px;
|
||||
font-size: 16px;
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
.menuitem:active:not(.has-submenu) {
|
||||
background: ${cssManager.bdTheme('rgba(0, 0, 0, 0.08)', 'rgba(255, 255, 255, 0.12)')};
|
||||
}
|
||||
|
||||
.menuitem.disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.menuitem dees-icon {
|
||||
font-size: 14px;
|
||||
opacity: 0.7;
|
||||
}
|
||||
|
||||
.menuitem-text {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.menuitem-shortcut {
|
||||
font-size: 11px;
|
||||
color: ${cssManager.bdTheme('#999', '#666')};
|
||||
margin-left: auto;
|
||||
opacity: 0.7;
|
||||
}
|
||||
|
||||
.menu-divider {
|
||||
height: 1px;
|
||||
background: ${cssManager.bdTheme('#e0e0e0', '#202020')};
|
||||
margin: 4px 0;
|
||||
}
|
||||
`,
|
||||
];
|
||||
|
||||
public render(): TemplateResult {
|
||||
return html`
|
||||
<div class="mainbox">
|
||||
${this.menuItems.map((menuItemArg) => {
|
||||
if ('divider' in menuItemArg && menuItemArg.divider) {
|
||||
return html`<div class="menu-divider"></div>`;
|
||||
}
|
||||
|
||||
const menuItem = menuItemArg as plugins.tsclass.website.IMenuItem & { shortcut?: string; disabled?: boolean; submenu?: any };
|
||||
const hasSubmenu = menuItem.submenu && menuItem.submenu.length > 0;
|
||||
return html`
|
||||
<div
|
||||
class="menuitem ${menuItem.disabled ? 'disabled' : ''} ${hasSubmenu ? 'has-submenu' : ''}"
|
||||
@click=${() => !menuItem.disabled && !hasSubmenu && this.handleClick(menuItem)}
|
||||
@mouseenter=${() => this.handleMenuItemHover(menuItem, hasSubmenu)}
|
||||
@mouseleave=${() => this.handleMenuItemLeave()}
|
||||
>
|
||||
${menuItem.iconName ? html`
|
||||
<dees-icon .icon="${`lucide:${menuItem.iconName}`}"></dees-icon>
|
||||
` : ''}
|
||||
<span class="menuitem-text">${menuItem.name}</span>
|
||||
${menuItem.shortcut && !hasSubmenu ? html`
|
||||
<span class="menuitem-shortcut">${menuItem.shortcut}</span>
|
||||
` : ''}
|
||||
</div>
|
||||
`;
|
||||
})}
|
||||
${this.menuItems.length === 0 ? html`
|
||||
<div class="menuitem" @click=${() => {
|
||||
DeesContextmenu.contextMenuDeactivated = true;
|
||||
this.destroy();
|
||||
}}>
|
||||
<dees-icon .icon="lucide:x"></dees-icon>
|
||||
<span class="menuitem-text">Allow native context</span>
|
||||
</div>
|
||||
` : html``}
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
public async firstUpdated() {
|
||||
// Focus on the menu for keyboard navigation
|
||||
this.focus();
|
||||
|
||||
// Add keyboard event listeners
|
||||
this.addEventListener('keydown', this.handleKeydown);
|
||||
}
|
||||
|
||||
private handleKeydown = (event: KeyboardEvent) => {
|
||||
const menuItems = Array.from(this.shadowRoot.querySelectorAll('.menuitem:not(.disabled)'));
|
||||
const currentIndex = menuItems.findIndex(item => item.matches(':hover'));
|
||||
|
||||
switch (event.key) {
|
||||
case 'ArrowDown':
|
||||
event.preventDefault();
|
||||
const nextIndex = currentIndex + 1 < menuItems.length ? currentIndex + 1 : 0;
|
||||
(menuItems[nextIndex] as HTMLElement).dispatchEvent(new MouseEvent('mouseenter'));
|
||||
break;
|
||||
|
||||
case 'ArrowUp':
|
||||
event.preventDefault();
|
||||
const prevIndex = currentIndex - 1 >= 0 ? currentIndex - 1 : menuItems.length - 1;
|
||||
(menuItems[prevIndex] as HTMLElement).dispatchEvent(new MouseEvent('mouseenter'));
|
||||
break;
|
||||
|
||||
case 'Enter':
|
||||
event.preventDefault();
|
||||
if (currentIndex >= 0) {
|
||||
(menuItems[currentIndex] as HTMLElement).click();
|
||||
}
|
||||
break;
|
||||
|
||||
case 'Escape':
|
||||
event.preventDefault();
|
||||
this.destroy();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public async handleClick(menuItem: plugins.tsclass.website.IMenuItem & { shortcut?: string; disabled?: boolean }) {
|
||||
menuItem.action();
|
||||
|
||||
// Close all menus in the chain (this menu and all parent menus)
|
||||
await this.destroyAll();
|
||||
}
|
||||
|
||||
private async handleMenuItemHover(menuItem: plugins.tsclass.website.IMenuItem & { submenu?: any }, hasSubmenu: boolean) {
|
||||
// Clear any existing timeout
|
||||
if (this.submenuTimeout) {
|
||||
clearTimeout(this.submenuTimeout);
|
||||
this.submenuTimeout = null;
|
||||
}
|
||||
|
||||
// Hide any existing submenu if hovering a different item
|
||||
if (this.submenu) {
|
||||
await this.hideSubmenu();
|
||||
}
|
||||
|
||||
// Show submenu if this item has one
|
||||
if (hasSubmenu && menuItem.submenu) {
|
||||
this.submenuTimeout = setTimeout(() => {
|
||||
this.showSubmenu(menuItem);
|
||||
}, 200); // Small delay to prevent accidental triggers
|
||||
}
|
||||
}
|
||||
|
||||
private handleMenuItemLeave() {
|
||||
// Add a delay before hiding to allow moving to submenu
|
||||
if (this.submenuTimeout) {
|
||||
clearTimeout(this.submenuTimeout);
|
||||
}
|
||||
|
||||
this.submenuTimeout = setTimeout(() => {
|
||||
if (this.submenu && !this.submenu.matches(':hover')) {
|
||||
this.hideSubmenu();
|
||||
}
|
||||
}, 300);
|
||||
}
|
||||
|
||||
private async showSubmenu(menuItem: plugins.tsclass.website.IMenuItem & { submenu?: any }) {
|
||||
if (!menuItem.submenu || menuItem.submenu.length === 0) return;
|
||||
|
||||
// Find the menu item element
|
||||
const menuItems = Array.from(this.shadowRoot.querySelectorAll('.menuitem'));
|
||||
const menuItemElement = menuItems.find(el => el.querySelector('.menuitem-text')?.textContent === menuItem.name) as HTMLElement;
|
||||
if (!menuItemElement) return;
|
||||
|
||||
// Create submenu
|
||||
this.submenu = new DeesContextmenu();
|
||||
this.submenu.menuItems = menuItem.submenu;
|
||||
this.submenu.parentMenu = this;
|
||||
this.submenu.style.position = 'fixed';
|
||||
this.submenu.style.zIndex = String(parseInt(this.style.zIndex) + 1);
|
||||
this.submenu.style.opacity = '0';
|
||||
this.submenu.style.transform = 'scale(0.95)';
|
||||
|
||||
// Don't create a window layer for submenus
|
||||
document.body.append(this.submenu);
|
||||
|
||||
// Position submenu
|
||||
await domtools.plugins.smartdelay.delayFor(0);
|
||||
const itemRect = menuItemElement.getBoundingClientRect();
|
||||
const menuRect = this.getBoundingClientRect();
|
||||
const submenuRect = this.submenu.getBoundingClientRect();
|
||||
const windowWidth = window.innerWidth;
|
||||
|
||||
let left = menuRect.right - 4; // Slight overlap
|
||||
let top = itemRect.top;
|
||||
|
||||
// Check if submenu would go off right edge
|
||||
if (left + submenuRect.width > windowWidth - 10) {
|
||||
// Show on left side instead
|
||||
left = menuRect.left - submenuRect.width + 4;
|
||||
}
|
||||
|
||||
// Adjust vertical position if needed
|
||||
if (top + submenuRect.height > window.innerHeight - 10) {
|
||||
top = window.innerHeight - submenuRect.height - 10;
|
||||
}
|
||||
|
||||
this.submenu.style.left = `${left}px`;
|
||||
this.submenu.style.top = `${top}px`;
|
||||
|
||||
// Animate in
|
||||
await domtools.plugins.smartdelay.delayFor(0);
|
||||
this.submenu.style.opacity = '1';
|
||||
this.submenu.style.transform = 'scale(1)';
|
||||
|
||||
// Handle submenu hover
|
||||
this.submenu.addEventListener('mouseenter', () => {
|
||||
if (this.submenuTimeout) {
|
||||
clearTimeout(this.submenuTimeout);
|
||||
this.submenuTimeout = null;
|
||||
}
|
||||
});
|
||||
|
||||
this.submenu.addEventListener('mouseleave', () => {
|
||||
this.handleMenuItemLeave();
|
||||
});
|
||||
}
|
||||
|
||||
private async hideSubmenu() {
|
||||
if (!this.submenu) return;
|
||||
|
||||
await this.submenu.destroy();
|
||||
this.submenu = null;
|
||||
}
|
||||
|
||||
public async destroy() {
|
||||
// Clear timeout
|
||||
if (this.submenuTimeout) {
|
||||
clearTimeout(this.submenuTimeout);
|
||||
this.submenuTimeout = null;
|
||||
}
|
||||
|
||||
// Destroy submenu first
|
||||
if (this.submenu) {
|
||||
await this.submenu.destroy();
|
||||
this.submenu = null;
|
||||
}
|
||||
|
||||
// Only destroy window layer if this is not a submenu
|
||||
if (this.windowLayer && !this.parentMenu) {
|
||||
this.windowLayer.destroy();
|
||||
}
|
||||
|
||||
this.style.opacity = '0';
|
||||
this.style.transform = 'scale(0.95) translateY(-10px)';
|
||||
await domtools.plugins.smartdelay.delayFor(100);
|
||||
|
||||
if (this.parentElement) {
|
||||
this.parentElement.removeChild(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys this menu and all parent menus in the chain
|
||||
*/
|
||||
public async destroyAll() {
|
||||
// First destroy parent menus if they exist
|
||||
if (this.parentMenu) {
|
||||
await this.parentMenu.destroyAll();
|
||||
} else {
|
||||
// If we're at the top level, just destroy this menu
|
||||
await this.destroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DeesContextmenu.initializeGlobalListener();
|
||||
Reference in New Issue
Block a user