UNPKG

@mirrormedia/lilith-draft-editor

Version:
146 lines (125 loc) 5.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InfoBoxButton = InfoBoxButton; exports.InfoBoxInput = InfoBoxInput; var _react = _interopRequireWildcard(require("react")); var _draftJs = require("draft-js"); var _modals = require("@keystone-ui/modals"); var _fields = require("@keystone-ui/fields"); var _draftConverter = _interopRequireDefault(require("../draft-converter")); var _styledComponents = _interopRequireDefault(require("styled-components")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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; } const TitleInput = (0, _styledComponents.default)(_fields.TextInput)` margin-top: 30px; margin-bottom: 10px; `; function InfoBoxInput(props) { const { isOpen, onChange, onCancel, title, rawContentStateForInfoBoxEditor, renderBasicEditor, decorators } = props; const rawContentState = rawContentStateForInfoBoxEditor || { blocks: [], entityMap: {} }; const initialInputValue = { title: title || '', // create an `editorState` from raw content state object editorStateOfBasicEditor: _draftJs.EditorState.createWithContent((0, _draftJs.convertFromRaw)(rawContentState), decorators) }; const [inputValue, setInputValue] = (0, _react.useState)(initialInputValue); const clearInputValue = () => { setInputValue(initialInputValue); }; const basicEditorJsx = renderBasicEditor({ editorState: inputValue.editorStateOfBasicEditor, onChange: editorStateOfBasicEditor => { setInputValue({ title: inputValue.title, editorStateOfBasicEditor }); } }); return /*#__PURE__*/_react.default.createElement(_modals.DrawerController, { isOpen: isOpen }, /*#__PURE__*/_react.default.createElement(_modals.Drawer, { title: `Insert Info Box`, actions: { cancel: { label: 'Cancel', action: () => { clearInputValue(); onCancel(); } }, confirm: { label: 'Confirm', action: () => { onChange({ title: inputValue.title, // convert `contentState` of the `editorState` into raw content state object rawContentState: (0, _draftJs.convertToRaw)(inputValue.editorStateOfBasicEditor.getCurrentContent()) }); clearInputValue(); } } } }, /*#__PURE__*/_react.default.createElement(TitleInput, { onChange: e => setInputValue({ title: e.target.value, editorStateOfBasicEditor: inputValue.editorStateOfBasicEditor }), type: "text", placeholder: "Title", value: inputValue.title }), basicEditorJsx)); } function InfoBoxButton(props) { const [toShowInput, setToShowInput] = (0, _react.useState)(false); const { className, editorState, onChange: onEditorStateChange, renderBasicEditor } = props; const onChange = ({ title, rawContentState }) => { const contentState = editorState.getCurrentContent(); // create an InfoBox entity const contentStateWithEntity = contentState.createEntity('INFOBOX', 'IMMUTABLE', { title, rawContentState, body: _draftConverter.default.convertToHtml(rawContentState) }); const entityKey = contentStateWithEntity.getLastCreatedEntityKey(); const newEditorState = _draftJs.EditorState.set(editorState, { currentContent: contentStateWithEntity }); //The third parameter here is a space string, not an empty string //If you set an empty string, you will get an error: Unknown DraftEntity key: null onEditorStateChange(_draftJs.AtomicBlockUtils.insertAtomicBlock(newEditorState, entityKey, ' ')); setToShowInput(false); }; return /*#__PURE__*/_react.default.createElement(_react.Fragment, null, /*#__PURE__*/_react.default.createElement(InfoBoxInput, { renderBasicEditor: renderBasicEditor, onChange: onChange, onCancel: () => { setToShowInput(false); }, isOpen: toShowInput }), /*#__PURE__*/_react.default.createElement("div", { className: className, onClick: () => { setToShowInput(true); } }, /*#__PURE__*/_react.default.createElement("span", null, "InfoBox"))); }