diff --git a/packages/widgets/src/contextmenu.ts b/packages/widgets/src/contextmenu.ts index fb9b57f3e..e77d7945c 100644 --- a/packages/widgets/src/contextmenu.ts +++ b/packages/widgets/src/contextmenu.ts @@ -167,6 +167,18 @@ namespace ContextMenu { * The default rank is `Infinity`. */ rank?: number; + + /** + * Pass a selected node's dataset to an item's command. + * + * If this flag is set, when a contextmenu item is matched + * to the target of a contextmenu event, the key-value pairs + * in `target.dataset` (if present) will be added as + * properties of `item.args`. + * + * Only meaningful for items of type `command`. + */ + passDataset?: boolean; } } @@ -190,6 +202,11 @@ namespace Private { */ rank: number; + /** + * Pass a selected node's dataset to an item's command. + */ + passDataset: boolean; + /** * The tie-breaking id for the item. */ @@ -203,7 +220,8 @@ namespace Private { function createItem(options: ContextMenu.IItemOptions, id: number): IItem { let selector = validateSelector(options.selector); let rank = options.rank !== undefined ? options.rank : Infinity; - return { ...options, selector, rank, id }; + let passDataset = options.passDataset !== undefined ? options.passDataset : false; + return { ...options, selector, rank, passDataset, id }; } /** @@ -266,6 +284,11 @@ namespace Private { continue; } + // Pass the target's dataset to the matched item's args, if requested. + if (item.passDataset && 'dataset' in target) { + item.args = {...item.args, ...(target as any)['dataset']}; + } + // Add the matched item to the result for this DOM level. matches.push(item);