@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
201 lines (200 loc) • 8.37 kB
JavaScript
/** @jsx jsx */ function _array_like_to_array(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
return arr2;
}
function _array_with_holes(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterable_to_array_limit(arr, i) {
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
if (_i == null) return;
var _arr = [];
var _n = true;
var _d = false;
var _s, _e;
try {
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally{
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally{
if (_d) throw _e;
}
}
return _arr;
}
function _non_iterable_rest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _sliced_to_array(arr, i) {
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
if (!o) return;
if (typeof o === "string") return _array_like_to_array(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(n);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import React, { useEffect, useLayoutEffect, useRef, useState } from 'react';
import PropTypes from 'prop-types';
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/util/withStyleOverrides';
var NBSP_CHAR_CODE = 160;
function BaseEditStem(props) {
var disabled = props.disabled, errors = props.errors, destroyBlank = props.destroyBlank, onCreateBlankHotkey = props.onCreateBlankHotkey, onStemChange = props.onStemChange, setSelectedText = props.setSelectedText, stemItems = props.stemItems, styles = props.styles, makeStyles = props.makeStyles;
var _useState = _sliced_to_array(useState(false), 2), isFocused = _useState[0], setIsFocused = _useState[1];
var controllerRef = useRef(new EditStemController(props));
var stemItemRefsRef = useRef({});
var domRef = useRef(null);
var oldOnSelectionChangeRef = useRef(null);
var prevStemItemsRef = useRef(stemItems);
controllerRef.current.updateProps(props);
useEffect(function() {
var oldOnSelectionChange = document.onselectionchange;
var newOnSelectionChange = debounce(function() {
var sel = window.getSelection();
var anchor = sel.anchorNode;
if (!anchor || !domRef.current) return;
if (domRef.current.contains(anchor) || // next line needed because of weird IE11 behavior
domRef.current.contains(anchor.parentNode)) {
setSelectedText(sel.toString());
}
}, 100);
document.onselectionchange = function(e) {
oldOnSelectionChange && oldOnSelectionChange(e);
newOnSelectionChange();
};
oldOnSelectionChangeRef.current = oldOnSelectionChange;
return function() {
document.onselectionchange = oldOnSelectionChangeRef.current;
};
}, [
setSelectedText
]);
useLayoutEffect(function() {
makeStyles();
if (!isFocused) {
prevStemItemsRef.current = stemItems;
return;
}
// Check if stemItems structurally changed (blank added/removed)
var prevProps = {
stemItems: prevStemItemsRef.current
};
var focusData = controllerRef.current.getFocusData(prevProps);
// Update ref after checking for changes
prevStemItemsRef.current = stemItems;
if (!focusData) {
return;
}
var focusRef = stemItemRefsRef.current[focusData.stemItem.id];
if (focusRef) {
focusData.stemItem.type === 'blank' ? focusRef.focus() : focusRef.focusAndSetCursorOnPosition(focusData.cursorPosition);
}
});
var handlePaste = function(stemItem, e) {
controllerRef.current.handlePaste(e);
onStemChange(stemItem, e);
};
var setFocus = function(focusState) {
setIsFocused(focusState);
};
var renderContentEditable = function(stemItem) {
var usePlaceholder = stemItems.length <= 1;
var placeholder = usePlaceholder ? t('Type a statement...') : '';
var content = controllerRef.current.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) {
stemItemRefsRef.current[stemItem.id] = span;
},
placeholder: placeholder,
enableFormatting: false,
disabled: disabled,
content: content,
onChange: partial(onStemChange, stemItem),
onPaste: partial(handlePaste, stemItem),
onKeyDown: onCreateBlankHotkey,
customStyles: styles.editableText
});
};
var renderBlank = function(stemItem) {
var dismissible = !disabled;
var text = controllerRef.current.textForStemItem(stemItem);
return /*#__PURE__*/ jsx(Tag, {
key: stemItem.id,
dismissible: dismissible,
elementRef: function(button) {
stemItemRefsRef.current[stemItem.id] = button;
},
onClick: dismissible ? partial(destroyBlank, stemItem) : null,
size: "large",
text: /*#__PURE__*/ jsx(AccessibleContent, {
alt: t('Delete "{ text }" blank', {
text: text
})
}, text),
variant: "inline"
});
};
var renderStemItem = function(stemItem) {
return stemItem.type === 'text' ? renderContentEditable(stemItem) : renderBlank(stemItem);
};
stemItemRefsRef.current = {};
return /*#__PURE__*/ jsx("div", {
ref: domRef,
onFocus: partial(setFocus, true),
onBlur: partial(setFocus, false)
}, /*#__PURE__*/ jsx(Errors, {
errorList: errors
}, stemItems.map(renderStemItem)), /*#__PURE__*/ jsx(View, {
as: "div",
borderWidth: "small none none none",
margin: "small none"
}));
}
BaseEditStem.displayName = 'EditStem';
BaseEditStem.componentId = 'QuizzesEditStem';
BaseEditStem.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
};
BaseEditStem.defaultProps = {
disabled: false,
errors: void 0
};
export default withStyleOverrides(generateStyle, generateComponentTheme)(BaseEditStem);