epmet pc工作端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.4 KiB

4 years ago
/* eslint-disable */
<template>
4 years ago
<textarea :id="tinymceId" style="visibility: hidden" />
4 years ago
</template>
<script>
4 years ago
import loadTinymce from "@/utils/loadTinymce";
import { plugins, toolbar } from "./config";
import { debounce } from "throttle-debounce";
import Cookie from "js-cookie";
4 years ago
// import constants from '@/utils/constants'
4 years ago
import nextTick from "dai-js/tools/nextTick";
4 years ago
4 years ago
let num = 1;
4 years ago
export default {
4 years ago
name: "Tinymce",
4 years ago
props: {
id: {
type: String,
default: () => {
4 years ago
num === 10000 && (num = 1);
return `tinymce${+new Date()}${num++}`;
},
4 years ago
},
value: {
4 years ago
default: "",
4 years ago
},
customerId: {
type: String,
4 years ago
default: "",
},
4 years ago
},
4 years ago
data() {
4 years ago
return {
4 years ago
tinymceId: this.id,
};
4 years ago
},
4 years ago
mounted() {
loadTinymce((tinymce) => {
let token = this.getUserToken();
4 years ago
// let uploadUrl = '1111'
4 years ago
let uploadUrl =
window.SITE_CONFIG["apiURL"] + "/oss/file/function/upload";
4 years ago
// :data="{customerId:customerId}"
// let uploadUrl = constants.userUploadUrl
// eslint-disable-next-line global-require
4 years ago
require("./zh_CN");
4 years ago
let conf = {
selector: `#${this.tinymceId}`,
4 years ago
language: "zh_CN",
menubar: "false",
skin_url:
"/" + process.env.VUE_APP_PUBLIC_PATH + "/tinymce/skins/ui/tduck",
content_style: "p {margin:3px 0; border:0px; padding:0px}",
content_css:
"/" +
process.env.VUE_APP_PUBLIC_PATH +
"/tinymce/skins/content/tduck",
cache_suffix: "?v=0.0.1",
4 years ago
plugins,
toolbar,
4 years ago
toolbar_drawer: "sliding",
toolbar_mode: "sliding",
4 years ago
height: 400,
// fontsize_formats: "8pt 10pt 12pt 14pt 18pt 24pt 36pt",
branding: false,
object_resizing: false,
end_container_on_empty_block: true,
4 years ago
powerpaste_word_import: "clean",
4 years ago
// code_dialog_height: 450,
// code_dialog_width: 1000,
// autoresize_max_height: 450, // 编辑区域的最大高度
// autoresize_min_height: 350, //编辑区域的最小高度
4 years ago
advlist_bullet_styles: "square",
advlist_number_styles: "default",
default_link_target: "_blank",
4 years ago
link_title: false,
statusbar: false,
nonbreaking_force_tab: true,
// images_upload_url: uploadUrl,
images_upload_handler: function (blobInfo, succFun, failFun) {
4 years ago
var xhr, formData;
var file = blobInfo.blob(); // 转化为易于理解的file对象
xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.open("POST", uploadUrl);
xhr.setRequestHeader("token", token);
4 years ago
xhr.onload = function () {
4 years ago
var json;
4 years ago
if (xhr.status != 200) {
4 years ago
failFun("HTTP Error: " + xhr.status);
return;
4 years ago
}
4 years ago
json = JSON.parse(xhr.responseText);
if (!json || typeof json.data.url != "string") {
failFun("Invalid JSON: " + xhr.responseText);
return;
4 years ago
}
4 years ago
succFun(json.data.url);
};
formData = new FormData();
4 years ago
4 years ago
formData.append("file", file, file.name); // 此处与源文档不一样
4 years ago
// formData.append('customerId', '111')
4 years ago
xhr.send(formData);
},
};
conf = Object.assign(conf, this.$attrs);
conf.init_instance_callback = (editor) => {
if (this.value) editor.setContent(this.value);
this.vModel(editor);
};
4 years ago
4 years ago
tinymce.init(conf);
});
4 years ago
},
4 years ago
destroyed() {
this.destroyTinymce();
4 years ago
},
methods: {
4 years ago
vModel(editor) {
4 years ago
// 控制连续写入时setContent的触发频率
4 years ago
const debounceSetContent = debounce(250, editor.setContent);
this.$watch("value", (val, prevVal) => {
4 years ago
if (editor && val !== prevVal && val !== editor.getContent()) {
4 years ago
if (typeof val !== "string") val = val.toString();
debounceSetContent.call(editor, val);
4 years ago
}
4 years ago
});
4 years ago
4 years ago
editor.on("change keyup undo redo", () => {
this.$emit("input", editor.getContent());
});
editor.on("blur", () => {
this.$emit("blur");
});
4 years ago
},
4 years ago
getUserToken() {
let token = localStorage.getItem("token");
return token;
4 years ago
},
4 years ago
destroyTinymce() {
if (!window.tinymce) return;
const tinymce = window.tinymce.get(this.tinymceId);
4 years ago
if (tinymce) {
4 years ago
tinymce.destroy();
4 years ago
}
4 years ago
},
},
};
4 years ago
</script>