Files
plugins_vue/src/pages/page1/components/InsertImage.vue
T
2024-01-30 17:03:09 +08:00

667 lines
23 KiB
Vue

<template>
<div class="wrap">
<a-button-group>
<!-- <a-button @click="handleClick">插入图</a-button>-->
<!-- <a-button @click="handleCaptionClick">插入图题注</a-button>-->
<a-button @click="openImageName">图题</a-button>
<a-button @click="openImageDialog">插入图片</a-button>
<a-button @click="openImageNoteFou">图片脚注</a-button>
</a-button-group>
<!-- <a-button-group>-->
<!-- <a-button @click="handleTest">测试</a-button>-->
<!-- </a-button-group>-->
<a-modal v-model="visible" title="插入图片" @ok="handleOk">
<a-form-model
id="components-form-demo-validate-other"
:model="form"
v-bind="formItemLayout"
@submit="handleSubmit"
>
<!-- <a-form-item label="标题">-->
<!-- <a-input-->
<!-- v-model="form.title"-->
<!-- placeholder="请输入标题"-->
<!-- />-->
<!-- </a-form-item>-->
<a-form-item label="图片">
<a-upload
ref="upload"
name="file"
list-type="picture-card"
class="avatar-uploader"
:show-upload-list="false"
action="/laws-sinotruk/onlyoffice/upload"
:before-upload="beforeUpload"
:customRequest="uploadFile"
@change="handleChange"
v-decorator="[
'file',
{
rules: [
{
required: true,
message: '请上传图片',
},
],
},
]"
>
<img ref="myImg" v-if="imageUrl" :src="imageUrl" style="width: 300px; height: 300px;" />
<div v-else>
<a-icon :type="loading ? 'loading' : 'plus'" />
<div class="ant-upload-text">
Upload
</div>
</div>
</a-upload>
</a-form-item>
<a-form-item label="图片大小">
<a-radio-group :options="plainOptions" v-model="form.size" />
</a-form-item>
</a-form-model>
</a-modal>
</div>
</template>
<script>
import _axios from '@/plugins/axios2'
import pxConversionMm from '../common/unitConversion'
function getBase64 (img, callback) {
console.log(img)
const reader = new FileReader()
reader.addEventListener('load', () => callback(reader.result))
reader.readAsDataURL(img)
}
const plainOptions = ['原图', '小', '中', '大']
export default {
data () {
return {
visible: false,
formItemLayout: {
labelCol: { span: 6 },
wrapperCol: { span: 14 }
},
loading: false,
imageUrl: '',
imageIpUrl: '', // 用IP的图片,给onlyoffice的
plainOptions,
form: {
title: '',
size: '中'
},
numData: {
1: 'A',
2: 'B',
3: 'C',
4: 'D',
5: 'E',
6: 'F',
7: 'G',
8: 'H',
9: 'I',
10: 'J',
11: 'K',
12: 'L',
13: 'M',
14: 'N',
15: 'O',
16: 'P',
17: 'Q',
18: 'R',
19: 'S',
20: 'T',
21: 'U',
22: 'V',
23: 'W',
24: 'X',
25: 'Y',
26: 'Z'
}
}
},
methods: {
handleClick () {
// console.log(window.Asc.plugin.info)
window.Asc.scope.guid = window.Asc.plugin.info.guid
this.ctx.callCommand(() => {
// eslint-disable-next-line no-undef
// console.log(document.getElementById(`iframe_${Asc.scope.guid}`))
try {
// setTimeout(() => {
// Api.asc_addImage()
console.log(Api)
Api.AddImage()
// const cb = (type, id) => {
// eslint-disable-next-line max-len
// // this.sync_EndAction(c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.LoadImage);
// // c_oAscAsyncActionType.BlockInteraction: 1
// // c_oAscAsyncAction.LoadImage: 8
// if (type === 1 && id === 8) {
// // 图片加载完成
// console.log('图片加载完成')
// const props = new window.Asc.CAscCaptionProperties()
// props.put_Before(false)
// props.put_Label('图')
// props.put_ExcludeLabel(false)
// props.put_Format(window.Asc.c_oAscNumberingFormat.Decimal)
// props.put_IncludeChapterNumber(false) // 是否包括章节号,
// // props.put_HeadingLvl(this.cmbChapter.getValue()) // 章节的风格,不包括章节号时不用填
// // props.put_Separator(this.cmbSeparator.getValue()) // 分隔符,不包括章节号时不用填
// props.updateName()
// // eslint-disable-next-line no-undef
// DE.controllers.Viewport.api.asc_AddObjectCaption(props)
// Api.asc_unregisterCallback('asc_onEndAction', cb)
// }
// // console.log(rest)
// }
const props = new window.Asc.CAscCaptionProperties()
props.put_Before(false)
props.put_Label('图')
props.put_ExcludeLabel(false)
props.put_Format(window.Asc.c_oAscNumberingFormat.Decimal)
props.put_IncludeChapterNumber(false) // 是否包括章节号,
// Api.asc_registerCallback('asc_onEndAction', cb)
// }, 1)
} catch (error) {
console.error(error)
}
// eslint-disable-next-line no-undef
// window.postMessage()
// document.getElementById(`iframe_${Asc.scope.guid}`).
// contentWindow.postMessage({ m: 'Wx Success' }, '*')
}, false)
},
handleCaptionClick () {
// console.log(window.Asc.plugin.info)
window.Asc.scope.guid = window.Asc.plugin.info.guid
this.ctx.callCommand(() => {
// eslint-disable-next-line no-undef
// console.log(document.getElementById(`iframe_${Asc.scope.guid}`))
try {
// setTimeout(() => {
// Api.asc_addImage()
Api.AddImage()
const cb = (type, id) => {
// eslint-disable-next-line max-len
// this.sync_EndAction(c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.LoadImage);
// c_oAscAsyncActionType.BlockInteraction: 1
// c_oAscAsyncAction.LoadImage: 8
if (type === 1 && id === 8) {
// 图片加载完成
console.log('图片加载完成')
const props = new window.Asc.CAscCaptionProperties()
props.put_Before(false)
props.put_Label('图')
props.put_ExcludeLabel(false)
props.put_Format(window.Asc.c_oAscNumberingFormat.Decimal)
props.put_IncludeChapterNumber(false) // 是否包括章节号,
// props.put_HeadingLvl(this.cmbChapter.getValue()) // 章节的风格,不包括章节号时不用填
// props.put_Separator(this.cmbSeparator.getValue()) // 分隔符,不包括章节号时不用填
props.updateName()
// eslint-disable-next-line no-undef
DE.controllers.Viewport.api.asc_AddObjectCaption(props)
Api.asc_unregisterCallback('asc_onEndAction', cb)
}
// console.log(rest)
}
Api.asc_registerCallback('asc_onEndAction', cb)
// }, 1)
} catch (error) {
console.error(error)
}
// eslint-disable-next-line no-undef
// window.postMessage()
// document.getElementById(`iframe_${Asc.scope.guid}`).
// contentWindow.postMessage({ m: 'Wx Success' }, '*')
}, false)
},
handleCaptionClick2 () {
// _api.init(oWordControl);
// this.ctx.callCommand(function () {
// try {
// // eslint-disable-next-line new-cap
// // const _api = new window.Asc.asc_docs_api()
// console.log(this.DE.controllers.Viewport.api.asc_AddObjectCaption)
// } catch (error) {
// console.error(error)
// }
// })
this.ctx.callCommand(() => {
try {
const props = new window.Asc.CAscCaptionProperties()
props.put_Before(false)
props.put_Label('图')
props.put_ExcludeLabel(false)
props.put_Format(window.Asc.c_oAscNumberingFormat.Decimal)
props.put_IncludeChapterNumber(false) // 是否包括章节号,
// props.put_HeadingLvl(this.cmbChapter.getValue()) // 章节的风格,不包括章节号时不用填
// props.put_Separator(this.cmbSeparator.getValue()) // 分隔符,不包括章节号时不用填
props.updateName()
// console.log(props)
// console.log(window)
// const oDocument = Api.GetDocument()
// const oParagraph = oDocument.GetElement(0)
// const oImage = Api.CreateImage('https://www.onlyoffice.com/blog/wp-content/uploads/2021/05/Blog_docs_63.jpg', 60 * 36000, 35 * 36000)
// oParagraph.AddDrawing(oImage)
// console.log(oImage)
// eslint-disable-next-line no-undef
DE.controllers.Viewport.api.asc_AddObjectCaption(props)
// oDocument.Push(oImage)
// window.Asc.asc_docs_api.asc_AddObjectCaption(props)
// console.log(window.Asc.asc_docs_api.prototype.asc_AddObjectCaption(props))
} catch (error) {
console.error(error)
}
}, false)
},
openImageDialog () {
this.visible = true
},
openImageNoteFou () {
this.ctx.callCommand(() => {
try {
const oDocument = Api.GetDocument()
const oParagraph = Api.CreateParagraph()
oDocument.InsertContent([oParagraph])
let GetPrevious = oParagraph.GetPrevious()
let oNumLvl
let currentNumbering
const getNumbering = GetPrevious.GetNumbering()
const text = GetPrevious.GetSpacingAfter()
if (getNumbering !== null) {
if (text == 32) {
currentNumbering = getNumbering
}
}
if (currentNumbering) {
oNumLvl = currentNumbering.GetNumbering().GetLevel(0)
} else {
let oNumbering = oDocument.CreateNumbering('numbered')
oNumLvl = oNumbering.GetLevel(0)
}
const sFormatString = '%1'
oNumLvl.SetCustomType('lowerLetter', sFormatString, 'left')
oNumLvl.SetSuff('space')
const textPr = oNumLvl.GetTextPr()
textPr.SetFontSize(18)
textPr.SetFontFamily('黑体')
textPr.SetSmallCaps(true)
textPr.SetVertAlign('superscript')
const paraPr = oNumLvl.GetParaPr()
paraPr.SetSpacingBefore(0)
paraPr.SetJc('left')
paraPr.SetIndFirstLine(-150)
const oParagraph1 = Api.CreateParagraph()
oParagraph1.AddText(oParagraph.GetText())
oParagraph1.SetIndLeft(574)
oParagraph1.SetNumbering(oNumLvl)
oParagraph1.SetSpacingAfter(32)
oParagraph1.SetFontSize(18)
oDocument.InsertContent([oParagraph1])
oParagraph.Delete()
} catch (error) {
console.error(error)
}
}, false)
},
openImageName () {
Asc.scope.numData = this.numData
this.ctx.callCommand(() => {
try {
const oDocument = Api.GetDocument()
const oParagraph = Api.CreateParagraph()
oDocument.InsertContent([oParagraph])
let num = oParagraph.GetPosInParent()
let oNumLvl
let currentNumbering
let table
let SpacingBefore
for (let i = num - 1; i >= 0; i--) {
const element = oDocument.GetElement(i)
if ('table' === element.GetClassType()) {
if (element.GetTableDescription() == 'appendix') {
table = element
break
}
} else if ('paragraph' === element.GetClassType()) {
const getNumbering = element.GetNumbering()
const text = element.GetSpacingAfter()
if (getNumbering !== null) {
if (text == 34) {
currentNumbering = getNumbering
break
}
}
}
}
const allElement = oDocument.GetElementsCount()
if (table) {
let number = table.GetCell(0, 0).GetContent().GetElement(1)
SpacingBefore = number.GetSpacingBefore()
}
if (SpacingBefore) {
if (currentNumbering) {
oNumLvl = currentNumbering.GetNumbering().GetLevel(0)
} else {
for (let i = num + 1; i < allElement; i++) {
const element = oDocument.GetElement(i)
if ('table' === element.GetClassType()) {
if (element.GetTableDescription() == 'appendix') {
break
}
} else if ('paragraph' === element.GetClassType()) {
const getNumbering = element.GetNumbering()
const text = element.GetSpacingAfter()
if (getNumbering !== null) {
if (text == 34) {
currentNumbering = getNumbering
break
}
}
}
}
if (currentNumbering) {
oNumLvl = currentNumbering.GetNumbering().GetLevel(0)
} else {
let oNumbering = oDocument.CreateNumbering('numbered')
oNumLvl = oNumbering.GetLevel(0)
const sFormatString = '图' + Asc.scope.numData[SpacingBefore] + '.%1'
oNumLvl.SetCustomType('decimal', sFormatString, 'center')
}
}
} else {
if (currentNumbering) {
oNumLvl = currentNumbering.GetNumbering().GetLevel(0)
} else {
for (let i = num + 1; i < allElement; i++) {
const element = oDocument.GetElement(i)
if ('table' === element.GetClassType()) {
if (element.GetTableDescription() == 'appendix') {
break
}
} else if ('paragraph' === element.GetClassType()) {
const getNumbering = element.GetNumbering()
const text = element.GetSpacingAfter()
if (getNumbering !== null) {
if (text == 34) {
currentNumbering = getNumbering
break
}
}
}
}
if (currentNumbering) {
oNumLvl = currentNumbering.GetNumbering().GetLevel(0)
} else {
let oNumbering = oDocument.CreateNumbering('numbered')
oNumLvl = oNumbering.GetLevel(0)
const sFormatString = '图 %1'
oNumLvl.SetCustomType('decimal', sFormatString, 'center')
}
}
}
const oNewDocumentStyle = oDocument.GetStyle('List Paragraph')
const oTextPr3 = oNewDocumentStyle.GetTextPr()
oTextPr3.SetFontFamily('黑体')
const textPr = oNumLvl.GetTextPr()
textPr.SetFontFamily('黑体')
textPr.SetFontSize(21)
// const CNParagraph = Api.CreateParagraph();
oParagraph.SetJc('center')
// oParagraph.AddText("图片");
oParagraph.SetFontSize(21)
oParagraph.SetStyle(oNewDocumentStyle)
oParagraph.SetFontFamily('黑体')
oParagraph.SetSpacingAfter(34, false)
oParagraph.SetNumbering(oNumLvl)
oDocument.InsertContent([oParagraph])
// let GetNext = oParagraph.GetNext()
// GetNext.Delete()
// oParagraph.Delete()
} catch (error) {
console.error(error)
}
}, false)
},
handleOk () {
console.log('图片上传和了')
this.visible = false
window.Asc.scope.imageUrl = this.imageIpUrl
window.Asc.scope.title = this.form.title
window.Asc.scope.imgSize = this.form.size
const width = pxConversionMm(this.imgOrigin.width)
const height = pxConversionMm(this.imgOrigin.height)
window.Asc.scope.imgOrigin = {
width,
height
}
this.ctx.callCommand(() => {
try {
const oDocument = Api.GetDocument()
const imgOParagraph = Api.CreateParagraph()
let imgSize = {
width: 0,
height: 0
}
switch (Asc.scope.imgSize) {
case '原图':
imgSize.width = Asc.scope.imgOrigin.width
imgSize.height = Asc.scope.imgOrigin.height
break
case '小':
imgSize.width = 165 / 3
imgSize.height = 89.3 / 3
break
case '中':
imgSize.width = 165 / 2
imgSize.height = 89.3 / 2
break
case '大':
imgSize.width = 165
imgSize.height = 89.3
break
}
console.log('创建文档图片的地址', Asc.scope.imageUrl)
const oDrawing = Api.CreateImage(Asc.scope.imageUrl, imgSize.width * 36000, imgSize.height * 36000)
imgOParagraph.AddDrawing(oDrawing)
// const allElement = oDocument.GetElementsCount()
// let numbering = null
// let currentNumbering = null
// for (let index = 0; index < allElement; index++) {
// const element = oDocument.GetElement(index)
// if ("paragraph" === element.GetClassType()) {
// const text = element.GetSpacingBefore()
// const getNumbering = element.GetNumbering()
// if (getNumbering !== null) {
// if (text == 10) {
// currentNumbering = getNumbering;
// break;
// }
// }
// }
// }
// if (!numbering) {
// if (currentNumbering) {
// numbering = currentNumbering.GetNumbering().GetLevel(0)
// } else {
// let oNumbering = oDocument.CreateNumbering('numbered')
// numbering = oNumbering.GetLevel(0)
// const sFormatString = '图 %1';
// numbering.SetCustomType("decimal", sFormatString, "right");
// }
// }
// const imgText = Api.CreateParagraph();
// var textPr = numbering.GetTextPr();
// textPr.SetFontFamily("黑体");
// textPr.SetFontSize(21)
// imgText.SetNumbering(numbering);
// imgText.AddText(`${Asc.scope.title}`);
// imgText.SetFontSize(21)
// imgText.SetFontFamily("黑体");
// imgText.SetBold(false)
// imgText.SetContextualSpacing(true);
// imgText.SetJc("center");
// imgText.SetSpacingBefore(10)
// imgText.SetSmallCaps(true);
imgOParagraph.SetJc('center')
imgOParagraph.SetSmallCaps(true)
const breakLine = Api.CreateParagraph()
breakLine.SetJc('left')
oDocument.InsertContent([imgOParagraph])
// oDocument.InsertContent([imgText]);
// 创建表格 包裹文字和列项
// oDocument.InsertContent([breakLine]);
} catch (e) {
console.log(e)
}
})
setTimeout(() => {
this.form = {}
this.imageIpUrl = ''
this.imageUrl = ''
}, 200)
},
handleChange (info) {
if (info.file.status === 'uploading') {
this.loading = true
return
}
if (info.file.status === 'done') {
// Get this url from response in real world.
getBase64(info.file.originFileObj, imageUrl => {
console.log(imageUrl)
this.imageUrl = imageUrl
this.loading = false
})
}
},
beforeUpload (file) {
const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'
if (!isJpgOrPng) {
this.$message.error('只能上传图片')
return false
}
const isLt2M = file.size / 1024 / 1024 < 2
if (!isLt2M) {
this.$message.error('图片大小不能超过2MB')
return false
}
return isJpgOrPng && isLt2M
},
uploadFile (fileObj) {
let { file } = fileObj
const _data = new FormData()
var renameReportFile = new File([file], '新名称.jpg', { type: file.type })
_data.append('file', renameReportFile)
_axios({
url: this.uploadFilePath,
method: 'post',
data: _data
}).then(res => {
this.loading = false
this.imageUrl = this.staticFilePath + '?path=' + res.result.url
this.imageIpUrl = this.downloadFilePath + '?path=' + res.result.url
const img = new Image()
img.src = this.staticFilePath + '?path=' + res.result.url
console.log('给onlyoffice的', this.imageIpUrl)
console.log('页面显示的', img.src)
console.log('页面显示的true', this.imageUrl)
if (img.complete) {
this.imgOrigin = {
width: img.width,
height: img.height
}
} else {
img.onload = () => {
console.log(12)
this.imgOrigin = {
width: img.width,
height: img.height
}
console.log(this.imgOrigin)
}
}
}).catch(e => {
this.loading = false
})
},
handleSubmit () {
this.visible = false
},
handleTest () {
this.ctx.callCommand(() => {
try {
window.imgNumOParagraph = window.imgNumOParagraph || null
var oNumbering, oNumLvl
oDocument = Api.GetDocument()
// 未创建图片列项
if (window.imgNumOParagraph) {
oNumbering = window.imgNumOParagraph
} else {
// 图片
oNumbering = oDocument.CreateNumbering('numbered')
window.imgNumOParagraph = oNumbering
}
oNumLvl = oNumbering.GetLevel(oNumbering.GetLevel(0).GetLevelIndex())
// console.log('this', this)
var num = oNumLvl.GetNumbering()
// console.log(oDocument.GetNumbering())
// console.log(Api.GetDocument())
// console.log(Api.GetNumbering())
oNumLvl.SetRestart(false)
// title = '图' + oNumLvl.GetNumbering()
// oNumLvl.SetCustomType('lowerRoman', oNumLvl.GetNumbering(), 'left')
oParagraph = Api.CreateParagraph()
oParagraph.SetNumbering(oNumLvl)
oParagraph.AddText('测试段落')
oParagraph.SetContextualSpacing(true)
oDocument.Push(oParagraph)
} catch (e) {
console.log(e)
}
})
}
},
computed: {},
mounted () {
}
}
</script>
<style>
.avatar-uploader > .ant-upload {
width: 128px;
height: 128px;
}
.ant-upload-select-picture-card i {
font-size: 32px;
color: #999;
}
.ant-upload-select-picture-card .ant-upload-text {
margin-top: 8px;
color: #666;
}
.ant-upload-select-picture-card {
img {
max-width: 100%;
height: auto !important;
}
}
</style>