highlight-active-line.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import {
  2. Decoration,
  3. DecorationSet,
  4. EditorView,
  5. layer,
  6. LayerMarker,
  7. RectangleMarker,
  8. ViewPlugin,
  9. ViewUpdate,
  10. } from '@codemirror/view'
  11. import { sourceOnly } from './visual/visual'
  12. import { fullHeightCoordsAtPos } from '../utils/layer'
  13. /**
  14. * An alternative version of the built-in highlightActiveLine extension,
  15. * using a custom approach for highlighting the full height of the active “visual line” of a wrapped line.
  16. */
  17. export const highlightActiveLine = (visual: boolean) => {
  18. // this extension should only be active in the source editor
  19. return sourceOnly(visual, [
  20. activeLineLayer,
  21. singleLineHighlighter,
  22. EditorView.baseTheme({
  23. '.ol-cm-activeLineLayer': {
  24. pointerEvents: 'none',
  25. },
  26. }),
  27. ])
  28. }
  29. /**
  30. * Line decoration approach used for non-wrapped lines, adapted from built-in
  31. * CodeMirror 6 highlightActiveLine, licensed under the MIT license:
  32. * https://github.com/codemirror/view/blob/main/src/active-line.ts
  33. */
  34. const lineDeco = Decoration.line({ class: 'cm-activeLine' })
  35. const singleLineHighlighter = ViewPlugin.fromClass(
  36. class {
  37. decorations: DecorationSet
  38. constructor(view: EditorView) {
  39. this.decorations = this.getDeco(view)
  40. }
  41. update(update: ViewUpdate) {
  42. if (update.geometryChanged || update.selectionSet) {
  43. this.decorations = this.getDeco(update.view)
  44. }
  45. }
  46. getDeco(view: EditorView) {
  47. const deco = []
  48. // NOTE: only highlighting the active line for the main selection
  49. const { main } = view.state.selection
  50. // No active line highlight when text is selected
  51. if (main.empty) {
  52. const line = view.lineBlockAt(main.head)
  53. if (line.height <= view.defaultLineHeight) {
  54. deco.push(lineDeco.range(line.from))
  55. }
  56. }
  57. return Decoration.set(deco)
  58. }
  59. },
  60. {
  61. decorations: v => v.decorations,
  62. }
  63. )
  64. // Custom layer approach, used only for wrapped lines
  65. const activeLineLayer = layer({
  66. above: false,
  67. class: 'ol-cm-activeLineLayer',
  68. markers(view: EditorView): readonly LayerMarker[] {
  69. const markers: LayerMarker[] = []
  70. // NOTE: only highlighting the active line for the main selection
  71. const { main } = view.state.selection
  72. // no active line highlight when text is selected
  73. if (!main.empty) {
  74. return markers
  75. }
  76. // Use line decoration when line doesn't wrap
  77. if (view.lineBlockAt(main.head).height <= view.defaultLineHeight) {
  78. return markers
  79. }
  80. const coords = fullHeightCoordsAtPos(
  81. view,
  82. main.head,
  83. main.assoc || undefined
  84. )
  85. if (coords) {
  86. const scrollRect = view.scrollDOM.getBoundingClientRect()
  87. const contentRect = view.contentDOM.getBoundingClientRect()
  88. const scrollTop = view.scrollDOM.scrollTop
  89. const top = coords.top - scrollRect.top + scrollTop
  90. const left = contentRect.left - scrollRect.left
  91. const width = contentRect.right - contentRect.left
  92. const height = coords.bottom - coords.top
  93. markers.push(
  94. new RectangleMarker('cm-activeLine', left, top, width, height)
  95. )
  96. }
  97. return markers
  98. },
  99. update(update: ViewUpdate): boolean {
  100. return update.geometryChanged || update.selectionSet
  101. },
  102. })