|
|
@@ -384,6 +384,13 @@ Vue.component('spaceEditor', {
|
|
|
// 标识,全部为 true 则代表全部加载完成
|
|
|
jsUrlFlag: [],
|
|
|
notLoadEditor: true, // 未加载完 editor 编辑器,true:未加载完,false:已加载完
|
|
|
+ imageQuality: 'standard',
|
|
|
+ uploading: false,
|
|
|
+ uploadStatus: '',
|
|
|
+ previewVisible: false,
|
|
|
+ previewMode: 'pc',
|
|
|
+ previewUrl: '',
|
|
|
+ previewLoading: false,
|
|
|
}
|
|
|
},
|
|
|
name: "spaceEditor",
|
|
|
@@ -400,7 +407,20 @@ Vue.component('spaceEditor', {
|
|
|
* type:编辑器工具栏类型
|
|
|
* isfullscreen:是否有全屏,
|
|
|
*/
|
|
|
- props: ['htmlContent', 'size', 'width', 'height', 'placeholder', 'type', 'isfullscreen'],
|
|
|
+ props: ['htmlContent', 'size', 'width', 'height', 'placeholder', 'type', 'isfullscreen', 'articlepreview', 'previewdata', 'previewurl'],
|
|
|
+ computed: {
|
|
|
+ previewFrameStyle: function () {
|
|
|
+ var widthMap = {
|
|
|
+ pc: '1200px',
|
|
|
+ pad: '768px',
|
|
|
+ mobile: '390px'
|
|
|
+ };
|
|
|
+ return {
|
|
|
+ width: widthMap[this.previewMode] || widthMap.pc,
|
|
|
+ height: '680px'
|
|
|
+ };
|
|
|
+ }
|
|
|
+ },
|
|
|
watch: {
|
|
|
height: function (newVal, oldVal) {
|
|
|
|
|
|
@@ -484,10 +504,26 @@ Vue.component('spaceEditor', {
|
|
|
document.getElementsByTagName('head')[0].appendChild(script);
|
|
|
|
|
|
},
|
|
|
+ isArticlePreview: function () {
|
|
|
+ return this.articlepreview === true || this.articlepreview === 'true';
|
|
|
+ },
|
|
|
+ showEditorTools: function () {
|
|
|
+ return this.type != 'flow' && this.isArticlePreview();
|
|
|
+ },
|
|
|
+ getUploadOptions: function () {
|
|
|
+ return {
|
|
|
+ imageQuality: this.imageQuality,
|
|
|
+ imageMaxWidth: 1920
|
|
|
+ };
|
|
|
+ },
|
|
|
myCustomUploadAdapterPlugin: function (editor) {
|
|
|
+ this.installUploadAdapter(editor);
|
|
|
+ },
|
|
|
+ installUploadAdapter: function (editor) {
|
|
|
+ var _this = this;
|
|
|
editor.plugins.get('FileRepository').createUploadAdapter = (loader) => {
|
|
|
// 第二个参数设置上传图片的地址
|
|
|
- return new MyUploadAdapter(loader);
|
|
|
+ return new MyUploadAdapter(loader, _this.getUploadOptions());
|
|
|
};
|
|
|
},
|
|
|
/**
|
|
|
@@ -597,6 +633,267 @@ Vue.component('spaceEditor', {
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
+ formatContent: function () {
|
|
|
+ if (!this.editor) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ var html = this.editor.getData();
|
|
|
+ if (space.utils.isNull(html)) {
|
|
|
+ space.message.warning("正文内容为空");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ var wrapper = document.createElement("div");
|
|
|
+ wrapper.innerHTML = html;
|
|
|
+
|
|
|
+ var all = wrapper.querySelectorAll("*");
|
|
|
+ for (var i = 0; i < all.length; i++) {
|
|
|
+ var node = all[i];
|
|
|
+ node.removeAttribute("lang");
|
|
|
+ node.removeAttribute("width");
|
|
|
+ node.removeAttribute("height");
|
|
|
+ node.removeAttribute("onload");
|
|
|
+ node.removeAttribute("border");
|
|
|
+ node.removeAttribute("vspace");
|
|
|
+ node.removeAttribute("hspace");
|
|
|
+
|
|
|
+ var style = node.getAttribute("style") || "";
|
|
|
+ if (/mso-|font-family|text-indent:\s*0|background:\s*transparent/i.test(style)) {
|
|
|
+ node.removeAttribute("style");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var inlineNodes = wrapper.querySelectorAll("span, font");
|
|
|
+ for (var s = inlineNodes.length - 1; s >= 0; s--) {
|
|
|
+ var inlineNode = inlineNodes[s];
|
|
|
+ if (inlineNode.attributes.length == 0 && inlineNode.parentNode) {
|
|
|
+ while (inlineNode.firstChild) {
|
|
|
+ inlineNode.parentNode.insertBefore(inlineNode.firstChild, inlineNode);
|
|
|
+ }
|
|
|
+ inlineNode.parentNode.removeChild(inlineNode);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var emptyBlocks = wrapper.querySelectorAll("p, div");
|
|
|
+ for (var e = emptyBlocks.length - 1; e >= 0; e--) {
|
|
|
+ var block = emptyBlocks[e];
|
|
|
+ var hasMedia = block.querySelector("img, video, iframe, table");
|
|
|
+ var text = (block.textContent || "").replace(/\u00a0/g, "").trim();
|
|
|
+ if (!hasMedia && text == "" && block.parentNode) {
|
|
|
+ block.parentNode.removeChild(block);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var images = wrapper.querySelectorAll("img");
|
|
|
+ for (var imgIndex = 0; imgIndex < images.length; imgIndex++) {
|
|
|
+ images[imgIndex].style.maxWidth = "100%";
|
|
|
+ images[imgIndex].style.height = "auto";
|
|
|
+ images[imgIndex].style.display = "block";
|
|
|
+ images[imgIndex].style.margin = "12px auto";
|
|
|
+ }
|
|
|
+
|
|
|
+ var videos = wrapper.querySelectorAll("video, iframe");
|
|
|
+ for (var v = 0; v < videos.length; v++) {
|
|
|
+ videos[v].style.maxWidth = "100%";
|
|
|
+ if (videos[v].tagName.toLowerCase() == "video") {
|
|
|
+ videos[v].setAttribute("controls", "controls");
|
|
|
+ videos[v].setAttribute("playsinline", "playsinline");
|
|
|
+ videos[v].style.display = "block";
|
|
|
+ videos[v].style.margin = "12px auto";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var tables = wrapper.querySelectorAll("table");
|
|
|
+ for (var t = 0; t < tables.length; t++) {
|
|
|
+ tables[t].style.width = "100%";
|
|
|
+ tables[t].style.borderCollapse = "collapse";
|
|
|
+ }
|
|
|
+
|
|
|
+ this.editor.setData(wrapper.innerHTML);
|
|
|
+ this.$emit("input", this.editor.getData());
|
|
|
+ space.message.success("正文已完成排版整理");
|
|
|
+ },
|
|
|
+ insertHtml: function (html) {
|
|
|
+ if (!this.editor || space.utils.isNull(html)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ var viewFragment = this.editor.data.processor.toView(html);
|
|
|
+ var modelFragment = this.editor.data.toModel(viewFragment);
|
|
|
+ this.editor.model.insertContent(modelFragment);
|
|
|
+ this.$emit("input", this.editor.getData());
|
|
|
+ },
|
|
|
+ openMultiImagePicker: function () {
|
|
|
+ if (this.$refs.multiImageInput) {
|
|
|
+ this.$refs.multiImageInput.click();
|
|
|
+ }
|
|
|
+ },
|
|
|
+ openVideoPicker: function () {
|
|
|
+ if (this.$refs.videoInput) {
|
|
|
+ this.$refs.videoInput.click();
|
|
|
+ }
|
|
|
+ },
|
|
|
+ handleMultiImageSelect: function (event) {
|
|
|
+ var files = Array.prototype.slice.call(event.target.files || []);
|
|
|
+ event.target.value = "";
|
|
|
+ this.uploadImageFiles(files);
|
|
|
+ },
|
|
|
+ handleVideoSelect: function (event) {
|
|
|
+ var files = Array.prototype.slice.call(event.target.files || []);
|
|
|
+ event.target.value = "";
|
|
|
+ if (files.length > 0) {
|
|
|
+ this.uploadVideoFile(files[0]);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ uploadImageFiles: function (files) {
|
|
|
+ var imageFiles = files.filter(function (file) {
|
|
|
+ return file && file.type && file.type.indexOf("image/") === 0;
|
|
|
+ });
|
|
|
+ if (imageFiles.length == 0) {
|
|
|
+ space.message.warning("请选择图片文件");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ var _this = this;
|
|
|
+ _this.uploading = true;
|
|
|
+ _this.uploadStatus = "正在上传图片 0/" + imageFiles.length;
|
|
|
+
|
|
|
+ var chain = Promise.resolve();
|
|
|
+ imageFiles.forEach(function (file, index) {
|
|
|
+ chain = chain.then(function () {
|
|
|
+ _this.uploadStatus = "正在上传图片 " + (index + 1) + "/" + imageFiles.length;
|
|
|
+ return MyUploadAdapter.uploadPlainFile(file, _this.getUploadOptions()).then(function (res) {
|
|
|
+ _this.insertHtml('<p><img src="' + res.default + '"></p>');
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ chain.then(function () {
|
|
|
+ space.message.success("图片已上传");
|
|
|
+ }).catch(function (msg) {
|
|
|
+ space.message.error(msg || "图片上传失败");
|
|
|
+ }).finally(function () {
|
|
|
+ _this.uploading = false;
|
|
|
+ _this.uploadStatus = "";
|
|
|
+ });
|
|
|
+ },
|
|
|
+ uploadVideoFile: function (file) {
|
|
|
+ if (!file || !file.type || file.type.indexOf("video/") !== 0) {
|
|
|
+ space.message.warning("请选择视频文件");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ var _this = this;
|
|
|
+ _this.uploading = true;
|
|
|
+ _this.uploadStatus = "正在上传视频";
|
|
|
+ MyUploadAdapter.uploadPlainFile(file, {
|
|
|
+ imageQuality: 'origin'
|
|
|
+ }, function (loaded, total) {
|
|
|
+ if (total > 0) {
|
|
|
+ _this.uploadStatus = "正在上传视频 " + Math.round(loaded * 100 / total) + "%";
|
|
|
+ }
|
|
|
+ }).then(function (res) {
|
|
|
+ if (_this.editor && _this.editor.commands.get('mediaEmbed')) {
|
|
|
+ _this.editor.execute('mediaEmbed', res.default);
|
|
|
+ } else {
|
|
|
+ _this.insertHtml('<p><video controls playsinline preload="metadata" src="' + res.default + '"></video></p>');
|
|
|
+ }
|
|
|
+ _this.$emit("input", _this.editor.getData());
|
|
|
+ space.message.success("视频已插入正文");
|
|
|
+ }).catch(function (msg) {
|
|
|
+ space.message.error(msg || "视频上传失败");
|
|
|
+ }).finally(function () {
|
|
|
+ _this.uploading = false;
|
|
|
+ _this.uploadStatus = "";
|
|
|
+ });
|
|
|
+ },
|
|
|
+ bindVideoPasteUpload: function () {
|
|
|
+ if (!this.editor || !this.editor.ui || !this.editor.ui.getEditableElement) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ var editable = this.editor.ui.getEditableElement();
|
|
|
+ if (!editable || editable.getAttribute("data-space-video-upload") == "true") {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ editable.setAttribute("data-space-video-upload", "true");
|
|
|
+ var _this = this;
|
|
|
+ var handler = function (event) {
|
|
|
+ var dataTransfer = event.clipboardData || event.dataTransfer;
|
|
|
+ if (!dataTransfer || !dataTransfer.files || dataTransfer.files.length == 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ var files = Array.prototype.slice.call(dataTransfer.files);
|
|
|
+ var videoFile = files.find(function (file) {
|
|
|
+ return file && file.type && file.type.indexOf("video/") === 0;
|
|
|
+ });
|
|
|
+
|
|
|
+ if (videoFile) {
|
|
|
+ event.preventDefault();
|
|
|
+ _this.uploadVideoFile(videoFile);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ editable.addEventListener("paste", handler);
|
|
|
+ editable.addEventListener("drop", handler);
|
|
|
+ },
|
|
|
+ getPreviewPostData: function () {
|
|
|
+ var data = this.previewdata || {};
|
|
|
+ return {
|
|
|
+ title: data.title || '',
|
|
|
+ source: data.source || '',
|
|
|
+ navigationid: data.navigationid || '',
|
|
|
+ desc: data.desc || '',
|
|
|
+ body: this.editor ? this.editor.getData() : (this.htmlContent || '')
|
|
|
+ };
|
|
|
+ },
|
|
|
+ openArticlePreview: function () {
|
|
|
+ if (!this.editor) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ var postData = this.getPreviewPostData();
|
|
|
+ if (space.utils.isNull(postData.navigationid)) {
|
|
|
+ space.message.warning("请先选择发布栏目");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (space.utils.isNull(postData.body)) {
|
|
|
+ space.message.warning("正文内容为空");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ var _this = this;
|
|
|
+ _this.previewLoading = true;
|
|
|
+ $.ajax({
|
|
|
+ url: _this.previewurl || "/apps?ark=website.article_preview",
|
|
|
+ type: "POST",
|
|
|
+ data: postData,
|
|
|
+ dataType: "json",
|
|
|
+ success: function (res) {
|
|
|
+ if (res.code != "success") {
|
|
|
+ space.message.error(res.msg || "生成预览失败");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ var data = res.data || {};
|
|
|
+ var baseUrl = data.url || data.previewUrl || "";
|
|
|
+ if (space.utils.isNull(baseUrl)) {
|
|
|
+ space.message.error("生成预览失败");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ _this.previewUrl = baseUrl + (baseUrl.indexOf("?") == -1 ? "?" : "&") + "_t=" + new Date().getTime();
|
|
|
+ _this.previewVisible = true;
|
|
|
+ },
|
|
|
+ error: function () {
|
|
|
+ space.message.error("生成预览失败");
|
|
|
+ },
|
|
|
+ complete: function () {
|
|
|
+ _this.previewLoading = false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
/**
|
|
|
* 初始化编辑器
|
|
|
*/
|
|
|
@@ -605,12 +902,26 @@ Vue.component('spaceEditor', {
|
|
|
|
|
|
var _this = this;
|
|
|
ClassicEditor.create(document.querySelector("#" + _this.id), {
|
|
|
- extraPlugins: [_this.myCustomUploadAdapterPlugin], // 添加上传适配器
|
|
|
+ extraPlugins: [function (editor) {
|
|
|
+ _this.installUploadAdapter(editor);
|
|
|
+ }], // 添加上传适配器
|
|
|
toolbar: toolbar || {},
|
|
|
language: 'zh-cn',
|
|
|
placeholder: _this.placeholder || '',
|
|
|
mediaEmbed: {
|
|
|
+ previewsInData: true,
|
|
|
providers: [{
|
|
|
+ name: 'spaceVideo',
|
|
|
+ url: [
|
|
|
+ /\.(mp4|webm|ogg)(\?.*)?$/i
|
|
|
+ ],
|
|
|
+ html: match => {
|
|
|
+ const input = match['input'];
|
|
|
+ return (
|
|
|
+ `<video controls playsinline preload="metadata" src="${input}" style="max-width:100%;"></video>`
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }, {
|
|
|
name: 'myprovider',
|
|
|
url: [
|
|
|
/^lizzy.*\.com.*\/media\/(\w+)/,
|
|
|
@@ -632,6 +943,7 @@ Vue.component('spaceEditor', {
|
|
|
|
|
|
_this.editor = editor;
|
|
|
_this.alignFullScreenToolbarButton();
|
|
|
+ _this.bindVideoPasteUpload();
|
|
|
|
|
|
if (_this.htmlContent != undefined) {
|
|
|
_this.editor.setData(_this.htmlContent);
|
|
|
@@ -686,8 +998,40 @@ Vue.component('spaceEditor', {
|
|
|
|
|
|
},
|
|
|
template: `
|
|
|
- <div :id="id">
|
|
|
- <script type="text/plain"></script>
|
|
|
+ <div class="space-editor-wrap">
|
|
|
+ <div class="space-editor-toolstrip" v-if="showEditorTools()">
|
|
|
+ <el-button-group>
|
|
|
+ <el-button size="small" icon="el-icon-magic-stick" @click="formatContent">一键排版</el-button>
|
|
|
+ <el-button size="small" icon="el-icon-picture-outline" @click="openMultiImagePicker" :loading="uploading">多图上传</el-button>
|
|
|
+ <el-button size="small" icon="el-icon-video-camera" @click="openVideoPicker" :loading="uploading">视频上传</el-button>
|
|
|
+ </el-button-group>
|
|
|
+ <el-select class="space-editor-quality" v-model="imageQuality" size="small" placeholder="图片质量">
|
|
|
+ <el-option label="原图" value="origin"></el-option>
|
|
|
+ <el-option label="高清 85%" value="high"></el-option>
|
|
|
+ <el-option label="标准 70%" value="standard"></el-option>
|
|
|
+ <el-option label="低流量 50%" value="low"></el-option>
|
|
|
+ </el-select>
|
|
|
+ <el-button size="small" type="primary" plain icon="el-icon-view" @click="openArticlePreview" :loading="previewLoading">三端预览</el-button>
|
|
|
+ <span class="space-editor-upload-status" v-if="uploadStatus">{{uploadStatus}}</span>
|
|
|
+ </div>
|
|
|
+ <input ref="multiImageInput" type="file" accept="image/*" multiple style="display:none;" @change="handleMultiImageSelect">
|
|
|
+ <input ref="videoInput" type="file" accept="video/*" style="display:none;" @change="handleVideoSelect">
|
|
|
+ <div :id="id">
|
|
|
+ <script type="text/plain"></script>
|
|
|
+ </div>
|
|
|
+ <el-dialog title="正文预览" :visible.sync="previewVisible" width="90%" append-to-body custom-class="space-editor-preview-dialog">
|
|
|
+ <div class="space-editor-preview-toolbar">
|
|
|
+ <el-radio-group v-model="previewMode" size="small">
|
|
|
+ <el-radio-button label="pc">PC端</el-radio-button>
|
|
|
+ <el-radio-button label="pad">平板端</el-radio-button>
|
|
|
+ <el-radio-button label="mobile">手机端</el-radio-button>
|
|
|
+ </el-radio-group>
|
|
|
+ <el-button size="small" icon="el-icon-refresh" @click="openArticlePreview" :loading="previewLoading">刷新预览</el-button>
|
|
|
+ </div>
|
|
|
+ <div class="space-editor-preview-stage">
|
|
|
+ <iframe v-if="previewUrl" :src="previewUrl" :style="previewFrameStyle" frameborder="0"></iframe>
|
|
|
+ </div>
|
|
|
+ </el-dialog>
|
|
|
</div>
|
|
|
`
|
|
|
})
|