@pie-players/pie-tool-protractor 0.1.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.
@@ -0,0 +1,343 @@
1
+ <svelte:options
2
+ customElement={{
3
+ tag: 'pie-tool-protractor',
4
+ shadow: 'none',
5
+ props: {
6
+ visible: { type: 'Boolean', attribute: 'visible' },
7
+ toolId: { type: 'String', attribute: 'tool-id' },
8
+ coordinator: { type: 'Object' }
9
+ }
10
+ }}
11
+ />
12
+
13
+ <script lang="ts">
14
+ import type { IToolCoordinator } from '@pie-players/pie-assessment-toolkit';
15
+ import { ZIndexLayer } from '@pie-players/pie-assessment-toolkit';
16
+ import Moveable from 'moveable';
17
+ import { onMount } from 'svelte';
18
+ import protractorSvg from './protractor.svg';
19
+
20
+ // Props
21
+ let { visible = false, toolId = 'protractor', coordinator }: { visible?: boolean; toolId?: string; coordinator?: IToolCoordinator } = $props();
22
+
23
+ // Check if running in browser
24
+ const isBrowser = typeof window !== 'undefined';
25
+
26
+ // State
27
+ let containerEl = $state<HTMLDivElement | undefined>();
28
+ let announceText = $state('');
29
+ let moveable: Moveable | null = null;
30
+
31
+ // Track registration state
32
+ let registered = $state(false);
33
+
34
+ // Keyboard navigation constants
35
+ const MOVE_STEP = 10; // pixels
36
+ const ROTATE_STEP = 5; // degrees
37
+ const FINE_ROTATE_STEP = 1; // degrees
38
+
39
+ function announce(message: string) {
40
+ announceText = message;
41
+ setTimeout(() => announceText = '', 1000);
42
+ }
43
+
44
+ // Initialize Moveable.js (matching production configuration)
45
+ function initMoveable() {
46
+ if (!containerEl || !isBrowser) {
47
+ return;
48
+ }
49
+
50
+ // Clean up any existing instance first
51
+ if (moveable) {
52
+ moveable.destroy();
53
+ moveable = null;
54
+ }
55
+
56
+ coordinator?.bringToFront(containerEl);
57
+
58
+ moveable = new Moveable(document.body, {
59
+ target: containerEl,
60
+ draggable: true,
61
+ rotatable: true,
62
+ snappable: true,
63
+ originDraggable: false,
64
+ originRelative: true,
65
+ origin: [0.5, 1], // Bottom center (matching production implementation)
66
+ hideDefaultLines: true,
67
+ keepRatio: false,
68
+ bounds: {
69
+ left: -110,
70
+ top: -110,
71
+ right: -110,
72
+ bottom: -110,
73
+ position: 'css'
74
+ }
75
+ } as any); // Type assertion needed for Moveable.js config
76
+
77
+ // Associate the moveable instance with the tool ID
78
+ const controlBox = moveable.getControlBoxElement();
79
+ controlBox?.setAttribute('data-moveablejs-tool-control-box', toolId);
80
+
81
+ moveable.on('drag', ({ target, transform }) => {
82
+ if (target) {
83
+ target.style.transform = transform;
84
+ }
85
+ });
86
+
87
+ moveable.on('rotate', ({ target, transform }) => {
88
+ if (target) {
89
+ target.style.transform = transform;
90
+ }
91
+ });
92
+ }
93
+
94
+ function destroyMoveable() {
95
+ if (moveable) {
96
+ moveable.destroy();
97
+ moveable = null;
98
+ }
99
+ }
100
+
101
+ function updateBounds() {
102
+ if (moveable) {
103
+ moveable.bounds = {
104
+ left: -110,
105
+ top: -110,
106
+ right: -110,
107
+ bottom: -110,
108
+ position: 'css'
109
+ };
110
+ moveable.updateRect();
111
+ }
112
+ }
113
+
114
+ // Keyboard navigation (preserved for accessibility)
115
+ function handleKeyDown(e: KeyboardEvent) {
116
+ if (!moveable || !containerEl) return;
117
+
118
+ let handled = false;
119
+ const isShift = e.shiftKey;
120
+
121
+ // Get current transform from element
122
+ const transform = containerEl.style.transform || '';
123
+ const matrix = new DOMMatrix(transform || 'none');
124
+
125
+ // Extract position and rotation
126
+ let x = matrix.e || (isBrowser ? window.innerWidth / 2 : 400);
127
+ let y = matrix.f || (isBrowser ? window.innerHeight / 2 : 300);
128
+ let rotation = Math.round(Math.atan2(matrix.b, matrix.a) * (180 / Math.PI));
129
+
130
+ switch (e.key) {
131
+ case 'ArrowUp':
132
+ if (isShift) {
133
+ rotation = (rotation - ROTATE_STEP + 360) % 360;
134
+ announce(`Rotated to ${rotation} degrees`);
135
+ } else {
136
+ y -= MOVE_STEP;
137
+ announce(`Moved up to ${Math.round(y)}`);
138
+ }
139
+ handled = true;
140
+ break;
141
+ case 'ArrowDown':
142
+ if (isShift) {
143
+ rotation = (rotation + ROTATE_STEP) % 360;
144
+ announce(`Rotated to ${rotation} degrees`);
145
+ } else {
146
+ y += MOVE_STEP;
147
+ announce(`Moved down to ${Math.round(y)}`);
148
+ }
149
+ handled = true;
150
+ break;
151
+ case 'ArrowLeft':
152
+ if (isShift) {
153
+ rotation = (rotation - ROTATE_STEP + 360) % 360;
154
+ announce(`Rotated to ${rotation} degrees`);
155
+ } else {
156
+ x -= MOVE_STEP;
157
+ announce(`Moved left to ${Math.round(x)}`);
158
+ }
159
+ handled = true;
160
+ break;
161
+ case 'ArrowRight':
162
+ if (isShift) {
163
+ rotation = (rotation + ROTATE_STEP) % 360;
164
+ announce(`Rotated to ${rotation} degrees`);
165
+ } else {
166
+ x += MOVE_STEP;
167
+ announce(`Moved right to ${Math.round(x)}`);
168
+ }
169
+ handled = true;
170
+ break;
171
+ case 'PageUp':
172
+ rotation = (rotation - FINE_ROTATE_STEP + 360) % 360;
173
+ announce(`Rotated to ${rotation} degrees`);
174
+ handled = true;
175
+ break;
176
+ case 'PageDown':
177
+ rotation = (rotation + FINE_ROTATE_STEP) % 360;
178
+ announce(`Rotated to ${rotation} degrees`);
179
+ handled = true;
180
+ break;
181
+ }
182
+
183
+ if (handled && moveable) {
184
+ e.preventDefault();
185
+ // Apply new transform via Moveable
186
+ const newTransform = `translate(-50%, -50%) translate(${x}px, ${y}px) rotate(${rotation}deg)`;
187
+ containerEl.style.transform = newTransform;
188
+ moveable.updateRect();
189
+ }
190
+ }
191
+
192
+ // Initialize Moveable when visible changes
193
+ $effect(() => {
194
+ if (visible && containerEl && isBrowser) {
195
+ // Wait for the next tick to ensure DOM is updated
196
+ setTimeout(initMoveable, 0);
197
+ } else {
198
+ destroyMoveable();
199
+ }
200
+ });
201
+
202
+ // Register with coordinator when it becomes available
203
+ $effect(() => {
204
+ if (coordinator && toolId && !registered) {
205
+ coordinator.registerTool(toolId, 'Protractor', undefined, ZIndexLayer.TOOL);
206
+ registered = true;
207
+ }
208
+ });
209
+
210
+ onMount(() => {
211
+ window.addEventListener('resize', updateBounds);
212
+ return () => {
213
+ destroyMoveable();
214
+ window.removeEventListener('resize', updateBounds);
215
+ if (coordinator && toolId) {
216
+ coordinator.unregisterTool(toolId);
217
+ }
218
+ };
219
+ });
220
+
221
+ // Update element reference when container becomes available
222
+ $effect(() => {
223
+ if (coordinator && containerEl && toolId) {
224
+ coordinator.updateToolElement(toolId, containerEl);
225
+ }
226
+ });
227
+
228
+ // Auto-focus when tool becomes visible
229
+ $effect(() => {
230
+ if (visible && containerEl) {
231
+ setTimeout(() => containerEl?.focus(), 100);
232
+ }
233
+ });
234
+ </script>
235
+
236
+ {#if visible && isBrowser}
237
+ <!-- Screen reader announcements -->
238
+ <div class="sr-only" role="status" aria-live="polite" aria-atomic="true">
239
+ {announceText}
240
+ </div>
241
+
242
+ <!-- svelte-ignore a11y_no_noninteractive_tabindex -->
243
+ <!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
244
+ <div
245
+ bind:this={containerEl}
246
+ class="protractor-frame"
247
+ data-moveablejs-tool-id={toolId}
248
+ onpointerdown={() => coordinator?.bringToFront(containerEl)}
249
+ onkeydown={handleKeyDown}
250
+ role="application"
251
+ tabindex="0"
252
+ aria-label="Protractor tool. Use arrow keys to move, Shift+arrows to rotate, PageUp/PageDown for fine rotation. Current rotation displayed via Moveable.js"
253
+ aria-roledescription="Draggable and rotatable protractor measurement tool"
254
+ >
255
+ <div class="protractor-container">
256
+ <img
257
+ class="protractor"
258
+ src={protractorSvg}
259
+ alt="Protractor with 180-degree semicircular scale marked from 0 to 180 degrees in both directions, with degree markings every 10 degrees"
260
+ draggable="false"
261
+ />
262
+ </div>
263
+ </div>
264
+ {/if}
265
+
266
+ <style>
267
+ .sr-only {
268
+ position: absolute;
269
+ width: 1px;
270
+ height: 1px;
271
+ padding: 0;
272
+ margin: -1px;
273
+ overflow: hidden;
274
+ clip: rect(0, 0, 0, 0);
275
+ white-space: nowrap;
276
+ border-width: 0;
277
+ }
278
+
279
+ .protractor-frame {
280
+ border: 0;
281
+ cursor: move;
282
+ left: 50%;
283
+ overflow: hidden;
284
+ position: absolute;
285
+ top: 50%;
286
+ transform: translate(-50%, -50%);
287
+ transform-origin: 50% calc(100% - 10px); /* Rotation origin at bottom center (matching production implementation) */
288
+ user-select: none;
289
+ touch-action: none;
290
+ }
291
+
292
+ .protractor-frame:focus {
293
+ outline: 3px solid #4A90E2;
294
+ outline-offset: 2px;
295
+ }
296
+
297
+ .protractor-frame:focus-visible {
298
+ outline: 3px solid #4A90E2;
299
+ outline-offset: 2px;
300
+ }
301
+
302
+ .protractor-container {
303
+ border: 0;
304
+ position: relative;
305
+ width: 400px;
306
+ height: 210px;
307
+ }
308
+
309
+ /* Semi-transparent white overlay for visibility (matching production implementation) */
310
+ .protractor-container::after {
311
+ background-color: #fff;
312
+ border-radius: 283px 283px 0 0;
313
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); /* Matching production implementation shadow */
314
+ content: '';
315
+ display: block;
316
+ height: 283px;
317
+ opacity: 0.5;
318
+ position: absolute;
319
+ top: 0;
320
+ width: 400px;
321
+ z-index: 1;
322
+ pointer-events: none;
323
+ }
324
+
325
+ .protractor {
326
+ width: 400px;
327
+ height: 210px;
328
+ position: relative;
329
+ z-index: 2;
330
+ display: block;
331
+ }
332
+
333
+ /* Moveable.js control styling (matching production implementation) */
334
+ :global(body .moveable-control-box[data-moveablejs-tool-control-box="protractor"]) {
335
+ --moveable-color: red;
336
+ z-index: 2003; /* ZIndexLayer.CONTROL */
337
+ }
338
+
339
+ :global([data-moveablejs-tool-id="protractor"]) {
340
+ z-index: 2002; /* ZIndexLayer.MODAL */
341
+ }
342
+ </style>
343
+