UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

360 lines (359 loc) • 12.9 kB
/** @jsx jsx */ 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 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 _object_spread_props(target, source) { source = source != null ? source : {}; if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function(key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(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; })(); } function _ts_decorate(decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; } import { Component } from 'react'; import PropTypes from 'prop-types'; import pickBy from 'lodash/pickBy'; import propTypes, { defaultProps } from './propTypes'; import { jsx } from '@instructure/emotion'; import generateComponentTheme from './theme'; import generateStyle from './styles'; import { withStyleOverrides } from '@instructure/quiz-common'; var Canvas = /*#__PURE__*/ function(Component) { "use strict"; _inherits(Canvas, Component); function Canvas(props) { _class_call_check(this, Canvas); var _this; _this = _call_super(this, Canvas, [ props ]), _define_property(_this, "state", { isDrawing: false }), _define_property(_this, "refCanvas", function(node) { _this.canvasElement = node; }), // =========== // ACTIONS // =========== _define_property(_this, "drawShape", function(coordinates) { _this.clearCanvas(); _this.props.drawShape(coordinates); }), // =========== // HANDLERS // =========== _define_property(_this, "onMouseOut", function(e) { // the mouse out event is handled by default as a click/release on the exiting canvas border _this.props.onMouseOut // eslint-disable-line no-unused-expressions ? _this.props.onMouseOut(e) : function(e) {}; }); // these handlers shouldn't change, so set them once per initialization _this.eventHandlers = _object_spread({ onMouseOut: _this.onMouseOut }, pickBy(props, function(prop, propName) { return prop !== null && propName.startsWith('on'); })); return _this; } _create_class(Canvas, [ { key: "componentDidMount", value: function componentDidMount() { if (this.props.realRef) { this.props.realRef(this); } this.drawShape(this.props.coordinates); } }, { key: "componentDidUpdate", value: function componentDidUpdate() { this.drawShape(this.props.coordinates); if (this.isDrawing()) { this.canvasElement.focus(); } } }, { // =========== // HELPERS // =========== key: "isDrawing", value: function isDrawing() { return this.state.isDrawing; } }, { key: "getInitialCoordinate", value: function getInitialCoordinate() { return this.props.coordinates[0]; } }, { key: "getLastCoordinate", value: function getLastCoordinate() { return this.props.coordinates[this.props.coordinates.length - 1]; } }, { key: "getCurrentCoordinates", value: function getCurrentCoordinates(e) { var x = e.nativeEvent.offsetX; var y = e.nativeEvent.offsetY; return { x: x, y: y }; } }, { key: "convertTouchCoordinates", value: function convertTouchCoordinates(touchEvent) { var rect = this.canvasElement.getBoundingClientRect(); var x = touchEvent.changedTouches[0].pageX - rect.left; var y = touchEvent.changedTouches[0].pageY - rect.top; var evt = { nativeEvent: { offsetX: x, offsetY: y } }; return evt; } }, { key: "focus", value: function focus() { this.canvasElement.focus(); } }, { key: "clearCanvas", value: function clearCanvas() { var ctx = this.canvasElement.getContext('2d'); ctx.clearRect(0, 0, this.canvasElement.width, this.canvasElement.height); } }, { key: "setStrokeStyle", value: function setStrokeStyle() { var color = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : this.props.styles.shapeColor; var ctx = this.canvasElement.getContext('2d'); ctx.lineWidth = 2; ctx.strokeStyle = color; } }, { key: "setFillStyle", value: function setFillStyle() { var color = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : this.props.styles.shapeColor; var ctx = this.canvasElement.getContext('2d'); ctx.globalAlpha = 0.5; ctx.fillStyle = color; } }, { key: "startDrawing", value: function startDrawing(e) { var initialCoordinate = this.getCurrentCoordinates(e); this.props.handleSetCoordinates([ initialCoordinate ], true); this.setState({ isDrawing: true }); } }, { key: "stopDrawing", value: function stopDrawing(coordinates) { this.props.handleSetCoordinates(coordinates, false); this.setState({ isDrawing: false }); } }, { // =========== // RENDER // =========== key: "renderCanvas", value: function renderCanvas() { if (this.props.readOnly) { return /*#__PURE__*/ jsx("canvas", { height: this.props.height, ref: this.refCanvas, width: this.props.width }); } return /*#__PURE__*/ jsx("canvas", _object_spread({ css: this.props.styles.canvas, height: this.props.height, ref: this.refCanvas, tabIndex: "0", width: this.props.width, onBlur: this.onMouseOut }, this.eventHandlers)); } }, { key: "render", value: function render() { return /*#__PURE__*/ jsx("div", { css: this.props.styles.canvasWrapper }, this.renderCanvas()); } } ]); return Canvas; }(Component); _define_property(Canvas, "displayName", 'Canvas'); _define_property(Canvas, "componentId", "Quizzes".concat(Canvas.displayName)); _define_property(Canvas, "propTypes", _object_spread_props(_object_spread({}, propTypes), { realRef: PropTypes.func, drawShape: PropTypes.func.isRequired, onMouseOut: PropTypes.func, onMouseMove: PropTypes.func, onDoubleClick: PropTypes.func, onClick: PropTypes.func, onKeyPress: PropTypes.func, onMouseDown: PropTypes.func, onMouseUp: PropTypes.func, onTouchStart: PropTypes.func, onTouchMove: PropTypes.func, onTouchEnd: PropTypes.func, styles: PropTypes.object })); _define_property(Canvas, "defaultProps", _object_spread({ realRef: null, onMouseOut: null, onMouseMove: null, onDoubleClick: null, onClick: null, onKeyPress: null, onMouseDown: null, onMouseUp: null, onTouchStart: null, onTouchMove: null, onTouchEnd: null }, defaultProps)); export { Canvas as default }; Canvas = _ts_decorate([ withStyleOverrides(generateStyle, generateComponentTheme) ], Canvas);