Browse Source

:sparkles: fix #1

Van 6 years ago
parent
commit
5776d9e985
7 changed files with 28 additions and 8 deletions
  1. 3 2
      CHANGELOG.md
  2. 1 1
      demo/index.js
  3. 1 1
      package-lock.json
  4. 0 0
      src/js/lute/lute.min.js
  5. 1 0
      src/ts/types/index.d.ts
  6. 11 4
      src/ts/upload/index.ts
  7. 11 0
      src/ts/util/getRange.ts

+ 3 - 2
CHANGELOG.md

@@ -32,7 +32,6 @@
 
 ### TODO
 
-* [1](https://github.com/Vanessa219/vditor/issues/1) 上传时支持 xhr.setRequestHeader 设置 `enhancement`
 * [2](https://github.com/Vanessa219/vditor/issues/2) 所见即所得 `enhancement`
 * [3](https://github.com/Vanessa219/vditor/issues/3) 编辑预览同步滚动改进 `enhancement`
 * [4](https://github.com/Vanessa219/vditor/issues/4) 添加支持思维导图的功能 `enhancement`
@@ -41,9 +40,11 @@
    * public static mermaidRender(element: HTMLElement, className?: string)
    * hotkey 和 setSelection 方法不支持 wysiwyg
    * setValue 参数改为 markdown
+   * 添加 options.upload.headers
 
-### v1.10.3 / 2019-11-24
+### v1.10.3 / 2019-11-30
 
+* [1](https://github.com/Vanessa219/vditor/issues/1) 上传时支持 xhr.setRequestHeader 设置 `enhancement`
 * [172](https://github.com/b3log/vditor/issues/172) 上传改进  `enhancement`
 * [171](https://github.com/b3log/vditor/issues/171) 编辑器在生成 preview 之前,添加处理函数  `feature`
 * [170](https://github.com/b3log/vditor/issues/170) 新增内联数学公式开关 `enhancement`

+ 1 - 1
demo/index.js

@@ -2,7 +2,7 @@ import Vditor from '../src/index'
 import '../src/assets/scss/classic.scss'
 
 window.vditor = new Vditor('vditor', {
-  mode: "wysiwyg-show",
+  // mode: "wysiwyg-show",
   typewriterMode: true,
   counter: 100,
   height: 300,

+ 1 - 1
package-lock.json

@@ -1,6 +1,6 @@
 {
   "name": "vditor",
-  "version": "1.9.6",
+  "version": "1.10.2",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {

File diff suppressed because it is too large
+ 0 - 0
src/js/lute/lute.min.js


+ 1 - 0
src/ts/types/index.d.ts

@@ -98,6 +98,7 @@ interface IUpload {
     token?: string;
     accept?: string;
     withCredentials?: boolean;
+    headers?: { [key: string]: string };
 
     success?(editor: HTMLPreElement, msg: string): void;
 

+ 11 - 4
src/ts/upload/index.ts

@@ -1,6 +1,7 @@
 import {insertText} from "../editor/insertText";
 import {setSelectionFocus} from "../editor/setSelection";
 import {i18n} from "../i18n/index";
+import {getRange} from "../util/getRange";
 
 class Upload {
     public element: HTMLElement;
@@ -171,8 +172,9 @@ const uploadFiles = (vditor: IVditor, files: FileList | DataTransferItemList | F
             return;
         }
     }
+    const editorElement = vditor.currentMode === "markdown" ? vditor.editor.element : vditor.wysiwyg.element;
 
-    vditor.upload.range = getSelection().getRangeAt(0);
+    vditor.upload.range = getRange(editorElement)
 
     const validateResult = validateFile(vditor, fileList);
     if (validateResult.length === 0) {
@@ -195,8 +197,13 @@ 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]);
+        });
+    }
     vditor.upload.isUploading = true;
-    vditor.editor.element.setAttribute("contenteditable", "false");
+    editorElement.setAttribute("contenteditable", "false");
 
     xhr.onreadystatechange = () => {
         if (xhr.readyState === XMLHttpRequest.DONE) {
@@ -204,11 +211,11 @@ const uploadFiles = (vditor: IVditor, files: FileList | DataTransferItemList | F
             if (element) {
                 element.value = "";
             }
-            vditor.editor.element.setAttribute("contenteditable", "true");
+            editorElement.setAttribute("contenteditable", "true");
 
             if (xhr.status === 200) {
                 if (vditor.options.upload.success) {
-                    vditor.options.upload.success(vditor.editor.element, xhr.responseText);
+                    vditor.options.upload.success(editorElement, xhr.responseText);
                 } else {
                     let responseText = xhr.responseText;
                     if (vditor.options.upload.format) {

+ 11 - 0
src/ts/util/getRange.ts

@@ -0,0 +1,11 @@
+export const getRange = (element: HTMLElement) => {
+    const selection = getSelection()
+    if (selection.rangeCount > 0) {
+        return selection.getRangeAt(0);
+    } else {
+        const range = element.ownerDocument.createRange();
+        range.setStart(element, 0);
+        range.collapse(true);
+        return range;
+    }
+}

Some files were not shown because too many files changed in this diff