kokopu-react 3.0.1 → 3.1.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.
Files changed (250) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/lib/chessboard/BoardProperties.d.ts +91 -0
  3. package/dist/lib/chessboard/BoardProperties.js +43 -0
  4. package/dist/lib/chessboard/BoardProperties.js.map +1 -0
  5. package/dist/lib/chessboard/Chessboard.css +7 -5
  6. package/dist/lib/chessboard/Chessboard.d.ts +36 -58
  7. package/dist/lib/chessboard/Chessboard.js +33 -97
  8. package/dist/lib/chessboard/Chessboard.js.map +1 -1
  9. package/dist/lib/chessboard/ChessboardImpl.d.ts +22 -0
  10. package/dist/lib/chessboard/ChessboardImpl.js +8 -6
  11. package/dist/lib/chessboard/ChessboardImpl.js.map +1 -1
  12. package/dist/lib/chessboard/Draggable.d.ts +22 -0
  13. package/dist/lib/chessboard/Draggable.js +7 -5
  14. package/dist/lib/chessboard/Draggable.js.map +1 -1
  15. package/dist/lib/chessboard/Motion.d.ts +22 -0
  16. package/dist/lib/chessboard/Motion.js +7 -5
  17. package/dist/lib/chessboard/Motion.js.map +1 -1
  18. package/dist/lib/chessboard/colorsets.d.ts +22 -0
  19. package/dist/lib/chessboard/colorsets.js +7 -5
  20. package/dist/lib/chessboard/colorsets.js.map +1 -1
  21. package/dist/lib/chessboard/piecesets.d.ts +22 -0
  22. package/dist/lib/chessboard/piecesets.js +7 -5
  23. package/dist/lib/chessboard/piecesets.js.map +1 -1
  24. package/dist/lib/errorbox/ErrorBox.css +7 -5
  25. package/dist/lib/errorbox/ErrorBox.d.ts +22 -0
  26. package/dist/lib/errorbox/ErrorBox.js +7 -5
  27. package/dist/lib/errorbox/ErrorBox.js.map +1 -1
  28. package/dist/lib/errorbox/parsing.d.ts +57 -0
  29. package/dist/lib/errorbox/parsing.js +140 -0
  30. package/dist/lib/errorbox/parsing.js.map +1 -0
  31. package/dist/lib/exception.d.ts +22 -0
  32. package/dist/lib/exception.js +7 -5
  33. package/dist/lib/exception.js.map +1 -1
  34. package/dist/lib/i18n.d.ts +27 -0
  35. package/dist/lib/i18n.js +13 -5
  36. package/dist/lib/i18n.js.map +1 -1
  37. package/dist/lib/icons/AnnotationSymbolShape.css +7 -5
  38. package/dist/lib/icons/AnnotationSymbolShape.d.ts +22 -0
  39. package/dist/lib/icons/AnnotationSymbolShape.js +7 -5
  40. package/dist/lib/icons/AnnotationSymbolShape.js.map +1 -1
  41. package/dist/lib/icons/ArrowMarkerIcon.d.ts +22 -0
  42. package/dist/lib/icons/ArrowMarkerIcon.js +7 -5
  43. package/dist/lib/icons/ArrowMarkerIcon.js.map +1 -1
  44. package/dist/lib/icons/ArrowTip.d.ts +22 -0
  45. package/dist/lib/icons/ArrowTip.js +7 -5
  46. package/dist/lib/icons/ArrowTip.js.map +1 -1
  47. package/dist/lib/icons/ChessPieceIcon.d.ts +22 -0
  48. package/dist/lib/icons/ChessPieceIcon.js +7 -5
  49. package/dist/lib/icons/ChessPieceIcon.js.map +1 -1
  50. package/dist/lib/icons/SquareMarkerIcon.d.ts +22 -0
  51. package/dist/lib/icons/SquareMarkerIcon.js +7 -5
  52. package/dist/lib/icons/SquareMarkerIcon.js.map +1 -1
  53. package/dist/lib/icons/TextMarkerIcon.d.ts +22 -0
  54. package/dist/lib/icons/TextMarkerIcon.js +7 -5
  55. package/dist/lib/icons/TextMarkerIcon.js.map +1 -1
  56. package/dist/lib/icons/arrow.css +7 -5
  57. package/dist/lib/index.d.ts +26 -1
  58. package/dist/lib/index.js +9 -5
  59. package/dist/lib/index.js.map +1 -1
  60. package/dist/lib/movetext/Movetext.css +7 -18
  61. package/dist/lib/movetext/Movetext.d.ts +31 -43
  62. package/dist/lib/movetext/Movetext.js +22 -72
  63. package/dist/lib/movetext/Movetext.js.map +1 -1
  64. package/dist/lib/movetext/MovetextImpl.d.ts +29 -28
  65. package/dist/lib/movetext/MovetextImpl.js +22 -99
  66. package/dist/lib/movetext/MovetextImpl.js.map +1 -1
  67. package/dist/lib/movetext/fonts.css +7 -5
  68. package/dist/lib/movetext/htmlFilter.d.ts +22 -0
  69. package/dist/lib/movetext/htmlFilter.js +21 -19
  70. package/dist/lib/movetext/htmlFilter.js.map +1 -1
  71. package/dist/lib/movetext/moveFormatter.d.ts +22 -0
  72. package/dist/lib/movetext/moveFormatter.js +7 -5
  73. package/dist/lib/movetext/moveFormatter.js.map +1 -1
  74. package/dist/lib/navigationboard/NavigationBoard.d.ts +118 -0
  75. package/dist/lib/navigationboard/NavigationBoard.js +112 -0
  76. package/dist/lib/navigationboard/NavigationBoard.js.map +1 -0
  77. package/dist/lib/navigationboard/NavigationField.css +36 -0
  78. package/dist/lib/navigationboard/NavigationField.d.ts +81 -0
  79. package/dist/lib/navigationboard/NavigationField.js +154 -0
  80. package/dist/lib/navigationboard/NavigationField.js.map +1 -0
  81. package/dist/lib/navigationboard/NavigationToolbar.css +51 -0
  82. package/dist/lib/navigationboard/NavigationToolbar.d.ts +41 -0
  83. package/dist/lib/navigationboard/NavigationToolbar.js +124 -0
  84. package/dist/lib/navigationboard/NavigationToolbar.js.map +1 -0
  85. package/dist/lib/sanitization.d.ts +22 -0
  86. package/dist/lib/sanitization.js +7 -5
  87. package/dist/lib/sanitization.js.map +1 -1
  88. package/dist/lib/types.d.ts +22 -0
  89. package/dist/lib/types.js +7 -5
  90. package/dist/lib/types.js.map +1 -1
  91. package/dist/lib/util.d.ts +22 -0
  92. package/dist/lib/util.js +7 -5
  93. package/dist/lib/util.js.map +1 -1
  94. package/doc_src/demo/PageChessboardBase.tsx +7 -5
  95. package/doc_src/demo/PageChessboardInteraction.tsx +32 -22
  96. package/doc_src/demo/PageChessboardMove.tsx +24 -5
  97. package/doc_src/demo/PageChessboardSmallScreens.tsx +7 -5
  98. package/doc_src/demo/PageMovetextBase.tsx +7 -5
  99. package/doc_src/demo/PageMovetextInteraction.tsx +7 -5
  100. package/doc_src/demo/PageNavigationBoardBase.tsx +194 -0
  101. package/doc_src/demo/PageNavigationBoardUncontrolledVsControlled.tsx +114 -0
  102. package/doc_src/demo/decs.d.ts +7 -5
  103. package/doc_src/demo/demo.css +7 -5
  104. package/doc_src/demo/game-3.pgn +10 -0
  105. package/doc_src/demo/util.ts +7 -5
  106. package/doc_src/examples/Movetext.md +1 -1
  107. package/doc_src/examples/NavigationBoard.md +15 -0
  108. package/doc_src/theming/LogoRenderer.js +7 -5
  109. package/doc_src/theming/theming.css +7 -5
  110. package/package.json +27 -26
  111. package/scripts/doc.styleguide.config.js +19 -7
  112. package/scripts/docker-compose.yml +3 -3
  113. package/scripts/test-graphic.webpack.config.js +7 -5
  114. package/scripts/test-headless.webpack.config.js +7 -5
  115. package/src/chessboard/BoardProperties.ts +131 -0
  116. package/src/chessboard/Chessboard.css +7 -5
  117. package/src/chessboard/Chessboard.tsx +48 -175
  118. package/src/chessboard/ChessboardImpl.tsx +7 -5
  119. package/src/chessboard/Draggable.tsx +7 -5
  120. package/src/chessboard/Motion.tsx +7 -5
  121. package/src/chessboard/colorsets.ts +7 -5
  122. package/src/chessboard/piecesets.ts +7 -5
  123. package/src/decs.d.ts +7 -5
  124. package/src/errorbox/ErrorBox.css +7 -5
  125. package/src/errorbox/ErrorBox.tsx +7 -5
  126. package/src/errorbox/parsing.tsx +158 -0
  127. package/src/exception.ts +7 -5
  128. package/src/i18n.ts +14 -5
  129. package/src/icons/AnnotationSymbolShape.css +7 -5
  130. package/src/icons/AnnotationSymbolShape.tsx +7 -5
  131. package/src/icons/ArrowMarkerIcon.tsx +7 -5
  132. package/src/icons/ArrowTip.tsx +7 -5
  133. package/src/icons/ChessPieceIcon.tsx +7 -5
  134. package/src/icons/SquareMarkerIcon.tsx +7 -5
  135. package/src/icons/TextMarkerIcon.tsx +7 -5
  136. package/src/icons/arrow.css +7 -5
  137. package/src/index.ts +12 -6
  138. package/src/movetext/Movetext.css +7 -18
  139. package/src/movetext/Movetext.tsx +27 -90
  140. package/src/movetext/MovetextImpl.tsx +35 -120
  141. package/src/movetext/fonts.css +7 -5
  142. package/src/movetext/htmlFilter.tsx +7 -5
  143. package/src/movetext/moveFormatter.tsx +7 -5
  144. package/src/navigationboard/NavigationBoard.tsx +231 -0
  145. package/src/navigationboard/NavigationField.css +36 -0
  146. package/src/navigationboard/NavigationField.tsx +185 -0
  147. package/src/navigationboard/NavigationToolbar.css +51 -0
  148. package/src/navigationboard/NavigationToolbar.tsx +174 -0
  149. package/src/sanitization.ts +7 -5
  150. package/src/types.ts +7 -5
  151. package/src/util.ts +7 -5
  152. package/test/01_types.js +7 -5
  153. package/test/02_chessboard_util.js +7 -5
  154. package/test/03_error_box_graphic.js +7 -5
  155. package/test/04_icons_graphic.js +7 -5
  156. package/test/05_chessboard_graphic.js +9 -5
  157. package/test/06_chessboard_animation.js +7 -5
  158. package/test/07_chessboard_interaction.js +7 -5
  159. package/test/08_chessboard_play_moves.js +7 -5
  160. package/test/09_chessboard_small_screens.js +28 -5
  161. package/test/10_movetext_graphic.js +12 -5
  162. package/test/11_movetext_interaction.js +19 -13
  163. package/test/12_navigation_board.js +168 -0
  164. package/test/common/graphic.js +7 -5
  165. package/test/graphic_references/05_chessboard_graphic/move/illegal_null-move.png +0 -0
  166. package/test/graphic_references/05_chessboard_graphic/move/null-move.png +0 -0
  167. package/test/graphic_references/09_chessboard_small_screens/top_bottom_components/default_bottom_component.png +0 -0
  168. package/test/graphic_references/09_chessboard_small_screens/top_bottom_components/default_top_component.png +0 -0
  169. package/test/graphic_references/09_chessboard_small_screens/top_bottom_components/reduced_bottom_component.png +0 -0
  170. package/test/graphic_references/09_chessboard_small_screens/top_bottom_components/reduced_top_component.png +0 -0
  171. package/test/graphic_references/10_movetext_graphic/format_move/format_text.png +0 -0
  172. package/test/graphic_references/10_movetext_graphic/options/wrong_piece_symbol_value.png +0 -0
  173. package/test/graphic_references/11_movetext_interaction/select_moves/click_on_unselected_without_callback.png +0 -0
  174. package/test/graphic_references/12_navigation_board/controlled/default.png +0 -0
  175. package/test/graphic_references/12_navigation_board/controlled/forbidden_navigation_0.png +0 -0
  176. package/test/graphic_references/12_navigation_board/controlled/forbidden_navigation_1.png +0 -0
  177. package/test/graphic_references/12_navigation_board/controlled/invalid_node_id.png +0 -0
  178. package/test/graphic_references/12_navigation_board/controlled/no_callback_defined_0.png +0 -0
  179. package/test/graphic_references/12_navigation_board/controlled/no_callback_defined_1.png +0 -0
  180. package/test/graphic_references/12_navigation_board/controlled/overriding_initial_node_id.png +0 -0
  181. package/test/graphic_references/12_navigation_board/controlled/regular_navigation_0.png +0 -0
  182. package/test/graphic_references/12_navigation_board/controlled/regular_navigation_1.png +0 -0
  183. package/test/graphic_references/12_navigation_board/controlled/regular_navigation_2.png +0 -0
  184. package/test/graphic_references/12_navigation_board/controlled/regular_navigation_3.png +0 -0
  185. package/test/graphic_references/12_navigation_board/error/pgn_parsing_error.png +0 -0
  186. package/test/graphic_references/12_navigation_board/error/wrong_game_type.png +0 -0
  187. package/test/graphic_references/12_navigation_board/theme/custom_1.png +0 -0
  188. package/test/graphic_references/12_navigation_board/theme/custom_2.png +0 -0
  189. package/test/graphic_references/12_navigation_board/theme/large.png +0 -0
  190. package/test/graphic_references/12_navigation_board/theme/small.png +0 -0
  191. package/test/graphic_references/12_navigation_board/uncontrolled/forbidden_navigation_at_beginning_0.png +0 -0
  192. package/test/graphic_references/12_navigation_board/uncontrolled/forbidden_navigation_at_beginning_1.png +0 -0
  193. package/test/graphic_references/12_navigation_board/uncontrolled/forbidden_navigation_at_beginning_2.png +0 -0
  194. package/test/graphic_references/12_navigation_board/uncontrolled/forbidden_navigation_at_end_0.png +0 -0
  195. package/test/graphic_references/12_navigation_board/uncontrolled/forbidden_navigation_at_end_1.png +0 -0
  196. package/test/graphic_references/12_navigation_board/uncontrolled/forbidden_navigation_at_end_2.png +0 -0
  197. package/test/graphic_references/12_navigation_board/uncontrolled/initial_state_at_end.png +0 -0
  198. package/test/graphic_references/12_navigation_board/uncontrolled/initial_state_at_specific_id.png +0 -0
  199. package/test/graphic_references/12_navigation_board/uncontrolled/initial_state_default.png +0 -0
  200. package/test/graphic_references/12_navigation_board/uncontrolled/initial_state_default_with_game_index.png +0 -0
  201. package/test/graphic_references/12_navigation_board/uncontrolled/initial_state_on_null-move.png +0 -0
  202. package/test/graphic_references/12_navigation_board/uncontrolled/navigation_with_keys_0.png +0 -0
  203. package/test/graphic_references/12_navigation_board/uncontrolled/navigation_with_keys_1.png +0 -0
  204. package/test/graphic_references/12_navigation_board/uncontrolled/navigation_with_keys_2.png +0 -0
  205. package/test/graphic_references/12_navigation_board/uncontrolled/navigation_with_keys_3.png +0 -0
  206. package/test/graphic_references/12_navigation_board/uncontrolled/navigation_with_keys_4.png +0 -0
  207. package/test/graphic_references/12_navigation_board/uncontrolled/regular_flipping_0.png +0 -0
  208. package/test/graphic_references/12_navigation_board/uncontrolled/regular_flipping_1.png +0 -0
  209. package/test/graphic_references/12_navigation_board/uncontrolled/regular_navigation_0.png +0 -0
  210. package/test/graphic_references/12_navigation_board/uncontrolled/regular_navigation_1.png +0 -0
  211. package/test/graphic_references/12_navigation_board/uncontrolled/regular_navigation_2.png +0 -0
  212. package/test/graphic_references/12_navigation_board/uncontrolled/regular_navigation_3.png +0 -0
  213. package/test/graphic_references/12_navigation_board/uncontrolled/regular_navigation_4.png +0 -0
  214. package/test/graphic_references/12_navigation_board/uncontrolled/regular_navigation_5.png +0 -0
  215. package/test/graphic_test_app/03_error_box_graphic/base.tsx +7 -5
  216. package/test/graphic_test_app/03_error_box_graphic/localization.tsx +7 -5
  217. package/test/graphic_test_app/04_icons_graphic/chess_piece.tsx +7 -5
  218. package/test/graphic_test_app/04_icons_graphic/marker.tsx +7 -5
  219. package/test/graphic_test_app/05_chessboard_graphic/annotations.tsx +7 -5
  220. package/test/graphic_test_app/05_chessboard_graphic/base.tsx +7 -5
  221. package/test/graphic_test_app/05_chessboard_graphic/chess_variants.tsx +7 -5
  222. package/test/graphic_test_app/05_chessboard_graphic/flipped.tsx +7 -5
  223. package/test/graphic_test_app/05_chessboard_graphic/move.tsx +9 -5
  224. package/test/graphic_test_app/05_chessboard_graphic/theme.tsx +7 -5
  225. package/test/graphic_test_app/06_chessboard_animation/base_1.tsx +7 -5
  226. package/test/graphic_test_app/06_chessboard_animation/base_2.tsx +7 -5
  227. package/test/graphic_test_app/06_chessboard_animation/base_3.tsx +7 -5
  228. package/test/graphic_test_app/06_chessboard_animation/on_redraw.tsx +7 -5
  229. package/test/graphic_test_app/07_chessboard_interaction/click_squares.tsx +7 -5
  230. package/test/graphic_test_app/07_chessboard_interaction/edit_arrows.tsx +7 -5
  231. package/test/graphic_test_app/07_chessboard_interaction/error.tsx +7 -5
  232. package/test/graphic_test_app/07_chessboard_interaction/move_pieces.tsx +7 -5
  233. package/test/graphic_test_app/08_chessboard_play_moves/base.tsx +7 -5
  234. package/test/graphic_test_app/08_chessboard_play_moves/promotions.tsx +7 -5
  235. package/test/graphic_test_app/09_chessboard_small_screens/base.tsx +7 -5
  236. package/test/graphic_test_app/09_chessboard_small_screens/top_bottom_components.tsx +48 -0
  237. package/test/graphic_test_app/10_movetext_graphic/base.tsx +7 -5
  238. package/test/graphic_test_app/10_movetext_graphic/error.tsx +7 -5
  239. package/test/graphic_test_app/10_movetext_graphic/format_move.tsx +31 -0
  240. package/test/graphic_test_app/10_movetext_graphic/html.tsx +7 -5
  241. package/test/graphic_test_app/10_movetext_graphic/options.tsx +8 -5
  242. package/test/graphic_test_app/11_movetext_interaction/error.tsx +7 -5
  243. package/test/graphic_test_app/11_movetext_interaction/select_moves.tsx +8 -5
  244. package/test/graphic_test_app/12_navigation_board/controlled.tsx +43 -0
  245. package/test/graphic_test_app/12_navigation_board/error.tsx +34 -0
  246. package/test/graphic_test_app/12_navigation_board/theme.tsx +38 -0
  247. package/test/graphic_test_app/12_navigation_board/uncontrolled.tsx +56 -0
  248. package/test/graphic_test_app/common/decs.d.ts +7 -5
  249. package/test/graphic_test_app/common/test_app.css +7 -5
  250. package/test/graphic_test_app/common/test_app.tsx +7 -5
@@ -0,0 +1,231 @@
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 { sanitizeBoolean, sanitizeOptional, sanitizeString } from '../sanitization';
30
+
31
+ import { DynamicBoardGraphicProps, defaultDynamicBoardProps } from '../chessboard/BoardProperties';
32
+ import { Chessboard } from '../chessboard/Chessboard';
33
+ import { parseGame } from '../errorbox/parsing';
34
+ import { NavigationField, firstNodeId, previousNodeId, nextNodeId, lastNodeId } from '../navigationboard/NavigationField';
35
+ import { NavigationToolbar } from './NavigationToolbar';
36
+
37
+
38
+ export interface NavigationBoardProps extends DynamicBoardGraphicProps {
39
+
40
+ /**
41
+ * Displayed game. Can be a [kokopu.Game](https://kokopu.yo35.org/docs/current/classes/Game.html) object,
42
+ * a [kokopu.Database](https://kokopu.yo35.org/docs/current/classes/Database.html) object,
43
+ * or a [PGN string](https://en.wikipedia.org/wiki/Portable_Game_Notation).
44
+ */
45
+ game: Game | Database | string;
46
+
47
+ /**
48
+ * Index of the game to display (only if attribute `game` is a [kokopu.Database](https://kokopu.yo35.org/docs/current/classes/Database.html)
49
+ * 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.
50
+ * If omitted, the first game of the database/PGN is displayed.
51
+ */
52
+ gameIndex: number;
53
+
54
+ /**
55
+ * ID of the move initially selected (or `'start'`/`'end'` for the beginning/end of the main variation) when the component is uncontrolled.
56
+ * Use [kokopu.Node#id](https://kokopu.yo35.org/docs/current/classes/Node.html#id) to get the ID of a game move.
57
+ * Ignored if the `nodeId` attribute is provided.
58
+ */
59
+ initialNodeId: string;
60
+
61
+ /**
62
+ * ID of the selected move (or `'start'`/`'end'` for the beginning/end of the main variation).
63
+ * Use [kokopu.Node#id](https://kokopu.yo35.org/docs/current/classes/Node.html#id) to get the ID of a game move.
64
+ * If provided (i.e. if the component is controlled), the attribute `onNodeIdChanged` must be provided as well.
65
+ */
66
+ nodeId?: string;
67
+
68
+ /**
69
+ * Callback invoked in controlled-component mode, when the user changes the selected move.
70
+ *
71
+ * @param nodeId - ID of the selected move (as returned by [kokopu.Node#id](https://kokopu.yo35.org/docs/current/classes/Node.html#id)),
72
+ * or `'start'` for the beginning of the main variation.
73
+ */
74
+ onNodeIdChanged?: (nodeId: string) => void;
75
+
76
+ /**
77
+ * Whether the board is initially flipped (i.e. seen from Black's point of view) or not, when the flip state is uncontrolled.
78
+ * Ignored if the `flipped` attribute is provided.
79
+ */
80
+ initialFlipped: boolean;
81
+
82
+ /**
83
+ * Whether the board is flipped (i.e. seen from Black's point of view) or not.
84
+ * If provided (i.e. if the flip state is controlled), the attribute `onFlippedChanged` must be provided as well.
85
+ */
86
+ flipped?: boolean;
87
+
88
+ /**
89
+ * Callback invoked in controlled-flip-state mode, when the user flips the board.
90
+ *
91
+ * @param flipped - New flip state.
92
+ */
93
+ onFlippedChanged?: (flipped: boolean) => void;
94
+
95
+ /**
96
+ * Whether the flip button is visible or not in the toolbar.
97
+ */
98
+ flipButtonVisible: boolean;
99
+ }
100
+
101
+
102
+ interface NavigationBoardState {
103
+ nodeIdAsUncontrolled: string;
104
+ flippedAsUncontrolled: boolean;
105
+ }
106
+
107
+
108
+ /**
109
+ * Component displaying the positions occurring in a chess game, with navigation buttons to browse these positions.
110
+ */
111
+ export class NavigationBoard extends React.Component<NavigationBoardProps, NavigationBoardState> {
112
+
113
+ static defaultProps = {
114
+ ...defaultDynamicBoardProps(),
115
+ game: new Game(),
116
+ gameIndex: 0,
117
+ initialNodeId: 'start',
118
+ initialFlipped: false,
119
+ flipButtonVisible: true,
120
+ };
121
+
122
+ private navigationFieldRef: React.RefObject<NavigationField> = React.createRef();
123
+
124
+ constructor(props: NavigationBoardProps) {
125
+ super(props);
126
+ this.state = {
127
+ nodeIdAsUncontrolled: sanitizeString(props.initialNodeId),
128
+ flippedAsUncontrolled: sanitizeBoolean(props.initialFlipped),
129
+ };
130
+ }
131
+
132
+ render() {
133
+
134
+ // Validate the game and game-index attributes.
135
+ const info = parseGame(this.props.game, this.props.gameIndex, 'NavigationBoard');
136
+ if (info.error) {
137
+ return info.errorBox;
138
+ }
139
+
140
+ // Fetch the current node/variation.
141
+ const currentNodeId = sanitizeOptional(this.props.nodeId, sanitizeString) ?? this.state.nodeIdAsUncontrolled;
142
+ const currentNode = info.game.findById(currentNodeId) ?? info.game.mainVariation();
143
+
144
+ // Flip state.
145
+ const flipped = sanitizeOptional(this.props.flipped, sanitizeBoolean) ?? this.state.flippedAsUncontrolled;
146
+
147
+ return (
148
+ <div className="kokopu-navigationBoard">
149
+ {this.renderBoard(info.game, currentNode, flipped)}
150
+ {this.renderNavigationField(info.game, currentNode.id())}
151
+ </div>
152
+ );
153
+ }
154
+
155
+ private renderBoard(game: Game, node: GameNode | Variation, flipped: boolean) {
156
+ const position = node instanceof GameNode ? node.positionBefore() : node.initialPosition();
157
+ const move = node instanceof GameNode ? node.notation() : undefined;
158
+ return <Chessboard
159
+ position={position} move={move} flipped={flipped}
160
+ squareMarkers={node.tag('csl')} arrowMarkers={node.tag('cal')} textMarkers={node.tag('ctl')}
161
+ squareSize={this.props.squareSize}
162
+ coordinateVisible={this.props.coordinateVisible}
163
+ turnVisible={this.props.turnVisible}
164
+ colorset={this.props.colorset}
165
+ pieceset={this.props.pieceset}
166
+ smallScreenLimits={this.props.smallScreenLimits}
167
+ moveArrowVisible={this.props.moveArrowVisible}
168
+ moveArrowColor={this.props.moveArrowColor}
169
+ animated={this.props.animated}
170
+ bottomComponent={({ squareSize }) => this.renderToolbar(game, node.id(), squareSize)}
171
+ />;
172
+ }
173
+
174
+ private renderNavigationField(game: Game, currentNodeId: string) {
175
+ return <NavigationField ref={this.navigationFieldRef}
176
+ onFirstPressed={() => this.handleNavigationButtonClicked(firstNodeId(game, currentNodeId))}
177
+ onPreviousPressed={() => this.handleNavigationButtonClicked(previousNodeId(game, currentNodeId))}
178
+ onNextPressed={() => this.handleNavigationButtonClicked(nextNodeId(game, currentNodeId))}
179
+ onLastPressed={() => this.handleNavigationButtonClicked(lastNodeId(game, currentNodeId))}
180
+ />;
181
+ }
182
+
183
+ private renderToolbar(game: Game, currentNodeId: string, squareSize: number) {
184
+ const flipButtonVisible = sanitizeBoolean(this.props.flipButtonVisible);
185
+ return <NavigationToolbar flipButtonVisible={flipButtonVisible} squareSize={squareSize}
186
+ onFirstClicked={() => this.handleNavigationButtonClicked(firstNodeId(game, currentNodeId))}
187
+ onPreviousClicked={() => this.handleNavigationButtonClicked(previousNodeId(game, currentNodeId))}
188
+ onNextClicked={() => this.handleNavigationButtonClicked(nextNodeId(game, currentNodeId))}
189
+ onLastClicked={() => this.handleNavigationButtonClicked(lastNodeId(game, currentNodeId))}
190
+ onFlipClicked={() => this.handleFlipButtonClicked()}
191
+ />;
192
+ }
193
+
194
+ private handleNavigationButtonClicked(targetNodeId: string | undefined) {
195
+ this.focus();
196
+ if (targetNodeId === undefined) {
197
+ return;
198
+ }
199
+
200
+ if (this.props.nodeId === undefined) { // uncontrolled-component behavior
201
+ this.setState({ nodeIdAsUncontrolled: targetNodeId });
202
+ }
203
+ else if (this.props.onNodeIdChanged) { // controlled-component behavior
204
+ this.props.onNodeIdChanged(targetNodeId);
205
+ }
206
+ }
207
+
208
+ private handleFlipButtonClicked() {
209
+ this.focus();
210
+ if (this.props.flipped === undefined) { // uncontrolled flip state
211
+ this.setState({ flippedAsUncontrolled: !this.state.flippedAsUncontrolled });
212
+ }
213
+ else if (this.props.onFlippedChanged) { // controlled flip state
214
+ this.props.onFlippedChanged(!this.props.flipped);
215
+ }
216
+ }
217
+
218
+ /**
219
+ * Set the focus to the current component.
220
+ *
221
+ * @public
222
+ */
223
+ focus(): void {
224
+ const target = this.navigationFieldRef.current;
225
+ // istanbul ignore else
226
+ if (target) {
227
+ target.focus();
228
+ }
229
+ }
230
+
231
+ }
@@ -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
+ }
@@ -0,0 +1,51 @@
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-navigationBoard {
26
+ display: flex;
27
+ flex-direction: column;
28
+ align-items: center;
29
+ }
30
+
31
+ .kokopu-navigationToolbar {
32
+ line-height: 0px;
33
+ }
34
+
35
+ .kokopu-navigationButton {
36
+ display: inline-block;
37
+ cursor: pointer;
38
+ }
39
+
40
+ .kokopu-navigationButton:hover {
41
+ opacity: 0.6;
42
+ }
43
+
44
+ * + .kokopu-navigationButton {
45
+ margin-left: 2px;
46
+ }
47
+
48
+ .kokopu-navigationSpacer {
49
+ display: inline-block;
50
+ width: 10px;
51
+ }
@@ -0,0 +1,174 @@
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
+ import * as React from 'react';
27
+
28
+ import { i18n } from '../i18n';
29
+
30
+ import './NavigationToolbar.css';
31
+
32
+ const THICKNESS = 2;
33
+ const CHEVRON_SIZE = 7;
34
+ const ARROW_SIZE = 5;
35
+ const ARROW_LENGTH = 16;
36
+
37
+
38
+ interface NavigationToolbarProps {
39
+ flipButtonVisible: boolean;
40
+ squareSize: number;
41
+ onFirstClicked?: () => void;
42
+ onPreviousClicked?: () => void;
43
+ onNextClicked?: () => void;
44
+ onLastClicked?: () => void;
45
+ onFlipClicked?: () => void;
46
+ }
47
+
48
+
49
+ /**
50
+ * Toolbar below the navigation board.
51
+ */
52
+ export class NavigationToolbar extends React.Component<NavigationToolbarProps> {
53
+
54
+ render() {
55
+ const buttonSize = computeButtonSize(this.props.squareSize);
56
+ const marginTop = Math.round(buttonSize / 4);
57
+ return (
58
+ <div className="kokopu-navigationToolbar" style={{ marginTop: marginTop }}>
59
+ <NavigationButton size={buttonSize} tooltip={i18n.TOOLTIP_GO_FIRST} path={chevronPath(14, 16, -1) + ' ' + chevronPath(10, 16, -1)}
60
+ onClick={this.props.onFirstClicked} />
61
+ <NavigationButton size={buttonSize} tooltip={i18n.TOOLTIP_GO_PREVIOUS} path={chevronPath(12, 16, -1)}
62
+ onClick={this.props.onPreviousClicked} />
63
+ <NavigationButton size={buttonSize} tooltip={i18n.TOOLTIP_GO_NEXT} path={chevronPath(20, 16, 1)}
64
+ onClick={this.props.onNextClicked} />
65
+ <NavigationButton size={buttonSize} tooltip={i18n.TOOLTIP_GO_LAST} path={chevronPath(18, 16, 1) + ' ' + chevronPath(22, 16, 1)}
66
+ onClick={this.props.onLastClicked} />
67
+ {this.renderFlipButton(buttonSize)}
68
+ </div>
69
+ );
70
+ }
71
+
72
+ private renderFlipButton(buttonSize: number) {
73
+ if (!this.props.flipButtonVisible) {
74
+ return undefined;
75
+ }
76
+ return (<>
77
+ <div className="kokopu-navigationSpacer" />
78
+ <NavigationButton size={buttonSize} tooltip={i18n.TOOLTIP_FLIP} path={arrowPath(11, 25, 1) + ' ' + arrowPath(21, 7, -1)}
79
+ onClick={this.props.onFlipClicked} />
80
+ </>);
81
+ }
82
+
83
+ }
84
+
85
+
86
+ interface NavigationButtonProps {
87
+ size: number;
88
+ tooltip: string;
89
+ path: string;
90
+ onClick?: () => void;
91
+ }
92
+
93
+
94
+ /**
95
+ * Button for the navigation toolbar.
96
+ */
97
+ function NavigationButton({ size, tooltip, path, onClick }: NavigationButtonProps) {
98
+ return (
99
+ <div className="kokopu-navigationButton" title={tooltip} onClick={onClick}>
100
+ <svg viewBox="0 0 32 32" width={size} height={size}>
101
+ <path d={`M 16 0 A 16 16 0 0 0 16 32 A 16 16 0 0 0 16 0 Z ${path}`} fill="currentcolor" />
102
+ </svg>
103
+ </div>
104
+ );
105
+ }
106
+
107
+
108
+ /**
109
+ * * C
110
+ * / \
111
+ * B * \
112
+ * \ \
113
+ * \ \
114
+ * A * * D
115
+ * / /
116
+ * / /
117
+ * F * /
118
+ * \ /
119
+ * * E
120
+ *
121
+ * (x,y) = middle of [AD]
122
+ */
123
+ function chevronPath(x: number, y: number, direction: 1 | -1) {
124
+ const xA = x - direction * THICKNESS * Math.sqrt(2) / 2;
125
+ const xB = x - direction * (CHEVRON_SIZE + THICKNESS / Math.sqrt(2) / 2);
126
+ const xC = x - direction * (CHEVRON_SIZE - THICKNESS / Math.sqrt(2) / 2);
127
+ const xD = x + direction * THICKNESS * Math.sqrt(2) / 2;
128
+ const yB = y - direction * (CHEVRON_SIZE - THICKNESS / Math.sqrt(2) / 2);
129
+ const yC = y - direction * (CHEVRON_SIZE + THICKNESS / Math.sqrt(2) / 2);
130
+ const yE = y + direction * (CHEVRON_SIZE + THICKNESS / Math.sqrt(2) / 2);
131
+ const yF = y + direction * (CHEVRON_SIZE - THICKNESS / Math.sqrt(2) / 2);
132
+ return `M ${xA} ${y} L ${xB} ${yB} L ${xC} ${yC} L ${xD} ${y} L ${xC} ${yE} L ${xB} ${yF} Z`;
133
+ }
134
+
135
+
136
+ /**
137
+ * D +---+ E
138
+ * | |
139
+ * | |
140
+ * | |
141
+ * B * | | * G
142
+ * / \ | | / \
143
+ * A * \| |/ * H
144
+ * \ * * /
145
+ * \ C F /
146
+ * \ O /
147
+ * \ * /
148
+ * \ /
149
+ * \ /
150
+ * *
151
+ * (x,y)
152
+ */
153
+ function arrowPath(x: number, y: number, direction: 1 | -1) {
154
+ const xA = x - direction * (ARROW_SIZE + THICKNESS / Math.sqrt(2) / 2);
155
+ const xB = x - direction * (ARROW_SIZE - THICKNESS / Math.sqrt(2) / 2);
156
+ const xC = x - direction * THICKNESS / 2;
157
+ const xF = x + direction * THICKNESS / 2;
158
+ const xG = x + direction * (ARROW_SIZE - THICKNESS / Math.sqrt(2) / 2);
159
+ const xH = x + direction * (ARROW_SIZE + THICKNESS / Math.sqrt(2) / 2);
160
+ const y0 = y - direction * THICKNESS * Math.sqrt(2) / 2;
161
+ const yA = y0 - direction * (ARROW_SIZE - THICKNESS / Math.sqrt(2) / 2);
162
+ const yB = y0 - direction * (ARROW_SIZE + THICKNESS / Math.sqrt(2) / 2);
163
+ const yC = yB + (xC - xB);
164
+ const yD = y - direction * ARROW_LENGTH;
165
+ return `M ${x} ${y} L ${xA} ${yA} L ${xB} ${yB} L ${xC} ${yC} V ${yD} H ${xF} V ${yC} L ${xG} ${yB} L ${xH} ${yA} Z`;
166
+ }
167
+
168
+
169
+ /**
170
+ * Return the height/width of the buttons assuming the given square size.
171
+ */
172
+ function computeButtonSize(squareSize: number) {
173
+ return Math.round((squareSize * 2 + 116) / 7);
174
+ }