EditScreen: Support ":w" in CodeMirror's Vim keymap

The Vim keymap can make use of a custom save command defined
in the CodeMirror.commands object. Adding this custom command
allows EditScreen to perform the actual save action instead of
showing an obscure TypeError.

Change-Id: I5bf01d520ed9a299685736368f43847f9bd33b63
This commit is contained in:
Michael Zhou
2016-03-25 17:54:16 -04:00
parent cced99ae2d
commit 561534b294
2 changed files with 19 additions and 0 deletions

View File

@@ -71,6 +71,7 @@ import com.google.gwtexpui.safehtml.client.SafeHtml;
import net.codemirror.lib.CodeMirror; import net.codemirror.lib.CodeMirror;
import net.codemirror.lib.CodeMirror.ChangesHandler; import net.codemirror.lib.CodeMirror.ChangesHandler;
import net.codemirror.lib.CodeMirror.CommandRunner;
import net.codemirror.lib.Configuration; import net.codemirror.lib.Configuration;
import net.codemirror.lib.KeyMap; import net.codemirror.lib.KeyMap;
import net.codemirror.lib.Pos; import net.codemirror.lib.Pos;
@@ -435,6 +436,13 @@ public class EditScreen extends Screen {
.set("keyMap", prefs.keyMapType().name().toLowerCase()) .set("keyMap", prefs.keyMapType().name().toLowerCase())
.set("theme", prefs.theme().name().toLowerCase()) .set("theme", prefs.theme().name().toLowerCase())
.set("mode", mode != null ? mode.mode() : null)); .set("mode", mode != null ? mode.mode() : null));
CodeMirror.addCommand("save", new CommandRunner() {
@Override
public void run(CodeMirror instance) {
save().run();
}
});
} }
private void renderLinks(EditFileInfo editInfo, private void renderLinks(EditFileInfo editInfo,

View File

@@ -364,6 +364,13 @@ public class CodeMirror extends JavaScriptObject {
$wnd.CodeMirror.keyMap[name] = km $wnd.CodeMirror.keyMap[name] = km
}-*/; }-*/;
public static final native void addCommand(String name, CommandRunner runner) /*-{
$wnd.CodeMirror.commands[name] = function(cm) {
runner.@net.codemirror.lib.CodeMirror.CommandRunner::run(
Lnet/codemirror/lib/CodeMirror;)(cm);
};
}-*/;
public final native Vim vim() /*-{ public final native Vim vim() /*-{
return this; return this;
}-*/; }-*/;
@@ -428,4 +435,8 @@ public class CodeMirror extends JavaScriptObject {
public interface ChangesHandler { public interface ChangesHandler {
void handle(CodeMirror instance); void handle(CodeMirror instance);
} }
public interface CommandRunner {
void run(CodeMirror instance);
}
} }