@mirrormedia/lilith-draft-editor
Version:
## Installation
109 lines (94 loc) • 4.06 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.EmbeddedCodeButton = EmbeddedCodeButton;
var _react = _interopRequireWildcard(require("react"));
var _draftJs = require("draft-js");
var _modals = require("@keystone-ui/modals");
var _fields = require("@keystone-ui/fields");
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 EmbeddedCodeButton(props) {
const {
editorState,
onChange,
className
} = props;
const [toShowInput, setToShowInput] = (0, _react.useState)(false);
const [inputValue, setInputValue] = (0, _react.useState)({
caption: '',
embeddedCode: ''
});
const promptForInput = () => {
setToShowInput(true);
setInputValue({
caption: '',
embeddedCode: ''
});
};
const confirmInput = () => {
const contentState = editorState.getCurrentContent();
const contentStateWithEntity = contentState.createEntity('EMBEDDEDCODE', 'IMMUTABLE', inputValue);
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
onChange(_draftJs.AtomicBlockUtils.insertAtomicBlock(newEditorState, entityKey, ' '));
setToShowInput(false);
setInputValue({
caption: '',
embeddedCode: ''
});
};
const input = /*#__PURE__*/_react.default.createElement(_modals.DrawerController, {
isOpen: toShowInput
}, /*#__PURE__*/_react.default.createElement(_modals.Drawer, {
title: `Insert Embedded Code` //isOpen={toShowInput}
,
actions: {
cancel: {
label: 'Cancel',
action: () => {
setToShowInput(false);
}
},
confirm: {
label: 'Confirm',
action: confirmInput
}
}
}, /*#__PURE__*/_react.default.createElement(_fields.TextInput, {
onChange: e => setInputValue({
caption: e.target.value,
embeddedCode: inputValue.embeddedCode
}),
type: "text",
placeholder: "Caption",
value: inputValue.caption,
style: {
marginBottom: '10px',
marginTop: '30px'
}
}), /*#__PURE__*/_react.default.createElement(_fields.TextArea, {
onChange: e => setInputValue({
caption: inputValue.caption,
embeddedCode: e.target.value
}),
placeholder: "Embedded Code",
type: "text",
value: inputValue.embeddedCode,
style: {
marginBottom: '30px'
}
})));
return /*#__PURE__*/_react.default.createElement(_react.Fragment, null, input, /*#__PURE__*/_react.default.createElement("div", {
onClick: () => {
promptForInput();
},
className: className
}, /*#__PURE__*/_react.default.createElement("i", {
className: "far"
}), /*#__PURE__*/_react.default.createElement("span", null, "Embed")));
}