UNPKG

@mirrormedia/lilith-draft-editor

Version:
141 lines (125 loc) 5.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BGImageBlock = BGImageBlock; exports.BGImageEditorBlock = BGImageEditorBlock; var _react = _interopRequireWildcard(require("react")); var _styledComponents = _interopRequireDefault(require("styled-components")); var _draftConverter = _interopRequireDefault(require("../../../draft-js/draft-converter")); var _backgroundImage = require("../../../draft-js/buttons/background-image"); 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 BGImageRenderWrapper = _styledComponents.default.div` padding: 30px; position: relative; width: 100%; height: 100%; background-image: url(${({ image }) => image}); background-size: cover; background-position: center center; ${({ textBlockAlign }) => { if (textBlockAlign === 'left') { return `padding-right: 50%;`; } else if (textBlockAlign === 'right') { return `padding-left: 50%;`; } else if (textBlockAlign === 'bottom') { return `padding-top: 50%;`; } }} `; const BGImageRenderBody = _styledComponents.default.div` background: rgba(0, 0, 0, 0.5); padding: 4px 20px; margin-bottom: 10px; `; function BGImageBlock(props) { var _image$imageFile, _image$resized, _image$resized2; const { block, contentState } = props; const entityKey = block.getEntityAt(0); const entity = contentState.getEntity(entityKey); const { textBlockAlign, image, body } = entity.getData(); return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(BGImageRenderWrapper, { image: (image === null || image === void 0 ? void 0 : (_image$imageFile = image.imageFile) === null || _image$imageFile === void 0 ? void 0 : _image$imageFile.url) || (image === null || image === void 0 ? void 0 : (_image$resized = image.resized) === null || _image$resized === void 0 ? void 0 : _image$resized.w800) || (image === null || image === void 0 ? void 0 : (_image$resized2 = image.resized) === null || _image$resized2 === void 0 ? void 0 : _image$resized2.original), textBlockAlign: textBlockAlign }, /*#__PURE__*/_react.default.createElement(BGImageRenderBody, { dangerouslySetInnerHTML: { __html: body } }))); } const BGImageRenderButton = _styledComponents.default.span` cursor: pointer; background-color: white; padding: 6px; border-radius: 6px; `; function BGImageEditorBlock(props) { const [toShowInput, setToShowInput] = (0, _react.useState)(false); const { block, blockProps, contentState } = props; const { onEditStart, onEditFinish, renderBasicEditor } = blockProps; const entityKey = block.getEntityAt(0); const entity = contentState.getEntity(entityKey); const { textBlockAlign, image, rawContentState } = entity.getData(); const onChange = ({ textBlockAlign: newTextBlockAlign, image: newImage, rawContentState: newRawContentState }) => { // close `BGImageInput` setToShowInput(false); onEditFinish({ entityKey, entityData: { textBlockAlign: newTextBlockAlign, image: newImage, body: _draftConverter.default.convertToHtml(newRawContentState), rawContentState: newRawContentState } }); }; return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_backgroundImage.BGImageInput, { renderBasicEditor: renderBasicEditor, textBlockAlign: textBlockAlign, image: image, rawContentStateForBGImageEditor: rawContentState, onChange: onChange, onCancel: () => { onEditFinish({}); setToShowInput(false); }, isOpen: toShowInput }), /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(BGImageBlock, props), /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(BGImageRenderButton, { onClick: () => { // call `onEditStart` prop as we are trying to update the BGImage entity onEditStart(); // open `BGImageInput` setToShowInput(true); } }, /*#__PURE__*/_react.default.createElement("i", { className: "fa-solid fa-pen" }), /*#__PURE__*/_react.default.createElement("span", null, "Modify"))))); }