@aidiruanjian/doc-helper
Version:
UOS环境下使用的wps的web插件
166 lines (165 loc) • 4.74 kB
JavaScript
import { openBlock, createElementBlock, createCommentVNode } from "vue";
class WordController$1 {
constructor(application, callbacks = {}) {
this.application = application;
if (callbacks.onBeforeSave) {
this.application.registerEvent("DIID_ApplicationEvents4", "DocumentBeforeSave", "onBeforeSave");
window.onBeforeSave = () => {
callbacks.onBeforeSave();
};
}
}
createDocument() {
this.application.createDocument("wps");
this.application.setToolbarAllVisible(true);
}
openRemote(url, readOnly = false) {
try {
this.application.openDocumentRemote(url, readOnly);
} catch (error) {
alert("\u6587\u6863\u6253\u5F00\u5931\u8D25");
}
if (readOnly === true) {
this.application.setToolbarAllVisible(false);
}
}
saveRemote(url, filename) {
this.application.saveURL(url, filename);
}
enableProtect(flag) {
this.application.enableProtect(flag);
}
}
class PdfController$1 {
constructor(application) {
this.application = application;
}
openRemote(url) {
this.download(url).then((data) => {
this.application.setAttribute("src", data);
this.refresh();
}, (err) => {
});
}
refresh() {
let parentNode = this.application.parentNode;
parentNode.removeChild(this.application);
parentNode.appendChild(this.application);
}
download(url) {
return new Promise((resolve, reject) => {
try {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.setRequestHeader("Content-Type", "application/pdf");
xhr.responseType = "blob";
xhr.onload = function(e) {
if (this.status == 200) {
var blob = this.response;
var pdfBlob = new Blob([blob], { type: "application/pdf" });
var url2 = URL.createObjectURL(pdfBlob);
resolve(url2);
} else {
alert("PDF\u52A0\u8F7D\u5931\u8D25");
console.error(e);
}
};
xhr.onerror = (e) => {
alert("PDF\u52A0\u8F7D\u5931\u8D25");
console.error(e);
};
xhr.send();
} catch (error) {
console.error(error);
reject();
}
});
}
}
const WordController = WordController$1;
const PdfController = PdfController$1;
var _export_sfc = (sfc, props) => {
const target = sfc.__vccOpts || sfc;
for (const [key, val] of props) {
target[key] = val;
}
return target;
};
const _sfc_main = {
data() {
return {
type: null,
application: null,
controller: null
};
},
methods: {
switchType(type, callback) {
this.type = type;
this.$nextTick(() => {
let plugin = this.$refs.plugin;
if (type == "word") {
let application = plugin.Application;
this.controller = new WordController(application, {
onBeforeSave: () => {
this.$emit("beforeSave");
}
});
} else if (type == "excel")
;
else if (type == "pdf") {
this.controller = new PdfController(plugin);
}
callback && callback();
});
},
createDocument(type) {
this.switchType(type, () => {
this.controller.createDocument();
});
},
openRemote(type, url, readonly = true) {
this.switchType(type, () => {
this.controller.openRemote(url, readonly);
});
},
saveRemote(url, filename) {
if (this.controller.saveRemote) {
this.controller.saveRemote(url, filename);
}
}
}
};
const _hoisted_1 = {
class: "plugin-container",
style: { "height": "100%", "width": "100%" }
};
const _hoisted_2 = {
key: 0,
class: "wps-plugin",
style: { "height": "100%", "width": "100%" },
ref: "plugin",
type: "application/x-wps"
};
const _hoisted_3 = {
key: 1,
class: "wps-plugin",
style: { "height": "100%", "width": "100%" },
ref: "plugin2",
type: "application/x-et"
};
const _hoisted_4 = {
key: 2,
class: "wps-plugin",
style: { "height": "100%", "width": "100%" },
ref: "plugin3"
};
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
return openBlock(), createElementBlock("div", _hoisted_1, [
$data.type == "word" ? (openBlock(), createElementBlock("embed", _hoisted_2, null, 512)) : createCommentVNode("", true),
$data.type == "excel" ? (openBlock(), createElementBlock("embed", _hoisted_3, null, 512)) : createCommentVNode("", true),
$data.type == "pdf" ? (openBlock(), createElementBlock("embed", _hoisted_4, null, 512)) : createCommentVNode("", true)
]);
}
var DocHelper = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
export { DocHelper as default };