Files
tildefriends/deps/codemirror_src/editor.mjs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

103 lines
2.9 KiB
JavaScript
Raw Normal View History

import {EditorState, Compartment} from "@codemirror/state"
import {EditorView} from '@codemirror/view';
import {javascript} from "@codemirror/lang-javascript"
import {htmlLanguage, html} from "@codemirror/lang-html"
import {css} from "@codemirror/lang-css"
import {markdown} from "@codemirror/lang-markdown"
import {xml} from "@codemirror/lang-xml"
import {search} from "@codemirror/search"
import {oneDark} from "./theme-tf-dark.js"
import {lineNumbers, highlightActiveLineGutter, highlightSpecialChars, highlightTrailingWhitespace, drawSelection, dropCursor, rectangularSelection, crosshairCursor, highlightActiveLine, keymap, highlightWhitespace} from '@codemirror/view';
import {language, foldGutter, indentUnit, indentOnInput, syntaxHighlighting, defaultHighlightStyle, bracketMatching, foldKeymap} from '@codemirror/language';
import {history, defaultKeymap, historyKeymap, indentWithTab} from '@codemirror/commands';
import {highlightSelectionMatches, searchKeymap} from '@codemirror/search';
import {autocompletion, closeBracketsKeymap, completionKeymap} from '@codemirror/autocomplete';
import {lintKeymap} from '@codemirror/lint';
let updateListenerExtension = EditorView.updateListener.of((update) => {
if (update.docChanged && update.view.onDocChange) {
update.view.onDocChange();
}
});
/* https://codemirror.net/examples/config/ */
const languageConfig = new Compartment();
const autoLanguage = EditorState.transactionExtender.of(tr => {
if (!tr.docChanged) {
return null;
}
let doc_is_html = /\s*</.test(tr.newDoc.sliceString(0, 100));
let state_is_html = tr.startState.facet(language) == htmlLanguage;
if (doc_is_html == state_is_html) {
return null;
}
return {
effects: languageConfig.reconfigure(doc_is_html ? html() : javascript()),
};
});
const extensions = [
lineNumbers(),
highlightActiveLineGutter(),
highlightSpecialChars(),
highlightWhitespace(),
history(),
foldGutter(),
drawSelection(),
dropCursor(),
EditorState.allowMultipleSelections.of(true),
indentOnInput(),
indentUnit.of('\t'),
syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
bracketMatching(),
autocompletion(),
rectangularSelection(),
crosshairCursor(),
highlightActiveLine(),
highlightSelectionMatches(),
highlightTrailingWhitespace(),
keymap.of([
...defaultKeymap,
...searchKeymap,
...historyKeymap,
...foldKeymap,
...completionKeymap,
...lintKeymap,
indentWithTab,
]),
languageConfig.of(javascript()),
autoLanguage,
search(),
oneDark,
updateListenerExtension,
];
function TildeFriendsEditorView(parent) {
return new EditorView({
extensions: extensions,
parent: parent,
});
}
function setEditorMode(view, mode) {
const k_modes = {
'css': css(),
'html': html(),
'javascript': javascript(),
'markdown': markdown(),
'xml': xml(),
};
view.dispatch({
effects: languageConfig.reconfigure(k_modes[mode])
});
}
export {
TildeFriendsEditorView,
EditorState,
EditorView,
extensions,
setEditorMode,
};