non-blinking-cursor.ts 990 B

123456789101112131415161718192021222324252627282930313233343536
  1. import { StateEffect, StateField, TransactionSpec } from '@codemirror/state'
  2. import { EditorView } from '@codemirror/view'
  3. const nonBlinkingCursorEffect = StateEffect.define<boolean>()
  4. const nonBlinkingCursorTheme = EditorView.baseTheme({
  5. '&.cm-non-blinking-cursor .cm-cursorLayer': {
  6. animationName: 'none !important',
  7. },
  8. })
  9. const nonBlinkingCursorField = StateField.define<boolean>({
  10. create: () => false,
  11. update: (value, tr) => {
  12. for (const effect of tr.effects) {
  13. if (effect.is(nonBlinkingCursorEffect)) {
  14. value = effect.value
  15. }
  16. }
  17. return value
  18. },
  19. provide(field) {
  20. return [
  21. EditorView.editorAttributes.from(field, field => ({
  22. class: field ? 'cm-non-blinking-cursor' : '',
  23. })),
  24. nonBlinkingCursorTheme,
  25. ]
  26. },
  27. })
  28. export const setNonBlinkingCursor = (enabled: boolean): TransactionSpec => ({
  29. effects: nonBlinkingCursorEffect.of(enabled),
  30. })
  31. export const nonBlinkingCursor = () => nonBlinkingCursorField