From f3a7151da49798fe5174912efbc4f0a7e893254f Mon Sep 17 00:00:00 2001 From: Liyuan Li Date: Thu, 19 Mar 2020 17:11:51 +0800 Subject: [PATCH] :package: #225 --- CHANGELOG.md | 1 + package.json | 3 +- src/assets/index.d.min.ts | 236 ------------------------------- src/ts/ir/input.ts | 5 +- src/ts/markdown/speechRender.ts | 2 +- src/ts/toolbar/Upload.ts | 19 +-- src/ts/types/index.d.ts | 84 +++++++++-- src/ts/util/editorCommenEvent.ts | 2 +- src/ts/wysiwyg/index.ts | 2 +- webpack.config.js | 1 - 10 files changed, 94 insertions(+), 261 deletions(-) delete mode 100644 src/assets/index.d.min.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 63dc933e4..2fbb78e32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,7 @@ ### v3.0.1 / 未发布 * [27](/~https://github.com/Vanessa219/vditor/issues/27) 支持类似 Typora 的及时渲染模式 `引入特性` +* [225](/~https://github.com/Vanessa219/vditor/pull/225) publish type declaration file `引入特性` ### v3.0.0 / 2020-03-19 diff --git a/package.json b/package.json index 6ca1d6812..f640cd6e4 100755 --- a/package.json +++ b/package.json @@ -10,13 +10,14 @@ "files": [ "dist/*", "src/index.ts", + "src/method.ts", "src/ts/*", "src/assets/*" ], "dependencies": { "diff-match-patch": "^1.0.4" }, - "types": "dist/index.d.min.ts", + "types": "src/ts/types/index.d.ts", "devDependencies": { "@babel/core": "^7.8.7", "@babel/preset-env": "^7.8.7", diff --git a/src/assets/index.d.min.ts b/src/assets/index.d.min.ts deleted file mode 100644 index 6c4978482..000000000 --- a/src/assets/index.d.min.ts +++ /dev/null @@ -1,236 +0,0 @@ -interface II18nLang { - en_US: string; - zh_CN: string; -} - -interface IClasses { - preview?: string; -} - -interface IUpload { - url?: string; - max?: number; - linkToImgUrl?: string; - token?: string; - accept?: string; - withCredentials?: boolean; - headers?: { [key: string]: string }; - - success?(editor: HTMLPreElement, msg: string): void; - - error?(msg: string): void; - - filename?(name: string): string; - - validate?(files: File[]): string | boolean; - - handler?(files: File[]): string | null; - - format?(files: File[], responseText: string): string; - - file?(files: File[]): File[]; -} - -interface IMenuItem { - name: string; - icon?: string; - tip?: string; - hotkey?: string; - suffix?: string; - prefix?: string; - tipPosition?: string; - - click?(): void; -} - -interface IPreviewMode { - both: string; - preview: string; - editor: string; -} - -interface IHljs { - lineNumber?: boolean; - style?: string; - enable?: boolean; -} - -interface IMath { - inlineDigit: boolean; - engine: "KaTeX" | "MathJax"; - config?: object; -} - -interface IPreview { - delay?: number; - maxWidth?: number; - mode?: keyof IPreviewMode; - url?: string; - hljs?: IHljs; - math?: IMath; - markdown?: { - autoSpace?: boolean; - fixTermTypo?: boolean; - chinesePunct?: boolean; - }; - - parse?(element: HTMLElement): void; - - transform?(html: string): string; -} - -interface IPreviewOptions { - theme?: "classic" | "dark"; - customEmoji?: { [key: string]: string }; - lang?: (keyof II18nLang); - emojiPath?: string; - hljs?: IHljs; - speech?: { - enable?: boolean, - }; - anchor?: boolean; - math?: IMath; - cdn?: string; - markdown?: { - autoSpace?: boolean; - fixTermTypo?: boolean; - chinesePunct?: boolean; - }; - - transform?(html: string): string; -} - -interface IHintData { - html: string; - value: string; -} - -interface IHint { - emojiTail?: string; - delay?: number; - emoji?: { [key: string]: string }; - emojiPath?: string; - - at?(value: string): IHintData[]; -} - -interface IResize { - position?: string; - enable?: boolean; - - after?(height: number): void; -} - -interface IOptions { - value?: string; - debugger?: boolean; - after?: () => void; - typewriterMode?: boolean; - keymap?: { [key: string]: string }; - height?: number | string; - width?: number | string; - placeholder?: string; - lang?: (keyof II18nLang); - toolbar?: Array; - resize?: IResize; - counter?: number; - cache?: boolean; - mode?: "sv" | "wysiwyg" | "ir"; - preview?: IPreview; - hint?: IHint; - upload?: IUpload; - classes?: IClasses; - cdn?: string; - tab?: string; - theme?: "classic" | "dark"; - - input?(value: string, previewElement?: HTMLElement): void; - - focus?(value: string): void; - - blur?(value: string): void; - - esc?(value: string): void; - - ctrlEnter?(value: string): void; - - select?(value: string): void; -} - -declare class Vditor { - - public static codeRender(element: HTMLElement, lang?: (keyof II18nLang)): void; - - public static graphvizRender(element: HTMLElement, cdn?: string): void; - - public static highlightRender(hljsOption?: IHljs, element?: HTMLElement | Document, cdn?: string): void; - - public static mathRender(element: HTMLElement, options?: { cdn?: string, math?: IMath }): void; - - public static mermaidRender(element: HTMLElement, className?: string, cdn?: string): void; - - public static chartRender(element?: HTMLElement | Document, cdn?: string): void; - - public static abcRender(element?: HTMLElement | Document, cdn?: string): void; - - public static mediaRender(element: HTMLElement): void; - - public static speechRender(element: HTMLElement, lang?: (keyof II18nLang)): void; - - public static md2html(mdText: string, options?: IPreviewOptions): string; - - public static preview(previewElement: HTMLDivElement, markdown: string, options?: IPreviewOptions): void; - - public readonly version: string; - - constructor(id: string, options?: IOptions) - - public setTheme(theme: "dark" | "classic"): void; - - public getValue(): string; - - public insertValue(value: string, render: boolean): void; - - public focus(): void; - - public blur(): void; - - public disabled(): void; - - public enable(): void; - - public setSelection(start: number, end: number): void; - - public getSelection(): string; - - public setValue(markdown: string): void; - - public renderPreview(value?: string): void; - - public getCursorPosition(editor: HTMLPreElement): { - left: number, - top: number, - }; - - public deleteValue(): void; - - public updateValue(): string; - - public isUploading(): boolean; - - public clearCache(): void; - - public disabledCache(): void; - - public enableCache(): void; - - public html2md(value: string): string; - - public getHTML(): string; - - public tip(text: string, time?: number): void; - - public setPreviewMode(mode: string): void; -} - -export default Vditor; diff --git a/src/ts/ir/input.ts b/src/ts/ir/input.ts index b782f3f1b..e5a27c1a2 100644 --- a/src/ts/ir/input.ts +++ b/src/ts/ir/input.ts @@ -1,8 +1,8 @@ import {getTopList, hasClosestBlock, hasClosestByTag} from "../util/hasClosest"; import {log} from "../util/log"; +import {getSelectPosition} from "../util/selection"; import {setRangeByWbr} from "../wysiwyg/setRangeByWbr"; import {processAfterRender} from "./process"; -import {getSelectPosition} from "../util/selection"; export const input = (vditor: IVditor, range: Range) => { Array.from(vditor.ir.element.querySelectorAll(".vditor-ir__node--expand")).forEach((item) => { @@ -42,11 +42,10 @@ export const input = (vditor: IVditor, range: Range) => { } if (startSpace || endSpace) { - return + return; } } - if (!blockElement) { // 使用顶级块元素,应使用 innerHTML blockElement = vditor.ir.element; diff --git a/src/ts/markdown/speechRender.ts b/src/ts/markdown/speechRender.ts index 926c53673..c1d59895f 100644 --- a/src/ts/markdown/speechRender.ts +++ b/src/ts/markdown/speechRender.ts @@ -77,7 +77,7 @@ export const speechRender = (element: HTMLElement, lang: (keyof II18nLang) = "zh }); } - element.addEventListener("mouseup", (event: IHTMLInputEvent) => { + element.addEventListener("mouseup", (event: MouseEvent) => { const text = getSelection().toString().trim(); speechSynthesis.cancel(); if (getSelection().toString().trim() === "") { diff --git a/src/ts/toolbar/Upload.ts b/src/ts/toolbar/Upload.ts index 3bab615d5..ea581d0cc 100644 --- a/src/ts/toolbar/Upload.ts +++ b/src/ts/toolbar/Upload.ts @@ -14,14 +14,15 @@ export class Upload extends MenuItem { } public _bindEvent(vditor: IVditor) { - this.element.querySelector("input").addEventListener("change", (event: IHTMLInputEvent) => { - if (this.element.firstElementChild.classList.contains("vditor-menu--disabled")) { - return; - } - if (event.target.files.length === 0) { - return; - } - uploadFiles(vditor, event.target.files, event.target); - }); + this.element.querySelector("input").addEventListener("change", + (event: InputEvent & { target: HTMLInputElement }) => { + if (this.element.firstElementChild.classList.contains("vditor-menu--disabled")) { + return; + } + if (event.target.files.length === 0) { + return; + } + uploadFiles(vditor, event.target.files, event.target); + }); } } diff --git a/src/ts/types/index.d.ts b/src/ts/types/index.d.ts index fcbf083ec..d1547b652 100644 --- a/src/ts/types/index.d.ts +++ b/src/ts/types/index.d.ts @@ -91,14 +91,6 @@ declare const webkitAudioContext: { new(contextOptions?: AudioContextOptions): AudioContext, }; -interface IHTMLInputEvent extends Event { - target: HTMLInputElement & EventTarget; - screenX: number; - isComposing: boolean; - inputType: string; - key: string; -} - interface II18nLang { en_US: string; ko_KR: string; @@ -355,3 +347,79 @@ interface IVditor { composingLock: boolean, }; } + +declare class Vditor { + + public static codeRender(element: HTMLElement, lang?: (keyof II18nLang)): void; + + public static graphvizRender(element: HTMLElement, cdn?: string): void; + + public static highlightRender(hljsOption?: IHljs, element?: HTMLElement | Document, cdn?: string): void; + + public static mathRender(element: HTMLElement, options?: { cdn?: string, math?: IMath }): void; + + public static mermaidRender(element: HTMLElement, className?: string, cdn?: string): void; + + public static chartRender(element?: HTMLElement | Document, cdn?: string): void; + + public static abcRender(element?: HTMLElement | Document, cdn?: string): void; + + public static mediaRender(element: HTMLElement): void; + + public static speechRender(element: HTMLElement, lang?: (keyof II18nLang)): void; + + public static md2html(mdText: string, options?: IPreviewOptions): string; + + public static preview(previewElement: HTMLDivElement, markdown: string, options?: IPreviewOptions): void; + + public readonly version: string; + + constructor(id: string, options?: IOptions) + + public setTheme(theme: "dark" | "classic"): void; + + public getValue(): string; + + public insertValue(value: string, render: boolean): void; + + public focus(): void; + + public blur(): void; + + public disabled(): void; + + public enable(): void; + + public setSelection(start: number, end: number): void; + + public getSelection(): string; + + public setValue(markdown: string): void; + + public renderPreview(value?: string): void; + + public getCursorPosition(editor: HTMLPreElement): { + left: number, + top: number, + }; + + public deleteValue(): void; + + public updateValue(): string; + + public isUploading(): boolean; + + public clearCache(): void; + + public disabledCache(): void; + + public enableCache(): void; + + public html2md(value: string): string; + + public getHTML(): string; + + public tip(text: string, time?: number): void; + + public setPreviewMode(mode: string): void; +} diff --git a/src/ts/util/editorCommenEvent.ts b/src/ts/util/editorCommenEvent.ts index 86892c865..c2e6f8825 100644 --- a/src/ts/util/editorCommenEvent.ts +++ b/src/ts/util/editorCommenEvent.ts @@ -192,7 +192,7 @@ export const selectEvent = (vditor: IVditor, editorElement: HTMLElement) => { if (!vditor.options.select) { return; } - editorElement.addEventListener("selectstart", (event: IHTMLInputEvent) => { + editorElement.addEventListener("selectstart", (event: Event) => { editorElement.onmouseup = () => { const element = vditor.currentMode === "wysiwyg" ? vditor.wysiwyg.element : vditor.sv.element; diff --git a/src/ts/wysiwyg/index.ts b/src/ts/wysiwyg/index.ts index 4bc07a130..c3127a62a 100644 --- a/src/ts/wysiwyg/index.ts +++ b/src/ts/wysiwyg/index.ts @@ -296,7 +296,7 @@ class WYSIWYG { input(vditor, range, event); }); - this.element.addEventListener("click", (event: IHTMLInputEvent) => { + this.element.addEventListener("click", (event: MouseEvent & { target: HTMLInputElement }) => { if (event.target.tagName === "INPUT") { if (event.target.checked) { event.target.setAttribute("checked", "checked"); diff --git a/webpack.config.js b/webpack.config.js index 60f8ab253..0c7e95d2e 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -103,7 +103,6 @@ module.exports = [ new CopyPlugin([ {from: 'src/images', to: 'images'}, {from: 'src/js', to: 'js'}, - {from: 'src/assets/index.d.min.ts', to: '.'}, ]), ], }, {