@xom11/whiteboard 0.6.5 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -0
- package/dist/chunk-3SSQKRRO.mjs +58 -0
- package/dist/chunk-3SSQKRRO.mjs.map +1 -0
- package/dist/chunk-7P7SQFOW.mjs +39 -0
- package/dist/chunk-7P7SQFOW.mjs.map +1 -0
- package/dist/chunk-BJX4YNA5.mjs +137 -0
- package/dist/chunk-BJX4YNA5.mjs.map +1 -0
- package/dist/chunk-C6SCVOMC.mjs +111 -0
- package/dist/chunk-C6SCVOMC.mjs.map +1 -0
- package/dist/chunk-DJTBZEAR.mjs +25 -0
- package/dist/chunk-DJTBZEAR.mjs.map +1 -0
- package/dist/chunk-HM7RIXJE.mjs +331 -0
- package/dist/chunk-HM7RIXJE.mjs.map +1 -0
- package/dist/chunk-HTBLO5JO.mjs +41 -0
- package/dist/chunk-HTBLO5JO.mjs.map +1 -0
- package/dist/chunk-HYXFHEDJ.mjs +129 -0
- package/dist/chunk-HYXFHEDJ.mjs.map +1 -0
- package/dist/chunk-LPM4MM45.mjs +211 -0
- package/dist/chunk-LPM4MM45.mjs.map +1 -0
- package/dist/chunk-P2AOIF7S.mjs +40 -0
- package/dist/chunk-P2AOIF7S.mjs.map +1 -0
- package/dist/chunk-SHFOGORM.mjs +44 -0
- package/dist/chunk-SHFOGORM.mjs.map +1 -0
- package/dist/chunk-X5R72SSJ.mjs +52 -0
- package/dist/chunk-X5R72SSJ.mjs.map +1 -0
- package/dist/geometry-2d.d.mts +16 -0
- package/dist/geometry-2d.d.ts +16 -0
- package/dist/geometry-2d.js +3549 -0
- package/dist/geometry-2d.js.map +1 -0
- package/dist/geometry-2d.mjs +7 -0
- package/dist/geometry-2d.mjs.map +1 -0
- package/dist/geometry-3d.d.mts +16 -0
- package/dist/geometry-3d.d.ts +16 -0
- package/dist/geometry-3d.js +2030 -0
- package/dist/geometry-3d.js.map +1 -0
- package/dist/geometry-3d.mjs +6 -0
- package/dist/geometry-3d.mjs.map +1 -0
- package/dist/graph-2d.d.mts +16 -0
- package/dist/graph-2d.d.ts +16 -0
- package/dist/graph-2d.js +1725 -0
- package/dist/graph-2d.js.map +1 -0
- package/dist/graph-2d.mjs +6 -0
- package/dist/graph-2d.mjs.map +1 -0
- package/dist/host-2QGKMGCT.mjs +1066 -0
- package/dist/host-2QGKMGCT.mjs.map +1 -0
- package/dist/host-T2W6R6SO.mjs +2859 -0
- package/dist/host-T2W6R6SO.mjs.map +1 -0
- package/dist/host-XUFON6CQ.mjs +1422 -0
- package/dist/host-XUFON6CQ.mjs.map +1 -0
- package/dist/host-Z3TEJKZA.mjs +466 -0
- package/dist/host-Z3TEJKZA.mjs.map +1 -0
- package/dist/index.d.mts +27 -146
- package/dist/index.d.ts +27 -146
- package/dist/index.js +4694 -4482
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +136 -7179
- package/dist/index.mjs.map +1 -1
- package/dist/latex.d.mts +15 -0
- package/dist/latex.d.ts +15 -0
- package/dist/latex.js +750 -0
- package/dist/latex.js.map +1 -0
- package/dist/latex.mjs +6 -0
- package/dist/latex.mjs.map +1 -0
- package/dist/types-CinstD7T.d.mts +110 -0
- package/dist/types-CinstD7T.d.ts +110 -0
- package/package.json +21 -2
|
@@ -0,0 +1,1066 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { EMPTY_GRAPH, addPointOnCurve, addIntersection, validate, stringifySerializedGraph, renderGraph2dSvgFromState, isGraph2DCustomData, parseSerializedGraph, compile, numericalDerivative } from './chunk-HM7RIXJE.mjs';
|
|
3
|
+
import { useIsMobile } from './chunk-P2AOIF7S.mjs';
|
|
4
|
+
import { insertStampImage } from './chunk-C6SCVOMC.mjs';
|
|
5
|
+
import { __require } from './chunk-BJTO5JO5.mjs';
|
|
6
|
+
import { forwardRef, useRef, useState, useCallback, useImperativeHandle, useEffect, useMemo } from 'react';
|
|
7
|
+
import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
|
|
8
|
+
|
|
9
|
+
// src/stamps/graph-2d/editor/tools.ts
|
|
10
|
+
var GRAPH_TOOLS = [
|
|
11
|
+
{ id: "move", label: "Di chuy\u1EC3n", title: "Di chuy\u1EC3n / ch\u1ECDn" },
|
|
12
|
+
{ id: "point-on-curve", label: "\u0110i\u1EC3m tr\xEAn curve", title: "T\u1EA1o \u0111i\u1EC3m c\u1ED1 \u0111\u1ECBnh tr\xEAn \u0111\u1ED3 th\u1ECB" },
|
|
13
|
+
{ id: "intersect", label: "Giao \u0111i\u1EC3m", title: "\u0110\xE1nh d\u1EA5u giao \u0111i\u1EC3m 2 \u0111\u1ED3 th\u1ECB" },
|
|
14
|
+
{ id: "tangent", label: "Ti\u1EBFp tuy\u1EBFn", title: "V\u1EBD ti\u1EBFp tuy\u1EBFn t\u1EA1i \u0111i\u1EC3m tr\xEAn \u0111\u1ED3 th\u1ECB" }
|
|
15
|
+
];
|
|
16
|
+
function FunctionRow(props) {
|
|
17
|
+
const { id, name, expression, color, visible, error } = props;
|
|
18
|
+
const [draft, setDraft] = useState(expression);
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
setDraft(expression);
|
|
21
|
+
}, [expression]);
|
|
22
|
+
const commit = () => {
|
|
23
|
+
if (draft !== expression) props.onExpressionCommit(draft);
|
|
24
|
+
};
|
|
25
|
+
const handleKeyDown = (e) => {
|
|
26
|
+
if (e.key === "Enter") {
|
|
27
|
+
e.preventDefault();
|
|
28
|
+
commit();
|
|
29
|
+
e.target.blur();
|
|
30
|
+
} else if (e.key === "Escape") {
|
|
31
|
+
setDraft(expression);
|
|
32
|
+
e.target.blur();
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const handleBlur = (_) => commit();
|
|
36
|
+
return /* @__PURE__ */ jsxs("div", { className: `graph-function-row${error ? " is-error" : ""}`, "data-testid": `graph-function-row-${id}`, children: [
|
|
37
|
+
/* @__PURE__ */ jsx(
|
|
38
|
+
"span",
|
|
39
|
+
{
|
|
40
|
+
className: "graph-function-color",
|
|
41
|
+
style: { backgroundColor: color },
|
|
42
|
+
"aria-hidden": "true"
|
|
43
|
+
}
|
|
44
|
+
),
|
|
45
|
+
/* @__PURE__ */ jsxs("span", { className: "graph-function-name", "data-testid": `graph-function-name-${id}`, children: [
|
|
46
|
+
name,
|
|
47
|
+
"(x) ="
|
|
48
|
+
] }),
|
|
49
|
+
/* @__PURE__ */ jsx(
|
|
50
|
+
"input",
|
|
51
|
+
{
|
|
52
|
+
"aria-label": "Bi\u1EC3u th\u1EE9c",
|
|
53
|
+
className: "graph-function-input",
|
|
54
|
+
type: "text",
|
|
55
|
+
value: draft,
|
|
56
|
+
onChange: (e) => setDraft(e.target.value),
|
|
57
|
+
onKeyDown: handleKeyDown,
|
|
58
|
+
onBlur: handleBlur,
|
|
59
|
+
spellCheck: false,
|
|
60
|
+
autoCorrect: "off",
|
|
61
|
+
autoCapitalize: "off"
|
|
62
|
+
}
|
|
63
|
+
),
|
|
64
|
+
/* @__PURE__ */ jsx(
|
|
65
|
+
"button",
|
|
66
|
+
{
|
|
67
|
+
type: "button",
|
|
68
|
+
"aria-label": "\u1EA8n/hi\u1EC7n \u0111\u1ED3 th\u1ECB",
|
|
69
|
+
className: `graph-function-eye${visible ? "" : " is-hidden"}`,
|
|
70
|
+
onClick: props.onToggleVisible,
|
|
71
|
+
children: visible ? "\u{1F441}" : "\u2298"
|
|
72
|
+
}
|
|
73
|
+
),
|
|
74
|
+
/* @__PURE__ */ jsx(
|
|
75
|
+
"button",
|
|
76
|
+
{
|
|
77
|
+
type: "button",
|
|
78
|
+
"aria-label": "Xo\xE1 \u0111\u1ED3 th\u1ECB",
|
|
79
|
+
className: "graph-function-remove",
|
|
80
|
+
onClick: props.onRemove,
|
|
81
|
+
children: "\u2715"
|
|
82
|
+
}
|
|
83
|
+
),
|
|
84
|
+
error ? /* @__PURE__ */ jsx("div", { className: "graph-function-error", children: error }) : null
|
|
85
|
+
] });
|
|
86
|
+
}
|
|
87
|
+
function SliderRow(props) {
|
|
88
|
+
const { name, value, min, max, step } = props;
|
|
89
|
+
return /* @__PURE__ */ jsxs("div", { className: "graph-slider-row", "data-testid": `graph-slider-row-${name}`, children: [
|
|
90
|
+
/* @__PURE__ */ jsxs("div", { className: "graph-slider-header", children: [
|
|
91
|
+
/* @__PURE__ */ jsx("span", { className: "graph-slider-name", children: name }),
|
|
92
|
+
/* @__PURE__ */ jsxs("span", { className: "graph-slider-value", children: [
|
|
93
|
+
"= ",
|
|
94
|
+
value.toFixed(2)
|
|
95
|
+
] }),
|
|
96
|
+
/* @__PURE__ */ jsx(
|
|
97
|
+
"button",
|
|
98
|
+
{
|
|
99
|
+
type: "button",
|
|
100
|
+
"aria-label": `Xo\xE1 tham s\u1ED1 ${name}`,
|
|
101
|
+
className: "graph-slider-remove",
|
|
102
|
+
onClick: props.onRemove,
|
|
103
|
+
children: "\u2715"
|
|
104
|
+
}
|
|
105
|
+
)
|
|
106
|
+
] }),
|
|
107
|
+
/* @__PURE__ */ jsx(
|
|
108
|
+
"input",
|
|
109
|
+
{
|
|
110
|
+
type: "range",
|
|
111
|
+
"aria-label": `Slider ${name}`,
|
|
112
|
+
min,
|
|
113
|
+
max,
|
|
114
|
+
step,
|
|
115
|
+
value,
|
|
116
|
+
onChange: (e) => props.onChange(parseFloat(e.target.value)),
|
|
117
|
+
className: "graph-slider-input"
|
|
118
|
+
}
|
|
119
|
+
),
|
|
120
|
+
/* @__PURE__ */ jsxs("div", { className: "graph-slider-range", children: [
|
|
121
|
+
/* @__PURE__ */ jsx("span", { children: min }),
|
|
122
|
+
/* @__PURE__ */ jsx("span", { children: max })
|
|
123
|
+
] })
|
|
124
|
+
] });
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// src/stamps/graph-2d/colors.ts
|
|
128
|
+
var GRAPH_PALETTE = [
|
|
129
|
+
"#2563eb",
|
|
130
|
+
// blue
|
|
131
|
+
"#dc2626",
|
|
132
|
+
// red
|
|
133
|
+
"#16a34a",
|
|
134
|
+
// green
|
|
135
|
+
"#9333ea",
|
|
136
|
+
// purple
|
|
137
|
+
"#ea580c",
|
|
138
|
+
// orange
|
|
139
|
+
"#0891b2",
|
|
140
|
+
// cyan
|
|
141
|
+
"#db2777",
|
|
142
|
+
// pink
|
|
143
|
+
"#65a30d"
|
|
144
|
+
// lime
|
|
145
|
+
];
|
|
146
|
+
var FUNCTION_NAMES = ["f", "g", "h", "i", "j", "k", "l", "m"];
|
|
147
|
+
var MAX_FUNCTIONS = 8;
|
|
148
|
+
var MAX_PARAMETERS = 8;
|
|
149
|
+
function nextColor(usedColors) {
|
|
150
|
+
for (const c of GRAPH_PALETTE) {
|
|
151
|
+
if (!usedColors.includes(c)) return c;
|
|
152
|
+
}
|
|
153
|
+
return GRAPH_PALETTE[usedColors.length % GRAPH_PALETTE.length];
|
|
154
|
+
}
|
|
155
|
+
function nextFunctionName(usedNames) {
|
|
156
|
+
for (const n of FUNCTION_NAMES) {
|
|
157
|
+
if (!usedNames.includes(n)) return n;
|
|
158
|
+
}
|
|
159
|
+
return FUNCTION_NAMES[usedNames.length % FUNCTION_NAMES.length];
|
|
160
|
+
}
|
|
161
|
+
function AlgebraView(props) {
|
|
162
|
+
const { graph, errors } = props;
|
|
163
|
+
const atMax = graph.functions.length >= MAX_FUNCTIONS;
|
|
164
|
+
return /* @__PURE__ */ jsxs("div", { className: "graph-algebra-view", children: [
|
|
165
|
+
/* @__PURE__ */ jsxs("div", { className: "graph-algebra-section", children: [
|
|
166
|
+
graph.functions.map((f) => /* @__PURE__ */ jsx(
|
|
167
|
+
FunctionRow,
|
|
168
|
+
{
|
|
169
|
+
id: f.id,
|
|
170
|
+
name: f.name,
|
|
171
|
+
expression: f.expression,
|
|
172
|
+
color: f.color,
|
|
173
|
+
visible: f.visible,
|
|
174
|
+
error: errors[f.id] ?? null,
|
|
175
|
+
onExpressionCommit: (expr) => props.onCommitFunctionExpr(f.id, expr),
|
|
176
|
+
onToggleVisible: () => props.onToggleFunctionVisible(f.id),
|
|
177
|
+
onRemove: () => props.onRemoveFunction(f.id)
|
|
178
|
+
},
|
|
179
|
+
f.id
|
|
180
|
+
)),
|
|
181
|
+
/* @__PURE__ */ jsx(
|
|
182
|
+
"button",
|
|
183
|
+
{
|
|
184
|
+
type: "button",
|
|
185
|
+
"aria-label": "Th\xEAm h\xE0m s\u1ED1",
|
|
186
|
+
className: "graph-algebra-add",
|
|
187
|
+
onClick: props.onAddFunctionDraft,
|
|
188
|
+
disabled: atMax,
|
|
189
|
+
children: "+ Th\xEAm h\xE0m"
|
|
190
|
+
}
|
|
191
|
+
)
|
|
192
|
+
] }),
|
|
193
|
+
graph.parameters.length > 0 ? /* @__PURE__ */ jsx("div", { className: "graph-algebra-section graph-algebra-parameters", children: graph.parameters.map((p) => /* @__PURE__ */ jsx(
|
|
194
|
+
SliderRow,
|
|
195
|
+
{
|
|
196
|
+
name: p.name,
|
|
197
|
+
value: p.value,
|
|
198
|
+
min: p.min,
|
|
199
|
+
max: p.max,
|
|
200
|
+
step: p.step,
|
|
201
|
+
onChange: (v) => props.onParameterChange(p.name, v),
|
|
202
|
+
onRangeChange: (min, max, step) => props.onParameterRangeChange(p.name, min, max, step),
|
|
203
|
+
onRemove: () => props.onRemoveParameter(p.name)
|
|
204
|
+
},
|
|
205
|
+
p.name
|
|
206
|
+
)) }) : null
|
|
207
|
+
] });
|
|
208
|
+
}
|
|
209
|
+
var GraphIconHeader = /* @__PURE__ */ jsxs("svg", { width: "14", height: "14", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "1.8", strokeLinecap: "round", strokeLinejoin: "round", children: [
|
|
210
|
+
/* @__PURE__ */ jsx("path", { d: "M3 21 V3" }),
|
|
211
|
+
/* @__PURE__ */ jsx("path", { d: "M3 21 H21" }),
|
|
212
|
+
/* @__PURE__ */ jsx("path", { d: "M5 19 C8 5, 14 5, 19 17" })
|
|
213
|
+
] });
|
|
214
|
+
function CloseIcon() {
|
|
215
|
+
return /* @__PURE__ */ jsxs("svg", { width: "14", height: "14", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: [
|
|
216
|
+
/* @__PURE__ */ jsx("line", { x1: "6", y1: "6", x2: "18", y2: "18" }),
|
|
217
|
+
/* @__PURE__ */ jsx("line", { x1: "18", y1: "6", x2: "6", y2: "18" })
|
|
218
|
+
] });
|
|
219
|
+
}
|
|
220
|
+
function UndoIcon() {
|
|
221
|
+
return /* @__PURE__ */ jsxs("svg", { width: "16", height: "16", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: [
|
|
222
|
+
/* @__PURE__ */ jsx("polyline", { points: "3 7 3 13 9 13" }),
|
|
223
|
+
/* @__PURE__ */ jsx("path", { d: "M3.51 13a9 9 0 1 0 2.13-9.36L3 7" })
|
|
224
|
+
] });
|
|
225
|
+
}
|
|
226
|
+
function ResetViewIcon() {
|
|
227
|
+
return /* @__PURE__ */ jsxs("svg", { width: "16", height: "16", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: [
|
|
228
|
+
/* @__PURE__ */ jsx("circle", { cx: "12", cy: "12", r: "9" }),
|
|
229
|
+
/* @__PURE__ */ jsx("line", { x1: "12", y1: "3", x2: "12", y2: "21" }),
|
|
230
|
+
/* @__PURE__ */ jsx("line", { x1: "3", y1: "12", x2: "21", y2: "12" })
|
|
231
|
+
] });
|
|
232
|
+
}
|
|
233
|
+
function MoveIcon() {
|
|
234
|
+
return /* @__PURE__ */ jsx("svg", { width: "16", height: "16", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "1.8", strokeLinecap: "round", strokeLinejoin: "round", children: /* @__PURE__ */ jsx("path", { d: "M3 4 L9 4 L9 9 L4 9 Z" }) });
|
|
235
|
+
}
|
|
236
|
+
function PointOnCurveIcon() {
|
|
237
|
+
return /* @__PURE__ */ jsxs("svg", { width: "16", height: "16", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "1.6", strokeLinecap: "round", strokeLinejoin: "round", children: [
|
|
238
|
+
/* @__PURE__ */ jsx("path", { d: "M3 17 C7 8, 14 8, 21 14" }),
|
|
239
|
+
/* @__PURE__ */ jsx("circle", { cx: "12", cy: "11", r: "2.2", fill: "currentColor", stroke: "none" })
|
|
240
|
+
] });
|
|
241
|
+
}
|
|
242
|
+
function IntersectIcon() {
|
|
243
|
+
return /* @__PURE__ */ jsxs("svg", { width: "16", height: "16", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "1.6", strokeLinecap: "round", strokeLinejoin: "round", children: [
|
|
244
|
+
/* @__PURE__ */ jsx("path", { d: "M3 17 C8 5, 14 5, 21 17" }),
|
|
245
|
+
/* @__PURE__ */ jsx("path", { d: "M3 5 C8 17, 14 17, 21 5" }),
|
|
246
|
+
/* @__PURE__ */ jsx("circle", { cx: "12", cy: "11", r: "1.6", fill: "currentColor", stroke: "none" })
|
|
247
|
+
] });
|
|
248
|
+
}
|
|
249
|
+
function TangentIcon() {
|
|
250
|
+
return /* @__PURE__ */ jsxs("svg", { width: "16", height: "16", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "1.6", strokeLinecap: "round", strokeLinejoin: "round", children: [
|
|
251
|
+
/* @__PURE__ */ jsx("path", { d: "M3 17 C8 7, 14 7, 21 16" }),
|
|
252
|
+
/* @__PURE__ */ jsx("line", { x1: "4", y1: "14", x2: "20", y2: "6" }),
|
|
253
|
+
/* @__PURE__ */ jsx("circle", { cx: "12", cy: "10", r: "1.8", fill: "currentColor", stroke: "none" })
|
|
254
|
+
] });
|
|
255
|
+
}
|
|
256
|
+
var TOOL_ICONS = {
|
|
257
|
+
move: /* @__PURE__ */ jsx(MoveIcon, {}),
|
|
258
|
+
"point-on-curve": /* @__PURE__ */ jsx(PointOnCurveIcon, {}),
|
|
259
|
+
intersect: /* @__PURE__ */ jsx(IntersectIcon, {}),
|
|
260
|
+
tangent: /* @__PURE__ */ jsx(TangentIcon, {})
|
|
261
|
+
};
|
|
262
|
+
function Section({ label, children }) {
|
|
263
|
+
return /* @__PURE__ */ jsxs("section", { children: [
|
|
264
|
+
/* @__PURE__ */ jsx("h4", { className: "mb-1.5 text-[10px] font-semibold uppercase tracking-wider text-slate-500", children: label }),
|
|
265
|
+
children
|
|
266
|
+
] });
|
|
267
|
+
}
|
|
268
|
+
function PanelBody(props) {
|
|
269
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
270
|
+
/* @__PURE__ */ jsx(Section, { label: "B\u1ED1 c\u1EE5c", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2 flex-wrap text-[11px] text-slate-700", children: [
|
|
271
|
+
/* @__PURE__ */ jsxs("label", { className: "inline-flex select-none items-center gap-1.5", children: [
|
|
272
|
+
/* @__PURE__ */ jsx(
|
|
273
|
+
"input",
|
|
274
|
+
{
|
|
275
|
+
type: "checkbox",
|
|
276
|
+
checked: props.showAxis,
|
|
277
|
+
onChange: (e) => props.onShowAxisChange(e.target.checked),
|
|
278
|
+
"data-testid": "toggle-axis"
|
|
279
|
+
}
|
|
280
|
+
),
|
|
281
|
+
"Tr\u1EE5c"
|
|
282
|
+
] }),
|
|
283
|
+
/* @__PURE__ */ jsxs("label", { className: "inline-flex select-none items-center gap-1.5", children: [
|
|
284
|
+
/* @__PURE__ */ jsx(
|
|
285
|
+
"input",
|
|
286
|
+
{
|
|
287
|
+
type: "checkbox",
|
|
288
|
+
checked: props.showGrid,
|
|
289
|
+
onChange: (e) => props.onShowGridChange(e.target.checked),
|
|
290
|
+
"data-testid": "toggle-grid"
|
|
291
|
+
}
|
|
292
|
+
),
|
|
293
|
+
"L\u01B0\u1EDBi"
|
|
294
|
+
] }),
|
|
295
|
+
/* @__PURE__ */ jsx(
|
|
296
|
+
"button",
|
|
297
|
+
{
|
|
298
|
+
type: "button",
|
|
299
|
+
onClick: props.onResetView,
|
|
300
|
+
title: "\u0110\u1EB7t l\u1EA1i t\u1EA7m nh\xECn",
|
|
301
|
+
"aria-label": "\u0110\u1EB7t l\u1EA1i t\u1EA7m nh\xECn",
|
|
302
|
+
className: "ml-auto inline-flex items-center justify-center rounded p-1 text-slate-600 transition hover:bg-slate-100 hover:text-slate-900",
|
|
303
|
+
children: /* @__PURE__ */ jsx(ResetViewIcon, {})
|
|
304
|
+
}
|
|
305
|
+
),
|
|
306
|
+
/* @__PURE__ */ jsx(
|
|
307
|
+
"button",
|
|
308
|
+
{
|
|
309
|
+
type: "button",
|
|
310
|
+
onClick: props.onUndo,
|
|
311
|
+
disabled: !props.canUndo,
|
|
312
|
+
title: "Ho\xE0n t\xE1c (Ctrl/Cmd+Z)",
|
|
313
|
+
"aria-label": "Ho\xE0n t\xE1c",
|
|
314
|
+
className: "inline-flex items-center justify-center rounded p-1 text-slate-600 transition hover:bg-slate-100 hover:text-slate-900 disabled:cursor-not-allowed disabled:text-slate-300 disabled:hover:bg-transparent",
|
|
315
|
+
children: /* @__PURE__ */ jsx(UndoIcon, {})
|
|
316
|
+
}
|
|
317
|
+
)
|
|
318
|
+
] }) }),
|
|
319
|
+
/* @__PURE__ */ jsx(Section, { label: "C\xF4ng c\u1EE5", children: /* @__PURE__ */ jsx("div", { className: "grid grid-cols-4 gap-1", children: GRAPH_TOOLS.map((t) => {
|
|
320
|
+
const isActive = props.activeTool === t.id;
|
|
321
|
+
return /* @__PURE__ */ jsx(
|
|
322
|
+
"button",
|
|
323
|
+
{
|
|
324
|
+
type: "button",
|
|
325
|
+
"aria-label": t.title,
|
|
326
|
+
title: t.title,
|
|
327
|
+
"aria-pressed": isActive,
|
|
328
|
+
onClick: () => props.onToolChange(t.id),
|
|
329
|
+
"data-testid": `graph-tool-${t.id}`,
|
|
330
|
+
className: [
|
|
331
|
+
"flex h-8 items-center justify-center rounded-md transition",
|
|
332
|
+
isActive ? "bg-orange-600 text-white shadow-sm" : "text-slate-700 hover:bg-slate-100 hover:text-slate-900"
|
|
333
|
+
].join(" "),
|
|
334
|
+
children: TOOL_ICONS[t.id]
|
|
335
|
+
},
|
|
336
|
+
t.id
|
|
337
|
+
);
|
|
338
|
+
}) }) }),
|
|
339
|
+
/* @__PURE__ */ jsx(Section, { label: "H\xE0m s\u1ED1", children: /* @__PURE__ */ jsx(
|
|
340
|
+
AlgebraView,
|
|
341
|
+
{
|
|
342
|
+
graph: props.graph,
|
|
343
|
+
errors: props.errors,
|
|
344
|
+
onAddFunctionDraft: props.onAddFunctionDraft,
|
|
345
|
+
onCommitFunctionExpr: props.onCommitFunctionExpr,
|
|
346
|
+
onToggleFunctionVisible: props.onToggleFunctionVisible,
|
|
347
|
+
onRemoveFunction: props.onRemoveFunction,
|
|
348
|
+
onParameterChange: props.onParameterChange,
|
|
349
|
+
onParameterRangeChange: props.onParameterRangeChange,
|
|
350
|
+
onRemoveParameter: props.onRemoveParameter
|
|
351
|
+
}
|
|
352
|
+
) })
|
|
353
|
+
] });
|
|
354
|
+
}
|
|
355
|
+
function GraphLeftPanel(props) {
|
|
356
|
+
const { isMobile, drawerOpen, isDark, onClose, onDrawerClose } = props;
|
|
357
|
+
if (isMobile && !drawerOpen) return null;
|
|
358
|
+
const handleClose = isMobile ? onDrawerClose : onClose;
|
|
359
|
+
return /* @__PURE__ */ jsxs(
|
|
360
|
+
"aside",
|
|
361
|
+
{
|
|
362
|
+
role: "complementary",
|
|
363
|
+
"aria-label": "\u0110\u1ED3 th\u1ECB 2D",
|
|
364
|
+
"data-testid": "graph-left-panel",
|
|
365
|
+
"data-stamp-area": "true",
|
|
366
|
+
className: [
|
|
367
|
+
isDark ? "theme--dark " : "",
|
|
368
|
+
isMobile ? "fixed inset-y-0 left-0 z-50 flex w-72 max-w-[85vw] flex-col bg-white shadow-2xl animate-in slide-in-from-left duration-200" : "absolute left-0 top-0 z-30 flex h-full w-60 flex-col border-r border-slate-200 bg-white shadow-md animate-in slide-in-from-left duration-200"
|
|
369
|
+
].join(" "),
|
|
370
|
+
children: [
|
|
371
|
+
/* @__PURE__ */ jsxs("header", { className: "flex items-center justify-between border-b border-slate-200 bg-gradient-to-r from-slate-50 to-white px-3 py-2", children: [
|
|
372
|
+
/* @__PURE__ */ jsxs("h3", { className: "flex items-center gap-2 text-sm font-semibold text-slate-800", children: [
|
|
373
|
+
/* @__PURE__ */ jsx("span", { className: "text-base leading-none", children: GraphIconHeader }),
|
|
374
|
+
"\u0110\u1ED3 th\u1ECB 2D"
|
|
375
|
+
] }),
|
|
376
|
+
/* @__PURE__ */ jsx(
|
|
377
|
+
"button",
|
|
378
|
+
{
|
|
379
|
+
onClick: handleClose,
|
|
380
|
+
"aria-label": "\u0110\xF3ng",
|
|
381
|
+
className: "rounded p-1 text-slate-500 transition hover:bg-slate-100 hover:text-slate-800",
|
|
382
|
+
children: /* @__PURE__ */ jsx(CloseIcon, {})
|
|
383
|
+
}
|
|
384
|
+
)
|
|
385
|
+
] }),
|
|
386
|
+
/* @__PURE__ */ jsx("div", { className: "min-h-0 flex-1 overflow-y-auto p-3 space-y-4", children: /* @__PURE__ */ jsx(PanelBody, { ...props }) })
|
|
387
|
+
]
|
|
388
|
+
}
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
function MiniBoard({ graph, activeTool, isDark, onBoardEvent }) {
|
|
392
|
+
const containerRef = useRef(null);
|
|
393
|
+
const boardRef = useRef(null);
|
|
394
|
+
const curvesRef = useRef(/* @__PURE__ */ new Map());
|
|
395
|
+
useEffect(() => {
|
|
396
|
+
let cancelled = false;
|
|
397
|
+
let createdBoard = null;
|
|
398
|
+
const containerEl = containerRef.current;
|
|
399
|
+
if (!containerEl) return;
|
|
400
|
+
const containerId = `jxg_graph2d_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
401
|
+
containerEl.id = containerId;
|
|
402
|
+
(async () => {
|
|
403
|
+
const JXG = (await import('jsxgraph')).default;
|
|
404
|
+
if (cancelled) return;
|
|
405
|
+
const opts = JXG.Options;
|
|
406
|
+
if (opts) {
|
|
407
|
+
opts.text = opts.text || {};
|
|
408
|
+
opts.text.display = "internal";
|
|
409
|
+
opts.label = opts.label || {};
|
|
410
|
+
opts.label.display = "internal";
|
|
411
|
+
}
|
|
412
|
+
const board = JXG.JSXGraph.initBoard(containerId, {
|
|
413
|
+
boundingbox: [graph.view.xMin, graph.view.yMax, graph.view.xMax, graph.view.yMin],
|
|
414
|
+
axis: graph.view.showAxis,
|
|
415
|
+
grid: graph.view.showGrid,
|
|
416
|
+
showCopyright: false,
|
|
417
|
+
showNavigation: true,
|
|
418
|
+
pan: { enabled: true, needShift: false },
|
|
419
|
+
zoom: { wheel: true, needShift: false },
|
|
420
|
+
keepAspectRatio: false
|
|
421
|
+
});
|
|
422
|
+
boardRef.current = board;
|
|
423
|
+
createdBoard = board;
|
|
424
|
+
syncObjects(board, graph, curvesRef.current);
|
|
425
|
+
board.on("boundingbox", () => {
|
|
426
|
+
const bb = board.getBoundingBox();
|
|
427
|
+
onBoardEvent({
|
|
428
|
+
type: "view-change",
|
|
429
|
+
view: {
|
|
430
|
+
xMin: bb[0],
|
|
431
|
+
xMax: bb[2],
|
|
432
|
+
yMax: bb[1],
|
|
433
|
+
yMin: bb[3],
|
|
434
|
+
showAxis: graph.view.showAxis,
|
|
435
|
+
showGrid: graph.view.showGrid
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
});
|
|
439
|
+
board.on("down", (ev) => {
|
|
440
|
+
const usrCoords = board.getUsrCoordsOfMouse?.(ev);
|
|
441
|
+
const x = usrCoords?.[0] ?? 0;
|
|
442
|
+
const y = usrCoords?.[1] ?? 0;
|
|
443
|
+
let functionId;
|
|
444
|
+
for (const [id, ref] of curvesRef.current) {
|
|
445
|
+
const obj = ref.obj;
|
|
446
|
+
if (obj?.hasPoint && obj.hasPoint(ev.clientX ?? 0, ev.clientY ?? 0)) {
|
|
447
|
+
functionId = id;
|
|
448
|
+
break;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
if (functionId) onBoardEvent({ type: "click-curve", functionId, x, y });
|
|
452
|
+
else onBoardEvent({ type: "click-empty", x, y });
|
|
453
|
+
});
|
|
454
|
+
})().catch((err) => console.error("MiniBoard init failed:", err));
|
|
455
|
+
return () => {
|
|
456
|
+
cancelled = true;
|
|
457
|
+
try {
|
|
458
|
+
if (createdBoard) __require("jsxgraph").default.JSXGraph.freeBoard(createdBoard);
|
|
459
|
+
} catch {
|
|
460
|
+
}
|
|
461
|
+
boardRef.current = null;
|
|
462
|
+
curvesRef.current.clear();
|
|
463
|
+
};
|
|
464
|
+
}, []);
|
|
465
|
+
useEffect(() => {
|
|
466
|
+
if (!boardRef.current) return;
|
|
467
|
+
syncObjects(boardRef.current, graph, curvesRef.current);
|
|
468
|
+
}, [graph]);
|
|
469
|
+
useEffect(() => {
|
|
470
|
+
const el = containerRef.current;
|
|
471
|
+
if (!el) return;
|
|
472
|
+
el.style.cursor = activeTool === "move" ? "" : "crosshair";
|
|
473
|
+
}, [activeTool]);
|
|
474
|
+
return /* @__PURE__ */ jsx(
|
|
475
|
+
"div",
|
|
476
|
+
{
|
|
477
|
+
ref: containerRef,
|
|
478
|
+
className: "graph-miniboard",
|
|
479
|
+
style: { width: "100%", height: "100%", minHeight: "300px" },
|
|
480
|
+
"data-testid": "graph-miniboard"
|
|
481
|
+
}
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
function paramSig(graph) {
|
|
485
|
+
return graph.parameters.map((p) => `${p.name}=${p.value}`).join(",");
|
|
486
|
+
}
|
|
487
|
+
function syncObjects(board, graph, curves) {
|
|
488
|
+
const sig = paramSig(graph);
|
|
489
|
+
const paramMap = {};
|
|
490
|
+
for (const p of graph.parameters) paramMap[p.name] = p.value;
|
|
491
|
+
const wantedIds = new Set(graph.functions.map((f) => f.id));
|
|
492
|
+
for (const [id, ref] of curves) {
|
|
493
|
+
if (!wantedIds.has(id)) {
|
|
494
|
+
try {
|
|
495
|
+
board.removeObject(ref.obj);
|
|
496
|
+
} catch {
|
|
497
|
+
}
|
|
498
|
+
curves.delete(id);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
for (const f of graph.functions) {
|
|
502
|
+
const existing = curves.get(f.id);
|
|
503
|
+
const needsRecreate = !existing || existing.expression !== f.expression || existing.color !== f.color || existing.visible !== f.visible || existing.paramSignature !== sig;
|
|
504
|
+
if (!needsRecreate) continue;
|
|
505
|
+
if (existing) {
|
|
506
|
+
try {
|
|
507
|
+
board.removeObject(existing.obj);
|
|
508
|
+
} catch {
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
if (!f.visible) {
|
|
512
|
+
curves.delete(f.id);
|
|
513
|
+
continue;
|
|
514
|
+
}
|
|
515
|
+
const compiled = compile(f.expression, paramMap);
|
|
516
|
+
if (typeof compiled !== "function") continue;
|
|
517
|
+
const domain = f.domain ?? { min: graph.view.xMin, max: graph.view.xMax };
|
|
518
|
+
const obj = board.create("functiongraph", [compiled, domain.min, domain.max], {
|
|
519
|
+
strokeColor: f.color,
|
|
520
|
+
strokeWidth: 2,
|
|
521
|
+
name: f.name,
|
|
522
|
+
withLabel: false,
|
|
523
|
+
highlight: false
|
|
524
|
+
});
|
|
525
|
+
curves.set(f.id, {
|
|
526
|
+
obj,
|
|
527
|
+
expression: f.expression,
|
|
528
|
+
color: f.color,
|
|
529
|
+
visible: f.visible,
|
|
530
|
+
paramSignature: sig
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
for (const point of graph.points) {
|
|
534
|
+
const fn = graph.functions.find((f) => f.id === point.functionId);
|
|
535
|
+
if (!fn || !fn.visible) continue;
|
|
536
|
+
const compiled = compile(fn.expression, paramMap);
|
|
537
|
+
if (typeof compiled !== "function") continue;
|
|
538
|
+
const y = compiled(point.x);
|
|
539
|
+
board.create("point", [point.x, y], {
|
|
540
|
+
name: point.label ?? "",
|
|
541
|
+
size: 3,
|
|
542
|
+
fillColor: fn.color,
|
|
543
|
+
strokeColor: fn.color,
|
|
544
|
+
withLabel: !!point.label
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
for (const inter of graph.intersections) {
|
|
548
|
+
const fa = graph.functions.find((f) => f.id === inter.functionIdA);
|
|
549
|
+
const fb = graph.functions.find((f) => f.id === inter.functionIdB);
|
|
550
|
+
if (!fa || !fb || !fa.visible || !fb.visible) continue;
|
|
551
|
+
const cfa = compile(fa.expression, paramMap);
|
|
552
|
+
const cfb = compile(fb.expression, paramMap);
|
|
553
|
+
if (typeof cfa !== "function" || typeof cfb !== "function") continue;
|
|
554
|
+
const roots = scanRoots((x) => cfa(x) - cfb(x), graph.view.xMin, graph.view.xMax);
|
|
555
|
+
for (const x of roots) {
|
|
556
|
+
board.create("point", [x, cfa(x)], {
|
|
557
|
+
size: 3,
|
|
558
|
+
fillColor: "#000",
|
|
559
|
+
strokeColor: "#000"
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
for (const tan of graph.tangents) {
|
|
564
|
+
const pt = graph.points.find((p) => p.id === tan.pointId);
|
|
565
|
+
if (!pt) continue;
|
|
566
|
+
const fn = graph.functions.find((f) => f.id === pt.functionId);
|
|
567
|
+
if (!fn || !fn.visible) continue;
|
|
568
|
+
const slope = numericalDerivative(fn.expression, paramMap, pt.x);
|
|
569
|
+
const cfn = compile(fn.expression, paramMap);
|
|
570
|
+
if (typeof cfn !== "function" || !Number.isFinite(slope)) continue;
|
|
571
|
+
const y0 = cfn(pt.x);
|
|
572
|
+
const x1 = graph.view.xMin;
|
|
573
|
+
const x2 = graph.view.xMax;
|
|
574
|
+
board.create(
|
|
575
|
+
"line",
|
|
576
|
+
[
|
|
577
|
+
[x1, slope * (x1 - pt.x) + y0],
|
|
578
|
+
[x2, slope * (x2 - pt.x) + y0]
|
|
579
|
+
],
|
|
580
|
+
{
|
|
581
|
+
strokeColor: fn.color,
|
|
582
|
+
strokeWidth: 1,
|
|
583
|
+
dash: 2,
|
|
584
|
+
straightFirst: false,
|
|
585
|
+
straightLast: false
|
|
586
|
+
}
|
|
587
|
+
);
|
|
588
|
+
}
|
|
589
|
+
board.update();
|
|
590
|
+
}
|
|
591
|
+
function scanRoots(fn, xMin, xMax, samples = 200) {
|
|
592
|
+
const roots = [];
|
|
593
|
+
const step = (xMax - xMin) / samples;
|
|
594
|
+
let prevX = xMin;
|
|
595
|
+
let prevY = fn(prevX);
|
|
596
|
+
for (let i = 1; i <= samples; i++) {
|
|
597
|
+
const x = xMin + i * step;
|
|
598
|
+
const y = fn(x);
|
|
599
|
+
if (Number.isFinite(prevY) && Number.isFinite(y) && prevY * y < 0) {
|
|
600
|
+
let a = prevX;
|
|
601
|
+
let b = x;
|
|
602
|
+
let ya = prevY;
|
|
603
|
+
for (let j = 0; j < 30; j++) {
|
|
604
|
+
const m = (a + b) / 2;
|
|
605
|
+
const ym = fn(m);
|
|
606
|
+
if (Math.abs(ym) < 1e-6) {
|
|
607
|
+
a = b = m;
|
|
608
|
+
break;
|
|
609
|
+
}
|
|
610
|
+
if (ya * ym < 0) {
|
|
611
|
+
b = m;
|
|
612
|
+
} else {
|
|
613
|
+
a = m;
|
|
614
|
+
ya = ym;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
roots.push((a + b) / 2);
|
|
618
|
+
}
|
|
619
|
+
prevX = x;
|
|
620
|
+
prevY = y;
|
|
621
|
+
}
|
|
622
|
+
return roots;
|
|
623
|
+
}
|
|
624
|
+
var GraphEditorPanel = forwardRef(function GraphEditorPanel2(props, ref) {
|
|
625
|
+
const initialGraph = props.initialState ?? EMPTY_GRAPH;
|
|
626
|
+
const graphRef = useRef(initialGraph);
|
|
627
|
+
const [, forceUpdate] = useState(0);
|
|
628
|
+
const [errors, setErrors] = useState({});
|
|
629
|
+
const [tool, setToolState] = useState("move");
|
|
630
|
+
const undoStackRef = useRef([]);
|
|
631
|
+
const idCounterRef = useRef(1);
|
|
632
|
+
const toolRef = useRef(tool);
|
|
633
|
+
toolRef.current = tool;
|
|
634
|
+
const intersectFirstRef = useRef(null);
|
|
635
|
+
const propsRef = useRef(props);
|
|
636
|
+
propsRef.current = props;
|
|
637
|
+
const initialGraphNotifiedRef = useRef(false);
|
|
638
|
+
const pushUndo = useCallback((g) => {
|
|
639
|
+
undoStackRef.current.push(g);
|
|
640
|
+
if (undoStackRef.current.length > 30) undoStackRef.current.shift();
|
|
641
|
+
}, []);
|
|
642
|
+
const setErrorsWithNotify = useCallback(
|
|
643
|
+
(updater) => {
|
|
644
|
+
setErrors((prev) => {
|
|
645
|
+
const next = updater(prev);
|
|
646
|
+
propsRef.current.onErrorsChange?.(next);
|
|
647
|
+
return next;
|
|
648
|
+
});
|
|
649
|
+
},
|
|
650
|
+
[]
|
|
651
|
+
);
|
|
652
|
+
const notifyStateChange = useCallback((g, t) => {
|
|
653
|
+
propsRef.current.onStateChange({
|
|
654
|
+
tool: t,
|
|
655
|
+
showAxis: g.view.showAxis,
|
|
656
|
+
showGrid: g.view.showGrid,
|
|
657
|
+
canUndo: undoStackRef.current.length > 0
|
|
658
|
+
});
|
|
659
|
+
}, []);
|
|
660
|
+
const updateGraph = useCallback(
|
|
661
|
+
(mutator) => {
|
|
662
|
+
const prev = graphRef.current;
|
|
663
|
+
pushUndo(prev);
|
|
664
|
+
const next = mutator(prev);
|
|
665
|
+
graphRef.current = next;
|
|
666
|
+
notifyStateChange(next, toolRef.current);
|
|
667
|
+
forceUpdate((n) => n + 1);
|
|
668
|
+
propsRef.current.onGraphChange?.(next);
|
|
669
|
+
},
|
|
670
|
+
[pushUndo, notifyStateChange]
|
|
671
|
+
);
|
|
672
|
+
const onBoardEvent = useCallback((ev) => {
|
|
673
|
+
const currentTool = toolRef.current;
|
|
674
|
+
if (currentTool === "point-on-curve" && ev.type === "click-curve" && ev.functionId && ev.x !== void 0) {
|
|
675
|
+
updateGraph(
|
|
676
|
+
(g) => addPointOnCurve(
|
|
677
|
+
g,
|
|
678
|
+
{ x: ev.x, y: ev.y ?? 0, functionId: ev.functionId },
|
|
679
|
+
() => `p${idCounterRef.current++}`
|
|
680
|
+
)
|
|
681
|
+
);
|
|
682
|
+
setToolState("move");
|
|
683
|
+
} else if (currentTool === "intersect" && ev.type === "click-curve" && ev.functionId) {
|
|
684
|
+
if (!intersectFirstRef.current) {
|
|
685
|
+
intersectFirstRef.current = ev.functionId;
|
|
686
|
+
} else {
|
|
687
|
+
const a = intersectFirstRef.current;
|
|
688
|
+
const b = ev.functionId;
|
|
689
|
+
intersectFirstRef.current = null;
|
|
690
|
+
updateGraph(
|
|
691
|
+
(g) => addIntersection(g, a, b, () => `i${idCounterRef.current++}`)
|
|
692
|
+
);
|
|
693
|
+
setToolState("move");
|
|
694
|
+
}
|
|
695
|
+
} else if (currentTool === "tangent" && ev.type === "click-curve" && ev.functionId && ev.x !== void 0) {
|
|
696
|
+
const pointId = `p${idCounterRef.current++}`;
|
|
697
|
+
const tangentId = `t${idCounterRef.current++}`;
|
|
698
|
+
updateGraph((g) => ({
|
|
699
|
+
...g,
|
|
700
|
+
points: [...g.points, { id: pointId, functionId: ev.functionId, x: ev.x }],
|
|
701
|
+
tangents: [...g.tangents, { id: tangentId, pointId }]
|
|
702
|
+
}));
|
|
703
|
+
setToolState("move");
|
|
704
|
+
}
|
|
705
|
+
}, [updateGraph]);
|
|
706
|
+
useImperativeHandle(
|
|
707
|
+
ref,
|
|
708
|
+
() => ({
|
|
709
|
+
insert: () => {
|
|
710
|
+
const g = graphRef.current;
|
|
711
|
+
if (g.functions.length === 0) return false;
|
|
712
|
+
const jsonState = stringifySerializedGraph(g);
|
|
713
|
+
renderGraph2dSvgFromState(jsonState).then((svg) => propsRef.current.onInsert(jsonState, svg)).catch((err) => console.error("Graph2D insert render failed:", err));
|
|
714
|
+
return true;
|
|
715
|
+
},
|
|
716
|
+
hasContent: () => graphRef.current.functions.length > 0,
|
|
717
|
+
setTool: (t) => {
|
|
718
|
+
setToolState(t);
|
|
719
|
+
const g = graphRef.current;
|
|
720
|
+
propsRef.current.onStateChange({
|
|
721
|
+
tool: t,
|
|
722
|
+
showAxis: g.view.showAxis,
|
|
723
|
+
showGrid: g.view.showGrid,
|
|
724
|
+
canUndo: undoStackRef.current.length > 0
|
|
725
|
+
});
|
|
726
|
+
},
|
|
727
|
+
setShowAxis: (b) => updateGraph((g) => ({ ...g, view: { ...g.view, showAxis: b } })),
|
|
728
|
+
setShowGrid: (b) => updateGraph((g) => ({ ...g, view: { ...g.view, showGrid: b } })),
|
|
729
|
+
resetView: () => updateGraph((g) => ({
|
|
730
|
+
...g,
|
|
731
|
+
view: { ...g.view, xMin: -10, xMax: 10, yMin: -10, yMax: 10 }
|
|
732
|
+
})),
|
|
733
|
+
undo: () => {
|
|
734
|
+
const prev = undoStackRef.current.pop();
|
|
735
|
+
if (!prev) return;
|
|
736
|
+
graphRef.current = prev;
|
|
737
|
+
forceUpdate((n) => n + 1);
|
|
738
|
+
propsRef.current.onStateChange({
|
|
739
|
+
tool: toolRef.current,
|
|
740
|
+
showAxis: prev.view.showAxis,
|
|
741
|
+
showGrid: prev.view.showGrid,
|
|
742
|
+
canUndo: undoStackRef.current.length > 0
|
|
743
|
+
});
|
|
744
|
+
propsRef.current.onGraphChange?.(prev);
|
|
745
|
+
},
|
|
746
|
+
addFunction: (expr) => {
|
|
747
|
+
const g = graphRef.current;
|
|
748
|
+
if (g.functions.length >= MAX_FUNCTIONS) {
|
|
749
|
+
return { ok: false, error: `T\u1ED1i \u0111a ${MAX_FUNCTIONS} h\xE0m` };
|
|
750
|
+
}
|
|
751
|
+
const v = validate(expr);
|
|
752
|
+
if (!v.ok) return { ok: false, error: v.error ?? "Invalid" };
|
|
753
|
+
const id = `f${idCounterRef.current++}`;
|
|
754
|
+
const usedNames = g.functions.map((f) => f.name);
|
|
755
|
+
const usedColors = g.functions.map((f) => f.color);
|
|
756
|
+
const newFn = {
|
|
757
|
+
id,
|
|
758
|
+
name: nextFunctionName(usedNames),
|
|
759
|
+
expression: expr,
|
|
760
|
+
color: nextColor(usedColors),
|
|
761
|
+
visible: true
|
|
762
|
+
};
|
|
763
|
+
const usedParamNames = new Set(g.parameters.map((p) => p.name));
|
|
764
|
+
const newParams = [];
|
|
765
|
+
for (const varName of v.freeVars) {
|
|
766
|
+
if (usedParamNames.has(varName)) continue;
|
|
767
|
+
if (g.parameters.length + newParams.length >= MAX_PARAMETERS) break;
|
|
768
|
+
newParams.push({ name: varName, value: 1, min: -5, max: 5, step: 0.1 });
|
|
769
|
+
}
|
|
770
|
+
updateGraph((prev) => ({
|
|
771
|
+
...prev,
|
|
772
|
+
functions: [...prev.functions, newFn],
|
|
773
|
+
parameters: [...prev.parameters, ...newParams]
|
|
774
|
+
}));
|
|
775
|
+
setErrorsWithNotify((e) => ({ ...e, [id]: null }));
|
|
776
|
+
return { ok: true, id };
|
|
777
|
+
},
|
|
778
|
+
commitFunctionExpression: (id, expr) => {
|
|
779
|
+
const g = graphRef.current;
|
|
780
|
+
const v = validate(expr);
|
|
781
|
+
if (!v.ok) {
|
|
782
|
+
setErrorsWithNotify((e) => ({ ...e, [id]: v.error ?? "Invalid" }));
|
|
783
|
+
return;
|
|
784
|
+
}
|
|
785
|
+
const usedParamNames = new Set(g.parameters.map((p) => p.name));
|
|
786
|
+
const newParams = [];
|
|
787
|
+
for (const varName of v.freeVars) {
|
|
788
|
+
if (usedParamNames.has(varName)) continue;
|
|
789
|
+
if (g.parameters.length + newParams.length >= MAX_PARAMETERS) break;
|
|
790
|
+
newParams.push({ name: varName, value: 1, min: -5, max: 5, step: 0.1 });
|
|
791
|
+
}
|
|
792
|
+
updateGraph((prev) => ({
|
|
793
|
+
...prev,
|
|
794
|
+
functions: prev.functions.map(
|
|
795
|
+
(f) => f.id === id ? { ...f, expression: expr } : f
|
|
796
|
+
),
|
|
797
|
+
parameters: [...prev.parameters, ...newParams]
|
|
798
|
+
}));
|
|
799
|
+
setErrorsWithNotify((e) => ({ ...e, [id]: null }));
|
|
800
|
+
},
|
|
801
|
+
toggleFunctionVisible: (id) => updateGraph((g) => ({
|
|
802
|
+
...g,
|
|
803
|
+
functions: g.functions.map(
|
|
804
|
+
(f) => f.id === id ? { ...f, visible: !f.visible } : f
|
|
805
|
+
)
|
|
806
|
+
})),
|
|
807
|
+
removeFunction: (id) => updateGraph((g) => ({
|
|
808
|
+
...g,
|
|
809
|
+
functions: g.functions.filter((f) => f.id !== id)
|
|
810
|
+
})),
|
|
811
|
+
// setParameter does NOT push undo — would flood the stack (slider drag)
|
|
812
|
+
setParameter: (name, value) => {
|
|
813
|
+
const next = {
|
|
814
|
+
...graphRef.current,
|
|
815
|
+
parameters: graphRef.current.parameters.map(
|
|
816
|
+
(p) => p.name === name ? { ...p, value } : p
|
|
817
|
+
)
|
|
818
|
+
};
|
|
819
|
+
graphRef.current = next;
|
|
820
|
+
forceUpdate((n) => n + 1);
|
|
821
|
+
propsRef.current.onGraphChange?.(next);
|
|
822
|
+
},
|
|
823
|
+
setParameterRange: (name, min, max, step) => updateGraph((g) => ({
|
|
824
|
+
...g,
|
|
825
|
+
parameters: g.parameters.map(
|
|
826
|
+
(p) => p.name === name ? { ...p, min, max, step, value: Math.min(max, Math.max(min, p.value)) } : p
|
|
827
|
+
)
|
|
828
|
+
})),
|
|
829
|
+
removeParameter: (name) => updateGraph((g) => ({
|
|
830
|
+
...g,
|
|
831
|
+
parameters: g.parameters.filter((p) => p.name !== name)
|
|
832
|
+
})),
|
|
833
|
+
getGraph: () => graphRef.current,
|
|
834
|
+
getErrors: () => errors
|
|
835
|
+
}),
|
|
836
|
+
// deps: updateGraph stable; errors changes when function errors change; setErrorsWithNotify stable
|
|
837
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
838
|
+
[updateGraph, errors, setErrorsWithNotify]
|
|
839
|
+
);
|
|
840
|
+
useEffect(() => {
|
|
841
|
+
if (!initialGraphNotifiedRef.current) {
|
|
842
|
+
initialGraphNotifiedRef.current = true;
|
|
843
|
+
propsRef.current.onGraphChange?.(graphRef.current);
|
|
844
|
+
}
|
|
845
|
+
}, []);
|
|
846
|
+
const graph = graphRef.current;
|
|
847
|
+
const hasContent = graph.functions.length > 0;
|
|
848
|
+
const handleInsert = () => {
|
|
849
|
+
const g = graphRef.current;
|
|
850
|
+
if (g.functions.length === 0) return;
|
|
851
|
+
const jsonState = stringifySerializedGraph(g);
|
|
852
|
+
renderGraph2dSvgFromState(jsonState).then((svg) => propsRef.current.onInsert(jsonState, svg)).catch((err) => console.error("Graph2D insert render failed:", err));
|
|
853
|
+
};
|
|
854
|
+
const { isMobile, isDark, withLeftPanel } = props;
|
|
855
|
+
const wrapperStyle = isMobile ? { position: "fixed", inset: 0, zIndex: 40 } : {
|
|
856
|
+
position: "absolute",
|
|
857
|
+
top: "50%",
|
|
858
|
+
left: withLeftPanel ? "calc(50% + 120px)" : "50%",
|
|
859
|
+
transform: "translate(-50%, -50%)",
|
|
860
|
+
zIndex: 40
|
|
861
|
+
};
|
|
862
|
+
return /* @__PURE__ */ jsxs(
|
|
863
|
+
"div",
|
|
864
|
+
{
|
|
865
|
+
role: "dialog",
|
|
866
|
+
"aria-label": "\u0110\u1ED3 th\u1ECB 2D",
|
|
867
|
+
"data-testid": "graph-editor-panel",
|
|
868
|
+
"data-stamp-area": "true",
|
|
869
|
+
"data-mobile-editor": isMobile ? "true" : void 0,
|
|
870
|
+
style: wrapperStyle,
|
|
871
|
+
className: [
|
|
872
|
+
isDark ? "theme--dark " : "",
|
|
873
|
+
"flex flex-col overflow-hidden bg-white",
|
|
874
|
+
isMobile ? "h-full w-full" : "h-[540px] max-h-[85vh] w-[640px] max-w-[calc(100vw-280px)] rounded-lg border border-slate-300 shadow-2xl ring-1 ring-black/5"
|
|
875
|
+
].join(" "),
|
|
876
|
+
children: [
|
|
877
|
+
/* @__PURE__ */ jsxs("header", { className: "flex items-center gap-2 border-b border-slate-200 bg-gradient-to-r from-orange-500 to-amber-600 px-3 py-2 text-white", children: [
|
|
878
|
+
isMobile && /* @__PURE__ */ jsx(
|
|
879
|
+
"button",
|
|
880
|
+
{
|
|
881
|
+
type: "button",
|
|
882
|
+
onClick: props.onOpenDrawer,
|
|
883
|
+
"aria-label": "M\u1EDF b\u1EA3ng \u0111\u1EA1i s\u1ED1",
|
|
884
|
+
"data-testid": "graph-drawer-toggle",
|
|
885
|
+
className: "-ml-1 inline-flex h-10 w-10 items-center justify-center rounded transition hover:bg-white/15",
|
|
886
|
+
children: /* @__PURE__ */ jsxs("svg", { width: "20", height: "20", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: [
|
|
887
|
+
/* @__PURE__ */ jsx("line", { x1: "4", y1: "6", x2: "20", y2: "6" }),
|
|
888
|
+
/* @__PURE__ */ jsx("line", { x1: "4", y1: "12", x2: "20", y2: "12" }),
|
|
889
|
+
/* @__PURE__ */ jsx("line", { x1: "4", y1: "18", x2: "20", y2: "18" })
|
|
890
|
+
] })
|
|
891
|
+
}
|
|
892
|
+
),
|
|
893
|
+
/* @__PURE__ */ jsxs("h3", { className: "flex flex-1 items-center gap-2 text-sm font-semibold", children: [
|
|
894
|
+
/* @__PURE__ */ jsxs("svg", { width: "16", height: "16", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: [
|
|
895
|
+
/* @__PURE__ */ jsx("path", { d: "M3 21 V3" }),
|
|
896
|
+
/* @__PURE__ */ jsx("path", { d: "M3 21 H21" }),
|
|
897
|
+
/* @__PURE__ */ jsx("path", { d: "M5 19 C8 5, 14 5, 19 17" })
|
|
898
|
+
] }),
|
|
899
|
+
"\u0110\u1ED3 th\u1ECB 2D"
|
|
900
|
+
] }),
|
|
901
|
+
isMobile && /* @__PURE__ */ jsx(
|
|
902
|
+
"button",
|
|
903
|
+
{
|
|
904
|
+
type: "button",
|
|
905
|
+
onClick: handleInsert,
|
|
906
|
+
disabled: !hasContent,
|
|
907
|
+
"data-testid": "graph-insert-btn-mobile",
|
|
908
|
+
className: "rounded bg-white/15 px-3 py-1.5 text-xs font-semibold transition hover:bg-white/25 disabled:opacity-50",
|
|
909
|
+
children: "Ch\xE8n"
|
|
910
|
+
}
|
|
911
|
+
),
|
|
912
|
+
/* @__PURE__ */ jsx(
|
|
913
|
+
"button",
|
|
914
|
+
{
|
|
915
|
+
onClick: props.onClose,
|
|
916
|
+
"aria-label": "\u0110\xF3ng",
|
|
917
|
+
className: "inline-flex h-9 w-9 items-center justify-center rounded transition hover:bg-white/15",
|
|
918
|
+
children: /* @__PURE__ */ jsxs("svg", { width: "18", height: "18", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: [
|
|
919
|
+
/* @__PURE__ */ jsx("line", { x1: "6", y1: "6", x2: "18", y2: "18" }),
|
|
920
|
+
/* @__PURE__ */ jsx("line", { x1: "18", y1: "6", x2: "6", y2: "18" })
|
|
921
|
+
] })
|
|
922
|
+
}
|
|
923
|
+
)
|
|
924
|
+
] }),
|
|
925
|
+
/* @__PURE__ */ jsx("div", { className: "min-h-0 flex-1", children: /* @__PURE__ */ jsx(
|
|
926
|
+
MiniBoard,
|
|
927
|
+
{
|
|
928
|
+
graph,
|
|
929
|
+
activeTool: tool,
|
|
930
|
+
isDark,
|
|
931
|
+
onBoardEvent
|
|
932
|
+
}
|
|
933
|
+
) }),
|
|
934
|
+
!isMobile && /* @__PURE__ */ jsxs("footer", { className: "flex items-center justify-between border-t border-slate-200 bg-slate-50 px-3 py-2", children: [
|
|
935
|
+
/* @__PURE__ */ jsx("span", { className: "text-xs text-slate-500", children: "Nh\u1EADp bi\u1EC3u th\u1EE9c trong b\u1EA3ng \u0111\u1EA1i s\u1ED1 b\xEAn tr\xE1i." }),
|
|
936
|
+
/* @__PURE__ */ jsxs("div", { className: "flex gap-2", children: [
|
|
937
|
+
/* @__PURE__ */ jsx(
|
|
938
|
+
"button",
|
|
939
|
+
{
|
|
940
|
+
onClick: props.onClose,
|
|
941
|
+
className: "rounded border border-slate-300 bg-white px-3 py-1 text-xs font-medium text-slate-700 transition hover:bg-slate-100",
|
|
942
|
+
children: "Hu\u1EF7"
|
|
943
|
+
}
|
|
944
|
+
),
|
|
945
|
+
/* @__PURE__ */ jsx(
|
|
946
|
+
"button",
|
|
947
|
+
{
|
|
948
|
+
onClick: handleInsert,
|
|
949
|
+
disabled: !hasContent,
|
|
950
|
+
"data-testid": "graph-insert-btn",
|
|
951
|
+
className: "rounded bg-orange-600 px-3 py-1 text-xs font-medium text-white transition hover:bg-orange-700 disabled:opacity-50",
|
|
952
|
+
children: "Ch\xE8n"
|
|
953
|
+
}
|
|
954
|
+
)
|
|
955
|
+
] })
|
|
956
|
+
] })
|
|
957
|
+
]
|
|
958
|
+
}
|
|
959
|
+
);
|
|
960
|
+
});
|
|
961
|
+
var INITIAL_GRAPH_STATE = {
|
|
962
|
+
tool: "move",
|
|
963
|
+
showAxis: true,
|
|
964
|
+
showGrid: true,
|
|
965
|
+
canUndo: false
|
|
966
|
+
};
|
|
967
|
+
var Graph2DStampHost = forwardRef(
|
|
968
|
+
function Graph2DStampHost2({ api, editingElement, onClose, isDark }, ref) {
|
|
969
|
+
const panelRef = useRef(null);
|
|
970
|
+
const [graphUIState, setGraphUIState] = useState(INITIAL_GRAPH_STATE);
|
|
971
|
+
const { isMobile } = useIsMobile();
|
|
972
|
+
const [drawerOpen, setDrawerOpen] = useState(false);
|
|
973
|
+
const initialState = useMemo(() => {
|
|
974
|
+
if (!editingElement) return null;
|
|
975
|
+
if (!isGraph2DCustomData(editingElement.customData)) return null;
|
|
976
|
+
return parseSerializedGraph(editingElement.customData.jsonState);
|
|
977
|
+
}, [editingElement]);
|
|
978
|
+
const [graphSnapshot, setGraphSnapshot] = useState(
|
|
979
|
+
initialState ?? EMPTY_GRAPH
|
|
980
|
+
);
|
|
981
|
+
const [errorsSnapshot, setErrorsSnapshot] = useState({});
|
|
982
|
+
const handleInsert = useCallback(
|
|
983
|
+
async (jsonState, svgString) => {
|
|
984
|
+
if (!api) return;
|
|
985
|
+
try {
|
|
986
|
+
await insertStampImage(api, {
|
|
987
|
+
svgString,
|
|
988
|
+
makeCustomData: (width, height) => ({
|
|
989
|
+
kind: "graph2d",
|
|
990
|
+
version: 1,
|
|
991
|
+
jsonState,
|
|
992
|
+
svgWidth: width,
|
|
993
|
+
svgHeight: height
|
|
994
|
+
}),
|
|
995
|
+
editingElementId: editingElement?.id ?? null
|
|
996
|
+
});
|
|
997
|
+
} catch (err) {
|
|
998
|
+
console.error("Graph2D insert failed:", err);
|
|
999
|
+
}
|
|
1000
|
+
onClose();
|
|
1001
|
+
},
|
|
1002
|
+
[api, editingElement?.id, onClose]
|
|
1003
|
+
);
|
|
1004
|
+
useImperativeHandle(
|
|
1005
|
+
ref,
|
|
1006
|
+
() => ({
|
|
1007
|
+
tryInsert: () => panelRef.current?.insert() ?? false,
|
|
1008
|
+
hasContent: () => panelRef.current?.hasContent() ?? false
|
|
1009
|
+
}),
|
|
1010
|
+
[]
|
|
1011
|
+
);
|
|
1012
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1013
|
+
/* @__PURE__ */ jsx(
|
|
1014
|
+
GraphLeftPanel,
|
|
1015
|
+
{
|
|
1016
|
+
activeTool: graphUIState.tool,
|
|
1017
|
+
onToolChange: (t) => panelRef.current?.setTool(t),
|
|
1018
|
+
showAxis: graphUIState.showAxis,
|
|
1019
|
+
showGrid: graphUIState.showGrid,
|
|
1020
|
+
onShowAxisChange: (b) => panelRef.current?.setShowAxis(b),
|
|
1021
|
+
onShowGridChange: (b) => panelRef.current?.setShowGrid(b),
|
|
1022
|
+
onResetView: () => panelRef.current?.resetView(),
|
|
1023
|
+
onUndo: () => panelRef.current?.undo(),
|
|
1024
|
+
canUndo: graphUIState.canUndo,
|
|
1025
|
+
onClose,
|
|
1026
|
+
isDark,
|
|
1027
|
+
isMobile,
|
|
1028
|
+
drawerOpen,
|
|
1029
|
+
onDrawerClose: () => setDrawerOpen(false),
|
|
1030
|
+
graph: graphSnapshot,
|
|
1031
|
+
errors: errorsSnapshot,
|
|
1032
|
+
onAddFunctionDraft: () => {
|
|
1033
|
+
const result = panelRef.current?.addFunction("x");
|
|
1034
|
+
if (result && !result.ok) console.warn("addFunction failed:", result.error);
|
|
1035
|
+
},
|
|
1036
|
+
onCommitFunctionExpr: (id, expr) => panelRef.current?.commitFunctionExpression(id, expr),
|
|
1037
|
+
onToggleFunctionVisible: (id) => panelRef.current?.toggleFunctionVisible(id),
|
|
1038
|
+
onRemoveFunction: (id) => panelRef.current?.removeFunction(id),
|
|
1039
|
+
onParameterChange: (name, v) => panelRef.current?.setParameter(name, v),
|
|
1040
|
+
onParameterRangeChange: (name, min, max, step) => panelRef.current?.setParameterRange(name, min, max, step),
|
|
1041
|
+
onRemoveParameter: (name) => panelRef.current?.removeParameter(name)
|
|
1042
|
+
}
|
|
1043
|
+
),
|
|
1044
|
+
/* @__PURE__ */ jsx(
|
|
1045
|
+
GraphEditorPanel,
|
|
1046
|
+
{
|
|
1047
|
+
ref: panelRef,
|
|
1048
|
+
initialState,
|
|
1049
|
+
onInsert: handleInsert,
|
|
1050
|
+
onClose,
|
|
1051
|
+
onStateChange: setGraphUIState,
|
|
1052
|
+
onGraphChange: setGraphSnapshot,
|
|
1053
|
+
onErrorsChange: setErrorsSnapshot,
|
|
1054
|
+
withLeftPanel: !isMobile,
|
|
1055
|
+
isDark,
|
|
1056
|
+
isMobile,
|
|
1057
|
+
onOpenDrawer: () => setDrawerOpen(true)
|
|
1058
|
+
}
|
|
1059
|
+
)
|
|
1060
|
+
] });
|
|
1061
|
+
}
|
|
1062
|
+
);
|
|
1063
|
+
|
|
1064
|
+
export { Graph2DStampHost };
|
|
1065
|
+
//# sourceMappingURL=host-2QGKMGCT.mjs.map
|
|
1066
|
+
//# sourceMappingURL=host-2QGKMGCT.mjs.map
|