@xom11/whiteboard 0.6.1 → 0.6.2

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/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  "use client";
2
+ require('./index.css');
2
3
  'use strict';
3
4
 
4
5
  var excalidraw = require('@excalidraw/excalidraw');
@@ -6,13 +7,11 @@ var jsxRuntime = require('react/jsx-runtime');
6
7
  var dynamic = require('next/dynamic');
7
8
  var react = require('react');
8
9
  var reactDom = require('react-dom');
9
- var JXG = require('jsxgraph');
10
10
  require('@excalidraw/excalidraw/index.css');
11
11
 
12
12
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
13
13
 
14
14
  var dynamic__default = /*#__PURE__*/_interopDefault(dynamic);
15
- var JXG__default = /*#__PURE__*/_interopDefault(JXG);
16
15
 
17
16
  var __defProp = Object.defineProperty;
18
17
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -678,9 +677,9 @@ function handleMove(ctx, e) {
678
677
  if (!ctx.boardRef.current || !ctx.phantomRef.current) return;
679
678
  try {
680
679
  const coords = ctx.boardRef.current.getUsrCoordsOfMouse(e);
681
- const JXG3 = ctx.jxgRef.current;
682
- if (!JXG3) return;
683
- ctx.phantomRef.current.setPositionDirectly(JXG3.COORDS_BY_USER, [coords[0], coords[1]]);
680
+ const JXG = ctx.jxgRef.current;
681
+ if (!JXG) return;
682
+ ctx.phantomRef.current.setPositionDirectly(JXG.COORDS_BY_USER, [coords[0], coords[1]]);
684
683
  ctx.boardRef.current.update();
685
684
  } catch {
686
685
  }
@@ -1487,11 +1486,11 @@ var JSXGraphMiniBoard = ({ onReady, initialState, isDark }) => {
1487
1486
  if (typeof window === "undefined" || !containerRef.current) return;
1488
1487
  let cancelled = false;
1489
1488
  (async () => {
1490
- const JXG3 = (await import('jsxgraph')).default;
1489
+ const JXG = (await import('jsxgraph')).default;
1491
1490
  if (cancelled || !containerRef.current) return;
1492
- jxgRef.current = JXG3;
1491
+ jxgRef.current = JXG;
1493
1492
  try {
1494
- const opts = JXG3.Options;
1493
+ const opts = JXG.Options;
1495
1494
  if (opts) {
1496
1495
  opts.text = opts.text || {};
1497
1496
  opts.text.display = "internal";
@@ -1505,7 +1504,7 @@ var JSXGraphMiniBoard = ({ onReady, initialState, isDark }) => {
1505
1504
  }
1506
1505
  } catch {
1507
1506
  }
1508
- const board = JXG3.JSXGraph.initBoard(containerId, {
1507
+ const board = JXG.JSXGraph.initBoard(containerId, {
1509
1508
  boundingbox: initialState?.bbox ?? [-10, 10, 10, -10],
1510
1509
  axis: false,
1511
1510
  // We manage axis manually via toggle for clean default
@@ -2137,9 +2136,9 @@ function renderGeometryToSvg(boardContainer) {
2137
2136
  async function renderGeometrySvgFromState(jsonState) {
2138
2137
  const parsed = JSON.parse(jsonState);
2139
2138
  const palette = paletteFor(false);
2140
- const JXG3 = (await import('jsxgraph')).default;
2139
+ const JXG = (await import('jsxgraph')).default;
2141
2140
  try {
2142
- const opts = JXG3.Options;
2141
+ const opts = JXG.Options;
2143
2142
  if (opts) {
2144
2143
  opts.text = opts.text || {};
2145
2144
  opts.text.display = "internal";
@@ -2164,7 +2163,7 @@ async function renderGeometrySvgFromState(jsonState) {
2164
2163
  document.body.appendChild(container);
2165
2164
  let board = null;
2166
2165
  try {
2167
- board = JXG3.JSXGraph.initBoard(containerId, {
2166
+ board = JXG.JSXGraph.initBoard(containerId, {
2168
2167
  boundingbox: parsed.bbox,
2169
2168
  axis: !!parsed.showAxis,
2170
2169
  grid: !!parsed.showGrid,
@@ -2177,7 +2176,7 @@ async function renderGeometrySvgFromState(jsonState) {
2177
2176
  return renderGeometryToSvg(container);
2178
2177
  } finally {
2179
2178
  try {
2180
- if (board) JXG3.JSXGraph.freeBoard(board);
2179
+ if (board) JXG.JSXGraph.freeBoard(board);
2181
2180
  } catch {
2182
2181
  }
2183
2182
  if (container.parentNode) container.parentNode.removeChild(container);
@@ -3849,126 +3848,134 @@ var MiniBoard3D = react.forwardRef(function MiniBoard3D2({ isDark, initialState
3849
3848
  react.useEffect(() => {
3850
3849
  const div = containerRef.current;
3851
3850
  if (!div) return;
3852
- JXG__default.default.Options.text.display = "internal";
3853
- const board = JXG__default.default.JSXGraph.initBoard(div, {
3854
- boundingbox: [-6, 6, 6, -6],
3855
- axis: false,
3856
- showCopyright: false,
3857
- showNavigation: false,
3858
- renderer: "svg"
3859
- });
3860
- boardRef.current = board;
3861
- const initView = initialState?.view ?? DEFAULT_VIEW3D;
3862
- const baseAttrs = VIEW3D_ATTRS(isDark);
3863
- const view = board.create(
3864
- "view3d",
3865
- [
3866
- [-5, -5],
3867
- [10, 10],
3851
+ let cancelled = false;
3852
+ let JXG = null;
3853
+ let board = null;
3854
+ void (async () => {
3855
+ JXG = (await import('jsxgraph')).default;
3856
+ if (cancelled || !containerRef.current) return;
3857
+ JXG.Options.text.display = "internal";
3858
+ board = JXG.JSXGraph.initBoard(div, {
3859
+ boundingbox: [-6, 6, 6, -6],
3860
+ axis: false,
3861
+ showCopyright: false,
3862
+ showNavigation: false,
3863
+ renderer: "svg"
3864
+ });
3865
+ boardRef.current = board;
3866
+ const initView = initialState?.view ?? DEFAULT_VIEW3D;
3867
+ const baseAttrs = VIEW3D_ATTRS(isDark);
3868
+ const view = board.create(
3869
+ "view3d",
3868
3870
  [
3869
- [initView.bbox3D[0], initView.bbox3D[3]],
3870
- [initView.bbox3D[1], initView.bbox3D[4]],
3871
- [initView.bbox3D[2], initView.bbox3D[5]]
3872
- ]
3873
- ],
3874
- {
3875
- ...baseAttrs,
3876
- az: { ...baseAttrs.az, value: initView.azimuth },
3877
- el: { ...baseAttrs.el, value: initView.elevation }
3878
- }
3879
- );
3880
- viewRef.current = view;
3881
- let idCounter = 1;
3882
- const ctx = createHandlerContext({
3883
- view,
3884
- pushLog: (e) => {
3885
- logRef.current.push(e);
3886
- notify();
3887
- },
3888
- objMap: objMapRef.current,
3889
- nextId: () => `obj_${Date.now().toString(36)}_${(idCounter++).toString(36)}`,
3890
- isDark,
3891
- promptCoords: (label) => {
3892
- const raw = window.prompt(`${label}
3871
+ [-5, -5],
3872
+ [10, 10],
3873
+ [
3874
+ [initView.bbox3D[0], initView.bbox3D[3]],
3875
+ [initView.bbox3D[1], initView.bbox3D[4]],
3876
+ [initView.bbox3D[2], initView.bbox3D[5]]
3877
+ ]
3878
+ ],
3879
+ {
3880
+ ...baseAttrs,
3881
+ az: { ...baseAttrs.az, value: initView.azimuth },
3882
+ el: { ...baseAttrs.el, value: initView.elevation }
3883
+ }
3884
+ );
3885
+ viewRef.current = view;
3886
+ let idCounter = 1;
3887
+ const ctx = createHandlerContext({
3888
+ view,
3889
+ pushLog: (e) => {
3890
+ logRef.current.push(e);
3891
+ notify();
3892
+ },
3893
+ objMap: objMapRef.current,
3894
+ nextId: () => `obj_${Date.now().toString(36)}_${(idCounter++).toString(36)}`,
3895
+ isDark,
3896
+ promptCoords: (label) => {
3897
+ const raw = window.prompt(`${label}
3893
3898
  (\u0111\u1ECBnh d\u1EA1ng "x,y,z")`, "0,0,0");
3894
- if (!raw) return null;
3895
- const parts = raw.split(",").map((s) => Number(s.trim()));
3896
- if (parts.length !== 3 || parts.some((n) => !isFinite(n))) return null;
3897
- return { x: parts[0], y: parts[1], z: parts[2] };
3898
- },
3899
- promptNumber: (label) => {
3900
- const raw = window.prompt(label, "1");
3901
- if (raw == null) return null;
3902
- const n = Number(raw);
3903
- return isFinite(n) ? n : null;
3904
- },
3905
- promptText: (label) => {
3906
- const raw = window.prompt(label, "");
3907
- return raw == null ? null : raw;
3908
- },
3909
- notify
3910
- });
3911
- ctxRef.current = ctx;
3912
- function findExistingPointAt(clientX, clientY) {
3913
- const containerRect = div.getBoundingClientRect();
3914
- const localX = clientX - containerRect.left;
3915
- const localY = clientY - containerRect.top;
3916
- const PICK = 18;
3917
- const svg = div.querySelector("svg");
3918
- if (!svg) return void 0;
3919
- for (const [id, obj] of objMapRef.current) {
3920
- const entry = obj;
3921
- if (entry?.elType !== "point3d") continue;
3922
- const sc = entry.element2D?.coords?.scrCoords;
3923
- if (!sc || sc.length < 3) continue;
3924
- const dx = sc[1] - localX;
3925
- const dy = sc[2] - localY;
3926
- if (dx * dx + dy * dy <= PICK * PICK) return id;
3899
+ if (!raw) return null;
3900
+ const parts = raw.split(",").map((s) => Number(s.trim()));
3901
+ if (parts.length !== 3 || parts.some((n) => !isFinite(n))) return null;
3902
+ return { x: parts[0], y: parts[1], z: parts[2] };
3903
+ },
3904
+ promptNumber: (label) => {
3905
+ const raw = window.prompt(label, "1");
3906
+ if (raw == null) return null;
3907
+ const n = Number(raw);
3908
+ return isFinite(n) ? n : null;
3909
+ },
3910
+ promptText: (label) => {
3911
+ const raw = window.prompt(label, "");
3912
+ return raw == null ? null : raw;
3913
+ },
3914
+ notify
3915
+ });
3916
+ ctxRef.current = ctx;
3917
+ function findExistingPointAt(clientX, clientY) {
3918
+ const containerRect = div.getBoundingClientRect();
3919
+ const localX = clientX - containerRect.left;
3920
+ const localY = clientY - containerRect.top;
3921
+ const PICK = 18;
3922
+ const svg = div.querySelector("svg");
3923
+ if (!svg) return void 0;
3924
+ for (const [id, obj] of objMapRef.current) {
3925
+ const entry = obj;
3926
+ if (entry?.elType !== "point3d") continue;
3927
+ const sc = entry.element2D?.coords?.scrCoords;
3928
+ if (!sc || sc.length < 3) continue;
3929
+ const dx = sc[1] - localX;
3930
+ const dy = sc[2] - localY;
3931
+ if (dx * dx + dy * dy <= PICK * PICK) return id;
3932
+ }
3933
+ return void 0;
3927
3934
  }
3928
- return void 0;
3929
- }
3930
- const handlePointerDown = (e) => {
3931
- const tool = toolRef.current;
3932
- if (tool === "move") return;
3933
- const existingPointId = findExistingPointAt(e.clientX, e.clientY);
3934
- let x3 = 0;
3935
- let y3 = 0;
3936
- const z3 = 0;
3937
- try {
3938
- const board2d = boardRef.current;
3939
- if (board2d?.getUsrCoordsOfMouse) {
3940
- const uc = board2d.getUsrCoordsOfMouse(e);
3941
- if (Array.isArray(uc) && uc.length >= 2) {
3942
- x3 = uc[0];
3943
- y3 = uc[1];
3935
+ const handlePointerDown = (e) => {
3936
+ const tool = toolRef.current;
3937
+ if (tool === "move") return;
3938
+ const existingPointId = findExistingPointAt(e.clientX, e.clientY);
3939
+ let x3 = 0;
3940
+ let y3 = 0;
3941
+ const z3 = 0;
3942
+ try {
3943
+ const board2d = boardRef.current;
3944
+ if (board2d?.getUsrCoordsOfMouse) {
3945
+ const uc = board2d.getUsrCoordsOfMouse(e);
3946
+ if (Array.isArray(uc) && uc.length >= 2) {
3947
+ x3 = uc[0];
3948
+ y3 = uc[1];
3949
+ }
3944
3950
  }
3951
+ } catch {
3952
+ }
3953
+ const hit = { x3, y3, z3, existingPointId };
3954
+ handleToolStep(ctx, tool, hit);
3955
+ };
3956
+ const svgEl = div.querySelector("svg");
3957
+ const targetEl = svgEl ?? div;
3958
+ const handlePointerDownEv = (e) => handlePointerDown(e);
3959
+ targetEl.addEventListener("pointerdown", handlePointerDownEv);
3960
+ pointerHandlerRef.current = { el: targetEl, fn: handlePointerDownEv };
3961
+ if (initialState?.elements?.length) {
3962
+ const map = objMapRef.current;
3963
+ for (const el of initialState.elements) {
3964
+ const parents = el.parents.map(
3965
+ (p2) => typeof p2 === "string" && p2.startsWith("@id:") ? map.get(p2.slice(4)) : p2
3966
+ );
3967
+ const obj = view.create(el.type, parents, {
3968
+ ...el.attributes,
3969
+ id: el.id,
3970
+ name: el.label
3971
+ });
3972
+ map.set(el.id, obj);
3973
+ logRef.current.push(el);
3945
3974
  }
3946
- } catch {
3947
- }
3948
- const hit = { x3, y3, z3, existingPointId };
3949
- handleToolStep(ctx, tool, hit);
3950
- };
3951
- const svgEl = div.querySelector("svg");
3952
- const targetEl = svgEl ?? div;
3953
- const handlePointerDownEv = (e) => handlePointerDown(e);
3954
- targetEl.addEventListener("pointerdown", handlePointerDownEv);
3955
- pointerHandlerRef.current = { el: targetEl, fn: handlePointerDownEv };
3956
- if (initialState?.elements?.length) {
3957
- const map = objMapRef.current;
3958
- for (const el of initialState.elements) {
3959
- const parents = el.parents.map(
3960
- (p2) => typeof p2 === "string" && p2.startsWith("@id:") ? map.get(p2.slice(4)) : p2
3961
- );
3962
- const obj = view.create(el.type, parents, {
3963
- ...el.attributes,
3964
- id: el.id,
3965
- name: el.label
3966
- });
3967
- map.set(el.id, obj);
3968
- logRef.current.push(el);
3969
3975
  }
3970
- }
3976
+ })();
3971
3977
  return () => {
3978
+ cancelled = true;
3972
3979
  if (pointerHandlerRef.current) {
3973
3980
  pointerHandlerRef.current.el.removeEventListener(
3974
3981
  "pointerdown",
@@ -3977,7 +3984,7 @@ var MiniBoard3D = react.forwardRef(function MiniBoard3D2({ isDark, initialState
3977
3984
  pointerHandlerRef.current = null;
3978
3985
  }
3979
3986
  try {
3980
- JXG__default.default.JSXGraph.freeBoard(board);
3987
+ if (board && JXG) JXG.JSXGraph.freeBoard(board);
3981
3988
  } catch {
3982
3989
  }
3983
3990
  boardRef.current = null;
@@ -4501,16 +4508,19 @@ function parseSerializedBoard3D(json) {
4501
4508
  }
4502
4509
  return parsed;
4503
4510
  }
4511
+
4512
+ // src/stamps/geometry-3d/render.ts
4504
4513
  var OUTPUT_WIDTH = 1024;
4505
4514
  var OUTPUT_HEIGHT = 768;
4506
4515
  async function renderGeometry3DSvgFromState(jsonState) {
4507
4516
  const state = parseSerializedBoard3D(jsonState);
4517
+ const JXG = (await import('jsxgraph')).default;
4508
4518
  const div = document.createElement("div");
4509
4519
  div.style.cssText = `position:absolute;left:-9999px;top:-9999px;width:${OUTPUT_WIDTH}px;height:${OUTPUT_HEIGHT}px;`;
4510
4520
  document.body.appendChild(div);
4511
4521
  try {
4512
- JXG__default.default.Options.text.display = "internal";
4513
- const board = JXG__default.default.JSXGraph.initBoard(div, {
4522
+ JXG.Options.text.display = "internal";
4523
+ const board = JXG.JSXGraph.initBoard(div, {
4514
4524
  boundingbox: state.bbox,
4515
4525
  axis: false,
4516
4526
  showCopyright: false,
@@ -4558,7 +4568,7 @@ async function renderGeometry3DSvgFromState(jsonState) {
4558
4568
  clone.setAttribute("height", String(OUTPUT_HEIGHT));
4559
4569
  const svgString = new XMLSerializer().serializeToString(clone);
4560
4570
  try {
4561
- JXG__default.default.JSXGraph.freeBoard(board);
4571
+ JXG.JSXGraph.freeBoard(board);
4562
4572
  } catch {
4563
4573
  }
4564
4574
  return { svgString, width: OUTPUT_WIDTH, height: OUTPUT_HEIGHT };