Skip to content

Commit 0b5c4a3

Browse files
committed
updated editor with safety
1 parent 67a542c commit 0b5c4a3

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

frontend/src/views/files/Editor.vue

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export default {
3131
},
3232
// Use beforeRouteUpdate to react to file changes
3333
beforeRouteUpdate(to, from, next) {
34-
3534
// Destroy the old editor instance to ensure a clean state
3635
if (this.editor) {
3736
this.editor.destroy();
@@ -62,7 +61,7 @@ export default {
6261
},
6362
methods: {
6463
setupEditor(attempt = 1) {
65-
this.filename = decodeURIComponent(this.$route.path.split("/").pop())
64+
this.filename = decodeURIComponent(this.$route.path.split("/").pop());
6665
// Safety Check 1: Use the component's 'filename' data property for comparison
6766
if (state.req.name !== this.filename) {
6867
if (attempt < 5) {
@@ -101,27 +100,37 @@ export default {
101100
readOnly: state.req.type === "textImmutable",
102101
wrap: false,
103102
});
104-
this.filename = decodeURIComponent(this.$route.path.split("/").pop())
103+
this.filename = decodeURIComponent(this.$route.path.split("/").pop());
105104
},
106105
handleEditorValueRequest() {
107106
// Safety Check 2: Final verification before saving
108107
if (state.req.name !== this.filename) {
109108
// Corrected the error message to be more accurate
110-
const errorMsg = `CRITICAL: Save operation aborted. The application's active file ("${state.req.name}") does not match the file you are trying to save ("${this.filename}").`;
109+
const errorMsg = `Save operation aborted. The application's active file ("${state.req.name}") does not match the file you are trying to save ("${this.filename}").`;
111110
notify.showError(errorMsg);
112111
return;
113112
}
114-
115-
if (this.editor) {
116-
filesApi.put(state.req.path, state.req.source, this.editor.getValue());
113+
try {
114+
// Attempt to save the editor content
115+
if (this.editor) {
116+
filesApi.put(state.req.path, state.req.source, this.editor.getValue());
117+
} else {
118+
notify.showError("Editor instance is not initialized.");
119+
return;
120+
}
121+
notify.showSuccess("File saved successfully.");
122+
} catch (error) {
123+
notify.showError("Failed to save file. Please try again.");
117124
}
118125
},
119126
keyEvent(event) {
120127
const { key, ctrlKey, metaKey } = event;
121128
if (getters.currentPromptName() != null) return;
122-
if (!ctrlKey && !metaKey) return;
123-
if (key.toLowerCase() === "s") {
129+
// Check if either Ctrl or Cmd is pressed along with the 'S' key.
130+
if ((ctrlKey || metaKey) && key.toLowerCase() === "s") {
131+
// This is the save shortcut, so prevent the browser's default save action.
124132
event.preventDefault();
133+
// Call your save handler.
125134
this.handleEditorValueRequest();
126135
}
127136
},

0 commit comments

Comments
 (0)