scroll-one-line.ts 875 B

123456789101112131415161718192021222324252627282930313233
  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. // Applied to Windows and Linux only
  18. export const scrollOneLineKeymap = [
  19. {
  20. linux: 'Ctrl-ArrowUp',
  21. win: 'Ctrl-ArrowUp',
  22. run: scrollUpOneLine,
  23. },
  24. {
  25. linux: 'Ctrl-ArrowDown',
  26. win: 'Ctrl-ArrowDown',
  27. run: scrollDownOneLine,
  28. },
  29. ]