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,357 @@
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
+ // @ts-check
17
+ import Logger from '@/Logger';
18
+ import { escapeHTMLSpecialCharOnce as $e } from '@/utils/sanitize';
19
+ import { createElement } from '@/utils/dom';
20
+
21
+ /**
22
+ * @typedef {Object} SubMenuConfigItem
23
+ * @property {string} name - 子菜单项名称
24
+ * @property {string=} iconName - 子菜单项图标名称
25
+ * @property {function} onclick - 子菜单项点击事件
26
+ */
27
+
28
+ /**
29
+ *
30
+ * @param {HTMLElement} targetDom
31
+ * @param {'absolute' | 'fixed'} [positionModel = 'absolute']
32
+ * @returns {Pick<DOMRect, 'left' | 'top' | 'width' | 'height'>}
33
+ */
34
+ function getPosition(targetDom, positionModel = 'absolute') {
35
+ const pos = targetDom.getBoundingClientRect();
36
+ if (positionModel === 'fixed') {
37
+ return pos;
38
+ }
39
+
40
+ return { left: targetDom.offsetLeft, top: targetDom.offsetTop, width: pos.width, height: pos.height };
41
+ }
42
+
43
+ /**
44
+ * @typedef {import('@/Editor').default} Editor
45
+ */
46
+
47
+ /**
48
+ * @class MenuBase
49
+ */
50
+ export default class MenuBase {
51
+ /**
52
+ * @deprecated
53
+ * @type {MenuBase['fire']}
54
+ */
55
+ _onClick;
56
+
57
+ /**
58
+ *
59
+ * @param {Cherry} $cherry
60
+ */
61
+ constructor($cherry) {
62
+ this.$cherry = $cherry;
63
+ this.bubbleMenu = false;
64
+ this.subMenu = null; // 子菜单实例
65
+ this.name = ''; // 菜单项Name
66
+ this.editor = $cherry.editor; // markdown实例
67
+ this.locale = $cherry.locale;
68
+ this.dom = null;
69
+ this.updateMarkdown = true; // 是否更新markdown原文
70
+ /** @type {SubMenuConfigItem[]} */
71
+ this.subMenuConfig = []; // 子菜单配置
72
+ this.noIcon = false; // 是否不显示图标
73
+ this.cacheOnce = false; // 是否保存一次点击事件生成的内容
74
+ /**
75
+ * 子菜单的定位方式
76
+ * @property
77
+ * @type {'absolute' | 'fixed' }
78
+ */
79
+ this.positionModel = 'absolute';
80
+ // eslint-disable-next-line no-underscore-dangle
81
+ if (typeof this._onClick === 'function') {
82
+ Logger.warn('`MenuBase._onClick` is deprecated. Override `fire` instead');
83
+ // eslint-disable-next-line no-underscore-dangle
84
+ this.fire = this._onClick;
85
+ }
86
+ }
87
+
88
+ getSubMenuConfig() {
89
+ return this.subMenuConfig;
90
+ }
91
+
92
+ /**
93
+ * 设置菜单
94
+ * @param {string} name 菜单名称
95
+ * @param {string} [iconName] 菜单图标名
96
+ */
97
+ setName(name, iconName) {
98
+ this.name = name;
99
+ this.iconName = iconName;
100
+ }
101
+
102
+ /**
103
+ * 设置一个一次性缓存
104
+ * 使用场景:
105
+ * 当需要异步操作是,比如上传视频、选择字体颜色、通过棋盘插入表格等
106
+ * 实现原理:
107
+ * 1、第一次点击按钮时触发fire()方法,触发选择文件、选择颜色、选择棋盘格的操作。此时onClick()不返回任何数据。
108
+ * 2、当异步操作完成后(如提交了文件、选择了颜色等),调用本方法(setCacheOnce)实现缓存,最后调用fire()方法
109
+ * 3、当fire()方法再次调用onClick()方法时,onClick()方法会返回缓存的数据(getAndCleanCacheOnce)
110
+ *
111
+ * 这么设计的原因:
112
+ * 1、可以复用MenuBase的相关方法
113
+ * 2、避免异步操作直接与codemirror交互
114
+ * @param {*} info
115
+ */
116
+ setCacheOnce(info) {
117
+ this.cacheOnce = info;
118
+ }
119
+
120
+ getAndCleanCacheOnce() {
121
+ this.updateMarkdown = true;
122
+ const ret = this.cacheOnce;
123
+ this.cacheOnce = false;
124
+ return ret;
125
+ }
126
+ getActiveSubMenuIndex(subMenuDomPanel) {
127
+ return -1;
128
+ }
129
+
130
+ hasCacheOnce() {
131
+ return this.cacheOnce !== false;
132
+ }
133
+
134
+ /**
135
+ * 创建一个一级菜单
136
+ * @param {boolean} asSubMenu 是否以子菜单的形式创建
137
+ */
138
+ createBtn(asSubMenu = false) {
139
+ const classNames = asSubMenu
140
+ ? 'cherry-dropdown-item'
141
+ : `cherry-toolbar-button cherry-toolbar-${this.iconName ? this.iconName : this.name}`;
142
+ const span = createElement('span', classNames, {
143
+ title: this.locale[this.name] || $e(this.name),
144
+ });
145
+ // 如果有图标,则添加图标
146
+ if (this.iconName && !this.noIcon) {
147
+ const icon = createElement('i', `material-symbols-outlined`);
148
+ icon.innerText = this.iconName;
149
+ span.appendChild(icon);
150
+ }
151
+ // 二级菜单强制显示文字,没有图标的按钮也显示文字
152
+ if (asSubMenu || this.noIcon) {
153
+ span.innerHTML += this.locale[this.name] || $e(this.name);
154
+ }
155
+ // 只有一级菜单才保存dom,且只保存一次
156
+ if (!asSubMenu && !this.dom) {
157
+ this.dom = span;
158
+ }
159
+ return span;
160
+ }
161
+
162
+ createSubBtnByConfig(config) {
163
+ const { name, iconName, onclick } = config;
164
+ const span = createElement('span', 'cherry-dropdown-item', {
165
+ title: this.locale[name] || $e(name),
166
+ });
167
+ if (iconName) {
168
+ const icon = createElement('i', `material-symbols-outlined`);
169
+ icon.innerText = iconName;
170
+ span.appendChild(icon);
171
+ }
172
+ span.innerHTML += this.locale[name] || $e(name);
173
+ span.addEventListener('click', onclick, false);
174
+ return span;
175
+ }
176
+
177
+ /**
178
+ * 处理菜单项点击事件
179
+ * @param {MouseEvent | KeyboardEvent | undefined} [event] 点击事件
180
+ * @returns {void}
181
+ */
182
+ fire(event, shortKey = '') {
183
+ event?.stopPropagation();
184
+ if (typeof this.onClick === 'function') {
185
+ const selections = this.editor.editor.getSelections();
186
+ // 判断是不是多选
187
+ this.isSelections = selections.length > 1;
188
+ // 当onClick返回null、undefined、false时,维持原样
189
+ const ret = selections.map(
190
+ (selection, index, srcArray) => this.onClick(selection, shortKey, event) || srcArray[index],
191
+ );
192
+
193
+ if (!this.bubbleMenu && this.updateMarkdown) {
194
+ // 非下拉菜单按钮保留selection
195
+ this.editor.editor.replaceSelections(ret, 'around');
196
+ this.editor.editor.focus();
197
+ this.$afterClick();
198
+ }
199
+ }
200
+ }
201
+
202
+ /**
203
+ * 获取当前选择区域的range
204
+ */
205
+ $getSelectionRange() {
206
+ const { anchor, head } = this.editor.editor.listSelections()[0];
207
+ // 如果begin在end的后面
208
+ if ((anchor.line === head.line && anchor.ch > head.ch) || anchor.line > head.line) {
209
+ return { begin: head, end: anchor };
210
+ }
211
+ return { begin: anchor, end: head };
212
+ }
213
+
214
+ /**
215
+ * 注册点击事件渲染后的回调函数
216
+ * @param {function} cb
217
+ */
218
+ registerAfterClickCb(cb) {
219
+ this.afterClickCb = cb;
220
+ }
221
+
222
+ /**
223
+ * 点击事件渲染后的回调函数
224
+ */
225
+ $afterClick() {
226
+ if (typeof this.afterClickCb === 'function' && !this.isSelections) {
227
+ this.afterClickCb();
228
+ this.afterClickCb = null;
229
+ }
230
+ }
231
+
232
+ /**
233
+ * 选中除了前后语法后的内容
234
+ * @param {String} lessBefore
235
+ * @param {String} lessAfter
236
+ */
237
+ setLessSelection(lessBefore, lessAfter) {
238
+ const cm = this.editor.editor;
239
+ const { begin, end } = this.$getSelectionRange();
240
+ const newBeginLine = lessBefore.match(/\n/g)?.length > 0 ? begin.line + lessBefore.match(/\n/g).length : begin.line;
241
+ const newBeginCh =
242
+ lessBefore.match(/\n/g)?.length > 0
243
+ ? lessBefore.replace(/^[\s\S]*?\n([^\n]*)$/, '$1').length
244
+ : begin.ch + lessBefore.length;
245
+ const newBegin = { line: newBeginLine, ch: newBeginCh };
246
+ const newEndLine = lessAfter.match(/\n/g)?.length > 0 ? end.line - lessAfter.match(/\n/g).length : end.line;
247
+ const newEndCh = lessAfter.match(/\n/g)?.length > 0 ? cm.getLine(newEndLine).length : end.ch - lessAfter.length;
248
+ const newEnd = { line: newEndLine, ch: newEndCh };
249
+ cm.setSelection(newBegin, newEnd);
250
+ }
251
+
252
+ /**
253
+ * 基于当前已选择区域,获取更多的选择区
254
+ * @param {string} [appendBefore] 选择区前面追加的内容
255
+ * @param {string} [appendAfter] 选择区后面追加的内容
256
+ * @param {function} [cb] 回调函数,如果返回false,则恢复原来的选取
257
+ */
258
+ getMoreSelection(appendBefore, appendAfter, cb) {
259
+ const cm = this.editor.editor;
260
+ const { begin, end } = this.$getSelectionRange();
261
+ let newBeginCh =
262
+ // 如果只包含换行,则起始位置一定是0
263
+ /\n/.test(appendBefore) ? 0 : begin.ch - appendBefore.length;
264
+ newBeginCh = newBeginCh < 0 ? 0 : newBeginCh;
265
+ let newBeginLine = /\n/.test(appendBefore) ? begin.line - appendBefore.match(/\n/g).length : begin.line;
266
+ newBeginLine = newBeginLine < 0 ? 0 : newBeginLine;
267
+ const newBegin = { line: newBeginLine, ch: newBeginCh };
268
+ let newEndLine = end.line;
269
+ let newEndCh = end.ch;
270
+ if (/\n/.test(appendAfter)) {
271
+ newEndLine = end.line + appendAfter.match(/\n/g).length;
272
+ newEndCh = cm.getLine(newEndLine)?.length;
273
+ } else {
274
+ newEndCh =
275
+ cm.getLine(end.line).length < end.ch + appendAfter.length
276
+ ? cm.getLine(end.line).length
277
+ : end.ch + appendAfter.length;
278
+ }
279
+ const newEnd = { line: newEndLine, ch: newEndCh };
280
+ cm.setSelection(newBegin, newEnd);
281
+ if (cb() === false) {
282
+ cm.setSelection(begin, end);
283
+ }
284
+ }
285
+
286
+ /**
287
+ * 获取用户选中的文本内容,如果没有选中文本,则返回光标所在的位置的内容
288
+ * @param {string} selection 当前选中的文本内容
289
+ * @param {string} type 'line': 当没有选择文本时,获取光标所在行的内容; 'word': 当没有选择文本时,获取光标所在单词的内容
290
+ * @param {boolean} focus true;强行选中光标处的内容,否则只获取选中的内容
291
+ * @returns {string}
292
+ */
293
+ getSelection(selection, type = 'word', focus = false) {
294
+ const cm = this.editor.editor;
295
+ // 多光标模式下不做处理
296
+ if (this.isSelections) {
297
+ return selection;
298
+ }
299
+ if (selection && !focus) {
300
+ return selection;
301
+ }
302
+ // 获取光标所在行的内容,同时选中所在行
303
+ if (type === 'line') {
304
+ const { begin, end } = this.$getSelectionRange();
305
+ cm.setSelection({ line: begin.line, ch: 0 }, { line: end.line, ch: cm.getLine(end.line).length });
306
+ return cm.getSelection();
307
+ }
308
+ // 获取光标所在单词的内容,同时选中所在单词
309
+ if (type === 'word') {
310
+ const { anchor: begin, head: end } = cm.findWordAt(cm.getCursor());
311
+ cm.setSelection(begin, end);
312
+ return cm.getSelection();
313
+ }
314
+ }
315
+
316
+ /**
317
+ * 反转子菜单点击事件参数顺序
318
+ * @deprecated
319
+ */
320
+ bindSubClick(shortcut, selection) {
321
+ return this.fire(null, shortcut);
322
+ }
323
+
324
+ onClick(selection, shortcut, callback) {
325
+ return selection;
326
+ }
327
+
328
+ get shortcutKeys() {
329
+ return [];
330
+ }
331
+
332
+ /**
333
+ * 获取当前菜单的位置
334
+ */
335
+ getMenuPosition() {
336
+ const parent = MenuBase.getTargetParentByButton(this.dom);
337
+ if (/cherry-bubble/.test(parent.className) || /cherry-floatmenu/.test(parent.className)) {
338
+ this.positionModel = 'fixed';
339
+ } else {
340
+ this.positionModel = 'absolute';
341
+ }
342
+ return getPosition(this.dom, this.positionModel);
343
+ }
344
+
345
+ /**
346
+ * 根据按钮获取按钮的父元素,这里父元素要绕过toolbar-(left|right)那一层
347
+ * @param {HTMLElement} dom 按钮元素
348
+ * @returns {HTMLElement} 父元素
349
+ */
350
+ static getTargetParentByButton(dom) {
351
+ let parent = dom.parentElement;
352
+ if (/toolbar-(left|right)/.test(parent.className)) {
353
+ parent = parent.parentElement;
354
+ }
355
+ return parent;
356
+ }
357
+ }