kokopu-react 3.0.2 → 3.2.0
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/.eslintrc.yml +1 -1
- package/CHANGELOG.md +13 -1
- package/dist/lib/chessboard/BoardProperties.d.ts +91 -0
- package/dist/lib/chessboard/BoardProperties.js +43 -0
- package/dist/lib/chessboard/BoardProperties.js.map +1 -0
- package/dist/lib/chessboard/Chessboard.css +13 -13
- package/dist/lib/chessboard/Chessboard.d.ts +14 -58
- package/dist/lib/chessboard/Chessboard.js +26 -92
- package/dist/lib/chessboard/Chessboard.js.map +1 -1
- package/dist/lib/chessboard/ChessboardImpl.js.map +1 -1
- package/dist/lib/chessboard/Draggable.js.map +1 -1
- package/dist/lib/chessboard/Motion.js.map +1 -1
- package/dist/lib/chessboard/colorsets.js.map +1 -1
- package/dist/lib/chessboard/piecesets.js.map +1 -1
- package/dist/lib/errorbox/ErrorBox.css +18 -18
- package/dist/lib/errorbox/ErrorBox.js.map +1 -1
- package/dist/lib/errorbox/parsing.d.ts +57 -0
- package/dist/lib/errorbox/parsing.js +140 -0
- package/dist/lib/errorbox/parsing.js.map +1 -0
- package/dist/lib/exception.js.map +1 -1
- package/dist/lib/i18n.d.ts +6 -0
- package/dist/lib/i18n.js +7 -0
- package/dist/lib/i18n.js.map +1 -1
- package/dist/lib/icons/AnnotationSymbolShape.css +6 -6
- package/dist/lib/icons/AnnotationSymbolShape.js.map +1 -1
- package/dist/lib/icons/ArrowMarkerIcon.js.map +1 -1
- package/dist/lib/icons/ArrowTip.js.map +1 -1
- package/dist/lib/icons/ChessPieceIcon.js.map +1 -1
- package/dist/lib/icons/SquareMarkerIcon.js.map +1 -1
- package/dist/lib/icons/TextMarkerIcon.js.map +1 -1
- package/dist/lib/icons/arrow.css +1 -1
- package/dist/lib/index.d.ts +5 -1
- package/dist/lib/index.js +3 -0
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/movetext/Movetext.css +27 -40
- package/dist/lib/movetext/Movetext.d.ts +9 -43
- package/dist/lib/movetext/Movetext.js +15 -67
- package/dist/lib/movetext/Movetext.js.map +1 -1
- package/dist/lib/movetext/MovetextImpl.d.ts +7 -28
- package/dist/lib/movetext/MovetextImpl.js +15 -94
- package/dist/lib/movetext/MovetextImpl.js.map +1 -1
- package/dist/lib/movetext/fonts.css +7 -7
- package/dist/lib/movetext/htmlFilter.js.map +1 -1
- package/dist/lib/movetext/moveFormatter.js.map +1 -1
- package/dist/lib/navigationboard/NavigationBoard.d.ts +156 -0
- package/dist/lib/navigationboard/NavigationBoard.js +195 -0
- package/dist/lib/navigationboard/NavigationBoard.js.map +1 -0
- package/dist/lib/navigationboard/NavigationButton.d.ts +54 -0
- package/dist/lib/navigationboard/NavigationButton.js +35 -0
- package/dist/lib/navigationboard/NavigationButton.js.map +1 -0
- package/dist/lib/navigationboard/NavigationField.css +36 -0
- package/dist/lib/navigationboard/NavigationField.d.ts +81 -0
- package/dist/lib/navigationboard/NavigationField.js +154 -0
- package/dist/lib/navigationboard/NavigationField.js.map +1 -0
- package/dist/lib/navigationboard/NavigationToolbar.css +54 -0
- package/dist/lib/navigationboard/NavigationToolbar.d.ts +34 -0
- package/dist/lib/navigationboard/NavigationToolbar.js +73 -0
- package/dist/lib/navigationboard/NavigationToolbar.js.map +1 -0
- package/dist/lib/navigationboard/iconPaths.d.ts +29 -0
- package/dist/lib/navigationboard/iconPaths.js +91 -0
- package/dist/lib/navigationboard/iconPaths.js.map +1 -0
- package/dist/lib/sanitization.js.map +1 -1
- package/dist/lib/types.js.map +1 -1
- package/dist/lib/util.js.map +1 -1
- package/doc_src/demo/PageChessboardBase.tsx +115 -115
- package/doc_src/demo/PageChessboardInteraction.tsx +303 -303
- package/doc_src/demo/PageChessboardMove.tsx +173 -156
- package/doc_src/demo/PageChessboardSmallScreens.tsx +94 -94
- package/doc_src/demo/PageMovetextBase.tsx +180 -180
- package/doc_src/demo/PageMovetextInteraction.tsx +123 -123
- package/doc_src/demo/PageNavigationBoardBase.tsx +201 -0
- package/doc_src/demo/PageNavigationBoardUncontrolledVsControlled.tsx +114 -0
- package/doc_src/demo/decs.d.ts +4 -4
- package/doc_src/demo/demo.css +12 -12
- package/doc_src/demo/game-3.pgn +10 -0
- package/doc_src/demo/util.ts +1 -1
- package/doc_src/examples/Movetext.md +1 -1
- package/doc_src/examples/NavigationBoard.md +15 -0
- package/doc_src/theming/LogoRenderer.js +13 -13
- package/doc_src/theming/theming.css +5 -5
- package/package.json +95 -95
- package/scripts/doc.styleguide.config.js +133 -123
- package/scripts/doc.tsconfig.json +5 -5
- package/scripts/docker-compose.yml +1 -1
- package/scripts/test-graphic.tsconfig.json +5 -5
- package/scripts/test-graphic.webpack.config.js +57 -57
- package/scripts/test-headless.webpack.config.js +29 -29
- package/src/chessboard/BoardProperties.ts +131 -0
- package/src/chessboard/Chessboard.css +13 -13
- package/src/chessboard/Chessboard.tsx +389 -518
- package/src/chessboard/ChessboardImpl.tsx +557 -557
- package/src/chessboard/Draggable.tsx +185 -185
- package/src/chessboard/Motion.tsx +67 -67
- package/src/chessboard/colorsets.ts +66 -66
- package/src/chessboard/piecesets.ts +84 -84
- package/src/decs.d.ts +2 -2
- package/src/errorbox/ErrorBox.css +18 -18
- package/src/errorbox/ErrorBox.tsx +75 -75
- package/src/errorbox/parsing.tsx +158 -0
- package/src/exception.ts +17 -17
- package/src/i18n.ts +8 -0
- package/src/icons/AnnotationSymbolShape.css +6 -6
- package/src/icons/AnnotationSymbolShape.tsx +31 -31
- package/src/icons/ArrowMarkerIcon.tsx +34 -34
- package/src/icons/ArrowTip.tsx +14 -14
- package/src/icons/ChessPieceIcon.tsx +45 -45
- package/src/icons/SquareMarkerIcon.tsx +20 -20
- package/src/icons/TextMarkerIcon.tsx +29 -29
- package/src/icons/arrow.css +1 -1
- package/src/index.ts +8 -3
- package/src/movetext/Movetext.css +27 -40
- package/src/movetext/Movetext.tsx +163 -228
- package/src/movetext/MovetextImpl.tsx +351 -438
- package/src/movetext/fonts.css +7 -7
- package/src/movetext/htmlFilter.tsx +129 -129
- package/src/movetext/moveFormatter.tsx +37 -37
- package/src/navigationboard/NavigationBoard.tsx +350 -0
- package/src/navigationboard/NavigationButton.ts +72 -0
- package/src/navigationboard/NavigationField.css +36 -0
- package/src/navigationboard/NavigationField.tsx +185 -0
- package/src/navigationboard/NavigationToolbar.css +54 -0
- package/src/navigationboard/NavigationToolbar.tsx +117 -0
- package/src/navigationboard/iconPaths.ts +98 -0
- package/src/sanitization.ts +17 -17
- package/src/types.ts +116 -116
- package/src/util.ts +11 -11
- package/test/01_types.js +167 -167
- package/test/02_chessboard_util.js +114 -114
- package/test/03_error_box_graphic.js +16 -16
- package/test/04_icons_graphic.js +32 -32
- package/test/05_chessboard_graphic.js +60 -58
- package/test/06_chessboard_animation.js +36 -36
- package/test/07_chessboard_interaction.js +101 -101
- package/test/08_chessboard_play_moves.js +62 -62
- package/test/09_chessboard_small_screens.js +42 -21
- package/test/10_movetext_graphic.js +36 -31
- package/test/11_movetext_interaction.js +100 -96
- package/test/12_navigation_board_base.js +151 -0
- package/test/13_navigation_board_graphic.js +68 -0
- package/test/14_navigation_board_autoplay.js +100 -0
- package/test/common/graphic.js +106 -97
- package/test/graphic_references/05_chessboard_graphic/move/illegal_null-move.png +0 -0
- package/test/graphic_references/05_chessboard_graphic/move/null-move.png +0 -0
- package/test/graphic_references/09_chessboard_small_screens/top_bottom_components/default_bottom_component.png +0 -0
- package/test/graphic_references/09_chessboard_small_screens/top_bottom_components/default_top_component.png +0 -0
- package/test/graphic_references/09_chessboard_small_screens/top_bottom_components/reduced_bottom_component.png +0 -0
- package/test/graphic_references/09_chessboard_small_screens/top_bottom_components/reduced_top_component.png +0 -0
- package/test/graphic_references/10_movetext_graphic/format_move/format_text.png +0 -0
- package/test/graphic_references/10_movetext_graphic/options/wrong_piece_symbol_value.png +0 -0
- package/test/graphic_references/11_movetext_interaction/select_moves/click_on_unselected_without_callback.png +0 -0
- package/test/graphic_references/12_navigation_board_base/controlled/default.png +0 -0
- package/test/graphic_references/12_navigation_board_base/controlled/forbidden_navigation_0.png +0 -0
- package/test/graphic_references/12_navigation_board_base/controlled/forbidden_navigation_1.png +0 -0
- package/test/graphic_references/12_navigation_board_base/controlled/invalid_node_id.png +0 -0
- package/test/graphic_references/12_navigation_board_base/controlled/no_callback_defined_0.png +0 -0
- package/test/graphic_references/12_navigation_board_base/controlled/no_callback_defined_1.png +0 -0
- package/test/graphic_references/12_navigation_board_base/controlled/overriding_initial_node_id.png +0 -0
- package/test/graphic_references/12_navigation_board_base/controlled/regular_navigation_0.png +0 -0
- package/test/graphic_references/12_navigation_board_base/controlled/regular_navigation_1.png +0 -0
- package/test/graphic_references/12_navigation_board_base/controlled/regular_navigation_2.png +0 -0
- package/test/graphic_references/12_navigation_board_base/controlled/regular_navigation_3.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/forbidden_navigation_at_beginning_0.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/forbidden_navigation_at_beginning_1.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/forbidden_navigation_at_beginning_2.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/forbidden_navigation_at_end_0.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/forbidden_navigation_at_end_1.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/forbidden_navigation_at_end_2.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/initial_state_at_end.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/initial_state_at_specific_id.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/initial_state_default.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/initial_state_default_with_game_index.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/initial_state_on_null-move.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/navigation_with_keys_0.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/navigation_with_keys_1.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/navigation_with_keys_2.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/navigation_with_keys_3.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/navigation_with_keys_4.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/regular_flipping_0.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/regular_flipping_1.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/regular_navigation_0.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/regular_navigation_1.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/regular_navigation_2.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/regular_navigation_3.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/regular_navigation_4.png +0 -0
- package/test/graphic_references/12_navigation_board_base/uncontrolled/regular_navigation_5.png +0 -0
- package/test/graphic_references/13_navigation_board_graphic/additional_buttons/error_1.png +0 -0
- package/test/graphic_references/13_navigation_board_graphic/additional_buttons/error_2.png +0 -0
- package/test/graphic_references/13_navigation_board_graphic/additional_buttons/multiple_buttons.png +0 -0
- package/test/graphic_references/13_navigation_board_graphic/additional_buttons/single_button.png +0 -0
- package/test/graphic_references/13_navigation_board_graphic/error/pgn_parsing_error.png +0 -0
- package/test/graphic_references/13_navigation_board_graphic/error/wrong_game_type.png +0 -0
- package/test/graphic_references/13_navigation_board_graphic/theme/custom_1.png +0 -0
- package/test/graphic_references/13_navigation_board_graphic/theme/custom_2.png +0 -0
- package/test/graphic_references/13_navigation_board_graphic/theme/large.png +0 -0
- package/test/graphic_references/13_navigation_board_graphic/theme/small.png +0 -0
- package/test/graphic_references/14_navigation_board_autoplay/controlled/after_1_move_acknowledging_is-playing_changes.png +0 -0
- package/test/graphic_references/14_navigation_board_autoplay/controlled/after_1_move_not_acknowledging_is-playing_changes.png +0 -0
- package/test/graphic_references/14_navigation_board_autoplay/controlled/after_2_moves_acknowledging_node_changes.png +0 -0
- package/test/graphic_references/14_navigation_board_autoplay/controlled/after_2_moves_not_acknowledging_node_changes.png +0 -0
- package/test/graphic_references/14_navigation_board_autoplay/controlled/after_reaching_the_end.png +0 -0
- package/test/graphic_references/14_navigation_board_autoplay/controlled/already_at_the_end.png +0 -0
- package/test/graphic_references/14_navigation_board_autoplay/controlled/initial_state_acknowledging_is-playing_changes.png +0 -0
- package/test/graphic_references/14_navigation_board_autoplay/controlled/initial_state_acknowledging_node_changes.png +0 -0
- package/test/graphic_references/14_navigation_board_autoplay/controlled/initial_state_at_the_end.png +0 -0
- package/test/graphic_references/14_navigation_board_autoplay/controlled/initial_state_close_to_end.png +0 -0
- package/test/graphic_references/14_navigation_board_autoplay/controlled/initial_state_not_acknowledging_is-playing_changes.png +0 -0
- package/test/graphic_references/14_navigation_board_autoplay/controlled/initial_state_not_acknowledging_node_changes.png +0 -0
- package/test/graphic_references/14_navigation_board_autoplay/uncontrolled/after_2_moves_from_beginning_after_stop.png +0 -0
- package/test/graphic_references/14_navigation_board_autoplay/uncontrolled/after_2_moves_from_beginning_before_stop.png +0 -0
- package/test/graphic_references/14_navigation_board_autoplay/uncontrolled/after_reaching_the_end_after_stop.png +0 -0
- package/test/graphic_references/14_navigation_board_autoplay/uncontrolled/after_reaching_the_end_before_stop.png +0 -0
- package/test/graphic_references/14_navigation_board_autoplay/uncontrolled/initial_state_close_to_end.png +0 -0
- package/test/graphic_references/14_navigation_board_autoplay/uncontrolled/initial_state_default.png +0 -0
- package/test/graphic_test_app/03_error_box_graphic/base.tsx +14 -14
- package/test/graphic_test_app/03_error_box_graphic/localization.tsx +1 -1
- package/test/graphic_test_app/04_icons_graphic/chess_piece.tsx +7 -7
- package/test/graphic_test_app/04_icons_graphic/marker.tsx +15 -15
- package/test/graphic_test_app/05_chessboard_graphic/annotations.tsx +7 -7
- package/test/graphic_test_app/05_chessboard_graphic/base.tsx +6 -6
- package/test/graphic_test_app/05_chessboard_graphic/chess_variants.tsx +7 -7
- package/test/graphic_test_app/05_chessboard_graphic/flipped.tsx +5 -5
- package/test/graphic_test_app/05_chessboard_graphic/move.tsx +12 -10
- package/test/graphic_test_app/05_chessboard_graphic/theme.tsx +12 -12
- package/test/graphic_test_app/06_chessboard_animation/base_1.tsx +6 -6
- package/test/graphic_test_app/06_chessboard_animation/base_2.tsx +6 -6
- package/test/graphic_test_app/06_chessboard_animation/base_3.tsx +6 -6
- package/test/graphic_test_app/06_chessboard_animation/on_redraw.tsx +16 -16
- package/test/graphic_test_app/07_chessboard_interaction/click_squares.tsx +5 -5
- package/test/graphic_test_app/07_chessboard_interaction/edit_arrows.tsx +4 -4
- package/test/graphic_test_app/07_chessboard_interaction/error.tsx +2 -2
- package/test/graphic_test_app/07_chessboard_interaction/move_pieces.tsx +5 -5
- package/test/graphic_test_app/08_chessboard_play_moves/base.tsx +8 -8
- package/test/graphic_test_app/08_chessboard_play_moves/promotions.tsx +4 -4
- package/test/graphic_test_app/09_chessboard_small_screens/base.tsx +9 -9
- package/test/graphic_test_app/09_chessboard_small_screens/top_bottom_components.tsx +48 -0
- package/test/graphic_test_app/10_movetext_graphic/base.tsx +4 -4
- package/test/graphic_test_app/10_movetext_graphic/error.tsx +6 -6
- package/test/graphic_test_app/10_movetext_graphic/format_move.tsx +31 -0
- package/test/graphic_test_app/10_movetext_graphic/html.tsx +5 -5
- package/test/graphic_test_app/10_movetext_graphic/options.tsx +10 -9
- package/test/graphic_test_app/11_movetext_interaction/error.tsx +1 -1
- package/test/graphic_test_app/11_movetext_interaction/select_moves.tsx +11 -10
- package/test/graphic_test_app/12_navigation_board_base/controlled.tsx +43 -0
- package/test/graphic_test_app/12_navigation_board_base/uncontrolled.tsx +56 -0
- package/test/graphic_test_app/13_navigation_board_graphic/additional_buttons.tsx +49 -0
- package/test/graphic_test_app/13_navigation_board_graphic/error.tsx +34 -0
- package/test/graphic_test_app/13_navigation_board_graphic/theme.tsx +38 -0
- package/test/graphic_test_app/14_navigation_board_autoplay/controlled.tsx +73 -0
- package/test/graphic_test_app/14_navigation_board_autoplay/uncontrolled.tsx +34 -0
- package/test/graphic_test_app/common/decs.d.ts +2 -2
- package/test/graphic_test_app/common/test_app.css +15 -15
- package/test/graphic_test_app/common/test_app.tsx +48 -48
- package/tsconfig.json +13 -13
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* -------------------------------------------------------------------------- *
|
|
3
|
+
* *
|
|
4
|
+
* Kokopu-React - A React-based library of chess-related components. *
|
|
5
|
+
* <https://www.npmjs.com/package/kokopu-react> *
|
|
6
|
+
* Copyright (C) 2021-2024 Yoann Le Montagner <yo35 -at- melix.net> *
|
|
7
|
+
* *
|
|
8
|
+
* Kokopu-React is free software: you can redistribute it and/or *
|
|
9
|
+
* modify it under the terms of the GNU Lesser General Public License *
|
|
10
|
+
* as published by the Free Software Foundation, either version 3 of *
|
|
11
|
+
* the License, or (at your option) any later version. *
|
|
12
|
+
* *
|
|
13
|
+
* Kokopu-React is distributed in the hope that it will be useful, *
|
|
14
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
15
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
16
|
+
* GNU Lesser General Public License for more details. *
|
|
17
|
+
* *
|
|
18
|
+
* You should have received a copy of the GNU Lesser General *
|
|
19
|
+
* Public License along with this program. If not, see *
|
|
20
|
+
* <http://www.gnu.org/licenses/>. *
|
|
21
|
+
* *
|
|
22
|
+
* -------------------------------------------------------------------------- */
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
import * as React from 'react';
|
|
26
|
+
|
|
27
|
+
import { Database, Game, Node as GameNode, Variation } from 'kokopu';
|
|
28
|
+
|
|
29
|
+
import { IllegalArgument } from '../exception';
|
|
30
|
+
import { i18n } from '../i18n';
|
|
31
|
+
import { sanitizeBoolean, sanitizeOptional, sanitizeString } from '../sanitization';
|
|
32
|
+
|
|
33
|
+
import { DynamicBoardGraphicProps, defaultDynamicBoardProps } from '../chessboard/BoardProperties';
|
|
34
|
+
import { Chessboard } from '../chessboard/Chessboard';
|
|
35
|
+
import { parseGame } from '../errorbox/parsing';
|
|
36
|
+
import { NavigationField, firstNodeId, previousNodeId, nextNodeId, lastNodeId } from '../navigationboard/NavigationField';
|
|
37
|
+
import { GO_FIRST_ICON_PATH, GO_PREVIOUS_ICON_PATH, GO_NEXT_ICON_PATH, GO_LAST_ICON_PATH, PLAY_ICON_PATH, STOP_ICON_PATH, FLIP_ICON_PATH } from './iconPaths';
|
|
38
|
+
import { NavigationButton, NavigationButtonList, isNavigationButton } from './NavigationButton';
|
|
39
|
+
import { NavigationToolbar } from './NavigationToolbar';
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
const INTER_MOVE_DURATION = 1000;
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
export interface NavigationBoardProps extends DynamicBoardGraphicProps {
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Displayed game. Can be a [kokopu.Game](https://kokopu.yo35.org/docs/current/classes/Game.html) object,
|
|
49
|
+
* a [kokopu.Database](https://kokopu.yo35.org/docs/current/classes/Database.html) object,
|
|
50
|
+
* or a [PGN string](https://en.wikipedia.org/wiki/Portable_Game_Notation).
|
|
51
|
+
*/
|
|
52
|
+
game: Game | Database | string;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Index of the game to display (only if attribute `game` is a [kokopu.Database](https://kokopu.yo35.org/docs/current/classes/Database.html)
|
|
56
|
+
* or a [PGN string](https://en.wikipedia.org/wiki/Portable_Game_Notation)): `0` for the first game of the database/PGN, `1` for the second one, etc.
|
|
57
|
+
* If omitted, the first game of the database/PGN is displayed.
|
|
58
|
+
*/
|
|
59
|
+
gameIndex: number;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* ID of the move initially selected (or `'start'`/`'end'` for the beginning/end of the main variation) when the component is uncontrolled.
|
|
63
|
+
* Use [kokopu.Node#id](https://kokopu.yo35.org/docs/current/classes/Node.html#id) to get the ID of a game move.
|
|
64
|
+
* Ignored if the `nodeId` attribute is provided.
|
|
65
|
+
*/
|
|
66
|
+
initialNodeId: string;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* ID of the selected move (or `'start'`/`'end'` for the beginning/end of the main variation).
|
|
70
|
+
* Use [kokopu.Node#id](https://kokopu.yo35.org/docs/current/classes/Node.html#id) to get the ID of a game move.
|
|
71
|
+
* If provided (i.e. if the component is controlled), the attribute `onNodeIdChanged` must be provided as well.
|
|
72
|
+
*/
|
|
73
|
+
nodeId?: string;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Callback invoked in controlled-component mode, when the user changes the selected move.
|
|
77
|
+
*
|
|
78
|
+
* @param nodeId - ID of the selected move (as returned by [kokopu.Node#id](https://kokopu.yo35.org/docs/current/classes/Node.html#id)),
|
|
79
|
+
* or `'start'` for the beginning of the main variation.
|
|
80
|
+
*/
|
|
81
|
+
onNodeIdChanged?: (nodeId: string) => void;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Whether auto-play is initially enabled or not.
|
|
85
|
+
* Ignored if the `isPlaying` attribute is provided.
|
|
86
|
+
*/
|
|
87
|
+
initialIsPlaying: boolean;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Whether auto-play is enabled or not.
|
|
91
|
+
* If provided (i.e. if the is-playing state is controlled), the attribute `onIsPlayingChanged` must be provided as well.
|
|
92
|
+
*/
|
|
93
|
+
isPlaying?: boolean;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Callback invoked in controlled-is-playing-state mode, when the user clicks on the play/stop button.
|
|
97
|
+
*
|
|
98
|
+
* @param isPlaying - New is-playing state.
|
|
99
|
+
*/
|
|
100
|
+
onIsPlayingChanged?: (isPlaying: boolean) => void;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Whether the board is initially flipped (i.e. seen from Black's point of view) or not, when the flip state is uncontrolled.
|
|
104
|
+
* Ignored if the `flipped` attribute is provided.
|
|
105
|
+
*/
|
|
106
|
+
initialFlipped: boolean;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Whether the board is flipped (i.e. seen from Black's point of view) or not.
|
|
110
|
+
* If provided (i.e. if the flip state is controlled), the attribute `onFlippedChanged` must be provided as well.
|
|
111
|
+
*/
|
|
112
|
+
flipped?: boolean;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Callback invoked in controlled-flip-state mode, when the user flips the board.
|
|
116
|
+
*
|
|
117
|
+
* @param flipped - New flip state.
|
|
118
|
+
*/
|
|
119
|
+
onFlippedChanged?: (flipped: boolean) => void;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Whether the play/stop button is visible or not in the toolbar.
|
|
123
|
+
*/
|
|
124
|
+
playButtonVisible: boolean;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Whether the flip button is visible or not in the toolbar.
|
|
128
|
+
*/
|
|
129
|
+
flipButtonVisible: boolean;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Additional buttons to be added to the toolbar.
|
|
133
|
+
*/
|
|
134
|
+
additionalButtons: NavigationButton | NavigationButtonList;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
interface NavigationBoardState {
|
|
139
|
+
nodeIdAsUncontrolled: string;
|
|
140
|
+
isPlayingAsUncontrolled: boolean;
|
|
141
|
+
flippedAsUncontrolled: boolean;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Component displaying the positions occurring in a chess game, with navigation buttons to browse these positions.
|
|
147
|
+
*/
|
|
148
|
+
export class NavigationBoard extends React.Component<NavigationBoardProps, NavigationBoardState> {
|
|
149
|
+
|
|
150
|
+
static defaultProps = {
|
|
151
|
+
...defaultDynamicBoardProps(),
|
|
152
|
+
game: new Game(),
|
|
153
|
+
gameIndex: 0,
|
|
154
|
+
initialNodeId: 'start',
|
|
155
|
+
initialIsPlaying: false,
|
|
156
|
+
initialFlipped: false,
|
|
157
|
+
playButtonVisible: false,
|
|
158
|
+
flipButtonVisible: true,
|
|
159
|
+
additionalButtons: [],
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
private navigationFieldRef: React.RefObject<NavigationField> = React.createRef();
|
|
163
|
+
private timeoutId?: number;
|
|
164
|
+
|
|
165
|
+
constructor(props: NavigationBoardProps) {
|
|
166
|
+
super(props);
|
|
167
|
+
this.state = {
|
|
168
|
+
nodeIdAsUncontrolled: sanitizeString(props.initialNodeId),
|
|
169
|
+
isPlayingAsUncontrolled: sanitizeBoolean(props.initialIsPlaying),
|
|
170
|
+
flippedAsUncontrolled: sanitizeBoolean(props.initialFlipped),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
componentWillUnmount() {
|
|
175
|
+
this.cancelCurrentTimeout();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
render() {
|
|
179
|
+
this.cancelCurrentTimeout();
|
|
180
|
+
|
|
181
|
+
// Validate the game and game-index attributes.
|
|
182
|
+
const info = parseGame(this.props.game, this.props.gameIndex, 'NavigationBoard');
|
|
183
|
+
if (info.error) {
|
|
184
|
+
return info.errorBox;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Fetch the current node/variation.
|
|
188
|
+
const currentNodeId = sanitizeOptional(this.props.nodeId, sanitizeString) ?? this.state.nodeIdAsUncontrolled;
|
|
189
|
+
const currentNode = info.game.findById(currentNodeId) ?? info.game.mainVariation();
|
|
190
|
+
|
|
191
|
+
// State flags.
|
|
192
|
+
const isPlaying = sanitizeOptional(this.props.isPlaying, sanitizeBoolean) ?? this.state.isPlayingAsUncontrolled;
|
|
193
|
+
const flipped = sanitizeOptional(this.props.flipped, sanitizeBoolean) ?? this.state.flippedAsUncontrolled;
|
|
194
|
+
|
|
195
|
+
return (
|
|
196
|
+
<div className="kokopu-navigationBoard">
|
|
197
|
+
{this.renderBoard(info.game, currentNode, isPlaying, flipped)}
|
|
198
|
+
{this.renderNavigationField(info.game, currentNode.id())}
|
|
199
|
+
</div>
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private renderBoard(game: Game, node: GameNode | Variation, isPlaying: boolean, flipped: boolean) {
|
|
204
|
+
const position = node instanceof GameNode ? node.positionBefore() : node.initialPosition();
|
|
205
|
+
const move = node instanceof GameNode ? node.notation() : undefined;
|
|
206
|
+
return <Chessboard
|
|
207
|
+
position={position} move={move} flipped={flipped}
|
|
208
|
+
squareMarkers={node.tag('csl')} arrowMarkers={node.tag('cal')} textMarkers={node.tag('ctl')}
|
|
209
|
+
squareSize={this.props.squareSize}
|
|
210
|
+
coordinateVisible={this.props.coordinateVisible}
|
|
211
|
+
turnVisible={this.props.turnVisible}
|
|
212
|
+
colorset={this.props.colorset}
|
|
213
|
+
pieceset={this.props.pieceset}
|
|
214
|
+
smallScreenLimits={this.props.smallScreenLimits}
|
|
215
|
+
moveArrowVisible={this.props.moveArrowVisible}
|
|
216
|
+
moveArrowColor={this.props.moveArrowColor}
|
|
217
|
+
animated={this.props.animated}
|
|
218
|
+
bottomComponent={({ squareSize }) => this.renderToolbar(game, node, squareSize, isPlaying)}
|
|
219
|
+
/>;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private renderNavigationField(game: Game, currentNodeId: string) {
|
|
223
|
+
return <NavigationField ref={this.navigationFieldRef}
|
|
224
|
+
onFirstPressed={() => this.handleNavClicked(firstNodeId(game, currentNodeId))}
|
|
225
|
+
onPreviousPressed={() => this.handleNavClicked(previousNodeId(game, currentNodeId))}
|
|
226
|
+
onNextPressed={() => this.handleNavClicked(nextNodeId(game, currentNodeId))}
|
|
227
|
+
onLastPressed={() => this.handleNavClicked(lastNodeId(game, currentNodeId))}
|
|
228
|
+
/>;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
private renderToolbar(game: Game, node: GameNode | Variation, squareSize: number, isPlaying: boolean) {
|
|
232
|
+
const buttons: NavigationButtonList = [];
|
|
233
|
+
|
|
234
|
+
// Core navigation buttons
|
|
235
|
+
const currentNodeId = node.id();
|
|
236
|
+
const hasPrevious = currentNodeId !== 'start';
|
|
237
|
+
const hasNext = (node instanceof Variation ? node.first() : node.next()) !== undefined;
|
|
238
|
+
buttons.push({ iconPath: GO_FIRST_ICON_PATH, tooltip: i18n.TOOLTIP_GO_FIRST, enabled: hasPrevious, onClick: () => this.handleNavClicked(firstNodeId(game, currentNodeId)) });
|
|
239
|
+
buttons.push({ iconPath: GO_PREVIOUS_ICON_PATH, tooltip: i18n.TOOLTIP_GO_PREVIOUS, enabled: hasPrevious, onClick: () => this.handleNavClicked(previousNodeId(game, currentNodeId)) });
|
|
240
|
+
if (sanitizeBoolean(this.props.playButtonVisible)) {
|
|
241
|
+
buttons.push({ iconPath: isPlaying ? STOP_ICON_PATH : PLAY_ICON_PATH, tooltip: i18n.TOOLTIP_PLAY_STOP, enabled: hasNext, onClick: () => this.handlePlayStopClicked(!isPlaying) });
|
|
242
|
+
}
|
|
243
|
+
buttons.push({ iconPath: GO_NEXT_ICON_PATH, tooltip: i18n.TOOLTIP_GO_NEXT, enabled: hasNext, onClick: () => this.handleNavClicked(nextNodeId(game, currentNodeId)) });
|
|
244
|
+
buttons.push({ iconPath: GO_LAST_ICON_PATH, tooltip: i18n.TOOLTIP_GO_LAST, enabled: hasNext, onClick: () => this.handleNavClicked(lastNodeId(game, currentNodeId)) });
|
|
245
|
+
buttons.push('spacer');
|
|
246
|
+
if (sanitizeBoolean(this.props.flipButtonVisible)) {
|
|
247
|
+
buttons.push({ iconPath: FLIP_ICON_PATH, tooltip: i18n.TOOLTIP_FLIP, onClick: () => this.handleFlipButtonClicked() });
|
|
248
|
+
}
|
|
249
|
+
buttons.push('spacer');
|
|
250
|
+
|
|
251
|
+
// Schedule the next transition if auto-play is enabled.
|
|
252
|
+
if (isPlaying) {
|
|
253
|
+
if (hasNext) {
|
|
254
|
+
this.timeoutId = window.setTimeout(() => this.handleNavClicked(nextNodeId(game, currentNodeId), false), INTER_MOVE_DURATION);
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
// ... or stop the auto-play if at the end of the game.
|
|
258
|
+
this.timeoutId = window.setTimeout(() => this.handlePlayStopClicked(false, false), 0);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// Additional buttons.
|
|
263
|
+
const additionalButtons = sanitizeNavigationButtonList(this.props.additionalButtons, () => new IllegalArgument('NavigationBoard', 'additionalButtons'));
|
|
264
|
+
for (const button of additionalButtons) {
|
|
265
|
+
buttons.push(button);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return <NavigationToolbar squareSize={squareSize} buttons={buttons} />;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
private handleNavClicked(targetNodeId: string | undefined, forceFocus = true) {
|
|
272
|
+
if (forceFocus) {
|
|
273
|
+
this.focus();
|
|
274
|
+
}
|
|
275
|
+
if (targetNodeId === undefined) {
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (this.props.nodeId === undefined) { // uncontrolled-component behavior
|
|
280
|
+
this.setState({ nodeIdAsUncontrolled: targetNodeId });
|
|
281
|
+
}
|
|
282
|
+
else if (this.props.onNodeIdChanged) { // controlled-component behavior
|
|
283
|
+
this.props.onNodeIdChanged(targetNodeId);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
private handlePlayStopClicked(targetIsPlaying: boolean, forceFocus = true) {
|
|
288
|
+
if (forceFocus) {
|
|
289
|
+
this.focus();
|
|
290
|
+
}
|
|
291
|
+
if (this.props.isPlaying === undefined) { // uncontrolled is-playing state
|
|
292
|
+
this.setState({ isPlayingAsUncontrolled: targetIsPlaying });
|
|
293
|
+
}
|
|
294
|
+
else if (this.props.onIsPlayingChanged) { // controlled is-playing state
|
|
295
|
+
this.props.onIsPlayingChanged(targetIsPlaying);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
private handleFlipButtonClicked() {
|
|
300
|
+
this.focus();
|
|
301
|
+
if (this.props.flipped === undefined) { // uncontrolled flip state
|
|
302
|
+
this.setState({ flippedAsUncontrolled: !this.state.flippedAsUncontrolled });
|
|
303
|
+
}
|
|
304
|
+
else if (this.props.onFlippedChanged) { // controlled flip state
|
|
305
|
+
this.props.onFlippedChanged(!this.props.flipped);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
private cancelCurrentTimeout() {
|
|
310
|
+
if (this.timeoutId !== undefined) {
|
|
311
|
+
window.clearTimeout(this.timeoutId);
|
|
312
|
+
this.timeoutId = undefined;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Set the focus to the current component.
|
|
318
|
+
*
|
|
319
|
+
* @public
|
|
320
|
+
*/
|
|
321
|
+
focus(): void {
|
|
322
|
+
const target = this.navigationFieldRef.current;
|
|
323
|
+
// istanbul ignore else
|
|
324
|
+
if (target) {
|
|
325
|
+
target.focus();
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Sanitization method for the additional button parameter.
|
|
334
|
+
*/
|
|
335
|
+
function sanitizeNavigationButtonList(type: NavigationButton | NavigationButtonList, exceptionBuilder: () => IllegalArgument): NavigationButtonList {
|
|
336
|
+
if (Array.isArray(type)) {
|
|
337
|
+
return type.map(t => {
|
|
338
|
+
if (t !== 'spacer' && !isNavigationButton(t)) {
|
|
339
|
+
throw exceptionBuilder();
|
|
340
|
+
}
|
|
341
|
+
return t;
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
else if (isNavigationButton(type)) {
|
|
345
|
+
return [ type ];
|
|
346
|
+
}
|
|
347
|
+
else {
|
|
348
|
+
throw exceptionBuilder();
|
|
349
|
+
}
|
|
350
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* -------------------------------------------------------------------------- *
|
|
3
|
+
* *
|
|
4
|
+
* Kokopu-React - A React-based library of chess-related components. *
|
|
5
|
+
* <https://www.npmjs.com/package/kokopu-react> *
|
|
6
|
+
* Copyright (C) 2021-2024 Yoann Le Montagner <yo35 -at- melix.net> *
|
|
7
|
+
* *
|
|
8
|
+
* Kokopu-React is free software: you can redistribute it and/or *
|
|
9
|
+
* modify it under the terms of the GNU Lesser General Public License *
|
|
10
|
+
* as published by the Free Software Foundation, either version 3 of *
|
|
11
|
+
* the License, or (at your option) any later version. *
|
|
12
|
+
* *
|
|
13
|
+
* Kokopu-React is distributed in the hope that it will be useful, *
|
|
14
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
15
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
16
|
+
* GNU Lesser General Public License for more details. *
|
|
17
|
+
* *
|
|
18
|
+
* You should have received a copy of the GNU Lesser General *
|
|
19
|
+
* Public License along with this program. If not, see *
|
|
20
|
+
* <http://www.gnu.org/licenses/>. *
|
|
21
|
+
* *
|
|
22
|
+
* -------------------------------------------------------------------------- */
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Descriptor for additional buttons to be added to the toolbar of {@link NavigationBoard}.
|
|
27
|
+
*/
|
|
28
|
+
export type NavigationButton = {
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Button icon, defined as a SVG path (see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d#path_commands for more details
|
|
32
|
+
* on how to define a SVG path).
|
|
33
|
+
*
|
|
34
|
+
* The path is rendered within 32 x 32 viewbox (defined as `viewbox="0 0 32 32"`). It must be be a closed path, and must fit within the viewbox incircle.
|
|
35
|
+
*/
|
|
36
|
+
iconPath: string;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Human-readable tooltip.
|
|
40
|
+
*/
|
|
41
|
+
tooltip?: string;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Whether or not the button is enabled (true by default).
|
|
45
|
+
*/
|
|
46
|
+
enabled?: boolean;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Callback invoked when the user clicks on the button.
|
|
50
|
+
*/
|
|
51
|
+
onClick?: () => void;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* List of additional buttons to be added to the toolbar of {@link NavigationBoard}, together with optional spacers in-between.
|
|
57
|
+
*/
|
|
58
|
+
export type NavigationButtonList = (NavigationButton | 'spacer')[];
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Whether the given object is a {@link NavigationButton} or not.
|
|
63
|
+
*/
|
|
64
|
+
export function isNavigationButton(button: unknown): button is NavigationButton {
|
|
65
|
+
if (typeof button !== 'object' || button === null) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
return typeof (button as NavigationButton).iconPath === 'string' &&
|
|
69
|
+
((button as NavigationButton).tooltip === undefined || typeof (button as NavigationButton).tooltip === 'string') &&
|
|
70
|
+
((button as NavigationButton).enabled === undefined || typeof (button as NavigationButton).enabled === 'boolean') &&
|
|
71
|
+
((button as NavigationButton).onClick === undefined || typeof (button as NavigationButton).onClick === 'function');
|
|
72
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* -------------------------------------------------------------------------- *
|
|
3
|
+
* *
|
|
4
|
+
* Kokopu-React - A React-based library of chess-related components. *
|
|
5
|
+
* <https://www.npmjs.com/package/kokopu-react> *
|
|
6
|
+
* Copyright (C) 2021-2024 Yoann Le Montagner <yo35 -at- melix.net> *
|
|
7
|
+
* *
|
|
8
|
+
* Kokopu-React is free software: you can redistribute it and/or *
|
|
9
|
+
* modify it under the terms of the GNU Lesser General Public License *
|
|
10
|
+
* as published by the Free Software Foundation, either version 3 of *
|
|
11
|
+
* the License, or (at your option) any later version. *
|
|
12
|
+
* *
|
|
13
|
+
* Kokopu-React is distributed in the hope that it will be useful, *
|
|
14
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
15
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
16
|
+
* GNU Lesser General Public License for more details. *
|
|
17
|
+
* *
|
|
18
|
+
* You should have received a copy of the GNU Lesser General *
|
|
19
|
+
* Public License along with this program. If not, see *
|
|
20
|
+
* <http://www.gnu.org/licenses/>. *
|
|
21
|
+
* *
|
|
22
|
+
* -------------------------------------------------------------------------- */
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
.kokopu-focusFieldContainer {
|
|
26
|
+
position: fixed;
|
|
27
|
+
bottom: 0px;
|
|
28
|
+
right: 0px;
|
|
29
|
+
width: 0px;
|
|
30
|
+
height: 0px;
|
|
31
|
+
overflow: hidden;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
.kokopu-focusField {
|
|
35
|
+
position: absolute; /* see #1 */
|
|
36
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* -------------------------------------------------------------------------- *
|
|
3
|
+
* *
|
|
4
|
+
* Kokopu-React - A React-based library of chess-related components. *
|
|
5
|
+
* <https://www.npmjs.com/package/kokopu-react> *
|
|
6
|
+
* Copyright (C) 2021-2024 Yoann Le Montagner <yo35 -at- melix.net> *
|
|
7
|
+
* *
|
|
8
|
+
* Kokopu-React is free software: you can redistribute it and/or *
|
|
9
|
+
* modify it under the terms of the GNU Lesser General Public License *
|
|
10
|
+
* as published by the Free Software Foundation, either version 3 of *
|
|
11
|
+
* the License, or (at your option) any later version. *
|
|
12
|
+
* *
|
|
13
|
+
* Kokopu-React is distributed in the hope that it will be useful, *
|
|
14
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
15
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
16
|
+
* GNU Lesser General Public License for more details. *
|
|
17
|
+
* *
|
|
18
|
+
* You should have received a copy of the GNU Lesser General *
|
|
19
|
+
* Public License along with this program. If not, see *
|
|
20
|
+
* <http://www.gnu.org/licenses/>. *
|
|
21
|
+
* *
|
|
22
|
+
* -------------------------------------------------------------------------- */
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
import * as React from 'react';
|
|
26
|
+
|
|
27
|
+
import { Game, Variation } from 'kokopu';
|
|
28
|
+
|
|
29
|
+
import './NavigationField.css';
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
interface NavigationFieldProps {
|
|
33
|
+
onFirstPressed?: () => void;
|
|
34
|
+
onPreviousPressed?: () => void;
|
|
35
|
+
onNextPressed?: () => void;
|
|
36
|
+
onLastPressed?: () => void;
|
|
37
|
+
onExitPressed?: () => void;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Invisible field, to capture keyboard events.
|
|
43
|
+
*/
|
|
44
|
+
export class NavigationField extends React.Component<NavigationFieldProps> {
|
|
45
|
+
|
|
46
|
+
private focusFieldRef: React.RefObject<HTMLAnchorElement> = React.createRef();
|
|
47
|
+
|
|
48
|
+
render() {
|
|
49
|
+
return (
|
|
50
|
+
<div className="kokopu-focusFieldContainer">
|
|
51
|
+
<a className="kokopu-focusField" href="#" ref={this.focusFieldRef} onKeyDown={evt => this.handleKeyDown(evt)}></a>
|
|
52
|
+
</div>
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private handleKeyDown(evt: React.KeyboardEvent<HTMLAnchorElement>) {
|
|
57
|
+
/* eslint-disable no-unused-expressions */
|
|
58
|
+
doHandleKeyDown(evt, 'Home', this.props.onFirstPressed) ||
|
|
59
|
+
doHandleKeyDown(evt, 'ArrowLeft', this.props.onPreviousPressed) ||
|
|
60
|
+
doHandleKeyDown(evt, 'ArrowRight', this.props.onNextPressed) ||
|
|
61
|
+
doHandleKeyDown(evt, 'End', this.props.onLastPressed) ||
|
|
62
|
+
doHandleKeyDown(evt, 'Escape', this.props.onExitPressed);
|
|
63
|
+
/* eslint-enable */
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Set the focus to the component.
|
|
68
|
+
*/
|
|
69
|
+
focus(): void {
|
|
70
|
+
const target = this.focusFieldRef.current;
|
|
71
|
+
// istanbul ignore else
|
|
72
|
+
if (target) {
|
|
73
|
+
target.focus();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
function doHandleKeyDown(evt: React.KeyboardEvent<HTMLAnchorElement>, targetKey: string, targetCallback: (() => void) | undefined) {
|
|
81
|
+
|
|
82
|
+
// Skip if the pressed key does not match the target key.
|
|
83
|
+
if (evt.key !== targetKey) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Otherwise, invoke the callback.
|
|
88
|
+
if (targetCallback) {
|
|
89
|
+
evt.preventDefault();
|
|
90
|
+
targetCallback();
|
|
91
|
+
}
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Return the ID of the main variation in the given chess game.
|
|
98
|
+
* If the given selection corresponds already at the main variation, `undefined` is returned.
|
|
99
|
+
*
|
|
100
|
+
* @param game - Considered chess game.
|
|
101
|
+
* @param selection - ID of the selected move (or `'start'` for the beginning of the main variation).
|
|
102
|
+
* @public
|
|
103
|
+
*/
|
|
104
|
+
export function firstNodeId(game: Game, selection: string): string | undefined {
|
|
105
|
+
if (!game.findById(selection)) {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
return selection === 'start' ? undefined : 'start';
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Return the ID of the node immediately preceding the given selection in the given chess game.
|
|
114
|
+
* If no such node exists, `undefined` is returned.
|
|
115
|
+
*
|
|
116
|
+
* @param game - Considered chess game.
|
|
117
|
+
* @param selection - ID of the selected move (or `'start'` for the beginning of the main variation).
|
|
118
|
+
* @public
|
|
119
|
+
*/
|
|
120
|
+
export function previousNodeId(game: Game, selection: string): string | undefined {
|
|
121
|
+
let currentNode = game.findById(selection);
|
|
122
|
+
if (!currentNode) {
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
if (currentNode instanceof Variation) {
|
|
126
|
+
currentNode = currentNode.parentNode();
|
|
127
|
+
if (!currentNode) {
|
|
128
|
+
return undefined; // This case corresponds to the first variation being selected initially.
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
while (currentNode) {
|
|
132
|
+
const previousNode = currentNode.previous();
|
|
133
|
+
if (previousNode) {
|
|
134
|
+
return previousNode.id();
|
|
135
|
+
}
|
|
136
|
+
currentNode = currentNode.parentVariation().parentNode();
|
|
137
|
+
}
|
|
138
|
+
return 'start';
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Return the ID of the node immediately following the given selection in the given chess game.
|
|
144
|
+
* If no such node exists, `undefined` is returned.
|
|
145
|
+
*
|
|
146
|
+
* @param game - Considered chess game.
|
|
147
|
+
* @param selection - ID of the selected move (or `'start'` for the beginning of the main variation).
|
|
148
|
+
* @public
|
|
149
|
+
*/
|
|
150
|
+
export function nextNodeId(game: Game, selection: string): string | undefined {
|
|
151
|
+
const currentNode = game.findById(selection);
|
|
152
|
+
if (!currentNode) {
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
const nextNode = currentNode instanceof Variation ? currentNode.first() : currentNode.next();
|
|
156
|
+
return nextNode ? nextNode.id() : undefined;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Return the ID of the node at then end of the variation in which lies the given selection in the given chess game.
|
|
162
|
+
* If the selection is already at the end its variation, `undefined` is returned.
|
|
163
|
+
*
|
|
164
|
+
* @param game - Considered chess game.
|
|
165
|
+
* @param selection - ID of the selected move (or `'start'` for the beginning of the main variation).
|
|
166
|
+
* @public
|
|
167
|
+
*/
|
|
168
|
+
export function lastNodeId(game: Game, selection: string): string | undefined {
|
|
169
|
+
let currentNode = game.findById(selection);
|
|
170
|
+
if (!currentNode) {
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
currentNode = currentNode instanceof Variation ? currentNode.first() : currentNode.next();
|
|
174
|
+
if (!currentNode) { // Ensure that the input node is not already the last one.
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
let currentNodeNotNull = currentNode;
|
|
178
|
+
while (true) {
|
|
179
|
+
const nextNode = currentNodeNotNull.next();
|
|
180
|
+
if (!nextNode) {
|
|
181
|
+
return currentNodeNotNull.id();
|
|
182
|
+
}
|
|
183
|
+
currentNodeNotNull = nextNode;
|
|
184
|
+
}
|
|
185
|
+
}
|