draw-selection.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { EditorSelection, Prec } from '@codemirror/state'
  2. import { EditorView, layer } from '@codemirror/view'
  3. import { rectangleMarkerForRange } from '../utils/layer'
  4. import { updateHasMouseDownEffect } from './visual/selection'
  5. import browser from './browser'
  6. /**
  7. * The built-in extension which draws the cursor and selection(s) in layers,
  8. * copied to make use of a custom version of rectangleMarkerForRange which calls
  9. * fullHeightCoordsAtPos when in Source mode, extending the top and bottom
  10. * of the coords to cover the full line height.
  11. *
  12. * Whenever possible, we should avoid making changes to this extension.
  13. */
  14. export const drawSelection = () => {
  15. return [cursorLayer, selectionLayer, Prec.highest(hideNativeSelection)]
  16. }
  17. const canHidePrimary = !browser.ios
  18. const hideNativeSelection = EditorView.theme({
  19. '.cm-line': {
  20. 'caret-color': canHidePrimary ? 'transparent !important' : null,
  21. '& ::selection': {
  22. backgroundColor: 'transparent !important',
  23. },
  24. '&::selection': {
  25. backgroundColor: 'transparent !important',
  26. },
  27. },
  28. })
  29. const cursorLayer = layer({
  30. above: true,
  31. markers(view) {
  32. const {
  33. selection: { ranges, main },
  34. } = view.state
  35. const cursors = []
  36. for (const range of ranges) {
  37. const primary = range === main
  38. if (!range.empty || !primary || canHidePrimary) {
  39. const className = primary
  40. ? 'cm-cursor cm-cursor-primary'
  41. : 'cm-cursor cm-cursor-secondary'
  42. const cursor = range.empty
  43. ? range
  44. : EditorSelection.cursor(
  45. range.head,
  46. range.head > range.anchor ? -1 : 1
  47. )
  48. for (const piece of rectangleMarkerForRange(view, className, cursor)) {
  49. cursors.push(piece)
  50. }
  51. }
  52. }
  53. return cursors
  54. },
  55. update(update, dom) {
  56. if (update.transactions.some(tr => tr.selection)) {
  57. dom.style.animationName =
  58. dom.style.animationName === 'cm-blink' ? 'cm-blink2' : 'cm-blink'
  59. }
  60. return (
  61. update.docChanged ||
  62. update.selectionSet ||
  63. updateHasMouseDownEffect(update)
  64. )
  65. },
  66. mount(dom) {
  67. dom.style.animationDuration = '1200ms'
  68. },
  69. class: 'cm-cursorLayer',
  70. })
  71. const selectionLayer = layer({
  72. above: false,
  73. markers(view) {
  74. const markers = []
  75. for (const range of view.state.selection.ranges) {
  76. if (!range.empty) {
  77. markers.push(
  78. ...rectangleMarkerForRange(view, 'cm-selectionBackground', range)
  79. )
  80. }
  81. }
  82. return markers
  83. },
  84. update(update) {
  85. return (
  86. update.docChanged ||
  87. update.selectionSet ||
  88. update.viewportChanged ||
  89. updateHasMouseDownEffect(update)
  90. )
  91. },
  92. class: 'cm-selectionLayer',
  93. })