mirror of
https://github.com/ankitects/anki.git
synced 2025-11-09 06:07:11 -05:00
* Add componentHook functionality
* Register package NoteEditor
* Rename OldEditorAdapter to NoteEditor
* Expose instances in component-hook as well
* Rename NoteTypeButtons to NotetypeButtons
* Move PreviewButton initialization to BrowserEditor.svelte
* Remove focusInRichText
- Same thing can be done by inspecting activeInput
* Satisfy formatter
* Fix remaining rebase issues
* Add .bazel to .prettierignore
* Rename currentField and activeInput to focused{Field,Input}
* Move identifier to lib and registration to sveltelib
* Fix Dynamic component insertion
* Simplify editingInputIsRichText
* Give extra warning in svelte/svelte.ts
- This was caused by doing a rename of a files, that only differed in
case: NoteTypeButtons.svelte to NotetypeButtons.svelte
- It was quite tough to figure out, and this console.log might make it
easier if it ever happens again
* Change signature of contextProperty
* Add ts/typings for add-on definition files
* Add Anki types in typings/common/index.d.ts
* Export without .svelte suffix
It conflicts with how Svelte types its packages
* Fix left over .svelte import from editor.py
* Rename NoteTypeButtons to unrelated to ensure case-only rename
* Rename back to NotetypeButtons.svelte
* Remove unused component-hook.ts, Fix typing in lifecycle-hooks
* Merge runtime-require and register-package into one file
+ Give some preliminary types to require
* Rename uiDidLoad to loaded
* Fix eslint / svelte-check
* Rename context imports to noteEditorContext
* Fix import name mismatch
- I wonder why these issues are not caught by svelte-check?
* Rename two missed usages of uiDidLoad
* Fix ButtonDropdown from having wrong border-radius
* Uniformly rename libraries to packages
- I don't have a strong opinion on whether to name them libraries or
packages, I just think we should have a uniform name.
- JS/TS only uses the terms "module" and "namespace", however `package`
is a reserved keyword for future use, whereas `library` is not.
* Refactor registration.ts into dynamic-slotting
- This is part of an effort to refactor the dynamic slotting (extending
buttons) functionality out of components like ButtonGroup.
* Remove dynamically-slottable logic from ButtonToolbar
* Use DynamicallySlottable in editor-toolbar
* Fix no border radius on indentation button dropdown
* Fix AddonButtons
* Remove Item/ButtonGroupItem in deck-options, where it's not necessary
* Remove unnecessary uses of Item and ButtonGroupItem
* Fix remaining tests
* Fix relative imports
* Revert change return value of remapBinToSrcDir to ./bazel/out...
* Remove typings directory
* Adjust comments for dynamic-slottings
184 lines
4.5 KiB
Svelte
184 lines
4.5 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 { EditingInputAPI } from "../EditingArea.svelte";
|
|
import type { CodeMirror as CodeMirrorType } from "../code-mirror";
|
|
|
|
export interface PlainTextInputAPI extends EditingInputAPI {
|
|
name: "plain-text";
|
|
moveCaretToEnd(): void;
|
|
toggle(): boolean;
|
|
getEditor(): CodeMirrorType.Editor;
|
|
}
|
|
|
|
export const parsingInstructions: string[] = [];
|
|
</script>
|
|
|
|
<script lang="ts">
|
|
import { tick, onMount } from "svelte";
|
|
import { writable } from "svelte/store";
|
|
import { pageTheme } from "../../sveltelib/theme";
|
|
import { context as editingAreaContext } from "../EditingArea.svelte";
|
|
import { context as decoratedElementsContext } from "../DecoratedElements.svelte";
|
|
import CodeMirror from "../CodeMirror.svelte";
|
|
import type { CodeMirrorAPI } from "../CodeMirror.svelte";
|
|
import { htmlanki, baseOptions, gutterOptions } from "../code-mirror";
|
|
|
|
export let hidden = false;
|
|
|
|
const configuration = {
|
|
mode: htmlanki,
|
|
...baseOptions,
|
|
...gutterOptions,
|
|
};
|
|
|
|
const { editingInputs, content } = editingAreaContext.get();
|
|
const decoratedElements = decoratedElementsContext.get();
|
|
const code = writable($content);
|
|
|
|
function adjustInputHTML(html: string): string {
|
|
for (const component of decoratedElements) {
|
|
html = component.toUndecorated(html);
|
|
}
|
|
|
|
return html;
|
|
}
|
|
|
|
const parser = new DOMParser();
|
|
|
|
function removeTag(element: HTMLElement, tagName: string): void {
|
|
for (const elem of element.getElementsByTagName(tagName)) {
|
|
elem.remove();
|
|
}
|
|
}
|
|
|
|
function parseAsHTML(html: string): string {
|
|
const doc = parser.parseFromString(
|
|
parsingInstructions.join("") + html,
|
|
"text/html",
|
|
);
|
|
const body = doc.body;
|
|
|
|
removeTag(body, "script");
|
|
removeTag(body, "link");
|
|
removeTag(body, "style");
|
|
|
|
return doc.body.innerHTML;
|
|
}
|
|
|
|
function adjustOutputHTML(html: string): string {
|
|
for (const component of decoratedElements) {
|
|
html = component.toStored(html);
|
|
}
|
|
|
|
return html;
|
|
}
|
|
|
|
let codeMirror: CodeMirrorAPI;
|
|
|
|
function focus(): void {
|
|
codeMirror?.editor.focus();
|
|
}
|
|
|
|
function refocus(): void {
|
|
(codeMirror?.editor as any).display.input.blur();
|
|
focus();
|
|
}
|
|
|
|
function moveCaretToEnd(): void {
|
|
codeMirror?.editor.setCursor(codeMirror.editor.lineCount(), 0);
|
|
}
|
|
|
|
function toggle(): boolean {
|
|
hidden = !hidden;
|
|
return hidden;
|
|
}
|
|
|
|
function getEditor(): CodeMirrorType.Editor {
|
|
return codeMirror?.editor;
|
|
}
|
|
|
|
export const api = {
|
|
name: "plain-text",
|
|
focus,
|
|
focusable: !hidden,
|
|
moveCaretToEnd,
|
|
refocus,
|
|
toggle,
|
|
getEditor,
|
|
} as PlainTextInputAPI;
|
|
|
|
function pushUpdate(): void {
|
|
api.focusable = !hidden;
|
|
$editingInputs = $editingInputs;
|
|
}
|
|
|
|
function refresh() {
|
|
codeMirror.editor.refresh();
|
|
}
|
|
|
|
$: {
|
|
hidden;
|
|
tick().then(refresh);
|
|
pushUpdate();
|
|
}
|
|
|
|
onMount(() => {
|
|
$editingInputs.push(api);
|
|
$editingInputs = $editingInputs;
|
|
|
|
const unsubscribeFromEditingArea = content.subscribe((value: string): void => {
|
|
const adjusted = adjustInputHTML(value);
|
|
code.set(adjusted);
|
|
});
|
|
|
|
const unsubscribeToEditingArea = code.subscribe((value: string): void => {
|
|
const parsed = parseAsHTML(value);
|
|
content.set(adjustOutputHTML(parsed));
|
|
});
|
|
|
|
return () => {
|
|
unsubscribeFromEditingArea();
|
|
unsubscribeToEditingArea();
|
|
};
|
|
});
|
|
</script>
|
|
|
|
<div
|
|
class="plain-text-input"
|
|
class:light-theme={!$pageTheme.isDark}
|
|
class:hidden
|
|
on:focusin
|
|
on:focusout
|
|
>
|
|
<CodeMirror
|
|
{configuration}
|
|
{code}
|
|
bind:api={codeMirror}
|
|
on:change={({ detail: html }) => code.set(parseAsHTML(html))}
|
|
/>
|
|
</div>
|
|
|
|
<style lang="scss">
|
|
.plain-text-input {
|
|
overflow-x: hidden;
|
|
|
|
:global(.CodeMirror) {
|
|
border-radius: 0 0 5px 5px;
|
|
}
|
|
|
|
:global(.CodeMirror-lines) {
|
|
padding: 6px 0;
|
|
}
|
|
|
|
&.hidden {
|
|
display: none;
|
|
}
|
|
}
|
|
|
|
.light-theme :global(.CodeMirror) {
|
|
border-top: 1px solid #ddd;
|
|
}
|
|
</style>
|