diff --git a/README.md b/README.md index 2429825..289bc64 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Contents: - [Events](#events) - [Methods](#methods) - [Interaction](#interaction) + - [Context menu items](#context-menu-items) - [Theming](#theming) - [Development](#development) - [Benchmarking](#benchmarking) @@ -88,7 +89,7 @@ The component is the default export of `@opencor/plot`, and is also exported as | `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. 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 \| ContextMenuHeader` | 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. | +| `ContextMenuAction` | `{ id?: string; label: string; icon?: IconName; checked?: boolean; command?: () => void; items?: ContextMenuItem[] }` | An action: its `id` (so that it can be found, see [Context menu items](#context-menu-items)), 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. | | `ContextMenuHeader` | `{ header: string }` | A header that can't be chosen, e.g., the full name of a trace when the context menu is requested from its legend item. It can contain a little HTML (see [Data](#data)). | | `IconName` | `'angle-right' \| 'arrows-h' \| 'arrows-v' \| 'chart-line' \| 'check' \| 'copy' \| 'download' \| 'eye' \| 'image' \| 'pencil' \| 'refresh' \| 'search-minus' \| 'search-plus'` | The name of an icon (from [PrimeIcons](https://primevue.org/icons/)). | @@ -227,6 +228,7 @@ To keep track of the ranges of the axes of a plot (e.g., to restore them later), | `yAxisSync` | [`PlotGroup`](#types) | | The group of plots whose Y axis is to be synchronised with this plot's, i.e. which share the range and the scale of their Y axis, so that zooming, panning, or resetting the zoom of one of them does the same to the others, as does making its Y axis logarithmic (or not). Their Y axes always show the same range, which, until one of them gets zoomed or panned, fits the data of all of them. | | `appendTo` | `HTMLElement \| string` | `body` | Where to append our overlays (context menu, hover tooltip, etc.). | | `customContextMenu` | `boolean` | `false` | Whether to emit a [`contextMenu`](#events) event rather than show our own context menu. To have no context menu at all, set it and don't handle the `contextMenu` event. | +| `contextMenuItems` | `(items: ContextMenuItem[]) => ContextMenuItem[]` | | A function that customises the items of the context menu, i.e. that is given the plot's items (a new array each time) and returns the items to show, e.g., to add an item that lets the user choose whether the X axis of the plot is synchronised with that of other plots (by setting the [`xAxisSync`](#props) prop accordingly). It gets called whenever the items are built (e.g., when the context menu is requested), so the items that it adds can reflect the current state of the app (e.g., whether an item is checked). Its items are also the `items` of the [`contextMenu`](#events) event. The plot's actions have an `id` (see [`ContextMenuAction`](#types)), so that they can be found whatever their label (see [Context menu items](#context-menu-items)). | `levelOfDetail` | `boolean` | `true` | Whether to only go through the points that are needed for a trace to look the same when drawing it (see [Large and live data](#large-and-live-data)). Hovering a trace whose X values are sorted always does. Only meant to be disabled to see how much it helps. | | `live` | `boolean` | `false` | Whether the data is live (e.g., while a simulation is running), in which case the last point of each visible trace is marked. | @@ -267,10 +269,51 @@ Hovering shows the value of each visible trace at the X value under the mouse po With synchronised axes (see [Several plots](#several-plots)), zooming, panning, resetting the zoom, editing the range of an axis, and making an axis logarithmic (or not) apply to all the plots of the group of that axis, be it done with the mouse, by touch, with the keyboard, or from the context menu. -The context menu can zoom in and out, reset the zoom, edit the range of an axis, make an axis logarithmic, copy the plot to the clipboard, export it as JPEG, PNG, SVG, or WebP, and export its data to CSV. When requested from a legend item, it can also show only that trace (or all the traces again), or show or hide it if it is the only trace. +The context menu can zoom in and out, reset the zoom, edit the range of an axis, make an axis logarithmic, copy the plot to the clipboard, export it as JPEG, PNG, SVG, or WebP, and export its data to CSV, and an app can customise it (see [`contextMenuItems`](#props)). When requested from a legend item, it can also show only that trace (or all the traces again), or show or hide it if it is the only trace. Copied and exported images always use the light colours (white background, dark text), whatever the current theme, since they are typically used in light documents. +### Context menu items + +The items of the context menu can be customised using the [`contextMenuItems`](#props) prop, e.g., to add an item that lets the user choose whether the X axes of some plots are synchronised, right before the items that make an axis logarithmic (and separated from them): + +```ts +const contextMenuItems = (items: ContextMenuItem[]): ContextMenuItem[] => { + const index = items.findIndex((item) => 'id' in item && item.id === 'logarithmicXAxis'); + + items.splice( + index, + 0, + { + icon: 'arrows-h', + label: 'Linked X Axes', + checked: linkedXAxes.value, + command: () => { + linkedXAxes.value = !linkedXAxes.value; + } + }, + { + separator: true + } + ); + + return items; +}; +``` + +The plot's actions have an `id`, so that they can be found whatever their label: + +| ID | Action | +|---|---| +| `traceVisibility` | Show only a trace (or all the traces again), or show or hide it if it is the only trace (only when the context menu is requested from a legend item, after a header with the name of its trace). | +| `zoomIn`, `zoomOut` | Zoom in and out. | +| `resetZoom` | Reset the zoom. | +| `editAxisRange` | Edit the range of an axis, with `xAxisMinimum`, `xAxisMaximum`, `yAxisMinimum`, and `yAxisMaximum` in its submenu. | +| `logarithmicXAxis`, `logarithmicYAxis` | Make an axis logarithmic (or not). | +| `copyToClipboard` | Copy the plot to the clipboard. | +| `exportPlot` | Export the plot, with `exportPlotAsJpeg`, `exportPlotAsPng`, `exportPlotAsSvg`, and `exportPlotAsWebp` in its submenu. | +| `exportDataToCsv` | Export the data to CSV. | + ### Theming Set any of these CSS variables on an ancestor of the component. Each has a light and a dark default, based on [PrimeVue](https://primevue.org/)'s Aura theme. They don't affect copied and exported images, which always use the light defaults: diff --git a/demo/App.vue b/demo/App.vue index b033f7d..1d78563 100644 --- a/demo/App.vue +++ b/demo/App.vue @@ -9,6 +9,7 @@ :live="liveRunning" :showLegend="showLegend" :customContextMenu="customContextMenu" + :contextMenuItems="contextMenuItems" :xAxisAlign="alignedXAxes ? ALIGN_GROUP : undefined" :xAxisSync="linkedXAxes ? SYNC_GROUP : undefined" :yAxisAlign="alignedYAxes ? ALIGN_GROUP : undefined" @@ -528,6 +529,39 @@ const ALIGN_GROUP = Symbol('demo'); const SYNC_GROUP = Symbol('demo'); +// Customise the context menu of our instances, so that the user can choose whether their X and/or Y axes are +// synchronised from there too, right before the items that make their axes logarithmic (and separated from them). + +const contextMenuItems = (items: ContextMenuItem[]): ContextMenuItem[] => { + const index = items.findIndex((item: ContextMenuItem) => 'id' in item && item.id === 'logarithmicXAxis'); + + items.splice( + index, + 0, + { + icon: 'arrows-h', + label: 'Linked X Axes', + checked: linkedXAxes.value, + command: () => { + linkedXAxes.value = !linkedXAxes.value; + } + }, + { + icon: 'arrows-v', + label: 'Linked Y Axes', + checked: linkedYAxes.value, + command: () => { + linkedYAxes.value = !linkedYAxes.value; + } + }, + { + separator: true + } + ); + + return items; +}; + // Our frame rate, and the number of points that our instances actually draw compared to their number of points (i.e. // without level of detail), which we update twice a second (rather than reactively, which would be costly with many // instances). diff --git a/package.json b/package.json index c0aac56..879d662 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ ], "type": "module", "types": "dist/index.d.ts", - "version": "0.20261202.1", + "version": "0.20261202.2", "exports": { ".": { "types": "./dist/index.d.ts", diff --git a/src/common/syncUtils.ts b/src/common/syncUtils.ts index 83678f9..6852c5f 100644 --- a/src/common/syncUtils.ts +++ b/src/common/syncUtils.ts @@ -164,7 +164,7 @@ export class AxisSyncer { } groupHover(): SyncHover | undefined { - // Return where another plot of our group is hovered, if any, e.g. so that our plot can show its hover guide and + // Return where another plot of our group is hovered, if any, e.g., so that our plot can show its hover guide and // tooltip there too when it joins a group while another plot of that group is hovered. const group = this.group(); diff --git a/src/widgets/ContextMenuWidget.vue b/src/widgets/ContextMenuWidget.vue index 9911b3a..54cca5a 100644 --- a/src/widgets/ContextMenuWidget.vue +++ b/src/widgets/ContextMenuWidget.vue @@ -77,6 +77,11 @@ import IconWidget from './IconWidget.vue'; /** An action of a context menu. */ export interface ContextMenuAction { + /** + * The ID of the action, if any, so that it can be found (e.g., `'logarithmicXAxis'` for one of the actions of a plot, + * see the `contextMenuItems` prop of `OpenCORPlot`). + */ + id?: string; /** The label of the action. */ label: string; /** The icon of the action, if any. */ diff --git a/src/widgets/PlotWidget.vue b/src/widgets/PlotWidget.vue index f804b48..b30fb3d 100644 --- a/src/widgets/PlotWidget.vue +++ b/src/widgets/PlotWidget.vue @@ -154,7 +154,7 @@ - +
@@ -454,6 +454,19 @@ const props = withDefaults( * context menu at all, set it and don't handle the `contextMenu` event. */ customContextMenu?: boolean; + /** + * A function that customises the items of our context menu, i.e. that is given our items and returns the items to + * show, e.g., to add an item that lets the user choose whether the X axis of the plot is synchronised with that of + * other plots (by setting the `xAxisSync` prop accordingly). Our actions have an ID, so that they can be found + * whatever their label (see `ContextMenuAction`): `'traceVisibility'` (only when requested from a legend item), + * `'zoomIn'`, `'zoomOut'`, `'resetZoom'`, `'editAxisRange'` (with `'xAxisMinimum'`, `'xAxisMaximum'`, + * `'yAxisMinimum'`, and `'yAxisMaximum'`), `'logarithmicXAxis'`, `'logarithmicYAxis'`, `'copyToClipboard'`, + * `'exportPlot'` (with `'exportPlotAsJpeg'`, `'exportPlotAsPng'`, `'exportPlotAsSvg'`, and `'exportPlotAsWebp'`), + * and `'exportDataToCsv'`. It gets called whenever our items are built (e.g., when our context menu is requested), + * so the items that it adds can reflect the current state of the app (e.g., whether an item is checked). Its items + * are also those of the `contextMenu` event. + */ + contextMenuItems?: (items: ContextMenuItem[]) => ContextMenuItem[]; /** * Whether to only go through the points that are needed for a trace to look the same when drawing it, and to only * draw as many segments as are needed for it to look the same (`true` by default). Hovering a trace whose X values @@ -765,8 +778,8 @@ const marginsAligners: Record = { y: new MarginsAligner('y', onSharedMarginsChanged) }; -// Whether our axes are logarithmic, which can be changed using our context menu (see contextMenuItems and Logarithmic -// axes below). +// Whether our axes are logarithmic, which can be changed using our context menu (see menuItems and Logarithmic axes +// below). const logAxes = vue.ref>({ x: false, y: false }); @@ -1502,6 +1515,7 @@ const legendTraceMenuItem = (key: string, index: number): ContextMenuItem => { // still exists) and to the visibility of our traces at that time. return { + id: 'traceVisibility', icon: 'eye', label: legendTraceAction(traceVisibilities.value, props.data.traces.length, index).label, command: () => { @@ -1525,111 +1539,135 @@ const legendTraceMenuHeader = (index: number): ContextMenuItem[] => { return name ? [{ header: name }] : []; }; -const contextMenuItems = vue.computed(() => [ - ...(contextMenuLegendKey.value === null || contextMenuLegendTraceIndex.value === -1 - ? [] - : [ - ...legendTraceMenuHeader(contextMenuLegendTraceIndex.value), - legendTraceMenuItem(contextMenuLegendKey.value, contextMenuLegendTraceIndex.value), +const menuItems = vue.computed(() => { + // Our items, customised by the app, if needed (see the contextMenuItems prop). + // Note: the app gets a new array of items each time, so that it can modify it. + + const items: ContextMenuItem[] = [ + ...(contextMenuLegendKey.value === null || contextMenuLegendTraceIndex.value === -1 + ? [] + : [ + ...legendTraceMenuHeader(contextMenuLegendTraceIndex.value), + legendTraceMenuItem(contextMenuLegendKey.value, contextMenuLegendTraceIndex.value), + { + separator: true as const + } + ]), + { + id: 'zoomIn', + icon: 'search-plus', + label: 'Zoom In', + command: () => zoomIn() + }, + { + id: 'zoomOut', + icon: 'search-minus', + label: 'Zoom Out', + command: () => zoomOut() + }, + { + id: 'resetZoom', + icon: 'refresh', + label: 'Reset Zoom', + command: () => resetZoom() + }, + { + id: 'editAxisRange', + icon: 'pencil', + label: 'Edit Axis Range', + items: [ + { + id: 'xAxisMinimum', + label: 'X Axis Minimum', + command: () => openRangeEditor('w') + }, + { + id: 'xAxisMaximum', + label: 'X Axis Maximum', + command: () => openRangeEditor('e') + }, { - separator: true as const + id: 'yAxisMinimum', + label: 'Y Axis Minimum', + command: () => openRangeEditor('s') + }, + { + id: 'yAxisMaximum', + label: 'Y Axis Maximum', + command: () => openRangeEditor('n') } - ]), - { - icon: 'search-plus', - label: 'Zoom In', - command: () => zoomIn() - }, - { - icon: 'search-minus', - label: 'Zoom Out', - command: () => zoomOut() - }, - { - icon: 'refresh', - label: 'Reset Zoom', - command: () => resetZoom() - }, - { - icon: 'pencil', - label: 'Edit Axis Range', - items: [ - { - label: 'X Axis Minimum', - command: () => openRangeEditor('w') - }, - { - label: 'X Axis Maximum', - command: () => openRangeEditor('e') - }, - { - label: 'Y Axis Minimum', - command: () => openRangeEditor('s') - }, - { - label: 'Y Axis Maximum', - command: () => openRangeEditor('n') - } - ] - }, - { - separator: true - }, - { - icon: 'arrows-h', - label: 'Logarithmic X Axis', - checked: logAxes.value.x, - command: () => toggleLogAxis('x') - }, - { - icon: 'arrows-v', - label: 'Logarithmic Y Axis', - checked: logAxes.value.y, - command: () => toggleLogAxis('y') - }, - { - separator: true - }, - { - icon: 'copy', - label: 'Copy to Clipboard', - command: () => copyToClipboard() - }, - { - icon: 'chart-line', - label: 'Export Plot', - items: [ - { - icon: 'image', - label: 'JPEG', - command: () => exportToImage('jpeg') - }, - { - icon: 'image', - label: 'PNG', - command: () => exportToImage('png') - }, - { - icon: 'image', - label: 'SVG', - command: () => exportToImage('svg') - }, - { - icon: 'image', - label: 'WebP', - command: () => exportToImage('webp') - } - ] - }, - { - separator: true - }, - { - label: 'Export Data to CSV', - icon: 'download', - command: () => exportToCsv() - } -]); + ] + }, + { + separator: true + }, + { + id: 'logarithmicXAxis', + icon: 'arrows-h', + label: 'Logarithmic X Axis', + checked: logAxes.value.x, + command: () => toggleLogAxis('x') + }, + { + id: 'logarithmicYAxis', + icon: 'arrows-v', + label: 'Logarithmic Y Axis', + checked: logAxes.value.y, + command: () => toggleLogAxis('y') + }, + { + separator: true + }, + { + id: 'copyToClipboard', + icon: 'copy', + label: 'Copy to Clipboard', + command: () => copyToClipboard() + }, + { + id: 'exportPlot', + icon: 'chart-line', + label: 'Export Plot', + items: [ + { + id: 'exportPlotAsJpeg', + icon: 'image', + label: 'JPEG', + command: () => exportToImage('jpeg') + }, + { + id: 'exportPlotAsPng', + icon: 'image', + label: 'PNG', + command: () => exportToImage('png') + }, + { + id: 'exportPlotAsSvg', + icon: 'image', + label: 'SVG', + command: () => exportToImage('svg') + }, + { + id: 'exportPlotAsWebp', + icon: 'image', + label: 'WebP', + command: () => exportToImage('webp') + } + ] + }, + { + separator: true + }, + { + id: 'exportDataToCsv', + icon: 'download', + label: 'Export Data to CSV', + command: () => exportToCsv() + } + ]; + + return props.contextMenuItems ? props.contextMenuItems(items) : items; +}); let contextMenuShownUsingKeyboard = false; let contextMenuFocusTarget: HTMLElement | null = null; @@ -1643,11 +1681,11 @@ const showContextMenu = (event: MouseEvent): void => { clientX: event.clientX, clientY: event.clientY, usingKeyboard: contextMenuShownUsingKeyboard, - items: contextMenuItems.value + items: menuItems.value }); } else { // Note: our context menu items may just have changed (e.g., if our context menu is requested from a legend item, - // see contextMenuItems), so we wait for our context menu to have been updated before showing it. + // see menuItems), so we wait for our context menu to have been updated before showing it. const usingKeyboard = contextMenuShownUsingKeyboard; @@ -1707,8 +1745,8 @@ const requestContextMenu = (event: MouseEvent, target: EventTarget | null = even contextMenuShownUsingKeyboard = false; - // Keep track of whether our context menu was requested from a legend item (see contextMenuItems), and of where the - // keyboard focus should go once our context menu has been hidden (see onContextMenuHide()). + // Keep track of whether our context menu was requested from a legend item (see menuItems), and of where the keyboard + // focus should go once our context menu has been hidden (see onContextMenuHide()). const legendItemElement = target instanceof Element ? target.closest('[data-legend-index]') : null; @@ -3140,7 +3178,7 @@ const updateRangesAsync = (): void => { }); }; -// Logarithmic axis toggling (see contextMenuItems). +// Logarithmic axis toggling (see menuItems). const toggleLogAxis = (axisKey: AxisKey): void => { // Toggle whether the given axis is logarithmic, keeping its zoom range, if any and if possible (i.e. unless it has a @@ -4548,7 +4586,7 @@ const closeRangeEditor = (apply: boolean): void => { // Note: if our range editor has the keyboard focus (e.g., it was closed using Enter or Escape, rather than by giving // the keyboard focus to something else), then we give it to our plot (since our range editor is about to be // removed and the keyboard focus would otherwise be lost), e.g., so that our range editor can be opened and - // closed using the keyboard (see contextMenuItems). + // closed using the keyboard (see menuItems). const text = rangeEditorRef.value?.textContent ?? ''; const hadFocus = !!rangeEditorRef.value?.contains(document.activeElement);