UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

261 lines (260 loc) • 11 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 striptags from 'striptags'; import { htmlDecode } from 'htmlencode'; import { Text } from '@instructure/ui-text'; import { jsx } from '@instructure/emotion'; import XRegExp from 'xregexp'; import generateStyle from './styles'; import generateComponentTheme from './theme'; import t from '@instructure/quiz-i18n/format-message'; import { withStyleOverrides } from '@instructure/quiz-common'; import { IconWarningSolid } from '@instructure/ui-icons'; import { View } from '@instructure/ui-view'; // XRegExp supports unicode so we can use the codepoints for all {L}etters and {N}umbers // See "Unicode Categories" here https://www.regular-expressions.info/unicode.html var WORD_REGEX = XRegExp("[-_'\\p{L}\\p{N}]+", 'gu'); export var countWords = function(content) { if (!content) { return 0; } var matches = content.match(WORD_REGEX); return matches ? matches.length : 0; }; export var getRceEssayContent = function(essay) { return htmlDecode(striptags(essay)); }; export var showWordCount = function(wordCount, rce, isEditing) { if (!wordCount) return false; if (rce && isEditing) return false; return true; }; export var WordCount = /*#__PURE__*/ function(Component) { "use strict"; _inherits(WordCount, Component); function WordCount() { _class_call_check(this, WordCount); var _this; _this = _call_super(this, WordCount, arguments), _define_property(_this, "count", function() { var props = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : _this.props; return countWords(props.rce ? getRceEssayContent(props.essay) : props.essay); }); return _this; } _create_class(WordCount, [ { key: "UNSAFE_componentWillReceiveProps", value: function UNSAFE_componentWillReceiveProps(nextProps) { var errorMessage = this.getErrorMessage(); var nextErrorMessage = this.getErrorMessage(nextProps); var errorMessageChanged = errorMessage !== nextErrorMessage; var overLimit = nextErrorMessage && this.isOverWordLimit(nextProps); var wordCountChangedAndOverLimit = overLimit && this.count() !== this.count(nextProps); if (this.props.notifyScreenreader && (errorMessageChanged || wordCountChangedAndOverLimit) && nextErrorMessage) { this.props.notifyScreenreader(nextErrorMessage); } } }, { key: "isOverWordLimit", value: function isOverWordLimit() { var props = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : this.props; return props.wordLimitMax != null && this.count(props) > props.wordLimitMax; } }, { key: "getErrorMessage", value: function getErrorMessage() { var props = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : this.props; var wordLimitMin = props.wordLimitMin, wordLimitEnabled = props.wordLimitEnabled; var minError = wordLimitMin && this.count(props) < wordLimitMin; var maxError = this.isOverWordLimit(props); var countError = wordLimitEnabled && (minError || maxError); var minErrorText = t("{wordLimitMin, plural,\n =1 {Must use at least # word}\n other {Must use at least # words}}", { wordLimitMin: wordLimitMin }); var maxErrorText = t('Maximum words exceeded!'); var errorText = this.count(props) < wordLimitMin ? minErrorText : maxErrorText; return countError ? errorText : ''; } }, { key: "getLimitsText", value: function getLimitsText() { if (this.props.wordLimitEnabled) { var limits = []; if (this.props.wordLimitMin) { limits.push(t('{wordLimitMin} min', { wordLimitMin: this.props.wordLimitMin })); } if (this.props.wordLimitMax) { limits.push(t('{wordLimitMax} max', { wordLimitMax: this.props.wordLimitMax })); } var limit = limits.join(' / '); return /*#__PURE__*/ jsx(Text, { color: "secondary", size: "small" }, limit, ' ', /*#__PURE__*/ jsx(Text, { color: "primary", size: "small" }, t('(word limit)'))); } } }, { key: "renderWordCount", value: function renderWordCount() { var wordLimitEnabled = this.props.wordLimitEnabled; var maxError = this.isOverWordLimit(); return /*#__PURE__*/ jsx("span", { css: this.props.styles.countText }, /*#__PURE__*/ jsx(Text, { size: "small", color: wordLimitEnabled && maxError ? 'danger' : 'primary', weight: "bold" }, t('{wordcount, plural, =1 {# word} other {# words}}', { wordcount: this.count() }))); } }, { key: "render", value: function render() { var _this_props = this.props, rce = _this_props.rce, wordCount = _this_props.wordCount, isEditing = _this_props.isEditing; var maxError = this.isOverWordLimit(); return /*#__PURE__*/ jsx("div", { className: "fs-mask", css: this.props.styles.newWordCount }, showWordCount(wordCount, rce, isEditing) && this.renderWordCount(), this.getLimitsText(), /*#__PURE__*/ jsx("div", null, /*#__PURE__*/ jsx(Text, { color: maxError ? 'danger' : null, size: "small" }, maxError && /*#__PURE__*/ jsx(IconWarningSolid, null), ' ', /*#__PURE__*/ jsx(View, { display: "inline-block", margin: "0 0 0 xxx-small" }, this.getErrorMessage())))); } } ]); return WordCount; }(Component); _define_property(WordCount, "displayName", 'WordCount'); _define_property(WordCount, "componentId", "Quizzes".concat(WordCount.displayName)); _define_property(WordCount, "propTypes", { essay: PropTypes.string, rce: PropTypes.bool, isEditing: PropTypes.bool, wordCount: PropTypes.bool, wordLimitEnabled: PropTypes.bool, wordLimitMin: PropTypes.number, wordLimitMax: PropTypes.number, notifyScreenreader: PropTypes.func, styles: PropTypes.object }); _define_property(WordCount, "defaultProps", { essay: void 0, rce: void 0, isEditing: true, wordCount: void 0, wordLimitEnabled: void 0, wordLimitMin: void 0, wordLimitMax: void 0, notifyScreenreader: void 0 }); WordCount = _ts_decorate([ withStyleOverrides(generateStyle, generateComponentTheme) ], WordCount);