Browse Source

:art: headers add x-mode

Van 5 years ago
parent
commit
f7ecf77ccd
4 changed files with 14 additions and 15 deletions
  1. 2 5
      src/ts/editor/html2md.ts
  2. 2 5
      src/ts/upload/index.ts
  3. 8 0
      src/ts/upload/setHeaders.ts
  4. 2 5
      src/ts/wysiwyg/index.ts

+ 2 - 5
src/ts/editor/html2md.ts

@@ -1,3 +1,4 @@
+import {setHeaders} from "../upload/setHeaders";
 import {processPasteCode} from "../util/processPasteCode";
 import {insertText} from "./insertText";
 import {setSelectionByInlineText} from "./setSelection";
@@ -27,11 +28,7 @@ export const html2md = (vditor: IVditor, textHTML: string, textPlain?: string) =
                         && vditor.options.upload.linkToImgUrl && typeof textPlain !== "undefined") {
                         const xhr = new XMLHttpRequest();
                         xhr.open("POST", vditor.options.upload.linkToImgUrl);
-                        if (vditor.options.upload.headers) {
-                            Object.keys(vditor.options.upload.headers).forEach((key) => {
-                                xhr.setRequestHeader(key, vditor.options.upload.headers[key]);
-                            });
-                        }
+                        setHeaders(vditor, xhr);
                         xhr.onreadystatechange = () => {
                             if (xhr.readyState === XMLHttpRequest.DONE) {
                                 const responseJSON = JSON.parse(xhr.responseText);

+ 2 - 5
src/ts/upload/index.ts

@@ -1,6 +1,7 @@
 import {insertText} from "../editor/insertText";
 import {i18n} from "../i18n/index";
 import {getEditorRange, setSelectionFocus} from "../util/selection";
+import {setHeaders} from "./setHeaders";
 
 class Upload {
     public element: HTMLElement;
@@ -205,11 +206,7 @@ const uploadFiles = (vditor: IVditor, files: FileList | DataTransferItemList | F
     if (vditor.options.upload.withCredentials) {
         xhr.withCredentials = true;
     }
-    if (vditor.options.upload.headers) {
-        Object.keys(vditor.options.upload.headers).forEach((key) => {
-            xhr.setRequestHeader(key, vditor.options.upload.headers[key]);
-        });
-    }
+    setHeaders(vditor, xhr);
     vditor.upload.isUploading = true;
     editorElement.setAttribute("contenteditable", "false");
 

+ 8 - 0
src/ts/upload/setHeaders.ts

@@ -0,0 +1,8 @@
+export const setHeaders = (vditor: IVditor, xhr: XMLHttpRequest) => {
+    if (vditor.options.upload.headers) {
+        Object.keys(vditor.options.upload.headers).forEach((key) => {
+            xhr.setRequestHeader(key, vditor.options.upload.headers[key]);
+        });
+        xhr.setRequestHeader("X-Mode", vditor.currentMode);
+    }
+};

+ 2 - 5
src/ts/wysiwyg/index.ts

@@ -1,4 +1,5 @@
 import {uploadFiles} from "../upload";
+import {setHeaders} from "../upload/setHeaders";
 import {isCtrl} from "../util/compatibility";
 import {focusEvent, hotkeyEvent, selectEvent} from "../util/editorCommenEvent";
 import {
@@ -176,11 +177,7 @@ class WYSIWYG {
                                         && src.indexOf("file://") === -1 && vditor.options.upload.linkToImgUrl) {
                                         const xhr = new XMLHttpRequest();
                                         xhr.open("POST", vditor.options.upload.linkToImgUrl);
-                                        if (vditor.options.upload.headers) {
-                                            Object.keys(vditor.options.upload.headers).forEach((key) => {
-                                                xhr.setRequestHeader(key, vditor.options.upload.headers[key]);
-                                            });
-                                        }
+                                        setHeaders(vditor, xhr);
                                         xhr.onreadystatechange = () => {
                                             if (xhr.readyState === XMLHttpRequest.DONE) {
                                                 const responseJSON = JSON.parse(xhr.responseText);