Anki/ts/editor/CodeMirror.svelte
Abdo bf46a5f08c
Update to Svelte 5 (#3292)
* Update to Svelte 5

* Fix `<tr> is invalid inside <table>`

* Update sveltekit-svg to match svelte version

Fixes deck options failing to load, and a bunch of warnings with
./yarn dev

* Fix graph tooltips

* Fix editor loading

* Fix MathJax editor not loading

* Formatting

* Fix new formatting errors

* Merge remote-tracking branch 'origin/main' into svelte5

* Remove slot inside EditorToolbar

I think this is just stray code left over from a refactor, but I'm
not 100% sure.

Fixes
Error: Object literal may only specify known properties, and 'children' does not exist in type '{ size: number; wrap: boolean; api?: Partial<EditorToolbarAPI> | undefined; }'. (ts)
<div class="note-editor">
    <EditorToolbar {size} {wrap} api={toolbar}>
        <slot slot="notetypeButtons" name="notetypeButtons" />

* Fix component typing error

* Comment out svelte/internal exports, so editor loads

* Fix image occlusions in editor

* Revert "Remove slot inside EditorToolbar"

This reverts commit b3095e07ac,
which prevented the Preview button from showing in the browser.

This will break our tests again.

* Update vite

* Disable routes/tmp for now

* Fix references issue in routes/tmp
2024-09-25 18:49:07 +10:00

114 lines
3.1 KiB
Svelte

<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script context="module" lang="ts">
import type CodeMirrorLib from "codemirror";
export interface CodeMirrorAPI {
readonly editor: Promise<CodeMirrorLib.Editor>;
setOption<T extends keyof CodeMirrorLib.EditorConfiguration>(
key: T,
value: CodeMirrorLib.EditorConfiguration[T],
): Promise<void>;
}
</script>
<script lang="ts">
import { directionKey } from "@tslib/context-keys";
import { promiseWithResolver } from "@tslib/promise";
import { createEventDispatcher, getContext, onMount } from "svelte";
import type { Writable } from "svelte/store";
import { pageTheme } from "$lib/sveltelib/theme";
import {
darkTheme,
lightTheme,
openCodeMirror,
setupCodeMirror,
} from "./code-mirror";
export let configuration: CodeMirrorLib.EditorConfiguration;
export let code: Writable<string>;
export let hidden = false;
const defaultConfiguration = {
rtlMoveVisually: true,
lineNumbers: false,
};
const [editorPromise, resolve] = promiseWithResolver<CodeMirrorLib.Editor>();
/**
* Convenience function for editor.setOption.
*/
async function setOption<T extends keyof CodeMirrorLib.EditorConfiguration>(
key: T,
value: CodeMirrorLib.EditorConfiguration[T],
): Promise<void> {
const editor = await editorPromise;
editor.setOption(key, value);
}
const direction = getContext<Writable<"ltr" | "rtl">>(directionKey);
let apiPartial: Partial<CodeMirrorAPI>;
export { apiPartial as api };
Object.assign(apiPartial, {
editor: editorPromise,
setOption,
});
const dispatch = createEventDispatcher();
onMount(async () => {
const editor = await editorPromise;
setupCodeMirror(editor, code);
editor.on("change", () => dispatch("change", editor.getValue()));
editor.on("focus", (codeMirror, event) =>
dispatch("focus", { codeMirror, event }),
);
editor.on("blur", (codeMirror, event) =>
dispatch("blur", { codeMirror, event }),
);
editor.on("keydown", (codeMirror, event) => {
if (event.code === "Tab") {
dispatch("tab", { codeMirror, event });
}
});
});
</script>
<div class="code-mirror">
<textarea
tabindex="-1"
hidden
use:openCodeMirror={{
configuration: {
...configuration,
...defaultConfiguration,
direction: $direction,
theme: $pageTheme.isDark ? darkTheme : lightTheme,
},
resolve,
hidden,
}}
></textarea>
</div>
<style lang="scss">
.code-mirror {
height: 100%;
:global(.CodeMirror) {
height: auto;
font-family: Consolas, monospace;
}
:global(.CodeMirror-wrap pre) {
word-break: break-word;
}
}
</style>