|
|
@@ -379,7 +379,7 @@ Vue.component('spaceEditor', {
|
|
|
// 编辑器需要的 js 文件
|
|
|
jsUrl: [
|
|
|
'/apps/space/plug-in/ckeditor5/build/ckeditor.js',
|
|
|
- '/apps/space/plug-in/ckeditor5/src/plugins/upload/src/myUploadAdapter.js?v=2026062712'
|
|
|
+ '/apps/space/plug-in/ckeditor5/src/plugins/upload/src/myUploadAdapter.js?v=20260628-pdf'
|
|
|
],
|
|
|
// 标识,全部为 true 则代表全部加载完成
|
|
|
jsUrlFlag: [],
|
|
|
@@ -517,6 +517,7 @@ Vue.component('spaceEditor', {
|
|
|
imageMaxWidth: 1920,
|
|
|
imageMaxSize: 20 * 1024 * 1024,
|
|
|
videoMaxSize: 1024 * 1024 * 1024,
|
|
|
+ pdfMaxSize: 100 * 1024 * 1024,
|
|
|
fileMaxSize: 10 * 1024 * 1024
|
|
|
};
|
|
|
},
|
|
|
@@ -705,6 +706,58 @@ Vue.component('spaceEditor', {
|
|
|
mediaDropdown.classList.add("space-editor-media-link-dropdown");
|
|
|
mediaDropdown.parentNode.insertBefore(videoButton, mediaDropdown);
|
|
|
},
|
|
|
+ mountPdfUploadToolbarButton: function (retryCount) {
|
|
|
+ if (!this.editor || !this.editor.ui || !this.editor.ui.view || !this.editor.ui.view.element) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ var toolbarItems = this.editor.ui.view.element.querySelector(".ck-toolbar__items");
|
|
|
+ if (!toolbarItems) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (toolbarItems.querySelector(".space-editor-pdf-upload-ck-button")) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ var dropdowns = toolbarItems.querySelectorAll(".ck-dropdown");
|
|
|
+ var mediaDropdown = null;
|
|
|
+ for (var i = 0; i < dropdowns.length; i++) {
|
|
|
+ var button = dropdowns[i].querySelector(".ck-button");
|
|
|
+ var label = button ? (button.getAttribute("aria-label") || button.getAttribute("title") || button.textContent || "") : "";
|
|
|
+ label = label.replace(/\s/g, "").toLowerCase();
|
|
|
+ if (label.indexOf("媒体") != -1 || label.indexOf("media") != -1) {
|
|
|
+ mediaDropdown = dropdowns[i];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!mediaDropdown) {
|
|
|
+ if ((retryCount || 0) < 3) {
|
|
|
+ var _this = this;
|
|
|
+ setTimeout(function () {
|
|
|
+ _this.mountPdfUploadToolbarButton((retryCount || 0) + 1);
|
|
|
+ }, 200);
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ var _this = this;
|
|
|
+ var pdfButton = document.createElement("button");
|
|
|
+ pdfButton.type = "button";
|
|
|
+ pdfButton.className = "ck ck-button ck-off space-editor-upload-ck-button space-editor-pdf-upload-ck-button";
|
|
|
+ pdfButton.setAttribute("aria-label", "上传PDF");
|
|
|
+ pdfButton.setAttribute("title", "上传PDF");
|
|
|
+ pdfButton.innerHTML = '<svg class="ck ck-icon ck-button__icon space-editor-pdf-upload-icon" viewBox="0 0 20 20" aria-hidden="true"><path d="M4 1.8h7.2L16 6.6v11.6H4V1.8zm7.6 1.9v3.5h3.5L11.6 3.7zM6 9.4h8v1.4H6V9.4zm0 2.7h8v1.4H6v-1.4zm0 2.7h5.2v1.4H6v-1.4z"></path><path d="M7.1 6.9h1.3c.8 0 1.3-.4 1.3-1.1 0-.7-.5-1.1-1.3-1.1H6v3.9h1.1V6.9zm0-.8v-.6h1.1c.2 0 .4.1.4.3 0 .2-.1.3-.4.3H7.1z"></path></svg><span class="ck ck-button__label">上传PDF</span>';
|
|
|
+ pdfButton.addEventListener("click", function (event) {
|
|
|
+ event.preventDefault();
|
|
|
+ event.stopPropagation();
|
|
|
+ _this.openPdfPicker();
|
|
|
+ });
|
|
|
+
|
|
|
+ mediaDropdown.classList.add("space-editor-media-link-dropdown");
|
|
|
+ mediaDropdown.parentNode.insertBefore(pdfButton, mediaDropdown);
|
|
|
+ },
|
|
|
formatContent: function () {
|
|
|
if (!this.editor) {
|
|
|
return;
|
|
|
@@ -750,7 +803,7 @@ Vue.component('spaceEditor', {
|
|
|
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 hasMedia = block.querySelector("img, video, iframe, object, embed, table, .space-editor-pdf-viewer");
|
|
|
var text = (block.textContent || "").replace(/\u00a0/g, "").trim();
|
|
|
if (!hasMedia && text == "" && block.parentNode) {
|
|
|
block.parentNode.removeChild(block);
|
|
|
@@ -768,7 +821,7 @@ Vue.component('spaceEditor', {
|
|
|
if (child.nodeType == 3 && child.nodeValue.replace(/[\s\u00a0\u3000]/g, "") != "") {
|
|
|
return child;
|
|
|
}
|
|
|
- if (child.nodeType == 1 && !/^(img|video|iframe|table)$/i.test(child.tagName)) {
|
|
|
+ if (child.nodeType == 1 && !/^(img|video|iframe|object|embed|table)$/i.test(child.tagName)) {
|
|
|
var textNode = findFirstTextNode(child);
|
|
|
if (textNode) {
|
|
|
return textNode;
|
|
|
@@ -788,7 +841,7 @@ Vue.component('spaceEditor', {
|
|
|
var textBlocks = wrapper.querySelectorAll("p, div");
|
|
|
for (var blockIndex = 0; blockIndex < textBlocks.length; blockIndex++) {
|
|
|
var textBlock = textBlocks[blockIndex];
|
|
|
- var blockHasMedia = textBlock.querySelector("img, video, iframe, table");
|
|
|
+ var blockHasMedia = textBlock.querySelector("img, video, iframe, object, embed, table, .space-editor-pdf-viewer");
|
|
|
var blockText = (textBlock.textContent || "").replace(/\u00a0/g, "").trim();
|
|
|
if (blockHasMedia) {
|
|
|
textBlock.style.textAlign = "center";
|
|
|
@@ -809,7 +862,7 @@ Vue.component('spaceEditor', {
|
|
|
images[imgIndex].style.margin = "12px auto";
|
|
|
}
|
|
|
|
|
|
- var videos = wrapper.querySelectorAll("video, iframe");
|
|
|
+ var videos = wrapper.querySelectorAll("video, iframe, object, embed");
|
|
|
for (var v = 0; v < videos.length; v++) {
|
|
|
videos[v].style.maxWidth = "100%";
|
|
|
if (videos[v].tagName.toLowerCase() == "video") {
|
|
|
@@ -863,11 +916,26 @@ Vue.component('spaceEditor', {
|
|
|
text = text.replace(/(\/web\/upload\/modules)\/+/i, "$1/");
|
|
|
return text;
|
|
|
},
|
|
|
+ toAbsoluteEditorMediaUrl: function (url) {
|
|
|
+ var text = String(url || "").trim();
|
|
|
+ if (/^(https?:)?\/\//i.test(text) || /^(data|blob):/i.test(text)) {
|
|
|
+ return text;
|
|
|
+ }
|
|
|
+ if (text.charAt(0) == "/") {
|
|
|
+ return window.location.origin + text;
|
|
|
+ }
|
|
|
+ return text;
|
|
|
+ },
|
|
|
openVideoPicker: function () {
|
|
|
if (this.$refs.videoInput) {
|
|
|
this.$refs.videoInput.click();
|
|
|
}
|
|
|
},
|
|
|
+ openPdfPicker: function () {
|
|
|
+ if (this.$refs.pdfInput) {
|
|
|
+ this.$refs.pdfInput.click();
|
|
|
+ }
|
|
|
+ },
|
|
|
handleVideoSelect: function (event) {
|
|
|
var files = Array.prototype.slice.call(event.target.files || []);
|
|
|
event.target.value = "";
|
|
|
@@ -875,6 +943,16 @@ Vue.component('spaceEditor', {
|
|
|
this.uploadVideoFile(files[0]);
|
|
|
}
|
|
|
},
|
|
|
+ handlePdfSelect: function (event) {
|
|
|
+ var files = Array.prototype.slice.call(event.target.files || []);
|
|
|
+ event.target.value = "";
|
|
|
+ if (files.length > 0) {
|
|
|
+ this.uploadPdfFile(files[0]);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ isPdfFile: function (file) {
|
|
|
+ return !!file && ((file.type && file.type.toLowerCase() == "application/pdf") || /\.pdf$/i.test(file.name || ""));
|
|
|
+ },
|
|
|
uploadVideoFile: function (file) {
|
|
|
if (!file || !file.type || file.type.indexOf("video/") !== 0) {
|
|
|
space.message.warning("请选择视频文件");
|
|
|
@@ -902,6 +980,62 @@ Vue.component('spaceEditor', {
|
|
|
_this.uploadStatus = "";
|
|
|
});
|
|
|
},
|
|
|
+ uploadPdfFile: function (file) {
|
|
|
+ if (!this.isPdfFile(file)) {
|
|
|
+ space.message.warning("请选择PDF文件");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ var _this = this;
|
|
|
+ _this.uploading = true;
|
|
|
+ _this.uploadStatus = "正在上传PDF";
|
|
|
+ _this.uploadPlainFileByAdapter(file, {
|
|
|
+ imageQuality: 'origin',
|
|
|
+ pdfMaxSize: 100 * 1024 * 1024
|
|
|
+ }, function (loaded, total) {
|
|
|
+ if (total > 0) {
|
|
|
+ _this.uploadStatus = "正在上传PDF " + Math.round(loaded * 100 / total) + "%";
|
|
|
+ }
|
|
|
+ }).then(function (res) {
|
|
|
+ var pdfUrl = _this.normalizeLocalUploadUrl(res.default);
|
|
|
+ _this.insertPdfViewer(pdfUrl, file.name);
|
|
|
+ _this.$emit("input", _this.editor.getData());
|
|
|
+ space.message.success("PDF已插入正文");
|
|
|
+ }).catch(function (msg) {
|
|
|
+ space.message.error(msg || "PDF上传失败");
|
|
|
+ }).finally(function () {
|
|
|
+ _this.uploading = false;
|
|
|
+ _this.uploadStatus = "";
|
|
|
+ });
|
|
|
+ },
|
|
|
+ buildPdfViewerHtml: function (url, fileName) {
|
|
|
+ var safeUrl = this.escapeHtmlAttr(url);
|
|
|
+ var safeName = this.escapeHtmlAttr(fileName || "PDF文件");
|
|
|
+ return '<figure class="space-editor-pdf-viewer media" data-space-pdf="true">'
|
|
|
+ + '<iframe class="space-editor-pdf-frame" src="' + safeUrl + '" title="' + safeName + '" loading="lazy"></iframe>'
|
|
|
+ + '<figcaption><a href="' + safeUrl + '" target="_blank" rel="noopener">打开 PDF:' + safeName + '</a></figcaption>'
|
|
|
+ + '</figure><p></p>';
|
|
|
+ },
|
|
|
+ insertPdfViewer: function (url, fileName) {
|
|
|
+ if (!this.editor || space.utils.isNull(url)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (this.editor.commands && this.editor.commands.get("mediaEmbed")) {
|
|
|
+ this.editor.execute("mediaEmbed", this.toAbsoluteEditorMediaUrl(url));
|
|
|
+ this.insertHtml(this.buildPdfLinkHtml(url, fileName));
|
|
|
+ this.deferSyncEditorPlayableVideos();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ this.insertHtml(this.buildPdfViewerHtml(url, fileName));
|
|
|
+ },
|
|
|
+ buildPdfLinkHtml: function (url, fileName) {
|
|
|
+ var safeUrl = this.escapeHtmlAttr(url);
|
|
|
+ var safeName = this.escapeHtmlAttr(fileName || "PDF文件");
|
|
|
+ return '<p class="space-editor-pdf-link"><a href="' + safeUrl
|
|
|
+ + '" target="_blank" rel="noopener">打开 PDF:' + safeName + '</a></p>';
|
|
|
+ },
|
|
|
bindVideoPasteUpload: function () {
|
|
|
if (!this.editor || !this.editor.ui || !this.editor.ui.getEditableElement) {
|
|
|
return;
|
|
|
@@ -924,10 +1058,16 @@ Vue.component('spaceEditor', {
|
|
|
var videoFile = files.find(function (file) {
|
|
|
return file && file.type && file.type.indexOf("video/") === 0;
|
|
|
});
|
|
|
+ var pdfFile = files.find(function (file) {
|
|
|
+ return _this.isPdfFile(file);
|
|
|
+ });
|
|
|
|
|
|
if (videoFile) {
|
|
|
event.preventDefault();
|
|
|
_this.uploadVideoFile(videoFile);
|
|
|
+ } else if (pdfFile) {
|
|
|
+ event.preventDefault();
|
|
|
+ _this.uploadPdfFile(pdfFile);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
@@ -1178,6 +1318,17 @@ Vue.component('spaceEditor', {
|
|
|
`<video controls playsinline preload="metadata" src="${input}" style="max-width:100%;"></video>`
|
|
|
);
|
|
|
}
|
|
|
+ }, {
|
|
|
+ name: 'spacePdf',
|
|
|
+ url: [
|
|
|
+ /\.pdf(\?.*)?$/i
|
|
|
+ ],
|
|
|
+ html: match => {
|
|
|
+ const input = match['input'];
|
|
|
+ return (
|
|
|
+ `<iframe class="space-editor-pdf-frame" src="${input}" title="PDF文件" loading="lazy" frameborder="0"></iframe>`
|
|
|
+ );
|
|
|
+ }
|
|
|
}, {
|
|
|
name: 'myprovider',
|
|
|
url: [
|
|
|
@@ -1201,6 +1352,7 @@ Vue.component('spaceEditor', {
|
|
|
_this.editor = editor;
|
|
|
_this.alignFullScreenToolbarButton();
|
|
|
_this.mountVideoUploadToolbarButton();
|
|
|
+ _this.mountPdfUploadToolbarButton();
|
|
|
_this.bindVideoPasteUpload();
|
|
|
_this.bindEditorVideoPlayback();
|
|
|
|
|
|
@@ -1273,6 +1425,7 @@ Vue.component('spaceEditor', {
|
|
|
<span class="space-editor-upload-status" v-if="uploadStatus">{{uploadStatus}}</span>
|
|
|
</div>
|
|
|
<input ref="videoInput" type="file" accept="video/*" style="display:none;" @change="handleVideoSelect">
|
|
|
+ <input ref="pdfInput" type="file" accept="application/pdf,.pdf" style="display:none;" @change="handlePdfSelect">
|
|
|
<div :id="id">
|
|
|
<script type="text/plain"></script>
|
|
|
</div>
|