-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from anxinJ/main
新增简单图床EasyImage支持
- Loading branch information
Showing
6 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Setting } from 'obsidian' | ||
import type Emo from '../main' | ||
import { EmoFragment } from '../base/emo-fragment' | ||
import { HostingProvider } from '../config' | ||
import { t } from '../lang/helpers' | ||
|
||
export class EasyImageFragment extends EmoFragment { | ||
constructor (el: HTMLElement, plugin: Emo) { | ||
super(HostingProvider.EasgyImage, el, plugin) | ||
} | ||
|
||
display (el: HTMLElement, plugin: Emo): void { | ||
const parms = plugin.config.easyimage_parms | ||
el.createEl('h3', { text: t('EasyImage Settings') }) | ||
|
||
new Setting(el) | ||
.setName(t('domain')) | ||
.addText((text) => { | ||
text | ||
.setValue(parms.required.domain) | ||
.onChange(async (value) => { | ||
parms.required.domain = value | ||
await plugin.saveSettings() | ||
}) | ||
}) | ||
|
||
new Setting(el) | ||
.setName('token') | ||
.addText((text) => { | ||
text | ||
.setValue(parms.required.token) | ||
.onChange(async (value) => { | ||
parms.required.token = value | ||
await plugin.saveSettings() | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { EmoParms } from '../base/emo-parms' | ||
|
||
export interface EasyImageParms extends EmoParms { | ||
required: Required | ||
} | ||
|
||
interface Required { | ||
domain: string | ||
token: string | ||
} | ||
|
||
export const EASYIMAGE_DEFAULT_PARMS: EasyImageParms = { | ||
required: { | ||
domain: '', | ||
token: '' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { RequestUrlParam } from 'obsidian' | ||
import { request } from 'obsidian' | ||
import type { EasyImageParms } from '../parms/parms-easyimage' | ||
import { EmoFormData } from '../utils/emo-formdata' | ||
import { EmoUploader } from '../base/emo-uploader' | ||
import { CONTENT_TYPE_FORMDATA } from '../base/constants' | ||
|
||
export class EasyImageUploader extends EmoUploader { | ||
parms!: EasyImageParms | ||
constructor (easyimageParms: EasyImageParms) { | ||
super() | ||
this.parms = easyimageParms | ||
} | ||
|
||
async upload (file: File): Promise<string> { | ||
const domain = this.parms.required.domain | ||
const formData = new EmoFormData() | ||
await formData.add('image', file) | ||
await formData.add('token', this.parms.required.token) | ||
const req: RequestUrlParam = { | ||
url: domain, | ||
method: 'POST', | ||
headers: { | ||
// 'X-API-Key': this.parms.required.token, | ||
'Content-Type': CONTENT_TYPE_FORMDATA | ||
}, | ||
body: formData.getBody() | ||
} | ||
return await new Promise((resolve, reject) => { | ||
request(req).then((res) => { | ||
const json = JSON.parse(res) | ||
let url = '' | ||
url = json.url | ||
const markdownText = `` | ||
resolve(markdownText) | ||
}).catch(err => { | ||
reject(err) | ||
}) | ||
}) | ||
} | ||
} |