Skip to content

Commit d8b66df

Browse files
committed
Initial TS set up
1 parent e661a4c commit d8b66df

File tree

14 files changed

+1335
-7
lines changed

14 files changed

+1335
-7
lines changed

package-lock.json

Lines changed: 29 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
"cibuild": "npm run empty-dist && npm run preprocess && node tasks/cibundle.mjs",
4141
"lint": "npx @biomejs/biome lint",
4242
"lint-fix": "npx @biomejs/biome format ./test/image/mocks --write; npx @biomejs/biome lint --write || true",
43+
"typecheck": "tsc --noEmit",
44+
"typecheck:watch": "tsc --noEmit --watch",
4345
"pretest": "node tasks/pretest.js",
4446
"test-jasmine": "karma start test/jasmine/karma.conf.js",
4547
"test-mock": "node tasks/test_mock.mjs",
@@ -122,6 +124,8 @@
122124
"@biomejs/biome": "2.2.0",
123125
"@plotly/mathjax-v2": "npm:[email protected]",
124126
"@plotly/mathjax-v3": "npm:mathjax@^3.2.2",
127+
"@types/d3": "3.5.34",
128+
"@types/node": "^24.10.0",
125129
"amdefine": "^1.0.1",
126130
"assert": "^2.1.0",
127131
"browserify-transform-tools": "^1.7.0",
@@ -174,6 +178,7 @@
174178
"through2": "^4.0.2",
175179
"transform-loader": "^0.2.4",
176180
"true-case-path": "^2.2.1",
181+
"typescript": "^5.9.3",
177182
"virtual-webgl": "^1.0.6"
178183
},
179184
"overrides": {

src/types/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Types Directory
2+
3+
Centralized TypeScript type definitions for plotly.js.
4+
5+
## Quick Import
6+
7+
```typescript
8+
// Import from main index (recommended)
9+
import type { GraphDiv, Layout, PlotData } from '../types';
10+
11+
// Or use path alias
12+
import type { GraphDiv } from '@types';
13+
```
14+
15+
## Directory Structure
16+
17+
```
18+
types/
19+
├── index.d.ts # Main export - import from here
20+
├── core/ # Core Plotly types (GraphDiv, Layout, Data, Config, Events)
21+
├── traces/ # Trace-specific types
22+
├── components/ # Component-specific types
23+
├── plots/ # Plot-specific types
24+
└── lib/ # Utility types
25+
```
26+
27+
## Most Common Types
28+
29+
### GraphDiv
30+
```typescript
31+
import type { GraphDiv } from '../types';
32+
33+
function draw(gd: GraphDiv): void {
34+
const layout = gd._fullLayout;
35+
const data = gd._fullData;
36+
}
37+
```
38+
39+
### Layout
40+
```typescript
41+
import type { Layout } from '../types';
42+
43+
function updateLayout(layout: Partial<Layout>): void {
44+
layout.title = 'New Title';
45+
}
46+
```
47+
48+
### PlotData (Traces)
49+
```typescript
50+
import type { PlotData, ScatterTrace } from '../types';
51+
52+
function processTrace(trace: PlotData): void {
53+
if (trace.type === 'scatter') {
54+
const scatter = trace as ScatterTrace;
55+
}
56+
}
57+
```
58+
59+
## Adding New Types
60+
61+
1. Create file in appropriate subdirectory
62+
2. Export from `index.d.ts`
63+
3. Use in your TypeScript files

src/types/components/common.d.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Common component-related types
3+
*
4+
* Types shared across different component modules
5+
*/
6+
7+
/**
8+
* Component module interface
9+
*/
10+
export interface ComponentModule {
11+
includeBasePlot?: (basePlotModule: string) => void;
12+
layoutAttributes?: any;
13+
name: string;
14+
supplyLayoutDefaults?: (layoutIn: any, layoutOut: any, fullData: any) => void;
15+
}
16+
17+
/**
18+
* Drawing options (used by many components)
19+
*/
20+
export interface DrawingOptions {
21+
duration?: number;
22+
ease?: string;
23+
redraw?: boolean;
24+
}
25+
26+
/**
27+
* SVG text utilities return type
28+
*/
29+
export interface TextBBox {
30+
bottom: number;
31+
height: number;
32+
left: number;
33+
right: number;
34+
top: number;
35+
width: number;
36+
}
37+
38+
/**
39+
* Color with alpha
40+
*/
41+
export type ColorString = string;

src/types/core/config.d.ts

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/**
2+
* Config types
3+
*
4+
* Defines the structure of Plotly config options.
5+
*/
6+
7+
/**
8+
* User-provided configuration for Plotly
9+
*/
10+
export interface Config {
11+
/**
12+
* Whether the plot should resize on window resize
13+
*/
14+
autosizable?: boolean;
15+
16+
/**
17+
* Display Plotly logo on mode bar
18+
*/
19+
displaylogo?: boolean;
20+
21+
/**
22+
* Display the mode bar
23+
*/
24+
displayModeBar?: boolean | 'hover';
25+
26+
/**
27+
* What happens on double click
28+
*/
29+
doubleClick?: 'reset' | 'autosize' | 'reset+autosize' | false;
30+
31+
/**
32+
* Allow plot to be edited
33+
*/
34+
editable?: boolean;
35+
36+
/**
37+
* What can be edited
38+
*/
39+
edits?: Partial<ConfigEdits>;
40+
41+
/**
42+
* Whether to fill the parent container
43+
*/
44+
fillFrame?: boolean;
45+
46+
/**
47+
* Margin around the plot when fillFrame is true
48+
*/
49+
frameMargins?: number;
50+
51+
/**
52+
* Text for the "Edit in Chart Studio" link
53+
*/
54+
linkText?: string;
55+
56+
/**
57+
* Locale for formatting
58+
*/
59+
locale?: string;
60+
61+
/**
62+
* Custom locale definitions
63+
*/
64+
locales?: { [locale: string]: any };
65+
66+
/**
67+
* Mapbox access token
68+
*/
69+
mapboxAccessToken?: string;
70+
71+
/**
72+
* Custom mode bar buttons configuration
73+
*/
74+
modeBarButtons?: any;
75+
76+
/**
77+
* Mode bar buttons to add
78+
*/
79+
modeBarButtonsToAdd?: any[];
80+
81+
/**
82+
* Mode bar buttons to remove
83+
*/
84+
modeBarButtonsToRemove?: string[];
85+
86+
/**
87+
* Pixel ratio for WebGL plots
88+
*/
89+
plotGlPixelRatio?: number;
90+
91+
/**
92+
* Base URL for plotly server
93+
*/
94+
plotlyServerURL?: string;
95+
96+
/**
97+
* Number of operations that can be queued
98+
*/
99+
queueLength?: number;
100+
101+
/**
102+
* Whether to resize on window resize (alternative)
103+
*/
104+
responsive?: boolean;
105+
106+
/**
107+
* Enable scroll zoom
108+
*/
109+
scrollZoom?: boolean;
110+
111+
/**
112+
* Send data to Chart Studio
113+
*/
114+
sendData?: boolean;
115+
116+
/**
117+
* Background color setting function
118+
*/
119+
setBackground?: string | ((gd: any) => void);
120+
121+
/**
122+
* Show "Edit in Chart Studio" link
123+
*/
124+
showLink?: boolean;
125+
126+
/**
127+
* Show tips on first hover
128+
*/
129+
showTips?: boolean;
130+
131+
/**
132+
* Make the chart static - no interactivity
133+
*/
134+
staticPlot?: boolean;
135+
136+
/**
137+
* Options for the "Download plot as PNG" button
138+
*/
139+
toImageButtonOptions?: Partial<ToImageButtonOptions>;
140+
141+
/**
142+
* URL for topojson files
143+
*/
144+
topojsonURL?: string;
145+
146+
/**
147+
* Add watermark to images
148+
*/
149+
watermark?: boolean;
150+
}
151+
152+
/**
153+
* Configuration for what can be edited
154+
*/
155+
export interface ConfigEdits {
156+
annotationPosition?: boolean;
157+
annotationTail?: boolean;
158+
annotationText?: boolean;
159+
axisTitleText?: boolean;
160+
colorbarPosition?: boolean;
161+
colorbarTitleText?: boolean;
162+
legendPosition?: boolean;
163+
legendText?: boolean;
164+
shapePosition?: boolean;
165+
titleText?: boolean;
166+
}
167+
168+
/**
169+
* Options for the "Download plot" button
170+
*/
171+
export interface ToImageButtonOptions {
172+
format?: 'png' | 'svg' | 'jpeg' | 'webp';
173+
filename?: string;
174+
height?: number;
175+
width?: number;
176+
scale?: number;
177+
}

0 commit comments

Comments
 (0)