UNPKG

@aliretail/react-materials-components

Version:
174 lines (144 loc) 5.68 kB
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _extends from "@babel/runtime/helpers/extends"; import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose"; var _excluded = ["readOnly", "path", "value", "materialCode"]; import _regeneratorRuntime from "@babel/runtime/regenerator"; import React, { forwardRef } from 'react'; import * as PropTypes from 'prop-types'; import { PowerEditor as RichEditor } from '@aliretail/epoch-base-materials'; import { omit as ROmit } from 'ramda'; import { getRemoteFileContent } from "./utils/api"; import PowerDialog from "./components/preview/index"; import MaterialDialog from "./components/MaterialDialog/index"; import { CLASS_PREFIX, COMPONENT_NAME } from "./utils/static"; var PROPS_LIST = ['disabled', 'readonly', 'isPreview', 'state', 'contentHeight', 'mode', 'FileComponentLabel', 'FileComponent', 'VideoComponent', 'width', 'value', 'onChange', 'multiSelect', 'readOnly', 'path', 'materialCode']; var PowerEditor = /*#__PURE__*/function (_React$Component) { _inheritsLoose(PowerEditor, _React$Component); function PowerEditor() { var _this; for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this; _this.state = { htmlData: '' }; _this.getMaterialDialog = /*#__PURE__*/forwardRef(function (props, ref) { var materialDialogProps = ROmit(PROPS_LIST, _this.props); var materialCode = _this.props.materialCode; if (!materialCode) { console.warn('error: @aliretail/react-materials-components/PowerEditor: 请填写素材中心相应code'); return null; } return /*#__PURE__*/React.createElement(MaterialDialog, _extends({}, props, materialDialogProps, { code: materialCode, ref: ref })); }); _this.powerValueChange = function (value) { var onChange = _this.props.onChange; onChange(value); _this.setState({ htmlData: value }); }; return _this; } var _proto = PowerEditor.prototype; _proto.componentDidMount = function componentDidMount() { this.setHtmlData(); }; _proto.componentDidUpdate = function componentDidUpdate(prevProps) { var prevPath = prevProps.path, prevValue = prevProps.value, prevReadOnly = prevProps.readOnly; var _this$props = this.props, path = _this$props.path, readOnly = _this$props.readOnly, value = _this$props.value; // 当path、value状态改变时 且非readOnly状态重新获取富文本内容 // const boo = !readOnly && (prevPath !== path || prevValue !== value); var pathBoo = !readOnly && prevPath !== path; var valueBoo = !readOnly && prevValue !== value; if (pathBoo) { this.setHtmlData(); } else if (valueBoo) { this.powerValueChange(value); } if (prevReadOnly !== readOnly) { this.setHtmlData(); } } // eslint-disable-next-line @typescript-eslint/member-ordering ; _proto.setHtmlData = /*#__PURE__*/function () { var _setHtmlData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var _this$props2, value, readOnly, path, onChange, tempContent; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _this$props2 = this.props, value = _this$props2.value, readOnly = _this$props2.readOnly, path = _this$props2.path, onChange = _this$props2.onChange; tempContent = value; if (!(!readOnly && !!path)) { _context.next = 6; break; } _context.next = 5; return getRemoteFileContent(path); case 5: tempContent = _context.sent; case 6: onChange(tempContent); this.setState({ htmlData: tempContent }); case 8: case "end": return _context.stop(); } } }, _callee, this); })); function setHtmlData() { return _setHtmlData.apply(this, arguments); } return setHtmlData; }(); _proto.render = function render() { var _this$props3 = this.props, readOnly = _this$props3.readOnly, path = _this$props3.path, value = _this$props3.value, materialCode = _this$props3.materialCode, others = _objectWithoutPropertiesLoose(_this$props3, _excluded); var htmlData = this.state.htmlData; var richEditorProps = _extends({}, materialCode ? { FileComponent: this.getMaterialDialog, VideoComponent: this.getMaterialDialog } : {}, others, { value: htmlData, onChange: this.powerValueChange }); return /*#__PURE__*/React.createElement("div", { className: CLASS_PREFIX, "data-name": COMPONENT_NAME }, readOnly ? /*#__PURE__*/React.createElement(PowerDialog, { content: value, path: path }) : /*#__PURE__*/React.createElement(RichEditor, richEditorProps)); }; return PowerEditor; }(React.Component); PowerEditor.propTypes = { readOnly: PropTypes.bool, path: PropTypes.string, value: PropTypes.string, onChange: PropTypes.func }; PowerEditor.defaultProps = { readOnly: false, path: '', value: '', onChange: function onChange() {} }; export default PowerEditor;