File watchers
This commit is contained in:
parent
9f6389845a
commit
2b86f2d741
|
@ -15,6 +15,7 @@ import Module from './modulebase';
|
|||
import { WindowUtils, FileUtils } from './utils';
|
||||
import BDIpc from './bdipc';
|
||||
import sass from 'node-sass';
|
||||
import chokidar from 'chokidar';
|
||||
|
||||
export default class Editor extends Module {
|
||||
|
||||
|
@ -23,6 +24,7 @@ export default class Editor extends Module {
|
|||
this.editorPath = path;
|
||||
this.bd = bd;
|
||||
this.initListeners();
|
||||
this.initWatchers();
|
||||
}
|
||||
|
||||
initListeners() {
|
||||
|
@ -148,6 +150,53 @@ export default class Editor extends Module {
|
|||
});
|
||||
}
|
||||
|
||||
initWatchers() {
|
||||
this.fileWatcher = chokidar.watch(this.bd.config.getPath('userfiles'));
|
||||
this.snippetWatcher = chokidar.watch(this.bd.config.getPath('snippets'));
|
||||
|
||||
this.fileWatcher.on('add', file => {
|
||||
const fileName = path.basename(file);
|
||||
try {
|
||||
this.send('editor-addFile', {
|
||||
type: 'file',
|
||||
name: fileName,
|
||||
saved: true,
|
||||
mode: this.resolveMode(fileName),
|
||||
content: '',
|
||||
savedContent: ''
|
||||
});
|
||||
} catch (err) {}
|
||||
});
|
||||
|
||||
this.fileWatcher.on('unlink', file => {
|
||||
const fileName = path.basename(file);
|
||||
try {
|
||||
this.send('editor-remFile', { name: fileName });
|
||||
} catch (err) {}
|
||||
});
|
||||
|
||||
this.snippetWatcher.on('add', file => {
|
||||
const fileName = path.basename(file);
|
||||
try {
|
||||
this.send('editor-addSnippet', {
|
||||
type: 'snippet',
|
||||
name: fileName,
|
||||
saved: true,
|
||||
mode: this.resolveMode(fileName),
|
||||
content: '',
|
||||
savedContent: ''
|
||||
});
|
||||
} catch (err) { }
|
||||
});
|
||||
|
||||
this.snippetWatcher.on('unlink', file => {
|
||||
const fileName = path.basename(file);
|
||||
try {
|
||||
this.send('editor-remSnippet', { name: fileName });
|
||||
} catch (err) { }
|
||||
});
|
||||
}
|
||||
|
||||
resolveMode(fileName) {
|
||||
if (!fileName.includes('.')) return 'text';
|
||||
const ext = fileName.substr(fileName.lastIndexOf('.') + 1);
|
||||
|
|
|
@ -63,8 +63,23 @@
|
|||
},
|
||||
components: { BDEdit },
|
||||
created() {
|
||||
ClientIPC.on('bd-editor-addFile', (_, file) => this.addFile(file));
|
||||
ClientIPC.on('bd-editor-addSnippet', (_, snippet) => this.addSnippet(snippet));
|
||||
ClientIPC.on('bd-editor-addFile', (_, file) => {
|
||||
if (this.files.find(f => f.name === file.name)) return;
|
||||
this.addFile(file);
|
||||
});
|
||||
|
||||
ClientIPC.on('bd-editor-remFile', (_, file) => {
|
||||
this.files = this.files.filter(f => f.name !== file.name);
|
||||
});
|
||||
|
||||
ClientIPC.on('bd-editor-addSnippet', (_, snippet) => {
|
||||
if (this.snippets.find(s => s.name === snippet.name)) return;
|
||||
this.addSnippet(snippet);
|
||||
});
|
||||
|
||||
ClientIPC.on('bd-editor-remSnippet', (_, snippet) => {
|
||||
this.snippets = this.snippets.filter(s => s.name !== snippet.name);
|
||||
});
|
||||
},
|
||||
mounted() {
|
||||
(async () => {
|
||||
|
|
Loading…
Reference in New Issue