UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

230 lines (228 loc) • 8.28 kB
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/esm/inherits"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; var _dec, _class, _Canvas; 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(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else 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 _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 = _getPrototypeOf(derived); return _possibleConstructorReturn(_this, isNativeReflectConstruct() ? Reflect.construct(derived, args || [], _getPrototypeOf(_this).constructor) : derived.apply(_this, args)); } /** @jsx jsx */ 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 = (_dec = withStyleOverrides(generateStyle, generateComponentTheme), _dec(_class = (_Canvas = /*#__PURE__*/function (_Component) { function Canvas(props) { var _this2; _classCallCheck(this, Canvas); _this2 = _callSuper(this, Canvas, [props]); // these handlers shouldn't change, so set them once per initialization _defineProperty(_this2, "state", { isDrawing: false }); _defineProperty(_this2, "refCanvas", function (node) { _this2.canvasElement = node; }); // =========== // ACTIONS // =========== _defineProperty(_this2, "drawShape", function (coordinates) { _this2.clearCanvas(); _this2.props.drawShape(coordinates); }); // =========== // HANDLERS // =========== _defineProperty(_this2, "onMouseOut", function (e) { // the mouse out event is handled by default as a click/release on the exiting canvas border _this2.props.onMouseOut // eslint-disable-line no-unused-expressions ? _this2.props.onMouseOut(e) : function (e) {}; }); _this2.eventHandlers = _objectSpread({ onMouseOut: _this2.onMouseOut }, pickBy(props, function (prop, propName) { return prop !== null && propName.startsWith('on'); })); return _this2; } _inherits(Canvas, _Component); return _createClass(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] !== undefined ? 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] !== undefined ? 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 }); } }, { key: "renderCanvas", value: // =========== // RENDER // =========== function renderCanvas() { if (this.props.readOnly) { return jsx("canvas", { height: this.props.height, ref: this.refCanvas, width: this.props.width }); } return jsx("canvas", Object.assign({ 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 jsx("div", { css: this.props.styles.canvasWrapper }, this.renderCanvas()); } }]); }(Component), _defineProperty(_Canvas, "displayName", 'Canvas'), _defineProperty(_Canvas, "componentId", "Quizzes".concat(_Canvas.displayName)), _defineProperty(_Canvas, "propTypes", _objectSpread(_objectSpread({}, 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 })), _defineProperty(_Canvas, "defaultProps", _objectSpread({ realRef: null, onMouseOut: null, onMouseMove: null, onDoubleClick: null, onClick: null, onKeyPress: null, onMouseDown: null, onMouseUp: null, onTouchStart: null, onTouchMove: null, onTouchEnd: null }, defaultProps)), _Canvas)) || _class); export { Canvas as default };