Spaces:
Running
Running
File size: 5,825 Bytes
e0d6c83 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
<script lang="ts">
import { createEventDispatcher, onMount } from "svelte";
import {
EditorView,
ViewUpdate,
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 class_names = "";
export let value = "";
export let dark_mode: boolean;
export let basic = true;
export let language: string;
export let lines = 5;
export let extensions: Extension[] = [];
export let use_tab = true;
export let readonly = false;
export let placeholder: string | HTMLElement | null | undefined = undefined;
const dispatch = createEventDispatcher<{
change: string;
blur: undefined;
focus: undefined;
}>();
let lang_extension: Extension | undefined;
let element: HTMLDivElement;
let view: EditorView;
$: get_lang(language);
async function get_lang(val: string): Promise<void> {
const ext = await getLanguageExtension(val);
lang_extension = ext;
}
$: reconfigure(), lang_extension;
$: set_doc(value);
$: update_lines();
function set_doc(new_doc: string): void {
if (view && new_doc !== view.state.doc.toString()) {
view.dispatch({
changes: {
from: 0,
to: view.state.doc.length,
insert: new_doc
}
});
}
}
function update_lines(): void {
if (view) {
view.requestMeasure({ read: update_gutters });
}
}
function create_editor_view(): EditorView {
const editorView = new EditorView({
parent: element,
state: create_editor_state(value)
});
editorView.dom.addEventListener("focus", handle_focus, true);
editorView.dom.addEventListener("blur", handle_blur, true);
return editorView;
}
function handle_focus(): void {
dispatch("focus");
}
function handle_blur(): void {
dispatch("blur");
}
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 update_gutters(_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;
}
function handle_change(vu: ViewUpdate): void {
if (vu.docChanged) {
const doc = vu.state.doc;
const text = doc.toString();
value = text;
dispatch("change", text);
}
view.requestMeasure({ read: update_gutters });
}
function get_extensions(): Extension[] {
const stateExtensions = [
...get_base_extensions(
basic,
use_tab,
placeholder,
readonly,
lang_extension
),
FontTheme,
...get_theme(),
...extensions
];
return stateExtensions;
}
const FontTheme = EditorView.theme({
"&": {
fontSize: "var(--text-sm)",
backgroundColor: "var(--border-color-secondary)"
},
".cm-content": {
paddingTop: "5px",
paddingBottom: "5px",
color: "var(--body-text-color)",
fontFamily: "var(--font-mono)",
minHeight: "100%"
},
".cm-gutters": {
marginRight: "1px",
borderRight: "1px solid var(--border-color-primary)",
backgroundColor: "transparent",
color: "var(--body-text-color-subdued)"
},
".cm-focused": {
outline: "none"
},
".cm-scroller": {
height: "auto"
},
".cm-cursor": {
borderLeftColor: "var(--body-text-color)"
}
});
function create_editor_state(_value: string | null | undefined): EditorState {
return EditorState.create({
doc: _value ?? undefined,
extensions: get_extensions()
});
}
function get_base_extensions(
basic: boolean,
use_tab: boolean,
placeholder: string | HTMLElement | null | undefined,
readonly: boolean,
lang: Extension | null | undefined
): Extension[] {
const extensions: Extension[] = [
EditorView.editable.of(!readonly),
EditorState.readOnly.of(readonly),
EditorView.contentAttributes.of({ "aria-label": "Code input container" })
];
if (basic) {
extensions.push(basicSetup);
}
if (use_tab) {
extensions.push(keymap.of([indentWithTab]));
}
if (placeholder) {
extensions.push(placeholderExt(placeholder));
}
if (lang) {
extensions.push(lang);
}
extensions.push(EditorView.updateListener.of(handle_change));
return extensions;
}
function get_theme(): 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(get_extensions())
});
}
onMount(() => {
view = create_editor_view();
return () => view?.destroy();
});
</script>
<div class="wrap">
<div class="codemirror-wrapper {class_names}" bind:this={element} />
</div>
<style>
.wrap {
display: flex;
flex-direction: column;
flex-flow: column;
margin: 0;
padding: 0;
height: 100%;
}
.codemirror-wrapper {
height: 100%;
overflow: auto;
}
:global(.cm-editor) {
height: 100%;
}
/* 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>
|