Skip to content

Commit

Permalink
feat(client): add save using backend
Browse files Browse the repository at this point in the history
Related to #894
  • Loading branch information
nikku committed Sep 24, 2018
1 parent 772b163 commit 8ac2e78
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 6 deletions.
32 changes: 28 additions & 4 deletions client/src/app/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ export class App extends Component {
}
}

async saveTab(tab) {
async saveTab(tab, options = {}) {
await this.showTab(tab);

const action = await this.askSave(tab);
Expand All @@ -430,9 +430,20 @@ export class App extends Component {
if (action === 'save') {
const contents = await this.tabRef.current.triggerAction('save');

// TODO(nikku): actually save using backend
// TODO(nikku): update dirty state once saved
console.log('saved contents', contents);
// unsaved ?
if (!tab.file.path) {
options = {
...options,
saveAs: true
};
}

const newFile = await this.props.globals.fileSystem.writeFile({
...tab.file,
contents
}, options);

tab.file = newFile;
}
}

Expand Down Expand Up @@ -484,6 +495,11 @@ export class App extends Component {

triggerAction = (action, options) => {

const {
activeTab
} = this.state;


console.log('App#triggerAction %s %o', action, options);

if (action === 'select-tab') {
Expand Down Expand Up @@ -522,6 +538,14 @@ export class App extends Component {
return this.saveAllTabs();
}

if (action === 'save') {
return this.saveTab(activeTab);
}

if (action === 'save-as') {
return this.saveTab(activeTab, { saveAs: true });
}

if (action === 'quit') {
return this.quit();
}
Expand Down
6 changes: 4 additions & 2 deletions client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import mitt from 'mitt';

import {
backend,
dialog
dialog,
fileSystem
} from './remote';

const eventBus = mitt();
Expand All @@ -17,7 +18,8 @@ const tabsProvider = new TabsProvider();
const globals = {
backend,
dialog,
eventBus
eventBus,
fileSystem
};

const rootElement = document.getElementById('root');
Expand Down
42 changes: 42 additions & 0 deletions client/src/remote/FileSystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* File system API used by app.
*/
export default class FileSystem {

constructor(backend) {
this.backend = backend;
}

/**
* Read file and return it.
*
* @param {String} filePath
*/
readFile(filePath) {
return this.backend.send('file:read', filePath);
}

/**
* Read file attributes, but skip content.
*
* @param {String} filePath
*/
readFileStats(filePath) {
return this.backend.send('file:read-stats', filePath);
}


/**
* Write file and return a promise for the written file.
*
* @param {File} file
* @param {Object} options
*/
writeFile(file, options) {

const action = options && options.saveAs ? 'file:save-as' : 'file:save';

return this.backend.send(action, file);
}

}
3 changes: 3 additions & 0 deletions client/src/remote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import { electronRequire } from './electron';

import Backend from './Backend';
import Dialog from './Dialog';
import FileSystem from './FileSystem';

export const ipcRenderer = electronRequire('ipcRenderer');

export const backend = new Backend(ipcRenderer);

export const fileSystem = new FileSystem(backend);

export const dialog = new Dialog(backend);

0 comments on commit 8ac2e78

Please sign in to comment.