UNPKG

@pierre/diffs

Version:

207 lines (206 loc) 9.51 kB
import { DiffLineAnnotation, DiffsHighlighter, FileContents, FileDiffMetadata, LineAnnotation, RenderRange } from "../types.js"; import { EditState, EditorCaret, EditorChangeEvent, EditorEditCompleteEvent, EditorInitialState, EditorType, EditorViewState, Range, RetainedDiffSessionSnapshot, TextEdit } from "./types.js"; import { File } from "../components/File.js"; import { FileDiff } from "../components/FileDiff.js"; import { EditorKeymap } from "./command.js"; import { EditPredictContext, EditPredictProvider, EditPredictRequest, EditPredictResponse } from "./editPrediction.js"; import { LanguageConfigMap } from "./languages.js"; import { Marker } from "./marker.js"; import { AutoSurround } from "./selection.js"; import { SelectionActionContext } from "./selectionAction.js"; //#region src/editor/editor.d.ts interface SyncRenderViewBaseProps { highlighter: DiffsHighlighter; fileContainer: HTMLElement; renderRange: RenderRange | undefined; /** Start fresh history instead of retaining or extending the current history. */ resetHistory?: boolean; } interface SyncFileRenderViewProps<LAnnotation> extends SyncRenderViewBaseProps { file: FileContents; lineAnnotations: LineAnnotation<LAnnotation>[] | undefined; /** Treat the supplied contents as an externally provided document update. */ externalDocument?: boolean; } interface SyncDiffRenderViewProps<LAnnotation> extends SyncRenderViewBaseProps { fileDiff: FileDiffMetadata; lineAnnotations: DiffLineAnnotation<LAnnotation>[] | undefined; /** Treat the supplied contents as an externally provided document update. */ externalDocument?: boolean; } type SyncRenderViewProps<EType extends EditorType, LAnnotation> = EType extends 'file' ? SyncFileRenderViewProps<LAnnotation> : SyncDiffRenderViewProps<LAnnotation>; type EditorComponent<EType extends EditorType, LAnnotation, Caret> = EType extends 'file' ? File<LAnnotation, Caret> : FileDiff<LAnnotation, Caret>; interface EditorOptions<EType extends EditorType, LAnnotation, Caret> { /** The maximum number of entries to keep in the undo stack. */ historyMaxEntries?: number; /** * Retain and restore the attached component's vertical viewport position. * Defaults to false because scroll views generally contain multiple items. * This option is captured when the editor is constructed. */ ownsVerticalViewport?: boolean; /** * Document and editor state transferred to the first attachment. Missing * fields are initialized from the attached component. The editor takes * ownership and does not clone supplied objects. */ initialState?: EditorInitialState<EType, LAnnotation>; /** Custom keymap groups checked before defaults; later groups take precedence. */ keymap?: EditorKeymap; /** Render rounded corners for selection ranges, default is true. */ roundedSelection?: boolean; /** Highlight matching brackets near the caret, default is true. */ matchBrackets?: boolean; /** * Controls auto-surround when typing quotes or brackets over a selection. * Default is `"default"` (both quotes and brackets). */ autoSurround?: AutoSurround; /** Per-language comment tokens used by the comment commands. */ languageCommentConfig?: LanguageConfigMap; /** * Show a floating selection action popover after a user-created selection. * Defaults to disabled. Programmatic selection updates do not open it. */ enabledSelectionAction?: boolean; /** * Configuration for inline edit prediction. */ editPrediction?: { /** * The edit prediction mode. * - 'eager': predictions appear inline when the user types. * - 'subtle': pressing the `Alt` key toggles predictions inline. * @default 'eager' */ mode?: 'eager' | 'subtle'; /** * The edit prediction provider. */ provider: EditPredictProvider; /** * Glob or regular-expression patterns for files to include in prediction. * String patterns support `?`, segment-local `*`, and cross-segment `**`. * An empty array matches no files. */ include?: readonly (string | RegExp)[]; /** * Glob or regular-expression patterns for files to exclude from prediction. * String patterns support `?`, segment-local `*`, and cross-segment `**`. * Exclusions take precedence over inclusions. */ exclude?: readonly (string | RegExp)[]; }; /** * Custom clipboard provider. * Highly recommended to use native clipboard API if you are building an electron app. * see https://www.electronjs.org/docs/latest/api/clipboard */ clipboard?: { readText: (type?: string) => Promise<string> | string; }; /** Render the selection action widget element. */ renderSelectionAction?: (context: SelectionActionContext<EType, LAnnotation>) => HTMLElement; /** * Render an externally owned caret at its normalized document position. */ renderCaret?: (caret: EditorCaret<Caret>) => HTMLElement; /** Callback when the editor is attached to a file. */ onAttach?: (editor: Editor<EType, LAnnotation, Caret>, fileInstance: EditorComponent<EType, LAnnotation, Caret>) => void; /** * Called with an `EditorChangeEvent` whenever the editor document changes. * Treat this as a document notification; do not feed the changes back into * the editor or you will create loops. */ onChange?: (event: EditorChangeEvent<EType, LAnnotation, Caret>) => void; /** * Observes completion with the same frozen event sent to the component. Runs * before the component callback, including when the component callback is * missing. There is no way to accept or reject from this API. */ onComplete?: (event: EditorEditCompleteEvent<EType, LAnnotation, Caret>) => void; /** Callback when the editor gains focus. */ onFocus?: () => void; /** Callback when the editor loses focus. */ onBlur?: () => void; __debug?: boolean; } interface EditorFocusOptions extends FocusOptions { /** One-based document line number or first editable line with a visible top. */ lineNumber?: number | 'first-visible'; /** Zero-based character offset for a numeric line. Defaults to 0. */ character?: number; /** Non-negative CSS pixels below the viewport or sticky header. */ offset?: number; } declare class Editor<EType extends EditorType = EditorType, LAnnotation = undefined, Caret = undefined> { #private; readonly type: EType; /** * @param type The component surface this editor can attach to. * @param options Configure editor behavior and lifecycle callbacks. * @param editStateKey Retain this editable draft and its undo/redo history * in memory so a later editor using the same type and key can resume them. */ constructor(type: EType, options?: EditorOptions<EType, LAnnotation, Caret>, editStateKey?: string); setOptions(options: EditorOptions<EType, LAnnotation, Caret>): void; __emitEditComplete(event: EditorEditCompleteEvent<EType, LAnnotation, Caret>): void; /** @internal */ __getGhostTextRows(): ReadonlyMap<number, number>; setCarets(carets: EditorCaret<Caret>[]): void; edit<T extends EditorComponent<EType, LAnnotation, Caret>>(fileInstance: T extends { readonly type: 'unresolved-file'; } ? never : T): () => void; /** * Apply edits to current attached file. Every edit joins the undo timeline: * a programmatic edit must leave the document and its history exactly as * the same edit typed by the user would (history equivalence — see * TextDocument.applyResolvedEdits), so it is undoable like any other edit. * * @param updateHistory Whether to record caller selection snapshots for * exact undo/redo restoration. Defaults to true. When false, live selections * are remapped during replay and the text edit still joins the undo timeline. */ applyEdits(edits: TextEdit[], updateHistory?: boolean): void; /** Whether there is an edit to undo. */ get canUndo(): boolean; /** Whether there is an undone edit to redo. */ get canRedo(): boolean; /** Undo the last edit. Does nothing when there is nothing to undo. */ undo(): void; /** Redo the last undone edit. Does nothing when there is nothing to redo. */ redo(): void; getFile(): FileContents | undefined; getText(): string; /** Return an isolated copy of selections and restorable view state. */ getViewState(): EditorViewState; /** * Return the objects that make up the active edit session, or undefined when * no complete session exists. */ getEditState(): EditState<EType, LAnnotation> | undefined; setViewState({ selections, view }: EditorViewState): void; setSelections(selections: (Range & { direction: 'none' | 'backward' | 'forward'; })[]): void; setMarkers(markers: Marker[]): void; focus(options?: EditorFocusOptions): void; blur(): void; cleanUp(reason?: 'discard' | 'recycle' | 'complete'): void; /** @internal */ __postponeBgTokenizeToNextFrame(): void; /** @internal */ __captureFocusForDOMReplacement(): void; /** @internal */ __getDocumentContents(fallbackFile?: FileContents): FileContents | undefined; /** @internal */ __getDocumentSessionState(): RetainedDiffSessionSnapshot | undefined; /** @internal */ __syncRenderView(props: SyncRenderViewProps<EType, LAnnotation>): void; } //#endregion export { type EditPredictContext, type EditPredictProvider, type EditPredictRequest, type EditPredictResponse, Editor, EditorFocusOptions, EditorOptions }; //# sourceMappingURL=editor.d.ts.map