(props: UseCodeMirror)
| 16 | const emptyExtensions: Extension[] = []; |
| 17 | |
| 18 | export function useCodeMirror(props: UseCodeMirror) { |
| 19 | const { |
| 20 | value, |
| 21 | selection, |
| 22 | onChange, |
| 23 | onStatistics, |
| 24 | onCreateEditor, |
| 25 | onUpdate, |
| 26 | extensions = emptyExtensions, |
| 27 | autoFocus, |
| 28 | theme = 'light', |
| 29 | height = null, |
| 30 | minHeight = null, |
| 31 | maxHeight = null, |
| 32 | width = null, |
| 33 | minWidth = null, |
| 34 | maxWidth = null, |
| 35 | placeholder: placeholderStr = '', |
| 36 | editable = true, |
| 37 | readOnly = false, |
| 38 | indentWithTab: defaultIndentWithTab = true, |
| 39 | basicSetup: defaultBasicSetup = true, |
| 40 | root, |
| 41 | initialState, |
| 42 | } = props; |
| 43 | const [container, setContainer] = useState<HTMLDivElement | null>(); |
| 44 | const [view, setView] = useState<EditorView>(); |
| 45 | const [state, setState] = useState<EditorState>(); |
| 46 | const typingLatch = useState<{ current: TimeoutLatch | null }>(() => ({ current: null }))[0]; |
| 47 | const pendingUpdate = useState<{ current: (() => void) | null }>(() => ({ current: null }))[0]; |
| 48 | const defaultThemeOption = EditorView.theme({ |
| 49 | '&': { |
| 50 | height, |
| 51 | minHeight, |
| 52 | maxHeight, |
| 53 | width, |
| 54 | minWidth, |
| 55 | maxWidth, |
| 56 | }, |
| 57 | '& .cm-scroller': { |
| 58 | height: '100% !important', |
| 59 | }, |
| 60 | }); |
| 61 | const updateListener = EditorView.updateListener.of((vu: ViewUpdate) => { |
| 62 | if ( |
| 63 | vu.docChanged && |
| 64 | typeof onChange === 'function' && |
| 65 | // Fix echoing of the remote changes: |
| 66 | // If transaction is market as remote we don't have to call `onChange` handler again |
| 67 | !vu.transactions.some((tr) => tr.annotation(ExternalChange)) |
| 68 | ) { |
| 69 | if (typingLatch.current) { |
| 70 | typingLatch.current.reset(); |
| 71 | } else { |
| 72 | typingLatch.current = new TimeoutLatch(() => { |
| 73 | if (pendingUpdate.current) { |
| 74 | const forceUpdate = pendingUpdate.current; |
| 75 | pendingUpdate.current = null; |
no test coverage detected
searching dependent graphs…