老产品前端代码
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.

214 lines
7.8 KiB

<template>
3 years ago
<editor :id="id" v-model="content" tag-name="div" :init="init" />
3 years ago
<!-- <textarea id="editors" v-model="content" /> -->
</template>
<script>
import tinymce from "tinymce/tinymce";
import Editor from "@tinymce/tinymce-vue";
import "tinymce/themes/silver/theme"; // 引用主题文件
import "tinymce/icons/default"; // 引用图标文件
import "tinymce/plugins/link";
import "tinymce/plugins/code";
import "tinymce/plugins/table";
import "tinymce/plugins/lists";
import "tinymce/plugins/advlist";
import "tinymce/plugins/anchor";
import "tinymce/plugins/autolink"; //锚点
import "tinymce/plugins/autoresize";
import "tinymce/plugins/autosave";
import "tinymce/plugins/charmap"; //特殊字符
import "tinymce/plugins/code"; //查看源码
import "tinymce/plugins/codesample"; //插入代码
import "tinymce/plugins/directionality"; //
import "tinymce/plugins/fullpage"; //页面属性编辑
import "tinymce/plugins/fullscreen"; //全屏
import "tinymce/plugins/help"; //帮助
import "tinymce/plugins/hr"; //横线
import "tinymce/plugins/image"; //图片
import "tinymce/plugins/imagetools"; //图片工具
import "tinymce/plugins/importcss"; //图片工具
import "tinymce/plugins/insertdatetime"; //时间插入
import "tinymce/plugins/media"; //媒体插入
import "tinymce/plugins/nonbreaking"; //
import "tinymce/plugins/noneditable"; //不间断空格
import "tinymce/plugins/pagebreak"; //分页
import "tinymce/plugins/paste"; //粘贴
import "tinymce/plugins/preview"; //预览
import "tinymce/plugins/print"; //打印
import "tinymce/plugins/quickbars"; //快捷菜单
import "tinymce/plugins/save"; //保存
import "tinymce/plugins/searchreplace"; //查询替换
import "tinymce/plugins/spellchecker"; //拼写检查
import "tinymce/plugins/tabfocus"; //
import "tinymce/plugins/template"; //插入模板
import "tinymce/plugins/textpattern"; //
import "tinymce/plugins/toc"; //
import "tinymce/plugins/visualblocks"; //
import "tinymce/plugins/visualchars"; //
import "tinymce/plugins/wordcount"; //数字统计
import { debounce } from "throttle-debounce";
3 years ago
let num = 1;
export default {
props: {
3 years ago
id: {
type: String,
default: () => {
num === 10000 && (num = 1);
return `tinymce${+new Date()}${num++}`;
},
},
value: {
default: "",
},
height: {
default: 400,
},
},
components: {
editor: Editor,
},
data() {
let uploadUrl =
window.SITE_CONFIG["apiURL"] + "/oss/file/upload-resi-event-file";
let token = this.getUserToken();
let init = {
3 years ago
selector: `#${this.id}`,
language_url: require("./zh_CN.js"), // 中文语言包路径
language: "zh_CN",
skin_url: require("tinymce/skins/ui/oxide/skin.css"), // 编辑器皮肤样式
menubar: false, // 隐藏菜单栏
autoresize_bottom_margin: 50,
max_height: this.height,
min_height: this.height,
// height: this.height || 500,
// height: 320,
toolbar_mode: "none",
toolbar_drawer: "sliding",
toolbar_mode: "sliding",
3 years ago
outputFormat: 'p',
plugins:
"wordcount visualchars visualblocks toc textpattern template tabfocus spellchecker searchreplace save quickbars print preview paste pagebreak noneditable nonbreaking media insertdatetime importcss imagetools image hr help fullscreen fullpage directionality codesample code charmap link code table lists advlist anchor autolink autoresize autosave", // 插件需要import进来
toolbar:
"formats undo redo paste print fontsizeselect fontselect template fullpage|wordcount ltr rtl visualchars visualblocks toc spellchecker searchreplace|save preview pagebreak nonbreaking|media image|outdent indent aligncenter alignleft alignright alignjustify lineheight underline quicklink h2 h3 blockquote numlist bullist table removeformat forecolor backcolor bold italic strikethrough hr charmap link insertdatetime|subscript superscript cut codesample code |anchor preview fullscreen|help",
content_style: "p {margin: 5px 0; font-size: 14px}",
fontsize_formats: "12px 14px 16px 18px 24px 36px 48px 56px 72px",
font_formats:
"微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方=PingFang SC,Microsoft YaHei,sans-serif;宋体=simsun,serif;仿宋体=FangSong,serif;黑体=SimHei,sans-serif;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;",
branding: false,
elementpath: false,
resize: false, // 禁止改变大小
statusbar: false, // 隐藏底部状态栏
object_resizing: false,
end_container_on_empty_block: true,
powerpaste_word_import: "clean",
// code_dialog_height: 450,
// code_dialog_width: 1000,
// autoresize_max_height: 450, // 编辑区域的最大高度
// autoresize_min_height: 350, //编辑区域的最小高度
advlist_bullet_styles: "square",
advlist_number_styles: "default",
default_link_target: "_blank",
link_title: false,
nonbreaking_force_tab: true,
// 图片上传
images_upload_handler: function (blobInfo, succFun, failFun) {
var xhr, formData;
var file = blobInfo.blob(); // 转化为易于理解的file对象
xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.open("POST", uploadUrl);
xhr.setRequestHeader("token", token);
xhr.onload = function () {
var json;
if (xhr.status != 200) {
failFun("HTTP Error: " + xhr.status);
return;
}
json = JSON.parse(xhr.responseText);
if (!json || typeof json.data.url != "string") {
failFun("Invalid JSON: " + xhr.responseText);
return;
}
succFun(json.data.url);
};
formData = new FormData();
formData.append("file", file, file.name); // 此处与源文档不一样
formData.append("customerId", localStorage.getItem("customerId"));
xhr.send(formData);
},
};
// init = Object.assign(init, this.$attrs);
init.init_instance_callback = (editor) => {
if (this.value) editor.setContent(this.value);
this.vModel(editor);
};
// tinymce.init; // 初始化
const revert_data = (content) => {
this.$emit("input", content);
};
return {
content: this.value,
init,
revert_data,
};
},
3 years ago
mounted() {
3 years ago
// tinymce.init(this.init)
3 years ago
},
methods: {
vModel(editor) {
// 控制连续写入时setContent的触发频率
const debounceSetContent = debounce(250, editor.setContent);
this.$watch("value", (val, prevVal) => {
if (editor && val !== prevVal && val !== editor.getContent()) {
if (typeof val !== "string") val = val.toString();
debounceSetContent.call(editor, val);
3 years ago
return
}
3 years ago
// debounceSetContent.call(editor, this.dormatHtml(val));
});
editor.on("change keyup undo redo", () => {
3 years ago
// console.log('editor.getContent()---', editor.getContent({ format : 'p' }))
const c = editor.getContent({ format : 'p' })
this.$emit("input", editor.getContent());
3 years ago
// debounce(500, this.$emit("input", editor.getContent({ format : 'p' })));
;
});
editor.on("blur", () => {
3 years ago
// console.log('editor.blur--', editor.getContent({ format : 'p' }))
// editor.getContent(editor.getContent({ format : 'p' }))
this.$emit("blur");
});
},
3 years ago
dormatHtml(content) {
let c = ''
if (content.indexOf('DOCTYPE') != -1) {
c = content.slice(45, -16);
}
console.log('content', typeof content)
return c || content
},
getUserToken() {
return localStorage.getItem("token");
},
destroyTinymce() {
tinymce.destroy();
},
},
watch: {
content() {
this.revert_data(this.content);
},
},
};
</script>