Skip to content

Commit

Permalink
🎨 fix #7
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Feb 13, 2019
1 parent 03985a7 commit 20d8927
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
10 changes: 8 additions & 2 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ const LazyLoadImage = () => {
} else {
window.imageIntersectionObserver = new IntersectionObserver((entries) => {
entries.forEach((entrie) => {
if ((typeof entrie.isIntersecting === 'undefined' ? entrie.intersectionRatio !== 0 : entrie.isIntersecting) && entrie.target.getAttribute('data-src')) {
if ((typeof entrie.isIntersecting === 'undefined'
? entrie.intersectionRatio !== 0
: entrie.isIntersecting) && entrie.target.getAttribute('data-src')) {
loadImg(entrie.target)
}
})
Expand All @@ -47,6 +49,10 @@ const vditor2 = new Vditor('vditor2', {
token: 'test',
url: '/api/upload/editor',
linkToImgUrl: '/api/upload/fetch',
filename: name => {
// ? \ / : | < > * [ ] white to -
return name.replace(/\?|\\|\/|:|\||<|>|\*|\[|\]|\s+/g, '-')
},
},
preview: {
show: true,
Expand All @@ -67,7 +73,7 @@ const vditor = new Vditor('vditor', {
counter: 100,
resize: {
enable: true,
position: 'top'
position: 'top',
},
placeholder: 'say sth...',
lang: 'en_US',
Expand Down
1 change: 1 addition & 0 deletions src/ts/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface Upload {
success?: { (textarea: HTMLTextAreaElement, msg: string): void }
error?: { (msg: string): void }
token?: string
filename?: { (name: string): string }
}

interface MenuItem {
Expand Down
23 changes: 14 additions & 9 deletions src/ts/upload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ const genUploadingLabel = (vditor: Vditor, files: any): string => {
if (!file.name) {
return ''
}
const lastIndex = file.name.lastIndexOf('.')
const filename = vditor.options.upload.filename(file.name.substr(0, lastIndex)) + file.name.substr(lastIndex)
if (file.size > vditor.options.upload.max) {
uploadingStr += `${tag}[${file.name.replace(/\W/g,
'')}](${i18n[vditor.options.lang].over} ${vditor.options.upload.max / 1024 / 1024}M)\n`
uploadingStr += `${tag}[${filename}](${i18n[vditor.options.lang].over} ${vditor.options.upload.max / 1024 / 1024}M)\n`
} else {
uploadingStr += `${tag}[${file.name.replace(/\W/g, '')}](${i18n[vditor.options.lang].uploading})\n`
uploadingStr += `${tag}[${filename}](${i18n[vditor.options.lang].uploading})\n`
}
}
return uploadingStr
}

const genUploadedLabel = (editorElement: HTMLTextAreaElement, responseText: string, lang: string, uploadElement: HTMLElement) => {
const genUploadedLabel = (editorElement: HTMLTextAreaElement, responseText: string, options: Options, uploadElement: HTMLElement) => {
editorElement.focus()
const response = JSON.parse(responseText)

Expand All @@ -46,10 +47,12 @@ const genUploadedLabel = (editorElement: HTMLTextAreaElement, responseText: stri
}

response.data.errFiles.forEach((data: string) => {
const original = `[${data.replace(/\W/g, '')}](${i18n[lang].uploading})`
const lastIndex = data.lastIndexOf('.')
const filename = options.upload.filename(data.substr(0, lastIndex)) + data.substr(lastIndex)
const original = `[${filename}](${i18n[options.lang].uploading})`
editorElement.selectionStart = editorElement.value.split(original)[0].length
editorElement.selectionEnd = editorElement.selectionStart + original.length
insertText(editorElement, `[${data.replace(/\W/g, '')}](${i18n[lang].uploadError})`, '', true)
insertText(editorElement, `[${filename}](${i18n[options.lang].uploadError})`, '', true)
})

Object.keys(response.data.succMap).forEach((key) => {
Expand All @@ -58,10 +61,12 @@ const genUploadedLabel = (editorElement: HTMLTextAreaElement, responseText: stri
insertText(editorElement, `<audio controls="controls" src="${path}"></audio>\n`, '')
return
}
const original = `[${key.replace(/\W/g, '')}](${i18n[lang].uploading})`
const lastIndex = key.lastIndexOf('.')
const filename = options.upload.filename(key.substr(0, lastIndex)) + key.substr(lastIndex)
const original = `[${filename}](${i18n[options.lang].uploading})`
editorElement.selectionStart = editorElement.value.split(original)[0].length
editorElement.selectionEnd = editorElement.selectionStart + original.length
insertText(editorElement, `[${key.replace(/\W/g, '')}](${path})`, '', true)
insertText(editorElement, `[${filename}](${path})`, '', true)
})
}

Expand Down Expand Up @@ -111,7 +116,7 @@ const uploadFiles = (vditor: Vditor, files: any, element?: HTMLInputElement) =>
if (vditor.options.upload.success) {
vditor.options.upload.success(vditor.editor.element, xhr.responseText)
} else {
genUploadedLabel(vditor.editor.element, xhr.responseText, vditor.options.lang, vditor.upload.element)
genUploadedLabel(vditor.editor.element, xhr.responseText, vditor.options, vditor.upload.element)
}

vditor.upload.element.style.opacity = 0
Expand Down
1 change: 1 addition & 0 deletions src/ts/util/OptionsClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class OptionsClass {
url: '',
max: 10 * 1024 * 1024,
linkToImgUrl: '',
filename: name => name.replace(/\W/g, '')
},
classes: {
preview: ''
Expand Down

0 comments on commit 20d8927

Please sign in to comment.