Merge pull request #23492 from davidmpaz/chessboardjs

Add types definitions for chessboardjs
This commit is contained in:
Daniel Rosenwasser
2018-02-13 11:19:47 -08:00
committed by GitHub
4 changed files with 202 additions and 0 deletions
+92
View File
@@ -0,0 +1,92 @@
import chessboardjs = require('chessboardjs');
/**
* Basic Usage
*/
// empty position
let board = chessboardjs.ChessBoard('board');
// Start position
board = chessboardjs.ChessBoard('board', 'start');
// FEN string
const ruyLopez = 'r1bqkbnr/pppp1ppp/2n5/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R';
board = chessboardjs.ChessBoard('board', ruyLopez);
// Position Object
let position: chessboardjs.BoardPositionType = {
d6: chessboardjs.Piece.bK,
d4: chessboardjs.Piece.wP,
e4: chessboardjs.Piece.wK
};
board = chessboardjs.ChessBoard('board', position);
// Multiple boards
const config1: chessboardjs.BoardConfig = {
position: 'start',
showNotation: false
};
const board1 = chessboardjs.ChessBoard('board1', config1);
const board2 = chessboardjs.ChessBoard('board2', {
position: 'r1bqkbnr/pppp1ppp/2n5/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R',
showNotation: false
});
const board3 = chessboardjs.ChessBoard('board3', {
position: 'r1k4r/p2nb1p1/2b4p/1p1n1p2/2PP4/3Q1NB1/1P3PPP/R5K1',
showNotation: false
});
/**
* Methods
*/
// Get Position
const cfg: chessboardjs.BoardConfig = {
draggable: true,
position: 'start'
};
board = chessboardjs.ChessBoard('board', cfg);
const pos: chessboardjs.BoardPositionType = board.position();
const posFEN: string = board.position('fen');
// Set position
// FEN
board.position('r1k4r/p2nb1p1/2b4p/1p1n1p2/2PP4/3Q1NB1/1P3PPP/R5K1');
// animated
board.position(ruyLopez, true);
// instantly
board.position(ruyLopez, false);
position = {
a4: chessboardjs.Piece.bK,
c4: chessboardjs.Piece.wK,
a7: chessboardjs.Piece.wR
};
board.position(position);
// Clear
board.clear(false);
// Move
board.start();
board.move('e2-e4');
// Orientation
const oritentation = board.orientation();
// change orientation
board.orientation('black');
// Recalculates board and square sizes based on the parent element and redraws
// the board accordingly.
board.resize();
// Destroy
board.destroy();
+82
View File
@@ -0,0 +1,82 @@
// Type definitions for chessboardjs 0.3
// Project: https://github.com/oakmac/chessboardjs/
// Definitions by: Bayo Olatunji <https://github.com/sliverb>
// David Paz <https://github.com/davidmpaz>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.4
export enum Square {
a8 = 'a8', b8 = 'b8', c8 = 'c8', d8 = 'd8', e8 = 'e8', f8 = 'f8', g8 = 'g7', h8 = 'h8',
a7 = 'a7', b7 = 'b7', c7 = 'c7', d7 = 'd7', e7 = 'e7', f7 = 'f7', g7 = 'g7', h7 = 'h7',
a6 = 'a6', b6 = 'b6', c6 = 'c6', d6 = 'd6', e6 = 'e6', f6 = 'f6', g6 = 'g7', h6 = 'h6',
a5 = 'a5', b5 = 'b5', c5 = 'c5', d5 = 'd5', e5 = 'e5', f5 = 'f5', g5 = 'g7', h5 = 'h5',
a4 = 'a4', b4 = 'b4', c4 = 'c4', d4 = 'd4', e4 = 'e4', f4 = 'f4', g4 = 'g7', h4 = 'h4',
a3 = 'a3', b3 = 'b3', c3 = 'c3', d3 = 'd3', e3 = 'e3', f3 = 'f3', g3 = 'g7', h3 = 'h3',
a2 = 'a2', b2 = 'b2', c2 = 'c2', d2 = 'd2', e2 = 'e2', f2 = 'f2', g2 = 'g7', h2 = 'h2',
a1 = 'a1', b1 = 'b1', c1 = 'c1', d1 = 'd1', e1 = 'e1', f1 = 'f1', g1 = 'g7', h1 = 'h1',
}
export enum Piece {
bK = 'bK', bQ = 'bQ', bR = 'bR', bN = 'bN', bB = 'bB', bP = 'bP',
wK = 'wK', wQ = 'wQ', wR = 'wR', wN = 'wN', wB = 'wB', wP = 'wP'
}
export type BoardPositionType = {
[P in Square]?: Piece;
};
export type PositionType = 'start' | string | BoardPositionType;
export type PositionFenType = 'fen';
export type SpeedType = 'slow' | 'fast';
export type OrientationFlipType = 'flip';
export type OrientationType = 'white' | 'black';
export type DropOffBoardType = 'snapback' | 'trash';
export type Callback = () => void;
export interface BoardConfig {
onDrop?: Callback;
draggable?: boolean;
onChange?: Callback;
onMoveEnd?: Callback;
onSnapEnd?: Callback;
sparePieces?: boolean;
onDragMove?: Callback;
showNotation?: boolean;
onDragStart?: Callback;
onSnapbackEnd?: Callback;
onMouseoutSquare?: Callback;
onMouseoverSquare?: Callback;
pieceTheme?: string | Callback;
orientation?: OrientationType;
showErrors?: boolean | string | Callback;
moveSpeed?: number | SpeedType;
snapSpeed?: number | SpeedType;
trashSpeed?: number | SpeedType;
dropOffBoard?: DropOffBoardType;
appearSpeed?: number | SpeedType;
snapbackSpeed?: number | SpeedType;
position?: PositionType;
}
export interface ChessBoardInstance {
clear(useAnimation?: boolean): void;
destroy(): void;
fen(): string;
flip(): void;
move(...args: string[]): BoardPositionType;
position(): BoardPositionType;
position(fen: PositionFenType): string;
position(newPosition: PositionType, useAnimation?: boolean): void;
orientation(side?: OrientationType | OrientationFlipType): string;
resize(): void;
start(useAnimation?: boolean): void;
}
export interface ChessBoardFactory {
(containerElOrId: any, config?: BoardConfig): ChessBoardInstance;
(containerElOrId: any, position: string | BoardPositionType): ChessBoardInstance;
fenToObj(fen: string): boolean | BoardPositionType;
objToFen(obj: BoardPositionType): boolean | string;
}
export const ChessBoard: ChessBoardFactory;
+25
View File
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true
},
"files": [
"index.d.ts",
"chessboardjs-tests.ts"
]
}
+3
View File
@@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}