diff --git a/README.md b/README.md index d48ea6c..87bb9e5 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ The component is the default export of `@opencor/plot`, and is also exported as | `PlotCsvExportProgressEvent` | `{ type: 'progress'; percent: number }` | An export to CSV has made some progress: `percent` goes from 0 to 100 (an integer). | | `PlotCsvExportFinishedEvent` | `{ type: 'finished'; success: boolean }` | An export to CSV has finished, whether successfully (and the file was downloaded) or not. | | `PlotImageExportEvent` | `{ action: 'copy' \| 'export'; format: 'jpeg' \| 'png' \| 'svg' \| 'webp'; success: boolean }` | The payload of the [`imageExport`](#events) event: whether the plot was copied to the clipboard (always as PNG) or exported to an image file, in which format, and whether it worked. | -| `PlotContextMenuEvent` | `{ clientX: number; clientY: number; usingKeyboard: boolean; items: ContextMenuItem[] }` | The payload of the [`contextMenu`](#events) event: where the context menu was requested (in client coordinates), whether it was requested using the keyboard (in which case the custom menu should take the keyboard focus), and the items that our context menu would have shown. | +| `PlotContextMenuEvent` | `{ clientX: number; clientY: number; usingKeyboard: boolean; items: ContextMenuItem[] }` | The payload of the [`contextMenu`](#events) event: where the context menu was requested (in client coordinates), whether it was requested using the keyboard (in which case the custom menu should take the keyboard focus), and the items that our context menu would have shown. These items reflect the plot as it was when the context menu was requested and don't get updated if the data changes while the custom context menu is shown, but their commands always act on the plot as it is when they are chosen. | | `ContextMenuItem` | `ContextMenuAction \| ContextMenuSeparator` | An item of a context menu. | | `ContextMenuAction` | `{ label: string; icon?: IconName; checked?: boolean; command?: () => void; items?: ContextMenuItem[] }` | An action: its `label`, its `icon`, whether it is checked (only for a checkable action, e.g. to make an axis logarithmic), the `command` to call when it is chosen, and its submenu `items`, if any. | | `ContextMenuSeparator` | `{ separator: true }` | A separator. | diff --git a/package.json b/package.json index 05608ac..a3f3043 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ ], "type": "module", "types": "dist/index.d.ts", - "version": "0.20261125.4", + "version": "0.20261125.5", "exports": { ".": { "types": "./dist/index.d.ts", diff --git a/src/widgets/ContextMenuWidget.vue b/src/widgets/ContextMenuWidget.vue index 5afc71c..ffc7359 100644 --- a/src/widgets/ContextMenuWidget.vue +++ b/src/widgets/ContextMenuWidget.vue @@ -414,6 +414,9 @@ const onWindowBlurOrResize = (): void => { const CONTEXT_MENU_SHOW_EVENT = 'opencor-context-menu-show'; +let anchorX = 0; +let anchorY = 0; + const contextMenuId = Symbol('ContextMenuWidget'); const show = async (event: MouseEvent, usingKeyboard: boolean = false): Promise => { @@ -434,12 +437,15 @@ const show = async (event: MouseEvent, usingKeyboard: boolean = false): Promise< window.addEventListener('resize', onWindowBlurOrResize); } + anchorX = event.clientX; + anchorY = event.clientY; + const level: Level = { items: props.items, parentIndex: -1, focusedIndex: -1, - left: event.clientX, - top: event.clientY + left: anchorX, + top: anchorY }; if (usingKeyboard) { @@ -450,10 +456,93 @@ const show = async (event: MouseEvent, usingKeyboard: boolean = false): Promise< await vue.nextTick(); - positionLevel(0, event.clientX, event.clientY); + positionLevel(0, anchorX, anchorY); focusLevel(0); }; +// Updating of our context menu while it is shown. +// Note: our items may change while our context menu is shown (e.g., the label of an item may depend on some data that +// gets updated in the meantime), in which case we update our levels. Our items may get recreated, so we identify +// an item by its label, but only if no other item of its level has the same label, both before and after the +// update: the focused item of a level remains focused (even if it has moved) as long as it can be identified, and +// so does an open submenu as long as its parent item can be identified and still has a submenu. Otherwise, that +// item is no longer focused (so that choosing the focused item can never run another action than the one that was +// focused) and that submenu gets closed. We then position our levels again since their size may have changed. + +const actionIndexes = (items: ContextMenuItem[], label: string): number[] => { + // Return the indexes of the actions with the given label in the given items. + + return items.flatMap((item: ContextMenuItem, index: number) => + !isSeparator(item) && item.label === label ? [index] : [] + ); +}; + +const matchingActionIndex = (items: ContextMenuItem[], oldItems: ContextMenuItem[], oldIndex: number): number => { + // Return the index, in the given items, of the action that has the same label as the action at the given index in the + // given old items, or -1 if there is no such action or if that label is not unique in the given items or old items + // (in which case we can't tell which action is which). + + const oldItem = oldItems[oldIndex]; + + if (!oldItem || isSeparator(oldItem) || actionIndexes(oldItems, oldItem.label).length !== 1) { + return -1; + } + + const indexes = actionIndexes(items, oldItem.label); + + return indexes.length === 1 ? (indexes[0] as number) : -1; +}; + +const updateLevels = async (items: ContextMenuItem[]): Promise => { + if (!levels.value.length) { + return; + } + + const newLevels: Level[] = []; + let levelItems: ContextMenuItem[] = items; + let parentIndex = -1; + + for (const [levelIndex, level] of levels.value.entries()) { + newLevels.push({ + ...level, + items: levelItems, + parentIndex, + focusedIndex: matchingActionIndex(levelItems, level.items, level.focusedIndex) + }); + + const nextLevel = levels.value[levelIndex + 1]; + + if (!nextLevel) { + break; + } + + parentIndex = matchingActionIndex(levelItems, level.items, nextLevel.parentIndex); + + const parentItem: ContextMenuItem | undefined = levelItems[parentIndex]; + + if (!parentItem || isSeparator(parentItem) || !parentItem.items) { + break; + } + + levelItems = parentItem.items; + } + + levels.value = newLevels; + + await vue.nextTick(); + + newLevels.forEach((_level: Level, levelIndex: number) => { + positionLevel(levelIndex, anchorX, anchorY); + }); +}; + +vue.watch( + () => props.items, + (items: ContextMenuItem[]) => { + void updateLevels(items); + } +); + const hide = (): void => { if (!levels.value.length) { return; @@ -487,7 +576,7 @@ vue.onUnmounted(() => { hide(); }); -defineExpose({ show }); +defineExpose({ show, hide });