168 lines
6.3 KiB
JavaScript
168 lines
6.3 KiB
JavaScript
/*
|
|
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
|
|
if you want to view the source, please visit the github repository of this plugin
|
|
*/
|
|
|
|
var __create = Object.create;
|
|
var __defProp = Object.defineProperty;
|
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
var __getProtoOf = Object.getPrototypeOf;
|
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
var __export = (target, all) => {
|
|
for (var name in all)
|
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
};
|
|
var __copyProps = (to, from, except, desc) => {
|
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
for (let key of __getOwnPropNames(from))
|
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
}
|
|
return to;
|
|
};
|
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
|
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
|
|
// main.ts
|
|
var main_exports = {};
|
|
__export(main_exports, {
|
|
default: () => ActivityWatchPlugin
|
|
});
|
|
module.exports = __toCommonJS(main_exports);
|
|
var import_obsidian = require("obsidian");
|
|
var os = __toESM(require("os"));
|
|
var AWrequest = class {
|
|
constructor(url, body) {
|
|
this.contentType = "application/json";
|
|
this.headers = { "Content-type": "application/json", "charset": "utf-8" };
|
|
this.method = "post";
|
|
this.throw = true;
|
|
this.url = url;
|
|
this.body = body;
|
|
}
|
|
};
|
|
var DEFAULT_SETTINGS = {
|
|
devServer: false
|
|
};
|
|
var ActivityWatchPlugin = class extends import_obsidian.Plugin {
|
|
constructor() {
|
|
super(...arguments);
|
|
this.hostname = os.hostname();
|
|
this.watcher_name = "aw-watcher-obsidian";
|
|
this.sleeptime = 5;
|
|
}
|
|
async init() {
|
|
this.statusBarItemEl.setText("ActivityWatch initializing...");
|
|
const port = this.settings.devServer ? 5666 : 5600;
|
|
this.bucket_id = `${this.watcher_name}_${this.hostname}`;
|
|
this.endpoint_url = `http://127.0.0.1:${port}/api/0/`;
|
|
if (this.settings.devServer) {
|
|
console.log(`sleeptime is ${this.sleeptime}` + (this.sleeptime <= 0 ? ", skipping any timed heartbeats" : ""));
|
|
console.log(`watcher_name is ${this.watcher_name}`);
|
|
console.log(`port is ${port}`);
|
|
console.log(`bucket_id is ${this.bucket_id}`);
|
|
console.log(`endpoint_url is ${this.endpoint_url}`);
|
|
}
|
|
await this.createBucket(this.bucket_id, "app.editor.activity");
|
|
this.statusBarItemEl.setText("ActivityWatch active");
|
|
}
|
|
async post(endpoint, data) {
|
|
const r = new AWrequest(this.endpoint_url + endpoint, JSON.stringify(data));
|
|
try {
|
|
await (0, import_obsidian.request)(r);
|
|
} catch (e) {
|
|
console.log(`Request to URL [${r.url}] using [${r.method}], Header [${r.headers}], Body [${r.body}] failed!`);
|
|
throw e;
|
|
}
|
|
}
|
|
async createBucket(id, event_type) {
|
|
const data = {
|
|
"client": this.watcher_name,
|
|
"hostname": this.hostname,
|
|
"type": event_type
|
|
};
|
|
await this.post(`buckets/${id}`, data);
|
|
}
|
|
async sendData(id, heartbeat_data, pulsetime) {
|
|
const endpoint = `buckets/${id}/heartbeat?pulsetime=${pulsetime}`;
|
|
await this.post(endpoint, { "timestamp": new Date().toISOString(), "duration": 0, "data": heartbeat_data });
|
|
}
|
|
async sendAbstractFileEvent(file, extraData, pulseTime) {
|
|
if (file) {
|
|
await this.sendData(this.bucket_id, {
|
|
"file": "/" + file.path,
|
|
"project": file.vault.getName(),
|
|
"language": "Markdown",
|
|
"projectPath": file.vault.adapter instanceof import_obsidian.FileSystemAdapter ? file.vault.adapter.getBasePath() : "unknown vault path",
|
|
"editor": "Obsidian",
|
|
"editorVersion": import_obsidian.apiVersion,
|
|
...extraData ? extraData : {}
|
|
}, pulseTime);
|
|
}
|
|
}
|
|
async sendFileHeartbeatEvent(file) {
|
|
await this.sendAbstractFileEvent(file, {
|
|
"eventType": "obsidian.activeFileHeartbeatEvent"
|
|
}, this.sleeptime + 1);
|
|
}
|
|
async sendFileRenameEvent(file, oldPath) {
|
|
await this.sendAbstractFileEvent(file, {
|
|
"eventType": "obsidian.renameFileEvent",
|
|
"oldPath": oldPath
|
|
}, 0);
|
|
}
|
|
async sendFileDeleteEvent(oldPath) {
|
|
await this.sendAbstractFileEvent(oldPath, {
|
|
"eventType": "obsidian.deleteFileEvent"
|
|
}, 0);
|
|
}
|
|
async sendFileCreateEvent(path) {
|
|
await this.sendAbstractFileEvent(path, {
|
|
"eventType": "obsidian.createFileEvent"
|
|
}, 0);
|
|
}
|
|
async onload() {
|
|
this.statusBarItemEl = this.addStatusBarItem();
|
|
await this.loadSettings();
|
|
await this.init();
|
|
this.registerEvent(this.app.vault.on("rename", (file, oldPath) => this.sendFileRenameEvent(file, oldPath)));
|
|
this.registerEvent(this.app.vault.on("delete", this.sendFileDeleteEvent));
|
|
this.app.workspace.onLayoutReady(() => {
|
|
this.registerEvent(this.app.vault.on("create", (f) => this.sendFileCreateEvent(f)));
|
|
});
|
|
this.addSettingTab(new ObsidianWatcherSettingTab(this.app, this));
|
|
if (this.sleeptime > 0) {
|
|
this.registerInterval(window.setInterval(() => {
|
|
this.sendFileHeartbeatEvent(this.app.workspace.getActiveFile());
|
|
}, this.sleeptime * 1e3));
|
|
}
|
|
}
|
|
onunload() {
|
|
this.statusBarItemEl.remove();
|
|
}
|
|
async loadSettings() {
|
|
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
|
|
}
|
|
async saveSettings() {
|
|
await this.saveData(this.settings);
|
|
}
|
|
};
|
|
var ObsidianWatcherSettingTab = class extends import_obsidian.PluginSettingTab {
|
|
constructor(app, plugin) {
|
|
super(app, plugin);
|
|
this.plugin = plugin;
|
|
}
|
|
display() {
|
|
const { containerEl } = this;
|
|
containerEl.empty();
|
|
containerEl.createEl("h2", { text: "Settings for ActivityWatch plugin" });
|
|
new import_obsidian.Setting(containerEl).setName("ActivityWatch development server").setDesc("If enabled, uses development server for ActivityWatch instead of production. Default off.").addToggle((t) => t.setValue(this.plugin.settings.devServer).onChange(async (value) => {
|
|
console.log(`switching plugin to use ${value ? "development" : "production"} backend`);
|
|
this.plugin.settings.devServer = value;
|
|
await this.plugin.saveSettings();
|
|
await this.plugin.init();
|
|
}));
|
|
}
|
|
};
|