UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

230 lines (229 loc) • 8.98 kB
function _assert_this_initialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _call_super(_this, derived, args) { derived = _get_prototype_of(derived); return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args)); } function _class_call_check(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 _create_class(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _define_property(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 _get_prototype_of(o) { _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _get_prototype_of(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) _set_prototype_of(subClass, superClass); } function _object_spread(target) { for(var i = 1; i < arguments.length; i++){ var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === "function") { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function(key) { _define_property(target, key, source[key]); }); } return target; } function _possible_constructor_return(self, call) { if (call && (_type_of(call) === "object" || typeof call === "function")) { return call; } return _assert_this_initialized(self); } function _set_prototype_of(o, p) { _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _set_prototype_of(o, p); } function _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } function _is_native_reflect_construct() { try { var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {})); } catch (_) {} return (_is_native_reflect_construct = function() { return !!result; })(); } import React, { Component } from 'react'; import Canvas from '../Canvas'; import canvasPropTypes, { pickProps, defaultProps } from '../Canvas/propTypes'; var Polygon = /*#__PURE__*/ function(Component) { "use strict"; _inherits(Polygon, Component); function Polygon() { _class_call_check(this, Polygon); var _this; _this = _call_super(this, Polygon, arguments), _define_property(_this, "canvasRef", function(node) { _this.canvas = node; }), _define_property(_this, "drawShape", function(coordinates) { if (coordinates.length === 0) return; var ctx = _this.canvas.canvasElement.getContext('2d'); _this.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; } _this.__drawLine(initialCoord, nextCoordinate); } }); _this.canvas.setFillStyle(); ctx.fill(); }), _define_property(_this, "onMouseMove", function(e) { if (_this.canvas.isDrawing()) { _this.clearCanvas(); var currentCoordinate = _this.canvas.getCurrentCoordinates(e); var lines = _this.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] ]); } _this.drawShape(lines); } }), _define_property(_this, "onMouseOut", function(e) { if (_this.canvas.isDrawing()) { var initialCoordinate = _this.canvas.getInitialCoordinate(); var newCoordinates = _this.props.coordinates.concat([ initialCoordinate ]); if (newCoordinates.length > 3) { _this.clearCanvas(); _this.drawShape(newCoordinates); _this.canvas.stopDrawing(newCoordinates); } } }), _define_property(_this, "onDoubleClick", function() { _this.clearCanvas(); var initialCoordinate = _this.canvas.getInitialCoordinate(); var newCoordinates = _this.props.coordinates.concat([ initialCoordinate ]); _this.drawShape(newCoordinates); if (_this.canvas.isDrawing()) { _this.canvas.stopDrawing(newCoordinates); } }), _define_property(_this, "onClick", function(e) { if (_this.canvas.isDrawing()) { _this.clearCanvas(); var currentCoordinate = _this.canvas.getCurrentCoordinates(e); var lastCoordinate = _this.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 = _this.props.coordinates.concat([ currentCoordinate ]); _this.drawShape(newCoordinates); _this.props.handleSetCoordinates(newCoordinates, true); } else { _this.canvas.startDrawing(e); } }); return _this; } _create_class(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.createElement(Canvas, _object_spread({ realRef: this.canvasRef, drawShape: this.drawShape, onMouseMove: this.onMouseMove, onMouseOut: this.onMouseOut, onDoubleClick: this.onDoubleClick, onClick: this.onClick }, pickProps(this.props))); } } ]); return Polygon; }(Component); _define_property(Polygon, "propTypes", canvasPropTypes); _define_property(Polygon, "defaultProps", defaultProps); export { Polygon as default };