From 100efe4dde38a89bf3201250fb0fb39eeb27e68c Mon Sep 17 00:00:00 2001 From: Matt McCutchen Date: Tue, 7 Aug 2018 17:24:40 -0400 Subject: [PATCH] codemirror: Define the type of keymaps. --- types/codemirror/index.d.ts | 12 ++++++++---- types/codemirror/test/index.ts | 13 ++++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/types/codemirror/index.d.ts b/types/codemirror/index.d.ts index f71f741659..5ece4f497e 100644 --- a/types/codemirror/index.d.ts +++ b/types/codemirror/index.d.ts @@ -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; diff --git a/types/codemirror/test/index.ts b/types/codemirror/test/index.ts index 431a20efb6..bd2aefac88 100644 --- a/types/codemirror/test/index.ts +++ b/types/codemirror/test/index.ts @@ -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));