index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { __awaiter, __rest } from "tslib";
  2. import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
  3. import { useEffect, useState } from "react";
  4. import { message } from "antd";
  5. import { Editor, Toolbar } from "@wangeditor/editor-for-react";
  6. import { getBaseURL, requestByPost } from "@dage/service";
  7. import { DageUploadType } from "../DageUpload";
  8. import { ACCEPT, validateFileType } from "../DageUpload/utils";
  9. import "@wangeditor/editor/dist/css/style.css";
  10. import { DageLoading } from "../DageLoading";
  11. import { LoadingOutlined } from "@ant-design/icons";
  12. import { assign } from "lodash";
  13. export const DageEditor = (_a) => {
  14. var { value, style, className, action, readOnly, fieldName = "file", maxImgSize = 2, maxVideoSize = 100, placeholder = "请输入内容", onChange, onCreated, onDestroyed, onFocus, onBlur, customPaste } = _a, tbConfig = __rest(_a, ["value", "style", "className", "action", "readOnly", "fieldName", "maxImgSize", "maxVideoSize", "placeholder", "onChange", "onCreated", "onDestroyed", "onFocus", "onBlur", "customPaste"]);
  15. const [uploadLoading, setUploadLoading] = useState(false);
  16. const [editor, setEditor] = useState(null);
  17. const validateFile = (file, type) => {
  18. if (!validateFileType(file, ACCEPT[type])) {
  19. message.error("选择的文件类型不正确!");
  20. return false;
  21. }
  22. const size = type === DageUploadType.IMG ? maxImgSize : maxVideoSize;
  23. // 校验文件大小
  24. const isLtM = file.size / 1024 / 1024 < size;
  25. if (!isLtM) {
  26. message.error(`最大支持 ${size}M!`);
  27. return false;
  28. }
  29. return true;
  30. };
  31. const uploadFile = (file, type) => __awaiter(void 0, void 0, void 0, function* () {
  32. const formData = new FormData();
  33. formData.append("type", type);
  34. if (file instanceof Blob) {
  35. formData.append(fieldName, file, file.name);
  36. }
  37. else {
  38. formData.append(fieldName, file);
  39. }
  40. const data = yield requestByPost(action, formData);
  41. return data;
  42. });
  43. // 工具栏配置
  44. const toolbarConfig = assign({
  45. excludeKeys: ["codeBlock", "group-video"],
  46. }, tbConfig);
  47. // 编辑器配置
  48. const editorConfig = {
  49. readOnly,
  50. placeholder,
  51. MENU_CONF: {
  52. uploadImage: {
  53. customUpload(file, insertFn) {
  54. return __awaiter(this, void 0, void 0, function* () {
  55. if (!validateFile(file, DageUploadType.IMG))
  56. return;
  57. try {
  58. setUploadLoading(true);
  59. const baseUrl = getBaseURL();
  60. const data = yield uploadFile(file, DageUploadType.IMG);
  61. insertFn(`${baseUrl}${data.filePath}`, data.fileName, data.filePath);
  62. }
  63. finally {
  64. setUploadLoading(false);
  65. }
  66. });
  67. },
  68. },
  69. uploadVideo: {
  70. customUpload(file, insertFn) {
  71. return __awaiter(this, void 0, void 0, function* () {
  72. if (!validateFile(file, DageUploadType.VIDEO))
  73. return;
  74. try {
  75. setUploadLoading(true);
  76. const baseUrl = getBaseURL();
  77. const data = yield uploadFile(file, DageUploadType.VIDEO);
  78. insertFn(`${baseUrl}${data.filePath}`, data.fileName, data.filePath);
  79. }
  80. finally {
  81. setUploadLoading(false);
  82. }
  83. });
  84. },
  85. },
  86. },
  87. onCreated,
  88. onDestroyed,
  89. onFocus,
  90. onBlur,
  91. customPaste,
  92. };
  93. useEffect(() => {
  94. return () => {
  95. if (editor == null)
  96. return;
  97. editor.destroy();
  98. setEditor(null);
  99. };
  100. }, [editor]);
  101. return (_jsxs("div", { className: className, style: Object.assign({ position: "relative", border: "1px solid var(--w-e-toolbar-border-color)" }, style), children: [_jsx(Toolbar, { editor: editor, defaultConfig: toolbarConfig, mode: "default", style: { borderBottom: "1px solid var(--w-e-toolbar-border-color)" } }), _jsx(Editor, { defaultConfig: editorConfig, value: value, onCreated: setEditor, onChange: (editor) => onChange === null || onChange === void 0 ? void 0 : onChange(editor.getHtml()), mode: "default", style: { height: "500px", overflowY: "hidden" } }), uploadLoading && (_jsx(DageLoading, { indicator: _jsx(LoadingOutlined, { style: { fontSize: 30 }, spin: true }) }))] }));
  102. };
  103. export * from "./types";