@pie-lib/mask-markup 3.1.4 → 3.1.6-beta.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/CHANGELOG.md +0 -101
- package/lib/choices/choice.js +6 -24
- package/lib/choices/choice.js.map +1 -1
- package/lib/choices/index.js +9 -32
- package/lib/choices/index.js.map +1 -1
- package/lib/components/blank.js +29 -106
- package/lib/components/blank.js.map +1 -1
- package/lib/components/dropdown.js +9 -6
- package/lib/components/dropdown.js.map +1 -1
- package/lib/drag-in-the-blank.js +30 -139
- package/lib/drag-in-the-blank.js.map +1 -1
- package/lib/keyboard-coordinates.js +1 -1
- package/lib/mask.js +15 -20
- package/lib/mask.js.map +1 -1
- package/package.json +6 -6
- package/src/choices/choice.jsx +3 -16
- package/src/choices/index.jsx +3 -27
- package/src/components/blank.jsx +35 -108
- package/src/components/dropdown.jsx +9 -7
- package/src/drag-in-the-blank.jsx +22 -132
- package/src/mask.jsx +11 -13
- package/LICENSE.md +0 -5
- package/lib/keyboard-coordinates.js.map +0 -1
- package/src/__tests__/drag-in-the-blank.test.js +0 -256
- package/src/__tests__/index.test.js +0 -38
- package/src/__tests__/keyboard-coordinates.test.js +0 -202
- package/src/__tests__/mask.test.js +0 -479
- package/src/__tests__/serialization.test.js +0 -54
- package/src/__tests__/utils.js +0 -1
- package/src/__tests__/with-mask.test.js +0 -76
- package/src/choices/__tests__/index.test.js +0 -212
- package/src/components/__tests__/blank.test.js +0 -413
- package/src/components/__tests__/correct-input.test.js +0 -90
- package/src/components/__tests__/dropdown.test.js +0 -129
- package/src/components/__tests__/input.test.js +0 -102
- package/src/keyboard-coordinates.js +0 -147
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
|
-
import { render, screen } from '@testing-library/react';
|
|
3
|
-
import userEvent from '@testing-library/user-event';
|
|
4
|
-
import CorrectInput from '../correct-input';
|
|
5
|
-
|
|
6
|
-
describe('CorrectInput', () => {
|
|
7
|
-
const onChange = jest.fn();
|
|
8
|
-
const defaultProps = {
|
|
9
|
-
disabled: false,
|
|
10
|
-
correct: false,
|
|
11
|
-
variant: 'outlined',
|
|
12
|
-
value: 'Cow',
|
|
13
|
-
onChange,
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
beforeEach(() => {
|
|
17
|
-
onChange.mockClear();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe('rendering', () => {
|
|
21
|
-
it('renders input with default props', () => {
|
|
22
|
-
render(<CorrectInput {...defaultProps} />);
|
|
23
|
-
const input = screen.getByRole('textbox');
|
|
24
|
-
expect(input).toBeInTheDocument();
|
|
25
|
-
expect(input).toHaveValue('Cow');
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('renders as disabled when disabled prop is true', () => {
|
|
29
|
-
render(<CorrectInput {...defaultProps} disabled={true} />);
|
|
30
|
-
const input = screen.getByRole('textbox');
|
|
31
|
-
expect(input).toBeDisabled();
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it('renders with correct state as false', () => {
|
|
35
|
-
const { container } = render(<CorrectInput {...defaultProps} correct={false} />);
|
|
36
|
-
const input = screen.getByRole('textbox');
|
|
37
|
-
expect(input).toBeInTheDocument();
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('renders with correct state as true', () => {
|
|
41
|
-
const { container } = render(<CorrectInput {...defaultProps} correct={true} />);
|
|
42
|
-
const input = screen.getByRole('textbox');
|
|
43
|
-
expect(input).toBeInTheDocument();
|
|
44
|
-
// Should show visual indication of correctness
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('renders with outlined variant', () => {
|
|
48
|
-
render(<CorrectInput {...defaultProps} variant="outlined" />);
|
|
49
|
-
const input = screen.getByRole('textbox');
|
|
50
|
-
expect(input).toBeInTheDocument();
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
describe('user interactions', () => {
|
|
55
|
-
it('calls onChange when user types', async () => {
|
|
56
|
-
const user = userEvent.setup();
|
|
57
|
-
render(<CorrectInput {...defaultProps} />);
|
|
58
|
-
|
|
59
|
-
const input = screen.getByRole('textbox');
|
|
60
|
-
await user.clear(input);
|
|
61
|
-
await user.type(input, '1');
|
|
62
|
-
|
|
63
|
-
expect(onChange).toHaveBeenCalled();
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it('calls onChange with event object', async () => {
|
|
67
|
-
const user = userEvent.setup();
|
|
68
|
-
render(<CorrectInput {...defaultProps} value="" />);
|
|
69
|
-
|
|
70
|
-
const input = screen.getByRole('textbox');
|
|
71
|
-
await user.type(input, 'test');
|
|
72
|
-
|
|
73
|
-
expect(onChange).toHaveBeenCalled();
|
|
74
|
-
// Check that onChange receives an event-like object
|
|
75
|
-
const lastCall = onChange.mock.calls[onChange.mock.calls.length - 1][0];
|
|
76
|
-
expect(lastCall).toHaveProperty('target');
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it('updates value when user changes input', async () => {
|
|
80
|
-
const user = userEvent.setup();
|
|
81
|
-
render(<CorrectInput {...defaultProps} />);
|
|
82
|
-
|
|
83
|
-
const input = screen.getByRole('textbox');
|
|
84
|
-
await user.clear(input);
|
|
85
|
-
await user.type(input, 'Dog');
|
|
86
|
-
|
|
87
|
-
expect(onChange).toHaveBeenCalled();
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
});
|
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
|
-
import { render, screen, waitFor } from '@testing-library/react';
|
|
3
|
-
import userEvent from '@testing-library/user-event';
|
|
4
|
-
import { choice } from '../../__tests__/utils';
|
|
5
|
-
import Dropdown from '../dropdown';
|
|
6
|
-
|
|
7
|
-
describe('Dropdown', () => {
|
|
8
|
-
const onChange = jest.fn();
|
|
9
|
-
const defaultProps = {
|
|
10
|
-
onChange,
|
|
11
|
-
id: '1',
|
|
12
|
-
correct: false,
|
|
13
|
-
disabled: false,
|
|
14
|
-
value: 'Jumped',
|
|
15
|
-
choices: [choice('Jumped'), choice('Laughed'), choice('Smiled')],
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
beforeEach(() => {
|
|
19
|
-
onChange.mockClear();
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
describe('rendering', () => {
|
|
23
|
-
it('renders dropdown with default props', () => {
|
|
24
|
-
render(<Dropdown {...defaultProps} />);
|
|
25
|
-
const button = screen.getByRole('combobox');
|
|
26
|
-
expect(button).toBeInTheDocument();
|
|
27
|
-
// Button displays the selected value
|
|
28
|
-
expect(button).toHaveTextContent('Jumped');
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it('renders with all choices as options when opened', async () => {
|
|
32
|
-
const user = userEvent.setup();
|
|
33
|
-
render(<Dropdown {...defaultProps} />);
|
|
34
|
-
|
|
35
|
-
const button = screen.getByRole('combobox');
|
|
36
|
-
await user.click(button);
|
|
37
|
-
|
|
38
|
-
// Options should now be visible - find them by role
|
|
39
|
-
const options = screen.getAllByRole('option');
|
|
40
|
-
expect(options).toHaveLength(3);
|
|
41
|
-
// Verify the text content of options using specific elements
|
|
42
|
-
expect(options[0]).toHaveTextContent('Jumped');
|
|
43
|
-
expect(options[1]).toHaveTextContent('Laughed');
|
|
44
|
-
expect(options[2]).toHaveTextContent('Smiled');
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('focuses the selected option when the menu opens', async () => {
|
|
48
|
-
const user = userEvent.setup();
|
|
49
|
-
render(<Dropdown {...defaultProps} />);
|
|
50
|
-
|
|
51
|
-
const button = screen.getByRole('combobox');
|
|
52
|
-
await user.click(button);
|
|
53
|
-
|
|
54
|
-
await waitFor(() => {
|
|
55
|
-
expect(document.getElementById('dropdown-option-1-0')).toHaveFocus();
|
|
56
|
-
});
|
|
57
|
-
expect(button).toHaveAttribute('aria-activedescendant', 'dropdown-option-1-0');
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it('focuses a non-first selected option when the menu opens', async () => {
|
|
61
|
-
const user = userEvent.setup();
|
|
62
|
-
render(<Dropdown {...defaultProps} value="Laughed" />);
|
|
63
|
-
|
|
64
|
-
const button = screen.getByRole('combobox');
|
|
65
|
-
await user.click(button);
|
|
66
|
-
|
|
67
|
-
await waitFor(() => {
|
|
68
|
-
expect(document.getElementById('dropdown-option-1-1')).toHaveFocus();
|
|
69
|
-
});
|
|
70
|
-
expect(button).toHaveAttribute('aria-activedescendant', 'dropdown-option-1-1');
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('does not highlight an option when no value is selected', async () => {
|
|
74
|
-
const user = userEvent.setup();
|
|
75
|
-
render(<Dropdown {...defaultProps} value={undefined} />);
|
|
76
|
-
|
|
77
|
-
const button = screen.getByRole('combobox');
|
|
78
|
-
await user.click(button);
|
|
79
|
-
|
|
80
|
-
expect(button).not.toHaveAttribute('aria-activedescendant');
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
it('renders as disabled when disabled prop is true', () => {
|
|
84
|
-
render(<Dropdown {...defaultProps} disabled={true} />);
|
|
85
|
-
const button = screen.getByRole('combobox');
|
|
86
|
-
expect(button).toBeDisabled();
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
it('shows correct state when correct is true', () => {
|
|
90
|
-
const { container } = render(<Dropdown {...defaultProps} correct={true} />);
|
|
91
|
-
const button = screen.getByRole('combobox');
|
|
92
|
-
expect(button).toBeInTheDocument();
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
describe('user interactions', () => {
|
|
97
|
-
it('calls onChange when user selects a different option', async () => {
|
|
98
|
-
const user = userEvent.setup();
|
|
99
|
-
render(<Dropdown {...defaultProps} />);
|
|
100
|
-
|
|
101
|
-
// Click button to open menu
|
|
102
|
-
const button = screen.getByRole('combobox');
|
|
103
|
-
await user.click(button);
|
|
104
|
-
|
|
105
|
-
// Find the option by getting all options and selecting the one with "Laughed" text
|
|
106
|
-
const options = screen.getAllByRole('option');
|
|
107
|
-
const laughedOption = options.find((opt) => opt.textContent.includes('Laughed'));
|
|
108
|
-
await user.click(laughedOption);
|
|
109
|
-
|
|
110
|
-
expect(onChange).toHaveBeenCalledWith('1', 'Laughed');
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it('calls onChange with correct value', async () => {
|
|
114
|
-
const user = userEvent.setup();
|
|
115
|
-
render(<Dropdown {...defaultProps} />);
|
|
116
|
-
|
|
117
|
-
// Click button to open menu
|
|
118
|
-
const button = screen.getByRole('combobox');
|
|
119
|
-
await user.click(button);
|
|
120
|
-
|
|
121
|
-
// Find the option by getting all options and selecting the one with "Smiled" text
|
|
122
|
-
const options = screen.getAllByRole('option');
|
|
123
|
-
const smiledOption = options.find((opt) => opt.textContent.includes('Smiled'));
|
|
124
|
-
await user.click(smiledOption);
|
|
125
|
-
|
|
126
|
-
expect(onChange).toHaveBeenCalledWith('1', 'Smiled');
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
});
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
|
-
import { render, screen } from '@testing-library/react';
|
|
3
|
-
import userEvent from '@testing-library/user-event';
|
|
4
|
-
import Input from '../input';
|
|
5
|
-
|
|
6
|
-
// Mock CorrectInput to simplify testing
|
|
7
|
-
jest.mock('../correct-input', () => {
|
|
8
|
-
return function CorrectInput({ value, onChange, disabled, correct, variant }) {
|
|
9
|
-
return (
|
|
10
|
-
<input
|
|
11
|
-
data-testid="correct-input"
|
|
12
|
-
value={value || ''}
|
|
13
|
-
onChange={onChange}
|
|
14
|
-
disabled={disabled}
|
|
15
|
-
data-correct={correct}
|
|
16
|
-
data-variant={variant}
|
|
17
|
-
/>
|
|
18
|
-
);
|
|
19
|
-
};
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
describe('Input', () => {
|
|
23
|
-
const onChange = jest.fn();
|
|
24
|
-
const defaultProps = {
|
|
25
|
-
disabled: false,
|
|
26
|
-
correct: false,
|
|
27
|
-
value: 'Cow',
|
|
28
|
-
id: '1',
|
|
29
|
-
onChange,
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
beforeEach(() => {
|
|
33
|
-
onChange.mockClear();
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
describe('rendering', () => {
|
|
37
|
-
it('renders with default props', () => {
|
|
38
|
-
render(<Input {...defaultProps} />);
|
|
39
|
-
const input = screen.getByTestId('correct-input');
|
|
40
|
-
|
|
41
|
-
expect(input).toBeInTheDocument();
|
|
42
|
-
expect(input).toHaveValue('Cow');
|
|
43
|
-
expect(input).not.toBeDisabled();
|
|
44
|
-
expect(input).toHaveAttribute('data-correct', 'false');
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('renders as disabled when disabled prop is true', () => {
|
|
48
|
-
render(<Input {...defaultProps} disabled={true} />);
|
|
49
|
-
const input = screen.getByTestId('correct-input');
|
|
50
|
-
|
|
51
|
-
expect(input).toBeDisabled();
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it('renders with correct state', () => {
|
|
55
|
-
render(<Input {...defaultProps} correct={true} />);
|
|
56
|
-
const input = screen.getByTestId('correct-input');
|
|
57
|
-
|
|
58
|
-
expect(input).toHaveAttribute('data-correct', 'true');
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it('shows correct answer when showCorrectAnswer is true', () => {
|
|
62
|
-
render(<Input {...defaultProps} showCorrectAnswer={true} />);
|
|
63
|
-
const input = screen.getByTestId('correct-input');
|
|
64
|
-
|
|
65
|
-
expect(input).toHaveAttribute('data-correct', 'true');
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
describe('user interactions', () => {
|
|
70
|
-
it('calls onChange with id and value when user types', async () => {
|
|
71
|
-
const user = userEvent.setup();
|
|
72
|
-
render(<Input {...defaultProps} value="" />);
|
|
73
|
-
|
|
74
|
-
const input = screen.getByTestId('correct-input');
|
|
75
|
-
await user.type(input, '20');
|
|
76
|
-
|
|
77
|
-
// userEvent.type types character by character, so onChange is called for each character
|
|
78
|
-
expect(onChange).toHaveBeenCalled();
|
|
79
|
-
expect(onChange).toHaveBeenCalledTimes(2);
|
|
80
|
-
// Check the last call has both characters
|
|
81
|
-
expect(onChange).toHaveBeenLastCalledWith('1', '0');
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it('calls onChange with updated value', async () => {
|
|
85
|
-
const user = userEvent.setup();
|
|
86
|
-
render(<Input {...defaultProps} />);
|
|
87
|
-
|
|
88
|
-
const input = screen.getByTestId('correct-input');
|
|
89
|
-
await user.clear(input);
|
|
90
|
-
await user.type(input, 'New Value');
|
|
91
|
-
|
|
92
|
-
// userEvent.type types character by character
|
|
93
|
-
// After clear, we start with empty string, and each character is typed
|
|
94
|
-
// The last call should have the full accumulated value up to the last character
|
|
95
|
-
expect(onChange).toHaveBeenCalled();
|
|
96
|
-
// With clear + "New Value", onChange is called for clearing ("") and each typed character
|
|
97
|
-
// The value accumulated in the input element after typing will be "CowNew Value"
|
|
98
|
-
// because the component starts with value="Cow" and we clear then type
|
|
99
|
-
expect(onChange.mock.calls.length).toBeGreaterThan(0);
|
|
100
|
-
});
|
|
101
|
-
});
|
|
102
|
-
});
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { defaultKeyboardCoordinateGetter, KeyboardCode } from '@dnd-kit/core';
|
|
2
|
-
|
|
3
|
-
// Matches the id `drag-in-the-blank-dp.jsx` registers for the choice board's droppable.
|
|
4
|
-
const CHOICE_BOARD_ID = 'drag-in-the-blank-droppable';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Custom keyboard coordinate getter for drag-in-the-blank's Tab-based placement.
|
|
8
|
-
*
|
|
9
|
-
* Tab/Shift+Tab cycle the dragged item directly onto the next/previous enabled
|
|
10
|
-
* droppable — a blank (`mask-blank-drop-{id}`), sorted by on-screen position
|
|
11
|
-
* (top-to-bottom, then left-to-right, since blanks sit inline in flowing text rather
|
|
12
|
-
* than a simple list) — followed by the choice board as a fixed last stop (see below
|
|
13
|
-
* for why the board isn't sorted alongside the blanks).
|
|
14
|
-
*
|
|
15
|
-
* Arrow keys are delegated to dnd-kit's own `defaultKeyboardCoordinateGetter`, leaving
|
|
16
|
-
* the existing free-form arrow-key dragging behavior completely unchanged.
|
|
17
|
-
*/
|
|
18
|
-
export const closestDroppableKeyboardCoordinates = (event, { active, context, currentCoordinates }) => {
|
|
19
|
-
const { code } = event;
|
|
20
|
-
const isTab = code === KeyboardCode.Tab;
|
|
21
|
-
const isArrow =
|
|
22
|
-
code === KeyboardCode.Down || code === KeyboardCode.Up || code === KeyboardCode.Left || code === KeyboardCode.Right;
|
|
23
|
-
|
|
24
|
-
if (!isTab && !isArrow) {
|
|
25
|
-
return undefined;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (isArrow) {
|
|
29
|
-
return defaultKeyboardCoordinateGetter(event, { context, currentCoordinates });
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
event.preventDefault();
|
|
33
|
-
|
|
34
|
-
const { droppableRects, droppableContainers, collisionRect } = context;
|
|
35
|
-
|
|
36
|
-
if (!droppableRects || droppableRects.size === 0) {
|
|
37
|
-
return currentCoordinates;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// A blank is itself a droppable for its own slot ("mask-blank-drop-{id}"), and
|
|
41
|
-
// simultaneously draggable from that same slot — the same shape as match-list's
|
|
42
|
-
// placed "target" tiles. That self drop-zone must never be treated as a navigable
|
|
43
|
-
// target: exclude it outright rather than relying on a distance threshold.
|
|
44
|
-
const draggedData = active?.data?.current;
|
|
45
|
-
const ownDropId =
|
|
46
|
-
draggedData && draggedData.fromChoice === false && draggedData.id != null
|
|
47
|
-
? `mask-blank-drop-${draggedData.id}`
|
|
48
|
-
: undefined;
|
|
49
|
-
|
|
50
|
-
const targets = [];
|
|
51
|
-
let boardTarget;
|
|
52
|
-
|
|
53
|
-
for (const [id, container] of droppableContainers) {
|
|
54
|
-
if (container?.disabled) continue;
|
|
55
|
-
if (id === ownDropId) continue;
|
|
56
|
-
|
|
57
|
-
const rect = droppableRects.get(id);
|
|
58
|
-
if (!rect) continue;
|
|
59
|
-
|
|
60
|
-
const center = {
|
|
61
|
-
x: rect.left + rect.width / 2,
|
|
62
|
-
y: rect.top + rect.height / 2,
|
|
63
|
-
};
|
|
64
|
-
// Land the dragged item's own top-left corner at the target's center-left point,
|
|
65
|
-
// rather than at the target's own top-left corner (avoids overshooting into a
|
|
66
|
-
// neighboring droppable when the target is much wider than the dragged item).
|
|
67
|
-
const dropPosition = {
|
|
68
|
-
x: rect.left,
|
|
69
|
-
y: rect.top + rect.height / 2,
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
const target = { id, rect, dropPosition, center };
|
|
73
|
-
|
|
74
|
-
if (id === CHOICE_BOARD_ID) {
|
|
75
|
-
boardTarget = target;
|
|
76
|
-
} else {
|
|
77
|
-
targets.push(target);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (targets.length === 0 && !boardTarget) {
|
|
82
|
-
return currentCoordinates;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const reverse = event.shiftKey;
|
|
86
|
-
|
|
87
|
-
targets.sort((a, b) => {
|
|
88
|
-
if (Math.abs(a.center.y - b.center.y) > 10) return a.center.y - b.center.y;
|
|
89
|
-
return a.center.x - b.center.x;
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
// The choice board can be an arbitrarily large container — e.g. several choices
|
|
93
|
-
// stacked beside the markup (`choicePosition: 'right'`/`'left'`) — so its own center
|
|
94
|
-
// can land anywhere relative to the blanks it sits beside, including numerically
|
|
95
|
-
// between two of them. Sorting it by position the same way as a blank would then
|
|
96
|
-
// interleave it into the middle of the cycle, making a single Tab press jump over
|
|
97
|
-
// more than one blank (confirmed live: with 3 stacked choices beside wrapped text,
|
|
98
|
-
// the board's center fell between blank 2 and blank 3, so the very first Tab press
|
|
99
|
-
// from the pool landed on blank 3, skipping blanks 1 and 2 entirely). Anchor it as a
|
|
100
|
-
// fixed stop after every blank, in every layout, instead of trusting its own bounds.
|
|
101
|
-
if (boardTarget) {
|
|
102
|
-
targets.push(boardTarget);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// Find the current target: whichever target's rect actually contains the dragged
|
|
106
|
-
// item's own center. This holds regardless of how the dragged item's size compares
|
|
107
|
-
// to the target's — a compact chip landed (per `dropPosition` above) at a much wider
|
|
108
|
-
// target's left edge is still contained within that target's rect. Comparing
|
|
109
|
-
// distances between reconstructed centers instead breaks down for exactly that shape
|
|
110
|
-
// (a small item in a much wider target): the reconstructed center can end up closer
|
|
111
|
-
// to a completely different droppable than to the one the item is actually sitting
|
|
112
|
-
// on, so the *next* press re-matches the wrong target and looks like it does nothing.
|
|
113
|
-
const draggedCenter = collisionRect
|
|
114
|
-
? { x: collisionRect.left + collisionRect.width / 2, y: collisionRect.top + collisionRect.height / 2 }
|
|
115
|
-
: currentCoordinates;
|
|
116
|
-
|
|
117
|
-
let currentIndex = targets.findIndex(
|
|
118
|
-
(t) =>
|
|
119
|
-
draggedCenter.x >= t.rect.left &&
|
|
120
|
-
draggedCenter.x <= t.rect.right &&
|
|
121
|
-
draggedCenter.y >= t.rect.top &&
|
|
122
|
-
draggedCenter.y <= t.rect.bottom,
|
|
123
|
-
);
|
|
124
|
-
|
|
125
|
-
// Fall back to nearest-by-dropPosition if the dragged item's center isn't strictly
|
|
126
|
-
// inside any target (e.g. mid-flight after a free arrow-key move).
|
|
127
|
-
if (currentIndex === -1) {
|
|
128
|
-
let minDist = Infinity;
|
|
129
|
-
|
|
130
|
-
for (let i = 0; i < targets.length; i++) {
|
|
131
|
-
const dist = distance(currentCoordinates, targets[i].dropPosition);
|
|
132
|
-
|
|
133
|
-
if (dist < minDist) {
|
|
134
|
-
minDist = dist;
|
|
135
|
-
currentIndex = i;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const nextIndex = reverse
|
|
141
|
-
? (currentIndex - 1 + targets.length) % targets.length
|
|
142
|
-
: (currentIndex + 1) % targets.length;
|
|
143
|
-
|
|
144
|
-
return targets[nextIndex].dropPosition;
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
const distance = (a, b) => Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2);
|