jspsych-tangram 0.0.1 → 0.0.3
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/construct/index.browser.js +91 -36
- package/dist/construct/index.browser.js.map +1 -1
- package/dist/construct/index.browser.min.js +15 -11
- package/dist/construct/index.browser.min.js.map +1 -1
- package/dist/construct/index.cjs +90 -35
- package/dist/construct/index.cjs.map +1 -1
- package/dist/construct/index.js +90 -35
- package/dist/construct/index.js.map +1 -1
- package/dist/index.cjs +90 -35
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +90 -35
- package/dist/index.js.map +1 -1
- package/dist/prep/index.browser.js +9 -9
- package/dist/prep/index.browser.js.map +1 -1
- package/dist/prep/index.browser.min.js +2 -2
- package/dist/prep/index.browser.min.js.map +1 -1
- package/dist/prep/index.cjs +8 -8
- package/dist/prep/index.cjs.map +1 -1
- package/dist/prep/index.js +8 -8
- package/dist/prep/index.js.map +1 -1
- package/package.json +1 -1
- package/src/core/domain/primitives.ts +8 -8
- package/src/core/domain/types.ts +3 -4
- package/src/core/io/quickstash.ts +3 -3
- package/src/core/io/stims.ts +6 -9
- package/src/core/types/plugin-interfaces.ts +6 -5
- package/src/index.spec.ts +0 -19
- package/src/plugins/tangram-construct/ConstructionApp.tsx +104 -30
- package/tangram-construct.min.js +15 -11
- package/tangram-prep.min.js +2 -2
- package/src/core/io/json-to-tangram-spec.ts +0 -110
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
// Convert raw stims JSON to clean TangramSpec[] for plugin interface
|
|
2
|
-
import type { TangramSpec } from "@/core/types/plugin-interfaces";
|
|
3
|
-
|
|
4
|
-
/** Raw JSON shapes from dev/assets/stims_dev.json */
|
|
5
|
-
type RawTan = {
|
|
6
|
-
name: string;
|
|
7
|
-
verticesAtOrigin: Array<[number, number] | { x: number; y: number }>;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
type RawStim = {
|
|
11
|
-
solutionTans: RawTan[];
|
|
12
|
-
stimImgPath?: string;
|
|
13
|
-
stimSilhouetteImgPath?: string;
|
|
14
|
-
set?: string;
|
|
15
|
-
id?: string | number;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
/** Canonical tangram names we accept as actual piece polys. */
|
|
19
|
-
const CANON = new Set([
|
|
20
|
-
"square",
|
|
21
|
-
"small_triangle",
|
|
22
|
-
"parallelogram",
|
|
23
|
-
"med_triangle",
|
|
24
|
-
"large_triangle",
|
|
25
|
-
]);
|
|
26
|
-
|
|
27
|
-
// ----------------------- guards & converters -----------------------
|
|
28
|
-
function isPoint(a: unknown): a is [number, number] {
|
|
29
|
-
return Array.isArray(a) && a.length >= 2
|
|
30
|
-
&& typeof a[0] === "number" && typeof a[1] === "number";
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function isPointObj(a: unknown): a is { x: number; y: number } {
|
|
34
|
-
return !!a && typeof a === "object"
|
|
35
|
-
&& "x" in (a as any) && "y" in (a as any)
|
|
36
|
-
&& typeof (a as any).x === "number" && typeof (a as any).y === "number";
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function toNumberPair(p: [number, number] | { x: number; y: number }): [number, number] {
|
|
40
|
-
// Keep in math coords (+y up) - conversion to SVG coords happens in plugin wrapper
|
|
41
|
-
if (isPoint(p)) return [p[0], p[1]];
|
|
42
|
-
const obj = p as any;
|
|
43
|
-
return [obj.x, obj.y];
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function polygonToNumbers(vertices: Array<[number, number] | { x: number; y: number }>): number[][] {
|
|
47
|
-
const out: number[][] = [];
|
|
48
|
-
for (const v of vertices) {
|
|
49
|
-
if (isPoint(v) || isPointObj(v)) {
|
|
50
|
-
out.push(toNumberPair(v));
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return out;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// ----------------------- main converter -----------------------
|
|
57
|
-
/**
|
|
58
|
-
* Convert raw stims JSON to TangramSpec[] for clean plugin interface.
|
|
59
|
-
* This replaces the old normalizeStims() function with proper separation of concerns.
|
|
60
|
-
*/
|
|
61
|
-
export function rawStimsToTangramSpecs(
|
|
62
|
-
src: unknown,
|
|
63
|
-
sectorIds: string[],
|
|
64
|
-
defaultRequired = 2
|
|
65
|
-
): TangramSpec[] {
|
|
66
|
-
const rawList: RawStim[] = Array.isArray(src)
|
|
67
|
-
? (src as RawStim[])
|
|
68
|
-
: (src && typeof src === "object" && Array.isArray((src as any).stims))
|
|
69
|
-
? ((src as any).stims as RawStim[])
|
|
70
|
-
: [];
|
|
71
|
-
|
|
72
|
-
const tangrams: TangramSpec[] = [];
|
|
73
|
-
|
|
74
|
-
for (let i = 0; i < Math.min(sectorIds.length, rawList.length); i++) {
|
|
75
|
-
const raw = rawList[i];
|
|
76
|
-
if (!raw) continue;
|
|
77
|
-
|
|
78
|
-
// Extract only canonical piece shapes; ignore macros
|
|
79
|
-
const mask: number[][][] = [];
|
|
80
|
-
for (const tan of raw.solutionTans ?? []) {
|
|
81
|
-
if (!tan || !CANON.has(tan.name)) continue;
|
|
82
|
-
if (Array.isArray(tan.verticesAtOrigin) && tan.verticesAtOrigin.length >= 3) {
|
|
83
|
-
mask.push(polygonToNumbers(tan.verticesAtOrigin));
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const id = sectorIds[i] ?? String(i);
|
|
88
|
-
tangrams.push({
|
|
89
|
-
id,
|
|
90
|
-
silhouette: {
|
|
91
|
-
mask,
|
|
92
|
-
requiredCount: defaultRequired,
|
|
93
|
-
},
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return tangrams;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/** Fetch + convert helper for dev path (e.g., "/dev/assets/stims_dev.json"). */
|
|
101
|
-
export async function loadTangramSpecsFromUrl(
|
|
102
|
-
url: string,
|
|
103
|
-
sectorIds: string[],
|
|
104
|
-
defaultRequired = 2
|
|
105
|
-
): Promise<TangramSpec[]> {
|
|
106
|
-
const res = await fetch(url);
|
|
107
|
-
if (!res.ok) throw new Error(`Failed to load stims: ${res.status} ${res.statusText}`);
|
|
108
|
-
const json = await res.json();
|
|
109
|
-
return rawStimsToTangramSpecs(json, sectorIds, defaultRequired);
|
|
110
|
-
}
|