Skip to content

Commit

Permalink
feat(hosting): Create project in dev.
Browse files Browse the repository at this point in the history
  • Loading branch information
walkthunder committed Aug 18, 2023
1 parent 2d48adc commit 0b852e5
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 2 deletions.
119 changes: 119 additions & 0 deletions src/components/CreateProjectModal.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
31 changes: 31 additions & 0 deletions src/hosting.ts
Original file line number Diff line number Diff line change
@@ -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;
});
}
16 changes: 15 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);

Check failure on line 1033 in src/main.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Cannot find name 'checkRemoteHosting'.
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() {}
Expand Down
2 changes: 1 addition & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ export class InvioSettingTab extends PluginSettingTab {
})
});

// ===============Hosting Settings ======================
// =============== Hosting Settings ======================

containerEl.createEl('h2', { text: 'Hosting Settings', cls: 'settings-pub-header' });

Expand Down

0 comments on commit 0b852e5

Please sign in to comment.