From 0b852e5551ac82618511d77b686f894db0ef24b8 Mon Sep 17 00:00:00 2001 From: walkthunder Cooper Date: Sat, 19 Aug 2023 00:09:00 +0800 Subject: [PATCH] feat(hosting): Create project in dev. --- src/components/CreateProjectModal.ts | 119 +++++++++++++++++++++++++++ src/hosting.ts | 31 +++++++ src/main.ts | 16 +++- src/settings.ts | 2 +- 4 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 src/components/CreateProjectModal.ts create mode 100644 src/hosting.ts diff --git a/src/components/CreateProjectModal.ts b/src/components/CreateProjectModal.ts new file mode 100644 index 0000000..68ac54e --- /dev/null +++ b/src/components/CreateProjectModal.ts @@ -0,0 +1,119 @@ +import { App, Modal, Setting, requestUrl } from "obsidian"; +import type InvioPlugin from "../main"; // unavoidable +import type { TransItemType } from "../i18n"; +import svg from '../utils/svg'; +import { log } from '../moreOnLog'; + +export class CreateProjectModal extends Modal { + readonly plugin: InvioPlugin; + readonly name: string; + slug: string; + domain: string; + confirmCB: any; + constructor(app: App, plugin: InvioPlugin, name: string, slug: string, domain: string, cb?: any) { + super(app); + this.plugin = plugin; + this.name = name; + this.slug = slug; + this.domain = domain; + this.confirmCB = cb; + } + async createProject() { + const token = this.plugin.settings.token; + if (!token) { + throw new Error('NoAuth'); + } + // require('request')(`http://localhost:8888/api/invio?priatoken=${token}`, {}, (error, response, body) => { + // console.log(JSON.parse(body)); + // }); + return fetch(`http://localhost:8888/api/invio?priatoken=${token}`, { + method: 'POST', + body: JSON.stringify({ + name: this.name, + slug: this.slug, + domain: this.domain || '' + }) + }) + .then(resp => resp.json()) + .then(resp => { + log.info('create project resp - ', resp); + return resp + }) + .catch(err => { + log.error('create project failed: ', JSON.stringify(err)); + }) + } + + onOpen() { + let { contentEl } = this; + const t = (x: TransItemType, vars?: any) => { + return this.plugin.i18n.t(x, vars); + }; + + contentEl.createEl("h2", { + text: 'Create a new Project' + }); + + const formContainer = contentEl.createDiv('form-container'); + // formContainer.innerHTML = 'FORM' + + new Setting(formContainer) + .setName('Name') + .setDesc('Current directory name') + .addText((text) => + text + .setPlaceholder("") + .setValue(this.name) + .setDisabled(true) + ); + new Setting(formContainer) + .setName('Slug') + .setDesc('Slug of Sub domain') + .addText((text) => + text + .setPlaceholder("") + .setValue(this.slug) + .onChange(txt => { + this.slug = txt; + log.info('slug changed: ', this.slug); + }) + ); + new Setting(formContainer) + .setName('Domain') + .setDesc('Your custom domain') + .addText((text) => + text + .setPlaceholder("www.baidu.com") + .setValue(this.domain) + .onChange(txt => { + this.domain = txt; + log.info('domain changed: ', this.domain); + }) + ); + + new Setting(formContainer) + .addButton((button) => { + button.setButtonText('cancel'); + button.onClick(async () => { + // this.plugin.settings.password = this.newPassword; + // await this.plugin.saveSettings(); + // new Notice(t("modal_password_notice")); + this.close(); + }); + }) + .addButton((button) => { + button.setClass("password-second-confirm"); + button.setButtonText('Confirm'); + button.onClick(async () => { + await this.createProject(); + this.confirmCB && this.confirmCB(); + this.close(); + }); + }); + } + + onClose() { + let { contentEl } = this; + contentEl.empty(); + } +} diff --git a/src/hosting.ts b/src/hosting.ts new file mode 100644 index 0000000..ba00960 --- /dev/null +++ b/src/hosting.ts @@ -0,0 +1,31 @@ +import { Vault, requestUrl, RequestUrlParam } from "obsidian"; +import InvioPlugin from "./main"; + +// const param: RequestUrlParam = { +// body: transformedBody, +// headers: transformedHeaders, +// method: method, +// url: url, +// contentType: contentType, +// }; + + // Check hosting service + export const checkRemoteHosting = async (plugin: InvioPlugin) => { + const curDir = plugin.settings.localWatchDir; + if (!curDir) { + return false; + } + const token = plugin.settings.token; + if (!token) { + throw new Error('NoAuth'); + } + return fetch(`http://localhost:8888/api/invio?priatoken=${token}`) + .then(resp => resp.json()) + .then(resp => { + console.log('projects: ', resp); + if (resp?.find((p: any) => p.name === curDir)) { + return true; + } + return false; + }); + } diff --git a/src/main.ts b/src/main.ts index 4c02c12..ad3ffa2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -49,6 +49,7 @@ import { DeletionOnRemote, MetadataOnRemote } from "./metadataOnRemote"; import { SyncAlgoV2Modal } from "./syncAlgoV2Notice"; import { TouchedPlanModel } from './touchedPlanModel'; import { LoadingModal } from './loadingModal'; +import { CreateProjectModal } from './components/CreateProjectModal'; import { applyLogWriterInplace, log } from "./moreOnLog"; import AggregateError from "aggregate-error"; @@ -1025,7 +1026,20 @@ export default class InvioPlugin extends Plugin { icon.removeIconInNode(document.body); const { iconSvgSyncWait } = getIconSvg(); icon.createIconNode(this, this.settings.localWatchDir, iconSvgSyncWait); - await this.saveSettings(); + + + const curDir = this.settings.localWatchDir; + + const existed = await checkRemoteHosting(this); + if (!existed) { + const cb = () => { + this.switchWorkingDir(value); + }; + const modal = new CreateProjectModal(this.app, this, curDir, curDir, null, cb.bind(this)); + modal.open(); + } else { + await this.saveSettings(); + } } async checkIfOauthExpires() {} diff --git a/src/settings.ts b/src/settings.ts index cf2e5e7..3ee89cd 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -425,7 +425,7 @@ export class InvioSettingTab extends PluginSettingTab { }) }); - // ===============Hosting Settings ====================== + // =============== Hosting Settings ====================== containerEl.createEl('h2', { text: 'Hosting Settings', cls: 'settings-pub-header' });