Skip to content

Commit

Permalink
fix #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Nov 30, 2019
1 parent cf763e2 commit 5776d9e
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 18 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Expand Up @@ -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`
Expand All @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion demo/index.js
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions src/js/lute/lute.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/ts/types/index.d.ts
Expand Up @@ -98,6 +98,7 @@ interface IUpload {
token?: string;
accept?: string;
withCredentials?: boolean;
headers?: { [key: string]: string };

success?(editor: HTMLPreElement, msg: string): void;

Expand Down
15 changes: 11 additions & 4 deletions 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;
Expand Down Expand Up @@ -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) {
Expand All @@ -195,20 +197,25 @@ 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) {
vditor.upload.isUploading = false;
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) {
Expand Down
11 changes: 11 additions & 0 deletions 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;
}
}

0 comments on commit 5776d9e

Please sign in to comment.