@xrn07/figure-renderer 0.2.0 → 0.2.1
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/parse.d.ts +2 -2
- package/dist/parse.d.ts.map +1 -1
- package/dist/parse.js +26 -4
- package/dist/renderers/forceDiagram.js +5 -3
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/compass.d.ts +3 -2
- package/dist/utils/compass.d.ts.map +1 -1
- package/dist/utils/compass.js +6 -1
- package/package.json +1 -1
package/dist/parse.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { FigureDSL } from './types';
|
|
2
2
|
/**
|
|
3
|
-
* Safely parse FigureDSL from JSON string
|
|
3
|
+
* Safely parse FigureDSL from JSON string or object
|
|
4
4
|
* Returns null if parsing or validation fails
|
|
5
5
|
*/
|
|
6
|
-
export declare function parseFigureDSL(
|
|
6
|
+
export declare function parseFigureDSL(input: string | object): FigureDSL | null;
|
|
7
7
|
/**
|
|
8
8
|
* Validate a FigureDSL object
|
|
9
9
|
*/
|
package/dist/parse.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAS,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAS,MAAM,SAAS,CAAC;AAuEhD;;;GAGG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,GAAG,IAAI,CAwBvE;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,SAAS,CAahE"}
|
package/dist/parse.js
CHANGED
|
@@ -10,7 +10,7 @@ const compassDirectionSchema = z.enum(['up', 'down', 'left', 'right', 'up-left',
|
|
|
10
10
|
const arrowSchema = z.object({
|
|
11
11
|
direction: compassDirectionSchema,
|
|
12
12
|
label: z.string(),
|
|
13
|
-
magnitude: z.string(),
|
|
13
|
+
magnitude: z.union([z.string(), z.number()]), // Accept both string and number
|
|
14
14
|
unit: z.string().optional(),
|
|
15
15
|
color: z.string().optional()
|
|
16
16
|
});
|
|
@@ -46,12 +46,34 @@ const forceDiagramSchema = z.object({
|
|
|
46
46
|
}).optional()
|
|
47
47
|
}).passthrough();
|
|
48
48
|
/**
|
|
49
|
-
*
|
|
49
|
+
* Normalize API response fields to DSL format
|
|
50
|
+
*/
|
|
51
|
+
function normalizeAPIResponse(obj) {
|
|
52
|
+
if (!obj || typeof obj !== 'object') {
|
|
53
|
+
return obj;
|
|
54
|
+
}
|
|
55
|
+
const normalized = { ...obj };
|
|
56
|
+
// Map alt_bn to alt
|
|
57
|
+
if (normalized.alt_bn && !normalized.alt) {
|
|
58
|
+
normalized.alt = normalized.alt_bn;
|
|
59
|
+
}
|
|
60
|
+
return normalized;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Safely parse FigureDSL from JSON string or object
|
|
50
64
|
* Returns null if parsing or validation fails
|
|
51
65
|
*/
|
|
52
|
-
export function parseFigureDSL(
|
|
66
|
+
export function parseFigureDSL(input) {
|
|
53
67
|
try {
|
|
54
|
-
|
|
68
|
+
let parsed;
|
|
69
|
+
if (typeof input === 'string') {
|
|
70
|
+
parsed = JSON.parse(input);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
parsed = input;
|
|
74
|
+
}
|
|
75
|
+
// Normalize API response fields
|
|
76
|
+
parsed = normalizeAPIResponse(parsed);
|
|
55
77
|
// Detect type and use appropriate schema
|
|
56
78
|
if (parsed.type === 'force_diagram') {
|
|
57
79
|
return forceDiagramSchema.parse(parsed);
|
|
@@ -110,8 +110,8 @@ function renderObject(obj, canvasWidth, canvasHeight) {
|
|
|
110
110
|
function renderArrow(arrow, centerX, centerY) {
|
|
111
111
|
const { direction, label, magnitude, color = '#dc2626' } = arrow;
|
|
112
112
|
const elements = [];
|
|
113
|
-
// Parse magnitude to get value
|
|
114
|
-
const { value: magnitudeValue } = parseMagnitude(magnitude);
|
|
113
|
+
// Parse magnitude to get value and unit
|
|
114
|
+
const { value: magnitudeValue, unit: magnitudeUnit } = parseMagnitude(magnitude);
|
|
115
115
|
// Calculate arrow length based on magnitude (with minimum and maximum)
|
|
116
116
|
const arrowLength = Math.max(40, Math.min(100, magnitudeValue * 0.8));
|
|
117
117
|
// Convert compass direction to angle
|
|
@@ -125,8 +125,10 @@ function renderArrow(arrow, centerX, centerY) {
|
|
|
125
125
|
'stroke-width': 3,
|
|
126
126
|
'marker-end': `url(#arrow-${color.replace('#', '')})`,
|
|
127
127
|
}));
|
|
128
|
+
// Format magnitude for display (add unit if not present)
|
|
129
|
+
const magnitudeDisplay = magnitudeUnit ? `${magnitudeValue} ${magnitudeUnit}` : `${magnitudeValue}`;
|
|
128
130
|
// Draw label with magnitude
|
|
129
|
-
const labelText = label ? `${label} (${
|
|
131
|
+
const labelText = label ? `${label} (${magnitudeDisplay})` : magnitudeDisplay;
|
|
130
132
|
// Calculate label position (offset from arrow end)
|
|
131
133
|
const angleRad = (angle * Math.PI) / 180;
|
|
132
134
|
const labelOffset = 20;
|
package/dist/types.d.ts
CHANGED
|
@@ -23,7 +23,7 @@ export interface ForceDiagramDSL extends FigureDSL {
|
|
|
23
23
|
export interface Arrow {
|
|
24
24
|
direction: 'up' | 'down' | 'left' | 'right' | 'up-left' | 'up-right' | 'down-left' | 'down-right';
|
|
25
25
|
label: string;
|
|
26
|
-
magnitude: string;
|
|
26
|
+
magnitude: string | number;
|
|
27
27
|
unit?: string;
|
|
28
28
|
color?: string;
|
|
29
29
|
}
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,eAAgB,SAAQ,SAAS;IAChD,IAAI,EAAE,eAAe,CAAC;IACtB,MAAM,EAAE,KAAK,EAAE,CAAC;IAChB,MAAM,CAAC,EAAE,cAAc,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,KAAK;IACpB,SAAS,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,GAAG,UAAU,GAAG,WAAW,GAAG,YAAY,CAAC;IAClG,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,eAAgB,SAAQ,SAAS;IAChD,IAAI,EAAE,eAAe,CAAC;IACtB,MAAM,EAAE,KAAK,EAAE,CAAC;IAChB,MAAM,CAAC,EAAE,cAAc,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,KAAK;IACpB,SAAS,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,GAAG,UAAU,GAAG,WAAW,GAAG,YAAY,CAAC;IAClG,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,GAAG,MAAM,CAAC;IAC3B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,KAAK,GAAG,QAAQ,GAAG,OAAO,CAAC;IACjC,CAAC,CAAC,EAAE,MAAM,CAAC;IACX,CAAC,CAAC,EAAE,MAAM,CAAC;IACX,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB"}
|
package/dist/utils/compass.d.ts
CHANGED
|
@@ -11,12 +11,13 @@ export declare function compassToAngle(direction: CompassDirection): number;
|
|
|
11
11
|
*/
|
|
12
12
|
export declare function angleToCompass(angle: number): CompassDirection;
|
|
13
13
|
/**
|
|
14
|
-
* Parse magnitude string to extract numeric value and unit
|
|
14
|
+
* Parse magnitude string or number to extract numeric value and unit
|
|
15
15
|
* Examples: "100 N" -> { value: 100, unit: "N" }
|
|
16
16
|
* "50 kg" -> { value: 50, unit: "kg" }
|
|
17
17
|
* "25" -> { value: 25, unit: "" }
|
|
18
|
+
* 25 -> { value: 25, unit: "" }
|
|
18
19
|
*/
|
|
19
|
-
export declare function parseMagnitude(magnitude: string): {
|
|
20
|
+
export declare function parseMagnitude(magnitude: string | number): {
|
|
20
21
|
value: number;
|
|
21
22
|
unit: string;
|
|
22
23
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compass.d.ts","sourceRoot":"","sources":["../../src/utils/compass.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,MAAM,gBAAgB,GACxB,IAAI,GACJ,MAAM,GACN,MAAM,GACN,OAAO,GACP,SAAS,GACT,UAAU,GACV,WAAW,GACX,YAAY,CAAC;AAEjB;;GAEG;AACH,wBAAgB,cAAc,CAAC,SAAS,EAAE,gBAAgB,GAAG,MAAM,CAalE;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,gBAAgB,CAa9D;AAED
|
|
1
|
+
{"version":3,"file":"compass.d.ts","sourceRoot":"","sources":["../../src/utils/compass.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,MAAM,gBAAgB,GACxB,IAAI,GACJ,MAAM,GACN,MAAM,GACN,OAAO,GACP,SAAS,GACT,UAAU,GACV,WAAW,GACX,YAAY,CAAC;AAEjB;;GAEG;AACH,wBAAgB,cAAc,CAAC,SAAS,EAAE,gBAAgB,GAAG,MAAM,CAalE;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,gBAAgB,CAa9D;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAyB1F;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,gBAAgB,EAC3B,MAAM,EAAE,MAAM,GACb;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,CAQ1B;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,gBAAgB,EAC3B,MAAM,EAAE,MAAM,GACb;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,CAQ1B"}
|
package/dist/utils/compass.js
CHANGED
|
@@ -39,12 +39,17 @@ export function angleToCompass(angle) {
|
|
|
39
39
|
return 'right'; // Default
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
|
-
* Parse magnitude string to extract numeric value and unit
|
|
42
|
+
* Parse magnitude string or number to extract numeric value and unit
|
|
43
43
|
* Examples: "100 N" -> { value: 100, unit: "N" }
|
|
44
44
|
* "50 kg" -> { value: 50, unit: "kg" }
|
|
45
45
|
* "25" -> { value: 25, unit: "" }
|
|
46
|
+
* 25 -> { value: 25, unit: "" }
|
|
46
47
|
*/
|
|
47
48
|
export function parseMagnitude(magnitude) {
|
|
49
|
+
// If it's already a number, return it
|
|
50
|
+
if (typeof magnitude === 'number') {
|
|
51
|
+
return { value: magnitude, unit: '' };
|
|
52
|
+
}
|
|
48
53
|
const trimmed = magnitude.trim();
|
|
49
54
|
// Try to extract numeric value and unit
|
|
50
55
|
const match = trimmed.match(/^(-?\d+\.?\d*)\s*(.*)$/);
|
package/package.json
CHANGED