diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 6ad294ed..29cf3fd4 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -26,6 +26,8 @@ import TurndownService from 'turndown' import { gfm } from 'turndown-plugin-gfm' +import { findStorage } from 'browser/lib/findStorage' +import { sendWakatimeHeartBeat } from 'browser/lib/wakatime-plugin' CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js' @@ -741,8 +743,14 @@ export default class CodeEditor extends React.Component { this.updateHighlight(editor, changeObject) this.value = editor.getValue() + + const { storageKey, noteKey } = this.props + const storage = findStorage(storageKey) if (this.props.onChange) { this.props.onChange(editor) + if (storage) sendWakatimeHeartBeat(storage.path, noteKey, storage.name, true, true, false) + } else { + if (storage) sendWakatimeHeartBeat(storage.path, noteKey, storage.name, false, false, false) } } @@ -846,6 +854,11 @@ export default class CodeEditor extends React.Component { } reload () { + // wakatime + const { storageKey, noteKey } = this.props + const storage = findStorage(storageKey) + if (storage) sendWakatimeHeartBeat(storage.path, noteKey, storage.name, false, false, true) + // Change event shouldn't be fired when switch note this.editor.off('change', this.changeHandler) this.value = this.props.value diff --git a/browser/lib/wakatime-plugin.js b/browser/lib/wakatime-plugin.js new file mode 100644 index 00000000..622eb96c --- /dev/null +++ b/browser/lib/wakatime-plugin.js @@ -0,0 +1,26 @@ +const exec = require('child_process').exec +const path = require('path') +let lastHeartbeat = 0 + +function sendWakatimeHeartBeat (storagePath, noteKey, storageName, isWrite, hasFileChanges, isFileChange) { + + if (new Date().getTime() - lastHeartbeat > 120000 || isFileChange) { + const notePath = path.join(storagePath, 'notes', noteKey + '.cson') + + if (!isWrite && !hasFileChanges && !isFileChange) { + return + } + + // TODO: add --key sdasdsa-sdsad-asdasd-asdsa-asdasdadas from configuration UI or use ~/.wakatime.conf + exec(`wakatime --file ${notePath} --project ${storageName} --plugin Boostnote-wakatime`, (error, stdOut, stdErr) => { + if (error) { + console.log(error) + } else { + lastHeartbeat = new Date() + console.log('wakatime', 'isWrite', isWrite, 'hasChanges', hasFileChanges) + } + }) + } +} + +export { sendWakatimeHeartBeat }