UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

297 lines (296 loc) • 12.1 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 _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 ReactDOM from 'react-dom'; import debounce from 'lodash/debounce'; import partial from 'lodash/partial'; import { jsx } from '@instructure/emotion'; import { AccessibleContent, ScreenReaderContent } from '@instructure/ui-a11y-content'; import { Tag } from '@instructure/ui-tag'; import { View } from '@instructure/ui-view'; import Errors from '../../../common/edit/components/Errors'; import ContentEditable from '../../../common/edit/components/ContentEditable'; import EditStemController from './editStemController'; import generateStyle from './styles'; import generateComponentTheme from './theme'; import t from '@instructure/quiz-i18n/format-message'; import { withStyleOverrides } from '@instructure/quiz-common'; var NBSP_CHAR_CODE = 160; var EditStem = /*#__PURE__*/ function(Component) { "use strict"; _inherits(EditStem, Component); function EditStem(props) { _class_call_check(this, EditStem); var _this; _this = _call_super(this, EditStem, [ props ]), _define_property(_this, "state", { isFocused: false }), _define_property(_this, "handlePaste", function(stemItem, e) { _this.controller.handlePaste(e); _this.props.onStemChange(stemItem, e); }), _define_property(_this, "setFocus", function(isFocused) { _this.setState({ isFocused: isFocused }); }), _define_property(_this, "renderStemItem", function(stemItem) { return stemItem.type === 'text' ? _this.renderContentEditable(stemItem) : _this.renderBlank(stemItem); }); _this.controller = new EditStemController(props); _this.stemItemRefs = {}; return _this; } _create_class(EditStem, [ { // own functions for stubbing purposes key: "_clearInterval", value: function _clearInterval(cb) { clearInterval(cb); } }, { key: "_setInterval", value: function _setInterval(cb, time) { setInterval(cb, time); } }, { // ============= // LIFECYCLE // ============= key: "componentDidMount", value: function componentDidMount() { var _this = this; this.props.makeStyles(); var oldOnSelectionChange = document.onselectionchange; this._dom = ReactDOM.findDOMNode(this); var newOnSelectionChange = debounce(function() { var sel = window.getSelection(); var anchor = sel.anchorNode; if (!anchor) return; if (_this._dom.contains(anchor) || // next line needed because of weird IE11 behavior _this._dom.contains(anchor.parentNode)) { _this.props.setSelectedText(sel.toString()); } }, 100); document.onselectionchange = function(e) { oldOnSelectionChange && oldOnSelectionChange(e); newOnSelectionChange(); }; this._oldOnSelectionChange = oldOnSelectionChange; } }, { key: "componentWillUnmount", value: function componentWillUnmount() { document.onselectionchange = this._oldOnSelectionChange; } }, { key: "UNSAFE_componentWillReceiveProps", value: function UNSAFE_componentWillReceiveProps(nextProps) { this.controller.UNSAFE_componentWillReceiveProps(nextProps); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { this.props.makeStyles(); if (!this.state.isFocused) { return; } var focusData = this.controller.getFocusData(prevProps); if (!focusData) { return; } var focusRef = this.stemItemRefs[focusData.stemItem.id]; focusData.stemItem.type === 'blank' ? focusRef.focus() : focusRef.focusAndSetCursorOnPosition(focusData.cursorPosition); } }, { // ============= // RENDERING // ============= key: "renderContentEditable", value: function renderContentEditable(stemItem) { var _this = this; var usePlaceholder = this.props.stemItems.length <= 1; var placeholder = usePlaceholder ? t('Type a statement...') : ''; var content = this.controller.textForStemItem(stemItem); if (!content || content === String.fromCharCode(NBSP_CHAR_CODE)) { // nbsp is used to give a width to a final stem item if blank // if we only have one stem, just use the placeholder to do this content = usePlaceholder ? '' : String.fromCharCode(NBSP_CHAR_CODE); } return /*#__PURE__*/ jsx(ContentEditable, { label: /*#__PURE__*/ jsx(ScreenReaderContent, null, t('Statement')), key: stemItem.id, id: stemItem.id, ref: function(span) { _this.stemItemRefs[stemItem.id] = span; }, placeholder: placeholder, enableFormatting: false, disabled: this.props.disabled, content: content, onChange: partial(this.props.onStemChange, stemItem), onPaste: partial(this.handlePaste, stemItem), onKeyDown: this.props.onCreateBlankHotkey, customStyles: this.props.styles.editableText }); } }, { key: "renderBlank", value: function renderBlank(stemItem) { var _this = this; var dismissible = !this.props.disabled; var text = this.controller.textForStemItem(stemItem); return /*#__PURE__*/ jsx(Tag, { key: stemItem.id, dismissible: dismissible, elementRef: function(button) { _this.stemItemRefs[stemItem.id] = button; }, onClick: dismissible ? partial(this.props.destroyBlank, stemItem) : null, size: "large", text: /*#__PURE__*/ jsx(AccessibleContent, { alt: t('Delete "{ text }" blank', { text: text }) }, text), variant: "inline" }); } }, { key: "render", value: function render() { this.stemItemRefs = {}; return /*#__PURE__*/ jsx("div", { onFocus: partial(this.setFocus, true), onBlur: partial(this.setFocus, false) }, /*#__PURE__*/ jsx(Errors, { errorList: this.props.errors }, this.props.stemItems.map(this.renderStemItem)), /*#__PURE__*/ jsx(View, { as: "div", borderWidth: "small none none none", margin: "small none" })); } } ]); return EditStem; }(Component); _define_property(EditStem, "displayName", 'EditStem'); _define_property(EditStem, "componentId", "Quizzes".concat(EditStem.displayName)); _define_property(EditStem, "propTypes", { disabled: PropTypes.bool, errors: PropTypes.array, destroyBlank: PropTypes.func.isRequired, onCreateBlankHotkey: PropTypes.func.isRequired, onStemChange: PropTypes.func.isRequired, setSelectedText: PropTypes.func.isRequired, stemItems: PropTypes.array.isRequired, styles: PropTypes.object, makeStyles: PropTypes.func }); _define_property(EditStem, "defaultProps", { disabled: false, errors: void 0 }); export { EditStem as default }; EditStem = _ts_decorate([ withStyleOverrides(generateStyle, generateComponentTheme) ], EditStem);