context menu refactor

This commit is contained in:
Jan Prochazka 2021-04-07 17:57:05 +02:00
parent e84d231a10
commit 82b63c70ed
4 changed files with 31 additions and 4 deletions

View File

@ -13,7 +13,7 @@
const rect = domButton.getBoundingClientRect(); const rect = domButton.getBoundingClientRect();
const left = rect.left; const left = rect.left;
const top = rect.bottom; const top = rect.bottom;
currentDropDownMenu.set({ left, top, items: _.isFunction(menu) ? menu() : menu }); currentDropDownMenu.set({ left, top, items: menu });
} }
</script> </script>

View File

@ -8,7 +8,7 @@
<DropDownMenu <DropDownMenu
left={$currentDropDownMenu.left} left={$currentDropDownMenu.left}
top={$currentDropDownMenu.top} top={$currentDropDownMenu.top}
items={_.compact(_.flatten($currentDropDownMenu.items))} items={$currentDropDownMenu.items}
on:close={() => ($currentDropDownMenu = null)} on:close={() => ($currentDropDownMenu = null)}
/> />
{/if} {/if}

View File

@ -48,6 +48,7 @@
import { createEventDispatcher } from 'svelte'; import { createEventDispatcher } from 'svelte';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { commands } from '../stores'; import { commands } from '../stores';
import { extractMenuItems } from '../utility/contextMenu';
export let items; export let items;
export let top; export let top;
@ -66,6 +67,8 @@
onMount(() => { onMount(() => {
fixPopupPlacement(element); fixPopupPlacement(element);
}); });
$: extracted = extractMenuItems(items);
</script> </script>
<ul <ul
@ -74,7 +77,7 @@
on:clickOutside={() => dispatch('close')} on:clickOutside={() => dispatch('close')}
bind:this={element} bind:this={element}
> >
{#each _.compact(items.map(x => mapItem(x, $commands))) as item} {#each _.compact(extracted.map(x => mapItem(x, $commands))) as item}
{#if item.divider} {#if item.divider}
<li class="divider" /> <li class="divider" />
{:else} {:else}

View File

@ -1,6 +1,12 @@
import _ from 'lodash'; import _ from 'lodash';
import { getContext, setContext } from 'svelte';
import { currentDropDownMenu } from '../stores'; import { currentDropDownMenu } from '../stores';
export function registerMenu(items) {
const parentMenu = getContext('componentContextMenu');
setContext('componentContextMenu', parentMenu ? [parentMenu, items] : items);
}
export default function contextMenu(node, items) { export default function contextMenu(node, items) {
const handleContextMenu = e => { const handleContextMenu = e => {
e.preventDefault(); e.preventDefault();
@ -8,7 +14,7 @@ export default function contextMenu(node, items) {
if (items) { if (items) {
const left = e.pageX; const left = e.pageX;
const top = e.pageY; const top = e.pageY;
currentDropDownMenu.set({ left, top, items: _.isFunction(items) ? items() : items }); currentDropDownMenu.set({ left, top, items });
} }
}; };
@ -23,3 +29,21 @@ export default function contextMenu(node, items) {
}, },
}; };
} }
function doExtractMenuItems(menu, res) {
if (_.isFunction(menu)) {
doExtractMenuItems(menu(), res);
} else if (_.isArray(menu)) {
for (const item of menu) {
doExtractMenuItems(item, res);
}
} else if (_.isPlainObject(menu)) {
res.push(menu);
}
}
export function extractMenuItems(menu) {
const res = [];
doExtractMenuItems(menu, res);
return res;
}