From 2d432d65822d8dda8d8fe6bc8527ac2de764a704 Mon Sep 17 00:00:00 2001 From: Matt McCutchen Date: Tue, 7 Aug 2018 17:23:35 -0400 Subject: [PATCH 1/3] codemirror: Enable strictNullChecks so I can test typing changes that are only relevant with strictNullChecks enabled. --- types/codemirror/test/index.ts | 4 ++-- types/codemirror/tsconfig.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/types/codemirror/test/index.ts b/types/codemirror/test/index.ts index 39c601f159..9b9231b25c 100644 --- a/types/codemirror/test/index.ts +++ b/types/codemirror/test/index.ts @@ -13,9 +13,9 @@ var head = range.head; var from = range.from(); var to = range.to(); -var myTextArea: HTMLTextAreaElement; +var myTextArea: HTMLTextAreaElement = undefined!; var myCodeMirror3: CodeMirror.Editor = CodeMirror(function (elt) { - myTextArea.parentNode.replaceChild(elt, myTextArea); + myTextArea.parentNode!.replaceChild(elt, myTextArea); }, { value: myTextArea.value }); var myCodeMirror4: CodeMirror.Editor = CodeMirror.fromTextArea(myTextArea); diff --git a/types/codemirror/tsconfig.json b/types/codemirror/tsconfig.json index 2ff349d98e..a2c6a52430 100644 --- a/types/codemirror/tsconfig.json +++ b/types/codemirror/tsconfig.json @@ -7,7 +7,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ From e9928ee53e6bd74af37e90acc57087bbdac412ba Mon Sep 17 00:00:00 2001 From: Matt McCutchen Date: Tue, 7 Aug 2018 17:24:10 -0400 Subject: [PATCH 2/3] codemirror: EditorChangeCancellable.update is undefined for changes coming from undo/redo. --- types/codemirror/index.d.ts | 5 +++-- types/codemirror/test/index.ts | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/types/codemirror/index.d.ts b/types/codemirror/index.d.ts index 6dbfef6e60..f71f741659 100644 --- a/types/codemirror/index.d.ts +++ b/types/codemirror/index.d.ts @@ -716,8 +716,9 @@ declare namespace CodeMirror { } interface EditorChangeCancellable extends CodeMirror.EditorChange { - /** may be used to modify the change. All three arguments to update are optional, and can be left off to leave the existing value for that field intact. */ - update(from?: CodeMirror.Position, to?: CodeMirror.Position, text?: string[]): void; + /** may be used to modify the change. All three arguments to update are optional, and can be left off to leave the existing value for that field intact. + If the change came from undo/redo, `update` is undefined and the change cannot be modified. */ + update?(from?: CodeMirror.Position, to?: CodeMirror.Position, text?: string[]): void; cancel(): void; } diff --git a/types/codemirror/test/index.ts b/types/codemirror/test/index.ts index 9b9231b25c..431a20efb6 100644 --- a/types/codemirror/test/index.ts +++ b/types/codemirror/test/index.ts @@ -67,6 +67,15 @@ myCodeMirror.on( (instance: CodeMirror.Editor, line: CodeMirror.LineHandle, element: HTMLElement) => { } ); +myCodeMirror.on( + "beforeChange", + (instance: CodeMirror.Editor, change: CodeMirror.EditorChangeCancellable) => { + // $ExpectError + change.update(); + if (change.update != null) change.update(); + } +); + CodeMirror.registerHelper("lint", "javascript", {}); myCodeMirror.isReadOnly(); From 100efe4dde38a89bf3201250fb0fb39eeb27e68c Mon Sep 17 00:00:00 2001 From: Matt McCutchen Date: Tue, 7 Aug 2018 17:24:40 -0400 Subject: [PATCH 3/3] 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));