cm-chessboard 7.6.10 → 7.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -78,12 +78,12 @@ examples.
78
78
  Below is the default configuration
79
79
 
80
80
  ```javascript
81
- this.props = {
81
+ this.props = {
82
82
  position: FEN.empty, // set position as fen, use FEN.start or FEN.empty as shortcuts
83
83
  orientation: COLOR.white, // white on bottom
84
84
  responsive: true, // resize the board automatically to the size of the context element
85
85
  language: navigator.language.substring(0, 2).toLowerCase(), // supports "de" and "en" for now, used for pieces naming
86
- assetsUrl: "./assets/", // put all css and sprites in this folder
86
+ assetsUrl: "./assets/", // put all css and sprites in this folder, will be ignored for absolute urls of assets files
87
87
  assetsCache: true, // cache sprites
88
88
  style: {
89
89
  cssClass: "default", // set the css theme of the board, try "green", "blue" or "chess-club"
@@ -92,7 +92,7 @@ this.props = {
92
92
  aspectRatio: 1, // height/width of the board
93
93
  pieces: {
94
94
  type: PIECES_FILE_TYPE.svgSprite, // pieces are in an SVG sprite, no other type supported for now
95
- file: "standard.svg", // the filename of the sprite in `assets/pieces/`
95
+ file: "pieces/standard.svg", // the filename of the sprite in `assetsUrl` or an absolute url like `https://…` or `/…`
96
96
  tileSize: 40 // the tile size in the sprite
97
97
  },
98
98
  animationDuration: 300 // pieces animation duration in milliseconds. Disable all animations with `0`.
@@ -451,10 +451,12 @@ you can call `chessboard.removeArrows(undefined, "e2")` and so on...
451
451
 
452
452
  To get all arrows, call `chessboard.getArrows()` without parameters, as with `removeArrows(type, from, to)`.
453
453
 
454
- ## Usage with React
454
+ ## Usage with JS Frameworks
455
455
 
456
- There exists a ticket from someone who is using cm-chessboard with react:
457
- https://github.com/shaack/cm-chessboard/issues/20
456
+ - Works with **Vue** out of the box
457
+ - Works with **Svelte** out of the box
458
+ - I don't use **React**, but there exists a ticket from someone who is using cm-chessboard with react: https://github.com/shaack/cm-chessboard/issues/20
459
+ - It should work also with **all other JS frameworks**, because cm-chessboard is written in standard ES6 and has **no dependencies**.
458
460
 
459
461
  ## Licenses
460
462
 
@@ -40,7 +40,7 @@
40
40
  const largeBoard = new Chessboard(boardDiv, {
41
41
  position: FEN.start,
42
42
  assetsUrl: "../assets/",
43
- style: {pieces: {file: "staunty.svg"}}
43
+ style: {pieces: {file: "pieces/staunty.svg"}}
44
44
  })
45
45
  const chess = new Chess()
46
46
  for (let i = 0; i < 5000; i++) {
@@ -51,7 +51,7 @@
51
51
  const board = new Chessboard(boardDiv, {
52
52
  position: chess.fen(),
53
53
  assetsUrl: "../assets/",
54
- style: {pieces: {file: "staunty.svg"}}
54
+ style: {pieces: {file: "pieces/staunty.svg"}}
55
55
  })
56
56
  makeRandomMove()
57
57
  largeBoard.setPosition(chess.fen(), true)
@@ -31,7 +31,7 @@
31
31
  cssClass: "green",
32
32
  borderType: BORDER_TYPE.frame,
33
33
  pieces: {
34
- file: "staunty.svg"
34
+ file: "pieces/staunty.svg"
35
35
  }
36
36
  }
37
37
  })
@@ -51,7 +51,7 @@
51
51
  style: {
52
52
  cssClass: "chess-club",
53
53
  borderType: BORDER_TYPE.frame,
54
- pieces: {file: "staunty.svg"}
54
+ pieces: {file: "pieces/staunty.svg"}
55
55
  }
56
56
  })
57
57
  new Chessboard(document.getElementById("board4"), {
@@ -68,14 +68,14 @@
68
68
  position: FEN.start,
69
69
  assetsUrl: "../assets/",
70
70
  assetsCache: false,
71
- style: {borderType: BORDER_TYPE.none, pieces: {file: "staunty.svg"}},
71
+ style: {borderType: BORDER_TYPE.none, pieces: {file: "pieces/staunty.svg"}},
72
72
  })
73
73
  new Chessboard(document.getElementById("board6"), {
74
74
  position: FEN.start,
75
75
  assetsUrl: "../assets/",
76
76
  assetsCache: false,
77
77
  style: {
78
- pieces: {file: "staunty.svg"}, cssClass: "blue",
78
+ pieces: {file: "pieces/staunty.svg"}, cssClass: "blue",
79
79
  borderType: BORDER_TYPE.frame
80
80
  },
81
81
  })
@@ -20,7 +20,7 @@
20
20
  const board = new Chessboard(document.getElementById("board"), {
21
21
  position: FEN.start,
22
22
  assetsUrl: "../assets/",
23
- style: {pieces: {file: "staunty.svg"}},
23
+ style: {pieces: {file: "pieces/staunty.svg"}},
24
24
  extensions: [{class: Markers}]
25
25
  })
26
26
 
@@ -52,7 +52,7 @@ function inputHandler(event) {
52
52
  window.board = new Chessboard(document.getElementById("board"), {
53
53
  position: FEN.start,
54
54
  assetsUrl: "../assets/",
55
- style: {pieces: {file: "staunty.svg"}},
55
+ style: {pieces: {file: "pieces/staunty.svg"}},
56
56
  extensions: [{class: Markers}]
57
57
  })
58
58
 
@@ -50,7 +50,7 @@
50
50
  position: "rn2k1r1/ppp1pp1p/3p2p1/5bn1/P7/2N2B2/1PPPPP2/2BNK1RR",
51
51
  assetsCache: false,
52
52
  assetsUrl: "../../assets/",
53
- style: {pieces: {file: "staunty.svg"}},
53
+ style: {pieces: {file: "pieces/staunty.svg"}},
54
54
  extensions: [{class: Markers, autoMarkers: MARKER_TYPE.frame}]
55
55
  })
56
56
  board2.enableMoveInput(() => {
@@ -28,10 +28,7 @@
28
28
  const chessboard = new Chessboard(document.getElementById("chessboard"), {
29
29
  position: position,
30
30
  assetsUrl: "../../assets/",
31
- extensions: [
32
- {class: PromotionDialog},
33
- {class: Markers}
34
- ]
31
+ extensions: [{class: PromotionDialog}, {class: Markers}]
35
32
  })
36
33
  chessboard.enableMoveInput((event) => {
37
34
  if (event.type === INPUT_EVENT_TYPE.validateMoveInput) {
@@ -35,7 +35,7 @@
35
35
  responsive: false,
36
36
  position: game.fen(),
37
37
  assetsUrl: "../assets/",
38
- style: {pieces: {file: "staunty.svg"}},
38
+ style: {pieces: {file: "pieces/staunty.svg"}},
39
39
  })
40
40
  makeRandomMove()
41
41
  }
@@ -51,7 +51,7 @@ await board.movePiece("d5", "f4")
51
51
  {
52
52
  position: FEN.start,
53
53
  assetsUrl: "../assets/",
54
- style: {pieces: {file: "staunty.svg"}, animationDuration: 500}
54
+ style: {pieces: {file: "pieces/staunty.svg"}, animationDuration: 500}
55
55
  })
56
56
  let i = 0
57
57
  window.setPosition = (position, animated) => {
@@ -24,7 +24,7 @@ new Chessboard(document.getElementById("board"), {
24
24
  new Chessboard(document.getElementById("board"), {
25
25
  position: FEN.start,
26
26
  assetsUrl: "../assets/",
27
- style: {aspectRatio: 0.9, pieces: {file: "staunty.svg"}}
27
+ style: {aspectRatio: 0.9, pieces: {file: "pieces/staunty.svg"}}
28
28
  })
29
29
  </script>
30
30
  </body>
@@ -19,7 +19,7 @@ new Chessboard(document.getElementById("board1"), {
19
19
  })
20
20
  new Chessboard(document.getElementById("board2"), {
21
21
  position: "rn2k1r1/ppp1pp1p/3p2p1/5bn1/P7/2N2B2/1PPPPP2/2BNK1RR w Gkq - 4 11",
22
- style: {pieces: {file: "staunty.svg"},cssClass: "green", borderType: BORDER_TYPE.frame},
22
+ style: {pieces: {file: "pieces/staunty.svg"},cssClass: "green", borderType: BORDER_TYPE.frame},
23
23
  orientation: COLOR.black
24
24
  })
25
25
  </pre>
@@ -35,7 +35,7 @@ new Chessboard(document.getElementById("board2"), {
35
35
  position: "rn2k1r1/ppp1pp1p/3p2p1/5bn1/P7/2N2B2/1PPPPP2/2BNK1RR w Gkq - 4 11",
36
36
  assetsUrl: "../assets/",
37
37
  assetsCache: false,
38
- style: {pieces: {file: "staunty.svg"},cssClass: "green", borderType: BORDER_TYPE.frame},
38
+ style: {pieces: {file: "pieces/staunty.svg"},cssClass: "green", borderType: BORDER_TYPE.frame},
39
39
  orientation: COLOR.black
40
40
  })
41
41
  </script>
@@ -98,7 +98,7 @@
98
98
  const board = new Chessboard(document.getElementById("board"), {
99
99
  position: chess.fen(),
100
100
  assetsUrl: "../assets/",
101
- style: {borderType: BORDER_TYPE.none, pieces: {file: "staunty.svg"}, animationDuration: 300},
101
+ style: {borderType: BORDER_TYPE.none, pieces: {file: "pieces/staunty.svg"}, animationDuration: 300},
102
102
  orientation: COLOR.white,
103
103
  extensions: [
104
104
  {class: Markers, props: {autoMarkers: MARKER_TYPE.square}},
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cm-chessboard",
3
- "version": "7.6.10",
3
+ "version": "7.7.0",
4
4
  "description": "A JavaScript chessboard which is lightweight, ES6 module based, responsive, SVG rendered and without dependencies.",
5
5
  "keywords": [
6
6
  "chess",
package/src/Chessboard.js CHANGED
@@ -56,7 +56,7 @@ export class Chessboard {
56
56
  orientation: COLOR.white, // white on bottom
57
57
  responsive: true, // resize the board automatically to the size of the context element
58
58
  language: navigator.language.substring(0, 2).toLowerCase(), // supports "de" and "en" for now, used for pieces naming
59
- assetsUrl: "./assets/", // put all css and sprites in this folder
59
+ assetsUrl: "./assets/", // put all css and sprites in this folder, will be ignored for absolute urls of assets files
60
60
  assetsCache: true, // cache sprites
61
61
  style: {
62
62
  cssClass: "default", // set the css theme of the board, try "green", "blue" or "chess-club"
@@ -65,7 +65,7 @@ export class Chessboard {
65
65
  aspectRatio: 1, // height/width of the board
66
66
  pieces: {
67
67
  type: PIECES_FILE_TYPE.svgSprite, // pieces are in an SVG sprite, no other type supported for now
68
- file: "standard.svg", // the filename of the sprite in `assets/pieces/`
68
+ file: "pieces/standard.svg", // the filename of the sprite in `assets/pieces/` or an absolute url like `https://…` or `/…`
69
69
  tileSize: 40 // the tile size in the sprite
70
70
  },
71
71
  animationDuration: 300 // pieces animation duration in milliseconds. Disable all animations with `0`.
@@ -6,6 +6,7 @@
6
6
 
7
7
  import {Extension, EXTENSION_POINT} from "../../model/Extension.js"
8
8
  import {Svg} from "../../lib/Svg.js"
9
+ import {Utils} from "../../lib/Utils.js"
9
10
 
10
11
  export const ARROW_TYPE = {
11
12
  default: {class: "arrow-default", slice: "arrowDefault", headSize: 7},
@@ -22,12 +23,11 @@ export class Arrows extends Extension {
22
23
  this.onRedrawBoard()
23
24
  })
24
25
  this.props = {
25
- sprite: "arrows.svg"
26
+ sprite: "extensions/arrows/arrows.svg"
26
27
  }
27
28
  Object.assign(this.props, props)
28
29
  if (this.chessboard.props.assetsCache) {
29
- this.chessboard.view.cacheSpriteToDiv("cm-chessboard-arrows",
30
- this.chessboard.props.assetsUrl + "extensions/arrows/" + this.props.sprite)
30
+ this.chessboard.view.cacheSpriteToDiv("cm-chessboard-arrows", this.getSpriteUrl())
31
31
  }
32
32
  chessboard.addArrow = this.addArrow.bind(this)
33
33
  chessboard.getArrows = this.getArrows.bind(this)
@@ -52,8 +52,7 @@ export class Arrows extends Extension {
52
52
  const view = this.chessboard.view
53
53
  const sqfrom = document.querySelectorAll('[data-square="' + arrow.from + '"]')[0]
54
54
  const sqto = document.querySelectorAll('[data-square="' + arrow.to + '"]')[0]
55
- const spriteUrl = this.chessboard.props.assetsCache ? "" : this.chessboard.props.assetsUrl +
56
- "extensions/arrows/" + this.props.sprite
55
+ const spriteUrl = this.chessboard.props.assetsCache ? "" : this.getSpriteUrl()
57
56
  const defs = Svg.addElement(arrowsGroup, "defs")
58
57
  const id = "arrow-" + arrow.from + arrow.to
59
58
  const marker = Svg.addElement(defs, "marker", {
@@ -109,6 +108,14 @@ export class Arrows extends Extension {
109
108
  this.arrows = this.arrows.filter((arrow) => !arrow.matches(from, to, type))
110
109
  this.chessboard.view.redrawBoard()
111
110
  }
111
+
112
+ getSpriteUrl() {
113
+ if(Utils.isAbsoluteUrl(this.props.sprite)) {
114
+ return this.props.sprite
115
+ } else {
116
+ return this.chessboard.props.assetsUrl + this.props.sprite
117
+ }
118
+ }
112
119
  }
113
120
 
114
121
  class Arrow {
@@ -7,6 +7,7 @@
7
7
  import {Extension, EXTENSION_POINT} from "../../model/Extension.js"
8
8
  import {Svg} from "../../lib/Svg.js"
9
9
  import {INPUT_EVENT_TYPE} from "../../Chessboard.js"
10
+ import {Utils} from "../../lib/Utils.js"
10
11
 
11
12
  export const MARKER_TYPE = {
12
13
  frame: {class: "marker-frame", slice: "markerFrame"},
@@ -30,12 +31,11 @@ export class Markers extends Extension {
30
31
  })
31
32
  this.props = {
32
33
  autoMarkers: MARKER_TYPE.frame, // set to `null` to disable autoMarkers
33
- sprite: "markers.svg" // the sprite file of the markers
34
+ sprite: "extensions/markers/markers.svg" // the sprite file of the markers
34
35
  }
35
36
  Object.assign(this.props, props)
36
37
  if (chessboard.props.assetsCache) {
37
- chessboard.view.cacheSpriteToDiv("cm-chessboard-markers", this.chessboard.props.assetsUrl +
38
- "extensions/markers/" + this.props.sprite)
38
+ chessboard.view.cacheSpriteToDiv("cm-chessboard-markers", this.getSpriteUrl())
39
39
  }
40
40
  chessboard.addMarker = this.addMarker.bind(this)
41
41
  chessboard.getMarkers = this.getMarkers.bind(this)
@@ -95,8 +95,7 @@ export class Markers extends Extension {
95
95
  const transform = (this.chessboard.view.svg.createSVGTransform())
96
96
  transform.setTranslate(point.x, point.y)
97
97
  markerGroup.transform.baseVal.appendItem(transform)
98
- const spriteUrl = this.chessboard.props.assetsCache ? "" : this.chessboard.props.assetsUrl +
99
- "extensions/markers/" + this.props.sprite
98
+ const spriteUrl = this.chessboard.props.assetsCache ? "" : this.getSpriteUrl()
100
99
  const markerUse = Svg.addElement(markerGroup, "use",
101
100
  {href: `${spriteUrl}#${marker.type.slice}`, class: "marker " + marker.type.class})
102
101
  const transformScale = (this.chessboard.view.svg.createSVGTransform())
@@ -136,6 +135,14 @@ export class Markers extends Extension {
136
135
  this.markers = this.markers.filter((marker) => !marker.matches(square, type))
137
136
  this.onRedrawBoard()
138
137
  }
138
+
139
+ getSpriteUrl() {
140
+ if(Utils.isAbsoluteUrl(this.props.sprite)) {
141
+ return this.props.sprite
142
+ } else {
143
+ return this.chessboard.props.assetsUrl + this.props.sprite
144
+ }
145
+ }
139
146
  }
140
147
 
141
148
  class Marker {
package/src/lib/Utils.js CHANGED
@@ -55,4 +55,8 @@ export class Utils {
55
55
  return promise
56
56
  }
57
57
 
58
+ static isAbsoluteUrl(url) {
59
+ return url.indexOf("://") !== -1 || url.startsWith("/")
60
+ }
61
+
58
62
  }
@@ -9,6 +9,7 @@ import {BORDER_TYPE, COLOR, INPUT_EVENT_TYPE} from "../Chessboard.js"
9
9
  import {Position} from "../model/Position.js"
10
10
  import {EXTENSION_POINT} from "../model/Extension.js"
11
11
  import {Svg} from "../lib/Svg.js"
12
+ import {Utils} from "../lib/Utils.js"
12
13
 
13
14
  export class ChessboardView {
14
15
  constructor(chessboard) {
@@ -64,7 +65,9 @@ export class ChessboardView {
64
65
  cacheSpriteToDiv(wrapperId, url) {
65
66
  if (!document.getElementById(wrapperId)) {
66
67
  const wrapper = document.createElement("div")
67
- wrapper.style.display = "none"
68
+ wrapper.style.transform = "scale(0)"
69
+ wrapper.style.position = "absolute"
70
+ wrapper.setAttribute("aria-hidden", "true")
68
71
  wrapper.id = wrapperId
69
72
  document.body.appendChild(wrapper)
70
73
  const xhr = new XMLHttpRequest()
@@ -435,6 +438,10 @@ export class ChessboardView {
435
438
  }
436
439
 
437
440
  getSpriteUrl() {
438
- return this.chessboard.props.assetsUrl + "pieces/" + this.chessboard.props.style.pieces.file
441
+ if(Utils.isAbsoluteUrl(this.chessboard.props.style.pieces.file)) {
442
+ return this.chessboard.props.style.pieces.file
443
+ } else {
444
+ return this.chessboard.props.assetsUrl + this.chessboard.props.style.pieces.file
445
+ }
439
446
  }
440
447
  }