UNPKG

@wirelineio/shogiboardjsx

Version:

Shogiboardjsx is a shogiboard for React. Inspired by Chessboardjsx

207 lines (175 loc) 8.5 kB
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } import React, { PureComponent, Fragment } from 'react'; import PropTypes from 'prop-types'; var getRow = function getRow(orientation, row) { return orientation === 'white' ? row + 1 : row - 1; }; var getColumn = function getColumn(orientation, alpha, col) { return orientation === 'black' ? alpha[8 - col] : alpha[col]; }; var Notation = /*#__PURE__*/ function (_PureComponent) { _inherits(Notation, _PureComponent); function Notation() { _classCallCheck(this, Notation); return _possibleConstructorReturn(this, _getPrototypeOf(Notation).apply(this, arguments)); } _createClass(Notation, [{ key: "render", value: function render() { var _this$props = this.props, row = _this$props.row, col = _this$props.col, orientation = _this$props.orientation, lightSquareStyle = _this$props.lightSquareStyle, darkSquareStyle = _this$props.darkSquareStyle; var whiteColor = lightSquareStyle.backgroundColor; var blackColor = darkSquareStyle.backgroundColor; var isRow = col === 0; var isColumn = orientation === 'white' && row === 0 || orientation === 'black' && row === 10; var isBottomLeftSquare = isRow && isColumn; if (isBottomLeftSquare) { return renderBottomLeft(this.props, { whiteColor: whiteColor }); } if (isColumn) { return renderLetters(this.props, { whiteColor: whiteColor, blackColor: blackColor }); } if (isRow) { return renderNumbers(this.props, { whiteColor: whiteColor, blackColor: blackColor, isRow: isRow, isBottomLeftSquare: isBottomLeftSquare }); } return null; } }]); return Notation; }(PureComponent); Notation.propTypes = { row: PropTypes.number, col: PropTypes.number, alpha: PropTypes.array, orientation: PropTypes.string, width: PropTypes.number, lightSquareStyle: PropTypes.object, darkSquareStyle: PropTypes.object }; export default Notation; /* eslint react/prop-types: 0 */ function renderBottomLeft(_ref, _ref2) { var orientation = _ref.orientation, row = _ref.row, width = _ref.width, alpha = _ref.alpha, col = _ref.col; var whiteColor = _ref2.whiteColor; return React.createElement(Fragment, null, React.createElement("div", { "data-testid": "bottom-left-".concat(getRow(orientation, row)), style: _objectSpread({}, notationStyle, {}, { fontSize: width / 48, color: whiteColor }, {}, numericStyle(width)) }, getRow(orientation, row)), React.createElement("div", { "data-testid": "bottom-left-".concat(getColumn(orientation, alpha, col)), style: _objectSpread({}, notationStyle, {}, { fontSize: width / 48, color: whiteColor }, {}, alphaStyle(width)) }, getColumn(orientation, alpha, col))); } function renderLetters(_ref3, _ref4) { var orientation = _ref3.orientation, width = _ref3.width, alpha = _ref3.alpha, col = _ref3.col; var whiteColor = _ref4.whiteColor, blackColor = _ref4.blackColor; return React.createElement("div", { "data-testid": "column-".concat(getColumn(orientation, alpha, col)), style: _objectSpread({}, notationStyle, {}, columnStyle({ col: col, width: width, blackColor: blackColor, whiteColor: whiteColor }), {}, alphaStyle(width)) }, getColumn(orientation, alpha, col)); } function renderNumbers(_ref5, _ref6) { var orientation = _ref5.orientation, row = _ref5.row, width = _ref5.width; var whiteColor = _ref6.whiteColor, blackColor = _ref6.blackColor, isRow = _ref6.isRow, isBottomLeftSquare = _ref6.isBottomLeftSquare; return React.createElement("div", { style: _objectSpread({}, notationStyle, {}, rowStyle({ row: row, width: width, blackColor: blackColor, whiteColor: whiteColor, orientation: orientation, isBottomLeftSquare: isBottomLeftSquare, isRow: isRow }), {}, numericStyle(width)) }, getRow(orientation, row)); } var columnStyle = function columnStyle(_ref7) { var col = _ref7.col, width = _ref7.width, blackColor = _ref7.blackColor, whiteColor = _ref7.whiteColor; return { fontSize: width / 48, color: col % 2 !== 0 ? blackColor : whiteColor }; }; var rowStyle = function rowStyle(_ref8) { var row = _ref8.row, width = _ref8.width, blackColor = _ref8.blackColor, whiteColor = _ref8.whiteColor, orientation = _ref8.orientation, isBottomLeftSquare = _ref8.isBottomLeftSquare, isRow = _ref8.isRow; return { fontSize: width / 48, color: orientation === 'black' ? isRow && !isBottomLeftSquare && row % 2 === 0 ? blackColor : whiteColor : isRow && !isBottomLeftSquare && row % 2 !== 0 ? blackColor : whiteColor }; }; var alphaStyle = function alphaStyle(width) { return { alignSelf: 'flex-end', paddingLeft: width / 8 - width / 48 }; }; var numericStyle = function numericStyle(width) { return { alignSelf: 'flex-start', paddingRight: width / 8 - width / 48 }; }; var notationStyle = { fontFamily: 'Helvetica Neue', zIndex: 3, position: 'absolute' };