Skip to content

Commit

Permalink
fix issues/2
Browse files Browse the repository at this point in the history
  • Loading branch information
metal-young committed Jul 24, 2023
1 parent e54317a commit 2ea0c8a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 33 deletions.
79 changes: 53 additions & 26 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const DEFAULT_SETTINGS: Settings = {

export default class ObsidianToFlomo extends Plugin {
settings: Settings;
checkResult: boolean;

async onload() {
await this.loadSettings();
Expand All @@ -19,9 +18,13 @@ export default class ObsidianToFlomo extends Plugin {
id: 'send-to-flome-all',
name: 'Send current content to Flomo',
editorCallback: (editor: Editor, view: MarkdownView) => {
this.checkResult = this.checkSettings();
if (this.checkResult) {
new sendFlomeAPI(this.app, this).sendRequest(editor.getSelection(),'The current content has been sent to Flomo');
if (view instanceof MarkdownView && this.checkSettings()) {
const content = view.getViewData();
if (content) {
new FlomoAPI(this.app, this).sendRequest(content,'The current content has been sent to Flomo');
} else {
new Notice('No file is currently open. Please open a file and try again.');
}
}
}
});
Expand All @@ -30,20 +33,20 @@ export default class ObsidianToFlomo extends Plugin {
id: 'send-to-flome-selected',
name: 'Send selected content to Flomo',
editorCallback: (editor: Editor, view: MarkdownView) => {
this.checkResult = this.checkSettings();
if (this.checkResult) {
new sendFlomeAPI(this.app, this).sendRequest(editor.getSelection(),'The selection has been sent to Flomo');
if (view instanceof MarkdownView && this.checkSettings()) {
const selectedText = editor.getSelection();
if (selectedText) {
new FlomoAPI(this.app, this).sendRequest(selectedText,'The selection has been sent to Flomo');
} else {
new Notice('No text selected. Please select some text and try again.');
}
}
}
});

this.addSettingTab(new SampleSettingTab(this.app, this));
}

onunload() {

}

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
Expand All @@ -61,41 +64,66 @@ export default class ObsidianToFlomo extends Plugin {
}
}

class sendFlomeAPI {
class FlomoAPI {
plugin: ObsidianToFlomo;

constructor(app: App, plugin: ObsidianToFlomo) {
this.plugin = plugin;
}

async sendRequest(text: string, successMsg: string) {
const imageList = this.extractImages(text);
text = this.removeImageNotations(text);

const xhr = new XMLHttpRequest();
xhr.open("POST",this.plugin.settings.flomoAPI);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.timeout = 5000; // Set timeout to 5 seconds
xhr.send(JSON.stringify({
"content": text
"content": text,
'image_urls': imageList,
}));
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.responseText);
//能不能转化成json格式
xhr.onreadystatechange = this.handleResponse.bind(this, successMsg, xhr);
xhr.onerror = () => {
new Notice('Network error, please check your connection');
};
xhr.ontimeout = () => {
new Notice('Request timed out, please try again later');
};
}

extractImages(text) {
const regex = /!\[\[(.*?)\]\]/g;
const matches = text.matchAll(regex);
const imageList = [];
for (const match of matches) {
const image = match[1];
imageList.push(image);
}
return imageList;
}

removeImageNotations(text) {
return text.replace(/!\[\[(.*?)\]\]/g, '');
}

handleResponse(successMsg, xhr) {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
try {
const json = JSON.parse(xhr.responseText);
console.log(json);
//json里的code如果是0就返回true,如果不是0就提示失败,如果是-1就返回json里的message
if (json.code == 0) {
new Notice(successMsg);
}
else if (json.code == -1) {
new Notice(json.message + 'please check your settings');
}
else {
new Notice('please check your settings');
new Notice(json.message + 'please check your settings');
}
}
catch (e) {
new Notice('please check your settings');
}
} else {
new Notice('Request failed with status code ' + xhr.status);
}
}
}
Expand Down Expand Up @@ -128,8 +156,7 @@ class SampleSettingTab extends PluginSettingTab {
}));

containerEl.createEl('button', {text: 'Send a test request'}).addEventListener('click', () => {
new sendFlomeAPI(this.app, this.plugin).sendRequest('This is a test request', 'The test request has been sent to Flomo');
}
);
new FlomoAPI(this.app, this.plugin).sendRequest('This is a test request', 'The test request has been sent to Flomo');
});
}
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-to-flomo",
"name": "Obsidian to Flomo",
"version": "0.1.1",
"version": "0.1.2",
"minAppVersion": "0.15.0",
"description": "Quickly share content to Flomo.",
"author": "Xiaoyu Li",
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-to-flomo",
"version": "0.1.0",
"version": "0.1.2",
"description": "Quickly share content to Flomo.",
"main": "main.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"0.1.0": "0.15.0"
"0.1.2": "0.15.0"
}

0 comments on commit 2ea0c8a

Please sign in to comment.