* 设置源码模式
(enabled: boolean)
| 751 | * 设置源码模式 |
| 752 | */ |
| 753 | private setSourceViewMode(enabled: boolean): void { |
| 754 | if (this.sourceViewMode === enabled) return; |
| 755 | this.sourceViewMode = enabled; |
| 756 | |
| 757 | if (enabled) { |
| 758 | // 进入源码模式 |
| 759 | this.dom.classList.add("source-view"); |
| 760 | |
| 761 | // 隐藏头部 |
| 762 | if (this.headerElement) { |
| 763 | this.headerElement.style.display = "none"; |
| 764 | } |
| 765 | |
| 766 | // 隐藏 Mermaid 预览 |
| 767 | if (this.mermaidPreview) { |
| 768 | this.mermaidPreview.style.display = "none"; |
| 769 | } |
| 770 | |
| 771 | // 隐藏 CodeMirror |
| 772 | if (this.editorContainer) { |
| 773 | this.editorContainer.style.display = "none"; |
| 774 | } |
| 775 | |
| 776 | // 创建源码容器(拆分成多行以参与行号计数) |
| 777 | if (!this.sourceTextElement) { |
| 778 | this.sourceTextElement = document.createElement("div"); |
| 779 | this.sourceTextElement.className = "milkup-code-block-source-container"; |
| 780 | this.sourceTextElement.contentEditable = "true"; |
| 781 | this.sourceTextElement.spellcheck = false; |
| 782 | this.sourceTextElement.style.cssText = ` |
| 783 | position: relative; |
| 784 | font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace; |
| 785 | font-size: 14px; |
| 786 | line-height: 1.6; |
| 787 | padding: 0; |
| 788 | margin: 0; |
| 789 | color: inherit; |
| 790 | background: transparent; |
| 791 | border: none; |
| 792 | outline: none; |
| 793 | `; |
| 794 | |
| 795 | // 监听输入事件 |
| 796 | this.sourceTextElement.addEventListener("input", () => { |
| 797 | this.handleSourceTextInput(); |
| 798 | }); |
| 799 | |
| 800 | // 监听键盘事件 |
| 801 | this.sourceTextElement.addEventListener("keydown", (e) => { |
| 802 | this.handleSourceTextKeydown(e); |
| 803 | }); |
| 804 | |
| 805 | // 阻止默认的粘贴行为,使用纯文本粘贴 |
| 806 | this.sourceTextElement.addEventListener("paste", (e) => { |
| 807 | e.preventDefault(); |
| 808 | const text = e.clipboardData?.getData("text/plain") || ""; |
| 809 | document.execCommand("insertText", false, text); |
| 810 | }); |
no test coverage detected