diff --git a/package.json b/package.json index fb25dd5..8b49c7e 100644 --- a/package.json +++ b/package.json @@ -32,12 +32,13 @@ ], "type": "module", "types": "dist/index.d.ts", - "version": "0.20261125.1", + "version": "0.20261125.2", "exports": { ".": { "types": "./dist/index.d.ts", - "import": "./dist/opencor-plot.es.js" + "default": "./dist/opencor-plot.es.js" }, + "./package.json": "./package.json", "./style.css": "./dist/opencor-plot.css" }, "scripts": { diff --git a/scripts/declarations.clean.ts b/scripts/declarations.clean.ts index 10fd1db..d06d659 100644 --- a/scripts/declarations.clean.ts +++ b/scripts/declarations.clean.ts @@ -2,7 +2,8 @@ // Clean the type declarations that `vue-tsc` emitted for our library (see tsconfig.lib.types.json), i.e.: // - remove their CSS side-effect imports (e.g., `import '../assets/theme.css';`); and -// - remove the type declarations that are not reachable from our entry point (i.e. dist/index.d.ts). +// - remove the type declarations that are not reachable from our entry point (i.e. dist/index.d.ts); and +// - give an explicit file extension to the relative imports of the remaining ones (e.g., './src/common/icons.js'). // Note #1: CSS side-effect imports are kept by `vue-tsc` when it emits our type declarations, but they are of no use // there (our CSS is bundled into opencor-plot.css) and some of them don't even exist in our package (e.g., // '../assets/theme.css'). So, a project that checks the type declarations of its dependencies (i.e. with @@ -10,6 +11,13 @@ // to type check with TypeScript 6 or later (since noUncheckedSideEffectImports is then true by default). // Note #2: `vue-tsc` emits a type declaration for each of our source files, but only those that are reachable from our // entry point are needed (e.g., not the one for our level of detail utilities). +// Note #3: `vue-tsc` keeps our relative imports as they are in our source files (e.g., './src/common/icons'), but a +// project that uses the node16 or nodenext module resolution requires them to have an explicit file extension +// (since we are an ES module). Without one, such a project would fail to resolve our type declarations, so our +// types would silently become `any` (with skipLibCheck set to true) or fail to type check (with it set to +// false). So, we make each relative import refer to the JavaScript file that its type declaration describes +// (e.g., './src/common/icons.js' for src/common/icons.d.ts and './src/widgets/PlotWidget.vue.js' for +// src/widgets/PlotWidget.vue.d.ts), which is what TypeScript expects whatever the module resolution. import fs from 'node:fs'; import path from 'node:path'; @@ -78,6 +86,25 @@ for (const file of allFiles) { } } +// Give an explicit file extension to the relative imports of our remaining type declarations (see Note #3 above). + +for (const file of reachableFiles) { + const contents = fs.readFileSync(file, 'utf8'); + const cleanedContents = contents.replace(RELATIVE_SPECIFIER, (match, specifier: string) => { + const javaScriptFile = path + .relative(path.dirname(file), declarationFile(file, specifier)) + .split(path.sep) + .join('/') + .replace(/\.d\.ts$/, '.js'); + + return match.replace(specifier, javaScriptFile.startsWith('.') ? javaScriptFile : `./${javaScriptFile}`); + }); + + if (cleanedContents !== contents) { + fs.writeFileSync(file, cleanedContents); + } +} + // Remove the directories that are now empty, from the deepest ones. const directories = fs diff --git a/src/common/icons.ts b/src/common/icons.ts index 0b9ff74..4bfbb26 100644 --- a/src/common/icons.ts +++ b/src/common/icons.ts @@ -65,4 +65,5 @@ export const ICONS = { ] } as const satisfies Record; +/** The name of an icon (from PrimeIcons, https://primevue.org/icons/). */ export type IconName = keyof typeof ICONS; diff --git a/src/widgets/ContextMenuWidget.vue b/src/widgets/ContextMenuWidget.vue index 0213040..5afc71c 100644 --- a/src/widgets/ContextMenuWidget.vue +++ b/src/widgets/ContextMenuWidget.vue @@ -67,18 +67,29 @@ import IconWidget from './IconWidget.vue'; // Note #2: only one context menu should be shown at any given time, so we let our other context menus know when we are // about to be shown, and we hide ourselves when another context menu is about to be shown. +/** An action of a context menu. */ + export interface ContextMenuAction { + /** The label of the action. */ label: string; + /** The icon of the action, if any. */ icon?: IconName; + /** Whether the action is checked, only for a checkable action (e.g., to make an axis logarithmic). */ checked?: boolean; + /** The command to call when the action is chosen, if any. */ command?: () => void; + /** The items of the submenu of the action, if any. */ items?: ContextMenuItem[]; } +/** A separator of a context menu. */ + export interface ContextMenuSeparator { separator: true; } +/** An item of a context menu, i.e. an action or a separator. */ + export type ContextMenuItem = ContextMenuAction | ContextMenuSeparator; const props = defineProps<{ diff --git a/src/widgets/PlotWidget.vue b/src/widgets/PlotWidget.vue index d4a2209..5e0e57e 100644 --- a/src/widgets/PlotWidget.vue +++ b/src/widgets/PlotWidget.vue @@ -177,103 +177,186 @@ import '../assets/theme.css'; import ContextMenuWidget, { type ContextMenuItem } from './ContextMenuWidget.vue'; import PlotLivePointsWidget, { type PlotLivePoint } from './PlotLivePointsWidget.vue'; +/** + * A trace to plot, which can be a function of X (e.g., against time) or not (e.g., a phase plot). A trace with fewer + * than 20 points is also drawn with markers. + */ + export interface PlotTrace { + /** + * The name of the trace, as shown in the legend, the hover tooltip, and the CSV export. It can contain a little HTML + * (`b`, `br`, `code`, `em`, `i`, `strong`, `sub`, and `sup`). Anything else is removed. + */ name: string; + /** + * An optional identifier that keeps the visibility of the trace when the data is updated. Otherwise, the trace is + * identified by its `name`, `xValue`, `yValue`, and `runLabel` (and, among traces that have the same ones, by their + * order), or by its position if it doesn't have a `name`, `xValue`, and `yValue`. + */ traceId?: string; + /** What the X values are (e.g., the name of a model variable), used in the CSV export. */ xValue: string; + /** + * The X values of the trace. A point with a non-finite X value (e.g., `NaN`) is a gap in the trace. For live data, + * see {@link https://github.com/opencor/plot#large-and-live-data | Large and live data}. + */ x: Float64Array; + /** What the Y values are (e.g., the name of a model variable), used in the CSV export. */ yValue: string; + /** + * The Y values of the trace. A point with a non-finite Y value (e.g., `NaN`) is a gap in the trace. For live data, + * see {@link https://github.com/opencor/plot#large-and-live-data | Large and live data}. + */ y: Float64Array; + /** The colour of the trace, as a CSS colour (e.g., `'#1f77b4'`). */ color: string; + /** The optional rank of the trace in the legend (lower ranks first, 1000 by default). */ legendRank?: number; + /** An optional label that tells apart the X column of the trace from those of other runs in the CSV export. */ runLabel?: string; } +/** + * The data to plot. It is not watched, so to update it, give the plot a new `PlotData` object (see + * {@link https://github.com/opencor/plot#large-and-live-data | Large and live data}). + */ + export interface PlotData { + /** The title of the X axis. It can contain a little HTML (see `PlotTrace.name`). */ xAxisTitle?: string; + /** The title of the Y axis. It can contain a little HTML (see `PlotTrace.name`). */ yAxisTitle?: string; + /** The range of the X axis when the zoom is reset. By default, it fits the data. */ xAxisRange?: [number, number]; + /** The range of the Y axis when the zoom is reset. By default, it fits the data. */ yAxisRange?: [number, number]; + /** The traces to plot. */ traces: PlotTrace[]; } +/** + * The left and right margins of a plot, i.e. the space taken by its Y axis and legend. To line up several plots, pass + * the largest left and right margins that their `marginsUpdated` events gave to the `margins` prop of each of them. + */ + export interface PlotMargins { + /** The left margin (in pixels), i.e. the space taken by the Y axis and its title. */ left: number; + /** The right margin (in pixels), i.e. the space taken by the legend. */ right: number; } -// CSV export events, which can be used to show the progress of an export to CSV (see exportToCsv()). +/** The payload of the `csvExport` event when an export to CSV has started. */ export interface PlotCsvExportStartedEvent { type: 'started'; } +/** The payload of the `csvExport` event when an export to CSV has made some progress. */ + export interface PlotCsvExportProgressEvent { type: 'progress'; + /** How far the export to CSV has got, from 0 to 100 (an integer). */ percent: number; } +/** The payload of the `csvExport` event when an export to CSV has finished. */ + export interface PlotCsvExportFinishedEvent { type: 'finished'; + /** Whether the export to CSV was successful (and the file was downloaded). */ success: boolean; } +/** + * The payload of the `csvExport` event, which can be used to show the progress of an export to CSV. Use its `type` to + * tell which one it is. + */ + export type PlotCsvExportEvent = PlotCsvExportStartedEvent | PlotCsvExportProgressEvent | PlotCsvExportFinishedEvent; -// Our theme, i.e. whether we should use whatever mode the system uses, light mode, or dark mode. +/** The value of the `theme` prop, i.e. whether to use light or dark mode, or whatever the system uses. */ export type PlotTheme = 'system' | 'light' | 'dark'; -// The number of points of our visible traces and the number of them that actually got drawn the last time our plot got -// drawn, i.e. how much drawing only the points that are needed for our traces to look the same helps (see -// pointCounts()). This is mainly meant for testing and benchmarking. +/** + * The value returned by the `pointCounts()` method, i.e. how much drawing only the points that are needed for the + * traces to look the same helps. Mainly meant for testing and benchmarking. + */ export interface PlotPointCounts { - nbOfPoints: number; // Number of points of our visible traces. - nbOfDrawnPoints: number; // Number of those points that actually got drawn (see pointCounts()). + /** The number of points of the visible traces. */ + nbOfPoints: number; + /** The number of those points that got drawn the last time the plot got drawn. */ + nbOfDrawnPoints: number; } -// The number of bytes that we use for our own purposes, i.e. not counting our data (which belongs to our consumer), see -// memoryUsage(). This is mainly meant for testing and benchmarking. +/** + * The value returned by the `memoryUsage()` method, i.e. the memory (in bytes) used by the plot itself, not counting + * its data. Mainly meant for testing and benchmarking. + */ export interface PlotMemoryUsage { - levelOfDetail: number; // Our levels of detail and the buffers of their selected points. - logValues: number; // The decimal logarithm of our values along our logarithmic axes (see plotValues()). + /** The memory used by the levels of detail of the traces and the buffers of their selected points. */ + levelOfDetail: number; + /** The memory used by the decimal logarithm of the values along logarithmic axes. */ + logValues: number; } -// Context menu event, which is emitted rather than showing our context menu if we are to use a custom context menu (see -// customContextMenu), so that it can be shown instead. Its items are those of our context menu (including their state, -// e.g. whether an axis is logarithmic, and their command, e.g. to zoom in around where our context menu was requested). -// Note: our context menu can be requested using the keyboard (i.e. Shift+F10 or the context menu key), in which case -// our custom context menu should get the keyboard focus, so that it can be used with the keyboard straight away. -// Whether our context menu was requested using the mouse or the keyboard, contextMenuHidden() should be called -// once our custom context menu has been hidden, so that our plot can get the keyboard focus back, if needed. +/** + * The payload of the `contextMenu` event, which is emitted rather than showing our context menu when the + * `customContextMenu` prop is set, so that a custom context menu can be shown instead. Once it has been hidden, call + * the `contextMenuHidden()` method, so that the plot can get the keyboard focus back, if needed. + */ export interface PlotContextMenuEvent { + /** The X coordinate (in client coordinates) where the context menu was requested. */ clientX: number; + /** The Y coordinate (in client coordinates) where the context menu was requested. */ clientY: number; + /** + * Whether the context menu was requested using the keyboard (i.e. Shift+F10 or the context menu key), in which case + * the custom context menu should get the keyboard focus, so that it can be used with the keyboard straight away. + */ usingKeyboard: boolean; + /** + * The items of our context menu, including their state (e.g., whether an axis is logarithmic) and their command + * (e.g., to zoom in around where the context menu was requested). + */ items: ContextMenuItem[]; } -// Note #1: appendTo is where our overlays (i.e. our context menu, hover tooltip, and drag cursor) get appended, which -// is our document's body by default. -// Note #2: customContextMenu is whether we should emit a contextMenu event rather than show our own context menu (see -// PlotContextMenuEvent). -// Note #3: levelOfDetail is whether we should only go through the points that are needed for our traces to look the -// same (see linearPathPoints()) and only draw as many segments as are needed for them to look the same (see -// Note #6 of our level of detail), which is what we should normally do. It can, however, be disabled to see -// (e.g., in our demo) how much it helps. - const props = withDefaults( defineProps<{ + /** The data to plot (see `PlotData`). */ data: PlotData; + /** Margins shared with other plots, so that their plotting areas line up (see the `marginsUpdated` event). */ margins?: PlotMargins; + /** + * Whether to show the legend (`true` by default). The legend takes at most 40% of the width of the plot (with long + * trace names truncated, and shown in full when hovered), and is hidden if the plot is too narrow for it to be + * useful. + */ showLegend?: boolean; + /** Whether to use light or dark mode, or whatever the system uses (`'system'` by default). */ theme?: PlotTheme; + /** Where to append our overlays, i.e. our context menu, hover tooltip, and drag cursor (`'body'` by default). */ appendTo?: HTMLElement | string; + /** + * Whether to emit a `contextMenu` event rather than show our own context menu (`false` by default). To have no + * context menu at all, set it and don't handle the `contextMenu` event. + */ customContextMenu?: boolean; + /** + * 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 + * are sorted always does the former. Only meant to be disabled to see how much it helps. + */ levelOfDetail?: boolean; + /** + * Whether the data is live (e.g., while a simulation is running), in which case the last point of each visible + * trace is marked, making its latest value easy to spot (`false` by default). + */ live?: boolean; }>(), { @@ -285,9 +368,21 @@ const props = withDefaults( } ); +// Note: the JSDoc comments of our events don't make it into our type declarations (since Vue transforms their type), +// which is why those of their payload types (e.g., PlotCsvExportEvent) also describe them. + const emit = defineEmits<{ + /** + * Only with the `customContextMenu` prop: the context menu was requested, so that a custom context menu can be shown + * instead of ours (see `PlotContextMenuEvent`). + */ contextMenu: [event: PlotContextMenuEvent]; + /** An export to CSV has started, made some progress, or finished, so that its progress can be shown. */ csvExport: [event: PlotCsvExportEvent]; + /** + * The margins that the plot needs have changed. To line up several plots, pass the largest left and right margins of + * all of them to the `margins` prop of each of them. + */ marginsUpdated: [newMargins: PlotMargins]; }>(); @@ -458,7 +553,31 @@ const memoryUsage = (): PlotMemoryUsage => { return res; }; -defineExpose({ +// Note: we give the type of our methods explicitly, so that their JSDoc comments make it into our type declarations. + +/** The methods of the plot, which are available through a template ref. */ + +interface PlotMethods { + /** + * Tell the plot that a custom context menu (see the `contextMenu` event) has been hidden, so that it can get the + * keyboard focus back, if needed. + */ + contextMenuHidden: () => void; + /** + * Return the memory (in bytes) used by the plot itself, not counting its data. It is not reactive, i.e. it is meant + * to be polled. Mainly meant for testing and benchmarking. + */ + memoryUsage: () => PlotMemoryUsage; + /** + * Return the number of points of the visible traces and how many of them got drawn the last time the plot got drawn. + * It is not reactive, i.e. it is meant to be polled. Mainly meant for testing and benchmarking. + */ + pointCounts: () => PlotPointCounts; + /** Emit the `marginsUpdated` event again (e.g., after the shared margins have been reset). */ + resetMargins: () => void; +} + +defineExpose({ contextMenuHidden, memoryUsage, pointCounts,