Inline Edit: Bind Ctrl/Cmd-S to save

Control/Command-S now saves the file while leaving the editor open.

To be consistent the Save button now only saves the file, but keeps
the editor open.

Cancel has been renamed to Close. Users that want to save and return
to the change must first click Save and then click Close.

Change-Id: Ib46a10b8f08362a0e5edf412982da52e692f4218
This commit is contained in:
Shawn Pearce
2015-01-02 10:58:19 -05:00
committed by David Pursehouse
parent 81aa90203c
commit b5e04f2a27
2 changed files with 36 additions and 15 deletions

View File

@@ -54,6 +54,7 @@ import com.google.gwtexpui.safehtml.client.SafeHtml;
import net.codemirror.lib.CodeMirror;
import net.codemirror.lib.CodeMirror.ChangesHandler;
import net.codemirror.lib.Configuration;
import net.codemirror.lib.KeyMap;
import net.codemirror.lib.Pos;
import net.codemirror.mode.ModeInfo;
import net.codemirror.mode.ModeInjector;
@@ -72,7 +73,7 @@ public class EditScreen extends Screen {
@UiField Element header;
@UiField Element project;
@UiField Element filePath;
@UiField Button cancel;
@UiField Button close;
@UiField Button save;
@UiField Element editor;
@@ -207,24 +208,21 @@ public class EditScreen extends Screen {
@UiHandler("save")
void onSave(@SuppressWarnings("unused") ClickEvent e) {
ChangeFileApi.putContentOrMessage(revision, path, cm.getValue(),
new GerritCallback<VoidResult>() {
@Override
public void onSuccess(VoidResult result) {
Gerrit.display(PageLinks.toChangeInEditMode(
revision.getParentKey()));
}
});
save().run();
}
@UiHandler("cancel")
void onCancel(@SuppressWarnings("unused") ClickEvent e) {
@UiHandler("close")
void onClose(@SuppressWarnings("unused") ClickEvent e) {
if (cm.isClean(generation)
|| Window.confirm(EditConstants.I.cancelUnsavedChanges())) {
Gerrit.display(PageLinks.toChangeInEditMode(revision.getParentKey()));
upToChange();
}
}
private void upToChange() {
Gerrit.display(PageLinks.toChangeInEditMode(revision.getParentKey()));
}
private void initEditor(String content) {
ModeInfo mode = prefs.syntaxHighlighting()
? ModeInfo.findMode(type, path)
@@ -243,6 +241,9 @@ public class EditScreen extends Screen {
.set("keyMap", "default")
.set("theme", prefs.theme().name().toLowerCase())
.set("mode", mode != null ? mode.mode() : null));
cm.addKeyMap(KeyMap.create()
.on("Cmd-S", save())
.on("Ctrl-S", save()));
}
private Runnable updateCursorPosition() {
@@ -274,6 +275,26 @@ public class EditScreen extends Screen {
cm.extras().activeLine(cm.getLineHandleVisualStart(p.line()));
}
private Runnable save() {
return new Runnable() {
@Override
public void run() {
if (!cm.isClean(generation)) {
String text = cm.getValue();
final int g = cm.changeGeneration(false);
ChangeFileApi.putContentOrMessage(revision, path, text,
new GerritCallback<VoidResult>() {
@Override
public void onSuccess(VoidResult result) {
generation = g;
save.setEnabled(!cm.isClean(g));
}
});
}
}
};
}
private void injectMode(String type, AsyncCallback<Void> cb) {
new ModeInjector().add(type).inject(cb);
}

View File

@@ -66,11 +66,11 @@ limitations under the License.
<g:HTMLPanel>
<div class='{style.headerLine}' ui:field='header'>
<div class='{style.headerButtons}'>
<g:Button ui:field='cancel'
<g:Button ui:field='close'
styleName=''
title='Cancel'>
title='Close file and return to change'>
<ui:attribute name='title'/>
<div><ui:msg>Cancel</ui:msg></div>
<div><ui:msg>Close</ui:msg></div>
</g:Button>
<g:Button ui:field='save'
styleName='{style.save}'