cherry-muse 1.0.1

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 (226) hide show
  1. package/LICENSE +162 -0
  2. package/README.md +139 -0
  3. package/dist/addons/cherry-code-block-card-plugin.js +1 -0
  4. package/dist/addons/cherry-code-block-echarts-plugin.js +1 -0
  5. package/dist/addons/cherry-code-block-mermaid-plugin.js +1 -0
  6. package/dist/cherry-markdown.core.common.js +1 -0
  7. package/dist/cherry-markdown.core.js +1 -0
  8. package/dist/cherry-markdown.css +4089 -0
  9. package/dist/cherry-markdown.engine.core.common.js +1 -0
  10. package/dist/cherry-markdown.engine.core.esm.js +1 -0
  11. package/dist/cherry-markdown.engine.core.js +1 -0
  12. package/dist/cherry-markdown.esm.js +1 -0
  13. package/dist/cherry-markdown.js +70837 -0
  14. package/dist/cherry-markdown.js.map +1 -0
  15. package/dist/cherry-markdown.min.css +1 -0
  16. package/dist/cherry-markdown.min.js +1 -0
  17. package/dist/stats.html +4838 -0
  18. package/examples/scripts/pinyin/README.md +53 -0
  19. package/package.json +167 -0
  20. package/src/Cherry.config.js +411 -0
  21. package/src/Cherry.js +788 -0
  22. package/src/CherryStatic.js +70 -0
  23. package/src/Editor.js +746 -0
  24. package/src/Engine.js +334 -0
  25. package/src/Event.js +74 -0
  26. package/src/Factory.js +180 -0
  27. package/src/Logger.js +31 -0
  28. package/src/Previewer.js +1147 -0
  29. package/src/Sanitizer.js +4 -0
  30. package/src/Sanitizer.node.js +7 -0
  31. package/src/Stats.js +101 -0
  32. package/src/Theme.js +46 -0
  33. package/src/UrlCache.js +98 -0
  34. package/src/addons/cherry-code-block-card-plugin.js +213 -0
  35. package/src/addons/cherry-code-block-echarts-plugin.js +161 -0
  36. package/src/addons/cherry-code-block-mermaid-plugin.js +118 -0
  37. package/src/core/HookCenter.js +303 -0
  38. package/src/core/HooksConfig.js +106 -0
  39. package/src/core/ParagraphBase.js +314 -0
  40. package/src/core/SentenceBase.js +65 -0
  41. package/src/core/SyntaxBase.js +197 -0
  42. package/src/core/hooks/AutoLink.js +251 -0
  43. package/src/core/hooks/BackgroundColor.js +46 -0
  44. package/src/core/hooks/Badge.js +100 -0
  45. package/src/core/hooks/Blockquote.js +113 -0
  46. package/src/core/hooks/Br.js +85 -0
  47. package/src/core/hooks/CodeBlock.js +876 -0
  48. package/src/core/hooks/Color.js +78 -0
  49. package/src/core/hooks/CommentReference.js +96 -0
  50. package/src/core/hooks/Detail.js +138 -0
  51. package/src/core/hooks/Emoji.config.js +9388 -0
  52. package/src/core/hooks/Emoji.js +223 -0
  53. package/src/core/hooks/Emphasis.js +113 -0
  54. package/src/core/hooks/Footnote.js +125 -0
  55. package/src/core/hooks/FrontMatter.js +52 -0
  56. package/src/core/hooks/FrontMatterVars.js +82 -0
  57. package/src/core/hooks/Header.js +229 -0
  58. package/src/core/hooks/HighLight.js +37 -0
  59. package/src/core/hooks/Hr.js +52 -0
  60. package/src/core/hooks/HtmlBlock.js +159 -0
  61. package/src/core/hooks/Iframe.js +80 -0
  62. package/src/core/hooks/Image.js +276 -0
  63. package/src/core/hooks/InlineCode.js +45 -0
  64. package/src/core/hooks/InlineMath.js +142 -0
  65. package/src/core/hooks/Link.js +169 -0
  66. package/src/core/hooks/List.js +260 -0
  67. package/src/core/hooks/Mark.js +55 -0
  68. package/src/core/hooks/MathBlock.js +97 -0
  69. package/src/core/hooks/Panel.js +170 -0
  70. package/src/core/hooks/Paragraph.js +84 -0
  71. package/src/core/hooks/Ruby.js +34 -0
  72. package/src/core/hooks/Size.js +84 -0
  73. package/src/core/hooks/Strikethrough.js +54 -0
  74. package/src/core/hooks/Sub.js +47 -0
  75. package/src/core/hooks/SuggestList.js +317 -0
  76. package/src/core/hooks/Suggester.js +759 -0
  77. package/src/core/hooks/Sup.js +47 -0
  78. package/src/core/hooks/Table.js +315 -0
  79. package/src/core/hooks/Toc.js +290 -0
  80. package/src/core/hooks/Transfer.js +47 -0
  81. package/src/core/hooks/Underline.js +37 -0
  82. package/src/index.core.js +29 -0
  83. package/src/index.engine.core.js +62 -0
  84. package/src/index.engine.js +30 -0
  85. package/src/index.js +28 -0
  86. package/src/locales/index.js +21 -0
  87. package/src/locales/zh_CN.js +170 -0
  88. package/src/sass/cherry.scss +122 -0
  89. package/src/sass/components/bubble.scss +122 -0
  90. package/src/sass/components/codemirror.scss +628 -0
  91. package/src/sass/components/dropdown.scss +37 -0
  92. package/src/sass/components/editor.scss +78 -0
  93. package/src/sass/components/preview.scss +71 -0
  94. package/src/sass/components/prism.scss +142 -0
  95. package/src/sass/components/stats.scss +32 -0
  96. package/src/sass/components/toc.scss +184 -0
  97. package/src/sass/components/toolbar.scss +117 -0
  98. package/src/sass/core/AutoLink.scss +20 -0
  99. package/src/sass/core/BackgroundColor.scss +0 -0
  100. package/src/sass/core/Badge.scss +116 -0
  101. package/src/sass/core/Blockquote.scss +12 -0
  102. package/src/sass/core/Br.scss +0 -0
  103. package/src/sass/core/Card.scss +219 -0
  104. package/src/sass/core/CodeBlock.scss +205 -0
  105. package/src/sass/core/Color.scss +37 -0
  106. package/src/sass/core/CommentReference.scss +0 -0
  107. package/src/sass/core/Detail.scss +107 -0
  108. package/src/sass/core/Emoji.scss +127 -0
  109. package/src/sass/core/Emphasis.scss +9 -0
  110. package/src/sass/core/Footnote.scss +21 -0
  111. package/src/sass/core/FrontMatterVars.scss +19 -0
  112. package/src/sass/core/Header.scss +103 -0
  113. package/src/sass/core/HighLight.scss +0 -0
  114. package/src/sass/core/Hr.scss +10 -0
  115. package/src/sass/core/HtmlBlock.scss +0 -0
  116. package/src/sass/core/Iframe.scss +36 -0
  117. package/src/sass/core/Image.scss +59 -0
  118. package/src/sass/core/InlineCode.scss +10 -0
  119. package/src/sass/core/InlineMath.scss +11 -0
  120. package/src/sass/core/Link.scss +16 -0
  121. package/src/sass/core/List.scss +61 -0
  122. package/src/sass/core/Mark.scss +15 -0
  123. package/src/sass/core/MathBlock.scss +0 -0
  124. package/src/sass/core/Panel.scss +150 -0
  125. package/src/sass/core/Paragraph.scss +6 -0
  126. package/src/sass/core/Ruby.scss +0 -0
  127. package/src/sass/core/Size.scss +8 -0
  128. package/src/sass/core/Strikethrough.scss +0 -0
  129. package/src/sass/core/Sub.scss +5 -0
  130. package/src/sass/core/Suggester.scss +62 -0
  131. package/src/sass/core/Sup.scss +5 -0
  132. package/src/sass/core/Table.scss +127 -0
  133. package/src/sass/core/Toc.scss +28 -0
  134. package/src/sass/core/Transfer.scss +0 -0
  135. package/src/sass/core/Underline.scss +0 -0
  136. package/src/sass/google-fonts.scss +34 -0
  137. package/src/sass/index.scss +3 -0
  138. package/src/sass/prism/dark.scss +131 -0
  139. package/src/sass/prism/light.scss +143 -0
  140. package/src/sass/variables/colors.scss +96 -0
  141. package/src/toolbars/Bubble.js +232 -0
  142. package/src/toolbars/BubbleTable.js +147 -0
  143. package/src/toolbars/HookCenter.js +185 -0
  144. package/src/toolbars/MenuBase.js +357 -0
  145. package/src/toolbars/PreviewerBubble.js +558 -0
  146. package/src/toolbars/Toc.js +246 -0
  147. package/src/toolbars/Toolbar.js +401 -0
  148. package/src/toolbars/hooks/Audio.js +53 -0
  149. package/src/toolbars/hooks/Badge.js +80 -0
  150. package/src/toolbars/hooks/BarTable.js +41 -0
  151. package/src/toolbars/hooks/Bold.js +70 -0
  152. package/src/toolbars/hooks/Br.js +34 -0
  153. package/src/toolbars/hooks/Card.js +64 -0
  154. package/src/toolbars/hooks/CheckList.js +41 -0
  155. package/src/toolbars/hooks/Code.js +46 -0
  156. package/src/toolbars/hooks/Color.js +285 -0
  157. package/src/toolbars/hooks/Copy.js +139 -0
  158. package/src/toolbars/hooks/Detail.js +70 -0
  159. package/src/toolbars/hooks/ECharts.js +303 -0
  160. package/src/toolbars/hooks/Emoji.js +303 -0
  161. package/src/toolbars/hooks/Export.js +47 -0
  162. package/src/toolbars/hooks/File.js +54 -0
  163. package/src/toolbars/hooks/Formula.js +36 -0
  164. package/src/toolbars/hooks/FullScreen.js +55 -0
  165. package/src/toolbars/hooks/Graph.js +281 -0
  166. package/src/toolbars/hooks/H1.js +71 -0
  167. package/src/toolbars/hooks/H2.js +71 -0
  168. package/src/toolbars/hooks/H3.js +71 -0
  169. package/src/toolbars/hooks/Header.js +100 -0
  170. package/src/toolbars/hooks/Hr.js +35 -0
  171. package/src/toolbars/hooks/Iframe.js +35 -0
  172. package/src/toolbars/hooks/Image.js +60 -0
  173. package/src/toolbars/hooks/Insert.js +36 -0
  174. package/src/toolbars/hooks/Italic.js +70 -0
  175. package/src/toolbars/hooks/LineTable.js +41 -0
  176. package/src/toolbars/hooks/Link.js +46 -0
  177. package/src/toolbars/hooks/List.js +55 -0
  178. package/src/toolbars/hooks/Ol.js +41 -0
  179. package/src/toolbars/hooks/Panel.js +155 -0
  180. package/src/toolbars/hooks/Quote.js +45 -0
  181. package/src/toolbars/hooks/Redo.js +33 -0
  182. package/src/toolbars/hooks/Ruby.js +59 -0
  183. package/src/toolbars/hooks/Size.js +100 -0
  184. package/src/toolbars/hooks/Split.js +37 -0
  185. package/src/toolbars/hooks/Strikethrough.js +65 -0
  186. package/src/toolbars/hooks/Sub.js +58 -0
  187. package/src/toolbars/hooks/Sup.js +58 -0
  188. package/src/toolbars/hooks/SwitchModel.js +78 -0
  189. package/src/toolbars/hooks/Table.js +56 -0
  190. package/src/toolbars/hooks/Toc.js +35 -0
  191. package/src/toolbars/hooks/TogglePreview.js +79 -0
  192. package/src/toolbars/hooks/Ul.js +41 -0
  193. package/src/toolbars/hooks/Underline.js +65 -0
  194. package/src/toolbars/hooks/Undo.js +30 -0
  195. package/src/toolbars/hooks/Video.js +53 -0
  196. package/src/utils/LazyLoadImg.js +341 -0
  197. package/src/utils/autoindent.js +58 -0
  198. package/src/utils/codeBlockContentHandler.js +351 -0
  199. package/src/utils/config.js +98 -0
  200. package/src/utils/copy.js +55 -0
  201. package/src/utils/dialog.js +196 -0
  202. package/src/utils/dom.js +162 -0
  203. package/src/utils/downloadUtil.js +23 -0
  204. package/src/utils/env.js +22 -0
  205. package/src/utils/error.js +61 -0
  206. package/src/utils/event.js +38 -0
  207. package/src/utils/export.js +115 -0
  208. package/src/utils/file.js +121 -0
  209. package/src/utils/formulaUtilsHandler.js +230 -0
  210. package/src/utils/htmlparser.js +977 -0
  211. package/src/utils/image.js +99 -0
  212. package/src/utils/imgSizeHandler.js +279 -0
  213. package/src/utils/jsonUtils.js +17 -0
  214. package/src/utils/lineFeed.js +49 -0
  215. package/src/utils/listContentHandler.js +227 -0
  216. package/src/utils/lookbehind-replace.js +81 -0
  217. package/src/utils/mathjax.js +89 -0
  218. package/src/utils/myersDiff.js +211 -0
  219. package/src/utils/pasteHelper.js +253 -0
  220. package/src/utils/recount-pos.js +59 -0
  221. package/src/utils/regexp.js +295 -0
  222. package/src/utils/sanitize.js +477 -0
  223. package/src/utils/selection.js +50 -0
  224. package/src/utils/svgUtils.js +96 -0
  225. package/src/utils/tableContentHandler.js +592 -0
  226. package/tools/README.md +3 -0
@@ -0,0 +1,60 @@
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 } from '@/utils/file';
18
+ /**
19
+ * 插入图片
20
+ */
21
+ export default class Image extends MenuBase {
22
+ constructor($cherry) {
23
+ super($cherry);
24
+ this.setName('image', 'image');
25
+ }
26
+
27
+ /**
28
+ * 响应点击事件
29
+ * @param {string} selection 被用户选中的文本内容
30
+ * @returns {string} 回填到编辑器光标位置/选中文本区域的内容
31
+ */
32
+ onClick(selection, shortKey = '') {
33
+ if (this.hasCacheOnce()) {
34
+ // @ts-ignore
35
+ const { name, url, params } = this.getAndCleanCacheOnce();
36
+ const begin = '![';
37
+ const end = `](${url})`;
38
+ this.registerAfterClickCb(() => {
39
+ this.setLessSelection(begin, end);
40
+ });
41
+ const finalName = params.name ? params.name : name;
42
+ return `${begin}${finalName}${handleParams(params)}${end}`;
43
+ }
44
+ const accept = this.$cherry.options?.fileTypeLimitMap?.image ?? '*';
45
+ // 插入图片,调用上传文件逻辑
46
+ handleUpload(this.editor, 'image', accept, (name, url, params) => {
47
+ this.setCacheOnce({ name, url, params });
48
+ this.fire(null);
49
+ });
50
+ this.updateMarkdown = false;
51
+ return selection;
52
+ }
53
+
54
+ /**
55
+ * 声明绑定的快捷键,快捷键触发onClick
56
+ */
57
+ get shortcutKeys() {
58
+ return ['Ctrl-g'];
59
+ }
60
+ }
@@ -0,0 +1,36 @@
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 Insert extends MenuBase {
21
+ constructor($cherry) {
22
+ super($cherry);
23
+ this.setName('insert', 'arrow_insert');
24
+ }
25
+
26
+ /**
27
+ * 响应点击事件
28
+ * @param {string} selection 被用户选中的文本内容
29
+ * @param {string} shortKey 快捷键参数
30
+ * @param {Function} [callback] 回调函数
31
+ * @returns {string} 回填到编辑器光标位置/选中文本区域的内容
32
+ */
33
+ onClick(selection, shortKey = '', callback) {
34
+ return '';
35
+ }
36
+ }
@@ -0,0 +1,70 @@
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 Italic extends MenuBase {
21
+ constructor($cherry) {
22
+ super($cherry);
23
+ this.setName('italic', 'format_italic');
24
+ }
25
+
26
+ /**
27
+ * 是不是包含加粗语法
28
+ * @param {String} selection
29
+ * @returns {Boolean}
30
+ */
31
+ $testIsItalic(selection) {
32
+ return /^\s*(\*|_)[\s\S]+(\1)/.test(selection);
33
+ }
34
+
35
+ /**
36
+ * 响应点击事件
37
+ * @param {string} selection 被用户选中的文本内容
38
+ * @param {string} shortKey 快捷键参数,本函数不处理这个参数
39
+ * @returns {string} 回填到编辑器光标位置/选中文本区域的内容
40
+ */
41
+ onClick(selection, shortKey = '') {
42
+ let $selection = this.getSelection(selection) || this.locale.italic;
43
+ // 如果是单选,并且选中内容的开始结束内没有加粗语法,则扩大选中范围
44
+ if (!this.isSelections && !this.$testIsItalic($selection)) {
45
+ this.getMoreSelection('*', '*', () => {
46
+ const newSelection = this.editor.editor.getSelection();
47
+ const isItalic = this.$testIsItalic(newSelection);
48
+ if (isItalic) {
49
+ $selection = newSelection;
50
+ }
51
+ return isItalic;
52
+ });
53
+ }
54
+ if (this.$testIsItalic($selection)) {
55
+ return $selection.replace(/(^)(\s*)(\*|_)([^\n]+)(\3)(\s*)($)/gm, '$1$4$7');
56
+ }
57
+ this.registerAfterClickCb(() => {
58
+ this.setLessSelection('*', '*');
59
+ });
60
+ return $selection.replace(/(^)([^\n]+)($)/gm, '$1*$2*$3');
61
+ }
62
+
63
+ /**
64
+ * 获得监听的快捷键
65
+ * 在windows下是Ctrl+i,在mac下是cmd+i
66
+ */
67
+ get shortcutKeys() {
68
+ return ['Ctrl-i'];
69
+ }
70
+ }
@@ -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 LineTable extends MenuBase {
21
+ constructor($cherry) {
22
+ super($cherry);
23
+ this.setName('line-table', 'table_chart_view');
24
+ }
25
+
26
+ /**
27
+ * 响应点击事件
28
+ * @param {string} selection 被用户选中的文本内容
29
+ * @returns {string} 回填到编辑器光标位置/选中文本区域的内容
30
+ */
31
+ onClick(selection, shortKey = '') {
32
+ // 插入带折线图的表格
33
+ return `${selection}\n\n${[
34
+ '| :line: {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,46 @@
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 Link extends MenuBase {
21
+ constructor($cherry) {
22
+ super($cherry);
23
+ this.setName('link', 'link');
24
+ }
25
+
26
+ /**
27
+ * 响应点击事件
28
+ * @param {string} selection 被用户选中的文本内容
29
+ * @returns {string} 回填到编辑器光标位置/选中文本区域的内容
30
+ */
31
+ onClick(selection, shortKey = '') {
32
+ // 插入图片,调用上传文件逻辑
33
+ if (/^http/.test(selection)) {
34
+ return `[${this.locale.link}](${selection})`;
35
+ }
36
+ const title = selection ? selection : this.locale.link;
37
+ return `[${title}](http://url.com) `;
38
+ }
39
+
40
+ /**
41
+ * 声明绑定的快捷键,快捷键触发onClick
42
+ */
43
+ get shortcutKeys() {
44
+ return ['Ctrl-l'];
45
+ }
46
+ }
@@ -0,0 +1,55 @@
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 { getSelection } from '@/utils/selection';
18
+ import { getListFromStr } from '@/utils/regexp';
19
+ /**
20
+ * 插入有序/无序/checklist列表的按钮
21
+ */
22
+ export default class List extends MenuBase {
23
+ constructor($cherry) {
24
+ super($cherry);
25
+ this.setName('list', 'list');
26
+ this.subMenuConfig = [
27
+ { iconName: 'ol', name: 'ol', onclick: this.bindSubClick.bind(this, '1') },
28
+ { iconName: 'ul', name: 'ul', onclick: this.bindSubClick.bind(this, '2') },
29
+ { iconName: 'checklist', name: 'checklist', onclick: this.bindSubClick.bind(this, '3') },
30
+ ];
31
+ }
32
+
33
+ getSubMenuConfig() {
34
+ return this.subMenuConfig;
35
+ }
36
+
37
+ /**
38
+ * 响应点击事件
39
+ * @param {string} selection 编辑区选中的文本内容
40
+ * @param {1|2|3|'ol'|'1'|'2'|'3'|'ul'|'checklist'|''} shortKey 快捷键:ol(1)有序列表,ul(2)无序列表,checklist(3) 检查项
41
+ * @returns 对应markdown的源码
42
+ */
43
+ onClick(selection, shortKey = '') {
44
+ const listType = [null, 'ol', 'ul', 'checklist']; // 下标1, 2, 3生效
45
+ const $selection = getSelection(this.editor.editor, selection, 'line', true);
46
+ const [before] = $selection.match(/^\n*/);
47
+ const [after] = $selection.match(/\n*$/);
48
+ const type = listType[shortKey] ? listType[shortKey] : shortKey;
49
+
50
+ if (!type || !/^(ol|ul|checklist)$/.test(type)) {
51
+ return $selection;
52
+ }
53
+ return `${before}${getListFromStr($selection, type)}${after}`;
54
+ }
55
+ }
@@ -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
+ import { getSelection } from '@/utils/selection';
18
+ import { getListFromStr } from '@/utils/regexp';
19
+
20
+ /**
21
+ * 下标的按钮
22
+ **/
23
+ export default class Ol extends MenuBase {
24
+ constructor($cherry) {
25
+ super($cherry);
26
+ this.setName('ol', 'format_list_numbered');
27
+ }
28
+
29
+ /**
30
+ *
31
+ * @param {string} selection 被用户选中的文本内容
32
+ * @param {string} shortKey 快捷键参数,本函数不处理这个参数
33
+ * @returns {string} 回填到编辑器光标位置/选中文本区域的内容
34
+ */
35
+ onClick(selection, shortKey = '') {
36
+ const $selection = getSelection(this.editor.editor, selection, 'line', true) || 'Item 1\n Item 1.1\nItem 2';
37
+ const [before] = $selection.match(/^\n*/);
38
+ const [after] = $selection.match(/\n*$/);
39
+ return `${before}${getListFromStr($selection, 'ol')}${after}`;
40
+ }
41
+ }
@@ -0,0 +1,155 @@
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 { getPanelRule } from '@/utils/regexp';
18
+ import { getSelection } from '@/utils/selection';
19
+ /**
20
+ * 插入面板
21
+ */
22
+ export default class Panel extends MenuBase {
23
+ constructor($cherry) {
24
+ super($cherry);
25
+ this.setName('panel', 'dock_to_bottom');
26
+ this.panelRule = getPanelRule().reg;
27
+ this.subMenuConfig = [
28
+ {
29
+ iconName: 'notification_important',
30
+ name: 'panelImportant',
31
+ onclick: this.bindSubClick.bind(this, 'important'),
32
+ },
33
+ {
34
+ iconName: 'info_i',
35
+ name: 'panelInfo',
36
+ onclick: this.bindSubClick.bind(this, 'info'),
37
+ },
38
+ {
39
+ iconName: 'priority_high',
40
+ name: 'panelWarn',
41
+ onclick: this.bindSubClick.bind(this, 'warning'),
42
+ },
43
+ {
44
+ iconName: 'close',
45
+ name: 'panelDanger',
46
+ onclick: this.bindSubClick.bind(this, 'danger'),
47
+ },
48
+ {
49
+ iconName: 'emoji_objects',
50
+ name: 'panelTip',
51
+ onclick: this.bindSubClick.bind(this, 'tip'),
52
+ },
53
+ {
54
+ iconName: 'align_justify_flex_start',
55
+ name: 'panelTop',
56
+ onclick: this.bindSubClick.bind(this, 'left'),
57
+ },
58
+ {
59
+ iconName: 'align_justify_center',
60
+ name: 'panelCenter',
61
+ onclick: this.bindSubClick.bind(this, 'center'),
62
+ },
63
+ {
64
+ iconName: 'align_justify_flex_end',
65
+ name: 'panelBottom',
66
+ onclick: this.bindSubClick.bind(this, 'right'),
67
+ },
68
+ ];
69
+ }
70
+
71
+ /**
72
+ * 从字符串中找打面板的name
73
+ * @param {string} str
74
+ * @returns {string | false}
75
+ */
76
+ $getNameFromStr(str) {
77
+ let ret = false;
78
+ this.panelRule.lastIndex = 0;
79
+ str.replace(this.panelRule, (match, preLines, name, content) => {
80
+ const $name = /\s/.test(name.trim()) ? name.trim().replace(/\s.*$/, '') : name;
81
+ ret = $name ? $name.trim().toLowerCase() : '';
82
+ return match;
83
+ });
84
+ return ret;
85
+ }
86
+
87
+ $getTitle(str) {
88
+ this.panelRule.lastIndex = 0;
89
+ str.replace(this.panelRule, (match, preLines, name, content) => {
90
+ const $name = name.trim();
91
+ return /\s/.test($name) ? $name.replace(/[^\s]+\s/, '') : '';
92
+ });
93
+ return '';
94
+ }
95
+
96
+ /**
97
+ * 响应点击事件
98
+ * @param {string} selection 被用户选中的文本内容
99
+ * @param {string} shortKey 快捷键参数
100
+ * @returns {string} 回填到编辑器光标位置/选中文本区域的内容
101
+ */
102
+ onClick(selection, shortKey = '') {
103
+ let $selection = getSelection(this.editor.editor, selection, 'line', true) || '内容';
104
+ let currentName = this.$getNameFromStr($selection);
105
+ let title = this.$getTitle($selection);
106
+ if (currentName === false) {
107
+ // 如果没有命中面板语法,则尝试扩大选区
108
+ this.getMoreSelection('::: ', '\n', () => {
109
+ const newSelection = this.editor.editor.getSelection();
110
+ const isMatch = this.$getNameFromStr(newSelection);
111
+ if (isMatch !== false) {
112
+ $selection = newSelection;
113
+ currentName = isMatch;
114
+ title = this.$getTitle(newSelection);
115
+ }
116
+ return isMatch !== false;
117
+ });
118
+ }
119
+ if (currentName !== false) {
120
+ // 如果命中了面板语法,则尝试去掉语法或者变更语法
121
+ if (currentName === shortKey) {
122
+ // 去掉面板语法
123
+ this.panelRule.lastIndex = 0;
124
+ return $selection.replace(this.panelRule, (match, preLines, name, content) => {
125
+ const $name = name.trim();
126
+ const $title = /\s/.test($name) ? $name.replace(/[^\s]+\s/, '') : '';
127
+ return `${$title}\n${content}`;
128
+ });
129
+ }
130
+ // 修改name
131
+ this.registerAfterClickCb(() => {
132
+ this.setLessSelection('::: ', '\n');
133
+ });
134
+ this.panelRule.lastIndex = 0;
135
+ return $selection.replace(this.panelRule, (match, preLines, name, content) => {
136
+ const $name = name.trim();
137
+ const $title = /\s/.test($name) ? $name.replace(/[^\s]+\s/, '') : '';
138
+ return `::: ${shortKey} ${$title}\n${content.replace(/\n+$/, '')}\n:::`;
139
+ });
140
+ }
141
+ this.registerAfterClickCb(() => {
142
+ this.setLessSelection('::: ', '\n');
143
+ });
144
+ $selection = $selection.replace(/^\n+/, '');
145
+ if (/\n/.test($selection)) {
146
+ if (!title) {
147
+ title = $selection.replace(/\n[\w\W]+$/, '');
148
+ $selection = $selection.replace(/^[^\n]+\n/, '');
149
+ }
150
+ } else {
151
+ title = title ? title : '标题';
152
+ }
153
+ return `::: ${shortKey} ${title}\n${$selection}\n:::`.replace(/\n{2,}:::/g, '\n:::');
154
+ }
155
+ }
@@ -0,0 +1,45 @@
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 { getSelection } from '@/utils/selection';
18
+ /**
19
+ * 插入“引用”的按钮
20
+ */
21
+ export default class Quote extends MenuBase {
22
+ constructor($cherry) {
23
+ super($cherry);
24
+ this.setName('quote', 'format_quote');
25
+ }
26
+
27
+ /**
28
+ * click handler
29
+ * @param {string} selection selection in editor
30
+ * @returns
31
+ */
32
+ onClick(selection) {
33
+ const $selection = getSelection(this.editor.editor, selection, 'line', true) || this.locale.quote;
34
+ const isWrapped = $selection.split('\n').every((text) => /^\s*>[^\n]+$/.exec(text));
35
+ if (isWrapped) {
36
+ // 去掉>号
37
+ return $selection.replace(/(^\s*)>\s*([^\n]+)($)/gm, '$1$2$3').replace(/\n+$/, '\n\n');
38
+ }
39
+ this.registerAfterClickCb(() => {
40
+ this.setLessSelection('> ', '');
41
+ });
42
+ // 给每一行增加>号
43
+ return $selection.replace(/(^)([^\n]+)($)/gm, '$1> $2$3').replace(/\n+$/, '\n\n');
44
+ }
45
+ }
@@ -0,0 +1,33 @@
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
+ * 依赖codemirror的undo接口
20
+ */
21
+ export default class Redo extends MenuBase {
22
+ constructor($cherry) {
23
+ super($cherry);
24
+ this.setName('redo', 'redo');
25
+ }
26
+
27
+ /**
28
+ * 直接调用codemirror的redo方法就好了
29
+ */
30
+ onClick() {
31
+ this.editor.editor.redo();
32
+ }
33
+ }
@@ -0,0 +1,59 @@
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 { getSelection } from '@/utils/selection';
18
+ /**
19
+ * 生成ruby,使用场景:给中文增加拼音、给中文增加英文、给英文增加中文等等
20
+ */
21
+ export default class Ruby extends MenuBase {
22
+ constructor($cherry) {
23
+ super($cherry);
24
+ this.setName('ruby', 'language_pinyin');
25
+ }
26
+
27
+ $testIsRuby(selection) {
28
+ return /^\s*\{[\s\S]+\|[\s\S]+\}/.test(selection);
29
+ }
30
+
31
+ /**
32
+ * 响应点击事件
33
+ * @param {string} selection 被用户选中的文本内容
34
+ * @param {string} shortKey 快捷键参数,本函数不处理这个参数
35
+ * @returns {string} 回填到编辑器光标位置/选中文本区域的内容
36
+ */
37
+ onClick(selection, shortKey = '') {
38
+ let $selection = getSelection(this.editor.editor, selection) || '拼音';
39
+ // 如果选中的文本中已经有ruby语法了,则去掉该语法
40
+ if (!this.isSelections && !this.$testIsRuby($selection)) {
41
+ this.getMoreSelection(' { ', ' } ', () => {
42
+ const newSelection = this.editor.editor.getSelection();
43
+ const isRuby = this.$testIsRuby(newSelection);
44
+ if (isRuby) {
45
+ $selection = newSelection;
46
+ }
47
+ return isRuby;
48
+ });
49
+ }
50
+ if (this.$testIsRuby($selection)) {
51
+ return $selection.replace(/^\s*\{\s*([\s\S]+?)\s*\|[\s\S]+\}\s*/gm, '$1');
52
+ }
53
+ const pinyin = (this.editor.$cherry.options.callback.changeString2Pinyin($selection) || 'pin yin').trim();
54
+ this.registerAfterClickCb(() => {
55
+ this.setLessSelection(' { ', ' } ');
56
+ });
57
+ return ` { ${$selection} | ${pinyin} } `;
58
+ }
59
+ }