codemirror: Define the type of keymaps.

This commit is contained in:
Matt McCutchen
2018-08-07 17:24:40 -04:00
parent e9928ee53e
commit 100efe4dde
2 changed files with 20 additions and 5 deletions

View File

@@ -15,7 +15,7 @@ declare function CodeMirror(callback: (host: HTMLElement) => void , options?: Co
declare namespace CodeMirror {
export var Doc : CodeMirror.DocConstructor;
export var Pos: CodeMirror.PositionConstructor;
export var Pass: any;
export var Pass: {toString(): "CodeMirror.PASS"};
function countColumn(line: string, index: number | null, tabSize: number): number;
function fromTextArea(host: HTMLTextAreaElement, options?: EditorConfiguration): CodeMirror.EditorFromTextArea;
@@ -129,6 +129,10 @@ declare namespace CodeMirror {
state: any;
}
interface KeyMap {
[keyName: string]: false | string | ((instance: Editor) => void | typeof Pass);
}
interface Editor {
/** Tells you whether the editor currently has focus. */
@@ -159,11 +163,11 @@ declare namespace CodeMirror {
Maps added in this way have a higher precedence than the extraKeys and keyMap options, and between them,
the maps added earlier have a lower precedence than those added later, unless the bottom argument was passed,
in which case they end up below other keymaps added with this method. */
addKeyMap(map: any, bottom?: boolean): void;
addKeyMap(map: string | KeyMap, bottom?: boolean): void;
/** Disable a keymap added with addKeyMap.Either pass in the keymap object itself , or a string,
which will be compared against the name property of the active keymaps. */
removeKeyMap(map: any): void;
removeKeyMap(map: string | KeyMap): void;
/** Enable a highlighting overlay.This is a stateless mini - mode that can be used to add extra highlighting.
For example, the search add - on uses it to highlight the term that's currently being searched.
@@ -781,7 +785,7 @@ declare namespace CodeMirror {
keyMap?: string;
/** Can be used to specify extra keybindings for the editor, alongside the ones defined by keyMap. Should be either null, or a valid keymap value. */
extraKeys?: any;
extraKeys?: string | KeyMap;
/** Whether CodeMirror should scroll or wrap for long lines. Defaults to false (scroll). */
lineWrapping?: boolean;

View File

@@ -4,7 +4,18 @@ var myCodeMirror: CodeMirror.Editor = CodeMirror(document.body);
var myCodeMirror2: CodeMirror.Editor = CodeMirror(document.body, {
value: "function myScript(){return 100;}\n",
mode: "javascript"
mode: "javascript",
extraKeys: {
Enter: (cm) => { console.log("save"); },
Esc: (cm) => { return CodeMirror.Pass; }
}
});
// $ExpectError
var myCodeMirror2_1: CodeMirror.Editor = CodeMirror(document.body, {
extraKeys: {
"Shift-Enter": (cm) => { return 42; } // not a valid return value
}
});
var range = myCodeMirror2.findWordAt(CodeMirror.Pos(0, 2));