修改主图从内容区获取

去掉图集
This commit is contained in:
jacky 2024-05-17 21:24:30 +08:00
parent 8984955415
commit 300da484f8
2 changed files with 92 additions and 42 deletions

View File

@ -217,6 +217,10 @@ const isEmpty = () => {
return editorRef.value.isEmpty() return editorRef.value.isEmpty()
} }
defineExpose({ isEmpty }) const getHtml = () => {
return editorRef.value.getHtml()
}
defineExpose({ isEmpty, getHtml })
</script> </script>

View File

@ -96,30 +96,24 @@
</el-select> </el-select>
</div> </div>
</el-form-item> </el-form-item>
<el-form-item label="图片(首张图可用来做栏目列表用)" prop="imgs"> <el-form-item label="" prop="imgs">
<ChooseImg ref="chooseImg" category="article_imgs" size="820px" @on-select="handleSelectImg" />
<template #label> <template #label>
<div style="margin-bottom: 10px;">图片首张图可用来做栏目列表用</div> <div style="margin-bottom: 10px;">首图如果为空默认文章内容第一张图</div>
<div class="flex gap-3"> <div class="flex gap-3">
<el-button type="success" plain icon="folder" @click="handleChooseImg">媒体库</el-button> <el-button type="success" plain icon="folder" @click="handleChooseImg">从内容选择</el-button>
<upload-common class="upload-btn-media-library" category="article_imgs" <upload-common class="upload-btn-media-library" category="article_imgs"
@on-success="handleImgUpload" /> @on-success="handleImgUpload" />
</div> </div>
</template> </template>
<div class="gva-media-card"> <div class="gva-media-card" v-if="editForm.mainImg">
<div v-for="(item, key) in imgFileList" :key="key" class="media-box"> <el-image :src="editForm.mainImg" fit="contain" hide-on-click-modal
<el-image :key="key" :src="getUrl(item.url)" fit="cover" :preview-src-list="[getUrl(item.url)]" style="width: 100%; max-height: 500px; max-width: 500px;">
hide-on-click-modal style="width: 138px; height: 138px;"> <template #error>
<template #error> <el-icon>
<el-icon> <picture />
<picture /> </el-icon>
</el-icon> </template>
</template> </el-image>
</el-image>
<div class="img-title">
<el-button size="small" type="default" icon="delete" @click="handleImgRemove(item)">删除</el-button>
</div>
</div>
</div> </div>
</el-form-item> </el-form-item>
</el-col> </el-col>
@ -128,6 +122,24 @@
</div> </div>
</div> </div>
</el-drawer> </el-drawer>
<el-dialog v-model="imgDialogShow" append-to-body title="选择主图" width="50%" style="min-width: 960px">
<div v-if="contentImgList.length > 0" class="gva-media-card">
<div v-for="item in contentImgList" class="main-img-box">
<el-image :src="item" fit="contain" hide-on-click-modal style="width: 300px; height: 200px;"
@click="handleSelectImg(item)">
<template #error>
<el-icon>
<picture />
</el-icon>
</template>
</el-image>
</div>
</div>
<div v-else>
<el-empty description="无数据" />
</div>
</el-dialog>
</template> </template>
<script setup> <script setup>
@ -270,7 +282,7 @@ const getSourceData = async () => {
const emptyForm = () => { const emptyForm = () => {
channelIdList.value = [] channelIdList.value = []
categoryIdList.value = [] categoryIdList.value = []
imgFileList.value = [] // imgFileList.value = []
tagList.value = [] tagList.value = []
editForm.value = { editForm.value = {
ID: 0, ID: 0,
@ -329,9 +341,9 @@ const initFormByArticle = async (id) => {
const { categories, channels, imgs, tags } = res.data.article const { categories, channels, imgs, tags } = res.data.article
categoryIdList.value = categories && categories.map(item => String(item.ID)) categoryIdList.value = categories && categories.map(item => String(item.ID))
channelIdList.value = channels && channels.map(item => String(item.ID)) channelIdList.value = channels && channels.map(item => String(item.ID))
if (imgs && (imgs !== '')) { // if (imgs && (imgs !== '')) {
imgFileList.value = JSON.parse(imgs) // imgFileList.value = JSON.parse(imgs)
} // }
if (tags !== '') { if (tags !== '') {
tagList.value = tags.split(',') tagList.value = tags.split(',')
} }
@ -370,29 +382,57 @@ const initFormByFetcher = async (id) => {
} }
// ---- ---- // ---- ----
const chooseImg = ref(null) const imgDialogShow = ref(false)
const imgFileList = ref([]) const contentImgList = ref([])
const handleChooseImg = () => { const handleChooseImg = () => {
chooseImg.value.open() contentImgList.value = []
return false const htmlData = contentEditorRef.value.getHtml()
console.log(htmlData)
const imgSrcs = htmlData.match(/<img [^>]*src=['"]([^'"]+)[^>]*>/g);
if (imgSrcs && Array.isArray(imgSrcs) && imgSrcs.length) {
imgSrcs.forEach(item => {
// console.log(item)
let src = item.match(/src=['"]([^'"]+)['"]/)
if (!src) {
return
}
// console.log(src)
contentImgList.value.push(src[1])
})
}
console.log(contentImgList.value)
imgDialogShow.value = true
} }
const handleSelectImg = (file) => { const handleSelectImg = (item) => {
imgFileList.value.push({ editForm.value.mainImg = item
key: file.key, imgDialogShow.value = false
name: file.name,
url: file.url,
})
}
const handleImgRemove = (file) => {
imgFileList.value = imgFileList.value.filter(item => item.key !== file.key)
} }
const handleImgUpload = (file) => { const handleImgUpload = (file) => {
imgFileList.value.push({ editForm.value.mainImg = file.url
key: file.key,
name: file.name,
url: file.url,
})
} }
// const chooseImg = ref(null)
// const imgFileList = ref([])
// const handleChooseImg = () => {
// chooseImg.value.open()
// return false
// }
// const handleSelectImg = (file) => {
// imgFileList.value.push({
// key: file.key,
// name: file.name,
// url: file.url,
// })
// }
// const handleImgRemove = (file) => {
// imgFileList.value = imgFileList.value.filter(item => item.key !== file.key)
// }
// const handleImgUpload = (file) => {
// imgFileList.value.push({
// key: file.key,
// name: file.name,
// url: file.url,
// })
// }
// ---- tag ---- // ---- tag ----
const tagInputFlag = ref(false) const tagInputFlag = ref(false)
@ -446,7 +486,7 @@ const handleFormSubmit = async valid => {
const rel = { const rel = {
categoryIds: categoryIdList.value.map(item => parseInt(item, 10)), categoryIds: categoryIdList.value.map(item => parseInt(item, 10)),
channelIds: channelIdList.value.map(item => parseInt(item, 10)), channelIds: channelIdList.value.map(item => parseInt(item, 10)),
imgList: imgFileList.value, // imgList: imgFileList.value,
tagList: tagList.value, tagList: tagList.value,
fetcherArticleId: fetcherArticleId.value fetcherArticleId: fetcherArticleId.value
} }
@ -476,3 +516,9 @@ const handleFormClose = () => {
// //
defineExpose({ openPage }) defineExpose({ openPage })
</script> </script>
<style scoped>
.main-img-box {
cursor: default;
}
</style>