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