Files
onlyoffice-front/src/views/editor.vue
T
2024-01-23 10:21:22 +08:00

261 lines
13 KiB
Vue

<template>
<div id="placeholder" class="nav" style="height: 100vh">
<div id="iframeEditor"></div>
</div>
</template>
<script>
let docEditor
const { location } = window
export default {
name: 'editor',
data () {
return {
docEditor: {},
documentUrl: window._CONFIG.documentUrl,
callbackUrl: window._CONFIG.callbackUrl
}
},
mounted () {
this.connectEditor()
// 从主系统跳过来需要调这个方法,目前系统内跳不触发
if (window.addEventListener) {
window.addEventListener('load', this.connectEditor)
window.addEventListener('resize', this.fixSize)
} else if (window.attachEvent) {
window.attachEvent('onload', this.connectEditor)
window.attachEvent('onresize', this.fixSize)
}
},
methods: {
innerAlert (message) {
if (console && console.log) console.log(message)
},
onAppReady () {
console.log(123)
this.innerAlert('Document editor ready')
},
onDocumentStateChange (event) {
const title = document.title.replace(/\*$/g, '')
document.title = title + (event.data ? '*' : '')
// console.log('尝试获取dom')
// console.log(document.getElementsByTagName('iframe'))
// console.log(document.getElementsByTagName('iframe')[0])
// console.log(document.getElementsByTagName('iframe')[0].contentDocument)
// console.log(document.getElementsByTagName('iframe')[0].contentWindow)
// console.log(document.getElementsByTagName('iframe')[0].contentWindow.document)
// console.log(document.getElementsByTagName('iframe')[0].contentWindow.document.getElementById('id_main_view'))
// console.log(document.getElementsByTagName('iframe')[0].contentDocument.getElementById('id_main_view'))
},
onRequestEditRights () {
location.href = location.href.replace(RegExp('mode=view', 'i'), '')
},
onRequestHistory (event) {
const historyObj =
[
{
changes: null,
key: this.$route.query.attId,
version: 1,
created: '2022-10-17 14:17:09',
user: { id: this.$route.query.userId, name: this.$route.query.userName }
}
] || null
this.docEditor.refreshHistory({
currentVersion: '1',
history: historyObj
})
},
onRequestHistoryData (data) {
console.log(data)
const version = data.data
const historyData =
[
{
'version': 1,
'key': this.$route.query.attId,
'url': this.documentUrl + this.$route.query.filePath + this.$route.query.fileName
}
] || null
this.docEditor.setHistoryData(historyData[version - 1])
},
onRequestHistoryClose (event) {
document.location.reload()
},
onError (event) {
console.log(event)
if (event) this.innerAlert(event.data)
},
onOutdatedVersion (event) {
// console.log(event);
location.reload(true)
},
replaceActionLink (href, linkParam) {
let link
const actionIndex = href.indexOf('&action=')
// eslint-disable-next-line eqeqeq
if (actionIndex != -1) {
const endIndex = href.indexOf('&', actionIndex + '&action='.length)
// eslint-disable-next-line eqeqeq
if (endIndex != -1) {
link = `${
href.substring(0, actionIndex) + href.substring(endIndex)
}&action=${encodeURIComponent(linkParam)}`
} else {
link = `${href.substring(0, actionIndex)}&action=${encodeURIComponent(
linkParam
)}`
}
} else {
link = `${href}&action=${encodeURIComponent(linkParam)}`
}
return link
},
onMakeActionLink (event) {
const actionData = event.data
const linkParam = JSON.stringify(actionData)
this.docEditor.setActionLink(this.replaceActionLink(location.href, linkParam))
},
fixSize () {
const wrapEl = document.getElementsByClassName('form')
if (wrapEl.length) {
// eslint-disable-next-line no-restricted-globals
wrapEl[0].style.height = `${screen.availHeight}px`
window.scrollTo(0, -1)
wrapEl[0].style.height = `${window.innerHeight}px`
}
},
onDocumentSaved () {
console.log(12)
},
connectEditor () {
console.log(this.$route.query)
console.log(`${this.documentUrl}${this.$route.query.filePath}`)
console.log(window._CONFIG.configUrl + 'page1/config.json')
console.log(`${this.callbackUrl}laws-sinotruk/onlyOffice/receiveFile?fileId=${this.$route.query.fileId}&fileNm=${this.$route.query.fileName}&key=${this.$route.query.key}`)
const nowTime = new Date().getTime()
this.docEditor = new DocsAPI.DocEditor('iframeEditor', {
width: '100%',
height: '100%',
type: 'desktop',
// 打开文档类型
// text对应各种文档类型(.doc, .docm, .docx, .dot, .dotm, .dotx, .epub, .fodt, .htm, .html, .mht, .odt, .ott, .pdf, .rtf, .txt, .djvu, .xps)
// spreadsheet对应表格类型(.csv, .fods, .ods, .ots, .xls, .xlsm, .xlsx, .xlt, .xltm, .xltx)
// presentation对应PPT类型(.fodp, .odp, .otp, .pot, .potm, .potx, .pps, .ppsm, .ppsx, .ppt, .pptm, .pptx)
documentType: 'word',
token: 'secret',
document: {
title: this.$route.query.fileName,
url: `${this.documentUrl}${this.$route.query.filePath}`,
fileType: this.$route.query.fileType,
key: this.$route.query.key + new Date().getTime(),
info: {
author: this.$route.query.userName,
created: 'Wed Oct 12 2022'
},
permissions: {
comment: true, // 定义是否可以注释文档。如果注释权限设置为true,则文档侧栏将包含“注释”菜单选项;只有将mode参数设置为edit时才生效,默认值与edit参数的值一致
download: true, // 定义是否可以下载文档或仅在线查看或编辑文档。如果下载权限设置为“false”下载为菜单选项将没有。默认值为true
edit: true, // 文件是否可以编辑,false时文件不可编辑
fillForms: true, // 定义是否能在文档中填充表单
modifyFilter: true, // 定义过滤器是否可以全局应用(true)影响所有其他用户,或局部应用(false),即仅适用于当前用户。如果将mode参数设置为edit,则过滤器修改仅对电子表格编辑器可用。默认值为true。
modifyContentControl: true, // 定义是否可以更改内容控件设置。仅当mode参数设置为edit时,内容控件修改才可用于文档编辑器。默认值为true。
review: true, // 第一是否显示审阅文档菜单
'commentGroups': {
'edit': ['Group2', ''],
'remove': [''],
'view': ''
},
'copy': true,
'deleteCommentAuthorOnly': false,
'editCommentAuthorOnly': false,
'print': true,
'reviewGroups': ['Group1', 'Group2', '']
}
},
editorConfig: {
actionLink: null,
mode: 'edit',
lang: 'zh',
query: {
id: 123456
},
callbackUrl: `${this.callbackUrl}laws-sinotruk/onlyOffice/receiveFile?fileId=${this.$route.query.fileId}&fileNm=${this.$route.query.fileName}&key=${this.$route.query.key}`,
user: {
id: this.$route.query.userId,
name: this.$route.query.userName
},
// embedded部分仅适用于嵌入式文档类型(请参阅config部分以了解如何定义嵌入式文档类型)。它允许更改设置,这些设置定义嵌入式模式下按钮的行为。
embedded: {
// saveUrl: this.documentUrl + this.$route.query.filePath + this.$route.query.fileName, // 定义允许将文档保存到用户个人计算机上的绝对URL。
// embedUrl: this.documentUrl + this.$route.query.filePath + this.$route.query.fileName, // 定义文档的绝对URL,以作为嵌入到网页中的文档的源文件
// shareUrl: this.documentUrl + this.$route.query.filePath + this.fileName, // 定义允许其他用户共享此文档的绝对URL。
saveUrl: `${this.documentUrl}${this.$route.query.filePath}`, // 定义允许将文档保存到用户个人计算机上的绝对URL。
embedUrl: `${this.documentUrl}${this.$route.query.filePath}`, // 定义文档的绝对URL,以作为嵌入到网页中的文档的源文件
shareUrl: `${this.documentUrl}${this.$route.query.filePath}`, // 定义允许其他用户共享此文档的绝对URL。
toolbarDocked: 'top' // 定义嵌入式查看器工具栏的位置,可以为top或bottom。
},
// 定制部分允许自定义编辑器界面,使其看起来像您的其他产品,并更改是否存在其他按钮,链接,更改徽标和编辑者所有者详细信息。
customization: {
about: true,
chat: true, // 定义“聊天”菜单按钮是显示还是隐藏;请注意,如果您隐藏“聊天”按钮,则相应的聊天功能也将被禁用。默认值为true。
comments: true, // 定义是显示还是隐藏“注释”菜单按钮;请注意,如果您隐藏“评论”按钮,则相应的评论功能将仅可用于查看,评论的添加和编辑将不可用。默认值为true。
feedback: true, // 反馈配置信息
forcesave: true, // 定义保存按钮是否显示默认false
atuosave: false,
'features': {
'spellcheck': {
'mode': false,
'change': true
}
},
// 定义“打开文件位置”菜单按钮和右上角按钮的设置
goback: {
// url: 'http://localhost/example/', // 单击“打开文件位置”菜单按钮时将打开的网站地址的绝对URL
},
unit: 'cm', // 定义在标尺和对话框中使用的度量单位。可以采用以下值:cm -厘米,pt-点,inch -英寸。默认值为厘米(cm)。
zoom: 100 // 定义以百分比为单位的文档显示缩放值。可以取大于0的值。对于文本文档和演示文稿,可以将此参数设置为-1(使文档适合页面选项)或-2(使文档页面宽度适合编辑器页面)。默认值为100。
},
fileChoiceUrl: '',
// 定义是否将启动插件并可用。默认值为true。
plugins: {
autostart: ['asc.{0616AE85-5DBE-4B6B-A0A9-455C4F1503AD}'],
pluginsData: [
window._CONFIG.configUrl + 'page1/config.json'
]
}
},
events: {
'onAppReady': this.onAppReady,
'onDocumentStateChange': this.onDocumentStateChange, // 文档改变后的回调
'onRequestEditRights': this.onRequestEditRights, // 用户尝试通过单击“编辑文档”按钮尝试将文档从视图切换到编辑模式时调用的函数。调用该函数时,必须在编辑模式下再次初始化编辑器。如果未声明该方法,则不会显示“编辑”按钮。
'onError': this.onError,
'onRequestHistory': this.onRequestHistory, // 用户尝试通过单击“版本历史记录”按钮显示文档版本历史记录时调用的函数。要显示文档版本历史,您必须调用refreshHistory方法。如果未声明该方法和onRequestHistoryData方法,则不会显示“版本历史记录”按钮。
'onRequestHistoryData': this.onRequestHistoryData, // 用户尝试单击文档版本历史记录中的特定文档版本时调用的函数。
'onRequestHistoryClose': this.onRequestHistoryClose, // 当用户尝试通过单击“关闭历史记录”按钮来查看文档版本历史记录时,试图调用该文档时调用的函数。调用该函数时,必须在编辑模式下再次初始化编辑器。如果未声明该方法,则不会显示“关闭历史记录”按钮。
'onOutdatedVersion': this.onOutdatedVersion, // 使用旧的document.key值打开文档进行编辑时,显示错误后调用的函数,该值用于编辑先前的文档版本并已成功保存。调用此事件时,必须使用新的document.key重新初始化编辑器。
'onMakeActionLink': this.onMakeActionLink,
'onDocumentSaved': this.onDocumentSaved
}
})
this.fixSize()
}
},
beforeDestroy () {
if (window.removeEventListener) {
window.removeEventListener('load', this.connectEditor)
window.removeEventListener('resize', this.fixSize)
} else if (window.detachEvent) {
window.detachEvent('onload', this.connectEditor)
window.detachEvent('onresize', this.fixSize)
}
}
}
</script>
<style scoped>
</style>