UNPKG

@mirrormedia/lilith-draft-editor

Version:
116 lines (93 loc) 4.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AnnotationButton = AnnotationButton; var _react = _interopRequireWildcard(require("react")); var _draftJs = require("draft-js"); var _modals = require("@keystone-ui/modals"); var _draftConverter = _interopRequireDefault(require("../draft-converter")); 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; } function escapeHTML(s) { return s.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/'/g, '&#39;'); } function AnnotationButton(props) { const toggleEntity = _draftJs.RichUtils.toggleLink; const { isActive, editorState, onChange, renderBasicEditor, decorators, className } = props; const [toShowInput, setToShowInput] = (0, _react.useState)(false); const [inputValue, setInputValue] = (0, _react.useState)({ editorStateOfBasicEditor: _draftJs.EditorState.createEmpty(decorators) }); const promptForAnnotation = e => { e.preventDefault(); const selection = editorState.getSelection(); if (!selection.isCollapsed()) { setToShowInput(true); } }; const confirmAnnotation = () => { const contentState = editorState.getCurrentContent(); const rawContentState = (0, _draftJs.convertToRaw)(inputValue.editorStateOfBasicEditor.getCurrentContent()); const bodyHTML = _draftConverter.default.convertToHtml(rawContentState); const contentStateWithEntity = contentState.createEntity('ANNOTATION', 'MUTABLE', { rawContentState, bodyHTML, bodyEscapedHTML: escapeHTML(bodyHTML) }); const entityKey = contentStateWithEntity.getLastCreatedEntityKey(); const newEditorState = _draftJs.EditorState.set(editorState, { currentContent: contentStateWithEntity }); onChange(toggleEntity(newEditorState, newEditorState.getSelection(), entityKey)); setToShowInput(false); setInputValue({ editorStateOfBasicEditor: _draftJs.EditorState.createEmpty(decorators) }); }; const removeAnnotation = () => { const selection = editorState.getSelection(); if (!selection.isCollapsed()) { onChange(toggleEntity(editorState, selection, null)); } setToShowInput(false); setInputValue({ editorStateOfBasicEditor: _draftJs.EditorState.createEmpty(decorators) }); }; const basicEditorJsx = renderBasicEditor({ editorState: inputValue.editorStateOfBasicEditor, onChange: editorStateOfBasicEditor => { setInputValue({ editorStateOfBasicEditor }); } }); const urlInput = /*#__PURE__*/_react.default.createElement(_modals.DrawerController, { isOpen: toShowInput }, /*#__PURE__*/_react.default.createElement(_modals.Drawer, { title: "Insert Annotation", actions: { cancel: { label: 'Cancel', action: removeAnnotation }, confirm: { label: 'Confirm', action: confirmAnnotation } } }, basicEditorJsx)); return /*#__PURE__*/_react.default.createElement(_react.Fragment, null, urlInput, /*#__PURE__*/_react.default.createElement("div", { className: className, onMouseDown: isActive ? removeAnnotation : promptForAnnotation }, /*#__PURE__*/_react.default.createElement("span", null, "Annotation"))); }