123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619 |
- "use strict";
-
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
-
- var _button = require("antd/es/button");
-
- var _button2 = _interopRequireDefault(_button);
-
- var _popover = require("antd/es/popover");
-
- var _popover2 = _interopRequireDefault(_popover);
-
- var _icon = require("antd/es/icon");
-
- var _icon2 = _interopRequireDefault(_icon);
-
- var _message2 = require("antd/es/message");
-
- var _message3 = _interopRequireDefault(_message2);
-
- var _input = require("antd/es/input");
-
- var _input2 = _interopRequireDefault(_input);
-
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
-
- require("antd/es/button/style/css");
-
- require("antd/es/popover/style/css");
-
- require("antd/es/icon/style/css");
-
- require("antd/es/message/style/css");
-
- require("antd/es/input/style/css");
-
- var _react = require("react");
-
- var _react2 = _interopRequireDefault(_react);
-
- var _propTypes = require("prop-types");
-
- var _propTypes2 = _interopRequireDefault(_propTypes);
-
- var _classnames = require("classnames");
-
- var _classnames2 = _interopRequireDefault(_classnames);
-
- var _reactIntlUniversal = require("react-intl-universal");
-
- var _reactIntlUniversal2 = _interopRequireDefault(_reactIntlUniversal);
-
- var _dayjs = require("dayjs");
-
- var _dayjs2 = _interopRequireDefault(_dayjs);
-
- var _shortid = require("shortid");
-
- var _shortid2 = _interopRequireDefault(_shortid);
-
- var _constant = require("../../constant");
-
- var _helper = require("../../helper");
-
- var _Upload = require("./Upload");
-
- var _Upload2 = _interopRequireDefault(_Upload);
-
- var _Emoji = require("./Emoji");
-
- var _Emoji2 = _interopRequireDefault(_Emoji);
-
- var _Comment = require("../../Comment");
-
- var _Comment2 = _interopRequireDefault(_Comment);
-
- require("./index.css");
-
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
-
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
-
- function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
-
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
-
- var TextArea = _input2.default.TextArea;
-
-
- var client = function client(oss) {
- return new window.OSS.Wrapper({
- accessKeyId: oss.access_key_id,
- accessKeySecret: oss.access_key_secret,
- stsToken: oss.security_token,
- endpoint: _constant.OSS_ENDPOINT, //常量,你可以自己定义
- bucket: _constant.OSS_BUCKET
- });
- };
-
- var uploadPath = function uploadPath(path, file) {
- return path + "/" + (0, _dayjs2.default)().format("YYYYMMDD") + "/" + _shortid2.default.generate() + "." + file.type.split("/")[1];
- };
-
- var UploadToOss = function UploadToOss(oss, path, file) {
- var url = uploadPath(path, file);
- return new Promise(function (resolve, reject) {
- client(oss).multipartUpload(url, file).then(function (data) {
- resolve(data);
- }).catch(function (error) {
- reject(error);
- });
- });
- };
-
- var Editor = function (_React$Component) {
- _inherits(Editor, _React$Component);
-
- function Editor(props) {
- _classCallCheck(this, Editor);
-
- var _this = _possibleConstructorReturn(this, (Editor.__proto__ || Object.getPrototypeOf(Editor)).call(this, props));
-
- _this.state = {
- showUpload: false,
- value: props.value || "", // 编辑器里面的值
- fileList: props.fileList || [], // 图片列表
- fileMap: {}, // 已经上传的图片路径和 uid 的映射 { uid: path }
- uploadVisible: false
- };
- _this.handleChange = _this.handleChange.bind(_this);
- _this.handleClickEmoji = _this.handleClickEmoji.bind(_this);
- _this.handleChangeFileList = _this.handleChangeFileList.bind(_this);
- _this.handleShowUpload = _this.handleShowUpload.bind(_this);
- _this.handleUpload = _this.handleUpload.bind(_this);
- _this.handleSubmit = _this.handleSubmit.bind(_this);
- _this.handlePaste = _this.handlePaste.bind(_this);
- _this.resetState = _this.resetState.bind(_this);
- _this.handleEmojiScroll = _this.handleEmojiScroll.bind(_this);
- return _this;
- }
-
- _createClass(Editor, [{
- key: "componentDidMount",
- value: function componentDidMount() {
- this.props.app.sOssSts();
- if ((0, _helper.isFunction)(this.props.onRef)) {
- this.props.onRef(this);
- }
- }
- }, {
- key: "handleEmojiScroll",
- value: function handleEmojiScroll(e) {
- if (!this.emoji) {
- return;
- }
- e.preventDefault();
- if (e.deltaY > 0) {
- this.emoji.next();
- } else if (e.deltaY < 0) {
- this.emoji.prev();
- }
- }
-
- /**
- * 编辑器的值改变事件
- * 将最新的值存储到 state 中
- * @param {string} value 输入的值
- */
-
- }, {
- key: "handleChange",
- value: function handleChange(value) {
- this.setState({ value: value });
- if (this.props.onChange) {
- this.props.onChange(value);
- }
- }
-
- /**
- * 点击 emoji 的事件
- * 点击后,需要将改 emoji 插入到编辑器中
- * 插入的值为 [emoji chinese name]
- * 参数 emoji 即为 emoji chinese name
- * @param {string}} emoji emoji 的中文,如 微笑
- */
-
- }, {
- key: "handleClickEmoji",
- value: function handleClickEmoji(emoji) {
- var value = this.props.value || this.state.value;
- value += "[" + emoji + "]";
- this.handleChange(value);
- }
-
- /**
- * 监听文件列表改变事件
- * @param {Array} fileList 文件列表
- */
-
- }, {
- key: "handleChangeFileList",
- value: function handleChangeFileList(fileList) {
- var list = fileList;
- if (fileList.length > this.props.maxUpload) {
- list = fileList.slice(0, this.props.maxUpload);
- }
- this.props.handleChangeFileList(list);
- this.setState({ fileList: list });
- }
-
- /**
- * 控制上传 Popover 的显示和隐藏
- * @param {boolean} showUpload 是否显示上传的 Popover
- */
-
- }, {
- key: "handleShowUpload",
- value: function handleShowUpload(showUpload) {
- if (typeof showUpload === "boolean") {
- this.setState({ showUpload: showUpload });
- } else {
- this.setState({ showUpload: !this.state.showUpload });
- }
- }
-
- /**
- * 上传文件
- * @param {object} param 文件对象
- */
-
- }, {
- key: "handleUpload",
- value: function handleUpload(_ref) {
- var uid = _ref.uid,
- path = _ref.path;
- var fileMap = this.state.fileMap;
- var fileList = this.state.fileList;
-
- fileMap[uid] = path;
- fileList = fileList.map(function (item) {
- if (item.uid === uid) {
- item.thumbUrl = _constant.OSS_LINK + path;
- }
- return item;
- });
- this.props.handleChangeFileList(fileList);
- this.setState({ fileMap: fileMap, fileList: fileList });
- }
-
- /**
- * 粘贴回调
- */
-
- }, {
- key: "handlePaste",
- value: function handlePaste(e) {
- var _this2 = this;
-
- if (this.state.fileList.length >= this.props.maxUpload) {
- return;
- }
- var items = e.clipboardData && e.clipboardData.items;
- var file = null;
- if (items && items.length) {
- for (var i = 0; i < items.length; i++) {
- if (items[i].type.indexOf("image") !== -1) {
- file = items[i].getAsFile();
- break;
- }
- }
- }
- this.setState({
- uploadVisible: true
- });
- var reader = new FileReader();
- reader.readAsDataURL(file);
- reader.onloadend = function () {
- // DRIVER_LICENSE_PATH oss 的存储路径位置
- UploadToOss(_this2.props.app.oss, _constant.DRIVER_LICENSE_PATH, file).then(function (data) {
- var fileList = _this2.state.fileList.concat({
- url: _constant.OSS_LINK + data.name,
- thumbUrl: _constant.OSS_LINK + data.name,
- type: file.type,
- uid: new Date().valueOf()
- });
- _this2.props.handleChangeFileList(fileList);
- _this2.setState({
- fileList: fileList
- });
- }).catch(function (e) {
- var msg = e.message || _constant.ERROR_DEFAULT;
- if (_this2.props.showError) {
- _message3.default.error(msg);
- }
- if (_this2.props.onError) {
- _this2.props.onError(msg, { response: e.response });
- }
- });
- };
- }
-
- /**
- * 提交编辑器内容
- * 提交功能,交给父组件来实现
- * 需要父组件传入 onSubmit
- */
-
- }, {
- key: "handleSubmit",
- value: function handleSubmit() {
- var _this3 = this;
-
- var maxLength = this.props.maxLength;
- var _state = this.state,
- value = _state.value,
- fileMap = _state.fileMap,
- fileList = _state.fileList;
-
- if (value.length > maxLength) {
- // message.error(`字数不得超过${maxLength}字`);
- _message3.default.error(_reactIntlUniversal2.default.get("editor.maxLength", { maxLength: maxLength }));
- return;
- }
- var files = [];
- if (fileList.length) {
- fileList.forEach(function (item) {
- if (item.url) {
- files.push(item.url);
- return;
- }
- if (!fileMap[item.uid]) {
- return;
- }
- files.push("" + _constant.OSS_LINK + fileMap[item.uid]);
- });
- }
- if (this.props.beforeSubmit) {
- Promise.resolve(this.props.beforeSubmit({ text: value, files: files })).then(function (res) {
- if (!(res === false)) {
- _this3.props.onSubmit({ text: value, files: files }, function (res, action) {
- _this3.resetState();
- if (action === "comment" && _this3.props.onCommentSuccess) {
- _this3.props.onCommentSuccess(res);
- }
- });
- }
- });
- } else {
- this.props.onSubmit({ text: value, files: files }, function (res, action) {
- _this3.resetState();
- if (action === "comment" && _this3.props.onCommentSuccess) {
- _this3.props.onCommentSuccess(res);
- }
- });
- }
- }
- }, {
- key: "resetState",
- value: function resetState() {
- this.handleChange("");
- this.handleChangeFileList([]);
- this.setState({
- showUpload: false,
- value: "",
- fileList: [],
- fileMap: {}
- });
- }
- }, {
- key: "checkDisabledSubmit",
- value: function checkDisabledSubmit() {
- var _props = this.props,
- btnDisabled = _props.btnDisabled,
- value = _props.value,
- fileList = _props.fileList;
-
- if (btnDisabled) {
- return true;
- }
- if (value && value !== "") {
- return false;
- }
- if (this.state.value && this.state.value !== "") {
- return false;
- }
- if (fileList && fileList.length > 0) {
- return false;
- }
- if (this.state.fileList.length > 0) {
- return false;
- }
- return true;
- }
- }, {
- key: "render",
- value: function render() {
- var _this4 = this;
-
- var _props2 = this.props,
- value = _props2.value,
- rows = _props2.rows,
- showEmoji = _props2.showEmoji,
- showUpload = _props2.showUpload,
- multiple = _props2.multiple,
- emojiPopoverPlacement = _props2.emojiPopoverPlacement,
- uploadPopoverPlacement = _props2.uploadPopoverPlacement,
- uploadOverlayClassName = _props2.uploadOverlayClassName,
- fileList = _props2.fileList,
- maxUpload = _props2.maxUpload,
- btnLoading = _props2.btnLoading,
- button = _props2.button,
- emojiToolIcon = _props2.emojiToolIcon,
- imageToolIcon = _props2.imageToolIcon,
- maxLength = _props2.maxLength,
- autoFocus = _props2.autoFocus;
-
- var placeholder = this.props.placeholder || _reactIntlUniversal2.default.get("editor.placeholder");
- var btnSubmitText = this.props.btnSubmitText || _reactIntlUniversal2.default.get("editor.SubmitBtn");
- var handleSubmit = this.handleSubmit;
- var disabledSubmit = this.checkDisabledSubmit();
- var inputValue = value || this.state.value;
- var uploadFileList = fileList || this.state.fileList;
- return _react2.default.createElement(
- "div",
- { className: "comment-editor-container", onPaste: this.handlePaste },
- _react2.default.createElement("div", {
- className: (0, _classnames2.default)({
- "comment-editor-toolbar": true,
- "comment-editor-toolbar-error": inputValue.length > maxLength
- })
- }),
- _react2.default.createElement(
- "div",
- { className: "comment-editor" },
- _react2.default.createElement(TextArea, {
- value: inputValue,
- onChange: function onChange(e) {
- return _this4.handleChange(e.target.value);
- },
- rows: rows,
- placeholder: placeholder,
- autoFocus: autoFocus
- }),
- _react2.default.createElement(
- "div",
- { className: "comment-toolbar" },
- _react2.default.createElement(
- "div",
- { className: "comment-toolbar-left" },
- showEmoji && _react2.default.createElement(
- _popover2.default,
- {
- trigger: "click",
- placement: emojiPopoverPlacement,
- autoAdjustOverflow: false,
- overlayStyle: { zIndex: 9999 },
- content: _react2.default.createElement(
- "div",
- {
- style: { width: 240, height: 205 },
- onWheel: this.handleEmojiScroll
- },
- _react2.default.createElement(_Emoji2.default, {
- onClick: this.handleClickEmoji,
- ref: function ref(node) {
- _this4.emoji = node;
- },
- emojiList: this.props.app.emojiList
- })
- ),
- overlayClassName: "comment-emoji-popover"
- },
- emojiToolIcon || _react2.default.createElement(_icon2.default, { type: "smile-o", className: "comment-toolbar-icon" })
- ),
- showUpload ? _react2.default.createElement(
- _popover2.default,
- {
- trigger: "click"
- // TODO: 针对非 react.js,直接使用 click 事件来控制展开或关闭
- , visible: this.state.uploadVisible,
- placement: uploadPopoverPlacement,
- overlayClassName: uploadOverlayClassName,
- autoAdjustOverflow: false,
- overlayStyle: { zIndex: 9999 },
- onVisibleChange: function onVisibleChange(visible) {
- _this4.setState({
- uploadVisible: visible
- });
- },
- content: _react2.default.createElement(
- "div",
- {
- style: {
- width: 336, // 一行显示3张
- minHeight: 100,
- margin: "0 auto"
- }
- },
- _react2.default.createElement(_Upload2.default, {
- onRef: function onRef(node) {
- return _this4.uploadRef = node;
- },
- multiple: multiple,
- onChangeFileList: this.handleChangeFileList,
- onUpload: this.handleUpload,
- maxUpload: maxUpload,
- fileList: uploadFileList,
- showError: this.props.showError,
- onError: this.props.onError
- }),
- _react2.default.createElement("div", { className: "clearfix" })
- ),
- title: _react2.default.createElement(
- "div",
- { style: { margin: "5px auto" } },
- _react2.default.createElement(
- "span",
- null,
- _reactIntlUniversal2.default.get("editor.uploadTip"),
- maxUpload >= 2 ? _react2.default.createElement(
- "span",
- { style: { color: "#666", fontWeight: 400 } },
- _reactIntlUniversal2.default.get("editor.uploadCount", {
- count: maxUpload - uploadFileList.length
- })
- ) : null
- )
- )
- },
- imageToolIcon ? _react2.default.cloneElement(imageToolIcon, {
- onClick: function onClick() {
- return _this4.handleShowUpload(true);
- }
- }) : _react2.default.createElement(_icon2.default, {
- type: "picture",
- className: "comment-toolbar-icon",
- style: { marginLeft: 20 },
- onClick: function onClick() {
- return _this4.handleShowUpload(true);
- }
- })
- ) : null
- ),
- _react2.default.createElement(
- "div",
- { className: "comment-toolbar-right" },
- button ? _react2.default.cloneElement(button, {
- onClick: button.props.onClick || handleSubmit
- }) : _react2.default.createElement(
- _button2.default,
- {
- onClick: function onClick() {
- return _this4.handleSubmit();
- },
- type: "primary",
- loading: btnLoading,
- disabled: disabledSubmit
- },
- btnSubmitText
- )
- )
- )
- )
- );
- }
- }]);
-
- return Editor;
- }(_react2.default.Component);
-
- Editor.propTypes = {
- rows: _propTypes2.default.number,
- placeholder: _propTypes2.default.string,
- showEmoji: _propTypes2.default.bool,
- emojiPopoverPlacement: _propTypes2.default.string,
- showUpload: _propTypes2.default.bool,
- uploadPopoverPlacement: _propTypes2.default.string,
- uploadOverlayClassName: _propTypes2.default.string,
- multiple: _propTypes2.default.bool,
- closeUploadWhenBlur: _propTypes2.default.bool,
- maxUpload: _propTypes2.default.number,
- value: _propTypes2.default.string,
- onChange: _propTypes2.default.func,
- onSubmit: _propTypes2.default.func,
- beforeSubmit: _propTypes2.default.func,
- btnSubmitText: _propTypes2.default.string,
- btnLoading: _propTypes2.default.bool,
- btnDisabled: _propTypes2.default.bool,
- button: _propTypes2.default.node,
- emojiToolIcon: _propTypes2.default.node,
- imageToolIcon: _propTypes2.default.node,
- showError: _propTypes2.default.bool,
- onError: _propTypes2.default.func,
- maxLength: _propTypes2.default.number
- };
-
- Editor.defaultProps = {
- rows: 5,
- // placeholder: "说点什么吧",
- showEmoji: true,
- showUpload: true,
- multiple: true,
- emojiPopoverPlacement: "bottomLeft",
- closeUploadWhenBlur: false,
- uploadPopoverPlacement: "bottomLeft",
- uploadOverlayClassName: "",
- maxUpload: 1,
- // btnSubmitText: "发表",
- btnLoading: false,
- btnDisabled: false,
- showError: true,
- maxLength: 5000,
- handleChangeFileList: function handleChangeFileList() {}
- };
-
- exports.default = (0, _Comment2.default)(Editor);
- //# sourceMappingURL=index.js.map
|