index.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. declare module "*.svg";
  2. declare module "*.png";
  3. declare const Lute: ILute;
  4. interface ILuteRender {
  5. renderLinkDest: (node: {
  6. TokensStr: () => string;
  7. __internal_object__: {
  8. Parent: {
  9. Type: number,
  10. },
  11. }
  12. }, entering: boolean) => [string, number];
  13. }
  14. interface ILute {
  15. WalkStop: number;
  16. New(): ILute;
  17. SetJSRenderers(options?: {
  18. renderers: {
  19. HTML2VditorDOM?: ILuteRender,
  20. HTML2VditorIRDOM?: ILuteRender,
  21. HTML2Md?: ILuteRender,
  22. },
  23. }): void;
  24. SetHeadingAnchor(enable: boolean): void;
  25. SetInlineMathAllowDigitAfterOpenMarker(enable: boolean): void;
  26. SetToC(enable: boolean): void;
  27. SetFootnotes(enable: boolean): void;
  28. SetAutoSpace(enable: boolean): void;
  29. SetChinesePunct(enable: boolean): void;
  30. SetFixTermTypo(enable: boolean): void;
  31. SetEmojiSite(emojiSite: string): void;
  32. PutEmojis(emojis: { [key: string]: string }): void;
  33. GetEmojis(): { [key: string]: string };
  34. FormatMd(markdown: string): string;
  35. // debugger md
  36. RenderEChartsJSON(text: string): string;
  37. // md 转换为 html
  38. Md2HTML(markdown: string): string;
  39. // 粘贴时将 html 转换为 md
  40. HTML2Md(html: string): string;
  41. // wysiwyg 转换为 html
  42. VditorDOM2HTML(vhtml: string): string;
  43. // wysiwyg 输入渲染
  44. SpinVditorDOM(html: string): string;
  45. // 粘贴时将 html 转换为 wysiwyg
  46. HTML2VditorDOM(html: string): string;
  47. // 将 wysiwyg 转换为 md
  48. VditorDOM2Md(html: string): string;
  49. // 将 md 转换为 wysiwyg
  50. Md2VditorDOM(markdown: string): string;
  51. // ir 输入渲染
  52. SpinVditorIRDOM(markdown: string): string;
  53. // ir 获取 md
  54. VditorIRDOM2Md(html: string): string;
  55. // md 转换为 ir
  56. Md2VditorIRDOM(html: string): string;
  57. // 获取 HTML
  58. VditorIRDOM2HTML(html: string): string;
  59. HTML2VditorIRDOM(html: string): string;
  60. }
  61. declare const webkitAudioContext: {
  62. prototype: AudioContext
  63. new(contextOptions?: AudioContextOptions): AudioContext,
  64. };
  65. interface II18nLang {
  66. en_US: string;
  67. ko_KR: string;
  68. zh_CN: string;
  69. }
  70. interface II18n {
  71. en_US: { [key: string]: string };
  72. ko_KR: { [key: string]: string };
  73. zh_CN: { [key: string]: string };
  74. }
  75. interface IClasses {
  76. preview?: string;
  77. }
  78. /** @link https://hacpai.com/article/1549638745630#options-upload */
  79. interface IUpload {
  80. /** 上传 url */
  81. url?: string;
  82. /** 上传文件最大 Byte */
  83. max?: number;
  84. /** 剪切板中包含图片地址时,使用此 url 重新上传 */
  85. linkToImgUrl?: string;
  86. /** CORS 上传验证,头为 X-Upload-Token */
  87. token?: string;
  88. /** 文件上传类型,同 [input accept](https://www.w3schools.com/tags/att_input_accept.asp) */
  89. accept?: string;
  90. /** 跨站点访问控制。默认值: false */
  91. withCredentials?: boolean;
  92. /** 请求头设置 */
  93. headers?: { [key: string]: string };
  94. /** 上传成功回调 */
  95. success?(editor: HTMLPreElement, msg: string): void;
  96. /** 上传失败回调 */
  97. error?(msg: string): void;
  98. /** 文件名安全处理。 默认值: name => name.replace(/\W/g, '') */
  99. filename?(name: string): string;
  100. /** 校验,成功时返回 true 否则返回错误信息 */
  101. validate?(files: File[]): string | boolean;
  102. /** 自定义上传,当发生错误时返回错误信息 */
  103. handler?(files: File[]): string | null;
  104. /** 对服务端返回的数据进行转换,以满足内置的数据结构 */
  105. format?(files: File[], responseText: string): string;
  106. /** 将上传的文件处理后再返回 */
  107. file?(files: File[]): File[];
  108. }
  109. /** @link https://hacpai.com/article/1549638745630#options-toolbar */
  110. interface IMenuItem {
  111. /** 唯一标示 */
  112. name: string;
  113. /** svg 图标 HTML */
  114. icon?: string;
  115. /** 提示 */
  116. tip?: string;
  117. /** 快捷键,支持⌘/ctrl-key 或 ⌘/ctrl-⇧/shif-key 格式的配置,不支持 wysiwyg 模式 */
  118. hotkey?: string;
  119. /** 插入编辑器中的后缀 */
  120. suffix?: string;
  121. /** 插入编辑器中的前缀 */
  122. prefix?: string;
  123. /** 提示位置:ne, nw */
  124. tipPosition?: string;
  125. panelElement?: HTMLElement;
  126. /** 自定义按钮点击时触发的事件 */
  127. click?(status?: boolean): void;
  128. }
  129. interface IPreviewMode {
  130. both: string;
  131. preview: string;
  132. editor: string;
  133. }
  134. /** @link https://hacpai.com/article/1549638745630#options-preview-hljs */
  135. interface IHljs {
  136. /** 是否启用行号。默认值: false */
  137. lineNumber?: boolean;
  138. /** 可选值参见 [Chroma](https://xyproto.github.io/splash/docs/longer/all.html)。 默认值: 'github' */
  139. style?: string;
  140. /** 是否启用代码高亮。默认值: true */
  141. enable?: boolean;
  142. }
  143. /** @link https://hacpai.com/article/1549638745630#options-preview-math */
  144. interface IMath {
  145. /** 内联数学公式起始 $ 后是否允许数字。默认值: false */
  146. inlineDigit: boolean;
  147. /** 使用 MathJax 渲染时传入的宏定义。默认值: {} */
  148. macros: object;
  149. /** 数学公式渲染引擎。默认值: 'KaTeX' */
  150. engine: "KaTeX" | "MathJax";
  151. }
  152. /** @link https://hacpai.com/article/1549638745630#options-preview-markdown */
  153. interface IMarkdownConfig {
  154. /** 自动空格。默认值: false */
  155. autoSpace?: boolean;
  156. /** 自动矫正术语。默认值: false */
  157. fixTermTypo?: boolean;
  158. /** 自动矫正标点。默认值: false */
  159. chinesePunct?: boolean;
  160. /** 插入目录。默认值: false */
  161. toc?: boolean;
  162. /** 脚注。默认值: true */
  163. footnotes?: boolean;
  164. }
  165. /** @link https://hacpai.com/article/1549638745630#options-preview */
  166. interface IPreview {
  167. /** 预览 debounce 毫秒间隔。默认值: 1000 */
  168. delay?: number;
  169. /** 预览区域最大宽度。默认值: 768 */
  170. maxWidth?: number;
  171. /** 显示模式。默认值: 'both' */
  172. mode?: keyof IPreviewMode;
  173. /** md 解析请求 */
  174. url?: string;
  175. /** @link https://hacpai.com/article/1549638745630#options-preview-hljs */
  176. hljs?: IHljs;
  177. /** @link https://hacpai.com/article/1549638745630#options-preview-math */
  178. math?: IMath;
  179. /** @link https://hacpai.com/article/1549638745630#options-preview-markdown */
  180. markdown?: IMarkdownConfig;
  181. /** 预览回调 */
  182. parse?(element: HTMLElement): void;
  183. /** 渲染之前回调 */
  184. transform?(html: string): string;
  185. }
  186. interface IPreviewOptions {
  187. theme?: "classic" | "dark";
  188. customEmoji?: { [key: string]: string };
  189. lang?: (keyof II18nLang);
  190. emojiPath?: string;
  191. hljs?: IHljs;
  192. speech?: {
  193. enable?: boolean,
  194. };
  195. anchor?: boolean;
  196. math?: IMath;
  197. cdn?: string;
  198. markdown?: IMarkdownConfig;
  199. transform?(html: string): string;
  200. }
  201. interface IHintData {
  202. html: string;
  203. value: string;
  204. }
  205. /** @link https://hacpai.com/article/1549638745630#options-hint */
  206. interface IHint {
  207. /** 常用表情提示 HTML */
  208. emojiTail?: string;
  209. /** 提示 debounce 毫秒间隔。默认值: 200 */
  210. delay?: number;
  211. /** 默认表情,可从 [lute/emoji_map](https://github.com/88250/lute/blob/master/parse/emoji_map.go#L32) 中选取,也可自定义 */
  212. emoji?: { [key: string]: string };
  213. /** 表情图片地址。默认值: 'https://cdn.jsdelivr.net/npm/vditor@${VDITOR_VERSION}/dist/images/emoji' */
  214. emojiPath?: string;
  215. /** `@`用户回调 */
  216. at?(value: string): IHintData[];
  217. }
  218. interface IResize {
  219. position?: string;
  220. enable?: boolean;
  221. after?(height: number): void;
  222. }
  223. /** @link https://hacpai.com/article/1549638745630#options */
  224. interface IOptions {
  225. /** 编辑器初始化值。默认值: '' */
  226. value?: string;
  227. /** 是否显示日志。默认值: false */
  228. debugger?: boolean;
  229. /** 编辑器异步渲染完成后的回调方法 */
  230. after?: () => void;
  231. /** 是否启用打字机模式。默认值: false */
  232. typewriterMode?: boolean;
  233. keymap?: { [key: string]: string };
  234. /** 编辑器总高度。默认值: 'auto' */
  235. height?: number | string;
  236. /** 编辑器总宽度,支持 %。默认值: 'auto' */
  237. width?: number | string;
  238. /** 输入区域为空时的提示。默认值: '' */
  239. placeholder?: string;
  240. /** 多语言。默认值: 'zh_CN' */
  241. lang?: (keyof II18nLang);
  242. /** @link https://hacpai.com/article/1549638745630#options-toolbar */
  243. toolbar?: Array<string | IMenuItem>;
  244. resize?: IResize;
  245. /** 计数器。默认值: '0' */
  246. counter?: number;
  247. /**
  248. * 是否使用 localStorage 进行缓存。
  249. *
  250. * 为 string 时使用传入值作为 key 。
  251. *
  252. * 使用元素 id 初始化时:
  253. * true: 启用,`vditor${id}` 作为 key。
  254. * false: 禁用。
  255. * 默认: true
  256. *
  257. * 使用元素初始化时:
  258. * true: 报错,必须使用 string。
  259. * false: 禁用。
  260. * 默认: false
  261. */
  262. cache?: boolean | string;
  263. /** 编辑模式。默认值: 'wysiwyg' */
  264. mode?: "wysiwyg" | "sv" | "ir";
  265. /** @link https://hacpai.com/article/1549638745630#options-preview */
  266. preview?: IPreview;
  267. /** @link https://hacpai.com/article/1549638745630#options-hint */
  268. hint?: IHint;
  269. /** 是否隐藏工具栏。默认值: false */
  270. hideToolbar?: boolean;
  271. /** 主题。默认值: 'classic' */
  272. theme?: "classic" | "dark";
  273. /** @link https://hacpai.com/article/1549638745630#options-upload */
  274. upload?: IUpload;
  275. classes?: IClasses;
  276. /** 配置自建 CDN 地址。默认值: 'https://cdn.jsdelivr.net/npm/vditor@${VDITOR_VERSION}' */
  277. cdn?: string;
  278. /** tab 键操作字符串,支持 \t 及任意字符串 */
  279. tab?: string;
  280. /** 输入后触发 */
  281. input?(value: string, previewElement?: HTMLElement): void;
  282. /** 聚焦后触发 */
  283. focus?(value: string): void;
  284. /** 失焦后触发 */
  285. blur?(value: string): void;
  286. /** `esc` 按下后触发 */
  287. esc?(value: string): void;
  288. /** `⌘/ctrl+enter` 按下后触发 */
  289. ctrlEnter?(value: string): void;
  290. /** 编辑器中选中文字后触发 */
  291. select?(value: string): void;
  292. }
  293. interface IEChart {
  294. setOption(option: any): void;
  295. resize(): void;
  296. }
  297. interface IVditor {
  298. el: HTMLElement;
  299. cacheKey?: string;
  300. options: Omit<IOptions, "cache">;
  301. originalInnerHTML: string;
  302. lute: ILute;
  303. currentMode: "sv" | "wysiwyg" | "ir";
  304. currentPreviewMode: keyof IPreviewMode;
  305. devtools?: {
  306. element: HTMLDivElement,
  307. renderEchart(vditor: IVditor): void,
  308. };
  309. toolbar?: {
  310. elements?: { [key: string]: HTMLElement },
  311. element?: HTMLElement,
  312. editModePanelElement?: HTMLElement;
  313. headingPanelElement?: HTMLElement;
  314. emojiPanelElement?: HTMLElement;
  315. };
  316. preview?: {
  317. element: HTMLElement
  318. render(vditor: IVditor, value?: string): void,
  319. };
  320. sv?: {
  321. element: HTMLPreElement,
  322. };
  323. counter?: {
  324. element: HTMLElement
  325. render(length: number, counter: number): void,
  326. };
  327. resize?: {
  328. element: HTMLElement,
  329. };
  330. hint?: {
  331. timeId: number
  332. element: HTMLDivElement
  333. fillEmoji(element: HTMLElement, vditor: IVditor): void
  334. render(vditor: IVditor): void,
  335. genHTML(data: IHintData[], key: string, vditor: IVditor): void
  336. select(event: KeyboardEvent, vditor: IVditor): boolean
  337. };
  338. tip: {
  339. element: HTMLElement
  340. show(text: string, time?: number): void
  341. hide(): void,
  342. };
  343. upload?: {
  344. element: HTMLElement
  345. isUploading: boolean
  346. range: Range,
  347. };
  348. undo?: {
  349. redo(vditor: IVditor): void
  350. undo(vditor: IVditor): void
  351. addToUndoStack(vditor: IVditor): void
  352. recordFirstPosition(vditor: IVditor): void,
  353. resetIcon(vditor: IVditor): void,
  354. };
  355. wysiwygUndo?: {
  356. redo(vditor: IVditor): void
  357. undo(vditor: IVditor): void
  358. addToUndoStack(vditor: IVditor): void
  359. recordFirstWbr(vditor: IVditor, event: KeyboardEvent): void,
  360. resetIcon(vditor: IVditor): void,
  361. };
  362. irUndo?: {
  363. redo(vditor: IVditor): void
  364. undo(vditor: IVditor): void
  365. addToUndoStack(vditor: IVditor): void
  366. recordFirstWbr(vditor: IVditor, event: KeyboardEvent): void,
  367. resetIcon(vditor: IVditor): void,
  368. };
  369. wysiwyg?: {
  370. element: HTMLPreElement,
  371. popover: HTMLDivElement,
  372. afterRenderTimeoutId: number,
  373. hlToolbarTimeoutId: number,
  374. preventInput: boolean,
  375. composingLock: boolean,
  376. };
  377. ir?: {
  378. element: HTMLPreElement,
  379. composingLock: boolean,
  380. preventInput: boolean,
  381. processTimeoutId: number,
  382. hlToolbarTimeoutId: number,
  383. };
  384. }