Skip to content

Commit

Permalink
♻️ #13 vditor types
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Feb 17, 2019
1 parent f0b3a4e commit a9a605e
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 31 deletions.
14 changes: 8 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {UploadClass} from "./ts/upload/index";

class VditorClass {
readonly version: string;
vditor: any
vditor: Vditor

constructor(id: string, options?: Options) {
this.version = VDITOR_VERSION;
Expand All @@ -40,10 +40,12 @@ class VditorClass {
this.vditor.resize = resize
}

const toolbar = new Toolbar(this.vditor)
this.vditor.toolbar = toolbar
if (mergedOptions.toolbar) {
const toolbar: Toolbar = new Toolbar(this.vditor)
this.vditor.toolbar = toolbar
}

if (toolbar.elements.preview) {
if (this.vditor.toolbar.elements.preview) {
const preview = new Preview(this.vditor)
this.vditor.preview = preview
}
Expand All @@ -55,7 +57,7 @@ class VditorClass {

new Ui(this.vditor)

if (this.vditor.options.atUser || this.vditor.toolbar.elements.emoji) {
if (this.vditor.options.hint.at || this.vditor.toolbar.elements.emoji) {
const hint = new Hint(this.vditor)
this.vditor.hint = hint
}
Expand Down Expand Up @@ -94,7 +96,7 @@ class VditorClass {
}

getSelection() {
return this.vditor.editor.element.value.substring(this.vditor.editor.element.selectionStart, this.vditor.editor.element.selectionEnd)
return this.vditor.editor.element.value.substring(this.vditor.editor.element.selectionStart, this.vditor.editor.element.selectionEnd)
}

setValue(value: string) {
Expand Down
6 changes: 4 additions & 2 deletions src/ts/editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ class Editor {
vditor.options.focus(this.element.value)
}
if (vditor.toolbar.elements.emoji && vditor.toolbar.elements.emoji.children[1]) {
vditor.toolbar.elements.emoji.children[1].style.display = 'none'
const emojiPanel = <HTMLElement>vditor.toolbar.elements.emoji.children[1]
emojiPanel.style.display = 'none'
}
if (vditor.toolbar.elements.headings && vditor.toolbar.elements.headings.children[1]) {
vditor.toolbar.elements.headings.children[1].style.display = 'none'
const headingsPanel = <HTMLElement>vditor.toolbar.elements.headings.children[1]
headingsPanel.style.display = 'none'
}
})

Expand Down
7 changes: 3 additions & 4 deletions src/ts/hint/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {getTextareaPosition} from "../util/textareaPosition";
import {insertText} from "../editor/index";
import {allEmoji} from "../emoji/allEmoji";

export class Hint {
timeId: number
editorElement: HTMLTextAreaElement
element: HTMLUListElement
atUser: { (value: string): Array<any> }
commonEmoji: any
atUser: { (value: string): Array<HintData> }
commonEmoji: { [key: string]: string }
hintDelay: number

constructor(vditor: Vditor) {
Expand Down Expand Up @@ -98,7 +97,7 @@ export class Hint {
return key
}

private genHTML(data: Array<any>, key: string) {
private genHTML(data: Array<HintData>, key: string) {
if (data.length === 0) {
this.element.style.display = 'none'
return
Expand Down
3 changes: 2 additions & 1 deletion src/ts/toolbar/Emoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export class Emoji extends MenuItemClass {
} else {
emojiPanelElement.style.display = 'block'
if (vditor.toolbar.elements.headings) {
vditor.toolbar.elements.headings.children[1].style.display = 'none'
const headingsPanel = <HTMLElement>vditor.toolbar.elements.headings.children[1]
headingsPanel.style.display = 'none'
}
}
})
Expand Down
4 changes: 2 additions & 2 deletions src/ts/toolbar/Fullscreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class Fullscreen extends MenuItemClass {
this.innerHTML = menuItem.icon || fullscreenSVG
vditorElement.className = vditorElement.className.replace(' vditor--fullscreen', '')
Object.keys(vditor.toolbar.elements).forEach((key) => {
const svgElement = vditor.toolbar.elements[key].firstChild
const svgElement = <HTMLElement>vditor.toolbar.elements[key].firstChild
if (svgElement) {
svgElement.className = svgElement.className.replace('__s', '__n')
}
Expand All @@ -25,7 +25,7 @@ export class Fullscreen extends MenuItemClass {
this.innerHTML = menuItem.icon || contractSVG
vditorElement.className = vditorElement.className + ' vditor--fullscreen'
Object.keys(vditor.toolbar.elements).forEach((key) => {
const svgElement = vditor.toolbar.elements[key].firstChild
const svgElement = <HTMLElement>vditor.toolbar.elements[key].firstChild
if (svgElement) {
svgElement.className = svgElement.className.replace('__n', '__s')
}
Expand Down
3 changes: 2 additions & 1 deletion src/ts/toolbar/Headings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export class Headings extends MenuItemClass {
} else {
headingsPanelElement.style.display = 'block'
if (vditor.toolbar.elements.emoji) {
vditor.toolbar.elements.emoji.children[1].style.display = 'none'
const panel = <HTMLElement>vditor.toolbar.elements.emoji.children[1]
panel.style.display = 'none'
}
}
})
Expand Down
4 changes: 2 additions & 2 deletions src/ts/toolbar/Record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Record extends MenuItemClass {
}
mediaRecorder.startRecordingNewWavFile()
vditor.upload.element.children[0].innerHTML = i18n[vditor.options.lang].recoding
vditor.upload.element.style.opacity = 1
vditor.upload.element.style.opacity = '1'
vditor.upload.element.className = 'vditor-upload vditor-upload--tip'
vditor.editor.element.setAttribute('disabled', 'disabled')
}).catch((err: ErrorEvent) => {
Expand All @@ -48,7 +48,7 @@ export class Record extends MenuItemClass {
uploadFiles(vditor, [file])
} else {
vditor.upload.element.children[0].innerHTML = i18n[vditor.options.lang].recoding
vditor.upload.element.style.opacity = 1
vditor.upload.element.style.opacity = '1'
vditor.upload.element.className = 'vditor-upload vditor-upload--tip'
vditor.editor.element.setAttribute('disabled', 'disabled')
mediaRecorder.startRecordingNewWavFile()
Expand Down
47 changes: 37 additions & 10 deletions src/ts/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ interface Preview {
url?: string
}

interface HintData {
html: string,
value: string
}

interface Hint {
emojiTail?: string
delay?: number
emoji?: any
at?: { (value: string): Array<any> }
emoji?: { [key: string]: string }
at?: { (value: string): Array<HintData> }
}

interface Resize {
Expand Down Expand Up @@ -83,12 +88,34 @@ interface Vditor {
id: string
mdTimeoutId: number
options: Options
timeId: number
toolbar?: any
preview?: any
editor?: any
counter?: any
resize?: any
hint?: any
upload?: any
toolbar?: {
elements?: { [key: string]: HTMLDivElement }
}
preview?: {
element: HTMLElement
render: { (vditor: Vditor, value?: string): void }
},
editor?: {
element: HTMLTextAreaElement
},
counter?: {
element: HTMLElement
render: { (length: number, counter: number): void }
},
resize?: {
element: HTMLElement
},
hint?: {
timeId: number
editorElement: HTMLTextAreaElement
element: HTMLUListElement
atUser: { (value: string): Array<HintData> }
commonEmoji: { [key: string]: string }
hintDelay: number
render: { (): void }
}
upload?: {
element: HTMLElement
isUploading: boolean
}
}
7 changes: 4 additions & 3 deletions src/ts/upload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const uploadFiles = (vditor: Vditor, files: any, element?: HTMLInputElement) =>
genUploadedLabel(vditor.editor.element, xhr.responseText, vditor.options, vditor.upload.element)
}

vditor.upload.element.style.opacity = 0
vditor.upload.element.style.opacity = '0'
} else {
if (vditor.options.upload.error) {
vditor.options.upload.error(xhr.responseText)
Expand All @@ -138,8 +138,9 @@ const uploadFiles = (vditor: Vditor, files: any, element?: HTMLInputElement) =>
return
}
const progress = event.loaded / event.total * 100
vditor.upload.element.style.opacity = 1
vditor.upload.element.children[0].style.width = progress + '%'
vditor.upload.element.style.opacity = '1'
const progressBar = <HTMLElement>vditor.upload.element.children[0]
progressBar.style.width = progress + '%'
}
xhr.send(formData)
}
Expand Down

0 comments on commit a9a605e

Please sign in to comment.