anki/ts/editor/CodeMirror.svelte
Matthias Metelka 68fa661b53
Finish #2070: Single overlay instead of per field (#2144)
* Move up MathjaxOverlay to be initialized only once

* Move ImageOverlay to NoteEditor root

* Move Symbols Overlay to NoteEditor root

* Refactor image overlay to not require second mutation observer

* Use elevation + overflow:hidden  in Editorfield

* Make it possible to show input next to each other again

* Set handle background color to code bg

* Make Collapsible unmount the component

* Simplify how decorated elements are mounted

* Set RichTextInput background to frame-bg again

* Strip out FocusTrap code

* Revert "Make Collapsible unmount the component"

This reverts commit 52722065ea199fa57ae750fa34bf47ee1c5aab3c.

* Allow clicking on label container to unfocus field

* Fix mathjax overlay resetting too its api too soon

* Allow scrolling on overlays

* Set focus-border border-color in focused field

* Fix background color of fields

* Add back grid-gap

removed it during merge to see if margin-top would behave any differently - which is not the case.

* Fix double border issue within Collapsible.svelte

* Format

* Edit appearance of focused fields a bit

* Remove unused properties

* Include elevation in button_mixins_lib

* Give label-container a background color

Co-authored-by: Henrik Giesel <hengiesel@gmail.com>
2022-10-27 09:11:36 +10:00

112 lines
3.0 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 { createEventDispatcher, getContext, onMount } from "svelte";
import type { Writable } from "svelte/store";
import { directionKey } from "../lib/context-keys";
import { promiseWithResolver } from "../lib/promise";
import { pageTheme } from "../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,
};
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,
}}
/>
</div>
<style lang="scss">
.code-mirror {
height: 100%;
:global(.CodeMirror) {
height: auto;
}
:global(.CodeMirror-wrap pre) {
word-break: break-word;
}
}
</style>