UNPKG

usfm-editor

Version:
169 lines (126 loc) 7.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ChapterEditor = void 0; exports.withChapterPaging = withChapterPaging; var React = _interopRequireWildcard(require("react")); var _UsfmEditor = require("../UsfmEditor"); var _NoopUsfmEditor = require("../NoopUsfmEditor"); var _lodash = require("lodash"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _defineProperty(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 _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function withChapterPaging(WrappedEditor) { var _WrappedEditor$displa; const fc = /*#__PURE__*/React.forwardRef(({ ...props }, ref) => /*#__PURE__*/React.createElement(ChapterEditor, _extends({}, props, { wrappedEditor: WrappedEditor, ref: ref // used to access the ChapterEditor and its API }))); fc.displayName = ((_WrappedEditor$displa = WrappedEditor.displayName) !== null && _WrappedEditor$displa !== void 0 ? _WrappedEditor$displa : "") + "WithChapterPaging"; return fc; } class ChapterEditor extends React.Component { constructor(props) { super(props); _defineProperty(this, "wholeBookUsfm", void 0); _defineProperty(this, "wrappedEditorRef", /*#__PURE__*/React.createRef()); _defineProperty(this, "wrappedEditorInstance", () => { var _this$wrappedEditorRe; return (_this$wrappedEditorRe = this.wrappedEditorRef.current) !== null && _this$wrappedEditorRe !== void 0 ? _this$wrappedEditorRe : new _NoopUsfmEditor.NoopUsfmEditor(); }); _defineProperty(this, "getMarksAtSelection", () => this.wrappedEditorInstance().getMarksAtSelection()); _defineProperty(this, "addMarkAtSelection", mark => this.wrappedEditorInstance().addMarkAtSelection(mark)); _defineProperty(this, "removeMarkAtSelection", mark => this.wrappedEditorInstance().removeMarkAtSelection(mark)); _defineProperty(this, "getParagraphTypesAtSelection", () => this.wrappedEditorInstance().getParagraphTypesAtSelection()); _defineProperty(this, "setParagraphTypeAtSelection", marker => this.wrappedEditorInstance().setParagraphTypeAtSelection(marker)); _defineProperty(this, "goToVerse", verseObject => { const chapterUsfm = getSingleChapterAndBookHeaders(this.wholeBookUsfm, verseObject.chapter); const goToVersePropValue = { chapter: verseObject.chapter, verse: verseObject.verse, key: Date.now() }; // We could alternatively call the wrapped editor's goToVerse() function, // in a callback of setState(). if (chapterUsfm) { this.setState({ chapterUsfmString: chapterUsfm, goToVersePropValue: goToVersePropValue }); } }); _defineProperty(this, "handleEditorChange", chapterUsfm => { if (this.props.onChange) { this.wholeBookUsfm = getUpdatedWholeBookUsfm(chapterUsfm, this.wholeBookUsfm); this.props.onChange(this.wholeBookUsfm); } }); this.wholeBookUsfm = props.usfmString; this.state = { chapterUsfmString: props.usfmString, goToVersePropValue: props.goToVerse }; } componentDidMount() { this.goToVerse(this.props.goToVerse || ChapterEditor.defaultGoToVerse); } componentDidUpdate(prevProps) { if (!(0, _lodash.isEqual)(prevProps.usfmString, this.props.usfmString)) { this.wholeBookUsfm = this.props.usfmString; this.goToVerse(this.props.goToVerse || ChapterEditor.defaultGoToVerse); } else if (!(0, _lodash.isEqual)(prevProps.goToVerse, this.props.goToVerse) && this.props.goToVerse) { this.goToVerse(this.props.goToVerse); } } render() { return /*#__PURE__*/React.createElement(this.props.wrappedEditor, _extends({}, this.props, { ref: this.wrappedEditorRef, onChange: this.handleEditorChange, usfmString: this.state.chapterUsfmString, goToVerse: this.state.goToVersePropValue, key: this.state.chapterUsfmString })); } } exports.ChapterEditor = ChapterEditor; _defineProperty(ChapterEditor, "propTypes", _UsfmEditor.usfmEditorPropTypes); _defineProperty(ChapterEditor, "defaultGoToVerse", { chapter: 1, verse: 1 }); _defineProperty(ChapterEditor, "defaultProps", { ..._UsfmEditor.usfmEditorDefaultProps, goToVerse: ChapterEditor.defaultGoToVerse }); function getSingleChapterAndBookHeaders(wholeBookUsfm, chapterNum) { const bookHeaders = getBookHeaders(wholeBookUsfm); const allChapters = getChapterUsfmArray(wholeBookUsfm); const regExp = chapterNumRegex(chapterNum); const chapter = allChapters.find(chapUsfm => regExp.test(chapUsfm)); return chapter ? bookHeaders + chapter : undefined; } function getUpdatedWholeBookUsfm(chapterUsfm, wholeBookUsfm) { var _thisChapter$match; const bookHeaders = getBookHeaders(chapterUsfm); const allChapters = getChapterUsfmArray(wholeBookUsfm); const thisChapter = chapterUsfm.substring(chapterUsfm.indexOf("\\c")) + "\n"; const chapterNum = (_thisChapter$match = thisChapter.match(/^\\c\s*(\d+)/)) === null || _thisChapter$match === void 0 ? void 0 : _thisChapter$match.slice(1)[0]; if (chapterNum) { const regExp = chapterNumRegex(parseInt(chapterNum)); const thisChapterIdx = allChapters.findIndex(chapUsfm => regExp.test(chapUsfm)); // Replace this chapter's contents in the book usfm allChapters.splice(thisChapterIdx, 1, thisChapter); } else { console.error("Unexpected state: could not find chapter number in chapter usfm"); } return bookHeaders + allChapters.join(""); } function getBookHeaders(usfm) { return usfm.substring(0, usfm.indexOf("\\c")); } function getChapterUsfmArray(wholeBookUsfm) { return wholeBookUsfm.split("\\c").map(chapStr => "\\c" + chapStr).slice(1); // Remove the first element, which is the book headers } function chapterNumRegex(chapterNum) { return new RegExp("\\c\\s*" + chapterNum + "\\s*"); }