This commit is contained in:
kkzzhizhou 2021-11-28 23:55:21 +08:00
parent ac57825452
commit 933c74804c
6 changed files with 71 additions and 68 deletions

View file

@ -2,26 +2,24 @@ import i18n from "i18next";
import enUsTrans from "./i18n/en-us.json"; import enUsTrans from "./i18n/en-us.json";
import zhCnTrans from "./i18n/zh-cn.json"; import zhCnTrans from "./i18n/zh-cn.json";
import { initReactI18next } from 'react-i18next'; import { initReactI18next } from 'react-i18next';
const lang = window.localStorage.getItem('language');
// console.log(lang)
i18n.use(initReactI18next) const lang = window.localStorage.getItem('language');
.init({
//资源文件 i18n.use(initReactI18next)
resources: { .init({
en: { resources: {
translation: enUsTrans, en: {
translation: enUsTrans,
},
zh: {
translation: zhCnTrans,
},
}, },
zh: { fallbackLng: lang,
translation: zhCnTrans, debug: false,
interpolation: {
escapeValue: false,
}, },
}, })
fallbackLng: lang,
debug: false,
interpolation: {
escapeValue: false,
},
})
export default i18n; export default i18n;

View file

@ -11,5 +11,11 @@
"submit": "添加", "submit": "添加",
"No Dailies Present": "目前每日任务为空", "No Dailies Present": "目前每日任务为空",
"No habits present.": "目前习惯为空", "No habits present.": "目前习惯为空",
"No Rewards present.": "目前奖励为空" "No Rewards present.": "目前奖励为空",
"There was an error running the cron. Please try again later.": "运行定时失败出错,请稍后再试。",
"Login Failed, Please check credentials and try again!": "登录失败, 请检查凭据后再次尝试",
"API Error: Please check credentials": "API错误请检查凭据",
"Resyncing, please try again": "重新同步失败,请稍后再试",
"Add!": "已添加",
"Loading....": "加载中...."
} }

View file

@ -1,6 +1,6 @@
import { Plugin } from "obsidian"; import { Plugin } from "obsidian";
import { HabiticaSyncSettingsTab } from "./settings"; import { HabiticaSyncSettingsTab } from "./settings";
import { HabiticaSyncView, VIEW_TYPE} from "./view" import { HabiticaSyncView, VIEW_TYPE } from "./view"
interface HabiticaSyncSettings { interface HabiticaSyncSettings {
userID: string userID: string
@ -15,26 +15,25 @@ export default class HabiticaSync extends Plugin {
view: HabiticaSyncView; view: HabiticaSyncView;
async onload() { async onload() {
// console.log("load plugin: habitica-sync")
await this.loadSettings(); await this.loadSettings();
this.addSettingTab(new HabiticaSyncSettingsTab(this.app, this)); this.addSettingTab(new HabiticaSyncSettingsTab(this.app, this));
this.registerView( this.registerView(
VIEW_TYPE, VIEW_TYPE,
(leaf) => (new HabiticaSyncView(leaf, this)) (leaf) => (new HabiticaSyncView(leaf, this))
); );
this.addRibbonIcon("popup-open", "Open Habitica Pane", () => { //activate view this.addRibbonIcon("popup-open", "Open Habitica Pane", () => { //activate view
this.activateView(); this.activateView();
}); });
this.addCommand({ this.addCommand({
id: "habitica-view-open", id: "habitica-view-open",
name: "Open Pane", name: "Open Pane",
hotkeys: [{ modifiers: ["Mod", "Shift"], key: "h"}], hotkeys: [{ modifiers: ["Mod", "Shift"], key: "h" }],
callback: () => { callback: () => {
this.activateView(); this.activateView();
} }
}); });
this.activateView(); this.activateView();
} }
async loadSettings() { async loadSettings() {
this.settings = Object.assign(DEFAULT_SETTINGS, await this.loadData()) this.settings = Object.assign(DEFAULT_SETTINGS, await this.loadData())
@ -44,23 +43,24 @@ export default class HabiticaSync extends Plugin {
} }
async onunload() { async onunload() {
// await this.view.onClose(); if (this.view) {
await this.view.onClose();
}
this.app.workspace this.app.workspace
.getLeavesOfType(VIEW_TYPE) .getLeavesOfType(VIEW_TYPE)
.forEach((leaf) => leaf.detach()); .forEach((leaf) => leaf.detach());
} }
async activateView() { async activateView() {
this.app.workspace.detachLeavesOfType(VIEW_TYPE); this.app.workspace.detachLeavesOfType(VIEW_TYPE);
await this.app.workspace.getRightLeaf(false).setViewState({ await this.app.workspace.getRightLeaf(false).setViewState({
type: VIEW_TYPE, type: VIEW_TYPE,
active: true, active: true,
}); });
this.app.workspace.revealLeaf( this.app.workspace.revealLeaf(
this.app.workspace.getLeavesOfType(VIEW_TYPE)[0] this.app.workspace.getLeavesOfType(VIEW_TYPE)[0]
); );
} }
} }

View file

@ -14,29 +14,29 @@ export class HabiticaSyncSettingsTab extends PluginSettingTab {
containerEl.empty(); containerEl.empty();
new Setting(containerEl) new Setting(containerEl)
.setName("Habitica User ID") .setName("Habitica User ID")
.setDesc("Can be found in Settings > API") .setDesc("Can be found in Settings > API")
.addText((text) => .addText((text) =>
text text
.setPlaceholder("User ID") .setPlaceholder("User ID")
.setValue(this.plugin.settings.userID) .setValue(this.plugin.settings.userID)
.onChange(async (value) => { .onChange(async (value) => {
this.plugin.settings.userID = value; this.plugin.settings.userID = value;
await this.plugin.saveSettings(); await this.plugin.saveSettings();
}) })
); );
new Setting(containerEl) new Setting(containerEl)
.setName("Habitica API Token") .setName("Habitica API Token")
.setDesc("Can be found in Settings > API") .setDesc("Can be found in Settings > API")
.addText((text) => .addText((text) =>
text text
.setPlaceholder("API Token") .setPlaceholder("API Token")
.setValue(this.plugin.settings.apiToken) .setValue(this.plugin.settings.apiToken)
.onChange(async (value) => { .onChange(async (value) => {
this.plugin.settings.apiToken = value; this.plugin.settings.apiToken = value;
await this.plugin.saveSettings(); await this.plugin.saveSettings();
}) })
); );
} }
} }

View file

@ -1,4 +1,4 @@
import { ItemView,WorkspaceLeaf } from "obsidian"; import { ItemView, WorkspaceLeaf } from "obsidian";
import * as React from "react"; import * as React from "react";
import * as ReactDOM from "react-dom"; import * as ReactDOM from "react-dom";
import App from "./view/App" import App from "./view/App"
@ -27,11 +27,11 @@ export class HabiticaSyncView extends ItemView {
async onOpen() { async onOpen() {
ReactDOM.render( ReactDOM.render(
<App plugin={this.plugin}/>, <App plugin={this.plugin} />,
this.containerEl.children[1] this.containerEl.children[1]
) )
} }
async onClose(){ async onClose() {
ReactDOM.unmountComponentAtNode(this.containerEl.children[1]); ReactDOM.unmountComponentAtNode(this.containerEl.children[1]);
} }
} }

View file

@ -4,7 +4,6 @@ import { getStats, scoreTask, makeCronReq, costReward, addTask, deleteTask, upda
import Statsview from "./Components/Statsview" import Statsview from "./Components/Statsview"
import Taskview from "./Components/Taskview" import Taskview from "./Components/Taskview"
import "../i18n" import "../i18n"
import { exit } from "process";
class App extends React.Component<any, any> { class App extends React.Component<any, any> {
private _username = ""; private _username = "";
@ -167,7 +166,7 @@ class App extends React.Component<any, any> {
} }
} }
async sendUpdateTask(id: string, type: string,message: string, title: string, notes: string) { async sendUpdateTask(id: string, type: string, message: string, title: string, notes: string) {
try { try {
let response = await updateTask(this.username, this.credentials, id, type, title, notes); let response = await updateTask(this.username, this.credentials, id, type, title, notes);
let result = await response.json(); let result = await response.json();
@ -218,13 +217,13 @@ class App extends React.Component<any, any> {
this.state.tasks.dailys.forEach((element: any) => { this.state.tasks.dailys.forEach((element: any) => {
if (element.id == event.target.id) { if (element.id == event.target.id) {
if (element.id == event.target.id) { if (element.id == event.target.id) {
if ( event.target.attributes.title.value == 'submit' ) { if (event.target.attributes.title.value == 'submit') {
const task_title = event.target.attributes['data-title'].value ? event.target.attributes['data-title'].value:element.text const task_title = event.target.attributes['data-title'].value ? event.target.attributes['data-title'].value : element.text
const task_notes = event.target.attributes['data-notes'].value ? event.target.attributes['data-notes'].value:element.notes const task_notes = event.target.attributes['data-notes'].value ? event.target.attributes['data-notes'].value : element.notes
this.sendUpdateTask(event.target.id,'daily',"Update!",task_title,task_notes) this.sendUpdateTask(event.target.id, 'daily', "Update!", task_title, task_notes)
} else if (event.target.attributes.title.value == 'delete') { } else if (event.target.attributes.title.value == 'delete') {
this.sendDeleteTask(event.target.id, "Deleted!") this.sendDeleteTask(event.target.id, "Deleted!")
} else if ( !element.completed) { } else if (!element.completed) {
this.sendScore(event.target.id, "up", "Checked!") this.sendScore(event.target.id, "up", "Checked!")
} else { } else {
this.sendScore(event.target.id, "down", "Un-Checked!") this.sendScore(event.target.id, "down", "Un-Checked!")
@ -274,7 +273,7 @@ class App extends React.Component<any, any> {
if (event.target.innerText == 'clear') { if (event.target.innerText == 'clear') {
this.sendDeleteTask(event.target.id, "Deleted!") this.sendDeleteTask(event.target.id, "Deleted!")
} else if (event.target.innerText == 'create') { } else if (event.target.innerText == 'create') {
this.sendUpdateTask(event.target.id,'reward',"Edit!","1","1") this.sendUpdateTask(event.target.id, 'reward', "Edit!", "1", "1")
} else { } else {
this.sendReward(target_id, "down", "Cost!") this.sendReward(target_id, "down", "Cost!")
} }