selection.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import {
  2. EditorSelection,
  3. StateEffect,
  4. Line,
  5. Text,
  6. StateField,
  7. EditorState,
  8. } from '@codemirror/state'
  9. import { EditorView } from '@codemirror/view'
  10. import { hasEffect, updateHasEffect } from '../../utils/effects'
  11. export const selectionIntersects = (
  12. selection: EditorSelection,
  13. extents: { from: number; to: number }
  14. ) =>
  15. selection.ranges.some(
  16. range =>
  17. // Case 1: from is inside node
  18. (extents.from <= range.from && extents.to >= range.from) ||
  19. // Case 2: to is inside node
  20. (extents.from <= range.to && extents.to >= range.to)
  21. )
  22. export const placeSelectionInsideBlock = (
  23. view: EditorView,
  24. event: MouseEvent
  25. ) => {
  26. const line = view.lineBlockAtHeight(event.pageY - view.documentTop)
  27. const selectionRange = EditorSelection.cursor(line.to)
  28. const selection = event.ctrlKey
  29. ? view.state.selection.addRange(selectionRange)
  30. : selectionRange
  31. return { selection, effects: EditorView.scrollIntoView(line.to) }
  32. }
  33. export const extendBackwardsOverEmptyLines = (doc: Text, line: Line) => {
  34. let { number, from } = line
  35. for (let lineNumber = number - 1; lineNumber > 0; lineNumber--) {
  36. const line = doc.line(lineNumber)
  37. if (line.text.trim().length > 0) {
  38. break
  39. }
  40. from = line.from
  41. }
  42. return from
  43. }
  44. export const extendForwardsOverEmptyLines = (doc: Text, line: Line) => {
  45. let { number, to } = line
  46. for (let lineNumber = number + 1; lineNumber <= doc.lines; lineNumber++) {
  47. const line = doc.line(lineNumber)
  48. if (line.text.trim().length > 0) {
  49. break
  50. }
  51. to = line.to
  52. }
  53. return to
  54. }
  55. export const mouseDownEffect = StateEffect.define<boolean>()
  56. export const hasMouseDownEffect = hasEffect(mouseDownEffect)
  57. export const updateHasMouseDownEffect = updateHasEffect(mouseDownEffect)
  58. /**
  59. * A listener for mousedown and mouseup events, dispatching an event
  60. * to record the current mousedown status, which is stored in a state field.
  61. */
  62. const mouseDownListener = EditorView.domEventHandlers({
  63. mousedown: (event, view) => {
  64. // not wrapped in a timeout, so update listeners know that the mouse is down before they process the selection
  65. view.dispatch({
  66. effects: mouseDownEffect.of(true),
  67. })
  68. },
  69. mouseup: (event, view) => {
  70. // wrap in a timeout, so update listeners receive this effect after the new selection has finished being handled
  71. window.setTimeout(() => {
  72. view.dispatch({
  73. effects: mouseDownEffect.of(false),
  74. })
  75. })
  76. },
  77. })
  78. const mousedownSelectionState = StateField.define<EditorSelection | undefined>({
  79. create() {
  80. return undefined
  81. },
  82. update(value, tr) {
  83. if (value && tr.docChanged) {
  84. value = value.map(tr.changes)
  85. }
  86. for (const effect of tr.effects) {
  87. // store the previous selection on mousedown
  88. if (effect.is(mouseDownEffect)) {
  89. value = effect.value ? tr.startState.selection : undefined
  90. }
  91. }
  92. return value
  93. },
  94. })
  95. export const getMousedownSelection = (state: EditorState) =>
  96. state.field(mousedownSelectionState)
  97. export const mousedown = [mouseDownListener, mousedownSelectionState]