UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

150 lines (147 loc) • 7.36 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = _interopRequireWildcard(require("react")); var _Canvas = _interopRequireDefault(require("../Canvas")); var _propTypes = _interopRequireWildcard(require("../Canvas/propTypes")); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _callSuper(_this, derived, args) { function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { return !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (e) { return false; } } derived = (0, _getPrototypeOf2["default"])(derived); return (0, _possibleConstructorReturn2["default"])(_this, isNativeReflectConstruct() ? Reflect.construct(derived, args || [], (0, _getPrototypeOf2["default"])(_this).constructor) : derived.apply(_this, args)); } var Polygon = exports["default"] = /*#__PURE__*/function (_Component) { function Polygon() { var _this2; (0, _classCallCheck2["default"])(this, Polygon); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this2 = _callSuper(this, Polygon, [].concat(args)); (0, _defineProperty2["default"])(_this2, "canvasRef", function (node) { _this2.canvas = node; }); (0, _defineProperty2["default"])(_this2, "drawShape", function (coordinates) { if (coordinates.length === 0) return; var ctx = _this2.canvas.canvasElement.getContext('2d'); _this2.canvas.setStrokeStyle(); coordinates.forEach(function (item, index, array) { var nextCoordinate = array[index + 1]; if (nextCoordinate !== void 0) { var initialCoord = null; if (index === 0) { initialCoord = item; } _this2.__drawLine(initialCoord, nextCoordinate); } }); _this2.canvas.setFillStyle(); ctx.fill(); }); (0, _defineProperty2["default"])(_this2, "onMouseMove", function (e) { if (_this2.canvas.isDrawing()) { _this2.clearCanvas(); var currentCoordinate = _this2.canvas.getCurrentCoordinates(e); var lines = _this2.props.coordinates.concat([currentCoordinate]); // add temporary coordinate // add initial coordinate at the end of the array to close the polygon if (lines.length > 2) { lines = lines.concat([lines[0]]); } _this2.drawShape(lines); } }); (0, _defineProperty2["default"])(_this2, "onMouseOut", function (e) { if (_this2.canvas.isDrawing()) { var initialCoordinate = _this2.canvas.getInitialCoordinate(); var newCoordinates = _this2.props.coordinates.concat([initialCoordinate]); if (newCoordinates.length > 3) { _this2.clearCanvas(); _this2.drawShape(newCoordinates); _this2.canvas.stopDrawing(newCoordinates); } } }); (0, _defineProperty2["default"])(_this2, "onDoubleClick", function () { _this2.clearCanvas(); var initialCoordinate = _this2.canvas.getInitialCoordinate(); var newCoordinates = _this2.props.coordinates.concat([initialCoordinate]); _this2.drawShape(newCoordinates); if (_this2.canvas.isDrawing()) { _this2.canvas.stopDrawing(newCoordinates); } }); (0, _defineProperty2["default"])(_this2, "onClick", function (e) { if (_this2.canvas.isDrawing()) { _this2.clearCanvas(); var currentCoordinate = _this2.canvas.getCurrentCoordinates(e); var lastCoordinate = _this2.canvas.getLastCoordinate(); if (currentCoordinate.x === lastCoordinate.x && currentCoordinate.y === lastCoordinate.y) { return; // in case the user click at the same point, this click is discarded } var newCoordinates = _this2.props.coordinates.concat([currentCoordinate]); _this2.drawShape(newCoordinates); _this2.props.handleSetCoordinates(newCoordinates, true); } else { _this2.canvas.startDrawing(e); } }); return _this2; } (0, _inherits2["default"])(Polygon, _Component); return (0, _createClass2["default"])(Polygon, [{ key: "__drawLine", value: function __drawLine(begin, end) { var ctx = this.canvas.canvasElement.getContext('2d'); if (begin) { ctx.beginPath(); ctx.moveTo(begin.x, begin.y); } ctx.lineTo(end.x, end.y); ctx.stroke(); } // =========== // ACTIONS // =========== }, { key: "clearCanvas", value: function clearCanvas() { var ctx = this.canvas.canvasElement.getContext('2d'); ctx.clearRect(0, 0, this.canvas.canvasElement.width, this.canvas.canvasElement.height); ctx.beginPath(); ctx.closePath(); } }, { key: "render", value: function render() { return /*#__PURE__*/_react["default"].createElement(_Canvas["default"], Object.assign({ realRef: this.canvasRef, drawShape: this.drawShape, onMouseMove: this.onMouseMove, onMouseOut: this.onMouseOut, onDoubleClick: this.onDoubleClick, onClick: this.onClick }, (0, _propTypes.pickProps)(this.props))); } }]); }(_react.Component); (0, _defineProperty2["default"])(Polygon, "propTypes", _propTypes["default"]); (0, _defineProperty2["default"])(Polygon, "defaultProps", _propTypes.defaultProps);