@mirasen/chessboard 0.1.1 → 1.0.0-rc.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 (125) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +224 -47
  3. package/dist/animation/planner.d.ts +11 -6
  4. package/dist/animation/planner.js +164 -45
  5. package/dist/animation/render/fade.d.ts +2 -2
  6. package/dist/animation/render/fade.js +3 -3
  7. package/dist/animation/render/move.d.ts +3 -3
  8. package/dist/animation/render/move.js +5 -5
  9. package/dist/animation/render/plan.d.ts +3 -3
  10. package/dist/animation/render/static.d.ts +2 -2
  11. package/dist/animation/render/static.js +3 -3
  12. package/dist/animation/types.d.ts +14 -15
  13. package/dist/animation/types.js +1 -6
  14. package/dist/extensions/factory/main.js +23 -2
  15. package/dist/extensions/factory/runtime.d.ts +2 -1
  16. package/dist/extensions/factory/runtime.js +48 -4
  17. package/dist/extensions/first-party/active-target/factory.js +5 -5
  18. package/dist/extensions/first-party/auto-promote/factory.d.ts +2 -0
  19. package/dist/extensions/first-party/auto-promote/factory.js +51 -0
  20. package/dist/extensions/first-party/auto-promote/types.d.ts +14 -0
  21. package/dist/extensions/first-party/auto-promote/types.js +2 -0
  22. package/dist/extensions/first-party/board-events/factory.js +1 -16
  23. package/dist/extensions/first-party/board-events/types.d.ts +1 -2
  24. package/dist/extensions/first-party/last-move/factory.js +6 -6
  25. package/dist/extensions/first-party/legal-moves/factory.js +1 -1
  26. package/dist/extensions/first-party/main-renderer/animation/render.js +1 -2
  27. package/dist/extensions/first-party/main-renderer/animation/update.js +9 -23
  28. package/dist/extensions/first-party/main-renderer/board/render.js +3 -3
  29. package/dist/extensions/first-party/main-renderer/board/update.js +5 -3
  30. package/dist/extensions/first-party/main-renderer/coordinates/render.js +4 -4
  31. package/dist/extensions/first-party/main-renderer/drag/render.js +1 -1
  32. package/dist/extensions/first-party/main-renderer/normalize.d.ts +2 -3
  33. package/dist/extensions/first-party/main-renderer/normalize.js +1 -1
  34. package/dist/extensions/first-party/main-renderer/pieces/render.js +5 -5
  35. package/dist/extensions/first-party/main-renderer/pieces/suppress.js +7 -2
  36. package/dist/extensions/first-party/main-renderer/types/extension.d.ts +1 -1
  37. package/dist/extensions/first-party/main-renderer/types/extension.js +1 -1
  38. package/dist/extensions/first-party/promotion/factory.d.ts +3 -0
  39. package/dist/extensions/first-party/promotion/factory.js +219 -0
  40. package/dist/extensions/first-party/promotion/normalize.d.ts +3 -0
  41. package/dist/extensions/first-party/promotion/normalize.js +37 -0
  42. package/dist/extensions/first-party/promotion/types/input.d.ts +10 -0
  43. package/dist/extensions/first-party/promotion/types/internal.d.ts +12 -0
  44. package/dist/extensions/first-party/promotion/types/internal.js +22 -0
  45. package/dist/extensions/first-party/promotion/types/main.d.ts +24 -0
  46. package/dist/extensions/first-party/promotion/types/main.js +2 -0
  47. package/dist/extensions/first-party/selected-square/factory.js +3 -3
  48. package/dist/extensions/index.d.ts +1 -1
  49. package/dist/extensions/types/basic/events.d.ts +8 -31
  50. package/dist/extensions/types/basic/events.js +9 -5
  51. package/dist/extensions/types/basic/interaction.d.ts +21 -0
  52. package/dist/extensions/types/basic/transient-visuals.d.ts +2 -1
  53. package/dist/extensions/types/context/events.d.ts +5 -0
  54. package/dist/extensions/types/context/events.js +1 -0
  55. package/dist/extensions/types/context/ui-move.d.ts +3 -15
  56. package/dist/extensions/types/extension.d.ts +10 -8
  57. package/dist/extensions/types/main.d.ts +11 -6
  58. package/dist/extensions/types/surface/commands.d.ts +9 -0
  59. package/dist/extensions/types/surface/events.d.ts +2 -3
  60. package/dist/extensions/types/wrapper.d.ts +23 -22
  61. package/dist/extensions/types/wrapper.js +34 -6
  62. package/dist/extensions/update.d.ts +2 -2
  63. package/dist/extensions/update.js +1 -1
  64. package/dist/layout/factory.js +3 -3
  65. package/dist/layout/geometry/factory.d.ts +2 -11
  66. package/dist/layout/geometry/factory.js +16 -17
  67. package/dist/layout/geometry/helpers.d.ts +5 -0
  68. package/dist/layout/geometry/helpers.js +33 -0
  69. package/dist/layout/geometry/types.d.ts +13 -12
  70. package/dist/layout/helpers.d.ts +3 -4
  71. package/dist/layout/helpers.js +5 -5
  72. package/dist/layout/reducers.js +9 -8
  73. package/dist/layout/types.d.ts +5 -5
  74. package/dist/render/mount.js +2 -2
  75. package/dist/render/rendering/state.js +7 -7
  76. package/dist/runtime/factory/events.d.ts +3 -0
  77. package/dist/runtime/factory/events.js +18 -0
  78. package/dist/runtime/factory/helpers.js +12 -12
  79. package/dist/runtime/factory/input.js +17 -10
  80. package/dist/runtime/factory/main.js +44 -4
  81. package/dist/runtime/input/adapter/event.d.ts +3 -0
  82. package/dist/runtime/input/adapter/event.js +19 -0
  83. package/dist/runtime/input/adapter/factory.js +19 -38
  84. package/dist/runtime/input/adapter/helpers.d.ts +3 -5
  85. package/dist/runtime/input/adapter/helpers.js +60 -12
  86. package/dist/runtime/input/adapter/pointer.d.ts +6 -10
  87. package/dist/runtime/input/adapter/pointer.js +37 -70
  88. package/dist/runtime/input/adapter/types.d.ts +6 -3
  89. package/dist/runtime/input/adapter/types.js +6 -1
  90. package/dist/runtime/input/controller/factory.js +12 -11
  91. package/dist/runtime/input/controller/pointer.d.ts +5 -6
  92. package/dist/runtime/input/controller/pointer.js +36 -35
  93. package/dist/runtime/input/controller/transient-visuals.d.ts +2 -2
  94. package/dist/runtime/input/controller/transient-visuals.js +12 -5
  95. package/dist/runtime/input/controller/types.d.ts +5 -4
  96. package/dist/runtime/lifecycle.js +6 -6
  97. package/dist/runtime/mutation/interaction.js +1 -5
  98. package/dist/runtime/mutation/types.d.ts +10 -8
  99. package/dist/runtime/types/main.d.ts +2 -0
  100. package/dist/state/change/{factory.d.ts → factory/main.d.ts} +1 -1
  101. package/dist/state/change/{factory.js → factory/main.js} +8 -5
  102. package/dist/state/change/factory/ui-move.d.ts +4 -0
  103. package/dist/{runtime → state/change}/factory/ui-move.js +9 -9
  104. package/dist/state/change/helpers.d.ts +4 -2
  105. package/dist/state/change/helpers.js +11 -1
  106. package/dist/state/change/mutation.d.ts +1 -1
  107. package/dist/state/change/reducers.d.ts +3 -3
  108. package/dist/state/change/reducers.js +4 -4
  109. package/dist/state/change/types/main.d.ts +18 -0
  110. package/dist/state/change/types/main.js +1 -0
  111. package/dist/state/change/types/ui-move.d.ts +25 -0
  112. package/dist/state/change/types/ui-move.js +1 -0
  113. package/dist/state/factory.js +1 -1
  114. package/dist/state/interaction/factory.js +4 -2
  115. package/dist/state/interaction/helpers.d.ts +2 -0
  116. package/dist/state/interaction/helpers.js +3 -0
  117. package/dist/state/interaction/types/internal.d.ts +9 -1
  118. package/dist/state/types.d.ts +1 -1
  119. package/dist/wrapper/factory.js +2 -26
  120. package/package.json +9 -5
  121. package/dist/runtime/factory/ui-move.d.ts +0 -4
  122. package/dist/runtime/types/ui-move.d.ts +0 -9
  123. package/dist/state/change/types.d.ts +0 -17
  124. /package/dist/{runtime/types/ui-move.js → extensions/first-party/promotion/types/input.js} +0 -0
  125. /package/dist/{state/change/types.js → extensions/types/basic/interaction.js} +0 -0
@@ -1,8 +1,11 @@
1
1
  import { LayoutSnapshot } from '../../../layout/types.js';
2
2
  import { ColorInput, MoveRequestInput, PiecePositionInput, PositionInput, SquareString } from '../../../state/board/types/input.js';
3
3
  import { Move } from '../../../state/board/types/internal.js';
4
+ import { DeferredUIMoveResolutionDetails } from '../../../state/change/types/ui-move.js';
4
5
  import { MovabilityInput } from '../../../state/interaction/types/input.js';
6
+ import { DragSessionExtensionOwned } from '../../../state/interaction/types/internal.js';
5
7
  import { RuntimeStateSnapshot } from '../../../state/types.js';
8
+ import { ExtensionDragSession } from '../basic/interaction.js';
6
9
  export interface ExtensionRuntimeSurfaceCommandsSnapshot {
7
10
  readonly state: RuntimeStateSnapshot;
8
11
  readonly layout: LayoutSnapshot;
@@ -19,8 +22,14 @@ export interface ExtensionRuntimeSurfaceCommands {
19
22
  setOrientation(orientation: ColorInput): boolean;
20
23
  setMovability(movability: MovabilityInput): boolean;
21
24
  select(square: SquareString | null): boolean;
25
+ startDrag(session: ExtensionDragSession): boolean;
22
26
  clearActiveInteraction(): boolean;
23
27
  clearInteraction(): boolean;
28
+ resolveDeferredUIMoveRequest(details: DeferredUIMoveResolutionDetails): Move;
29
+ cancelDeferredUIMoveRequest(): boolean;
24
30
  requestRender(request: ExtensionRuntimeSurfaceCommandsRenderRequest): void;
25
31
  getSnapshot(): ExtensionRuntimeSurfaceCommandsSnapshot;
26
32
  }
33
+ export interface ExtensionRuntimeSurfaceCommandsInternalSurface extends ExtensionRuntimeSurfaceCommands {
34
+ startDrag(session: DragSessionExtensionOwned): boolean;
35
+ }
@@ -1,5 +1,4 @@
1
- import { SceneEventType } from '../basic/events.js';
2
1
  export interface ExtensionRuntimeSurfaceEvents {
3
- subscribe(events: Iterable<SceneEventType>): void;
4
- unsubscribe(events?: Iterable<SceneEventType>): void;
2
+ subscribeEvent<K extends keyof HTMLElementEventMap>(type: K): void;
3
+ unsubscribeEvent<K extends keyof HTMLElementEventMap>(type: K): void;
5
4
  }
@@ -1,26 +1,27 @@
1
1
  /**
2
2
  * Public built-in extension ids accepted by createBoard({ extensions })
3
3
  */
4
- import { ActiveTargetDefinition } from '../first-party/active-target/types.js';
5
- import { BoardEventsDefinition } from '../first-party/board-events/types.js';
6
- import { LastMoveDefinition } from '../first-party/last-move/types.js';
7
- import { LegalMovesDefinition } from '../first-party/legal-moves/types.js';
8
- import { MainRendererDefinition } from '../first-party/main-renderer/types/extension.js';
9
- import { SelectedSquareDefinition } from '../first-party/selected-square/types.js';
10
- export declare const DefaultBuiltinChessboardExtensions: readonly ["mainRenderer", "events", "selectedSquare", "activeTarget", "legalMoves", "lastMove"];
11
- export type BuiltInExtensionId = (typeof DefaultBuiltinChessboardExtensions)[number];
12
- /**
13
- * Default built-in extensions used when createBoard(...) receives no extensions option
14
- */
4
+ import { createActiveTarget } from '../first-party/active-target/factory.js';
5
+ import { createAutoPromote } from '../first-party/auto-promote/factory.js';
6
+ import { createBoardEvents } from '../first-party/board-events/factory.js';
7
+ import { createLastMove } from '../first-party/last-move/factory.js';
8
+ import { createLegalMoves } from '../first-party/legal-moves/factory.js';
9
+ import { createMainRenderer } from '../first-party/main-renderer/factory.js';
10
+ import { createPromotion } from '../first-party/promotion/factory.js';
11
+ import { createSelectedSquare } from '../first-party/selected-square/factory.js';
12
+ export declare const builtInExtensionFactoryMap: {
13
+ renderer: typeof createMainRenderer;
14
+ selectedSquare: typeof createSelectedSquare;
15
+ lastMove: typeof createLastMove;
16
+ activeTarget: typeof createActiveTarget;
17
+ legalMoves: typeof createLegalMoves;
18
+ events: typeof createBoardEvents;
19
+ autoPromote: typeof createAutoPromote;
20
+ promotion: typeof createPromotion;
21
+ };
22
+ export type BuiltInExtensionId = keyof typeof builtInExtensionFactoryMap;
23
+ export type BuiltInExtensionDefinitionMap = {
24
+ [Id in BuiltInExtensionId]: ReturnType<(typeof builtInExtensionFactoryMap)[Id]>;
25
+ };
26
+ export declare const DefaultBuiltinChessboardExtensions: readonly ["renderer", "selectedSquare", "lastMove", "activeTarget", "legalMoves", "events", "autoPromote", "promotion"];
15
27
  export type DefaultBuiltinChessboardExtensions = typeof DefaultBuiltinChessboardExtensions;
16
- /**
17
- * Map built-in public ids -> concrete extension definition types
18
- */
19
- export interface BuiltInExtensionDefinitionMap {
20
- mainRenderer: MainRendererDefinition;
21
- events: BoardEventsDefinition;
22
- selectedSquare: SelectedSquareDefinition;
23
- activeTarget: ActiveTargetDefinition;
24
- legalMoves: LegalMovesDefinition;
25
- lastMove: LastMoveDefinition;
26
- }
@@ -1,11 +1,39 @@
1
1
  /**
2
2
  * Public built-in extension ids accepted by createBoard({ extensions })
3
3
  */
4
+ import { createActiveTarget } from '../first-party/active-target/factory.js';
5
+ import { EXTENSION_ID as EXTENSION_ID_ACTIVE_TARGET } from '../first-party/active-target/types.js';
6
+ import { createAutoPromote } from '../first-party/auto-promote/factory.js';
7
+ import { EXTENSION_ID as EXTENSION_ID_AUTO_PROMOTE } from '../first-party/auto-promote/types.js';
8
+ import { createBoardEvents } from '../first-party/board-events/factory.js';
9
+ import { EXTENSION_ID as EXTENSION_ID_BOARD_EVENTS } from '../first-party/board-events/types.js';
10
+ import { createLastMove } from '../first-party/last-move/factory.js';
11
+ import { EXTENSION_ID as EXTENSION_ID_LAST_MOVE } from '../first-party/last-move/types.js';
12
+ import { createLegalMoves } from '../first-party/legal-moves/factory.js';
13
+ import { EXTENSION_ID as EXTENSION_ID_LEGAL_MOVES } from '../first-party/legal-moves/types.js';
14
+ import { createMainRenderer } from '../first-party/main-renderer/factory.js';
15
+ import { EXTENSION_ID as EXTENSION_ID_RENDERER } from '../first-party/main-renderer/types/extension.js';
16
+ import { createPromotion } from '../first-party/promotion/factory.js';
17
+ import { EXTENSION_ID as EXTENSION_ID_PROMOTION } from '../first-party/promotion/types/main.js';
18
+ import { createSelectedSquare } from '../first-party/selected-square/factory.js';
19
+ import { EXTENSION_ID as EXTENSION_ID_SELECTED_SQUARE } from '../first-party/selected-square/types.js';
20
+ export const builtInExtensionFactoryMap = {
21
+ [EXTENSION_ID_RENDERER]: createMainRenderer,
22
+ [EXTENSION_ID_SELECTED_SQUARE]: createSelectedSquare,
23
+ [EXTENSION_ID_LAST_MOVE]: createLastMove,
24
+ [EXTENSION_ID_ACTIVE_TARGET]: createActiveTarget,
25
+ [EXTENSION_ID_LEGAL_MOVES]: createLegalMoves,
26
+ [EXTENSION_ID_BOARD_EVENTS]: createBoardEvents,
27
+ [EXTENSION_ID_AUTO_PROMOTE]: createAutoPromote,
28
+ [EXTENSION_ID_PROMOTION]: createPromotion
29
+ };
4
30
  export const DefaultBuiltinChessboardExtensions = [
5
- 'mainRenderer',
6
- 'events',
7
- 'selectedSquare',
8
- 'activeTarget',
9
- 'legalMoves',
10
- 'lastMove'
31
+ EXTENSION_ID_RENDERER,
32
+ EXTENSION_ID_SELECTED_SQUARE,
33
+ EXTENSION_ID_LAST_MOVE,
34
+ EXTENSION_ID_ACTIVE_TARGET,
35
+ EXTENSION_ID_LEGAL_MOVES,
36
+ EXTENSION_ID_BOARD_EVENTS,
37
+ EXTENSION_ID_AUTO_PROMOTE,
38
+ EXTENSION_ID_PROMOTION
11
39
  ];
@@ -1,4 +1,4 @@
1
- import { ExtensionUIMoveRequestContext } from './types/context/ui-move.js';
1
+ import { ExtensionPendingUIMoveRequestContext } from './types/context/ui-move.js';
2
2
  import { ExtensionSystemInternal, ExtensionSystemUpdateRequest } from './types/main.js';
3
3
  export declare function extensionSystemUpdateState(state: ExtensionSystemInternal, request: ExtensionSystemUpdateRequest): void;
4
- export declare function extensionSystemUpdateUIMoveRequest(state: ExtensionSystemInternal, context: ExtensionUIMoveRequestContext): void;
4
+ export declare function extensionSystemUpdateUIMoveRequest(state: ExtensionSystemInternal, context: ExtensionPendingUIMoveRequestContext): void;
@@ -16,7 +16,7 @@ export function extensionSystemUpdateState(state, request) {
16
16
  : {
17
17
  ...contextCommon
18
18
  };
19
- extension.instance.onUpdate(context);
19
+ extension.instance.onUpdate?.(context);
20
20
  }
21
21
  state.currentFrame = request.state;
22
22
  }
@@ -2,7 +2,7 @@ import { cloneDeep } from 'es-toolkit/object';
2
2
  import { layoutRefreshGeometry } from './reducers.js';
3
3
  function createLayoutInternal() {
4
4
  return {
5
- boardSize: 0,
5
+ sceneSize: null,
6
6
  orientation: 0 /* ColorCode.White */,
7
7
  geometry: null,
8
8
  layoutEpoch: 0
@@ -11,8 +11,8 @@ function createLayoutInternal() {
11
11
  export function createLayout() {
12
12
  const internalState = createLayoutInternal();
13
13
  return {
14
- get boardSize() {
15
- return internalState.boardSize;
14
+ get sceneSize() {
15
+ return internalState.sceneSize;
16
16
  },
17
17
  get orientation() {
18
18
  return internalState.orientation;
@@ -1,12 +1,3 @@
1
1
  import { ColorCode } from '../../state/board/types/internal.js';
2
- import { RenderGeometry } from './types.js';
3
- /**
4
- * Create RenderGeometry for a given board size (in px) and orientation.
5
- * - Geometry uses an SVG-local coordinate system with origin at top-left.
6
- * - Orientation mapping:
7
- * - 'white': files increase left→right (a..h), ranks increase bottom→top (1..8).
8
- * yIndex = 7 - rank, xIndex = file
9
- * - 'black': files increase right→left (a..h), ranks increase top→bottom (1..8).
10
- * yIndex = rank, xIndex = 7 - file
11
- */
12
- export declare function createRenderGeometry(boardSize: number, orientation: ColorCode): RenderGeometry;
2
+ import { SceneRenderGeometry, Size } from './types.js';
3
+ export declare function createRenderGeometry(sceneSize: Size, orientation: ColorCode): SceneRenderGeometry;
@@ -1,32 +1,31 @@
1
1
  import { fileOf, rankOf } from '../../state/board/coords.js';
2
- /**
3
- * Create RenderGeometry for a given board size (in px) and orientation.
4
- * - Geometry uses an SVG-local coordinate system with origin at top-left.
5
- * - Orientation mapping:
6
- * - 'white': files increase left→right (a..h), ranks increase bottom→top (1..8).
7
- * yIndex = 7 - rank, xIndex = file
8
- * - 'black': files increase right→left (a..h), ranks increase top→bottom (1..8).
9
- * yIndex = rank, xIndex = 7 - file
10
- */
11
- export function createRenderGeometry(boardSize, orientation) {
12
- if (!(boardSize > 0 && Number.isFinite(boardSize))) {
13
- throw new RangeError(`Invalid boardSize: ${boardSize}`);
2
+ import { makeBoardRect, measureBoardSize } from './helpers.js';
3
+ export function createRenderGeometry(sceneSize, orientation) {
4
+ if (!(sceneSize.height > 0 &&
5
+ Number.isFinite(sceneSize.height) &&
6
+ sceneSize.width > 0 &&
7
+ Number.isFinite(sceneSize.width))) {
8
+ throw new RangeError(`Invalid sceneSize: ${JSON.stringify(sceneSize)}`);
14
9
  }
10
+ const boardSize = measureBoardSize(sceneSize);
11
+ const boardRect = makeBoardRect(sceneSize);
15
12
  const squareSize = boardSize / 8;
16
13
  const white = orientation === 0 /* ColorCode.White */;
17
14
  return {
18
- boardSize,
15
+ sceneSize: { ...sceneSize },
16
+ boardRect,
19
17
  squareSize,
20
18
  orientation,
21
- squareRect(sq) {
19
+ getSquareRect(sq) {
22
20
  const f = fileOf(sq);
23
21
  const r = rankOf(sq);
24
22
  const xIndex = white ? f : 7 - f;
25
23
  const yIndex = white ? 7 - r : r;
26
24
  return {
27
- x: xIndex * squareSize,
28
- y: yIndex * squareSize,
29
- size: squareSize
25
+ x: boardRect.x + xIndex * squareSize,
26
+ y: boardRect.y + yIndex * squareSize,
27
+ width: squareSize,
28
+ height: squareSize
30
29
  };
31
30
  }
32
31
  };
@@ -0,0 +1,5 @@
1
+ import { Rect, RectSnapshot, Size, SizeSnapshot } from './types.js';
2
+ export declare function sceneRectsEqual(rect1: RectSnapshot | null, rect2: RectSnapshot | null): boolean;
3
+ export declare function sceneSizesEqual(size1: SizeSnapshot | null, size2: SizeSnapshot | null): boolean;
4
+ export declare function measureBoardSize(sceneSize: Size): number;
5
+ export declare function makeBoardRect(sceneSize: Size): Rect;
@@ -0,0 +1,33 @@
1
+ export function sceneRectsEqual(rect1, rect2) {
2
+ if (rect1 === null && rect2 === null) {
3
+ return true;
4
+ }
5
+ if (rect1 === null || rect2 === null) {
6
+ return false;
7
+ }
8
+ return (rect1.x === rect2.x &&
9
+ rect1.y === rect2.y &&
10
+ rect1.width === rect2.width &&
11
+ rect1.height === rect2.height);
12
+ }
13
+ export function sceneSizesEqual(size1, size2) {
14
+ if (size1 === null && size2 === null) {
15
+ return true;
16
+ }
17
+ if (size1 === null || size2 === null) {
18
+ return false;
19
+ }
20
+ return size1.width === size2.width && size1.height === size2.height;
21
+ }
22
+ export function measureBoardSize(sceneSize) {
23
+ return Math.min(sceneSize.width, sceneSize.height);
24
+ }
25
+ export function makeBoardRect(sceneSize) {
26
+ const boardSize = measureBoardSize(sceneSize);
27
+ return {
28
+ x: (sceneSize.width - boardSize) / 2,
29
+ y: (sceneSize.height - boardSize) / 2,
30
+ width: boardSize,
31
+ height: boardSize
32
+ };
33
+ }
@@ -1,20 +1,21 @@
1
1
  import { ReadonlyDeep } from 'type-fest';
2
2
  import { ColorCode, Square } from '../../state/board/types/internal.js';
3
- export interface SquareRect {
3
+ export interface Size {
4
+ width: number;
5
+ height: number;
6
+ }
7
+ export type SizeSnapshot = ReadonlyDeep<Size>;
8
+ export interface Rect {
4
9
  x: number;
5
10
  y: number;
6
- size: number;
11
+ width: number;
12
+ height: number;
7
13
  }
8
- export type SquareRectSnapshot = ReadonlyDeep<SquareRect>;
9
- /**
10
- * RenderGeometry represents the derived geometry of the rendered chessboard.
11
- * It includes the board size, square size, orientation, and square rectangle mapping.
12
- *
13
- * It is computed from layout inputs and used by rendering and interaction code.
14
- */
15
- export interface RenderGeometry {
16
- readonly boardSize: number;
14
+ export type RectSnapshot = ReadonlyDeep<Rect>;
15
+ export interface SceneRenderGeometry {
16
+ readonly sceneSize: SizeSnapshot;
17
+ readonly boardRect: RectSnapshot;
17
18
  readonly squareSize: number;
18
19
  readonly orientation: ColorCode;
19
- squareRect(sq: Square): SquareRectSnapshot;
20
+ getSquareRect(sq: Square): RectSnapshot;
20
21
  }
@@ -1,4 +1,3 @@
1
- /**
2
- * Measure board size from container (square board fits within host).
3
- */
4
- export declare function measureBoardSize(container: HTMLElement): number;
1
+ import { Size } from './geometry/types.js';
2
+ export declare function measureSceneSize(container: HTMLElement): Size;
3
+ export declare function isSceneSizeValid(size: Size | null): boolean;
@@ -1,6 +1,6 @@
1
- /**
2
- * Measure board size from container (square board fits within host).
3
- */
4
- export function measureBoardSize(container) {
5
- return Math.min(container.clientWidth, container.clientHeight);
1
+ export function measureSceneSize(container) {
2
+ return { width: container.clientWidth, height: container.clientHeight };
3
+ }
4
+ export function isSceneSizeValid(size) {
5
+ return size !== null && size.width > 0 && size.height > 0;
6
6
  }
@@ -1,5 +1,6 @@
1
1
  import { createRenderGeometry } from './geometry/factory.js';
2
- import { measureBoardSize } from './helpers.js';
2
+ import { sceneSizesEqual } from './geometry/helpers.js';
3
+ import { isSceneSizeValid, measureSceneSize } from './helpers.js';
3
4
  export function layoutRefreshGeometry(state, options) {
4
5
  const orientation = options.orientation ?? state.orientation;
5
6
  if (orientation === null) {
@@ -7,23 +8,23 @@ export function layoutRefreshGeometry(state, options) {
7
8
  // For now we throw
8
9
  throw new Error('Orientation is expected to be provided on the first pass of layout.refreshGeometry');
9
10
  }
10
- let boardSize = options.container ? measureBoardSize(options.container) : state.boardSize;
11
- if (boardSize !== null && boardSize <= 0)
12
- boardSize = null;
13
- const changed = boardSize !== state.boardSize || orientation !== state.orientation;
11
+ let sceneSize = options.container ? measureSceneSize(options.container) : state.sceneSize;
12
+ if (sceneSize !== null && !isSceneSizeValid(sceneSize))
13
+ sceneSize = null;
14
+ const changed = !sceneSizesEqual(sceneSize, state.sceneSize) || orientation !== state.orientation;
14
15
  if (!changed) {
15
16
  return false; // no-op, no relevant changes
16
17
  }
17
18
  // Update the state
18
- state.boardSize = boardSize;
19
+ state.sceneSize = sceneSize;
19
20
  state.orientation = orientation;
20
- if (boardSize === null || orientation === null) {
21
+ if (sceneSize === null || orientation === null) {
21
22
  // General invalid-geometry rule. Orientation is currently guarded above,
22
23
  // but this branch stays explicit in case null orientation is allowed later.
23
24
  state.geometry = null;
24
25
  }
25
26
  else {
26
- state.geometry = createRenderGeometry(boardSize, orientation);
27
+ state.geometry = createRenderGeometry(sceneSize, orientation);
27
28
  }
28
29
  state.layoutEpoch++;
29
30
  return true;
@@ -1,11 +1,11 @@
1
1
  import { ReadonlyDeep } from 'type-fest';
2
2
  import { ColorCode } from '../state/board/types/internal.js';
3
- import { RenderGeometry } from './geometry/types.js';
3
+ import { SceneRenderGeometry, Size } from './geometry/types.js';
4
4
  import { LayoutMutationSession } from './mutation.js';
5
5
  export interface LayoutInternal {
6
- boardSize: number | null;
6
+ sceneSize: Size | null;
7
7
  orientation: ColorCode | null;
8
- geometry: RenderGeometry | null;
8
+ geometry: SceneRenderGeometry | null;
9
9
  layoutEpoch: number;
10
10
  }
11
11
  export type LayoutSnapshot = ReadonlyDeep<LayoutInternal>;
@@ -14,9 +14,9 @@ export interface LayoutRefreshOptions {
14
14
  orientation?: ColorCode;
15
15
  }
16
16
  export interface Layout {
17
- readonly boardSize: number | null;
17
+ readonly sceneSize: Size | null;
18
18
  readonly orientation: ColorCode | null;
19
- readonly geometry: RenderGeometry | null;
19
+ readonly geometry: SceneRenderGeometry | null;
20
20
  readonly layoutEpoch: number;
21
21
  refreshGeometry(options: LayoutRefreshOptions, mutationSession: LayoutMutationSession): boolean;
22
22
  getSnapshot(): LayoutSnapshot;
@@ -8,13 +8,13 @@ export function renderMount(state, element) {
8
8
  // Now call onMount for all extensions with their slot roots
9
9
  for (const extensionRec of state.extensions.values()) {
10
10
  const slotRoots = extensionRec.render.slots;
11
- extensionRec.extension.instance.mount({ slotRoots });
11
+ extensionRec.extension.instance.mount?.({ slotRoots });
12
12
  }
13
13
  }
14
14
  export function renderUnmount(state) {
15
15
  validateIsMounted(state);
16
16
  for (const extensionRec of state.extensions.values()) {
17
- extensionRec.extension.instance.unmount();
17
+ extensionRec.extension.instance.unmount?.();
18
18
  // clear the slot roots
19
19
  for (const slotRoot of Object.values(extensionRec.render.slots)) {
20
20
  clearElementChildren(slotRoot);
@@ -1,3 +1,4 @@
1
+ import { sceneSizesEqual } from '../../layout/geometry/helpers.js';
1
2
  import { updateElementAttributes } from '../svg/helpers.js';
2
3
  import { validateIsMounted } from './helpers.js';
3
4
  export function checkNeedsRender(state) {
@@ -16,14 +17,13 @@ export function performRenderPass(state, request) {
16
17
  if (!request.layout.geometry) {
17
18
  throw new Error('render() called without a valid layout geometry');
18
19
  }
19
- const currentSize = request.layout.geometry.boardSize;
20
- const prevSize = state.currentFrame?.layout.geometry?.boardSize;
21
- if (currentSize !== prevSize) {
22
- const size = String(currentSize);
20
+ const currentSize = request.layout.geometry.sceneSize;
21
+ const prevSize = state.currentFrame?.layout.geometry?.sceneSize ?? null;
22
+ if (!sceneSizesEqual(currentSize, prevSize)) {
23
23
  updateElementAttributes(state.svgRoots.svgRoot, {
24
- width: size,
25
- height: size,
26
- viewBox: `0 0 ${size} ${size}`
24
+ width: currentSize.width.toString(),
25
+ height: currentSize.height.toString(),
26
+ viewBox: `0 0 ${currentSize.width} ${currentSize.height}`
27
27
  });
28
28
  }
29
29
  // Check if we have any invalidation states
@@ -0,0 +1,3 @@
1
+ import { ExtensionRuntimeSurfaceEvents } from '../../extensions/types/surface/events.js';
2
+ import { GetInternalState } from '../types/main.js';
3
+ export declare function createExtensionRuntimeSurfaceEvents(getInternalState: GetInternalState): ExtensionRuntimeSurfaceEvents;
@@ -0,0 +1,18 @@
1
+ import assert from '@ktarmyshov/assert';
2
+ import { NEED_EVENT_TYPES } from '../input/adapter/types.js';
3
+ export function createExtensionRuntimeSurfaceEvents(getInternalState) {
4
+ return {
5
+ subscribeEvent(type) {
6
+ const internalState = getInternalState();
7
+ assert(internalState.inputAdapter, 'Input adapter not available in the current runtime state');
8
+ internalState.inputAdapter.subscribeEvent(type);
9
+ },
10
+ unsubscribeEvent(type) {
11
+ const internalState = getInternalState();
12
+ assert(internalState.inputAdapter, 'Input adapter not available in the current runtime state');
13
+ if (!NEED_EVENT_TYPES.has(type)) {
14
+ internalState.inputAdapter.unsubscribeEvent(type);
15
+ }
16
+ }
17
+ };
18
+ }
@@ -1,9 +1,9 @@
1
1
  import assert from '@ktarmyshov/assert';
2
2
  import { isEmptyPieceCode } from '../../state/board/check.js';
3
+ import { createPendingUIMoveRequest } from '../../state/change/factory/ui-move.js';
3
4
  import { canMoveTo } from '../input/controller/helpers.js';
4
- import { createUIMoveRequestContext } from './ui-move.js';
5
- function assertResolvedAfterAutoResolve(state) {
6
- if (state.status !== 'resolved') {
5
+ function assertResolvedAfterAutoResolve(request) {
6
+ if (request.status !== 'resolved') {
7
7
  throw new Error('UI move request must be resolved after autoresolve');
8
8
  }
9
9
  }
@@ -22,18 +22,18 @@ export function uiMoveCompleteTo(state, target) {
22
22
  assert(canMoveTo(interaction.getSnapshot(), target), 'dropTo/releaseTo target must be a valid move destination');
23
23
  // canMoveTo already confirmed we can move, but if 'FREE' movability it can be that there are no activeDestinations for the target square, so we need to handle that case as well
24
24
  const destination = interaction.activeDestinations.get(target) ?? { to: target };
25
- const moveRequestContext = createUIMoveRequestContext(dragSession.sourceSquare, destination);
26
- state.extensionSystem.onUIMoveRequest(moveRequestContext);
27
- if (moveRequestContext.status === 'deferred') {
28
- state.state.change.setDeferredUIMoveRequestContext(moveRequestContext, mutationSession);
25
+ const pendingUIMoveRequest = createPendingUIMoveRequest(dragSession.sourceSquare, destination);
26
+ state.extensionSystem.onUIMoveRequest({ request: pendingUIMoveRequest });
27
+ if (pendingUIMoveRequest.status === 'deferred') {
28
+ state.state.change.setDeferredUIMoveRequest(pendingUIMoveRequest, mutationSession);
29
29
  }
30
- else if (moveRequestContext.status === 'unresolved') {
30
+ else if (pendingUIMoveRequest.status === 'unresolved') {
31
31
  // Try to auto-resolve if possible (e.g. if there are no promotion choices to be made)
32
- moveRequestContext.autoresolve();
33
- assertResolvedAfterAutoResolve(moveRequestContext);
32
+ pendingUIMoveRequest.autoresolve();
33
+ assertResolvedAfterAutoResolve(pendingUIMoveRequest);
34
34
  }
35
- if (moveRequestContext.status === 'resolved') {
36
- const moveRequest = moveRequestContext.resolvedMoveRequest;
35
+ if (pendingUIMoveRequest.status === 'resolved') {
36
+ const moveRequest = pendingUIMoveRequest.resolvedMoveRequest;
37
37
  if (moveRequest !== null) {
38
38
  state.state.board.move(moveRequest, mutationSession);
39
39
  }
@@ -1,5 +1,6 @@
1
1
  import assert from '@ktarmyshov/assert';
2
2
  import { isEmptyPieceCode } from '../../state/board/check.js';
3
+ import { isDragSessionCoreOwned } from '../../state/interaction/helpers.js';
3
4
  import { runtimeRunMutationPipeline } from '../mutation/run.js';
4
5
  import { uiMoveCompleteTo } from './helpers.js';
5
6
  export function createRuntimeInteractionSurface(state) {
@@ -24,6 +25,7 @@ export function createRuntimeInteractionSurface(state) {
24
25
  };
25
26
  interaction.setSelected(interactionSource, interactionMutationSession);
26
27
  const dragSession = {
28
+ owner: 'core',
27
29
  type: 'lifted-piece-drag',
28
30
  sourceSquare: interactionSource.square,
29
31
  sourcePieceCode: interactionSource.pieceCode,
@@ -36,27 +38,27 @@ export function createRuntimeInteractionSurface(state) {
36
38
  const internalState = state();
37
39
  internalState.renderSystem.requestRenderVisuals(input);
38
40
  },
39
- dropTo(target) {
41
+ completeCoreDragTo(target) {
40
42
  const internalState = state();
41
43
  const mutationSession = internalState.mutation.getSession();
42
44
  const interaction = internalState.state.interaction;
43
45
  const dragSession = interaction.dragSession;
44
- assert(dragSession !== null, 'dropTo requires an active drag session');
45
- assert(dragSession.type === 'lifted-piece-drag', 'dropTo requires a lifted-piece-drag session');
46
+ assert(dragSession !== null, 'completeDragTo requires an active drag session');
47
+ assert(isDragSessionCoreOwned(dragSession), 'completeDragTo can only be called for core-owned drag sessions');
46
48
  uiMoveCompleteTo(internalState, target);
47
- mutationSession.addMutation('runtime.interaction.dropTo', true);
49
+ mutationSession.addMutation('runtime.interaction.completeDragTo', true, dragSession);
48
50
  runtimeRunMutationPipeline(internalState);
49
51
  },
50
- releaseTo(target) {
52
+ completeExtensionDrag(target) {
51
53
  const internalState = state();
52
54
  const mutationSession = internalState.mutation.getSession();
53
55
  const interaction = internalState.state.interaction;
54
56
  const dragSession = interaction.dragSession;
55
- assert(dragSession !== null, 'releaseTo requires an active drag session');
56
- assert(dragSession.type === 'release-targeting', 'releaseTo requires a release-targeting session');
57
- uiMoveCompleteTo(internalState, target);
58
- mutationSession.addMutation('runtime.interaction.releaseTo', true);
59
- runtimeRunMutationPipeline(internalState);
57
+ assert(dragSession !== null, 'completeExtensionDrag requires an active drag session');
58
+ assert(!isDragSessionCoreOwned(dragSession), 'completeExtensionDrag can only be called for extension-owned drag sessions');
59
+ assert(dragSession.targetSquare === target, 'The target square in completeExtensionDrag must match the current target square of the drag session');
60
+ internalState.extensionSystem.completeDrag(dragSession);
61
+ internalState.state.interaction.setDragSession(null, mutationSession);
60
62
  },
61
63
  startReleaseTargetingDrag(source, target) {
62
64
  const internalState = state();
@@ -65,6 +67,7 @@ export function createRuntimeInteractionSurface(state) {
65
67
  assert(interaction.selected !== null, 'startReleaseTargetingDrag requires a selected piece');
66
68
  assert(interaction.selected.square === source, 'startReleaseTargetingDrag source must match selected square');
67
69
  const dragSession = {
70
+ owner: 'core',
68
71
  type: 'release-targeting',
69
72
  sourceSquare: source,
70
73
  sourcePieceCode: interaction.selected.pieceCode,
@@ -93,6 +96,10 @@ export function createRuntimeInteractionSurface(state) {
93
96
  assert(currentDragSession !== null, 'No active drag session to update');
94
97
  internalState.state.interaction.updateDragSessionCurrentTarget(target, interactionMutationSession);
95
98
  runtimeRunMutationPipeline(internalState);
99
+ },
100
+ onEvent(context) {
101
+ const internalState = state();
102
+ internalState.extensionSystem.onEvent(context);
96
103
  }
97
104
  };
98
105
  }