@mirasen/chessboard 1.1.4 → 1.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.
Files changed (135) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/README.md +7 -1
  3. package/assets/watermark/mirasen.svg +10 -0
  4. package/dist/animation/render/fade.d.ts +2 -3
  5. package/dist/animation/render/fade.js +6 -6
  6. package/dist/animation/render/move.d.ts +2 -3
  7. package/dist/animation/render/move.js +6 -6
  8. package/dist/animation/render/plan.d.ts +3 -4
  9. package/dist/animation/render/plan.js +5 -5
  10. package/dist/animation/render/static.d.ts +2 -3
  11. package/dist/animation/render/static.js +4 -4
  12. package/dist/animation/render/types.d.ts +10 -4
  13. package/dist/extensions/build/index.d.ts +1 -2
  14. package/dist/extensions/build/index.js +1 -1
  15. package/dist/extensions/factory/main.js +11 -7
  16. package/dist/extensions/factory/runtime.js +24 -1
  17. package/dist/extensions/first-party/active-target/factory.js +18 -11
  18. package/dist/extensions/first-party/annotations/api.d.ts +16 -0
  19. package/dist/extensions/first-party/annotations/api.js +79 -0
  20. package/dist/extensions/first-party/annotations/committed.d.ts +3 -0
  21. package/dist/extensions/first-party/annotations/committed.js +11 -0
  22. package/dist/extensions/first-party/annotations/constants.d.ts +3 -0
  23. package/dist/extensions/first-party/annotations/constants.js +54 -0
  24. package/dist/extensions/first-party/annotations/denormalize.d.ts +4 -0
  25. package/dist/extensions/first-party/annotations/denormalize.js +22 -0
  26. package/dist/extensions/first-party/annotations/factory.d.ts +3 -0
  27. package/dist/extensions/first-party/annotations/factory.js +143 -0
  28. package/dist/extensions/first-party/annotations/interaction.d.ts +8 -0
  29. package/dist/extensions/first-party/annotations/interaction.js +129 -0
  30. package/dist/extensions/first-party/annotations/invalidation.d.ts +3 -0
  31. package/dist/extensions/first-party/annotations/invalidation.js +7 -0
  32. package/dist/extensions/first-party/annotations/normalize.d.ts +12 -0
  33. package/dist/extensions/first-party/annotations/normalize.js +54 -0
  34. package/dist/extensions/first-party/annotations/render/committed.d.ts +3 -0
  35. package/dist/extensions/first-party/annotations/render/committed.js +123 -0
  36. package/dist/extensions/first-party/annotations/render/helpers/arrows.d.ts +33 -0
  37. package/dist/extensions/first-party/annotations/render/helpers/arrows.js +93 -0
  38. package/dist/extensions/first-party/annotations/render/helpers/circles.d.ts +18 -0
  39. package/dist/extensions/first-party/annotations/render/helpers/circles.js +30 -0
  40. package/dist/extensions/first-party/annotations/render/preview.d.ts +3 -0
  41. package/dist/extensions/first-party/annotations/render/preview.js +96 -0
  42. package/dist/extensions/first-party/annotations/types/internal.d.ts +77 -0
  43. package/dist/extensions/first-party/annotations/types/internal.js +1 -0
  44. package/dist/extensions/first-party/annotations/types/main.d.ts +40 -0
  45. package/dist/extensions/first-party/annotations/types/main.js +6 -0
  46. package/dist/extensions/first-party/annotations/types/public.d.ts +48 -0
  47. package/dist/extensions/first-party/annotations/types/public.js +1 -0
  48. package/dist/extensions/first-party/annotations/update.d.ts +3 -0
  49. package/dist/extensions/first-party/annotations/update.js +45 -0
  50. package/dist/extensions/first-party/auto-promote/factory.js +6 -6
  51. package/dist/extensions/first-party/board-events/factory.js +6 -6
  52. package/dist/extensions/first-party/common/helpers.d.ts +2 -1
  53. package/dist/extensions/first-party/common/helpers.js +6 -5
  54. package/dist/extensions/first-party/common/types.d.ts +2 -0
  55. package/dist/extensions/first-party/last-move/factory.js +9 -9
  56. package/dist/extensions/first-party/legal-moves/factory.js +8 -8
  57. package/dist/extensions/first-party/main-renderer/animation/factory.d.ts +2 -2
  58. package/dist/extensions/first-party/main-renderer/animation/factory.js +4 -4
  59. package/dist/extensions/first-party/main-renderer/animation/render.d.ts +1 -1
  60. package/dist/extensions/first-party/main-renderer/animation/render.js +2 -2
  61. package/dist/extensions/first-party/main-renderer/animation/types.d.ts +3 -3
  62. package/dist/extensions/first-party/main-renderer/board/factory.js +2 -2
  63. package/dist/extensions/first-party/main-renderer/board/render.d.ts +1 -1
  64. package/dist/extensions/first-party/main-renderer/board/render.js +4 -4
  65. package/dist/extensions/first-party/main-renderer/board/types.d.ts +1 -1
  66. package/dist/extensions/first-party/main-renderer/coordinates/factory.js +2 -2
  67. package/dist/extensions/first-party/main-renderer/coordinates/render.d.ts +1 -1
  68. package/dist/extensions/first-party/main-renderer/coordinates/render.js +5 -5
  69. package/dist/extensions/first-party/main-renderer/coordinates/types.d.ts +1 -1
  70. package/dist/extensions/first-party/main-renderer/drag/factory.d.ts +2 -2
  71. package/dist/extensions/first-party/main-renderer/drag/factory.js +11 -8
  72. package/dist/extensions/first-party/main-renderer/drag/render.d.ts +1 -1
  73. package/dist/extensions/first-party/main-renderer/drag/render.js +7 -6
  74. package/dist/extensions/first-party/main-renderer/drag/types.d.ts +6 -5
  75. package/dist/extensions/first-party/main-renderer/drag/update.js +2 -2
  76. package/dist/extensions/first-party/main-renderer/factory.js +9 -5
  77. package/dist/extensions/first-party/main-renderer/piece-symbols.d.ts +19 -0
  78. package/dist/extensions/first-party/main-renderer/piece-symbols.js +45 -0
  79. package/dist/extensions/first-party/main-renderer/pieces/factory.d.ts +2 -2
  80. package/dist/extensions/first-party/main-renderer/pieces/factory.js +4 -4
  81. package/dist/extensions/first-party/main-renderer/pieces/render.d.ts +1 -1
  82. package/dist/extensions/first-party/main-renderer/pieces/render.js +8 -8
  83. package/dist/extensions/first-party/main-renderer/pieces/types.d.ts +4 -4
  84. package/dist/extensions/first-party/main-renderer/types/extension.d.ts +1 -1
  85. package/dist/extensions/first-party/main-renderer/types/extension.js +8 -1
  86. package/dist/extensions/first-party/main-renderer/types/instance.d.ts +2 -0
  87. package/dist/extensions/first-party/promotion/factory.js +10 -10
  88. package/dist/extensions/first-party/selected-square/factory.js +8 -8
  89. package/dist/extensions/first-party/watermark/factory.d.ts +2 -0
  90. package/dist/extensions/first-party/watermark/factory.js +82 -0
  91. package/dist/extensions/first-party/watermark/types.d.ts +13 -0
  92. package/dist/extensions/first-party/watermark/types.js +2 -0
  93. package/dist/extensions/types/basic/events.d.ts +32 -3
  94. package/dist/extensions/types/basic/events.js +2 -1
  95. package/dist/extensions/types/basic/mount.d.ts +5 -2
  96. package/dist/extensions/types/basic/mount.js +0 -2
  97. package/dist/extensions/types/basic/transient-visuals.d.ts +1 -1
  98. package/dist/extensions/types/context/animation.d.ts +3 -3
  99. package/dist/extensions/types/context/events.d.ts +2 -1
  100. package/dist/extensions/types/context/update.d.ts +6 -7
  101. package/dist/extensions/types/extension.d.ts +3 -0
  102. package/dist/extensions/types/main.d.ts +1 -0
  103. package/dist/extensions/types/surface/commands.d.ts +1 -1
  104. package/dist/extensions/types/surface/main.d.ts +6 -4
  105. package/dist/extensions/types/wrapper.d.ts +5 -1
  106. package/dist/extensions/types/wrapper.js +10 -2
  107. package/dist/extensions/update.js +6 -10
  108. package/dist/index.d.ts +1 -1
  109. package/dist/render/mount.js +3 -3
  110. package/dist/render/rendering/state.js +2 -2
  111. package/dist/render/svg/factory.js +40 -25
  112. package/dist/render/svg/helpers.d.ts +8 -4
  113. package/dist/render/svg/helpers.js +27 -12
  114. package/dist/render/svg/ids.d.ts +6 -0
  115. package/dist/render/svg/ids.js +11 -0
  116. package/dist/render/types.d.ts +4 -3
  117. package/dist/render/types.js +2 -1
  118. package/dist/runtime/factory/input.d.ts +3 -1
  119. package/dist/runtime/factory/input.js +11 -2
  120. package/dist/runtime/factory/main.js +10 -4
  121. package/dist/runtime/input/adapter/event.js +2 -2
  122. package/dist/runtime/input/adapter/pointer.d.ts +2 -2
  123. package/dist/runtime/input/controller/factory.d.ts +1 -1
  124. package/dist/runtime/input/controller/factory.js +35 -15
  125. package/dist/runtime/input/controller/helpers.d.ts +1 -1
  126. package/dist/runtime/input/controller/interaction.d.ts +3 -0
  127. package/dist/runtime/input/controller/interaction.js +14 -0
  128. package/dist/runtime/input/controller/pointer.d.ts +6 -6
  129. package/dist/runtime/input/controller/pointer.js +53 -27
  130. package/dist/runtime/input/controller/transient-visuals.d.ts +2 -3
  131. package/dist/runtime/input/controller/types.d.ts +6 -5
  132. package/dist/runtime/types/main.d.ts +1 -1
  133. package/dist/wrapper/factory.js +2 -7
  134. package/dist/wrapper/types.d.ts +3 -11
  135. package/package.json +1 -3
@@ -0,0 +1,82 @@
1
+ import assert from '@ktarmyshov/assert';
2
+ import { createSvgElement, updateSvgElementAttributes } from '../../../render/svg/helpers.js';
3
+ import { isUpdateContextRenderable } from '../../types/context/update.js';
4
+ import { extensionCreateInternalBase, extensionDestroyBase, extensionMountBase, extensionUnmountBase } from '../common/helpers.js';
5
+ import { EXTENSION_ID, EXTENSION_SLOTS } from './types.js';
6
+ export function createWatermark() {
7
+ return {
8
+ id: EXTENSION_ID,
9
+ slots: EXTENSION_SLOTS,
10
+ createInstance(options) {
11
+ return createWatermarkInstance(options);
12
+ }
13
+ };
14
+ }
15
+ function createWatermarkInternal(options) {
16
+ return {
17
+ ...extensionCreateInternalBase(options),
18
+ svgWatermark: null
19
+ };
20
+ }
21
+ function extensionClean(state) {
22
+ state.svgWatermark = null;
23
+ }
24
+ const watermarkUrl = new URL('../../../../assets/watermark/mirasen.svg', import.meta.url).toString();
25
+ const squareRatio = 0.8; // watermark size relative to square size
26
+ const opacity = '0.3';
27
+ function createWatermarkInstance(options) {
28
+ const internalState = createWatermarkInternal(options);
29
+ return {
30
+ id: EXTENSION_ID,
31
+ mount(env) {
32
+ extensionMountBase(internalState, env.slotRoots);
33
+ },
34
+ onUpdate(context) {
35
+ const needsRender = context.mutation.hasMutation({
36
+ causes: ['layout.refreshGeometry']
37
+ }) && isUpdateContextRenderable(context);
38
+ if (!needsRender) {
39
+ return;
40
+ }
41
+ context.invalidation.markDirty(1 /* DirtyLayer.Watermark */);
42
+ },
43
+ render(context) {
44
+ assert(context.invalidation.dirtyLayers !== 0, 'Render should only be called when there are dirty layers');
45
+ const watermarkSquare = context.currentFrame.state.view.orientation === 0 /* ColorCode.White */ ? 7 : 56;
46
+ const geometry = context.currentFrame.layout.geometry;
47
+ const r = geometry.getSquareRect(watermarkSquare);
48
+ const centerX = r.x + r.width / 2;
49
+ const centerY = r.y + r.height / 2;
50
+ const size = Math.min(r.width, r.height) * squareRatio;
51
+ const x = centerX - size / 2;
52
+ const y = centerY - size / 2;
53
+ const watermarkAttributes = {
54
+ opacity: opacity,
55
+ x: x.toString(),
56
+ y: y.toString(),
57
+ width: size.toString(),
58
+ height: size.toString(),
59
+ href: watermarkUrl,
60
+ 'pointer-events': 'none'
61
+ };
62
+ if (internalState.svgWatermark === null) {
63
+ assert(internalState.slotRoots, 'Slot roots should be available when render is called');
64
+ internalState.svgWatermark = createSvgElement(internalState.slotRoots.board, 'image', {
65
+ 'data-chessboard-id': 'watermark',
66
+ ...watermarkAttributes
67
+ });
68
+ }
69
+ else {
70
+ updateSvgElementAttributes(internalState.svgWatermark, watermarkAttributes);
71
+ }
72
+ },
73
+ unmount() {
74
+ extensionUnmountBase(internalState);
75
+ extensionClean(internalState);
76
+ },
77
+ destroy() {
78
+ extensionDestroyBase(internalState);
79
+ extensionClean(internalState);
80
+ }
81
+ };
82
+ }
@@ -0,0 +1,13 @@
1
+ import { ExtensionDefinition, ExtensionInstance } from '../../types/extension.js';
2
+ import { ExtensionInternalBase } from '../common/types.js';
3
+ export declare const EXTENSION_SLOTS: readonly ["board"];
4
+ export type ExtensionSlotsType = typeof EXTENSION_SLOTS;
5
+ export declare const EXTENSION_ID: "watermark";
6
+ export type WatermarkDefinition = ExtensionDefinition<typeof EXTENSION_ID, typeof EXTENSION_SLOTS, never>;
7
+ export type WatermarkInstance = ExtensionInstance<typeof EXTENSION_ID, typeof EXTENSION_SLOTS, never>;
8
+ export interface WatermarkInstanceInternal extends ExtensionInternalBase<ExtensionSlotsType> {
9
+ svgWatermark: SVGImageElement | null;
10
+ }
11
+ export declare const enum DirtyLayer {
12
+ Watermark = 1
13
+ }
@@ -0,0 +1,2 @@
1
+ export const EXTENSION_SLOTS = ['board'];
2
+ export const EXTENSION_ID = 'watermark';
@@ -1,7 +1,7 @@
1
- import { Square } from '../../../state/board/types/internal.js';
2
- import { ScenePoint } from '../basic/transient-visuals.js';
1
+ import type { Square } from '../../../state/board/types/internal.js';
2
+ import type { ScenePoint } from '../basic/transient-visuals.js';
3
3
  export interface ScenePointerEvent {
4
- readonly type: 'pointercancel' | 'pointerdown' | 'pointerenter' | 'pointerleave' | 'pointermove' | 'pointerout' | 'pointerover' | 'pointerrawupdate' | 'pointerup';
4
+ readonly type: 'pointercancel' | 'pointerdown' | 'pointerenter' | 'pointerleave' | 'pointermove' | 'pointerout' | 'pointerover' | 'pointerrawupdate' | 'pointerup' | 'contextmenu';
5
5
  readonly point: ScenePoint;
6
6
  readonly clampedPoint: ScenePoint;
7
7
  readonly boardClampedPoint: ScenePoint | null;
@@ -9,3 +9,32 @@ export interface ScenePointerEvent {
9
9
  }
10
10
  export declare const ALL_SCENE_POINTER_EVENT_TYPES: ReadonlySet<string>;
11
11
  export type SceneEvent = ScenePointerEvent;
12
+ export type RuntimeInteractionActionStartLiftedDrag = {
13
+ type: 'startLiftedDrag';
14
+ source: Square;
15
+ target: Square;
16
+ };
17
+ export type RuntimeInteractionActionStartReleaseTargetingDrag = {
18
+ type: 'startReleaseTargetingDrag';
19
+ source: Square;
20
+ target: Square;
21
+ };
22
+ export type RuntimeInteractionActionCompleteCoreDrag = {
23
+ type: 'completeCoreDragTo';
24
+ target: Square;
25
+ };
26
+ export type RuntimeInteractionActionCompleteExtensionDrag = {
27
+ type: 'completeExtensionDrag';
28
+ target: Square | null;
29
+ };
30
+ export type RuntimeInteractionActionUpdateDragSessionCurrentTarget = {
31
+ type: 'updateDragSessionCurrentTarget';
32
+ target: Square | null;
33
+ };
34
+ export type RuntimeInteractionActionCancelActiveInteraction = {
35
+ type: 'cancelActiveInteraction';
36
+ };
37
+ export type RuntimeInteractionActionCancelInteraction = {
38
+ type: 'cancelInteraction';
39
+ };
40
+ export type RuntimeInteractionAction = RuntimeInteractionActionStartLiftedDrag | RuntimeInteractionActionStartReleaseTargetingDrag | RuntimeInteractionActionCompleteCoreDrag | RuntimeInteractionActionCompleteExtensionDrag | RuntimeInteractionActionUpdateDragSessionCurrentTarget | RuntimeInteractionActionCancelActiveInteraction | RuntimeInteractionActionCancelInteraction;
@@ -7,5 +7,6 @@ export const ALL_SCENE_POINTER_EVENT_TYPES = new Set([
7
7
  'pointerout',
8
8
  'pointerover',
9
9
  'pointerrawupdate',
10
- 'pointerup'
10
+ 'pointerup',
11
+ 'contextmenu'
11
12
  ]);
@@ -1,6 +1,9 @@
1
- export type ExtensionSlotName = 'defs' | 'board' | 'coordinates' | 'underPieces' | 'pieces' | 'overPieces' | 'animation' | 'underDrag' | 'drag' | 'overDrag';
2
1
  export declare const ALL_EXTENSION_SLOTS: readonly ["defs", "board", "coordinates", "underPieces", "pieces", "overPieces", "animation", "underDrag", "drag", "overDrag"];
3
- export type ExtensionSlotSvgRoots<TSlots extends readonly ExtensionSlotName[]> = Readonly<Record<TSlots[number], SVGGElement>>;
2
+ export type ExtensionSlotName = (typeof ALL_EXTENSION_SLOTS)[number];
3
+ export type ExtensionSlotSvgRoot<TSlot extends ExtensionSlotName> = TSlot extends 'defs' ? SVGDefsElement : SVGGElement;
4
+ export type ExtensionSlotSvgRoots<TSlots extends readonly ExtensionSlotName[]> = Readonly<{
5
+ [TSlot in TSlots[number]]: ExtensionSlotSvgRoot<TSlot>;
6
+ }>;
4
7
  export type ExtensionAllocatedSlotsInternal = Partial<ExtensionSlotSvgRoots<readonly ExtensionSlotName[]>>;
5
8
  export interface ExtensionInstanceMountOptions<TSlots extends readonly ExtensionSlotName[]> {
6
9
  slotRoots: ExtensionSlotSvgRoots<TSlots>;
@@ -10,5 +10,3 @@ export const ALL_EXTENSION_SLOTS = [
10
10
  'drag',
11
11
  'overDrag'
12
12
  ];
13
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
14
- const _checkAllExtensionSlotsExact = true;
@@ -1,4 +1,4 @@
1
- import { Square } from '../../../state/board/types/internal.js';
1
+ import type { Square } from '../../../state/board/types/internal.js';
2
2
  export interface ScenePoint {
3
3
  readonly x: number;
4
4
  readonly y: number;
@@ -1,6 +1,6 @@
1
- import { ExtensionInvalidationState } from '../../invalidation/types.js';
2
- import { ExtensionAnimationSession } from '../basic/animation.js';
3
- import { ExtensionRenderContext } from './render.js';
1
+ import type { ExtensionInvalidationState } from '../../invalidation/types.js';
2
+ import type { ExtensionAnimationSession } from '../basic/animation.js';
3
+ import type { ExtensionRenderContext } from './render.js';
4
4
  export interface ExtensionSubmittedAnimationSession extends ExtensionAnimationSession {
5
5
  readonly status: 'submitted';
6
6
  }
@@ -1,5 +1,6 @@
1
- import { SceneEvent } from '../basic/events.js';
1
+ import type { RuntimeInteractionAction, SceneEvent } from '../basic/events.js';
2
2
  export interface ExtensionOnEventContext {
3
3
  rawEvent: Event;
4
4
  sceneEvent: SceneEvent | null;
5
+ runtimeInteractionActionPreview: RuntimeInteractionAction | null;
5
6
  }
@@ -1,11 +1,12 @@
1
- import { RuntimeReadonlyMutationSession } from '../../../runtime/mutation/types.js';
2
- import { ExtensionInvalidationState } from '../../invalidation/types.js';
3
- import { RenderFrameSnapshot } from '../basic/render.js';
4
- import { UpdateFrameSnapshot, UpdateFrameSnapshotMounted, UpdateFrameSnapshotUnmounted } from '../basic/update.js';
1
+ import type { RuntimeReadonlyMutationSession } from '../../../runtime/mutation/types.js';
2
+ import type { ExtensionInvalidationState } from '../../invalidation/types.js';
3
+ import type { RenderFrameSnapshot } from '../basic/render.js';
4
+ import type { UpdateFrameSnapshot, UpdateFrameSnapshotMounted, UpdateFrameSnapshotUnmounted } from '../basic/update.js';
5
5
  export interface ExtensionUpdateContextCommon {
6
6
  readonly previousFrame: UpdateFrameSnapshot | null;
7
7
  readonly mutation: RuntimeReadonlyMutationSession;
8
8
  readonly currentFrame: UpdateFrameSnapshot;
9
+ readonly invalidation: ExtensionInvalidationState;
9
10
  }
10
11
  export interface ExtensionUpdateContextCommonUnmounted extends ExtensionUpdateContextCommon {
11
12
  readonly currentFrame: UpdateFrameSnapshotUnmounted;
@@ -14,9 +15,7 @@ export interface ExtensionUpdateContextCommonMounted extends ExtensionUpdateCont
14
15
  readonly currentFrame: UpdateFrameSnapshotMounted;
15
16
  }
16
17
  export type ExtensionUpdateContextUnmounted = ExtensionUpdateContextCommonUnmounted;
17
- export interface ExtensionUpdateContextMounted extends ExtensionUpdateContextCommonMounted {
18
- readonly invalidation: ExtensionInvalidationState;
19
- }
18
+ export type ExtensionUpdateContextMounted = ExtensionUpdateContextCommonMounted;
20
19
  export type ExtensionUpdateContext = ExtensionUpdateContextUnmounted | ExtensionUpdateContextMounted;
21
20
  export declare function isUpdateContextCommonMounted<T extends ExtensionUpdateContextCommon>(context: T): context is T & ExtensionUpdateContextCommonMounted;
22
21
  export declare function isUpdateContextMounted<T extends ExtensionUpdateContext>(context: T): context is T & ExtensionUpdateContextMounted;
@@ -1,3 +1,4 @@
1
+ import type { SvgIdResolver } from '../../render/svg/ids.js';
1
2
  import { ExtensionDragSessionSnapshot } from './basic/interaction.js';
2
3
  import { ExtensionInstanceMountOptions, ExtensionSlotName } from './basic/mount.js';
3
4
  import { ExtensionAnimationFinishedContext, ExtensionCleanAnimationContext, ExtensionPrepareAnimationContext, ExtensionRenderAnimationContext } from './context/animation.js';
@@ -19,6 +20,7 @@ interface ExtensionInstanceBase<TId extends string, TSlots extends readonly Exte
19
20
  onAnimationFinished?(context: ExtensionAnimationFinishedContext): void;
20
21
  cleanAnimation?(context: ExtensionCleanAnimationContext): void;
21
22
  completeDrag?(session: ExtensionDragSessionSnapshot): void;
23
+ cancelDrag?(session: ExtensionDragSessionSnapshot): void;
22
24
  renderTransientVisuals?(context: ExtensionRenderTransientVisualsContext): void;
23
25
  onUIMoveRequest?(context: ExtensionPendingUIMoveRequestContext): void;
24
26
  onEvent?(context: ExtensionOnEventContext): void;
@@ -30,6 +32,7 @@ export type ExtensionInstance<TId extends string, TSlots extends readonly Extens
30
32
  export type AnyExtensionInstance = ExtensionInstance<string, readonly ExtensionSlotName[], unknown>;
31
33
  export interface ExtensionCreateInstanceOptions {
32
34
  runtimeSurface: ExtensionRuntimeSurface;
35
+ svgIds: SvgIdResolver;
33
36
  }
34
37
  export interface ExtensionDefinition<TId extends string, TSlots extends readonly ExtensionSlotName[], TPublic> {
35
38
  readonly id: TId;
@@ -43,6 +43,7 @@ export interface ExtensionSystem {
43
43
  onUIMoveRequest(context: ExtensionPendingUIMoveRequestContext): void;
44
44
  onEvent(context: ExtensionOnEventContext): void;
45
45
  completeDrag(session: DragSessionExtensionOwned): void;
46
+ cancelDrag(session: DragSessionExtensionOwned): void;
46
47
  onUnmount(): void;
47
48
  onDestroy(): void;
48
49
  }
@@ -27,7 +27,7 @@ export interface ExtensionRuntimeSurfaceCommands {
27
27
  clearInteraction(): boolean;
28
28
  resolveDeferredUIMoveRequest(details: DeferredUIMoveResolutionDetails): Move;
29
29
  cancelDeferredUIMoveRequest(): boolean;
30
- requestRender(request: ExtensionRuntimeSurfaceCommandsRenderRequest): void;
30
+ requestRender(request: ExtensionRuntimeSurfaceCommandsRenderRequest): boolean;
31
31
  getSnapshot(): ExtensionRuntimeSurfaceCommandsSnapshot;
32
32
  }
33
33
  export interface ExtensionRuntimeSurfaceCommandsInternalSurface extends ExtensionRuntimeSurfaceCommands {
@@ -1,10 +1,12 @@
1
- import { ExtensionAnimationController } from '../basic/animation.js';
2
- import { ExtensionRuntimeSurfaceCommands } from './commands.js';
3
- import { ExtensionRuntimeSurfaceEvents } from './events.js';
4
- import { ExtensionRuntimeSurfaceTransientVisuals } from './transient-visuals.js';
1
+ import type { ExtensionInvalidationState } from '../../invalidation/types.js';
2
+ import type { ExtensionAnimationController } from '../basic/animation.js';
3
+ import type { ExtensionRuntimeSurfaceCommands } from './commands.js';
4
+ import type { ExtensionRuntimeSurfaceEvents } from './events.js';
5
+ import type { ExtensionRuntimeSurfaceTransientVisuals } from './transient-visuals.js';
5
6
  export interface ExtensionRuntimeSurface {
6
7
  readonly commands: ExtensionRuntimeSurfaceCommands;
7
8
  readonly animation: ExtensionAnimationController;
8
9
  readonly events: ExtensionRuntimeSurfaceEvents;
9
10
  readonly transientVisuals: ExtensionRuntimeSurfaceTransientVisuals;
11
+ readonly invalidation: ExtensionInvalidationState;
10
12
  }
@@ -1,4 +1,5 @@
1
1
  import { createActiveTarget } from '../first-party/active-target/factory.js';
2
+ import { createAnnotations } from '../first-party/annotations/factory.js';
2
3
  import { createAutoPromote } from '../first-party/auto-promote/factory.js';
3
4
  import { createBoardEvents } from '../first-party/board-events/factory.js';
4
5
  import { createLastMove } from '../first-party/last-move/factory.js';
@@ -6,8 +7,10 @@ import { createLegalMoves } from '../first-party/legal-moves/factory.js';
6
7
  import { createMainRenderer } from '../first-party/main-renderer/factory.js';
7
8
  import { createPromotion } from '../first-party/promotion/factory.js';
8
9
  import { createSelectedSquare } from '../first-party/selected-square/factory.js';
10
+ import { createWatermark } from '../first-party/watermark/factory.js';
9
11
  export declare const builtInExtensionFactoryMap: {
10
12
  renderer: typeof createMainRenderer;
13
+ watermark: typeof createWatermark;
11
14
  selectedSquare: typeof createSelectedSquare;
12
15
  lastMove: typeof createLastMove;
13
16
  activeTarget: typeof createActiveTarget;
@@ -15,10 +18,11 @@ export declare const builtInExtensionFactoryMap: {
15
18
  events: typeof createBoardEvents;
16
19
  autoPromote: typeof createAutoPromote;
17
20
  promotion: typeof createPromotion;
21
+ annotations: typeof createAnnotations;
18
22
  };
19
23
  export type BuiltInExtensionId = keyof typeof builtInExtensionFactoryMap;
20
24
  export type BuiltInExtensionDefinitionMap = {
21
25
  [Id in BuiltInExtensionId]: ReturnType<(typeof builtInExtensionFactoryMap)[Id]>;
22
26
  };
23
- export declare const DefaultBuiltinChessboardExtensions: readonly ["renderer", "selectedSquare", "lastMove", "activeTarget", "legalMoves", "events", "autoPromote", "promotion"];
27
+ export declare const DefaultBuiltinChessboardExtensions: readonly ["renderer", "watermark", "selectedSquare", "lastMove", "activeTarget", "legalMoves", "events", "autoPromote", "promotion", "annotations"];
24
28
  export type DefaultBuiltinChessboardExtensions = typeof DefaultBuiltinChessboardExtensions;
@@ -1,5 +1,7 @@
1
1
  import { createActiveTarget } from '../first-party/active-target/factory.js';
2
2
  import { EXTENSION_ID as EXTENSION_ID_ACTIVE_TARGET } from '../first-party/active-target/types.js';
3
+ import { createAnnotations } from '../first-party/annotations/factory.js';
4
+ import { EXTENSION_ID as EXTENSION_ID_ANNOTATIONS } from '../first-party/annotations/types/main.js';
3
5
  import { createAutoPromote } from '../first-party/auto-promote/factory.js';
4
6
  import { EXTENSION_ID as EXTENSION_ID_AUTO_PROMOTE } from '../first-party/auto-promote/types.js';
5
7
  import { createBoardEvents } from '../first-party/board-events/factory.js';
@@ -14,23 +16,29 @@ import { createPromotion } from '../first-party/promotion/factory.js';
14
16
  import { EXTENSION_ID as EXTENSION_ID_PROMOTION } from '../first-party/promotion/types/main.js';
15
17
  import { createSelectedSquare } from '../first-party/selected-square/factory.js';
16
18
  import { EXTENSION_ID as EXTENSION_ID_SELECTED_SQUARE } from '../first-party/selected-square/types.js';
19
+ import { createWatermark } from '../first-party/watermark/factory.js';
20
+ import { EXTENSION_ID as EXTENSION_ID_WATERMARK } from '../first-party/watermark/types.js';
17
21
  export const builtInExtensionFactoryMap = {
18
22
  [EXTENSION_ID_RENDERER]: createMainRenderer,
23
+ [EXTENSION_ID_WATERMARK]: createWatermark,
19
24
  [EXTENSION_ID_SELECTED_SQUARE]: createSelectedSquare,
20
25
  [EXTENSION_ID_LAST_MOVE]: createLastMove,
21
26
  [EXTENSION_ID_ACTIVE_TARGET]: createActiveTarget,
22
27
  [EXTENSION_ID_LEGAL_MOVES]: createLegalMoves,
23
28
  [EXTENSION_ID_BOARD_EVENTS]: createBoardEvents,
24
29
  [EXTENSION_ID_AUTO_PROMOTE]: createAutoPromote,
25
- [EXTENSION_ID_PROMOTION]: createPromotion
30
+ [EXTENSION_ID_PROMOTION]: createPromotion,
31
+ [EXTENSION_ID_ANNOTATIONS]: createAnnotations
26
32
  };
27
33
  export const DefaultBuiltinChessboardExtensions = [
28
34
  EXTENSION_ID_RENDERER,
35
+ EXTENSION_ID_WATERMARK,
29
36
  EXTENSION_ID_SELECTED_SQUARE,
30
37
  EXTENSION_ID_LAST_MOVE,
31
38
  EXTENSION_ID_ACTIVE_TARGET,
32
39
  EXTENSION_ID_LEGAL_MOVES,
33
40
  EXTENSION_ID_BOARD_EVENTS,
34
41
  EXTENSION_ID_AUTO_PROMOTE,
35
- EXTENSION_ID_PROMOTION
42
+ EXTENSION_ID_PROMOTION,
43
+ EXTENSION_ID_ANNOTATIONS
36
44
  ];
@@ -1,21 +1,17 @@
1
- import { isUpdateContextCommonMounted } from './types/context/update.js';
2
1
  export function extensionSystemUpdateState(state, request) {
3
2
  // Prepare base context
4
- const contextCommon = {
3
+ const contextCommonPart = {
5
4
  previousFrame: state.currentFrame,
6
5
  mutation: request.mutation,
7
6
  currentFrame: request.state
8
7
  };
9
8
  // Update invalidation state based on the new request
10
9
  for (const extension of state.extensions.values()) {
11
- const context = isUpdateContextCommonMounted(contextCommon)
12
- ? {
13
- ...contextCommon,
14
- invalidation: extension.invalidation
15
- }
16
- : {
17
- ...contextCommon
18
- };
10
+ const contextCommon = {
11
+ ...contextCommonPart,
12
+ invalidation: extension.invalidation
13
+ };
14
+ const context = contextCommon;
19
15
  extension.instance.onUpdate?.(context);
20
16
  }
21
17
  state.currentFrame = request.state;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- export type { MoveOutput } from './extensions/build/index.js';
2
1
  export type { ColorInput, MoveRequestInput, PiecePositionRecordString, PositionInput } from './state/board/types/input.js';
2
+ export type { MoveOutput } from './state/board/types/output.js';
3
3
  export type { MovabilityInput, MoveDestinationInput } from './state/interaction/types/input.js';
4
4
  export { createBoard } from './wrapper/factory.js';
5
5
  export type { Chessboard, ChessboardExtensionInput, ChessboardInitOptions } from './wrapper/types.js';
@@ -1,5 +1,5 @@
1
1
  import { validateIsMounted, validateIsNotMounted } from './rendering/helpers.js';
2
- import { clearElementChildren } from './svg/helpers.js';
2
+ import { clearSvgElementChildren } from './svg/helpers.js';
3
3
  export function renderMount(state, element) {
4
4
  validateIsNotMounted(state);
5
5
  state.container = element;
@@ -16,8 +16,8 @@ export function renderUnmount(state) {
16
16
  for (const extensionRec of state.extensions.values()) {
17
17
  extensionRec.extension.instance.unmount?.();
18
18
  // clear the slot roots
19
- for (const slotRoot of Object.values(extensionRec.render.slots)) {
20
- clearElementChildren(slotRoot);
19
+ for (const [, slotRoot] of Object.entries(extensionRec.render.slots)) {
20
+ clearSvgElementChildren(slotRoot);
21
21
  }
22
22
  }
23
23
  // remove our svg root from the container
@@ -1,5 +1,5 @@
1
1
  import { sceneSizesEqual } from '../../layout/geometry/helpers.js';
2
- import { updateElementAttributes } from '../svg/helpers.js';
2
+ import { updateSvgElementAttributes } from '../svg/helpers.js';
3
3
  import { validateIsMounted } from './helpers.js';
4
4
  export function checkNeedsRender(state) {
5
5
  for (const extensionRec of state.extensions.values()) {
@@ -20,7 +20,7 @@ export function performRenderPass(state, request) {
20
20
  const currentSize = request.layout.geometry.sceneSize;
21
21
  const prevSize = state.currentFrame?.layout.geometry?.sceneSize ?? null;
22
22
  if (!sceneSizesEqual(currentSize, prevSize)) {
23
- updateElementAttributes(state.svgRoots.svgRoot, {
23
+ updateSvgElementAttributes(state.svgRoots.svgRoot, {
24
24
  width: currentSize.width.toString(),
25
25
  height: currentSize.height.toString(),
26
26
  viewBox: `0 0 ${currentSize.width} ${currentSize.height}`
@@ -1,43 +1,58 @@
1
- import { createSvgElement } from './helpers.js';
1
+ import { createSvgDefsElement, createSvgElement, createSvgRootElement } from './helpers.js';
2
2
  export function createSvgRoots(options) {
3
- const { doc } = options;
4
- const svgRoot = createSvgElement(doc, 'svg', { 'data-chessboard-id': 'svg-root' });
3
+ const { element } = options;
4
+ const svgRoot = createSvgRootElement(element, { 'data-chessboard-id': 'svg-root' });
5
5
  svgRoot.style.setProperty('user-select', 'none');
6
6
  svgRoot.style.setProperty('-webkit-user-select', 'none');
7
7
  svgRoot.style.setProperty('touch-action', 'pinch-zoom');
8
8
  svgRoot.style.setProperty('-webkit-touch-callout', 'none');
9
9
  svgRoot.style.setProperty('-webkit-tap-highlight-color', 'transparent');
10
+ // Create children in the correct order
11
+ const board = createSvgElement(svgRoot, 'g', { 'data-chessboard-id': 'board-root' });
12
+ const coordinates = createSvgElement(svgRoot, 'g', {
13
+ 'data-chessboard-id': 'coordinates-root'
14
+ });
15
+ const underPieces = createSvgElement(svgRoot, 'g', {
16
+ 'data-chessboard-id': 'under-pieces-root'
17
+ });
18
+ const pieces = createSvgElement(svgRoot, 'g', { 'data-chessboard-id': 'pieces-root' });
19
+ const overPieces = createSvgElement(svgRoot, 'g', {
20
+ 'data-chessboard-id': 'over-pieces-root'
21
+ });
22
+ const animation = createSvgElement(svgRoot, 'g', {
23
+ 'data-chessboard-id': 'animation-root'
24
+ });
25
+ const underDrag = createSvgElement(svgRoot, 'g', {
26
+ 'data-chessboard-id': 'under-drag-root'
27
+ });
28
+ const drag = createSvgElement(svgRoot, 'g', { 'data-chessboard-id': 'drag-root' });
29
+ const overDrag = createSvgElement(svgRoot, 'g', {
30
+ 'data-chessboard-id': 'over-drag-root'
31
+ });
10
32
  const result = {
11
33
  svgRoot,
12
- defs: createSvgElement(doc, 'defs', { 'data-chessboard-id': 'defs-root' }),
13
- board: createSvgElement(doc, 'g', { 'data-chessboard-id': 'board-root' }),
14
- coordinates: createSvgElement(doc, 'g', { 'data-chessboard-id': 'coordinates-root' }),
15
- underPieces: createSvgElement(doc, 'g', { 'data-chessboard-id': 'under-pieces-root' }),
16
- pieces: createSvgElement(doc, 'g', { 'data-chessboard-id': 'pieces-root' }),
17
- overPieces: createSvgElement(doc, 'g', { 'data-chessboard-id': 'over-pieces-root' }),
18
- animation: createSvgElement(doc, 'g', { 'data-chessboard-id': 'animation-root' }),
19
- underDrag: createSvgElement(doc, 'g', { 'data-chessboard-id': 'under-drag-root' }),
20
- drag: createSvgElement(doc, 'g', { 'data-chessboard-id': 'drag-root' }),
21
- overDrag: createSvgElement(doc, 'g', { 'data-chessboard-id': 'over-drag-root' })
34
+ board,
35
+ coordinates,
36
+ underPieces,
37
+ pieces,
38
+ overPieces,
39
+ animation,
40
+ underDrag,
41
+ drag,
42
+ overDrag
22
43
  };
23
- // Attach layers in the correct order
24
- svgRoot.appendChild(result.defs);
25
- svgRoot.appendChild(result.board);
26
- svgRoot.appendChild(result.coordinates);
27
- svgRoot.appendChild(result.underPieces);
28
- svgRoot.appendChild(result.pieces);
29
- svgRoot.appendChild(result.overPieces);
30
- svgRoot.appendChild(result.animation);
31
- svgRoot.appendChild(result.underDrag);
32
- svgRoot.appendChild(result.drag);
33
- svgRoot.appendChild(result.overDrag);
34
44
  return result;
35
45
  }
36
46
  export function allocateExtensionSlotRoots(svgRoots, extensionId, slots) {
37
47
  const result = {};
38
48
  for (const slot of slots) {
39
49
  const id = `extension-slot-root-${slot}-${extensionId}`;
40
- result[slot] = createSvgElement(svgRoots[slot], 'g', { 'data-chessboard-id': id });
50
+ if (slot === 'defs') {
51
+ result[slot] = createSvgDefsElement(svgRoots.svgRoot, { 'data-chessboard-id': id });
52
+ }
53
+ else {
54
+ result[slot] = createSvgElement(svgRoots[slot], 'g', { 'data-chessboard-id': id });
55
+ }
41
56
  }
42
57
  return result;
43
58
  }
@@ -9,10 +9,14 @@ export declare const SVG_NS = "http://www.w3.org/2000/svg";
9
9
  export declare function isLightSquare(sq: Square): boolean;
10
10
  export type SvgElementNames = keyof SVGElementTagNameMap;
11
11
  export type SvgElementAttributes = Record<string, string>;
12
- export type SvgElementOtherAttributes = SvgElementAttributes;
13
12
  export type SvgElementWithIdAttributes = SvgElementAttributes & {
14
13
  'data-chessboard-id': string;
15
14
  };
16
- export declare function createSvgElement<K extends SvgElementNames>(parentOrDoc: Element | Document, name: K, attrs: SvgElementWithIdAttributes): SVGElementTagNameMap[K];
17
- export declare function updateElementAttributes(element: Element, attrs: SvgElementOtherAttributes): void;
18
- export declare function clearElementChildren(element: Element): void;
15
+ export type SvgElementOtherAttributes = SvgElementAttributes & {
16
+ 'data-chessboard-id'?: never;
17
+ };
18
+ export declare function createSvgRootElement(parent: HTMLElement, attrs: SvgElementWithIdAttributes): SVGSVGElement;
19
+ export declare function createSvgDefsElement(parent: SVGSVGElement, attrs: SvgElementWithIdAttributes): SVGDefsElement;
20
+ export declare function createSvgElement<K extends Exclude<SvgElementNames, 'svg' | 'defs'>>(parent: SVGElement, name: K, attrs: SvgElementWithIdAttributes): SVGElementTagNameMap[K];
21
+ export declare function updateSvgElementAttributes(element: SVGElement, attrs: SvgElementOtherAttributes): void;
22
+ export declare function clearSvgElementChildren(element: SVGElement): void;
@@ -11,23 +11,38 @@ export function isLightSquare(sq) {
11
11
  const r = rankOf(sq);
12
12
  return ((f + r) & 1) === 1;
13
13
  }
14
- export function createSvgElement(parentOrDoc, name, attrs) {
15
- const doc = parentOrDoc instanceof Document ? parentOrDoc : parentOrDoc.ownerDocument;
16
- const el = doc.createElementNS(SVG_NS, name);
14
+ function _updateElementAttributes(element, attrs) {
17
15
  for (const [key, value] of Object.entries(attrs)) {
18
- el.setAttribute(key, value);
19
- }
20
- if (parentOrDoc instanceof Element) {
21
- parentOrDoc.appendChild(el);
16
+ element.setAttribute(key, value);
22
17
  }
18
+ }
19
+ export function createSvgRootElement(parent, attrs) {
20
+ const doc = parent.ownerDocument;
21
+ const el = doc.createElementNS(SVG_NS, 'svg');
22
+ _updateElementAttributes(el, attrs);
23
+ parent.appendChild(el);
23
24
  return el;
24
25
  }
25
- export function updateElementAttributes(element, attrs) {
26
- for (const [key, value] of Object.entries(attrs)) {
27
- element.setAttribute(key, value);
28
- }
26
+ export function createSvgDefsElement(parent, attrs) {
27
+ const doc = parent.ownerDocument;
28
+ const el = doc.createElementNS(SVG_NS, 'defs');
29
+ _updateElementAttributes(el, attrs);
30
+ // Keep extension-owned defs roots before visual roots.
31
+ const firstNonDefsChild = Array.from(parent.children).find((child) => child.tagName.toLowerCase() !== 'defs');
32
+ parent.insertBefore(el, firstNonDefsChild ?? null);
33
+ return el;
34
+ }
35
+ export function createSvgElement(parent, name, attrs) {
36
+ const doc = parent.ownerDocument;
37
+ const el = doc.createElementNS(SVG_NS, name);
38
+ _updateElementAttributes(el, attrs);
39
+ parent.appendChild(el);
40
+ return el;
41
+ }
42
+ export function updateSvgElementAttributes(element, attrs) {
43
+ _updateElementAttributes(element, attrs);
29
44
  }
30
- export function clearElementChildren(element) {
45
+ export function clearSvgElementChildren(element) {
31
46
  while (element.firstChild) {
32
47
  element.removeChild(element.firstChild);
33
48
  }
@@ -0,0 +1,6 @@
1
+ export interface SvgIdResolver {
2
+ readonly prefix: string;
3
+ makeId(scope: string, localId: string): string;
4
+ makeHref(scope: string, localId: string): string;
5
+ }
6
+ export declare function createSvgIdResolver(): SvgIdResolver;
@@ -0,0 +1,11 @@
1
+ let nextId = 0;
2
+ export function createSvgIdResolver() {
3
+ const prefix = `cb${nextId++}`;
4
+ function makeId(scope, localId) {
5
+ return `${prefix}-${scope}-${localId}`;
6
+ }
7
+ function makeHref(scope, localId) {
8
+ return `#${makeId(scope, localId)}`;
9
+ }
10
+ return { prefix, makeId, makeHref };
11
+ }
@@ -3,9 +3,10 @@ import { RenderFrameSnapshot } from '../extensions/types/basic/render.js';
3
3
  import { TransientInput } from '../extensions/types/basic/transient-visuals.js';
4
4
  import { ExtensionSystemExtensionRecord, ExtensionSystemSharedDataForRenderSystem } from '../extensions/types/main.js';
5
5
  import { Scheduler } from './scheduler/types.js';
6
- export interface SvgRoots extends ExtensionSlotSvgRoots<typeof ALL_EXTENSION_SLOTS> {
6
+ export type RootExtensionSlotName = Exclude<(typeof ALL_EXTENSION_SLOTS)[number], 'defs'>;
7
+ export declare const ROOT_EXTENSION_SLOTS: readonly RootExtensionSlotName[];
8
+ export interface SvgRoots extends ExtensionSlotSvgRoots<typeof ROOT_EXTENSION_SLOTS> {
7
9
  readonly svgRoot: SVGSVGElement;
8
- readonly defs: SVGDefsElement;
9
10
  }
10
11
  export interface RenderExtensionRecordRender {
11
12
  readonly slots: ExtensionAllocatedSlotsInternal;
@@ -24,7 +25,7 @@ export interface RenderSystemInternal {
24
25
  readonly transientVisualsSubscribers: ReadonlySet<string>;
25
26
  }
26
27
  export interface RenderSystemInitOptions {
27
- doc: Document;
28
+ element: HTMLElement;
28
29
  sharedDataFromExtensionSystem: ExtensionSystemSharedDataForRenderSystem;
29
30
  }
30
31
  export interface RenderSystemInitOptionsInternal extends RenderSystemInitOptions {