|
|
@@ -29,13 +29,13 @@
|
|
|
|
|
|
<el-form-item label="内容简介">
|
|
|
<div class="editor-wrapper">
|
|
|
- <QuillEditor v-model:content="form.contentIntro" content-type="html" :toolbar="toolbar" class="quill-editor" />
|
|
|
+ <QuillEditor ref="quillIntroRef" @ready="onReadyIntro" v-model:content="form.contentIntro" content-type="html" :toolbar="toolbar" class="quill-editor" />
|
|
|
</div>
|
|
|
</el-form-item>
|
|
|
|
|
|
<el-form-item label="付费内容">
|
|
|
<div class="editor-wrapper">
|
|
|
- <QuillEditor v-model:content="form.contentPaid" content-type="html" :toolbar="toolbar" class="quill-editor" />
|
|
|
+ <QuillEditor ref="quillPaidRef" @ready="onReadyPaid" v-model:content="form.contentPaid" content-type="html" :toolbar="toolbar" class="quill-editor" />
|
|
|
</div>
|
|
|
</el-form-item>
|
|
|
|
|
|
@@ -57,6 +57,7 @@ import { ElMessage } from 'element-plus'
|
|
|
import { QuillEditor } from '@vueup/vue-quill'
|
|
|
import '@vueup/vue-quill/dist/vue-quill.snow.css'
|
|
|
import { getPostController } from '../api/post-controller'
|
|
|
+import { uploadImage } from '../util/upload'
|
|
|
|
|
|
const route = useRoute()
|
|
|
const router = useRouter()
|
|
|
@@ -66,6 +67,9 @@ const isEdit = computed(() => !!route.params.id)
|
|
|
const submitting = ref(false)
|
|
|
const formRef = ref()
|
|
|
|
|
|
+const quillIntroRef = ref()
|
|
|
+const quillPaidRef = ref()
|
|
|
+
|
|
|
const toolbar = [
|
|
|
['bold', 'italic', 'underline', 'strike'],
|
|
|
[{ list: 'ordered' }, { list: 'bullet' }],
|
|
|
@@ -92,6 +96,47 @@ function goBack() {
|
|
|
router.push('/admin/posts')
|
|
|
}
|
|
|
|
|
|
+function setupImageHandler(quill: any) {
|
|
|
+ const toolbar = quill.getModule('toolbar')
|
|
|
+ toolbar.addHandler('image', () => {
|
|
|
+ const input = document.createElement('input')
|
|
|
+ input.type = 'file'
|
|
|
+ input.accept = 'image/jpeg,image/png,image/gif,image/webp,image/bmp'
|
|
|
+ input.click()
|
|
|
+
|
|
|
+ input.onchange = async () => {
|
|
|
+ const file = input.files?.[0]
|
|
|
+ if (!file) return
|
|
|
+
|
|
|
+ if (file.size > 1024 * 1024) {
|
|
|
+ ElMessage.error('图片大小不能超过 1MB')
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ const loading = (ElMessage as any)({ message: '正在上传图片...', type: 'loading', duration: 0 })
|
|
|
+ try {
|
|
|
+ const url = await uploadImage(file, 'post_image')
|
|
|
+ const range = quill.getSelection(true)
|
|
|
+ quill.insertEmbed(range.index, 'image', url)
|
|
|
+ quill.setSelection(range.index + 1)
|
|
|
+ } catch {
|
|
|
+ ElMessage.error('图片上传失败')
|
|
|
+ } finally {
|
|
|
+ loading.close()
|
|
|
+ input.remove()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+function onReadyIntro(quill: any) {
|
|
|
+ setupImageHandler(quill)
|
|
|
+}
|
|
|
+
|
|
|
+function onReadyPaid(quill: any) {
|
|
|
+ setupImageHandler(quill)
|
|
|
+}
|
|
|
+
|
|
|
async function loadPost() {
|
|
|
if (!isEdit.value) return
|
|
|
try {
|