|
| 1 | +import {EditorView, ViewPlugin, ViewUpdate} from '@codemirror/view' |
| 2 | + |
| 3 | +export function useCodeMirrorSpaceOmission() |
| 4 | +{ |
| 5 | + const spaceOmissionPlugin = ViewPlugin.fromClass(class |
| 6 | + { |
| 7 | + private styleElement: HTMLStyleElement | null = null |
| 8 | + |
| 9 | + constructor(private view: EditorView) |
| 10 | + { |
| 11 | + this.updateCSS() |
| 12 | + } |
| 13 | + |
| 14 | + update(update: ViewUpdate) |
| 15 | + { |
| 16 | + if (update.selectionSet) { |
| 17 | + this.updateCSS() |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + updateCSS() |
| 22 | + { |
| 23 | + const selection = this.view.state.selection.main |
| 24 | + |
| 25 | + // 移除之前的样式 |
| 26 | + if (this.styleElement) { |
| 27 | + this.styleElement.remove() |
| 28 | + this.styleElement = null |
| 29 | + } |
| 30 | + |
| 31 | + if (selection.empty) { |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + const doc = this.view.state.doc |
| 36 | + const fromLine = doc.lineAt(selection.from) |
| 37 | + const toLine = doc.lineAt(selection.to) |
| 38 | + |
| 39 | + const cssRules: string[] = [] |
| 40 | + |
| 41 | + // 为每个有缩进的选中行生成CSS规则 |
| 42 | + for (let lineNum = fromLine.number; lineNum <= toLine.number; lineNum++) { |
| 43 | + const line = doc.line(lineNum) |
| 44 | + const text = line.text |
| 45 | + |
| 46 | + // 计算空格数 |
| 47 | + let spaceCount = 0 |
| 48 | + for (let i = 0; i < text.length; i++) { |
| 49 | + if (text[i] === ' ') { |
| 50 | + spaceCount++ |
| 51 | + } |
| 52 | + else { |
| 53 | + break |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if (spaceCount > 0) { |
| 58 | + // 使用行号作为选择器,添加伪元素 |
| 59 | + const arrowText = '·'.repeat(spaceCount) |
| 60 | + cssRules.push(` |
| 61 | + .cm-editor .cm-line:nth-child(${lineNum}) { |
| 62 | + position: relative; |
| 63 | + } |
| 64 | + .cm-editor .cm-line:nth-child(${lineNum})::before { |
| 65 | + content: "${arrowText}"; |
| 66 | + position: absolute; |
| 67 | + left: 0.4em; |
| 68 | + top: 0; |
| 69 | + color: #9ca3af; |
| 70 | + font-weight: 500; |
| 71 | + pointer-events: none; |
| 72 | + z-index: 2; |
| 73 | + font-family: monospace; |
| 74 | + font-size: inherit; |
| 75 | + line-height: inherit; |
| 76 | + } |
| 77 | + .cm-editor .cm-selectionBackground .cm-line:nth-child(${lineNum})::before, |
| 78 | + .cm-editor .cm-line:nth-child(${lineNum}).cm-selectionBackground::before { |
| 79 | + background-color: var(--cm-selectionBackground, #b3d4fc); |
| 80 | + } |
| 81 | + `) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // 插入动态样式 |
| 86 | + if (cssRules.length > 0) { |
| 87 | + this.styleElement = document.createElement('style') |
| 88 | + this.styleElement.textContent = cssRules.join('\n') |
| 89 | + document.head.appendChild(this.styleElement) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + destroy() |
| 94 | + { |
| 95 | + if (this.styleElement) { |
| 96 | + this.styleElement.remove() |
| 97 | + } |
| 98 | + } |
| 99 | + }) |
| 100 | + |
| 101 | + const spaceOmissionTheme = EditorView.theme({ |
| 102 | + '.cm-line': { |
| 103 | + position: 'relative' |
| 104 | + } |
| 105 | + }) |
| 106 | + |
| 107 | + return { |
| 108 | + spaceOmissionPlugin, |
| 109 | + spaceOmissionTheme |
| 110 | + } |
| 111 | +} |
0 commit comments