UNPKG

@bhsd/codemirror-mediawiki

Version:

Modified CodeMirror mode based on wikimedia/mediawiki-extensions-CodeMirror

154 lines (153 loc) 5.25 kB
import { EditorView } from '@codemirror/view'; import type { KeyBinding } from '@codemirror/view'; import type { Extension } from '@codemirror/state'; import type { SyntaxNode } from '@lezer/common'; import type { ConfigData } from 'wikiparser-node'; import type { MwConfig } from './token'; import type { DocRange, foldHandler } from './fold'; import type { Option, LiveOption } from './linter'; import type { LintSource, LintSources, LintSourceGetter } from './lintsource'; import type { detectIndent } from './indent'; import type statusBar from './statusBar'; export type AddonMain<T> = (config?: T, cm?: CodeMirror6) => Extension; export type Addon<T> = [AddonMain<T>, Record<string, T>?]; export type Dialect = 'sanitized-css' | undefined; declare interface MenuItem { name: string; isActionable(this: void, cm: CodeMirror6): boolean; getItems(this: void, cm: CodeMirror6): HTMLDivElement[]; } declare interface OptionalFunctions { statusBar: typeof statusBar; detectIndent: typeof detectIndent; foldHandler: typeof foldHandler; } export declare const plain: () => Extension; export declare const languages: Record<string, (config?: any) => Extension>; export declare const avail: Record<string, Addon<any>>; export declare const linterRegistry: Record<string, LintSourceGetter>; export declare const menuRegistry: MenuItem[]; export declare const destroyListeners: ((view: EditorView) => void)[]; export declare const themes: Record<string, Extension>; export declare const optionalFunctions: OptionalFunctions; /** CodeMirror 6 editor */ export declare class CodeMirror6 { #private; /** only for sanitized-css */ dialect: Dialect; getWikiConfig?: () => Promise<ConfigData>; langConfig: MwConfig | undefined; /** textarea element */ get textarea(): HTMLTextAreaElement; /** EditorView instance */ get view(): EditorView | undefined; /** language */ get lang(): string; /** whether the editor view is visible */ get visible(): boolean; /** * @param textarea textarea element * @param lang language * @param config language configuration * @param init whether to initialize the editor immediately */ constructor(textarea: HTMLTextAreaElement, lang?: string, config?: unknown, init?: boolean); /** * Initialize the editor * @param config language configuration */ initialize(config?: unknown): void; /** * Set language * @param lang language * @param config language configuration */ setLanguage(lang?: string, config?: unknown): Promise<void>; /** * Start syntax checking * @param lintSource function for syntax checking */ lint(lintSource?: LintSources): void; /** Update syntax checking immediately */ update(): void; /** * Check if the editor enables a specific extension * @param name extension name */ hasPreference(name: string): boolean; /** * Add extensions * @param names extension names */ prefer(names: string[] | Record<string, boolean>): void; /** * Set text indentation * @param indent indentation string */ setIndent(indent: string): void; /** * Set line wrapping * @param wrapping whether to enable line wrapping */ setLineWrapping(wrapping: boolean): void; /** * Get default linter * @param opt linter options */ getLinter(opt?: Option | LiveOption): Promise<LintSource | undefined>; /** * Set content * @param insert new content * @param force whether to forcefully replace the content */ setContent(insert: string, force?: boolean): void; /** * Switch between textarea and editor view * @param show whether to show the editor view */ toggle(show?: boolean): void; /** Destroy the editor */ destroy(): void; /** * Define extra key bindings * @param keys key bindings */ extraKeys(keys: KeyBinding[]): void; /** * Set translation messages * @param messages translation messages */ localize(messages?: Record<string, string>): void; /** * Get the syntax node at the specified position * @param position position */ getNodeAt(position: number): SyntaxNode | undefined; /** * Scroll to the specified position * @param position position or selection range */ scrollTo(position?: number | { anchor: number; head: number; }): void; /** * Set the editor theme * @param theme theme name * @since 3.3.0 */ setTheme(theme: string): void; /** * Replace the current selection with the result of a function * @param view EditorView instance * @param func function to produce the replacement text */ static replaceSelections(view: EditorView, func: (str: string, range: DocRange) => string | [string, number, number?]): void; /** * Convert a [WikiParser-Node](https://npmjs.com/package/wikiparser-node) configuration * to a CodeMirror-MediaWiki configuration * @param config WikiParser-Node configuration */ abstract static getMwConfig(config: ConfigData): MwConfig; } export {};