UNPKG

@mirrormedia/lilith-draft-editor

Version:
225 lines (197 loc) 8.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BGVideoButton = BGVideoButton; exports.BGVideoInput = BGVideoInput; var _react = _interopRequireWildcard(require("react")); var _draftJs = require("draft-js"); var _modals = require("@keystone-ui/modals"); var _button = require("@keystone-ui/button"); var _draftConverter = _interopRequireDefault(require("../draft-converter")); var _styledComponents = _interopRequireDefault(require("styled-components")); var _videoSelector = require("./selector/video-selector"); var _alignSelector = require("./selector/align-selector"); 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 Label = _styledComponents.default.label` display: block; font-weight: 600; margin: 10px 0; `; const VideoInputText = _styledComponents.default.span` display: inline-block; margin-right: 10px; `; function BGVideoInput(props) { var _inputValue$video; const { isOpen, onChange, onCancel, textBlockAlign, video, rawContentStateForBGVideoEditor, VideoSelector = _videoSelector.VideoSelector, renderBasicEditor, decorators } = props; const rawContentState = rawContentStateForBGVideoEditor || { blocks: [], entityMap: {} }; const options = [{ value: 'fixed', label: 'fixed (default)', isDisabled: false }, { value: 'bottom', label: 'bottom', isDisabled: false }, { value: 'left', label: 'left', isDisabled: false }, { value: 'right', label: 'right', isDisabled: false }]; const initialInputValue = { textBlockAlign: textBlockAlign || 'fixed', video: video || undefined, // 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 [toShowVideoSelector, setToShowVideoSelector] = (0, _react.useState)(false); const clearInputValue = () => { setInputValue(oldInputValue => ({ ...oldInputValue, editorStateOfBasicEditor: _draftJs.EditorState.createWithContent((0, _draftJs.convertFromRaw)({ blocks: [], entityMap: {} }), decorators) })); }; const onVideoSelectorChange = selectedVideosWithMeta => { var _selectedVideosWithMe; const video = selectedVideosWithMeta === null || selectedVideosWithMeta === void 0 ? void 0 : (_selectedVideosWithMe = selectedVideosWithMeta[0]) === null || _selectedVideosWithMe === void 0 ? void 0 : _selectedVideosWithMe.video; if (!video) { setToShowVideoSelector(false); return; } setInputValue(oldInputValue => ({ ...oldInputValue, video: video })); setToShowVideoSelector(false); }; const basicEditorJsx = renderBasicEditor({ editorState: inputValue.editorStateOfBasicEditor, onChange: editorStateOfBasicEditor => { setInputValue(oldInputValue => ({ ...oldInputValue, editorStateOfBasicEditor })); } }); (0, _react.useEffect)(() => { if (isOpen) { setInputValue(initialInputValue); } }, [isOpen]); return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, toShowVideoSelector && /*#__PURE__*/_react.default.createElement(VideoSelector, { onChange: onVideoSelectorChange }), /*#__PURE__*/_react.default.createElement(_modals.DrawerController, { isOpen: isOpen }, /*#__PURE__*/_react.default.createElement(_modals.Drawer, { title: `Insert Background Video`, actions: { cancel: { label: 'Cancel', action: () => { clearInputValue(); onCancel(); } }, confirm: { label: 'Confirm', action: () => { onChange({ textBlockAlign: inputValue.textBlockAlign, video: inputValue.video, // convert `contentState` of the `editorState` into raw content state object rawContentState: (0, _draftJs.convertToRaw)(inputValue.editorStateOfBasicEditor.getCurrentContent()) }); clearInputValue(); } } } }, /*#__PURE__*/_react.default.createElement(Label, null, "\u5F71\u7247"), /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(VideoInputText, null, (_inputValue$video = inputValue.video) !== null && _inputValue$video !== void 0 && _inputValue$video.name ? inputValue.video.name : '尚未選取影片'), /*#__PURE__*/_react.default.createElement(_button.Button, { type: "button", onClick: () => setToShowVideoSelector(true), tone: "passive" }, "\u6DFB\u52A0\u5F71\u7247")), /*#__PURE__*/_react.default.createElement(_alignSelector.AlignSelector, { align: inputValue.textBlockAlign, options: options, onChange: textBlockAlign => { setInputValue(oldInputValue => ({ ...oldInputValue, textBlockAlign })); } }), /*#__PURE__*/_react.default.createElement(Label, null, "\u5167\u6587"), basicEditorJsx))); } function BGVideoButton(props) { const [toShowInput, setToShowInput] = (0, _react.useState)(false); const { className, editorState, onChange: onEditorStateChange, VideoSelector, renderBasicEditor, decorators } = props; const onChange = ({ textBlockAlign, video, rawContentState }) => { const contentState = editorState.getCurrentContent(); // create an BGVideo entity const contentStateWithEntity = contentState.createEntity('BACKGROUNDVIDEO', 'IMMUTABLE', { textBlockAlign, video, 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(BGVideoInput, { renderBasicEditor: renderBasicEditor, onChange: onChange, onCancel: () => { setToShowInput(false); }, isOpen: toShowInput, VideoSelector: VideoSelector, decorators: decorators }), /*#__PURE__*/_react.default.createElement("div", { className: className, onClick: () => { setToShowInput(true); } }, /*#__PURE__*/_react.default.createElement("svg", { width: "16", height: "10", viewBox: "0 0 16 10", fill: "none", xmlns: "http://www.w3.org/2000/svg" }, /*#__PURE__*/_react.default.createElement("path", { d: "M10.6667 1.25V8.75C10.6667 9.44036 10.0697 10 9.33333 10H1.33333C0.596944 10 0 9.44036 0 8.75V1.25C0 0.559635 0.596944 0 1.33333 0H9.33333C10.0694 0 10.6667 0.559635 10.6667 1.25ZM16 1.65365V8.34375C16 9.00781 15.1897 9.39557 14.6003 9.01536L11.5556 7.04948V2.95052L14.6 0.982812C15.1917 0.602344 16 0.992188 16 1.65365Z", fill: "#6b7280" })), /*#__PURE__*/_react.default.createElement("span", null, "Background Video"))); }