2023-03-11 01:52:17 +08:00
|
|
|
<script lang="ts">
|
|
|
|
import type { ViewUpdate } from "@codemirror/view";
|
|
|
|
import { createEventDispatcher, onMount } from "svelte";
|
|
|
|
import {
|
|
|
|
EditorView,
|
|
|
|
keymap,
|
|
|
|
placeholder as placeholderExt
|
|
|
|
} from "@codemirror/view";
|
|
|
|
import { StateEffect, EditorState, type Extension } from "@codemirror/state";
|
|
|
|
import { indentWithTab } from "@codemirror/commands";
|
|
|
|
|
|
|
|
import { basicDark } from "cm6-theme-basic-dark";
|
|
|
|
import { basicLight } from "cm6-theme-basic-light";
|
|
|
|
import { basicSetup } from "./extensions";
|
|
|
|
import { getLanguageExtension } from "./language";
|
|
|
|
|
|
|
|
export let classNames = "";
|
|
|
|
export let value = "";
|
|
|
|
export let dark_mode: boolean;
|
|
|
|
|
|
|
|
export let basic = true;
|
|
|
|
export let language: string;
|
2023-04-06 22:52:57 +08:00
|
|
|
export let lines: number = 5;
|
2023-03-11 01:52:17 +08:00
|
|
|
export let extensions: Extension[] = [];
|
|
|
|
|
|
|
|
export let useTab = true;
|
|
|
|
|
|
|
|
export let readonly = false;
|
|
|
|
export let placeholder: string | HTMLElement | null | undefined = undefined;
|
|
|
|
|
|
|
|
const dispatch = createEventDispatcher<{ change: string }>();
|
|
|
|
let lang_extension: Extension | undefined;
|
|
|
|
let element: HTMLDivElement;
|
|
|
|
let view: EditorView;
|
|
|
|
|
|
|
|
$: get_lang(language);
|
|
|
|
|
|
|
|
async function get_lang(val: string) {
|
|
|
|
const ext = await getLanguageExtension(val);
|
|
|
|
lang_extension = ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
$: reconfigure(), lang_extension;
|
|
|
|
$: setDoc(value);
|
2023-04-06 22:52:57 +08:00
|
|
|
$: updateLines(lines);
|
2023-03-11 01:52:17 +08:00
|
|
|
|
|
|
|
function setDoc(newDoc: string) {
|
|
|
|
if (view && newDoc !== view.state.doc.toString()) {
|
|
|
|
view.dispatch({
|
|
|
|
changes: {
|
|
|
|
from: 0,
|
|
|
|
to: view.state.doc.length,
|
|
|
|
insert: newDoc
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-06 22:52:57 +08:00
|
|
|
function updateLines(newLines: number) {
|
|
|
|
if (view) {
|
|
|
|
view.requestMeasure({ read: updateGutters });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-11 01:52:17 +08:00
|
|
|
function createEditorView(): EditorView {
|
|
|
|
return new EditorView({
|
|
|
|
parent: element,
|
|
|
|
state: createEditorState(value)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-06 22:52:57 +08:00
|
|
|
function getGutterLineHeight(view: EditorView): string | null {
|
|
|
|
let elements = view.dom.querySelectorAll<HTMLElement>(".cm-gutterElement");
|
|
|
|
if (elements.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
for (var i = 0; i < elements.length; i++) {
|
|
|
|
let node = elements[i];
|
|
|
|
let height = getComputedStyle(node)?.height ?? "0px";
|
|
|
|
if (height != "0px") {
|
|
|
|
return height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateGutters(view: EditorView): any {
|
|
|
|
let gutters = view.dom.querySelectorAll<HTMLElement>(".cm-gutter");
|
|
|
|
let _lines = lines + 1;
|
|
|
|
let lineHeight = getGutterLineHeight(view);
|
|
|
|
if (!lineHeight) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
for (var i = 0; i < gutters.length; i++) {
|
|
|
|
let node = gutters[i];
|
|
|
|
node.style.minHeight = `calc(${lineHeight} * ${_lines})`;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-03-11 01:52:17 +08:00
|
|
|
function handleChange(vu: ViewUpdate): void {
|
|
|
|
if (vu.docChanged) {
|
|
|
|
const doc = vu.state.doc;
|
|
|
|
const text = doc.toString();
|
|
|
|
value = text;
|
|
|
|
dispatch("change", text);
|
|
|
|
}
|
2023-04-06 22:52:57 +08:00
|
|
|
view.requestMeasure({ read: updateGutters });
|
2023-03-11 01:52:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function getExtensions() {
|
|
|
|
const stateExtensions = [
|
|
|
|
...getBaseExtensions(
|
|
|
|
basic,
|
|
|
|
useTab,
|
|
|
|
placeholder,
|
|
|
|
readonly,
|
|
|
|
lang_extension
|
|
|
|
),
|
|
|
|
FontTheme,
|
|
|
|
...getTheme(),
|
|
|
|
...extensions
|
|
|
|
];
|
|
|
|
return stateExtensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FontTheme = EditorView.theme({
|
|
|
|
"&": {
|
|
|
|
fontSize: "var(--text-sm)",
|
2023-03-17 22:41:53 +08:00
|
|
|
backgroundColor: "var(--border-color-secondary)"
|
2023-03-11 01:52:17 +08:00
|
|
|
},
|
|
|
|
".cm-content": {
|
|
|
|
paddingTop: "5px",
|
|
|
|
paddingBottom: "5px",
|
2023-05-03 22:52:52 +08:00
|
|
|
color: "var(--body-text-color)",
|
2023-03-11 01:52:17 +08:00
|
|
|
fontFamily: "var(--font-mono)",
|
|
|
|
minHeight: "100%"
|
|
|
|
},
|
|
|
|
".cm-gutters": {
|
|
|
|
marginRight: "1px",
|
2023-03-17 22:41:53 +08:00
|
|
|
borderRight: "1px solid var(--border-color-primary)",
|
2023-03-11 01:52:17 +08:00
|
|
|
backgroundColor: "transparent",
|
2023-03-17 22:41:53 +08:00
|
|
|
color: "var(--body-text-color-subdued)"
|
2023-03-11 01:52:17 +08:00
|
|
|
},
|
|
|
|
".cm-focused": {
|
|
|
|
outline: "none"
|
2023-05-03 22:52:52 +08:00
|
|
|
},
|
|
|
|
".cm-scroller": {
|
|
|
|
height: "auto"
|
|
|
|
},
|
|
|
|
".cm-cursor": {
|
|
|
|
borderLeftColor: "var(--body-text-color)"
|
2023-03-11 01:52:17 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function createEditorState(value: string | null | undefined): EditorState {
|
|
|
|
return EditorState.create({
|
|
|
|
doc: value ?? undefined,
|
|
|
|
extensions: getExtensions()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBaseExtensions(
|
|
|
|
basic: boolean,
|
|
|
|
useTab: boolean,
|
|
|
|
placeholder: string | HTMLElement | null | undefined,
|
|
|
|
readonly: boolean,
|
|
|
|
lang: Extension | null | undefined
|
|
|
|
): Extension[] {
|
|
|
|
const extensions: Extension[] = [
|
|
|
|
EditorView.editable.of(!readonly),
|
|
|
|
EditorState.readOnly.of(readonly)
|
|
|
|
];
|
|
|
|
|
|
|
|
if (basic) {
|
|
|
|
extensions.push(basicSetup);
|
|
|
|
}
|
|
|
|
if (useTab) {
|
|
|
|
extensions.push(keymap.of([indentWithTab]));
|
|
|
|
}
|
|
|
|
if (placeholder) {
|
|
|
|
extensions.push(placeholderExt(placeholder));
|
|
|
|
}
|
|
|
|
if (lang) {
|
|
|
|
extensions.push(lang);
|
|
|
|
}
|
|
|
|
|
|
|
|
extensions.push(EditorView.updateListener.of(handleChange));
|
|
|
|
return extensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTheme(): Extension[] {
|
|
|
|
const extensions: Extension[] = [];
|
|
|
|
|
|
|
|
if (dark_mode) {
|
|
|
|
extensions.push(basicDark);
|
|
|
|
} else {
|
|
|
|
extensions.push(basicLight);
|
|
|
|
}
|
|
|
|
return extensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
function reconfigure(): void {
|
|
|
|
view?.dispatch({
|
|
|
|
effects: StateEffect.reconfigure.of(getExtensions())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
view = createEditorView();
|
|
|
|
return () => view?.destroy();
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<div class="wrap">
|
2023-03-31 02:20:34 +08:00
|
|
|
<div class="codemirror-wrapper {classNames}" bind:this={element} />
|
2023-03-11 01:52:17 +08:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<style>
|
2023-04-06 22:52:57 +08:00
|
|
|
.wrap {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
flex-flow: column;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
height: 100%;
|
|
|
|
}
|
2023-03-11 01:52:17 +08:00
|
|
|
.codemirror-wrapper {
|
2023-04-06 22:52:57 +08:00
|
|
|
height: 100%;
|
2023-03-11 01:52:17 +08:00
|
|
|
overflow: auto;
|
|
|
|
}
|
|
|
|
|
2023-04-06 22:52:57 +08:00
|
|
|
:global(.cm-editor) {
|
|
|
|
height: 100%;
|
|
|
|
}
|
|
|
|
|
2023-03-11 01:52:17 +08:00
|
|
|
/* Dunno why this doesn't work through the theme API -- don't remove*/
|
|
|
|
:global(.cm-selectionBackground) {
|
|
|
|
background-color: #b9d2ff30 !important;
|
|
|
|
}
|
|
|
|
|
|
|
|
:global(.cm-focused) {
|
|
|
|
outline: none !important;
|
|
|
|
}
|
|
|
|
</style>
|