UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

207 lines (206 loc) • 8.12 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 Square = /*#__PURE__*/ function(Component) { "use strict"; _inherits(Square, Component); function Square() { _class_call_check(this, Square); var _this; _this = _call_super(this, Square, arguments), _define_property(_this, "canvasRef", function(ref) { _this.canvas = ref; }), // this type needs exactly 2 coordinates to draw a square _define_property(_this, "drawShape", function() { var coordinates = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : []; if (coordinates.length === 2) { var ctx = _this.canvas.canvasElement.getContext('2d'); var startPoint = coordinates[0]; var endPoint = coordinates[1]; var minX = Math.min(endPoint.x, startPoint.x); var minY = Math.min(endPoint.y, startPoint.y); var maxX = Math.max(endPoint.x, startPoint.x); var maxY = Math.max(endPoint.y, startPoint.y); var width = maxX - minX; var height = maxY - minY; _this.canvas.setFillStyle(); // content ctx.fillRect(minX, minY, width, height); _this.canvas.setStrokeStyle(); // border ctx.strokeRect(minX, minY, width, height); } }), // =========== // ACTIONS // =========== _define_property(_this, "onMouseMove", function(e) { if (_this.canvas.isDrawing()) { var coordinates = _this.__getSquareCoordinates(e); _this.canvas.drawShape(coordinates); } }), _define_property(_this, "onMouseDown", function(e) { if (!_this.canvas.isDrawing()) { _this.canvas.startDrawing(e); } }), _define_property(_this, "onMouseUp", function(e) { if (_this.canvas.isDrawing()) { var coordinates = _this.__getSquareCoordinates(e); var firstPoint = coordinates[0]; var nextPoint = coordinates[1]; var pointsDiffer = firstPoint.x !== nextPoint.x || firstPoint.y !== nextPoint.y; if (pointsDiffer) { _this.canvas.stopDrawing(coordinates); } } }), _define_property(_this, "onTouchStart", function(e) { e.preventDefault(); var evt = _this.canvas.convertTouchCoordinates(e); _this.onMouseDown(evt); }), _define_property(_this, "onTouchMove", function(e) { var evt = _this.canvas.convertTouchCoordinates(e); _this.onMouseMove(evt); }), _define_property(_this, "onTouchEnd", function(e) { e.preventDefault(); var evt = _this.canvas.convertTouchCoordinates(e); _this.onMouseUp(evt); }); return _this; } _create_class(Square, [ { key: "__getSquareCoordinates", value: function __getSquareCoordinates(e) { var startPoint = this.canvas.getInitialCoordinate(); var x = e.nativeEvent.offsetX; var y = e.nativeEvent.offsetY; return [ { x: startPoint.x, y: startPoint.y }, { x: x, y: y } ]; } }, { key: "render", value: function render() { return /*#__PURE__*/ React.createElement(Canvas, _object_spread({ realRef: this.canvasRef, drawShape: this.drawShape, onMouseMove: this.onMouseMove, onMouseDown: this.onMouseDown, onMouseUp: this.onMouseUp, onTouchStart: this.onTouchStart, onTouchMove: this.onTouchMove, onTouchEnd: this.onTouchEnd }, pickProps(this.props))); } } ]); return Square; }(Component); _define_property(Square, "propTypes", canvasPropTypes); _define_property(Square, "defaultProps", defaultProps); export { Square as default };