Skip to content

Commit af126dc

Browse files
ysulymaYuri Sulyma
andauthored
add Calculator3D, GeometryCalculator, and Text expressions to @types/desmos (DefinitelyTyped#71720)
Co-authored-by: Yuri Sulyma <[email protected]>
1 parent f94babd commit af126dc

File tree

3 files changed

+154
-29
lines changed

3 files changed

+154
-29
lines changed

types/desmos/desmos-tests.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ calculator.asyncScreenshot(setImageSrc);
7373

7474
// Preserve the aspect ratio if the axes are square, otherwise show the exact region
7575
const opts = {
76-
mode: calculator.isProjectionUniform() ? "contain" as const : "stretch" as const,
76+
mode: calculator.isProjectionUniform()
77+
? ("contain" as const)
78+
: ("stretch" as const),
7779
width: 500,
7880
height: 300,
7981
mathBounds: { left: -5, right: 5, bottom: -20, top: 0 },
@@ -190,6 +192,13 @@ calculator.setExpression({
190192
color: calculator.colors.customBlue,
191193
});
192194

195+
// Text expression
196+
calculator.setExpression({
197+
type: "text",
198+
id: "4",
199+
text: "Hello World",
200+
});
201+
193202
// Make a dashed line
194203
calculator.setExpression({
195204
id: "line",
@@ -239,18 +248,46 @@ calculator.updateSettings({ language: "fr" });
239248

240249
// All features enabled
241250
// $ExpectType boolean
242-
Desmos.enabledFeatures.GraphingCalculator
251+
Desmos.enabledFeatures.Calculator3D
243252
&& Desmos.enabledFeatures.FourFunctionCalculator
253+
&& Desmos.enabledFeatures.GeometryCalculator
254+
&& Desmos.enabledFeatures.GraphingCalculator
244255
&& Desmos.enabledFeatures.ScientificCalculator;
245256

246257
// Only graphing calculator enabled
247258
// $ExpectType boolean
248259
Desmos.enabledFeatures.GraphingCalculator
260+
&& !Desmos.enabledFeatures.Calculator3D
249261
&& !Desmos.enabledFeatures.FourFunctionCalculator
262+
&& !Desmos.enabledFeatures.GeometryCalculator
250263
&& !Desmos.enabledFeatures.ScientificCalculator;
251264

252-
const elt1 = document.getElementById("four-function-calculator") as HTMLDivElement;
253-
const calculator1 = Desmos.FourFunctionCalculator(elt1);
265+
const elt1 = document.getElementById(
266+
"four-function-calculator",
267+
) as HTMLDivElement;
268+
const fourFunctionCalculator = Desmos.FourFunctionCalculator(elt1);
254269

255270
const elt2 = document.getElementById("scientific-calculator") as HTMLDivElement;
256-
const calculator2 = Desmos.ScientificCalculator(elt2);
271+
const scientificCalculator = Desmos.ScientificCalculator(elt2);
272+
273+
// 3d calculator
274+
const elt3 = document.getElementById("calculator-3d") as HTMLDivElement;
275+
const calculator3d = Desmos.Calculator3D(elt3);
276+
277+
calculator3d.setExpression({
278+
id: "1",
279+
latex: "y=x",
280+
color: Desmos.Colors.BLUE,
281+
});
282+
283+
// geometry calculator
284+
const eltGeometry = document.getElementById(
285+
"calculator-geometry",
286+
) as HTMLDivElement;
287+
const geometryCalculator = Desmos.Geometry(eltGeometry);
288+
289+
geometryCalculator.setExpression({
290+
id: "1",
291+
latex: "y=x",
292+
color: Desmos.Colors.BLUE,
293+
});

types/desmos/index.d.ts

Lines changed: 108 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ declare namespace Desmos {
33
* Which features are enabled for your API key.
44
*/
55
const enabledFeatures: {
6+
Calculator3D: boolean;
7+
GeometryCalculator: boolean;
68
GraphingCalculator: boolean;
79
FourFunctionCalculator: boolean;
810
ScientificCalculator: boolean;
@@ -164,10 +166,49 @@ declare namespace Desmos {
164166
},
165167
): BasicCalculator;
166168

169+
/**
170+
* Creates a Calculator3D object to control the embedded instance in the DOM element specified by element.
171+
*
172+
* It is possible to create a `Calculator3D` instance outside of the DOM and call methods on the returned object, but the view will not be created until element is inserted into the DOM.
173+
*
174+
* Note: for every instantiated `Calculator3D` instance, we run a small set of computations on every frame in order to reliably detect when the instance is added to the DOM, removed from it, show, hidden, or resized.
175+
* This could, theoretically, have performance and/or memory implications, although the effects are miniscule unless you instantiate many Calculator3D instances.
176+
*
177+
* If you want to manage sizing yourself, you can instantiate with the API option `{autosize: false}`.
178+
* In this case, you must call `.resize()` anytime that the instance is added to the DOM, removed from the DOM, or resized.
179+
*
180+
* When you're done using a calculator instance, call `.destroy()` to remove all of our listeners.
181+
*/
182+
function Calculator3D(
183+
element: HTMLElement,
184+
options?: GraphConfiguration & GraphSettings,
185+
): Calculator;
186+
187+
/**
188+
* Creates a `Geometry` object to control the embedded instance in the DOM element specified by element.
189+
*
190+
* It is possible to create a `Geometry` instance outside of the DOM and call methods on the returned object, but the view will not be created until element is inserted into the DOM.
191+
*
192+
* Note: for every instantiated `Geometry` instance, we run a small set of computations on every frame in order to reliably detect when the instance is added to the DOM, removed from it, show, hidden, or resized.
193+
* This could, theoretically, have performance and/or memory implications, although the effects are miniscule unless you instantiate many `Geometry` instances.
194+
*
195+
* If you want to manage sizing yourself, you can instantiate with the API option `{autosize: false}`.
196+
* In this case, you must call `.resize()` anytime that the instance is added to the DOM, removed from the DOM, or resized.
197+
*
198+
* When you're done using a geometry instance, call `.destroy()` to remove all of our listeners.
199+
*/
200+
function Geometry(
201+
element: HTMLElement,
202+
options?: GraphConfiguration & GraphSettings,
203+
): Calculator;
204+
167205
/**
168206
* Creates a calculator object to control the calculator embedded in the DOM element specified by element.
169207
*/
170-
function GraphingCalculator(element: HTMLElement, options?: GraphConfiguration & GraphSettings): Calculator;
208+
function GraphingCalculator(
209+
element: HTMLElement,
210+
options?: GraphConfiguration & GraphSettings,
211+
): Calculator;
171212

172213
/**
173214
* Creates a scientific calculator object to control the calculator embedded in the DOM element specified by element.
@@ -253,7 +294,10 @@ declare namespace Desmos {
253294
},
254295
): BasicCalculator;
255296

256-
function imageFileToDataURL(file: File, cb: (err: Error, url: string) => void): void;
297+
function imageFileToDataURL(
298+
file: File,
299+
cb: (err: Error, url: string) => void,
300+
): void;
257301

258302
type GraphState = unknown;
259303

@@ -351,7 +395,9 @@ declare namespace Desmos {
351395
/**
352396
* Convert math coordinates to pixel coordinates.
353397
*/
354-
mathToPixels<C extends { x: number } | { y: number } | { x: number; y: number }>(coords: C): C;
398+
mathToPixels<
399+
C extends { x: number } | { y: number } | { x: number; y: number },
400+
>(coords: C): C;
355401
/**
356402
* Update the settings.randomSeed property to a new random value.
357403
*/
@@ -366,7 +412,9 @@ declare namespace Desmos {
366412
/**
367413
* Convert pixel coordinates to math coordinates.
368414
*/
369-
pixelsToMath<C extends { x: number } | { y: number } | { x: number; y: number }>(coords: C): C;
415+
pixelsToMath<
416+
C extends { x: number } | { y: number } | { x: number; y: number },
417+
>(coords: C): C;
370418
/**
371419
* Advance to the next state in the undo/redo history, if available.
372420
*/
@@ -444,7 +492,12 @@ declare namespace Desmos {
444492
* Updates the math coordinates of the graphpaper bounds.
445493
* If invalid bounds are provided, the graphpaper bounds will not be changed.
446494
*/
447-
setMathBounds(bounds: { left?: number; right?: number; bottom?: number; top?: number }): void;
495+
setMathBounds(bounds: {
496+
left?: number;
497+
right?: number;
498+
bottom?: number;
499+
top?: number;
500+
}): void;
448501
/**
449502
* Reset the calculator to a state previously saved using GraphingCalculator.getState.
450503
*/
@@ -486,7 +539,10 @@ declare namespace Desmos {
486539
HelperExpression(expression: ExpressionState): {
487540
listValue: number[];
488541
numericValue: number;
489-
observe(eventName: "numericValue" | "listValue" | string, callback: () => void): void;
542+
observe(
543+
eventName: "numericValue" | "listValue" | string,
544+
callback: () => void,
545+
): void;
490546
};
491547

492548
// properties
@@ -520,7 +576,9 @@ declare namespace Desmos {
520576
/**
521577
* Numeric value(s)
522578
*/
523-
evaluation?: { type: "Number"; value: number } | { type: "ListOfNumber"; value: readonly number[] };
579+
evaluation?:
580+
| { type: "Number"; value: number }
581+
| { type: "ListOfNumber"; value: readonly number[] };
524582
};
525583
};
526584

@@ -563,8 +621,13 @@ declare namespace Desmos {
563621
& GraphConfiguration
564622
& GraphSettings
565623
& {
566-
observe(eventName: keyof GraphConfiguration | keyof GraphSettings | string, callback: () => void): void;
567-
unobserve(eventName: keyof GraphConfiguration | keyof GraphSettings | string): void;
624+
observe(
625+
eventName: keyof GraphConfiguration | keyof GraphSettings | string,
626+
callback: () => void,
627+
): void;
628+
unobserve(
629+
eventName: keyof GraphConfiguration | keyof GraphSettings | string,
630+
): void;
568631
};
569632

570633
/**
@@ -574,38 +637,59 @@ declare namespace Desmos {
574637
}
575638

576639
type ExpressionState =
640+
| {
641+
type?: "text";
642+
643+
/**
644+
* The text content of the note.
645+
* @default ""
646+
*/
647+
text?: string;
648+
649+
/**
650+
* Should be a valid property name for a javascript object (letters, numbers, and _).
651+
*/
652+
id?: string;
653+
}
577654
| {
578655
type?: "expression";
579656
/**
580-
* Following Desmos Expressions.
657+
* Following {@link https://www.desmos.com/api/v1.11/docs/index.html#document-expressions Desmos Expressions}.
581658
*/
582659
latex?: string;
583660
/**
584-
* , hex color. See Colors. Default will cycle through 6 default colors.
661+
* Hex color. See {@link https://www.desmos.com/api/v1.11/docs/#document-colors Colors}.
662+
* Default will cycle through 6 default colors.
585663
*/
586664
color?: string;
587665
/**
588-
* Sets the line drawing style of curves or point lists. See Styles.
666+
* Sets the line drawing style of curves or point lists.
667+
* See {@link https://www.desmos.com/api/v1.11/docs/#document-styles} Styles.
589668
*/
590669
lineStyle?: keyof typeof Styles;
591670
/**
592-
* Determines width of lines in pixels. May be any positive number, or a LaTeX string that evaluates to a positive number. Defaults to 2.5.
671+
* Determines width of lines in pixels. May be any positive number, or a LaTeX string that evaluates to a positive number.
672+
* @default 2.5
593673
*/
594674
lineWidth?: number | string;
595675
/**
596-
* Determines opacity of lines. May be a number between 0 and 1, or a LaTeX string that evaluates to a number between 0 and 1. Defaults to 0.9.
676+
* Determines opacity of lines. May be a number between 0 and 1, or a LaTeX string that evaluates to a number between 0 and 1.
677+
* @default 0.9
597678
*/
598679
lineOpacity?: number | string;
599680
/**
600-
* Sets the point drawing style of point lists. See Styles.
681+
* Sets the point drawing style of point lists.
682+
* See {@link https://www.desmos.com/api/v1.11/docs/#document-styles} Styles.
601683
*/
602684
pointStyle?: keyof typeof Styles;
603685
/**
604-
* Determines diameter of points in pixels. May be any positive number, or a LaTeX string that evaluates to a positive number. Defaults to 9.
686+
* Determines diameter of points in pixels. May be any positive number, or a LaTeX string that evaluates to a positive number.
687+
* @default 9
605688
*/
606689
pointSize?: number | string;
607690
/**
608-
* Determines opacity of points. May be a number between 0 and 1, or a LaTeX string that evaluates to a number between 0 and 1. Defaults to 0.9.
691+
* Determines opacity of points. May be a number between 0 and 1, or a LaTeX string that evaluates to a number between 0 and 1.
692+
* @default 0.9
609693
*/
610694
pointOpacity?: number | string;
611695
/**
@@ -629,7 +713,8 @@ declare namespace Desmos {
629713
*/
630714
hidden?: boolean;
631715
/**
632-
* Determines whether the expression should appear in the expressions list. Does not affect graph visibility. Defaults to false.
716+
* Determines whether the expression should appear in the expressions list. Does not affect graph visibility.
717+
* @default false
633718
*/
634719
secret?: boolean;
635720
/**
@@ -826,7 +911,10 @@ declare namespace Desmos {
826911
* Specify custom processing for user-uploaded images. See Image Uploads for more details.
827912
* @param file comment for stuff
828913
*/
829-
imageUploadCallback?(file: File, cb: (err: Error, url: string) => void): void;
914+
imageUploadCallback?(
915+
file: File,
916+
cb: (err: Error, url: string) => void,
917+
): void;
830918
/**
831919
* Allow the creation of folders in the expressions list
832920
* @default true
@@ -929,7 +1017,7 @@ declare namespace Desmos {
9291017
*/
9301018
invertedColors?: boolean;
9311019
/**
932-
* Language. See the https://www.desmos.com/api/v1.6/docs/index.html#document-languages for more information.
1020+
* Language. See the https://www.desmos.com/api/v1.11/docs/index.html#document-languages for more information.
9331021
* @default "en"
9341022
*/
9351023
language?: string;

types/desmos/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"private": true,
33
"name": "@types/desmos",
4-
"version": "1.6.9999",
5-
"projects": [
6-
"https://www.desmos.com/api/v1.6/docs/"
7-
],
4+
"version": "1.11.9999",
5+
"projects": ["https://www.desmos.com/api/v1.11/docs/"],
86
"devDependencies": {
97
"@types/desmos": "workspace:."
108
},
9+
"nonNpm": "conflict",
10+
"nonNpmDescription": "Desmos graphing calculator API (https://www.desmos.com/api/v1.11/docs/).\nThese types are for use with the Desmos API loaded via <script> tag.\nThe NPM package `desmos` is unofficial and unmaintained, and should not be used.",
1111
"owners": [
1212
{
1313
"name": "ysulyma",

0 commit comments

Comments
 (0)