284 lines
7.8 KiB
Vue
284 lines
7.8 KiB
Vue
<!-- 用户编辑弹窗 -->
|
|
<template>
|
|
<ele-modal
|
|
width="80%"
|
|
:visible="visible"
|
|
:maskClosable="false"
|
|
:maxable="maxable"
|
|
:title="isUpdate ? '编辑' : '新增'"
|
|
:body-style="{ paddingBottom: '28px' }"
|
|
@update:visible="updateVisible"
|
|
@ok="save"
|
|
>
|
|
<a-form
|
|
ref="formRef"
|
|
:model="form"
|
|
:label-col="{ md: { span: 7 }, sm: { span: 4 }, xs: { span: 24 } }"
|
|
:wrapper-col="{ md: { span: 17 }, sm: { span: 20 }, xs: { span: 24 } }"
|
|
>
|
|
<!-- 编辑器 -->
|
|
<div class="content">
|
|
<tinymce-editor
|
|
v-model:value="content"
|
|
:disabled="disabled"
|
|
:init="config"
|
|
placeholder="图片直接粘贴自动上传"
|
|
@paste="onPaste"
|
|
/>
|
|
</div>
|
|
<!-- 编辑器 -->
|
|
<!-- <byte-md-editor-->
|
|
<!-- v-model:value="content"-->
|
|
<!-- placeholder="请输入您的内容,图片请直接粘贴"-->
|
|
<!-- :locale="zh_Hans"-->
|
|
<!-- mode="split"-->
|
|
<!-- :plugins="plugins"-->
|
|
<!-- height="500px"-->
|
|
<!-- :editorConfig="{ lineNumbers: true }"-->
|
|
<!-- @paste="onPaste"-->
|
|
<!-- />-->
|
|
</a-form>
|
|
</ele-modal>
|
|
</template>
|
|
|
|
<script lang="ts" setup>
|
|
import { ref, reactive, watch } from 'vue';
|
|
import { Form, message } from 'ant-design-vue';
|
|
import {assignObject} from "ele-admin-pro";
|
|
import { addApp, updateApp } from '@/api/oa/app';
|
|
import type { App } from '@/api/oa/app/model';
|
|
import { useThemeStore } from '@/store/modules/theme';
|
|
import { storeToRefs } from 'pinia';
|
|
// 中文语言文件
|
|
import zh_Hans from 'bytemd/locales/zh_Hans.json';
|
|
// 链接、删除线、复选框、表格等的插件
|
|
// 插件的中文语言文件
|
|
import zh_HansGfm from '@bytemd/plugin-gfm/locales/zh_Hans.json';
|
|
import 'bytemd/dist/index.min.css';
|
|
import 'github-markdown-css/github-markdown-light.css';
|
|
import highlight from '@bytemd/plugin-highlight';
|
|
// 中文语言文件
|
|
import gfm from '@bytemd/plugin-gfm';
|
|
// // 预览界面的样式,这里用的 github 的 markdown 主题
|
|
import 'github-markdown-css/github-markdown-light.css';
|
|
import {FormInstance} from 'ant-design-vue/es/form';
|
|
import { ItemType } from 'ele-admin-pro/es/ele-image-upload/types';
|
|
import {uploadFile, uploadOss} from "@/api/system/file";
|
|
import { TOKEN_STORE_NAME } from "@/config/setting";
|
|
import TinymceEditor from "@/components/TinymceEditor/index.vue";
|
|
|
|
|
|
// 是否是修改
|
|
const isUpdate = ref(false);
|
|
const useForm = Form.useForm;
|
|
// 是否开启响应式布局
|
|
const themeStore = useThemeStore();
|
|
const { styleResponsive } = storeToRefs(themeStore);
|
|
|
|
const props = defineProps<{
|
|
// 弹窗是否打开
|
|
visible: boolean;
|
|
// 修改回显的数据
|
|
data?: App | null;
|
|
}>();
|
|
|
|
const emit = defineEmits<{
|
|
(e: 'done'): void;
|
|
(e: 'update:visible', visible: boolean): void;
|
|
}>();
|
|
|
|
// 提交状态
|
|
const loading = ref(false);
|
|
// 是否显示最大化切换按钮
|
|
const maxable = ref(true);
|
|
const disabled = ref(false);
|
|
const images = ref<ItemType[]>([]);
|
|
const content = ref('');
|
|
|
|
// 插件
|
|
const plugins = ref([
|
|
gfm({
|
|
locale: zh_HansGfm
|
|
}),
|
|
highlight()
|
|
]);
|
|
|
|
const token = localStorage.getItem(TOKEN_STORE_NAME);
|
|
const formRef = ref<FormInstance | null>(null);
|
|
|
|
|
|
// 用户信息
|
|
const form = reactive<App>({
|
|
// 应用id
|
|
appId: undefined,
|
|
// 项目介绍
|
|
content: ''
|
|
});
|
|
|
|
/* 更新visible */
|
|
const updateVisible = (value: boolean) => {
|
|
emit('update:visible', value);
|
|
};
|
|
|
|
const { resetFields, validate, validateInfos } = useForm(form);
|
|
const editorRef = ref<InstanceType<typeof TinymceEditor> | null>(null);
|
|
const config = ref({
|
|
height: 500,
|
|
images_upload_handler: (blobInfo, success, error) => {
|
|
const file = blobInfo.blob();
|
|
const formData = new FormData();
|
|
formData.append('file', file, file.name);
|
|
uploadOss(file).then(res => {
|
|
success(res.url)
|
|
}).catch((msg) => {
|
|
error(msg);
|
|
})
|
|
return false;
|
|
},
|
|
// 自定义文件上传(这里使用把选择的文件转成 blob 演示)
|
|
file_picker_callback: (callback: any, _value: any, meta: any) => {
|
|
const input = document.createElement('input');
|
|
input.setAttribute('type', 'file');
|
|
// 设定文件可选类型
|
|
if (meta.filetype === 'image') {
|
|
input.setAttribute('accept', 'image/*');
|
|
} else if (meta.filetype === 'media') {
|
|
input.setAttribute('accept', 'video/*,.pdf');
|
|
}
|
|
input.onchange = () => {
|
|
const file = input.files?.[0];
|
|
if (!file) {
|
|
return;
|
|
}
|
|
if (meta.filetype === 'media') {
|
|
if (file.size / 1024 / 1024 > 200) {
|
|
editorRef.value?.alert({ content: '大小不能超过 200MB' });
|
|
return;
|
|
}
|
|
if (!file.type.startsWith('video/')) {
|
|
editorRef.value?.alert({ content: '只能选择视频文件' });
|
|
return;
|
|
}
|
|
uploadOss(file).then(res => {
|
|
callback(res.downloadUrl);
|
|
})
|
|
}
|
|
};
|
|
input.click();
|
|
}
|
|
});
|
|
|
|
/* 粘贴图片上传服务器并插入编辑器 */
|
|
const onPaste = (e) => {
|
|
const items = (e.clipboardData || e.originalEvent.clipboardData).items;
|
|
let hasFile = false;
|
|
for (let i = 0; i < items.length; i++) {
|
|
if (items[i].type.indexOf('image') !== -1) {
|
|
let file = items[i].getAsFile();
|
|
const item: ItemType = {
|
|
file,
|
|
uid: (file as any).lastModified,
|
|
name: file.name
|
|
};
|
|
uploadFile(<File>item.file)
|
|
.then((result) => {
|
|
const addPath = `<p><img class="content-img" src="${result.url}"></p>`;
|
|
content.value = content.value + addPath
|
|
// const addPath = '\n\r';
|
|
// content.value = content.value + addPath
|
|
})
|
|
.catch((e) => {
|
|
message.error(e.message);
|
|
});
|
|
hasFile = true;
|
|
}
|
|
}
|
|
if (hasFile) {
|
|
e.preventDefault();
|
|
}
|
|
}
|
|
|
|
/* 保存编辑 */
|
|
const save = () => {
|
|
if (!formRef.value) {
|
|
return;
|
|
}
|
|
formRef.value
|
|
.validate()
|
|
.then(() => {
|
|
loading.value = true;
|
|
const formData = {
|
|
...form,
|
|
content: content.value
|
|
};
|
|
const saveOrUpdate = isUpdate.value ? updateApp : addApp;
|
|
saveOrUpdate(formData)
|
|
.then((msg) => {
|
|
loading.value = false;
|
|
message.success(msg);
|
|
updateVisible(false);
|
|
emit('done');
|
|
})
|
|
.catch((e) => {
|
|
loading.value = false;
|
|
message.error(e.message);
|
|
});
|
|
})
|
|
.catch(() => {});
|
|
};
|
|
|
|
/* 上传事件 */
|
|
const uploadHandler = (file: File) => {
|
|
const item: ItemType = {
|
|
file,
|
|
uid: (file as any).uid,
|
|
name: file.name
|
|
};
|
|
if (!file.type.startsWith('image')) {
|
|
message.error('只能选择图片');
|
|
return;
|
|
}
|
|
if (file.size / 1024 / 1024 > 2) {
|
|
message.error('大小不能超过 2MB');
|
|
return;
|
|
}
|
|
onUpload(item);
|
|
};
|
|
|
|
// 上传文件
|
|
const onUpload = (item) => {
|
|
const { file } = item;
|
|
uploadFile(file)
|
|
.then((data) => {
|
|
images.value.push({
|
|
uid: data.id,
|
|
url: data.url,
|
|
status: 'done'
|
|
});
|
|
})
|
|
.catch((e) => {
|
|
message.error(e.message);
|
|
});
|
|
};
|
|
|
|
watch(
|
|
() => props.visible,
|
|
(visible) => {
|
|
if (visible) {
|
|
content.value = '';
|
|
if (props.data) {
|
|
assignObject(form, props.data);
|
|
if (props.data.content) {
|
|
content.value = props.data.content;
|
|
}
|
|
isUpdate.value = true;
|
|
} else {
|
|
isUpdate.value = false;
|
|
}
|
|
} else {
|
|
resetFields();
|
|
}
|
|
}
|
|
);
|
|
</script>
|