-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.tsx
186 lines (155 loc) · 4.83 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import {createContext, forwardRef, useContext, useEffect, useRef, useState, memo} from "react";
const DesmosContext = createContext<Desmos.Calculator>(null);
/* elt() */
declare global {
namespace Desmos {
interface BasicCalculator {
domChangeDetector: {
elt: HTMLDivElement;
};
}
interface Calculator {
domChangeDetector: {
elt: HTMLDivElement;
};
}
}
}
/**
* Get the container element of a calculator.
*/
export function elt(calc: Desmos.Calculator | Desmos.BasicCalculator): HTMLDivElement {
return calc.domChangeDetector.elt;
}
/**
* The Desmos graphing calculator.
*/
export const GraphingCalculator = forwardRef<
Desmos.Calculator,
React.PropsWithChildren<Parameters<typeof Desmos.GraphingCalculator>[1] & { attributes?: React.HTMLAttributes<HTMLDivElement>; }>
>(function GraphingCalculator(props, ref) {
const [calculator, setCalculator] = useState<Desmos.Calculator>();
const div = useRef<HTMLDivElement>();
useEffect(() => {
// create calculator
const {attributes, children, ...options} = props;
const calculator = Desmos.GraphingCalculator(div.current, options);
applyRef(ref, calculator);
setCalculator(calculator);
return () => {
calculator.destroy();
};
}, []);
useEffect(() => {
const {attributes, children, ...settings} = props;
if (calculator)
calculator.updateSettings(settings);
});
return (
<DesmosContext.Provider value={calculator}>
<div ref={div} {...(props.attributes ?? {})}>
{calculator ? props.children : null}
</div>
</DesmosContext.Provider>
);
});
/**
* A Desmos {@link https://www.desmos.com/api/v1.6/docs/index.html#document-expressions expression}.
*/
export const Expression = memo(function Expression(props: Desmos.ExpressionState): null {
const calculator = useCalculator();
const old = useRef<Desmos.ExpressionState>({});
const update: Desmos.ExpressionState = {id: props.id};
for (const k in props) {
if (props[k] !== old.current[k]) {
update[k] = props[k];
}
}
old.current = props;
calculator.setExpression(update);
useEffect(() => {
return () => {
// @ts-expect-error _destroyed is not exposed
if (!calculator._destroyed)
calculator.removeExpression({id: props.id});
};
}, [props.id]);
return null;
});
function applyRef<T>(ref: React.ForwardedRef<T>, val: T) {
if (ref === null)
return;
if (typeof ref === "function") {
ref(val);
} else {
ref.current = val;
}
}
/* Basic calculators */
// you just couldn't help yourself could you?
// you doofus
function makeBasicCalculator
<T extends typeof Desmos.FourFunctionCalculator | typeof Desmos.ScientificCalculator>
(constructor: T, name: string)
{
const component = (
props: React.PropsWithChildren<Parameters<T>[1] & { attributes?: React.HTMLAttributes<HTMLDivElement>; }>,
ref: React.ForwardedRef<Desmos.BasicCalculator>
) => {
const [calculator, setCalculator] = useState<Desmos.BasicCalculator>();
const div = useRef<HTMLDivElement>();
// create calculator
useEffect(() => {
const {attributes, children, ...options} = props;
const calculator = constructor(div.current, options);
applyRef(ref, calculator);
setCalculator(calculator);
return () => {
calculator.destroy();
};
}, []);
// update settings
useEffect(() => {
const {attributes, children, ...settings} = props;
if (calculator)
calculator.updateSettings(settings);
});
// render
return (
<div ref={div} {...(props.attributes ?? {})}/>
);
};
// function components need to have a name
Object.defineProperty(component, "name", {value: name});
return forwardRef(component);
}
/**
* The Desmos four-function calculator.
*/
export const FourFunctionCalculator = makeBasicCalculator(Desmos.FourFunctionCalculator, "FourFunctionCalculator");
/**
* The Desmos scientific calculator.
*/
export const ScientificCalculator = makeBasicCalculator(Desmos.ScientificCalculator, "ScientificCalculator");
// hooks
/**
* Get a reference to the containing {@link Calculator}.
*/
export function useCalculator(): Desmos.Calculator {
return useContext(DesmosContext);
}
/**
* Subscribe to a Desmos {@link https://www.desmos.com/api/v1.6/docs/index.html#document-helper-expressions Helper Expression}.
*/
export function useHelperExpression(opts: Desmos.ExpressionState): number {
const calculator = useCalculator();
const helper = useRef<ReturnType<Desmos.Calculator["HelperExpression"]>>();
if (helper.current === undefined) {
helper.current = calculator.HelperExpression(opts);
}
const [value, setValue] = useState(helper.current.numericValue);
useEffect(() => {
helper.current.observe("numericValue", () => setValue(helper.current.numericValue));
}, []);
return value;
}