|
| 1 | +import { |
| 2 | + KeyBinding, |
| 3 | + lineNumbers, |
| 4 | + highlightActiveLineGutter, |
| 5 | + highlightSpecialChars, |
| 6 | + drawSelection, |
| 7 | + dropCursor, |
| 8 | + rectangularSelection, |
| 9 | + crosshairCursor, |
| 10 | + highlightActiveLine, |
| 11 | + keymap, |
| 12 | +} from '@codemirror/view'; |
| 13 | +import { EditorState, Extension } from '@codemirror/state'; |
| 14 | +import { history, defaultKeymap, historyKeymap } from '@codemirror/commands'; |
| 15 | +import { highlightSelectionMatches, searchKeymap } from '@codemirror/search'; |
| 16 | +import { closeBrackets, autocompletion, closeBracketsKeymap, completionKeymap } from '@codemirror/autocomplete'; |
| 17 | +import { |
| 18 | + foldGutter, |
| 19 | + indentOnInput, |
| 20 | + syntaxHighlighting, |
| 21 | + defaultHighlightStyle, |
| 22 | + bracketMatching, |
| 23 | + foldKeymap, |
| 24 | +} from '@codemirror/language'; |
| 25 | +import { lintKeymap } from '@codemirror/lint'; |
| 26 | + |
| 27 | +export interface BasicSetupOptions { |
| 28 | + lineNumbers?: boolean; |
| 29 | + highlightActiveLineGutter?: boolean; |
| 30 | + highlightSpecialChars?: boolean; |
| 31 | + history?: boolean; |
| 32 | + foldGutter?: boolean; |
| 33 | + drawSelection?: boolean; |
| 34 | + dropCursor?: boolean; |
| 35 | + allowMultipleSelections?: boolean; |
| 36 | + indentOnInput?: boolean; |
| 37 | + syntaxHighlighting?: boolean; |
| 38 | + bracketMatching?: boolean; |
| 39 | + closeBrackets?: boolean; |
| 40 | + autocompletion?: boolean; |
| 41 | + rectangularSelection?: boolean; |
| 42 | + crosshairCursor?: boolean; |
| 43 | + highlightActiveLine?: boolean; |
| 44 | + highlightSelectionMatches?: boolean; |
| 45 | + |
| 46 | + closeBracketsKeymap?: boolean; |
| 47 | + defaultKeymap?: boolean; |
| 48 | + searchKeymap?: boolean; |
| 49 | + historyKeymap?: boolean; |
| 50 | + foldKeymap?: boolean; |
| 51 | + completionKeymap?: boolean; |
| 52 | + lintKeymap?: boolean; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | +This is an extension value that just pulls together a number of |
| 57 | +extensions that you might want in a basic editor. It is meant as a |
| 58 | +convenient helper to quickly set up CodeMirror without installing |
| 59 | +and importing a lot of separate packages. |
| 60 | +
|
| 61 | +Specifically, it includes... |
| 62 | +
|
| 63 | + - [the default command bindings](https://codemirror.net/6/docs/ref/#commands.defaultKeymap) |
| 64 | + - [line numbers](https://codemirror.net/6/docs/ref/#view.lineNumbers) |
| 65 | + - [special character highlighting](https://codemirror.net/6/docs/ref/#view.highlightSpecialChars) |
| 66 | + - [the undo history](https://codemirror.net/6/docs/ref/#commands.history) |
| 67 | + - [a fold gutter](https://codemirror.net/6/docs/ref/#language.foldGutter) |
| 68 | + - [custom selection drawing](https://codemirror.net/6/docs/ref/#view.drawSelection) |
| 69 | + - [drop cursor](https://codemirror.net/6/docs/ref/#view.dropCursor) |
| 70 | + - [multiple selections](https://codemirror.net/6/docs/ref/#state.EditorState^allowMultipleSelections) |
| 71 | + - [reindentation on input](https://codemirror.net/6/docs/ref/#language.indentOnInput) |
| 72 | + - [the default highlight style](https://codemirror.net/6/docs/ref/#language.defaultHighlightStyle) (as fallback) |
| 73 | + - [bracket matching](https://codemirror.net/6/docs/ref/#language.bracketMatching) |
| 74 | + - [bracket closing](https://codemirror.net/6/docs/ref/#autocomplete.closeBrackets) |
| 75 | + - [autocompletion](https://codemirror.net/6/docs/ref/#autocomplete.autocompletion) |
| 76 | + - [rectangular selection](https://codemirror.net/6/docs/ref/#view.rectangularSelection) and [crosshair cursor](https://codemirror.net/6/docs/ref/#view.crosshairCursor) |
| 77 | + - [active line highlighting](https://codemirror.net/6/docs/ref/#view.highlightActiveLine) |
| 78 | + - [active line gutter highlighting](https://codemirror.net/6/docs/ref/#view.highlightActiveLineGutter) |
| 79 | + - [selection match highlighting](https://codemirror.net/6/docs/ref/#search.highlightSelectionMatches) |
| 80 | + - [search](https://codemirror.net/6/docs/ref/#search.searchKeymap) |
| 81 | + - [linting](https://codemirror.net/6/docs/ref/#lint.lintKeymap) |
| 82 | +
|
| 83 | +(You'll probably want to add some language package to your setup |
| 84 | +too.) |
| 85 | +
|
| 86 | +This extension does not allow customization. The idea is that, |
| 87 | +once you decide you want to configure your editor more precisely, |
| 88 | +you take this package's source (which is just a bunch of imports |
| 89 | +and an array literal), copy it into your own code, and adjust it |
| 90 | +as desired. |
| 91 | +*/ |
| 92 | +export const basicSetup = (options: BasicSetupOptions = {}): Extension[] => { |
| 93 | + const keymaps: KeyBinding[][] = []; |
| 94 | + if (options.closeBracketsKeymap !== false) { |
| 95 | + keymaps.push([...closeBracketsKeymap]); |
| 96 | + } |
| 97 | + if (options.defaultKeymap !== false) { |
| 98 | + keymaps.push([...defaultKeymap]); |
| 99 | + } |
| 100 | + if (options.searchKeymap !== false) { |
| 101 | + keymaps.push([...searchKeymap]); |
| 102 | + } |
| 103 | + if (options.historyKeymap !== false) { |
| 104 | + keymaps.push([...historyKeymap]); |
| 105 | + } |
| 106 | + if (options.foldKeymap !== false) { |
| 107 | + keymaps.push([...foldKeymap]); |
| 108 | + } |
| 109 | + if (options.completionKeymap !== false) { |
| 110 | + keymaps.push([...completionKeymap]); |
| 111 | + } |
| 112 | + if (options.lintKeymap !== false) { |
| 113 | + keymaps.push([...lintKeymap]); |
| 114 | + } |
| 115 | + const extensions: Extension[] = []; |
| 116 | + if (options.lineNumbers !== false) extensions.push(lineNumbers()); |
| 117 | + if (options.highlightActiveLineGutter !== false) extensions.push(highlightActiveLineGutter()); |
| 118 | + if (options.highlightSpecialChars !== false) extensions.push(highlightSpecialChars()); |
| 119 | + if (options.history !== false) extensions.push(history()); |
| 120 | + if (options.foldGutter !== false) extensions.push(foldGutter()); |
| 121 | + if (options.drawSelection !== false) extensions.push(drawSelection()); |
| 122 | + if (options.dropCursor !== false) extensions.push(dropCursor()); |
| 123 | + if (options.allowMultipleSelections !== false) extensions.push(EditorState.allowMultipleSelections.of(true)); |
| 124 | + if (options.indentOnInput !== false) extensions.push(indentOnInput()); |
| 125 | + if (options.syntaxHighlighting !== false) |
| 126 | + extensions.push(syntaxHighlighting(defaultHighlightStyle, { fallback: true })); |
| 127 | + if (options.bracketMatching !== false) extensions.push(bracketMatching()); |
| 128 | + if (options.closeBrackets !== false) extensions.push(closeBrackets()); |
| 129 | + if (options.autocompletion !== false) extensions.push(autocompletion()); |
| 130 | + if (options.rectangularSelection !== false) extensions.push(rectangularSelection()); |
| 131 | + if (options.crosshairCursor !== false) extensions.push(crosshairCursor()); |
| 132 | + if (options.highlightActiveLine !== false) extensions.push(highlightActiveLine()); |
| 133 | + if (options.highlightSelectionMatches !== false) extensions.push(highlightSelectionMatches()); |
| 134 | + |
| 135 | + return [...extensions, keymap.of(keymaps.flat())].filter(Boolean); |
| 136 | +}; |
0 commit comments