@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.
- package/package.json +149 -0
- package/src/Cherry.config.js +625 -0
- package/src/Cherry.js +1104 -0
- package/src/CherryStatic.js +70 -0
- package/src/Editor.js +748 -0
- package/src/Engine.js +381 -0
- package/src/Event.js +140 -0
- package/src/Factory.js +180 -0
- package/src/Logger.js +31 -0
- package/src/Previewer.js +1183 -0
- package/src/Sanitizer.js +4 -0
- package/src/Sanitizer.node.js +7 -0
- package/src/UrlCache.js +98 -0
- package/src/addons/advance/cherry-table-echarts-plugin.js +170 -0
- package/src/addons/cherry-code-block-mermaid-plugin.js +158 -0
- package/src/addons/cherry-code-block-plantuml-plugin.js +106 -0
- package/src/core/HookCenter.js +297 -0
- package/src/core/HooksConfig.js +100 -0
- package/src/core/ParagraphBase.js +332 -0
- package/src/core/SentenceBase.js +65 -0
- package/src/core/SyntaxBase.js +194 -0
- package/src/core/hooks/AutoLink.js +232 -0
- package/src/core/hooks/BackgroundColor.js +46 -0
- package/src/core/hooks/Blockquote.js +70 -0
- package/src/core/hooks/Br.js +85 -0
- package/src/core/hooks/CodeBlock.js +446 -0
- package/src/core/hooks/Color.js +46 -0
- package/src/core/hooks/CommentReference.js +96 -0
- package/src/core/hooks/Detail.js +108 -0
- package/src/core/hooks/Emoji.config.js +1825 -0
- package/src/core/hooks/Emoji.js +119 -0
- package/src/core/hooks/Emphasis.js +113 -0
- package/src/core/hooks/Footnote.js +125 -0
- package/src/core/hooks/FrontMatter.js +51 -0
- package/src/core/hooks/Header.js +234 -0
- package/src/core/hooks/HighLight.js +37 -0
- package/src/core/hooks/Hr.js +52 -0
- package/src/core/hooks/HtmlBlock.js +184 -0
- package/src/core/hooks/Image.js +174 -0
- package/src/core/hooks/InlineCode.js +48 -0
- package/src/core/hooks/InlineMath.js +107 -0
- package/src/core/hooks/Link.js +160 -0
- package/src/core/hooks/List.js +264 -0
- package/src/core/hooks/MathBlock.js +103 -0
- package/src/core/hooks/Panel.js +145 -0
- package/src/core/hooks/Paragraph.js +84 -0
- package/src/core/hooks/Ruby.js +34 -0
- package/src/core/hooks/Size.js +51 -0
- package/src/core/hooks/Strikethrough.js +54 -0
- package/src/core/hooks/Sub.js +47 -0
- package/src/core/hooks/SuggestList.js +333 -0
- package/src/core/hooks/Suggester.js +707 -0
- package/src/core/hooks/Sup.js +47 -0
- package/src/core/hooks/Table.js +275 -0
- package/src/core/hooks/Toc.js +292 -0
- package/src/core/hooks/Transfer.js +47 -0
- package/src/core/hooks/Underline.js +37 -0
- package/src/index.core.js +29 -0
- package/src/index.engine.core.js +68 -0
- package/src/index.engine.js +28 -0
- package/src/index.js +32 -0
- package/src/libs/mermaidAPI.8.4.8.js +1 -0
- package/src/libs/mermaidAPI.8.5.2.js +42 -0
- package/src/libs/rawdeflate.js +1663 -0
- package/src/locales/en_US.js +139 -0
- package/src/locales/index.js +25 -0
- package/src/locales/ru_RU.js +139 -0
- package/src/locales/zh_CN.js +142 -0
- package/src/sass/base.scss +26 -0
- package/src/sass/bubble_formula.scss +166 -0
- package/src/sass/ch-icon.scss +118 -0
- package/src/sass/cherry.scss +1116 -0
- package/src/sass/components/bubble.scss +173 -0
- package/src/sass/components/shortcut_key_config.scss +108 -0
- package/src/sass/formula_utils_bubble.scss +82 -0
- package/src/sass/icon_template.scss +24 -0
- package/src/sass/icons/uEA03-list.svg +19 -0
- package/src/sass/icons/uEA04-check.svg +14 -0
- package/src/sass/icons/uEA09-square.svg +10 -0
- package/src/sass/icons/uEA0A-bold.svg +20 -0
- package/src/sass/icons/uEA0B-code.svg +18 -0
- package/src/sass/icons/uEA0C-color.svg +13 -0
- package/src/sass/icons/uEA0D-header.svg +8 -0
- package/src/sass/icons/uEA0E-image.svg +15 -0
- package/src/sass/icons/uEA0F-italic.svg +8 -0
- package/src/sass/icons/uEA10-link.svg +16 -0
- package/src/sass/icons/uEA11-ol.svg +21 -0
- package/src/sass/icons/uEA12-size.svg +11 -0
- package/src/sass/icons/uEA13-strike.svg +16 -0
- package/src/sass/icons/uEA14-table.svg +12 -0
- package/src/sass/icons/uEA15-ul.svg +17 -0
- package/src/sass/icons/uEA16-underline.svg +13 -0
- package/src/sass/icons/uEA17-word.svg +16 -0
- package/src/sass/icons/uEA18-blockquote.svg +11 -0
- package/src/sass/icons/uEA19-font.svg +10 -0
- package/src/sass/icons/uEA1F-insertClass.svg +39 -0
- package/src/sass/icons/uEA20-insertFlow.svg +8 -0
- package/src/sass/icons/uEA21-insertFormula.svg +23 -0
- package/src/sass/icons/uEA22-insertGantt.svg +13 -0
- package/src/sass/icons/uEA23-insertGraph.svg +13 -0
- package/src/sass/icons/uEA24-insertPie.svg +19 -0
- package/src/sass/icons/uEA25-insertSeq.svg +20 -0
- package/src/sass/icons/uEA26-insertState.svg +35 -0
- package/src/sass/icons/uEA27-line.svg +11 -0
- package/src/sass/icons/uEA28-preview.svg +18 -0
- package/src/sass/icons/uEA29-previewClose.svg +24 -0
- package/src/sass/icons/uEA2A-toc.svg +24 -0
- package/src/sass/icons/uEA2D-sub.svg +15 -0
- package/src/sass/icons/uEA2E-sup.svg +15 -0
- package/src/sass/icons/uEA2F-h1.svg +16 -0
- package/src/sass/icons/uEA30-h2.svg +20 -0
- package/src/sass/icons/uEA31-h3.svg +23 -0
- package/src/sass/icons/uEA32-h4.svg +16 -0
- package/src/sass/icons/uEA33-h5.svg +20 -0
- package/src/sass/icons/uEA34-h6.svg +17 -0
- package/src/sass/icons/uEA35-video.svg +20 -0
- package/src/sass/icons/uEA36-insert.svg +25 -0
- package/src/sass/icons/uEA37-little_table.svg +30 -0
- package/src/sass/icons/uEA38-pdf.svg +27 -0
- package/src/sass/icons/uEA39-checklist.svg +22 -0
- package/src/sass/icons/uEA40-close.svg +12 -0
- package/src/sass/icons/uEA41-fullscreen.svg +81 -0
- package/src/sass/icons/uEA42-minscreen.svg +77 -0
- package/src/sass/icons/uEA43-insertChart.svg +23 -0
- package/src/sass/icons/uEA44-question.svg +25 -0
- package/src/sass/icons/uEA45-settings.svg +32 -0
- package/src/sass/icons/uEA46-ok.svg +7 -0
- package/src/sass/icons/uEA47-br.svg +22 -0
- package/src/sass/icons/uEA48-normal.svg +15 -0
- package/src/sass/icons/uEA49-undo.svg +19 -0
- package/src/sass/icons/uEA50-redo.svg +21 -0
- package/src/sass/icons/uEA51-copy.svg +6 -0
- package/src/sass/icons/uEA52-phone.svg +5 -0
- package/src/sass/icons/uEA53-cherry-table-delete.svg +17 -0
- package/src/sass/icons/uEA54-cherry-table-insert-bottom.svg +16 -0
- package/src/sass/icons/uEA55-cherry-table-insert-left.svg +15 -0
- package/src/sass/icons/uEA56-cherry-table-insert-right.svg +16 -0
- package/src/sass/icons/uEA57-cherry-table-insert-top.svg +16 -0
- package/src/sass/icons/uEA58-sort-s.svg +13 -0
- package/src/sass/icons/uEA59-pinyin.svg +1 -0
- package/src/sass/icons/uEA5A-create.svg +24 -0
- package/src/sass/icons/uEA5B-download.svg +34 -0
- package/src/sass/icons/uEA5C-edit.svg +3 -0
- package/src/sass/icons/uEA5D-export.svg +53 -0
- package/src/sass/icons/uEA5E-folder-open.svg +3 -0
- package/src/sass/icons/uEA5F-folder.svg +3 -0
- package/src/sass/icons/uEA60-help.svg +5 -0
- package/src/sass/icons/uEA61-pen-fill.svg +13 -0
- package/src/sass/icons/uEA62-pen.svg +3 -0
- package/src/sass/icons/uEA64-tips.svg +5 -0
- package/src/sass/icons/uEA65-warn.svg +5 -0
- package/src/sass/icons/uEA66-mistake.svg +4 -0
- package/src/sass/icons/uEA67-success.svg +4 -0
- package/src/sass/icons/uEA68-danger.svg +4 -0
- package/src/sass/icons/uEA69-info.svg +5 -0
- package/src/sass/icons/uEA6A-primary.svg +5 -0
- package/src/sass/icons/uEA6B-warning.svg +5 -0
- package/src/sass/icons/uEA6C-justify.svg +19 -0
- package/src/sass/icons/uEA6D-justifyCenter.svg +19 -0
- package/src/sass/icons/uEA6E-justifyLeft.svg +19 -0
- package/src/sass/icons/uEA6F-justifyRight.svg +19 -0
- package/src/sass/icons/uEA70-chevronsLeft.svg +1 -0
- package/src/sass/icons/uEA71-chevronsRight.svg +1 -0
- package/src/sass/icons/uEA72-trendingUp.svg +1 -0
- package/src/sass/icons/uEA74-codeBlock.svg +1 -0
- package/src/sass/icons/uEA75-expand.svg +3 -0
- package/src/sass/icons/uEA76-unExpand.svg +3 -0
- package/src/sass/icons/uEA77-swap-vert.svg +1 -0
- package/src/sass/icons/uEA78-swap.svg +1 -0
- package/src/sass/icons/uEA79-keyboard.svg +1 -0
- package/src/sass/icons/uEA7A-command.svg +1 -0
- package/src/sass/icons/uEA7B-search.svg +1 -0
- package/src/sass/index.scss +3 -0
- package/src/sass/markdown.scss +668 -0
- package/src/sass/markdown_pure.scss +9 -0
- package/src/sass/prettyprint/prettyprint.scss +118 -0
- package/src/sass/previewer.scss +179 -0
- package/src/sass/print.scss +13 -0
- package/src/sass/prism/coy.scss +220 -0
- package/src/sass/prism/dark.scss +132 -0
- package/src/sass/prism/default.scss +143 -0
- package/src/sass/prism/funky.scss +133 -0
- package/src/sass/prism/okaidia.scss +126 -0
- package/src/sass/prism/one-dark.scss +440 -0
- package/src/sass/prism/one-light.scss +428 -0
- package/src/sass/prism/solarized-light.scss +153 -0
- package/src/sass/prism/tomorrow-night.scss +125 -0
- package/src/sass/prism/twilight.scss +202 -0
- package/src/sass/prism/vs-dark.scss +275 -0
- package/src/sass/prism/vs-light.scss +168 -0
- package/src/sass/themes/blue.scss +411 -0
- package/src/sass/themes/dark.scss +517 -0
- package/src/sass/themes/default.scss +255 -0
- package/src/sass/themes/green.scss +395 -0
- package/src/sass/themes/light.scss +368 -0
- package/src/sass/themes/red.scss +397 -0
- package/src/sass/themes/violet.scss +410 -0
- package/src/sass/variable.scss +84 -0
- package/src/toolbars/Bubble.js +234 -0
- package/src/toolbars/BubbleFormula.js +298 -0
- package/src/toolbars/BubbleTable.js +147 -0
- package/src/toolbars/FloatMenu.js +131 -0
- package/src/toolbars/HiddenToolbar.js +36 -0
- package/src/toolbars/HookCenter.js +234 -0
- package/src/toolbars/MenuBase.js +569 -0
- package/src/toolbars/PreviewerBubble.js +608 -0
- package/src/toolbars/ShortcutKeyConfigPanel.js +345 -0
- package/src/toolbars/Sidebar.js +36 -0
- package/src/toolbars/Toc.js +242 -0
- package/src/toolbars/Toolbar.js +449 -0
- package/src/toolbars/ToolbarRight.js +37 -0
- package/src/toolbars/hooks/Audio.js +79 -0
- package/src/toolbars/hooks/BarTable.js +41 -0
- package/src/toolbars/hooks/Bold.js +73 -0
- package/src/toolbars/hooks/Br.js +34 -0
- package/src/toolbars/hooks/ChangeLocale.js +62 -0
- package/src/toolbars/hooks/ChatGpt.js +182 -0
- package/src/toolbars/hooks/CheckList.js +41 -0
- package/src/toolbars/hooks/Code.js +49 -0
- package/src/toolbars/hooks/CodeTheme.js +66 -0
- package/src/toolbars/hooks/Color.js +298 -0
- package/src/toolbars/hooks/Copy.js +141 -0
- package/src/toolbars/hooks/Detail.js +69 -0
- package/src/toolbars/hooks/DrawIo.js +57 -0
- package/src/toolbars/hooks/Export.js +49 -0
- package/src/toolbars/hooks/File.js +79 -0
- package/src/toolbars/hooks/Formula.js +69 -0
- package/src/toolbars/hooks/FullScreen.js +50 -0
- package/src/toolbars/hooks/Graph.js +263 -0
- package/src/toolbars/hooks/H1.js +71 -0
- package/src/toolbars/hooks/H2.js +71 -0
- package/src/toolbars/hooks/H3.js +71 -0
- package/src/toolbars/hooks/Header.js +118 -0
- package/src/toolbars/hooks/Hr.js +35 -0
- package/src/toolbars/hooks/Image.js +91 -0
- package/src/toolbars/hooks/InlineCode.js +53 -0
- package/src/toolbars/hooks/Insert.js +193 -0
- package/src/toolbars/hooks/Italic.js +72 -0
- package/src/toolbars/hooks/Justify.js +49 -0
- package/src/toolbars/hooks/LineTable.js +41 -0
- package/src/toolbars/hooks/Link.js +49 -0
- package/src/toolbars/hooks/List.js +55 -0
- package/src/toolbars/hooks/MobilePreview.js +44 -0
- package/src/toolbars/hooks/Ol.js +41 -0
- package/src/toolbars/hooks/Panel.js +140 -0
- package/src/toolbars/hooks/Pdf.js +78 -0
- package/src/toolbars/hooks/Publish.js +123 -0
- package/src/toolbars/hooks/QuickTable.js +43 -0
- package/src/toolbars/hooks/Quote.js +45 -0
- package/src/toolbars/hooks/Redo.js +33 -0
- package/src/toolbars/hooks/Ruby.js +59 -0
- package/src/toolbars/hooks/Search.js +53 -0
- package/src/toolbars/hooks/Settings.js +220 -0
- package/src/toolbars/hooks/ShortcutKey.js +62 -0
- package/src/toolbars/hooks/Size.js +118 -0
- package/src/toolbars/hooks/Split.js +37 -0
- package/src/toolbars/hooks/Strikethrough.js +71 -0
- package/src/toolbars/hooks/Sub.js +58 -0
- package/src/toolbars/hooks/Sup.js +58 -0
- package/src/toolbars/hooks/SwitchModel.js +56 -0
- package/src/toolbars/hooks/Table.js +56 -0
- package/src/toolbars/hooks/Theme.js +62 -0
- package/src/toolbars/hooks/Toc.js +35 -0
- package/src/toolbars/hooks/TogglePreview.js +91 -0
- package/src/toolbars/hooks/Ul.js +41 -0
- package/src/toolbars/hooks/Underline.js +68 -0
- package/src/toolbars/hooks/Undo.js +30 -0
- package/src/toolbars/hooks/Video.js +79 -0
- package/src/toolbars/hooks/Word.js +78 -0
- package/src/toolbars/hooks/WordCount.js +106 -0
- package/src/utils/autoindent.js +58 -0
- package/src/utils/cm-search-replace.js +794 -0
- package/src/utils/code-preview-language-setting.js +180 -0
- package/src/utils/codeBlockContentHandler.js +400 -0
- package/src/utils/config.js +174 -0
- package/src/utils/copy.js +55 -0
- package/src/utils/dialog.js +214 -0
- package/src/utils/dom.js +163 -0
- package/src/utils/downloadUtil.js +23 -0
- package/src/utils/env.js +22 -0
- package/src/utils/error.js +61 -0
- package/src/utils/event.js +38 -0
- package/src/utils/export.js +166 -0
- package/src/utils/file.js +164 -0
- package/src/utils/formulaUtilsHandler.js +232 -0
- package/src/utils/htmlparser.js +976 -0
- package/src/utils/image.js +99 -0
- package/src/utils/imgSizeHandler.js +279 -0
- package/src/utils/lazyLoadImg.js +327 -0
- package/src/utils/lineFeed.js +49 -0
- package/src/utils/listContentHandler.js +227 -0
- package/src/utils/lookbehind-replace.js +81 -0
- package/src/utils/mathjax.js +89 -0
- package/src/utils/myersDiff.js +211 -0
- package/src/utils/pasteHelper.js +253 -0
- package/src/utils/platformTransform.js +71 -0
- package/src/utils/recount-pos.js +59 -0
- package/src/utils/regexp.js +295 -0
- package/src/utils/sanitize.js +477 -0
- package/src/utils/selection.js +50 -0
- package/src/utils/shortcutKey.js +291 -0
- package/src/utils/svgUtils.js +96 -0
- package/src/utils/tableContentHandler.js +876 -0
- package/test/core/CommonMark.spec.ts +62 -0
- package/test/core/hooks/AutoLink.spec.ts +28 -0
- package/test/core/hooks/List.spec.ts +79 -0
- package/test/core/hooks/__snapshots__/List.spec.ts.snap +11 -0
- package/test/example.md +778 -0
- package/test/node.js +10 -0
- package/test/suites/commonmark.spec.json +5218 -0
- package/test/tsconfig.test.json +6 -0
- package/test/utils/regexp.spec.ts +28 -0
- package/types/cherry.d.ts +675 -0
- package/types/codemirror.d.ts +22 -0
- package/types/editor.d.ts +72 -0
- package/types/global.d.ts +16 -0
- package/types/menus.d.ts +24 -0
- package/types/previewer.d.ts +53 -0
- package/types/syntax.d.ts +52 -0
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import { mac } from 'codemirror/src/util/browser';
|
|
2
|
+
import {
|
|
3
|
+
getAllowedShortcutKey,
|
|
4
|
+
keyStackIsModifierkeys,
|
|
5
|
+
ENTER_KEY,
|
|
6
|
+
BACKSPACE_KEY,
|
|
7
|
+
keyStack2UniqueString,
|
|
8
|
+
shortcutCode2Key,
|
|
9
|
+
keyStack2UnPlatformUniqueString,
|
|
10
|
+
isEnableShortcutKey,
|
|
11
|
+
setDisableShortcutKey,
|
|
12
|
+
storageKeyMap,
|
|
13
|
+
} from '@/utils/shortcutKey';
|
|
14
|
+
import { createElement } from '@/utils/dom';
|
|
15
|
+
/**
|
|
16
|
+
* 隐藏输入框,展示快捷键配置项
|
|
17
|
+
* @param {Element} inputWrapper
|
|
18
|
+
* @param {HTMLElement} shortcutPanel
|
|
19
|
+
*/
|
|
20
|
+
function hiddenInputWrapper(inputWrapper, shortcutPanel) {
|
|
21
|
+
inputWrapper.setAttribute('style', 'display: none;');
|
|
22
|
+
shortcutPanel.style.display = 'flex';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default class ShortcutKeyConfigPanel {
|
|
26
|
+
/**
|
|
27
|
+
*
|
|
28
|
+
* @param {Partial<import('@/Cherry').default> & {$currentMenuOptions?:import('~types/menus').CustomMenuConfig}} $cherry
|
|
29
|
+
*/
|
|
30
|
+
constructor($cherry) {
|
|
31
|
+
this.$cherry = $cherry;
|
|
32
|
+
this.shortcutUlClassName = 'cherry-shortcut-key-config-panel-ul';
|
|
33
|
+
this.shortcutUlId = this.shortcutUlClassName;
|
|
34
|
+
this.shortcutConfigPanelKbdClassName = 'shortcut-key-config-panel-kbd';
|
|
35
|
+
this.shortcutKeyboardKeyClassName = 'keyboard-key';
|
|
36
|
+
// 双击快捷键区域
|
|
37
|
+
this.handleDbClick = (/** @type {MouseEvent} */ e) => {
|
|
38
|
+
if (!isEnableShortcutKey(this.$cherry.nameSpace)) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (e.target instanceof HTMLElement) {
|
|
42
|
+
if (
|
|
43
|
+
e.target.classList.contains(this.shortcutConfigPanelKbdClassName) ||
|
|
44
|
+
e.target.classList.contains(this.shortcutKeyboardKeyClassName)
|
|
45
|
+
) {
|
|
46
|
+
const shortcutPanel = e.target.classList.contains(this.shortcutConfigPanelKbdClassName)
|
|
47
|
+
? e.target
|
|
48
|
+
: e.target.parentElement;
|
|
49
|
+
// 隐藏展示快捷键的容器
|
|
50
|
+
shortcutPanel.style.display = 'none';
|
|
51
|
+
const inputWrapper = shortcutPanel.nextElementSibling;
|
|
52
|
+
// 显示输入框
|
|
53
|
+
inputWrapper.setAttribute('style', 'display: block;');
|
|
54
|
+
const inputElement = inputWrapper.querySelector('input');
|
|
55
|
+
const placeholder = [];
|
|
56
|
+
shortcutPanel.childNodes.forEach((element) => {
|
|
57
|
+
// @ts-ignore
|
|
58
|
+
placeholder.push(element.innerText);
|
|
59
|
+
});
|
|
60
|
+
inputElement.placeholder = placeholder.join('-');
|
|
61
|
+
// 获取焦点
|
|
62
|
+
inputElement.focus();
|
|
63
|
+
inputElement.onblur = () => {
|
|
64
|
+
hiddenInputWrapper(inputWrapper, shortcutPanel);
|
|
65
|
+
inputElement.value = '';
|
|
66
|
+
};
|
|
67
|
+
/** @type {string[]} 按下的快捷键栈 */
|
|
68
|
+
let keyStack = [];
|
|
69
|
+
inputElement.onkeydown = (/** @type {KeyboardEvent} */ e) => {
|
|
70
|
+
e.preventDefault();
|
|
71
|
+
e.stopPropagation();
|
|
72
|
+
if (e.key === ENTER_KEY || e.key === BACKSPACE_KEY) {
|
|
73
|
+
if (e.key === ENTER_KEY) {
|
|
74
|
+
const { hookname = '' } = shortcutPanel.parentElement?.dataset ?? {};
|
|
75
|
+
const oldShortcutKeys = [];
|
|
76
|
+
for (let i = 0; i < shortcutPanel.children.length; i++) {
|
|
77
|
+
/** @type {HTMLDivElement} */
|
|
78
|
+
// @ts-ignore
|
|
79
|
+
const element = shortcutPanel.children.item(i);
|
|
80
|
+
const { code } = element.dataset ?? {};
|
|
81
|
+
if (code) {
|
|
82
|
+
oldShortcutKeys.push(code);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// 防止修改
|
|
86
|
+
if (oldShortcutKeys.length === shortcutPanel.children.length) {
|
|
87
|
+
// 旧的shortcutKey用于在更新时比较,删除旧值
|
|
88
|
+
const oldShortcutKey = keyStack2UniqueString(oldShortcutKeys);
|
|
89
|
+
if (hookname) {
|
|
90
|
+
// 触发更新快捷键
|
|
91
|
+
this.$cherry?.toolbar?.updateShortcutKeyMap(oldShortcutKey, keyStack2UniqueString(keyStack));
|
|
92
|
+
// 取二者较大者
|
|
93
|
+
const endIndex = Math.max(keyStack.length, shortcutPanel.children.length);
|
|
94
|
+
// 更新界面展示的快捷键
|
|
95
|
+
for (let i = 0; i < endIndex; i++) {
|
|
96
|
+
const element = shortcutPanel.children.item(i);
|
|
97
|
+
// 如果当前快捷键栈不存在了,说明新的快捷键个数比上一次少,则应该删除当前element
|
|
98
|
+
if (!keyStack[i] && element) {
|
|
99
|
+
element.remove();
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
const matchRes = shortcutCode2Key(keyStack[i], mac);
|
|
103
|
+
if (element) {
|
|
104
|
+
element.setAttribute('title', matchRes.tip);
|
|
105
|
+
element.textContent = matchRes.text;
|
|
106
|
+
element.setAttribute('data-code', keyStack[i]);
|
|
107
|
+
} else {
|
|
108
|
+
const matchRes = shortcutCode2Key(keyStack[i], mac);
|
|
109
|
+
const kbd = createElement('span', this.shortcutKeyboardKeyClassName, {
|
|
110
|
+
title: matchRes.tip,
|
|
111
|
+
});
|
|
112
|
+
kbd.setAttribute('data-code', keyStack[i]);
|
|
113
|
+
kbd.innerText = matchRes.text;
|
|
114
|
+
shortcutPanel.appendChild(kbd);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
hiddenInputWrapper(inputWrapper, shortcutPanel);
|
|
120
|
+
} else {
|
|
121
|
+
if (keyStack.length === 0) {
|
|
122
|
+
hiddenInputWrapper(inputWrapper, shortcutPanel);
|
|
123
|
+
}
|
|
124
|
+
// 退栈
|
|
125
|
+
keyStack.pop();
|
|
126
|
+
inputElement.value = keyStack2UnPlatformUniqueString(keyStack, mac);
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
keyStack = getAllowedShortcutKey(e);
|
|
130
|
+
if (!keyStackIsModifierkeys(keyStack) && Array.isArray(keyStack) && keyStack.length >= 2) {
|
|
131
|
+
inputElement.value = keyStack2UnPlatformUniqueString(keyStack, mac);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
this.clickSettingsDisableBtn = () => {
|
|
139
|
+
if (!isEnableShortcutKey(this.$cherry.nameSpace)) {
|
|
140
|
+
setDisableShortcutKey(this.$cherry.nameSpace, 'enable');
|
|
141
|
+
this.dom.classList.remove('disable');
|
|
142
|
+
this.$cherry.editor.disableShortcut(false);
|
|
143
|
+
} else {
|
|
144
|
+
setDisableShortcutKey(this.$cherry.nameSpace, 'disable');
|
|
145
|
+
this.dom.classList.add('disable');
|
|
146
|
+
this.$cherry.editor.disableShortcut(true);
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
this.clickSettingsRecoverBtn = () => {
|
|
150
|
+
setDisableShortcutKey(this.$cherry.nameSpace, 'enable');
|
|
151
|
+
this.dom.classList.remove('disable');
|
|
152
|
+
this.$cherry.editor.disableShortcut(false);
|
|
153
|
+
this.$cherry.toolbar.shortcutKeyMap = {};
|
|
154
|
+
this.$cherry.toolbar.collectShortcutKey(false);
|
|
155
|
+
storageKeyMap(this.$cherry.nameSpace, this.$cherry.toolbar.shortcutKeyMap);
|
|
156
|
+
this.dom.innerHTML = this.generateShortcutKeyConfigPanelHtmlStr();
|
|
157
|
+
this.show();
|
|
158
|
+
};
|
|
159
|
+
this.init();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
init() {
|
|
163
|
+
if (this.$cherry?.toolbar?.shortcutKeyMap) {
|
|
164
|
+
this.dom = document.createElement('div');
|
|
165
|
+
this.dom.className = [
|
|
166
|
+
'cherry-dropdown',
|
|
167
|
+
'cherry-shortcut-key-config-panel',
|
|
168
|
+
'cherry-shortcut-key-config-panel-wrapper',
|
|
169
|
+
].join(' ');
|
|
170
|
+
this.dom.innerHTML = this.generateShortcutKeyConfigPanelHtmlStr();
|
|
171
|
+
// 实例化后,将容器插入到富文本编辑器中,默认隐藏
|
|
172
|
+
this.dom.style.display = 'none';
|
|
173
|
+
if (!isEnableShortcutKey(this.$cherry.nameSpace)) {
|
|
174
|
+
this.dom.classList.add('disable');
|
|
175
|
+
}
|
|
176
|
+
this.$cherry.wrapperDom.append(this.dom);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
generateShortcutKeyConfigPanelHtmlStr() {
|
|
181
|
+
const liStr = Object.entries(this.$cherry.toolbar.shortcutKeyMap ?? {})
|
|
182
|
+
.filter(([key, val]) => typeof val === 'object' && val)
|
|
183
|
+
.map(([key, val]) => {
|
|
184
|
+
const { hookName, aliasName, ...rest } = val;
|
|
185
|
+
let otherDataSet = '';
|
|
186
|
+
if (rest && typeof rest === 'object') {
|
|
187
|
+
otherDataSet = Object.entries(rest)
|
|
188
|
+
.map(([otherField, fieldValue]) => `data-${otherField}=${fieldValue}`)
|
|
189
|
+
.join(' ');
|
|
190
|
+
}
|
|
191
|
+
return `<li class="cherry-dropdown-item shortcut-key-item" data-hookname=${hookName} ${otherDataSet}>
|
|
192
|
+
<div class="shortcut-key-config-panel-name">${aliasName}</div>
|
|
193
|
+
<div class="${this.shortcutConfigPanelKbdClassName}">${key
|
|
194
|
+
?.split('-')
|
|
195
|
+
.map((singalKey) => {
|
|
196
|
+
const matchRes = shortcutCode2Key(singalKey, mac);
|
|
197
|
+
const shortKey = matchRes ?? {
|
|
198
|
+
text: singalKey,
|
|
199
|
+
tip: singalKey,
|
|
200
|
+
};
|
|
201
|
+
return `<span class="${this.shortcutKeyboardKeyClassName}" title="${shortKey.tip}" data-code="${singalKey}">${shortKey.text}</span>`;
|
|
202
|
+
})
|
|
203
|
+
.join('')}</div>
|
|
204
|
+
<div style="display: none;" class="input-shortcut-wrapper"><input type="text" /></div>
|
|
205
|
+
</li>`;
|
|
206
|
+
})
|
|
207
|
+
.join('');
|
|
208
|
+
// <div class="cherry-dropdown-item">
|
|
209
|
+
// <input type="checkbox" id="enableMacControl" name="enableMacControl" checked />
|
|
210
|
+
// <label for="enableMacControl">启用Mac平台的Control键</label>
|
|
211
|
+
// </div>
|
|
212
|
+
const ulStr = `
|
|
213
|
+
<div class="cherry-shortcut-key-config-panel-inner">
|
|
214
|
+
<div class="shortcut-panel-settings">
|
|
215
|
+
<btn class="shortcut-settings-btn j-shortcut-settings-disable-btn"><i class="ch-icon ch-icon-cherry-table-delete"></i> ${
|
|
216
|
+
this.$cherry.locale.disableShortcut
|
|
217
|
+
}</btn>
|
|
218
|
+
<btn class="shortcut-settings-btn j-shortcut-settings-recover-btn"><i class="ch-icon ch-icon-undo"></i> ${
|
|
219
|
+
this.$cherry.locale.recoverShortcut
|
|
220
|
+
}</btn>
|
|
221
|
+
</div>
|
|
222
|
+
<div class="shortcut-panel-title">${this.$cherry.locale.editShortcutKeyConfigTip}</div>
|
|
223
|
+
<ul class="${this.shortcutUlClassName}" id="${this.shortcutUlId}">${liStr}</ul>
|
|
224
|
+
${this.$getStaticShortcut()}
|
|
225
|
+
</div>`;
|
|
226
|
+
return ulStr;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* 定义不支持修改的快捷键信息(是codemirror提供的类sublime快捷键)
|
|
231
|
+
*/
|
|
232
|
+
$getStaticShortcut() {
|
|
233
|
+
if (this.$cherry.options.editor.keyMap === 'vim') {
|
|
234
|
+
return '';
|
|
235
|
+
}
|
|
236
|
+
const list = [
|
|
237
|
+
{ name: this.$cherry.locale.shortcutStatic1, key: 'Ctrl+[' },
|
|
238
|
+
{ name: this.$cherry.locale.shortcutStatic2, key: 'Ctrl+]' },
|
|
239
|
+
{ name: this.$cherry.locale.shortcutStatic3, key: 'Ctrl+Shift+D' },
|
|
240
|
+
{ name: this.$cherry.locale.shortcutStatic4, key: 'Ctrl+Enter' },
|
|
241
|
+
{ name: this.$cherry.locale.shortcutStatic5, key: 'Ctrl+Shift+Enter' },
|
|
242
|
+
{ name: this.$cherry.locale.shortcutStatic6, key: 'Ctrl+Shift+↑' },
|
|
243
|
+
{ name: this.$cherry.locale.shortcutStatic7, key: 'Ctrl+Shift+↓' },
|
|
244
|
+
{ name: this.$cherry.locale.shortcutStatic8, key: 'Ctrl+Shift+K' },
|
|
245
|
+
{ name: this.$cherry.locale.shortcutStatic9, key: 'Ctrl+Shift+←' },
|
|
246
|
+
{ name: this.$cherry.locale.shortcutStatic10, key: 'Ctrl+Shift+→' },
|
|
247
|
+
{ name: this.$cherry.locale.shortcutStatic11, key: 'Ctrl+Backspace' },
|
|
248
|
+
{ name: this.$cherry.locale.shortcutStatic12, key: 'Ctrl+Shift+M' },
|
|
249
|
+
{ name: this.$cherry.locale.shortcutStatic13, key: `Ctrl+${this.$cherry.locale.leftMouseButton}` },
|
|
250
|
+
{ name: this.$cherry.locale.shortcutStatic14, key: 'Ctrl+Shift+L' },
|
|
251
|
+
{ name: this.$cherry.locale.shortcutStatic16, key: 'Alt+F3' },
|
|
252
|
+
{ name: this.$cherry.locale.shortcutStatic17, key: 'Ctrl+Z' },
|
|
253
|
+
{ name: this.$cherry.locale.shortcutStatic18, key: 'Ctrl+Y' },
|
|
254
|
+
];
|
|
255
|
+
const li = [];
|
|
256
|
+
for (let i = 0; i < list.length; i++) {
|
|
257
|
+
const one = list[i];
|
|
258
|
+
li.push(`
|
|
259
|
+
<li class="cherry-dropdown-item shortcut-key-item">
|
|
260
|
+
<div class="shortcut-key-config-panel-name">${one.name}</div>
|
|
261
|
+
<div class="shortcut-key-config-panel-static">${one.key.replace(
|
|
262
|
+
/\+/g,
|
|
263
|
+
'<span class="shortcut-split">+</span>',
|
|
264
|
+
)}
|
|
265
|
+
</div>
|
|
266
|
+
</li>
|
|
267
|
+
`);
|
|
268
|
+
}
|
|
269
|
+
return `<div class="shortcut-static">
|
|
270
|
+
<div class="shortcut-panel-title">${this.$cherry.locale.shortcutStaticTitle}</div>
|
|
271
|
+
<ul class="cherry-shortcut-key-config-panel-ul">${li.join('')}</ul>
|
|
272
|
+
</div>`;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* 显示快捷键配置面板
|
|
277
|
+
*/
|
|
278
|
+
show() {
|
|
279
|
+
this.dom.style.removeProperty('display');
|
|
280
|
+
const ulWrapper = this.dom.querySelector(`#${this.shortcutUlId}`);
|
|
281
|
+
if (ulWrapper instanceof HTMLUListElement) {
|
|
282
|
+
// 监听双击
|
|
283
|
+
ulWrapper.addEventListener('dblclick', this.handleDbClick);
|
|
284
|
+
}
|
|
285
|
+
const settingsDisableBtn = this.dom.querySelector('.j-shortcut-settings-disable-btn');
|
|
286
|
+
if (settingsDisableBtn instanceof HTMLElement) {
|
|
287
|
+
settingsDisableBtn.addEventListener('click', this.clickSettingsDisableBtn);
|
|
288
|
+
}
|
|
289
|
+
const settingsRecoverBtn = this.dom.querySelector('.j-shortcut-settings-recover-btn');
|
|
290
|
+
if (settingsRecoverBtn instanceof HTMLElement) {
|
|
291
|
+
settingsRecoverBtn.addEventListener('click', this.clickSettingsRecoverBtn);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
hide() {
|
|
296
|
+
this.dom.style.display = 'none';
|
|
297
|
+
const ulWrapper = this.dom.querySelector(`#${this.shortcutUlId}`);
|
|
298
|
+
if (ulWrapper instanceof HTMLUListElement) {
|
|
299
|
+
// 销毁时取消监听
|
|
300
|
+
ulWrapper.removeEventListener('dblclick', this.handleDbClick);
|
|
301
|
+
}
|
|
302
|
+
const settingsDisableBtn = this.dom.querySelector('.j-shortcut-settings-disable-btn');
|
|
303
|
+
if (settingsDisableBtn instanceof HTMLElement) {
|
|
304
|
+
settingsDisableBtn.removeEventListener('click', this.clickSettingsDisableBtn);
|
|
305
|
+
}
|
|
306
|
+
const settingsRecoverBtn = this.dom.querySelector('.j-shortcut-settings-recover-btn');
|
|
307
|
+
if (settingsRecoverBtn instanceof HTMLElement) {
|
|
308
|
+
settingsRecoverBtn.removeEventListener('click', this.clickSettingsRecoverBtn);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
isShow() {
|
|
313
|
+
return this.dom.style.display === 'block';
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
isHide() {
|
|
317
|
+
return this.dom.style.display === 'none';
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* 展示/隐藏快捷键配置面板
|
|
322
|
+
* @param {HTMLElement} settingsDom
|
|
323
|
+
*/
|
|
324
|
+
toggle(settingsDom) {
|
|
325
|
+
if (!(settingsDom instanceof HTMLElement)) {
|
|
326
|
+
throw new Error(`settingsDom must be an instance of HTMLElement, but got: ${settingsDom}`);
|
|
327
|
+
}
|
|
328
|
+
const pos = settingsDom.getBoundingClientRect();
|
|
329
|
+
const cherryWrapPos = this.$cherry.wrapperDom.getBoundingClientRect();
|
|
330
|
+
if (this.isHide()) {
|
|
331
|
+
this.dom.style.left = `${pos.left - cherryWrapPos.left + pos.width / 2}px`;
|
|
332
|
+
this.dom.style.top = `${pos.top - cherryWrapPos.top + pos.height}px`;
|
|
333
|
+
this.show();
|
|
334
|
+
const me = this.dom.getBoundingClientRect();
|
|
335
|
+
this.dom.style.marginLeft = `0px`;
|
|
336
|
+
this.dom.style.left = `${pos.left - cherryWrapPos.left + pos.width / 2 - me.width / 2}px`;
|
|
337
|
+
// 如果弹窗位置超出屏幕,则自动调整位置
|
|
338
|
+
if (me.left + me.width > window.innerWidth) {
|
|
339
|
+
this.dom.style.left = `${window.innerWidth - me.width - 5}px`;
|
|
340
|
+
}
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
return this.hide();
|
|
344
|
+
}
|
|
345
|
+
}
|
|
@@ -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 Toolbar from './Toolbar';
|
|
17
|
+
/**
|
|
18
|
+
* 预览区域右侧悬浮的工具栏
|
|
19
|
+
* 推荐放置跟编辑区域完全无关的工具栏
|
|
20
|
+
* 比如复制预览区域内容、修改预览区域主题等
|
|
21
|
+
*/
|
|
22
|
+
export default class Sidebar extends Toolbar {
|
|
23
|
+
// constructor(options) {
|
|
24
|
+
// super(options);
|
|
25
|
+
// }
|
|
26
|
+
appendMenusToDom(menus) {
|
|
27
|
+
this.options.dom.appendChild(menus);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
init() {
|
|
31
|
+
super.init();
|
|
32
|
+
Object.entries(this.shortcutKeyMap).forEach(([key, value]) => {
|
|
33
|
+
this.$cherry.toolbar.shortcutKeyMap[key] = value;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,242 @@
|
|
|
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 { createElement } from '../utils/dom';
|
|
17
|
+
/**
|
|
18
|
+
* 悬浮目录
|
|
19
|
+
*/
|
|
20
|
+
export default class Toc {
|
|
21
|
+
constructor(options) {
|
|
22
|
+
this.$cherry = options.$cherry;
|
|
23
|
+
this.editor = options.$cherry.editor.editor;
|
|
24
|
+
this.tocStr = '';
|
|
25
|
+
this.updateLocationHash = options.updateLocationHash ?? true;
|
|
26
|
+
this.defaultModel = options.defaultModel ?? 'full';
|
|
27
|
+
this.showAutoNumber = options.showAutoNumber ?? false;
|
|
28
|
+
this.position = options.position ?? 'absolute';
|
|
29
|
+
this.cssText = options.cssText ?? '';
|
|
30
|
+
this.init();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
init() {
|
|
34
|
+
this.drawDom();
|
|
35
|
+
this.timer = setTimeout(() => {
|
|
36
|
+
this.updateTocList();
|
|
37
|
+
}, 300);
|
|
38
|
+
this.editor.on('change', (codemirror, evt) => {
|
|
39
|
+
clearTimeout(this.timer);
|
|
40
|
+
this.timer = setTimeout(() => {
|
|
41
|
+
this.updateTocList();
|
|
42
|
+
this.$switchModel(this.model);
|
|
43
|
+
}, 300);
|
|
44
|
+
});
|
|
45
|
+
this.$switchModel(this.getModelFromLocalStorage());
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
getModelFromLocalStorage() {
|
|
49
|
+
if (typeof localStorage === 'undefined') {
|
|
50
|
+
return this.defaultModel;
|
|
51
|
+
}
|
|
52
|
+
return localStorage.getItem('cherry-toc-model') || this.defaultModel;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
setModelToLocalStorage(model) {
|
|
56
|
+
if (typeof localStorage === 'undefined') {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
localStorage.setItem('cherry-toc-model', model);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
drawDom() {
|
|
63
|
+
const tocDom = createElement(
|
|
64
|
+
'div',
|
|
65
|
+
`cherry-flex-toc cherry-flex-toc__pure${this.showAutoNumber ? ' auto-num' : ''}`,
|
|
66
|
+
);
|
|
67
|
+
if (this.position === 'fixed') {
|
|
68
|
+
tocDom.classList.add('cherry-flex-toc__fixed');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (this.cssText.length > 0) {
|
|
72
|
+
tocDom.style.cssText = this.cssText;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const tocHead = createElement('div', 'cherry-toc-head');
|
|
76
|
+
const tocTitle = createElement('span', 'cherry-toc-title');
|
|
77
|
+
tocTitle.append(this.$cherry.locale.toc);
|
|
78
|
+
const tocClose = createElement('i', 'ch-icon ch-icon-chevronsRight');
|
|
79
|
+
const tocOpen = createElement('i', 'ch-icon ch-icon-chevronsLeft');
|
|
80
|
+
this.tocClose = tocClose;
|
|
81
|
+
this.tocOpen = tocOpen;
|
|
82
|
+
tocHead.appendChild(tocTitle);
|
|
83
|
+
tocHead.appendChild(tocClose);
|
|
84
|
+
tocHead.appendChild(tocOpen);
|
|
85
|
+
tocDom.appendChild(tocHead);
|
|
86
|
+
const tocListDom = createElement('div', 'cherry-toc-list');
|
|
87
|
+
this.tocListDom = tocListDom;
|
|
88
|
+
tocDom.appendChild(tocListDom);
|
|
89
|
+
this.tocDom = tocDom;
|
|
90
|
+
this.$cherry.wrapperDom.appendChild(tocDom);
|
|
91
|
+
this.bindClickEvent();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
bindClickEvent() {
|
|
95
|
+
this.tocDom.addEventListener('click', (e) => {
|
|
96
|
+
const a = this.$getClosestNode(e.target, 'A');
|
|
97
|
+
if (a === false) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (/cherry-toc-one-a/.test(a.className)) {
|
|
101
|
+
const { id, index } = a.dataset;
|
|
102
|
+
if (this.$cherry.status.previewer === 'hide') {
|
|
103
|
+
// editorOnly模式下,需要定位到编辑区对应位置
|
|
104
|
+
const searcher = this.$cherry.editor.editor.getSearchCursor(
|
|
105
|
+
/(?:^|\n)\n*((?:[ \t\u00a0]*#{1,6}).+?|(?:[ \t\u00a0]*.+)\n(?:[ \t\u00a0]*[=]+|[-]+))(?=$|\n)/g,
|
|
106
|
+
);
|
|
107
|
+
for (let i = 0; i <= index; i++) {
|
|
108
|
+
searcher.findNext();
|
|
109
|
+
}
|
|
110
|
+
const target = searcher.from();
|
|
111
|
+
this.$cherry.editor.scrollToLineNum(target.line, target.line + 1, 0);
|
|
112
|
+
} else {
|
|
113
|
+
// 有预览的情况下,直接通过滚动预览区位置实现滚动到锚点
|
|
114
|
+
this.$cherry.previewer.scrollToHeadByIndex(index);
|
|
115
|
+
}
|
|
116
|
+
if (this.updateLocationHash) {
|
|
117
|
+
location.href = id;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
this.tocClose.addEventListener('click', (e) => {
|
|
122
|
+
this.$switchModel('pure');
|
|
123
|
+
this.setModelToLocalStorage('pure');
|
|
124
|
+
});
|
|
125
|
+
this.tocOpen.addEventListener('click', (e) => {
|
|
126
|
+
this.$switchModel('full');
|
|
127
|
+
this.setModelToLocalStorage('full');
|
|
128
|
+
});
|
|
129
|
+
if (window) {
|
|
130
|
+
window.addEventListener('resize', () => {
|
|
131
|
+
this.$switchModel(this.model);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
this.editor.on('scroll', (codemirror, evt) => {
|
|
135
|
+
this.updateTocList(true);
|
|
136
|
+
});
|
|
137
|
+
const scrollDom = this.$cherry.previewer.getDomCanScroll();
|
|
138
|
+
if (scrollDom.nodeName === 'HTML') {
|
|
139
|
+
window.addEventListener('scroll', () => {
|
|
140
|
+
this.updateTocList(true);
|
|
141
|
+
});
|
|
142
|
+
} else {
|
|
143
|
+
scrollDom.addEventListener('scroll', () => {
|
|
144
|
+
this.updateTocList(true);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
$switchModel(model = 'pure') {
|
|
150
|
+
this.model = model;
|
|
151
|
+
const targetClassName = `cherry-flex-toc__${model}`;
|
|
152
|
+
if (!this.tocDom.classList.contains(targetClassName)) {
|
|
153
|
+
this.tocDom.classList.remove(`cherry-flex-toc__pure`);
|
|
154
|
+
this.tocDom.classList.remove(`cherry-flex-toc__full`);
|
|
155
|
+
this.tocDom.classList.add(targetClassName);
|
|
156
|
+
}
|
|
157
|
+
const list = this.tocListDom.querySelectorAll('.cherry-toc-one-a');
|
|
158
|
+
if (list.length > 0) {
|
|
159
|
+
let targetHeight = 28;
|
|
160
|
+
if (model === 'pure') {
|
|
161
|
+
const { height } = this.tocListDom.getBoundingClientRect();
|
|
162
|
+
const minHeight = Math.floor((height - list.length * 3) / list.length);
|
|
163
|
+
// eslint-disable-next-line no-nested-ternary
|
|
164
|
+
targetHeight = minHeight < 3 ? 3 : minHeight > 10 ? 10 : minHeight;
|
|
165
|
+
}
|
|
166
|
+
for (let i = 0; i < list.length; i++) {
|
|
167
|
+
// @ts-ignore
|
|
168
|
+
if (list[i].style.height !== `${targetHeight}px`) {
|
|
169
|
+
// @ts-ignore
|
|
170
|
+
list[i].style.height = `${targetHeight}px`;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
$getClosestNode(node, targetNodeName) {
|
|
177
|
+
if (!node || !node.tagName) {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
if (node.tagName === targetNodeName) {
|
|
181
|
+
return node;
|
|
182
|
+
}
|
|
183
|
+
if (node.parentNode.tagName === 'BODY') {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
return this.$getClosestNode(node.parentNode, targetNodeName);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
updateTocList(onlyScroll = false) {
|
|
190
|
+
if (onlyScroll === true) {
|
|
191
|
+
// do nothing
|
|
192
|
+
} else {
|
|
193
|
+
const tocList = this.$cherry.getToc();
|
|
194
|
+
let tocStr = '';
|
|
195
|
+
tocList.map((item) => {
|
|
196
|
+
tocStr += item.text;
|
|
197
|
+
return item;
|
|
198
|
+
});
|
|
199
|
+
tocStr = this.$cherry.engine.hash(tocStr);
|
|
200
|
+
if (this.tocStr !== tocStr) {
|
|
201
|
+
this.tocStr = tocStr;
|
|
202
|
+
let tocHtml = '';
|
|
203
|
+
let index = 0;
|
|
204
|
+
tocList.map((item) => {
|
|
205
|
+
const text = item.text.replace(/<a .+?<\/a>/g, '');
|
|
206
|
+
const title = text.replace(/<[^>]+?>/g, '');
|
|
207
|
+
tocHtml += `<a class="cherry-toc-one-a cherry-toc-one-a__${item.level > 5 ? 5 : item.level}"
|
|
208
|
+
title="${title}"
|
|
209
|
+
data-index="${index}"
|
|
210
|
+
data-id="#${item.id}"
|
|
211
|
+
>${text}</a>`;
|
|
212
|
+
index += 1;
|
|
213
|
+
return item;
|
|
214
|
+
});
|
|
215
|
+
this.tocListDom.innerHTML = tocHtml;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
// 处理当前标题的高亮
|
|
219
|
+
if (this.$cherry.status.previewer === 'hide') {
|
|
220
|
+
// 似乎没有特别好的办法,先欠着
|
|
221
|
+
} else {
|
|
222
|
+
const scrollDom = this.$cherry.previewer.getDomCanScroll();
|
|
223
|
+
const minY = scrollDom.nodeName === 'HTML' ? 0 : scrollDom.getBoundingClientRect().y;
|
|
224
|
+
const headList = this.$cherry.previewer.getDomContainer().querySelectorAll('h1,h2,h3,h4,h5,h6,h7,h8');
|
|
225
|
+
let index = 0;
|
|
226
|
+
for (; index < headList.length; index++) {
|
|
227
|
+
const { y } = headList[index].getBoundingClientRect();
|
|
228
|
+
if (y > minY + 20) {
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
index = index > 0 ? index - 1 : index;
|
|
233
|
+
this.tocListDom.querySelectorAll('.cherry-toc-one-a').forEach((item, key) => {
|
|
234
|
+
if (key === index) {
|
|
235
|
+
item.classList.add('current');
|
|
236
|
+
} else {
|
|
237
|
+
item.classList.remove('current');
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|