index.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { EditorView, ViewPlugin } from '@codemirror/view'
  2. import {
  3. EditorState,
  4. StateEffect,
  5. StateField,
  6. TransactionSpec,
  7. } from '@codemirror/state'
  8. import { misspelledWordsField } from './misspelled-words'
  9. import {
  10. addIgnoredWord,
  11. ignoredWordsField,
  12. removeIgnoredWord,
  13. resetSpellChecker,
  14. updateAfterAddingIgnoredWord,
  15. } from './ignored-words'
  16. import { addWordToCache, cacheField, removeWordFromCache } from './cache'
  17. import { hideSpellingMenu, spellingMenuField } from './context-menu'
  18. import { SpellChecker } from './spellchecker'
  19. import { parserWatcher } from '../wait-for-parser'
  20. import type { HunspellManager } from '@/features/source-editor/hunspell/HunspellManager'
  21. import { debugConsole } from '@/utils/debugging'
  22. import { captureException } from '@/infrastructure/error-reporter'
  23. type Options = {
  24. spellCheckLanguage?: string
  25. hunspellManager?: HunspellManager
  26. }
  27. /**
  28. * A custom extension that creates a spell checker for the current language (from the user settings).
  29. * The spell check runs on the server whenever a line changes.
  30. * The mis-spelled words, ignored words and spell-checked words are stored in a state field.
  31. * Mis-spelled words are decorated with a Mark decoration.
  32. * The suggestions menu is displayed in a tooltip, activated with a right-click on the decoration.
  33. */
  34. export const spelling = ({ spellCheckLanguage, hunspellManager }: Options) => {
  35. return [
  36. spellingTheme,
  37. parserWatcher,
  38. spellCheckLanguageField.init(() => spellCheckLanguage),
  39. spellCheckerField.init(() =>
  40. spellCheckLanguage
  41. ? new SpellChecker(spellCheckLanguage, hunspellManager)
  42. : null
  43. ),
  44. misspelledWordsField,
  45. ignoredWordsField,
  46. cacheField,
  47. spellingMenuField,
  48. ]
  49. }
  50. const spellingTheme = EditorView.baseTheme({
  51. '.ol-cm-spelling-error': {
  52. textDecorationColor: 'red',
  53. textDecorationLine: 'underline',
  54. textDecorationStyle: 'dotted',
  55. textDecorationThickness: '2px',
  56. textDecorationSkipInk: 'none',
  57. textUnderlineOffset: '0.2em',
  58. },
  59. '.cm-tooltip.ol-cm-spelling-context-menu-tooltip': {
  60. borderWidth: '0',
  61. },
  62. })
  63. export const getSpellChecker = (state: EditorState) =>
  64. state.field(spellCheckerField, false)
  65. const spellCheckerField = StateField.define<SpellChecker | null>({
  66. create() {
  67. return null
  68. },
  69. update(value, tr) {
  70. for (const effect of tr.effects) {
  71. if (effect.is(setSpellCheckLanguageEffect)) {
  72. value?.destroy()
  73. value = effect.value.spellCheckLanguage
  74. ? new SpellChecker(
  75. effect.value.spellCheckLanguage,
  76. effect.value.hunspellManager
  77. )
  78. : null
  79. } else if (effect.is(addIgnoredWord)) {
  80. value?.addWord(effect.value.text).catch(error => {
  81. captureException(error)
  82. debugConsole.error(error)
  83. })
  84. } else if (effect.is(removeIgnoredWord)) {
  85. value?.removeWord(effect.value.text).catch(error => {
  86. captureException(error)
  87. debugConsole.error(error)
  88. })
  89. }
  90. }
  91. return value
  92. },
  93. provide(field) {
  94. return [
  95. ViewPlugin.define(view => {
  96. return {
  97. destroy: () => {
  98. view.state.field(field)?.destroy()
  99. },
  100. }
  101. }),
  102. EditorView.domEventHandlers({
  103. focus: (_event, view) => {
  104. if (view.state.facet(EditorView.editable)) {
  105. view.state.field(field)?.scheduleSpellCheck(view)
  106. }
  107. },
  108. }),
  109. EditorView.updateListener.of(update => {
  110. if (update.state.facet(EditorView.editable)) {
  111. update.state.field(field)?.handleUpdate(update)
  112. }
  113. }),
  114. ]
  115. },
  116. })
  117. export const getSpellCheckLanguage = (state: EditorState) =>
  118. state.field(spellCheckLanguageField, false)
  119. const spellCheckLanguageField = StateField.define<string | undefined>({
  120. create() {
  121. return undefined
  122. },
  123. update(value, tr) {
  124. for (const effect of tr.effects) {
  125. if (effect.is(setSpellCheckLanguageEffect)) {
  126. value = effect.value.spellCheckLanguage
  127. }
  128. }
  129. return value
  130. },
  131. })
  132. export const setSpellCheckLanguageEffect = StateEffect.define<{
  133. spellCheckLanguage: string | undefined
  134. hunspellManager?: HunspellManager
  135. }>()
  136. export const setSpellCheckLanguage = ({
  137. spellCheckLanguage,
  138. hunspellManager,
  139. }: Options): TransactionSpec => {
  140. return {
  141. effects: [
  142. setSpellCheckLanguageEffect.of({
  143. spellCheckLanguage,
  144. hunspellManager,
  145. }),
  146. hideSpellingMenu.of(null),
  147. ],
  148. }
  149. }
  150. export const addLearnedWord = (
  151. spellCheckLanguage: string,
  152. word: string
  153. ): TransactionSpec => {
  154. return {
  155. effects: [
  156. addWordToCache.of({
  157. lang: spellCheckLanguage,
  158. wordText: word,
  159. value: true,
  160. }),
  161. updateAfterAddingIgnoredWord.of(word),
  162. ],
  163. }
  164. }
  165. export const removeLearnedWord = (
  166. spellCheckLanguage: string,
  167. word: string
  168. ): TransactionSpec => {
  169. return {
  170. effects: [
  171. removeWordFromCache.of({
  172. lang: spellCheckLanguage,
  173. wordText: word,
  174. }),
  175. removeIgnoredWord.of({ text: word }),
  176. ],
  177. }
  178. }
  179. export const resetLearnedWords = (): TransactionSpec => {
  180. return {
  181. effects: [resetSpellChecker.of(null)],
  182. }
  183. }