@cherry-markdown/cherry-markdown-dev 0.8.58-dev

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (319) hide show
  1. package/package.json +149 -0
  2. package/src/Cherry.config.js +625 -0
  3. package/src/Cherry.js +1104 -0
  4. package/src/CherryStatic.js +70 -0
  5. package/src/Editor.js +748 -0
  6. package/src/Engine.js +381 -0
  7. package/src/Event.js +140 -0
  8. package/src/Factory.js +180 -0
  9. package/src/Logger.js +31 -0
  10. package/src/Previewer.js +1183 -0
  11. package/src/Sanitizer.js +4 -0
  12. package/src/Sanitizer.node.js +7 -0
  13. package/src/UrlCache.js +98 -0
  14. package/src/addons/advance/cherry-table-echarts-plugin.js +170 -0
  15. package/src/addons/cherry-code-block-mermaid-plugin.js +158 -0
  16. package/src/addons/cherry-code-block-plantuml-plugin.js +106 -0
  17. package/src/core/HookCenter.js +297 -0
  18. package/src/core/HooksConfig.js +100 -0
  19. package/src/core/ParagraphBase.js +332 -0
  20. package/src/core/SentenceBase.js +65 -0
  21. package/src/core/SyntaxBase.js +194 -0
  22. package/src/core/hooks/AutoLink.js +232 -0
  23. package/src/core/hooks/BackgroundColor.js +46 -0
  24. package/src/core/hooks/Blockquote.js +70 -0
  25. package/src/core/hooks/Br.js +85 -0
  26. package/src/core/hooks/CodeBlock.js +446 -0
  27. package/src/core/hooks/Color.js +46 -0
  28. package/src/core/hooks/CommentReference.js +96 -0
  29. package/src/core/hooks/Detail.js +108 -0
  30. package/src/core/hooks/Emoji.config.js +1825 -0
  31. package/src/core/hooks/Emoji.js +119 -0
  32. package/src/core/hooks/Emphasis.js +113 -0
  33. package/src/core/hooks/Footnote.js +125 -0
  34. package/src/core/hooks/FrontMatter.js +51 -0
  35. package/src/core/hooks/Header.js +234 -0
  36. package/src/core/hooks/HighLight.js +37 -0
  37. package/src/core/hooks/Hr.js +52 -0
  38. package/src/core/hooks/HtmlBlock.js +184 -0
  39. package/src/core/hooks/Image.js +174 -0
  40. package/src/core/hooks/InlineCode.js +48 -0
  41. package/src/core/hooks/InlineMath.js +107 -0
  42. package/src/core/hooks/Link.js +160 -0
  43. package/src/core/hooks/List.js +264 -0
  44. package/src/core/hooks/MathBlock.js +103 -0
  45. package/src/core/hooks/Panel.js +145 -0
  46. package/src/core/hooks/Paragraph.js +84 -0
  47. package/src/core/hooks/Ruby.js +34 -0
  48. package/src/core/hooks/Size.js +51 -0
  49. package/src/core/hooks/Strikethrough.js +54 -0
  50. package/src/core/hooks/Sub.js +47 -0
  51. package/src/core/hooks/SuggestList.js +333 -0
  52. package/src/core/hooks/Suggester.js +707 -0
  53. package/src/core/hooks/Sup.js +47 -0
  54. package/src/core/hooks/Table.js +275 -0
  55. package/src/core/hooks/Toc.js +292 -0
  56. package/src/core/hooks/Transfer.js +47 -0
  57. package/src/core/hooks/Underline.js +37 -0
  58. package/src/index.core.js +29 -0
  59. package/src/index.engine.core.js +68 -0
  60. package/src/index.engine.js +28 -0
  61. package/src/index.js +32 -0
  62. package/src/libs/mermaidAPI.8.4.8.js +1 -0
  63. package/src/libs/mermaidAPI.8.5.2.js +42 -0
  64. package/src/libs/rawdeflate.js +1663 -0
  65. package/src/locales/en_US.js +139 -0
  66. package/src/locales/index.js +25 -0
  67. package/src/locales/ru_RU.js +139 -0
  68. package/src/locales/zh_CN.js +142 -0
  69. package/src/sass/base.scss +26 -0
  70. package/src/sass/bubble_formula.scss +166 -0
  71. package/src/sass/ch-icon.scss +118 -0
  72. package/src/sass/cherry.scss +1116 -0
  73. package/src/sass/components/bubble.scss +173 -0
  74. package/src/sass/components/shortcut_key_config.scss +108 -0
  75. package/src/sass/formula_utils_bubble.scss +82 -0
  76. package/src/sass/icon_template.scss +24 -0
  77. package/src/sass/icons/uEA03-list.svg +19 -0
  78. package/src/sass/icons/uEA04-check.svg +14 -0
  79. package/src/sass/icons/uEA09-square.svg +10 -0
  80. package/src/sass/icons/uEA0A-bold.svg +20 -0
  81. package/src/sass/icons/uEA0B-code.svg +18 -0
  82. package/src/sass/icons/uEA0C-color.svg +13 -0
  83. package/src/sass/icons/uEA0D-header.svg +8 -0
  84. package/src/sass/icons/uEA0E-image.svg +15 -0
  85. package/src/sass/icons/uEA0F-italic.svg +8 -0
  86. package/src/sass/icons/uEA10-link.svg +16 -0
  87. package/src/sass/icons/uEA11-ol.svg +21 -0
  88. package/src/sass/icons/uEA12-size.svg +11 -0
  89. package/src/sass/icons/uEA13-strike.svg +16 -0
  90. package/src/sass/icons/uEA14-table.svg +12 -0
  91. package/src/sass/icons/uEA15-ul.svg +17 -0
  92. package/src/sass/icons/uEA16-underline.svg +13 -0
  93. package/src/sass/icons/uEA17-word.svg +16 -0
  94. package/src/sass/icons/uEA18-blockquote.svg +11 -0
  95. package/src/sass/icons/uEA19-font.svg +10 -0
  96. package/src/sass/icons/uEA1F-insertClass.svg +39 -0
  97. package/src/sass/icons/uEA20-insertFlow.svg +8 -0
  98. package/src/sass/icons/uEA21-insertFormula.svg +23 -0
  99. package/src/sass/icons/uEA22-insertGantt.svg +13 -0
  100. package/src/sass/icons/uEA23-insertGraph.svg +13 -0
  101. package/src/sass/icons/uEA24-insertPie.svg +19 -0
  102. package/src/sass/icons/uEA25-insertSeq.svg +20 -0
  103. package/src/sass/icons/uEA26-insertState.svg +35 -0
  104. package/src/sass/icons/uEA27-line.svg +11 -0
  105. package/src/sass/icons/uEA28-preview.svg +18 -0
  106. package/src/sass/icons/uEA29-previewClose.svg +24 -0
  107. package/src/sass/icons/uEA2A-toc.svg +24 -0
  108. package/src/sass/icons/uEA2D-sub.svg +15 -0
  109. package/src/sass/icons/uEA2E-sup.svg +15 -0
  110. package/src/sass/icons/uEA2F-h1.svg +16 -0
  111. package/src/sass/icons/uEA30-h2.svg +20 -0
  112. package/src/sass/icons/uEA31-h3.svg +23 -0
  113. package/src/sass/icons/uEA32-h4.svg +16 -0
  114. package/src/sass/icons/uEA33-h5.svg +20 -0
  115. package/src/sass/icons/uEA34-h6.svg +17 -0
  116. package/src/sass/icons/uEA35-video.svg +20 -0
  117. package/src/sass/icons/uEA36-insert.svg +25 -0
  118. package/src/sass/icons/uEA37-little_table.svg +30 -0
  119. package/src/sass/icons/uEA38-pdf.svg +27 -0
  120. package/src/sass/icons/uEA39-checklist.svg +22 -0
  121. package/src/sass/icons/uEA40-close.svg +12 -0
  122. package/src/sass/icons/uEA41-fullscreen.svg +81 -0
  123. package/src/sass/icons/uEA42-minscreen.svg +77 -0
  124. package/src/sass/icons/uEA43-insertChart.svg +23 -0
  125. package/src/sass/icons/uEA44-question.svg +25 -0
  126. package/src/sass/icons/uEA45-settings.svg +32 -0
  127. package/src/sass/icons/uEA46-ok.svg +7 -0
  128. package/src/sass/icons/uEA47-br.svg +22 -0
  129. package/src/sass/icons/uEA48-normal.svg +15 -0
  130. package/src/sass/icons/uEA49-undo.svg +19 -0
  131. package/src/sass/icons/uEA50-redo.svg +21 -0
  132. package/src/sass/icons/uEA51-copy.svg +6 -0
  133. package/src/sass/icons/uEA52-phone.svg +5 -0
  134. package/src/sass/icons/uEA53-cherry-table-delete.svg +17 -0
  135. package/src/sass/icons/uEA54-cherry-table-insert-bottom.svg +16 -0
  136. package/src/sass/icons/uEA55-cherry-table-insert-left.svg +15 -0
  137. package/src/sass/icons/uEA56-cherry-table-insert-right.svg +16 -0
  138. package/src/sass/icons/uEA57-cherry-table-insert-top.svg +16 -0
  139. package/src/sass/icons/uEA58-sort-s.svg +13 -0
  140. package/src/sass/icons/uEA59-pinyin.svg +1 -0
  141. package/src/sass/icons/uEA5A-create.svg +24 -0
  142. package/src/sass/icons/uEA5B-download.svg +34 -0
  143. package/src/sass/icons/uEA5C-edit.svg +3 -0
  144. package/src/sass/icons/uEA5D-export.svg +53 -0
  145. package/src/sass/icons/uEA5E-folder-open.svg +3 -0
  146. package/src/sass/icons/uEA5F-folder.svg +3 -0
  147. package/src/sass/icons/uEA60-help.svg +5 -0
  148. package/src/sass/icons/uEA61-pen-fill.svg +13 -0
  149. package/src/sass/icons/uEA62-pen.svg +3 -0
  150. package/src/sass/icons/uEA64-tips.svg +5 -0
  151. package/src/sass/icons/uEA65-warn.svg +5 -0
  152. package/src/sass/icons/uEA66-mistake.svg +4 -0
  153. package/src/sass/icons/uEA67-success.svg +4 -0
  154. package/src/sass/icons/uEA68-danger.svg +4 -0
  155. package/src/sass/icons/uEA69-info.svg +5 -0
  156. package/src/sass/icons/uEA6A-primary.svg +5 -0
  157. package/src/sass/icons/uEA6B-warning.svg +5 -0
  158. package/src/sass/icons/uEA6C-justify.svg +19 -0
  159. package/src/sass/icons/uEA6D-justifyCenter.svg +19 -0
  160. package/src/sass/icons/uEA6E-justifyLeft.svg +19 -0
  161. package/src/sass/icons/uEA6F-justifyRight.svg +19 -0
  162. package/src/sass/icons/uEA70-chevronsLeft.svg +1 -0
  163. package/src/sass/icons/uEA71-chevronsRight.svg +1 -0
  164. package/src/sass/icons/uEA72-trendingUp.svg +1 -0
  165. package/src/sass/icons/uEA74-codeBlock.svg +1 -0
  166. package/src/sass/icons/uEA75-expand.svg +3 -0
  167. package/src/sass/icons/uEA76-unExpand.svg +3 -0
  168. package/src/sass/icons/uEA77-swap-vert.svg +1 -0
  169. package/src/sass/icons/uEA78-swap.svg +1 -0
  170. package/src/sass/icons/uEA79-keyboard.svg +1 -0
  171. package/src/sass/icons/uEA7A-command.svg +1 -0
  172. package/src/sass/icons/uEA7B-search.svg +1 -0
  173. package/src/sass/index.scss +3 -0
  174. package/src/sass/markdown.scss +668 -0
  175. package/src/sass/markdown_pure.scss +9 -0
  176. package/src/sass/prettyprint/prettyprint.scss +118 -0
  177. package/src/sass/previewer.scss +179 -0
  178. package/src/sass/print.scss +13 -0
  179. package/src/sass/prism/coy.scss +220 -0
  180. package/src/sass/prism/dark.scss +132 -0
  181. package/src/sass/prism/default.scss +143 -0
  182. package/src/sass/prism/funky.scss +133 -0
  183. package/src/sass/prism/okaidia.scss +126 -0
  184. package/src/sass/prism/one-dark.scss +440 -0
  185. package/src/sass/prism/one-light.scss +428 -0
  186. package/src/sass/prism/solarized-light.scss +153 -0
  187. package/src/sass/prism/tomorrow-night.scss +125 -0
  188. package/src/sass/prism/twilight.scss +202 -0
  189. package/src/sass/prism/vs-dark.scss +275 -0
  190. package/src/sass/prism/vs-light.scss +168 -0
  191. package/src/sass/themes/blue.scss +411 -0
  192. package/src/sass/themes/dark.scss +517 -0
  193. package/src/sass/themes/default.scss +255 -0
  194. package/src/sass/themes/green.scss +395 -0
  195. package/src/sass/themes/light.scss +368 -0
  196. package/src/sass/themes/red.scss +397 -0
  197. package/src/sass/themes/violet.scss +410 -0
  198. package/src/sass/variable.scss +84 -0
  199. package/src/toolbars/Bubble.js +234 -0
  200. package/src/toolbars/BubbleFormula.js +298 -0
  201. package/src/toolbars/BubbleTable.js +147 -0
  202. package/src/toolbars/FloatMenu.js +131 -0
  203. package/src/toolbars/HiddenToolbar.js +36 -0
  204. package/src/toolbars/HookCenter.js +234 -0
  205. package/src/toolbars/MenuBase.js +569 -0
  206. package/src/toolbars/PreviewerBubble.js +608 -0
  207. package/src/toolbars/ShortcutKeyConfigPanel.js +345 -0
  208. package/src/toolbars/Sidebar.js +36 -0
  209. package/src/toolbars/Toc.js +242 -0
  210. package/src/toolbars/Toolbar.js +449 -0
  211. package/src/toolbars/ToolbarRight.js +37 -0
  212. package/src/toolbars/hooks/Audio.js +79 -0
  213. package/src/toolbars/hooks/BarTable.js +41 -0
  214. package/src/toolbars/hooks/Bold.js +73 -0
  215. package/src/toolbars/hooks/Br.js +34 -0
  216. package/src/toolbars/hooks/ChangeLocale.js +62 -0
  217. package/src/toolbars/hooks/ChatGpt.js +182 -0
  218. package/src/toolbars/hooks/CheckList.js +41 -0
  219. package/src/toolbars/hooks/Code.js +49 -0
  220. package/src/toolbars/hooks/CodeTheme.js +66 -0
  221. package/src/toolbars/hooks/Color.js +298 -0
  222. package/src/toolbars/hooks/Copy.js +141 -0
  223. package/src/toolbars/hooks/Detail.js +69 -0
  224. package/src/toolbars/hooks/DrawIo.js +57 -0
  225. package/src/toolbars/hooks/Export.js +49 -0
  226. package/src/toolbars/hooks/File.js +79 -0
  227. package/src/toolbars/hooks/Formula.js +69 -0
  228. package/src/toolbars/hooks/FullScreen.js +50 -0
  229. package/src/toolbars/hooks/Graph.js +263 -0
  230. package/src/toolbars/hooks/H1.js +71 -0
  231. package/src/toolbars/hooks/H2.js +71 -0
  232. package/src/toolbars/hooks/H3.js +71 -0
  233. package/src/toolbars/hooks/Header.js +118 -0
  234. package/src/toolbars/hooks/Hr.js +35 -0
  235. package/src/toolbars/hooks/Image.js +91 -0
  236. package/src/toolbars/hooks/InlineCode.js +53 -0
  237. package/src/toolbars/hooks/Insert.js +193 -0
  238. package/src/toolbars/hooks/Italic.js +72 -0
  239. package/src/toolbars/hooks/Justify.js +49 -0
  240. package/src/toolbars/hooks/LineTable.js +41 -0
  241. package/src/toolbars/hooks/Link.js +49 -0
  242. package/src/toolbars/hooks/List.js +55 -0
  243. package/src/toolbars/hooks/MobilePreview.js +44 -0
  244. package/src/toolbars/hooks/Ol.js +41 -0
  245. package/src/toolbars/hooks/Panel.js +140 -0
  246. package/src/toolbars/hooks/Pdf.js +78 -0
  247. package/src/toolbars/hooks/Publish.js +123 -0
  248. package/src/toolbars/hooks/QuickTable.js +43 -0
  249. package/src/toolbars/hooks/Quote.js +45 -0
  250. package/src/toolbars/hooks/Redo.js +33 -0
  251. package/src/toolbars/hooks/Ruby.js +59 -0
  252. package/src/toolbars/hooks/Search.js +53 -0
  253. package/src/toolbars/hooks/Settings.js +220 -0
  254. package/src/toolbars/hooks/ShortcutKey.js +62 -0
  255. package/src/toolbars/hooks/Size.js +118 -0
  256. package/src/toolbars/hooks/Split.js +37 -0
  257. package/src/toolbars/hooks/Strikethrough.js +71 -0
  258. package/src/toolbars/hooks/Sub.js +58 -0
  259. package/src/toolbars/hooks/Sup.js +58 -0
  260. package/src/toolbars/hooks/SwitchModel.js +56 -0
  261. package/src/toolbars/hooks/Table.js +56 -0
  262. package/src/toolbars/hooks/Theme.js +62 -0
  263. package/src/toolbars/hooks/Toc.js +35 -0
  264. package/src/toolbars/hooks/TogglePreview.js +91 -0
  265. package/src/toolbars/hooks/Ul.js +41 -0
  266. package/src/toolbars/hooks/Underline.js +68 -0
  267. package/src/toolbars/hooks/Undo.js +30 -0
  268. package/src/toolbars/hooks/Video.js +79 -0
  269. package/src/toolbars/hooks/Word.js +78 -0
  270. package/src/toolbars/hooks/WordCount.js +106 -0
  271. package/src/utils/autoindent.js +58 -0
  272. package/src/utils/cm-search-replace.js +794 -0
  273. package/src/utils/code-preview-language-setting.js +180 -0
  274. package/src/utils/codeBlockContentHandler.js +400 -0
  275. package/src/utils/config.js +174 -0
  276. package/src/utils/copy.js +55 -0
  277. package/src/utils/dialog.js +214 -0
  278. package/src/utils/dom.js +163 -0
  279. package/src/utils/downloadUtil.js +23 -0
  280. package/src/utils/env.js +22 -0
  281. package/src/utils/error.js +61 -0
  282. package/src/utils/event.js +38 -0
  283. package/src/utils/export.js +166 -0
  284. package/src/utils/file.js +164 -0
  285. package/src/utils/formulaUtilsHandler.js +232 -0
  286. package/src/utils/htmlparser.js +976 -0
  287. package/src/utils/image.js +99 -0
  288. package/src/utils/imgSizeHandler.js +279 -0
  289. package/src/utils/lazyLoadImg.js +327 -0
  290. package/src/utils/lineFeed.js +49 -0
  291. package/src/utils/listContentHandler.js +227 -0
  292. package/src/utils/lookbehind-replace.js +81 -0
  293. package/src/utils/mathjax.js +89 -0
  294. package/src/utils/myersDiff.js +211 -0
  295. package/src/utils/pasteHelper.js +253 -0
  296. package/src/utils/platformTransform.js +71 -0
  297. package/src/utils/recount-pos.js +59 -0
  298. package/src/utils/regexp.js +295 -0
  299. package/src/utils/sanitize.js +477 -0
  300. package/src/utils/selection.js +50 -0
  301. package/src/utils/shortcutKey.js +291 -0
  302. package/src/utils/svgUtils.js +96 -0
  303. package/src/utils/tableContentHandler.js +876 -0
  304. package/test/core/CommonMark.spec.ts +62 -0
  305. package/test/core/hooks/AutoLink.spec.ts +28 -0
  306. package/test/core/hooks/List.spec.ts +79 -0
  307. package/test/core/hooks/__snapshots__/List.spec.ts.snap +11 -0
  308. package/test/example.md +778 -0
  309. package/test/node.js +10 -0
  310. package/test/suites/commonmark.spec.json +5218 -0
  311. package/test/tsconfig.test.json +6 -0
  312. package/test/utils/regexp.spec.ts +28 -0
  313. package/types/cherry.d.ts +675 -0
  314. package/types/codemirror.d.ts +22 -0
  315. package/types/editor.d.ts +72 -0
  316. package/types/global.d.ts +16 -0
  317. package/types/menus.d.ts +24 -0
  318. package/types/previewer.d.ts +53 -0
  319. package/types/syntax.d.ts +52 -0
@@ -0,0 +1,449 @@
1
+ /**
2
+ * Copyright (C) 2021 THL A29 Limited, a Tencent company.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import HookCenter from './HookCenter';
17
+ import { createElement } from '@/utils/dom';
18
+ import Logger from '@/Logger';
19
+ import {
20
+ getAllowedShortcutKey,
21
+ getStorageKeyMap,
22
+ keyStack2UniqueString,
23
+ storageKeyMap,
24
+ isEnableShortcutKey,
25
+ } from '@/utils/shortcutKey';
26
+
27
+ /**
28
+ * @typedef {()=>void} Bold 向cherry编辑器中插入粗体语法
29
+ * @typedef {()=>void} Italic 向cherry编辑器中插入斜体语法
30
+ * @typedef {(level:1|2|3|4|5|'1'|'2'|'3'|'4'|'5')=>void} Header 向cherry编辑器中插入标题语法
31
+ * - level 标题等级 1~5
32
+ * @typedef {()=>void} Strikethrough 向cherry编辑器中插入删除线语法
33
+ * @typedef {(type:'ol'|'ul'|'checklist'|1|2|3|'1'|'2'|'3')=>void} List 向cherry编辑器中插入有序、无序列表或者checklist语法
34
+ * - ol(1)有序
35
+ * - ul(2)无序列表
36
+ * - checklist(3)checklist
37
+ * @typedef {`normal-table-${number}*${number}`} normalTableRowCol 插入表格语法约束
38
+ * @typedef {(insert:'hr'|'br'|'code'|'formula'|'checklist'|'toc'|'link'|'image'|'video'|'audio'|'normal-table'|normalTableRowCol)=>void} Insert 向cherry编辑器中插入特定语法(需要在`toolbar`中预先配置功能)
39
+ * - hr 水平分割线
40
+ * - br 换行
41
+ * - code 代码块
42
+ * - formula 公式
43
+ * - checklist 检查项
44
+ * - toc 目录
45
+ * - link 链接
46
+ * - image 图片
47
+ * - video 视频
48
+ * - audio 音频
49
+ * - normal-table 插入3行5列的表格
50
+ * - normal-table-row*col 如normal-table-2*4插入2行(包含表头是3行)4列的表格
51
+ * @typedef {(type:'1'|'2'|'3'|'4'|'5'|'6'|1|2|3|4|5|6|'flow'|'sequence'|'state'|'class'|'pie'|'gantt')=>void} Graph 向cherry编辑器中插入画图语法
52
+ * - flow(1) 流程图
53
+ * - sequence(2) 时序图
54
+ * - state(3)状态图
55
+ * - class(4)类图
56
+ * - pie(5)饼图
57
+ * - gantt(6)甘特图
58
+ */
59
+
60
+ export default class Toolbar {
61
+ /**
62
+ * @typedef {{
63
+ * bold?:Bold;
64
+ * italic?:Italic;
65
+ * header?:Header;
66
+ * strikethrough?:Strikethrough;
67
+ * list?:List;
68
+ * insert?:Insert;
69
+ * graph?:Graph;
70
+ * [key:string]:any;
71
+ * }} ToolbarHandlers
72
+ * @type ToolbarHandlers 外部获取 toolbarHandlers 的部分功能
73
+ */
74
+ toolbarHandlers = {};
75
+
76
+ constructor(options) {
77
+ // 存储所有菜单的实例
78
+ this.menus = {};
79
+ // 存储所有快捷键的影射 {快捷键: 菜单名称}
80
+ this.shortcutKeyMap = {};
81
+ // 存储所有二级菜单面板
82
+ this.subMenus = {};
83
+ this.currentActiveSubMenu = null;
84
+ // 默认的菜单配置
85
+ this.options = {
86
+ dom: document.createElement('div'),
87
+ buttonConfig: ['bold'],
88
+ customMenu: [],
89
+ };
90
+
91
+ Object.assign(this.options, options);
92
+ this.$cherry = this.options.$cherry;
93
+ this.instanceId = this.$cherry.instanceId;
94
+ this.menus = new HookCenter(this);
95
+ this.drawMenus();
96
+ this.collectShortcutKey();
97
+ this.collectToolbarHandler();
98
+ this.init();
99
+ }
100
+
101
+ init() {
102
+ this.$cherry.$event.on('cleanAllSubMenus', () => this.hideAllSubMenu());
103
+ }
104
+
105
+ previewOnly() {
106
+ this.options.dom.classList.add('preview-only');
107
+ this.$cherry.wrapperDom.classList.add('cherry--no-toolbar');
108
+ this.$cherry.$event.emit('toolbarHide');
109
+ }
110
+
111
+ showToolbar() {
112
+ this.options.dom.classList.remove('preview-only');
113
+ this.$cherry.wrapperDom.classList.remove('cherry--no-toolbar');
114
+ this.$cherry.$event.emit('toolbarShow');
115
+ }
116
+
117
+ isHasLevel2Menu(name) {
118
+ // FIXME: return boolean
119
+ return this.menus.level2MenusName[name];
120
+ }
121
+
122
+ isHasConfigMenu(name) {
123
+ // FIXME: return boolean
124
+ return this.menus.hooks[name].subMenuConfig || [];
125
+ }
126
+
127
+ /**
128
+ * 判断是否有子菜单,目前有两种子菜单配置方式:1、通过`subMenuConfig`属性 2、通过`buttonConfig`配置属性
129
+ * @param {string} name
130
+ * @returns {boolean} 是否有子菜单
131
+ */
132
+ isHasSubMenu(name) {
133
+ return Boolean(this.isHasLevel2Menu(name) || this.isHasConfigMenu(name).length > 0);
134
+ }
135
+
136
+ /**
137
+ * 根据配置画出来一级工具栏
138
+ */
139
+ drawMenus() {
140
+ const fragLeft = document.createDocumentFragment();
141
+
142
+ this.menus.level1MenusName.forEach((name) => {
143
+ const btn = this.menus.hooks[name].createBtn();
144
+ if (typeof window === 'object' && 'onpointerup' in window) {
145
+ // 只有先down再up的才触发click逻辑,避免误触(尤其是float menu的场景)
146
+ btn.addEventListener(
147
+ 'pointerdown',
148
+ () => {
149
+ this.isPointerDown = true;
150
+ },
151
+ false,
152
+ );
153
+ btn.addEventListener(
154
+ 'pointerup',
155
+ (event) => {
156
+ this.isPointerDown && this.onClick(event, name);
157
+ this.isPointerDown = false;
158
+ },
159
+ false,
160
+ );
161
+ } else {
162
+ // vscode 插件里不支持 pointer event
163
+ btn.addEventListener(
164
+ 'click',
165
+ (event) => {
166
+ this.onClick(event, name);
167
+ },
168
+ false,
169
+ );
170
+ }
171
+ if (this.isHasSubMenu(name)) {
172
+ btn.classList.add('cherry-toolbar-dropdown');
173
+ }
174
+ fragLeft.appendChild(btn);
175
+ });
176
+
177
+ this.appendMenusToDom(fragLeft);
178
+ }
179
+
180
+ appendMenusToDom(menus) {
181
+ const toolbarLeft = createElement('div', 'toolbar-left');
182
+ toolbarLeft.appendChild(menus);
183
+ this.options.dom.appendChild(toolbarLeft);
184
+ }
185
+
186
+ setSubMenuPosition(menuObj, subMenuObj) {
187
+ const pos = menuObj.getMenuPosition();
188
+ subMenuObj.style.left = `${pos.left + pos.width / 2}px`;
189
+ subMenuObj.style.top = `${pos.top + pos.height}px`;
190
+ subMenuObj.style.position = menuObj.positionModel;
191
+ }
192
+
193
+ drawSubMenus(name) {
194
+ this.subMenus[name] = createElement('div', 'cherry-dropdown', { name });
195
+ this.setSubMenuPosition(this.menus.hooks[name], this.subMenus[name]);
196
+ // 如果有配置的二级菜单
197
+ const level2MenusName = this.isHasLevel2Menu(name);
198
+ if (level2MenusName) {
199
+ level2MenusName.forEach((level2Name) => {
200
+ const subMenu = this.menus.hooks[level2Name];
201
+ if (subMenu !== undefined && typeof subMenu.createBtn === 'function') {
202
+ const btn = subMenu.createBtn(true);
203
+ // 二级菜单的dom认定为一级菜单的
204
+ subMenu.dom = subMenu.dom ? subMenu.dom : this.menus.hooks[name].dom;
205
+ btn.addEventListener('click', (event) => this.onClick(event, level2Name, true), false);
206
+ this.subMenus[name].appendChild(btn);
207
+ }
208
+ });
209
+ }
210
+ // 兼容旧版本配置的二级菜单
211
+ const subMenuConfig = this.isHasConfigMenu(name);
212
+ if (subMenuConfig.length > 0) {
213
+ subMenuConfig.forEach((config) => {
214
+ const btn = this.menus.hooks[name].createSubBtnByConfig(config);
215
+ if (!config?.disabledHideAllSubMenu) {
216
+ btn.addEventListener('click', () => this.hideAllSubMenu(), false);
217
+ }
218
+ this.subMenus[name].appendChild(btn);
219
+ });
220
+ }
221
+ this.$cherry.wrapperDom.appendChild(this.subMenus[name]);
222
+ }
223
+
224
+ /**
225
+ * 处理点击事件
226
+ */
227
+ onClick(event, name, focusEvent = false) {
228
+ const menu = this.menus.hooks[name];
229
+ if (!menu) {
230
+ return;
231
+ }
232
+ if (this.isHasSubMenu(name) && !focusEvent) {
233
+ this.toggleSubMenu(name);
234
+ } else {
235
+ /**
236
+ * 如果定义了hideOtherSubMenu,则隐藏其他二级菜单,但不隐藏自己(因为其二级菜单是自己实现的独立逻辑)
237
+ * 比如:颜色选择器、快捷键配置
238
+ */
239
+ // @ts-ignore
240
+ if (typeof menu.hideOtherSubMenu === 'function') {
241
+ // @ts-ignore
242
+ menu.hideOtherSubMenu(() => this.hideAllSubMenu());
243
+ } else {
244
+ this.hideAllSubMenu();
245
+ }
246
+ menu.fire(event, name);
247
+ }
248
+ }
249
+
250
+ /**
251
+ * 激活二级菜单添加选中颜色
252
+ * @param {string} name
253
+ */
254
+ activeSubMenuItem(name) {
255
+ const subMenu = this.subMenus[name];
256
+ const index = this.menus.hooks?.[name]?.getActiveSubMenuIndex(subMenu);
257
+ subMenu?.querySelectorAll('.cherry-dropdown-item').forEach((item, i) => {
258
+ item.classList.toggle('cherry-dropdown-item__selected', i === index);
259
+ });
260
+ }
261
+ updateSubMenuPosition() {
262
+ if (this.currentActiveSubMenu && this.subMenus[this.currentActiveSubMenu]) {
263
+ this.setSubMenuPosition(this.menus.hooks[this.currentActiveSubMenu], this.subMenus[this.currentActiveSubMenu]);
264
+ }
265
+ }
266
+
267
+ /**
268
+ * 展开/收起二级菜单
269
+ */
270
+ toggleSubMenu(name) {
271
+ if (!this.subMenus[name]) {
272
+ // 如果没有二级菜单,则先画出来,然后再显示
273
+ this.hideAllSubMenu();
274
+ this.drawSubMenus(name);
275
+ this.subMenus[name].style.display = 'block';
276
+ this.activeSubMenuItem(name);
277
+ this.currentActiveSubMenu = name;
278
+ return;
279
+ }
280
+ if (this.subMenus[name].style.display === 'none') {
281
+ // 如果是隐藏的,则先隐藏所有二级菜单,再显示当前二级菜单
282
+ this.hideAllSubMenu();
283
+ this.subMenus[name].style.display = 'block';
284
+ this.setSubMenuPosition(this.menus.hooks[name], this.subMenus[name]);
285
+ this.activeSubMenuItem(name);
286
+ this.currentActiveSubMenu = name;
287
+ } else {
288
+ // 如果是显示的,则隐藏当前二级菜单
289
+ this.subMenus[name].style.display = 'none';
290
+ this.currentActiveSubMenu = null;
291
+ }
292
+ }
293
+
294
+ /**
295
+ * 隐藏所有的二级菜单
296
+ */
297
+ hideAllSubMenu() {
298
+ this.currentActiveSubMenu = null;
299
+ this.$cherry.wrapperDom.querySelectorAll('.cherry-dropdown').forEach((dom) => {
300
+ dom.style.display = 'none';
301
+ });
302
+ }
303
+
304
+ /**
305
+ * 收集工具栏的各项信息,主要有:
306
+ * this.toolbarHandlers
307
+ * this.menus.hooks
308
+ * this.shortcutKeyMap
309
+ * @param {Toolbar} toolbarObj 工具栏对象
310
+ */
311
+ collectMenuInfo(toolbarObj) {
312
+ this.toolbarHandlers = Object.assign({}, this.toolbarHandlers, toolbarObj.toolbarHandlers);
313
+ this.menus.hooks = Object.assign({}, toolbarObj.menus.hooks, this.menus.hooks);
314
+ // 只有没设置自定义快捷键的时候才需要收集其他toolbar对象的快捷键配置
315
+ if (!this.options.shortcutKey || Object.keys(this.options.shortcutKey).length <= 0) {
316
+ this.shortcutKeyMap = Object.assign({}, this.shortcutKeyMap, toolbarObj.shortcutKeyMap);
317
+ }
318
+ }
319
+
320
+ /**
321
+ * 收集快捷键
322
+ * @param {boolean} useUserSettings 是否使用用户配置的快捷键
323
+ */
324
+ collectShortcutKey(useUserSettings = true) {
325
+ // 兼容旧版本配置
326
+ if (
327
+ this.$cherry.options.toolbars.shortcutKey &&
328
+ Object.keys(this.$cherry.options.toolbars.shortcutKey).length > 0
329
+ ) {
330
+ Object.entries(this.$cherry.options.toolbars.shortcutKey).forEach(([key, value]) => {
331
+ const $key = key
332
+ .replace(/Ctrl-/g, 'Control-')
333
+ .replace(/-([A-Za-z])$/g, (w, m1) => {
334
+ return `-Key${m1.toUpperCase()}`;
335
+ })
336
+ .replace(/-([0-9])$/g, '-Digit$1');
337
+ this.shortcutKeyMap[$key] = { hookName: value, aliasName: this.$cherry.locale[value] || value };
338
+ });
339
+ }
340
+ if (this.$cherry.options.toolbars.shortcutKeySettings.isReplace) {
341
+ this.shortcutKeyMap = this.$cherry.options.toolbars.shortcutKeySettings.shortcutKeyMap;
342
+ } else {
343
+ this.menus.allMenusName.forEach((name) => {
344
+ this.menus.hooks[name].shortcutKeys?.forEach((key) => {
345
+ this.shortcutKeyMap[key] = name;
346
+ });
347
+ if (typeof this.menus.hooks[name].shortcutKeyMap === 'object' && this.menus.hooks[name].shortcutKeyMap) {
348
+ Object.entries(this.menus.hooks[name].shortcutKeyMap).forEach(([key, value]) => {
349
+ if (key in this.shortcutKeyMap) {
350
+ console.error(`The shortcut key ${key} is already registered`);
351
+ return;
352
+ }
353
+ this.shortcutKeyMap[key] = value;
354
+ });
355
+ }
356
+ });
357
+ Object.entries(this.$cherry.options.toolbars.shortcutKeySettings.shortcutKeyMap).forEach(([key, value]) => {
358
+ this.shortcutKeyMap[key] = value;
359
+ });
360
+ if (!useUserSettings) {
361
+ return;
362
+ }
363
+ // 本地缓存的快捷键配置优先级最高,按 hookName-aliasName 替换相同的快捷键
364
+ const cachedMap = getStorageKeyMap(this.$cherry.nameSpace);
365
+ if (cachedMap) {
366
+ const nameKeyMap = {};
367
+ Object.entries(this.shortcutKeyMap).forEach(([key, value]) => {
368
+ nameKeyMap[`${value.hookName}-${value.aliasName}`] = key;
369
+ });
370
+ Object.entries(cachedMap).forEach(([key, value]) => {
371
+ const testKey = `${value.hookName}-${value.aliasName}`;
372
+ if (nameKeyMap[testKey]) {
373
+ delete this.shortcutKeyMap[nameKeyMap[testKey]];
374
+ }
375
+ this.shortcutKeyMap[key] = value;
376
+ });
377
+ }
378
+ }
379
+ }
380
+
381
+ /**
382
+ * 更新快捷键映射
383
+ * @param {string} oldShortcutKey 旧的快捷键
384
+ * @param {string} newShortcutKey 新的快捷键
385
+ */
386
+ updateShortcutKeyMap(oldShortcutKey, newShortcutKey) {
387
+ if (oldShortcutKey === newShortcutKey) {
388
+ return false;
389
+ }
390
+ const old = this.shortcutKeyMap[oldShortcutKey];
391
+ if (!old) {
392
+ return false;
393
+ }
394
+ // 删除旧值
395
+ delete this.shortcutKeyMap[oldShortcutKey];
396
+ // 更新内存中的映射
397
+ this.shortcutKeyMap[newShortcutKey] = old;
398
+ // 更新缓存中的映射
399
+ storageKeyMap(this.$cherry.nameSpace, this.shortcutKeyMap);
400
+ }
401
+
402
+ collectToolbarHandler() {
403
+ this.toolbarHandlers = this.menus.allMenusName.reduce((handlerMap, name) => {
404
+ const menuHook = this.menus.hooks[name];
405
+ if (!menuHook) {
406
+ return handlerMap;
407
+ }
408
+ handlerMap[name] = (shortcut, _callback) => {
409
+ if (typeof _callback === 'function') {
410
+ Logger.warn(
411
+ 'MenuBase#onClick param callback is no longer supported. Please register the callback via MenuBase#registerAfterClickCb instead.',
412
+ );
413
+ }
414
+ menuHook.fire.call(menuHook, undefined, shortcut);
415
+ };
416
+ return handlerMap;
417
+ }, {});
418
+ }
419
+
420
+ /**
421
+ * 监测是否有对应的快捷键
422
+ * @param {KeyboardEvent} evt keydown 事件
423
+ * @returns {boolean} 是否有对应的快捷键
424
+ */
425
+ matchShortcutKey(evt) {
426
+ const onKeyStack = getAllowedShortcutKey(evt);
427
+ const shortcutKey = keyStack2UniqueString(onKeyStack);
428
+ return !!this.shortcutKeyMap?.[shortcutKey];
429
+ }
430
+
431
+ /**
432
+ * 触发对应快捷键的事件
433
+ * @param {KeyboardEvent} evt
434
+ * @returns {boolean} 是否需要阻塞后续事件,true: 阻塞;false: 不阻塞
435
+ */
436
+ fireShortcutKey(evt) {
437
+ // 如果禁用了快捷键,则不再触发快捷键事件
438
+ if (!isEnableShortcutKey(this.$cherry.nameSpace)) {
439
+ return false;
440
+ }
441
+ const onKeyStack = getAllowedShortcutKey(evt);
442
+ const currentKey = keyStack2UniqueString(onKeyStack);
443
+ const keyMap = this.shortcutKeyMap[currentKey]?.hookName;
444
+ if (typeof keyMap === 'string' && keyMap) {
445
+ this.menus.hooks[keyMap]?.fire(evt, currentKey);
446
+ }
447
+ return true;
448
+ }
449
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Copyright (C) 2021 THL A29 Limited, a Tencent company.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import Toolbar from './Toolbar';
17
+ import { createElement } from '@/utils/dom';
18
+ /**
19
+ * 在编辑区域选中文本时浮现的bubble工具栏
20
+ */
21
+ export default class ToolbarRight extends Toolbar {
22
+ /**
23
+ * 根据配置画出来一级工具栏
24
+ */
25
+ appendMenusToDom(menus) {
26
+ const toolbarLeft = createElement('div', 'toolbar-right');
27
+ toolbarLeft.appendChild(menus);
28
+ this.options.dom.appendChild(toolbarLeft);
29
+ }
30
+
31
+ init() {
32
+ super.init();
33
+ Object.entries(this.shortcutKeyMap).forEach(([key, value]) => {
34
+ this.$cherry.toolbar.shortcutKeyMap[key] = value;
35
+ });
36
+ }
37
+ }
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Copyright (C) 2021 THL A29 Limited, a Tencent company.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import MenuBase from '@/toolbars/MenuBase';
17
+ import { handleUpload, handleParams, handleUploadMulti } from '@/utils/file';
18
+ /**
19
+ * 插入音频
20
+ */
21
+ export default class Audio extends MenuBase {
22
+ constructor($cherry) {
23
+ super($cherry);
24
+ this.setName('audio', 'video');
25
+ }
26
+
27
+ /**
28
+ * 响应点击事件
29
+ * @param {string} selection 被用户选中的文本内容
30
+ * @returns {string} 回填到编辑器光标位置/选中文本区域的内容
31
+ */
32
+ onClick(selection, shortKey = '') {
33
+ const accept = this.$cherry.options?.fileTypeLimitMap?.audio ?? '*';
34
+ const multiple = this.$cherry?.options.multipleFileSelection?.audio ?? false;
35
+ if (multiple) {
36
+ if (this.hasCacheOnce()) {
37
+ // @ts-ignore
38
+ const arr = this.getAndCleanCacheOnce();
39
+ let res = '';
40
+ // @ts-ignore
41
+ for (const { url, params } of arr) {
42
+ const begin = '!audio[';
43
+ const end = `](${url})`;
44
+ this.registerAfterClickCb(() => {
45
+ this.setLessSelection(begin, end);
46
+ });
47
+ const finalName = params.name ? params.name : name;
48
+ res += `${begin}${finalName}${handleParams(params)}${end}\n`;
49
+ }
50
+ return res;
51
+ }
52
+ // 插入图片,调用上传文件逻辑
53
+ handleUploadMulti(this.editor, 'audio', accept, (arr) => {
54
+ this.setCacheOnce(arr);
55
+ this.fire(null);
56
+ });
57
+ this.updateMarkdown = false;
58
+ return selection;
59
+ }
60
+ if (this.hasCacheOnce()) {
61
+ // @ts-ignore
62
+ const { name, url, params } = this.getAndCleanCacheOnce();
63
+ const begin = '!audio[';
64
+ const end = `](${url})`;
65
+ this.registerAfterClickCb(() => {
66
+ this.setLessSelection(begin, end);
67
+ });
68
+ const finalName = params.name ? params.name : name;
69
+ return `${begin}${finalName}${handleParams(params)}${end}`;
70
+ }
71
+ // 插入图片,调用上传文件逻辑
72
+ handleUpload(this.editor, 'audio', accept, (name, url, params) => {
73
+ this.setCacheOnce({ name, url, params });
74
+ this.fire(null);
75
+ });
76
+ this.updateMarkdown = false;
77
+ return selection;
78
+ }
79
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Copyright (C) 2021 THL A29 Limited, a Tencent company.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import MenuBase from '@/toolbars/MenuBase';
17
+ /**
18
+ * 插入柱状图图+表格
19
+ */
20
+ export default class BrTable extends MenuBase {
21
+ constructor($cherry) {
22
+ super($cherry);
23
+ this.setName('brTable', 'table');
24
+ }
25
+
26
+ /**
27
+ * 响应点击事件
28
+ * @param {string} selection 被用户选中的文本内容
29
+ * @returns {string} 回填到编辑器光标位置/选中文本区域的内容
30
+ */
31
+ onClick(selection, shortKey = '') {
32
+ // 插入带折线图的表格
33
+ return `${selection}\n\n${[
34
+ '| :bar: {x,y} | a | b | c |',
35
+ '| :-: | :-: | :-: | :-: |',
36
+ '| x | 1 | 2 | 3 |',
37
+ '| y | 2 | 4 | 6 |',
38
+ '| z | 7 | 5 | 3 |',
39
+ ].join('\n')}\n\n`;
40
+ }
41
+ }
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Copyright (C) 2021 THL A29 Limited, a Tencent company.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import MenuBase from '@/toolbars/MenuBase';
17
+ import { CONTROL_KEY, getKeyCode } from '@/utils/shortcutKey';
18
+ /**
19
+ * 加粗按钮
20
+ */
21
+ export default class Bold extends MenuBase {
22
+ /**
23
+ * @param {import('@/toolbars/MenuBase').MenuBaseConstructorParams} $cherry
24
+ */
25
+ constructor($cherry) {
26
+ super($cherry);
27
+ this.setName('bold', 'bold');
28
+ this.shortcutKeyMap = {
29
+ [`${CONTROL_KEY}-${getKeyCode('b')}`]: {
30
+ hookName: this.name,
31
+ aliasName: $cherry.locale[this.name],
32
+ },
33
+ };
34
+ }
35
+
36
+ /**
37
+ * 是不是包含加粗语法
38
+ * @param {String} selection
39
+ * @returns {Boolean}
40
+ */
41
+ $testIsBold(selection) {
42
+ return /^\s*(\*\*|__)[\s\S]+(\1)/.test(selection);
43
+ }
44
+
45
+ /**
46
+ * 响应点击事件
47
+ * @param {string} selection 被用户选中的文本内容
48
+ * @param {string} shortKey 快捷键参数,本函数不处理这个参数
49
+ * @returns {string} 回填到编辑器光标位置/选中文本区域的内容
50
+ */
51
+ onClick(selection, shortKey = '') {
52
+ let $selection = this.getSelection(selection) || this.locale.bold;
53
+ // 如果是单选,并且选中内容的开始结束内没有加粗语法,则扩大选中范围
54
+ if (!this.isSelections && !this.$testIsBold($selection)) {
55
+ this.getMoreSelection('**', '**', () => {
56
+ const newSelection = this.editor.editor.getSelection();
57
+ const isBold = this.$testIsBold(newSelection);
58
+ if (isBold) {
59
+ $selection = newSelection;
60
+ }
61
+ return isBold;
62
+ });
63
+ }
64
+ // 如果选中的文本中已经有加粗语法了,则去掉加粗语法
65
+ if (this.$testIsBold($selection)) {
66
+ return $selection.replace(/(^)(\s*)(\*\*|__)([^\n]+)(\3)(\s*)($)/gm, '$1$4$7');
67
+ }
68
+ this.registerAfterClickCb(() => {
69
+ this.setLessSelection('**', '**');
70
+ });
71
+ return $selection.replace(/(^)([^\n]+)($)/gm, '$1**$2**$3');
72
+ }
73
+ }