mirror of
https://github.com/doocs/md.git
synced 2024-11-24 19:10:34 +08:00
refactor: update upload component
This commit is contained in:
parent
2f3bdeadbf
commit
71704264c5
@ -237,9 +237,9 @@ function fileUpload(content, file) {
|
||||
case "github":
|
||||
return ghFileUpload(content, file.name);
|
||||
default:
|
||||
// return file.size / 1024 < 1024
|
||||
// ? giteeUpload(content, file.name)
|
||||
// : ghFileUpload(content, file.name);
|
||||
return file.size / 1024 < 1024
|
||||
? giteeUpload(content, file.name)
|
||||
: ghFileUpload(content, file.name);
|
||||
return ghFileUpload(content, file.name);
|
||||
}
|
||||
}
|
||||
|
@ -2,12 +2,6 @@ import fileApi from "../../api/file";
|
||||
|
||||
export function uploadImgFile(file) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const checkImageResult = isImageIllegal(file);
|
||||
if (checkImageResult) {
|
||||
reject(checkImageResult);
|
||||
return;
|
||||
}
|
||||
|
||||
const base64Reader = new FileReader();
|
||||
base64Reader.readAsDataURL(file);
|
||||
base64Reader.onload = function () {
|
||||
@ -23,13 +17,3 @@ export function uploadImgFile(file) {
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function isImageIllegal(file) {
|
||||
if (!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(file.name)) {
|
||||
return "请上传 JPG/PNG/GIF 格式的图片";
|
||||
}
|
||||
if (file.size > 5 * 1024 * 1024) {
|
||||
return "由于公众号限制,图片大小不能超过 5.0M";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -263,3 +263,10 @@ export function createTable({ data, rows, cols }) {
|
||||
|
||||
return table;
|
||||
}
|
||||
|
||||
export const toBase64 = file => new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.readAsDataURL(file);
|
||||
reader.onload = () => resolve(reader.result.split(",").pop());
|
||||
reader.onerror = error => reject(error);
|
||||
});
|
@ -48,7 +48,7 @@
|
||||
<i
|
||||
class="el-icon-s-grid"
|
||||
size="medium"
|
||||
@click="$emit('showDialogForm')"
|
||||
@click="$emit('show-dialog-form')"
|
||||
></i>
|
||||
</el-tooltip>
|
||||
<el-form size="mini" class="ctrl" :inline="true">
|
||||
|
@ -29,7 +29,8 @@
|
||||
:multiple="true"
|
||||
accept=".jpg, .jpeg, .png, .gif"
|
||||
name="file"
|
||||
:before-upload="beforeUpload"
|
||||
:before-upload="beforeImageUpload"
|
||||
:http-request="uploadImage"
|
||||
v-loading="uploadingImg"
|
||||
>
|
||||
<i class="el-icon-upload"></i>
|
||||
@ -305,6 +306,8 @@
|
||||
|
||||
<script>
|
||||
import { uploadImgFile } from "../../assets/scripts/uploadImageFile";
|
||||
import { toBase64 } from "../../assets/scripts/util";
|
||||
import fileApi from "../../api/file";
|
||||
|
||||
export default {
|
||||
props: {
|
||||
@ -516,30 +519,42 @@ export default {
|
||||
});
|
||||
},
|
||||
|
||||
// 图片上传前的处理
|
||||
beforeUpload(file) {
|
||||
const imgHost = localStorage.getItem("imgHost");
|
||||
const config = localStorage.getItem(`${imgHost}Config`);
|
||||
if (!config && imgHost !== "" && imgHost !== "default") {
|
||||
this.$message.error(`请先配置 ${imgHost} 图床参数`);
|
||||
return false;
|
||||
}
|
||||
beforeImageUpload(file) {
|
||||
let imgHost = localStorage.getItem("imgHost");
|
||||
imgHost = !imgHost ? "default" : imgHost;
|
||||
|
||||
const config = localStorage.getItem(`${imgHost}Config`);
|
||||
const maxSize = 5;
|
||||
|
||||
const isValidSuffix = /\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(
|
||||
file.name
|
||||
);
|
||||
const isLt5M = file.size / 1024 / 1024 <= maxSize;
|
||||
const isValidHost = imgHost == "default" || config;
|
||||
|
||||
if (!isValidSuffix) {
|
||||
this.$message.error("请上传 JPG/PNG/GIF 格式的图片");
|
||||
}
|
||||
if (!isLt5M) {
|
||||
this.$message.error(
|
||||
`由于公众号限制,图片大小不能超过 ${maxSize}M`
|
||||
);
|
||||
}
|
||||
if (!isValidHost) {
|
||||
this.$message.error(`请先配置 ${imgHost} 图床参数`);
|
||||
}
|
||||
return isValidSuffix && isLt5M && isValidHost;
|
||||
},
|
||||
uploadImage(params) {
|
||||
this.uploadingImg = true;
|
||||
uploadImgFile(file)
|
||||
uploadImgFile(params.file)
|
||||
.then((res) => {
|
||||
this.$emit("uploaded", res);
|
||||
this.uploadingImg = false;
|
||||
})
|
||||
.catch((err) => {
|
||||
this.uploadingImg = false;
|
||||
this.$message({
|
||||
showClose: true,
|
||||
message: err,
|
||||
type: "error",
|
||||
});
|
||||
this.$message.error(err);
|
||||
});
|
||||
return false;
|
||||
this.uploadingImg = false;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
@ -5,7 +5,6 @@ import ElementUI from "element-ui";
|
||||
import "element-ui/lib/theme-chalk/index.css";
|
||||
import "./plugins/element";
|
||||
import "codemirror/lib/codemirror.css";
|
||||
import "codemirror/theme/ambiance.css";
|
||||
import "codemirror/theme/xq-light.css";
|
||||
import "codemirror/mode/css/css";
|
||||
import "codemirror/mode/markdown/markdown";
|
||||
|
@ -152,7 +152,6 @@ const mutations = {
|
||||
},
|
||||
clearEditorToDefault(state) {
|
||||
const doc = formatDoc(DEFAULT_CONTENT);
|
||||
|
||||
state.editor.setValue(doc);
|
||||
state.cssEditor.setValue(DEFAULT_CSS_CONTENT);
|
||||
},
|
||||
|
@ -9,7 +9,7 @@
|
||||
@download="downloadEditorContent"
|
||||
@showCssEditor="showCssEditor = !showCssEditor"
|
||||
@show-about-dialog="aboutDialogVisible = true"
|
||||
@showDialogForm="dialogFormVisible = true"
|
||||
@show-dialog-form="dialogFormVisible = true"
|
||||
@show-dialog-upload-img="dialogUploadImgVisible = true"
|
||||
@startCopy="(isCoping = true), (backLight = true)"
|
||||
@endCopy="endCopy"
|
||||
|
Loading…
Reference in New Issue
Block a user