@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
213 lines (212 loc) • 8.48 kB
JavaScript
/** @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 _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 { jsx } from '@instructure/emotion';
import { Component } from 'react';
import PropTypes from 'prop-types';
import '../../common/ellipsePolyfill';
import Canvas from '../../common/Canvas';
import canvasPropTypes, { pickProps, defaultProps } from '../../common/Canvas/propTypes';
import generateComponentTheme from './theme';
import generateStyle from './styles';
import { withStyleOverrides } from '@instructure/quiz-common';
var Target = /*#__PURE__*/ function(Component) {
"use strict";
_inherits(Target, Component);
function Target() {
_class_call_check(this, Target);
var _this;
_this = _call_super(this, Target, arguments), _define_property(_this, "canvasRef", function(node) {
_this.canvas = node;
}), _define_property(_this, "drawShape", function() {
var coordinates = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : [];
if (coordinates.length === 1) {
var internalGap = 2;
var pointA = {
x: coordinates[0].x - internalGap,
y: coordinates[0].y - internalGap
};
var pointB = {
x: coordinates[0].x + internalGap,
y: coordinates[0].y + internalGap
};
var radiusX = (pointB.x - pointA.x) * 0.5;
var radiusY = (pointB.y - pointA.y) * 0.5;
var centerX = pointA.x + radiusX;
var centerY = pointA.y + radiusY;
var externalGap = 10;
_this.__drawEllipse(// internal circle
radiusX, radiusY, centerX, centerY);
_this.__drawEllipse(// external circle
radiusX + externalGap, radiusY + externalGap, centerX, centerY);
}
}), // ===========
// ACTIONS
// ===========
_define_property(_this, "onMouseUp", function(e) {
var coordinates = _this.__getCoordinates(e);
_this.canvas.drawShape(coordinates);
_this.canvas.stopDrawing(coordinates);
}), // override the default behaviour that's not needed for this type
_define_property(_this, "onMouseOut", function() {});
return _this;
}
_create_class(Target, [
{
key: "__getCoordinates",
value: function __getCoordinates(e) {
var x = e.nativeEvent.offsetX;
var y = e.nativeEvent.offsetY;
return [
{
x: x,
y: y
}
];
}
},
{
key: "__drawEllipse",
value: function __drawEllipse(radiusX, radiusY, centerX, centerY) {
var ctx = this.canvas.canvasElement.getContext('2d');
this.canvas.setStrokeStyle();
ctx.globalAlpha = 1;
ctx.lineWidth = 4;
ctx.beginPath();
ctx.ellipse(centerX, centerY, Math.abs(radiusX), Math.abs(radiusY), 0, 0, 2 * Math.PI);
ctx.closePath();
ctx.shadowColor = this.props.styles.shadowColor;
ctx.shadowBlur = 1;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
ctx.stroke(); // render border
}
},
{
key: "render",
value: function render() {
return /*#__PURE__*/ jsx(Canvas, _object_spread({
realRef: this.canvasRef,
drawShape: this.drawShape,
onMouseUp: this.onMouseUp,
onMouseOut: this.onMouseOut
}, pickProps(this.props)));
}
}
]);
return Target;
}(Component);
_define_property(Target, "displayName", 'Target');
_define_property(Target, "componentId", "Quizzes".concat(Target.displayName));
_define_property(Target, "propTypes", _object_spread({}, canvasPropTypes, {
styles: PropTypes.object
}));
_define_property(Target, "defaultProps", defaultProps);
export { Target as default };
Target = _ts_decorate([
withStyleOverrides(generateStyle, generateComponentTheme)
], Target);