anki/ts/editor/index.ts

104 lines
2.8 KiB
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
/* eslint
@typescript-eslint/no-non-null-assertion: "off",
@typescript-eslint/no-explicit-any: "off",
*/
import { filterHTML } from "../html-filter";
import { execCommand } from "./helpers";
import { updateAllState } from "../components/WithState.svelte";
export function pasteHTML(
html: string,
internal: boolean,
extendedMode: boolean,
): void {
html = filterHTML(html, internal, extendedMode);
if (html !== "") {
setFormat("inserthtml", html);
}
}
export function setFormat(cmd: string, arg?: string, _nosave = false): void {
execCommand(cmd, false, arg);
updateAllState(new Event(cmd));
}
export { editorToolbar } from "./EditorToolbar.svelte";
import "../sveltelib/export-runtime";
import "../lib/register-package";
import { setupI18n, ModuleName } from "../lib/i18n";
import { isApplePlatform } from "../lib/platform";
import { registerShortcut } from "../lib/shortcuts";
import { bridgeCommand } from "../lib/bridgecommand";
declare global {
interface Selection {
modify(s: string, t: string, u: string): void;
addRange(r: Range): void;
removeAllRanges(): void;
getRangeAt(n: number): Range;
}
}
if (isApplePlatform()) {
registerShortcut(() => bridgeCommand("paste"), "Control+Shift+V");
}
export const i18n = setupI18n({
modules: [
ModuleName.EDITING,
ModuleName.KEYBOARD,
ModuleName.ACTIONS,
ModuleName.BROWSING,
],
});
import OldEditorAdapter from "./OldEditorAdapter.svelte";
import type { NoteEditorAPI } from "./OldEditorAdapter.svelte";
import { nightModeKey } from "../components/context-keys";
import "./editor-base.css";
import "./bootstrap.css";
import "./legacy.css";
async function setupNoteEditor(): Promise<NoteEditorAPI> {
const context = new Map<symbol, unknown>();
context.set(
nightModeKey,
document.documentElement.classList.contains("night-mode"),
);
await i18n;
const noteEditor = new OldEditorAdapter({
target: document.body,
context,
});
Object.assign(globalThis, {
setFields: noteEditor.setFields,
setFonts: noteEditor.setFonts,
focusField: noteEditor.focusField,
setColorButtons: noteEditor.setColorButtons,
setTags: noteEditor.setTags,
setSticky: noteEditor.setSticky,
setBackgrounds: noteEditor.setBackgrounds,
setClozeHint: noteEditor.setClozeHint,
saveNow: noteEditor.saveFieldNow,
activateStickyShortcuts: noteEditor.activateStickyShortcuts,
focusIfField: noteEditor.focusIfField,
setNoteId: noteEditor.setNoteId,
});
return noteEditor.api;
}
export const noteEditorPromise = setupNoteEditor();