scroll-one-line.ts 953 B

1234567891011121314151617181920212223242526272829303132333435
  1. import { Command, EditorView } from '@codemirror/view'
  2. function scrollByLine(view: EditorView, lineCount: number) {
  3. view.scrollDOM.scrollTop += view.defaultLineHeight * lineCount
  4. }
  5. const scrollUpOneLine: Command = (view: EditorView) => {
  6. scrollByLine(view, -1)
  7. // Always consume the keypress to prevent the cursor going up a line when the
  8. // editor is scrolled to the top
  9. return true
  10. }
  11. const scrollDownOneLine: Command = (view: EditorView) => {
  12. scrollByLine(view, 1)
  13. // Always consume the keypress to prevent the cursor going down a line when
  14. // the editor is scrolled to the bottom
  15. return true
  16. }
  17. /**
  18. * Custom keymap for Windows and Linux for scrolling the viewport up/down one line with Ctrl+ArrowUp/Down.
  19. */
  20. export const scrollOneLineKeymap = [
  21. {
  22. linux: 'Ctrl-ArrowUp',
  23. win: 'Ctrl-ArrowUp',
  24. run: scrollUpOneLine,
  25. },
  26. {
  27. linux: 'Ctrl-ArrowDown',
  28. win: 'Ctrl-ArrowDown',
  29. run: scrollDownOneLine,
  30. },
  31. ]