Commit 2fea9bba by 胡悦

提交

1 parent 8c630a4f
This diff could not be displayed because it is too large.
...@@ -13,12 +13,20 @@ import { Editor, Toolbar } from '@wangeditor/editor-for-vue'; ...@@ -13,12 +13,20 @@ import { Editor, Toolbar } from '@wangeditor/editor-for-vue';
let route = useRoute() let route = useRoute()
let { query } = toRefs(route) let { query } = toRefs(route)
const router = useRouter(); const router = useRouter();
let form = ref<Partial<DataInfo>>({ let form = ref<Partial<DataInfo & {
titleEn?: string,
newsAbstractEn?: string,
contentEn?: string
}>>({
publishType: '1', publishType: '1',
oaImg: [], oaImg: [],
pcImg: [], pcImg: [],
title: '', title: '',
newsAbstract: '' titleEn: '',
newsAbstract: '',
newsAbstractEn: '',
content: '',
contentEn: ''
}); });
const data = ref({ const data = ref({
oaImgLoading: false, oaImgLoading: false,
...@@ -45,6 +53,7 @@ const findById = async () => { ...@@ -45,6 +53,7 @@ const findById = async () => {
form.value.publishTime = '' form.value.publishTime = ''
} }
valueHtml.value = res.content valueHtml.value = res.content
valueHtmlEn.value = res.contentEn || ''
console.log(form.value) console.log(form.value)
} }
/** 提交表单 */ /** 提交表单 */
...@@ -136,14 +145,16 @@ const back = () => { ...@@ -136,14 +145,16 @@ const back = () => {
/** 富文本 */ /** 富文本 */
// 编辑器实例,必须用 shallowRef,重要! // 编辑器实例,必须用 shallowRef,重要!
const editorRef = shallowRef(); const editorRef = shallowRef();
const editorRefEn = shallowRef();
const toolbarConfig: Partial<IToolbarConfig> = { const toolbarConfig: Partial<IToolbarConfig> = {
excludeKeys: ["insertImage", "group-video", "blockquote", "code", "insertTable", "codeBlock", "todo"] excludeKeys: ["insertImage", "group-video", "blockquote", "code", "insertTable", "codeBlock", "todo"]
}; };
const mode = 'default' const mode = 'default'
const valueHtml = ref('') const valueHtml = ref('')
const valueHtmlEn = ref('')
type InsertFnType = (url: string, alt: string, href: string) => void; type InsertFnType = (url: string, alt: string, href: string) => void;
const editorConfig: Partial<IEditorConfig> = { const editorConfig: Partial<IEditorConfig> = {
placeholder: '请输入新闻内容', placeholder: '请输入新闻内容(中文)',
MENU_CONF: { MENU_CONF: {
uploadImage: { uploadImage: {
pasteIgnoreImg: true, pasteIgnoreImg: true,
...@@ -155,11 +166,31 @@ const editorConfig: Partial<IEditorConfig> = { ...@@ -155,11 +166,31 @@ const editorConfig: Partial<IEditorConfig> = {
}, },
} }
}; };
const editorConfigEn: Partial<IEditorConfig> = {
placeholder: '请输入新闻内容(英文)',
MENU_CONF: {
uploadImage: {
pasteIgnoreImg: true,
// 自定义上传
async customUpload (file: File, insertFn: InsertFnType) {
const res = await translateBase64(file, 'image') as string
insertFn(res, 'alt', '')
}
},
}
};
// 编辑器回调函数 // 编辑器回调函数
const handleCreated = (editor: IDomEditor) => { const handleCreated = (editor: IDomEditor) => {
editorRef.value = editor; // 记录 editor 实例,重要! editorRef.value = editor; // 记录 editor 实例,重要!
// 获取工具栏的key setTimeout(() => {
const toolbar = DomEditor.getToolbar(editor);
const toolbarKeys = toolbar?.getConfig().toolbarKeys
console.log('toolbarKeys', toolbarKeys)
})
};
const handleCreatedEn = (editor: IDomEditor) => {
editorRefEn.value = editor;
setTimeout(() => { setTimeout(() => {
const toolbar = DomEditor.getToolbar(editor); const toolbar = DomEditor.getToolbar(editor);
const toolbarKeys = toolbar?.getConfig().toolbarKeys const toolbarKeys = toolbar?.getConfig().toolbarKeys
...@@ -173,45 +204,25 @@ const handleChange = (editor: IDomEditor) => { ...@@ -173,45 +204,25 @@ const handleChange = (editor: IDomEditor) => {
form.value.content = editor.getHtml() form.value.content = editor.getHtml()
} }
}; };
const handleChangeEn = (editor: IDomEditor) => {
if (valueHtmlEn.value == '<p><br></p>') {
form.value.contentEn = ''
} else {
form.value.contentEn = editor.getHtml()
}
};
const customPaste = (editor, event, callback) => { const customPaste = (editor, event, callback) => {
// 禁用粘贴图片
// 获取粘贴的html部分(??没错粘贴word时候,一部分内容就是html),该部分包含了图片img标签
// let html = event.clipboardData.getData('text/html');
// // 获取rtf数据(从word、wps复制粘贴时有),复制粘贴过程中图片的数据就保存在rtf中
// const rtf = event.clipboardData.getData('text/rtf');
// if (html && rtf) { // 该条件分支即表示要自定义word粘贴
// // 列表缩进会超出边框,直接过滤掉
// html = html.replace(/text\-indent:\-(.*?)pt/gi, '')
// // 从html内容中查找粘贴内容中是否有图片元素,并返回img标签的属性src值的集合
// const imgSrcs = findAllImgSrcsFromHtml(html);
// // 如果有
// if (imgSrcs && Array.isArray(imgSrcs) && imgSrcs.length) {
// // 从rtf内容中查找图片数据
// const rtfImageData = extractImageDataFromRtf(rtf);
// // 如果找到
// if (rtfImageData.length) {
// // TODO:此处可以将图片上传到自己的服务器上
// // 执行替换:将html内容中的img标签的src替换成ref中的图片数据,如果上面上传了则为图片路径
// html = replaceImagesFileSourceWithInlineRepresentation(html, imgSrcs, rtfImageData)
// editor.dangerouslyInsertHtml(html);
// }
// }
// // 阻止默认的粘贴行为
// event.preventDefault();
// callback(false);
// } else {
callback(true); callback(true);
// } };
const customPasteEn = (editor, event, callback) => {
callback(true);
}; };
// 组件销毁时,也及时销毁编辑器,重要! // 组件销毁时,也及时销毁编辑器,重要!
onBeforeUnmount(() => { onBeforeUnmount(() => {
const editor = editorRef.value; const editor = editorRef.value;
if (editor == null) return; if (editor != null) editor.destroy();
editor.destroy(); const editorEn = editorRefEn.value;
if (editorEn != null) editorEn.destroy();
}); });
</script> </script>
<template> <template>
...@@ -225,19 +236,35 @@ onBeforeUnmount(() => { ...@@ -225,19 +236,35 @@ onBeforeUnmount(() => {
> >
<a-form-item <a-form-item
field="title" field="title"
label="新闻标题" label="新闻标题(中文)"
validate-trigger="change"
:rules="[{ required: true, message: '新闻标题(中文)必填' }]"
>
<a-input :style="{width}" v-model="form.title" placeholder="请输入新闻标题(中文)" :max-length="40" allow-clear show-word-limit />
</a-form-item>
<a-form-item
field="titleEn"
label="新闻标题(英文)"
validate-trigger="change" validate-trigger="change"
:rules="[{ required: true, message: '新闻标题必填' }]" :rules="[{ required: true, message: '新闻标题(英文)必填' }]"
> >
<a-input :style="{width}" v-model="form.title" placeholder="请输入新闻标题" :max-length="40" allow-clear show-word-limit /> <a-input :style="{width}" v-model="form.titleEn" placeholder="请输入新闻标题(英文)" :max-length="100" allow-clear show-word-limit />
</a-form-item> </a-form-item>
<a-form-item <a-form-item
field="newsAbstract" field="newsAbstract"
label="新闻摘要" label="新闻摘要(中文)"
validate-trigger="change"
:rules="[{ required: true, message: '新闻摘要(中文)必填' }]"
>
<a-textarea :style="{width}" v-model="form.newsAbstract" placeholder="请输入新闻摘要(中文)" :max-length="100" allow-clear show-word-limit />
</a-form-item>
<a-form-item
field="newsAbstractEn"
label="新闻摘要(英文)"
validate-trigger="change" validate-trigger="change"
:rules="[{ required: true, message: '新闻摘要必填' }]" :rules="[{ required: true, message: '新闻摘要(英文)必填' }]"
> >
<a-textarea :style="{width}" v-model="form.newsAbstract" placeholder="请输入新闻摘要" :max-length="100" allow-clear show-word-limit /> <a-textarea :style="{width}" v-model="form.newsAbstractEn" placeholder="请输入新闻摘要(英文)" :max-length="200" allow-clear show-word-limit />
</a-form-item> </a-form-item>
<a-form-item <a-form-item
field="publishType" field="publishType"
...@@ -309,8 +336,8 @@ onBeforeUnmount(() => { ...@@ -309,8 +336,8 @@ onBeforeUnmount(() => {
</div> </div>
</div> </div>
</a-form-item> </a-form-item>
<a-form-item field="content" label="新闻内容" validate-trigger="change" <a-form-item field="content" label="新闻内容(中文)" validate-trigger="change"
:rules="[{ required: true, message: '新闻内容必填' }]"> :rules="[{ required: true, message: '新闻内容(中文)必填' }]">
<div class="editor-box"> <div class="editor-box">
<Toolbar <Toolbar
:editor="editorRef" :editor="editorRef"
...@@ -329,6 +356,26 @@ onBeforeUnmount(() => { ...@@ -329,6 +356,26 @@ onBeforeUnmount(() => {
/> />
</div> </div>
</a-form-item> </a-form-item>
<a-form-item field="contentEn" label="新闻内容(英文)" validate-trigger="change"
:rules="[{ required: true, message: '新闻内容(英文)必填' }]">
<div class="editor-box">
<Toolbar
:editor="editorRefEn"
:defaultConfig="toolbarConfig"
:mode="mode"
style="border-bottom: 1px solid #DCDEE0"
/>
<Editor
:defaultConfig="editorConfigEn"
:mode="mode"
v-model="valueHtmlEn"
style="height: 400px; overflow-y: hidden;"
@onCreated="handleCreatedEn"
@onChange="handleChangeEn"
@customPaste="customPasteEn"
/>
</div>
</a-form-item>
<a-form-item> <a-form-item>
<a-button type="primary" html-type="submit">提交</a-button> <a-button type="primary" html-type="submit">提交</a-button>
<a-button class="back" @click="back">取消</a-button> <a-button class="back" @click="back">取消</a-button>
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!