mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-06-01 11:29:41 +08:00
🔨 Refactor(ts): change js -> ts
This commit is contained in:
63
src/universal/datastore/index.ts
Normal file
63
src/universal/datastore/index.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import Datastore from 'lowdb'
|
||||
// @ts-ignore
|
||||
import LodashId from 'lodash-id'
|
||||
import FileSync from 'lowdb/adapters/FileSync'
|
||||
import path from 'path'
|
||||
import fs from 'fs-extra'
|
||||
import { remote, app } from 'electron'
|
||||
|
||||
const APP = process.type === 'renderer' ? remote.app : app
|
||||
const STORE_PATH = APP.getPath('userData')
|
||||
|
||||
if (process.type !== 'renderer') {
|
||||
if (!fs.pathExistsSync(STORE_PATH)) {
|
||||
fs.mkdirpSync(STORE_PATH)
|
||||
}
|
||||
}
|
||||
|
||||
class DB {
|
||||
private db: Datastore.LowdbSync<Datastore.AdapterSync>
|
||||
constructor () {
|
||||
const adapter = new FileSync(path.join(STORE_PATH, '/data.json'))
|
||||
|
||||
this.db = Datastore(adapter)
|
||||
this.db._.mixin(LodashId)
|
||||
|
||||
if (!this.db.has('uploaded').value()) {
|
||||
this.db.set('uploaded', []).write()
|
||||
}
|
||||
|
||||
if (!this.db.has('picBed').value()) {
|
||||
this.db.set('picBed', {
|
||||
current: 'weibo'
|
||||
}).write()
|
||||
}
|
||||
|
||||
if (!this.db.has('settings.shortKey').value()) {
|
||||
this.db.set('settings.shortKey[picgo:upload]', {
|
||||
enable: true,
|
||||
key: 'CommandOrControl+Shift+P',
|
||||
name: 'picgo:upload',
|
||||
label: '快捷上传'
|
||||
}).write()
|
||||
}
|
||||
}
|
||||
read () {
|
||||
return this.db.read()
|
||||
}
|
||||
get (key = '') {
|
||||
return this.read().get(key).value()
|
||||
}
|
||||
set (key: string, value: any) {
|
||||
return this.read().set(key, value).write()
|
||||
}
|
||||
has (key: string) {
|
||||
return this.read().has(key).value()
|
||||
}
|
||||
insert (key: string, value: any): void {
|
||||
// @ts-ignore
|
||||
return this.read().get(key).insert(value).write()
|
||||
}
|
||||
}
|
||||
|
||||
export default new DB()
|
||||
12
src/universal/types/extra-vue.d.ts
vendored
Normal file
12
src/universal/types/extra-vue.d.ts
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
import VueRouter, { Route } from 'vue-router'
|
||||
import db from '#/datastore'
|
||||
import axios from 'axios'
|
||||
declare module 'vue/types/vue' {
|
||||
interface Vue {
|
||||
$router: VueRouter,
|
||||
$route: Route,
|
||||
$db: typeof db
|
||||
$http: typeof axios
|
||||
$builtInPicBed: string[]
|
||||
}
|
||||
}
|
||||
2
src/universal/types/picgo.d.ts
vendored
Normal file
2
src/universal/types/picgo.d.ts
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
import PicGoCore from 'picgo'
|
||||
export default PicGoCore
|
||||
13
src/universal/types/shims-tsx.d.ts
vendored
Normal file
13
src/universal/types/shims-tsx.d.ts
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
import Vue, { VNode } from 'vue'
|
||||
|
||||
declare global {
|
||||
namespace JSX {
|
||||
// tslint:disable no-empty-interface
|
||||
interface Element extends VNode {}
|
||||
// tslint:disable no-empty-interface
|
||||
interface ElementClass extends Vue {}
|
||||
interface IntrinsicElements {
|
||||
[elem: string]: any
|
||||
}
|
||||
}
|
||||
}
|
||||
4
src/universal/types/shims-vue.d.ts
vendored
Normal file
4
src/universal/types/shims-vue.d.ts
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
declare module '*.vue' {
|
||||
import Vue from 'vue'
|
||||
export default Vue
|
||||
}
|
||||
161
src/universal/types/types.d.ts
vendored
Normal file
161
src/universal/types/types.d.ts
vendored
Normal file
@@ -0,0 +1,161 @@
|
||||
// global
|
||||
interface Obj {
|
||||
[propName: string]: any
|
||||
}
|
||||
|
||||
interface ObjT<T> {
|
||||
[propName: string]: T
|
||||
}
|
||||
|
||||
// Image && PicBed
|
||||
interface ImgInfo {
|
||||
buffer?: Buffer
|
||||
base64Image?: string
|
||||
fileName?: string
|
||||
width?: number
|
||||
height?: number
|
||||
extname?: string
|
||||
imgUrl?: string
|
||||
[propName: string]: any
|
||||
}
|
||||
|
||||
interface PicBedType {
|
||||
type: string
|
||||
name: string
|
||||
visible: boolean
|
||||
}
|
||||
|
||||
// Config Settings
|
||||
interface ShortKeyConfig {
|
||||
enable: boolean
|
||||
key: string // 按键
|
||||
name: string
|
||||
label: string
|
||||
}
|
||||
|
||||
interface ShortKeyConfigs {
|
||||
[propName: string]: ShortKeyConfig
|
||||
}
|
||||
|
||||
interface OldShortKeyConfigs {
|
||||
upload: string
|
||||
}
|
||||
|
||||
// Main process
|
||||
interface BrowserWindowOptions {
|
||||
height: number,
|
||||
width: number,
|
||||
show: boolean,
|
||||
fullscreenable: boolean,
|
||||
resizable: boolean,
|
||||
webPreferences: {
|
||||
nodeIntegration: boolean,
|
||||
nodeIntegrationInWorker: boolean,
|
||||
backgroundThrottling: boolean
|
||||
webSecurity?: boolean
|
||||
},
|
||||
vibrancy?: string | any,
|
||||
frame?: boolean
|
||||
center?: boolean
|
||||
title?: string
|
||||
titleBarStyle?: string | any
|
||||
backgroundColor?: string
|
||||
autoHideMenuBar?: boolean
|
||||
transparent?: boolean
|
||||
icon?: string
|
||||
skipTaskbar?: boolean
|
||||
alwaysOnTop?: boolean
|
||||
}
|
||||
|
||||
interface FileWithPath {
|
||||
path: string
|
||||
name?: string
|
||||
}
|
||||
|
||||
interface Bounds {
|
||||
x: number
|
||||
y: number
|
||||
}
|
||||
|
||||
declare enum PasteStyle {
|
||||
MARKDOWN = 'markdown',
|
||||
HTML = 'HTML',
|
||||
URL = 'URL',
|
||||
UBB = 'UBB',
|
||||
CUSTOM = 'Custom'
|
||||
}
|
||||
|
||||
// global value
|
||||
declare var __static: string
|
||||
|
||||
// third-party
|
||||
declare module 'fix-path' {
|
||||
function fixPath(): void
|
||||
export default fixPath
|
||||
}
|
||||
|
||||
// PicGo Types
|
||||
declare enum PicGoHelperType {
|
||||
afterUploadPlugins = 'afterUploadPlugins',
|
||||
beforeTransformPlugins = 'beforeTransformPlugins',
|
||||
beforeUploadPlugins = 'beforeUploadPlugins',
|
||||
uploader = 'uploader',
|
||||
transformer = 'transformer'
|
||||
}
|
||||
|
||||
interface IPicGoPlugin {
|
||||
name: string
|
||||
author: string
|
||||
description: string
|
||||
logo: string
|
||||
version: string | number
|
||||
gui: boolean
|
||||
config: {
|
||||
plugin: IPluginMenuConfig
|
||||
uploader: IPluginMenuConfig
|
||||
transformer: IPluginMenuConfig
|
||||
[index: string]: IPluginMenuConfig
|
||||
} | {
|
||||
[propName: string]: any
|
||||
}
|
||||
enabled?: boolean
|
||||
homepage: string
|
||||
guiMenu?: any[]
|
||||
ing: boolean
|
||||
hasInstall?: boolean
|
||||
}
|
||||
|
||||
interface IPluginMenuConfig {
|
||||
name: string
|
||||
config: any[]
|
||||
}
|
||||
|
||||
interface INPMSearchResult {
|
||||
data: {
|
||||
objects: INPMSearchResultObject[]
|
||||
}
|
||||
}
|
||||
|
||||
interface INPMSearchResultObject {
|
||||
package: {
|
||||
name: string
|
||||
scope: string
|
||||
version: string
|
||||
description: string
|
||||
keywords: string[]
|
||||
author: {
|
||||
name: string
|
||||
}
|
||||
links: {
|
||||
npm: string
|
||||
homepage: string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GuiApi
|
||||
|
||||
interface IShowInputBoxOption {
|
||||
title: string
|
||||
placeholder: string
|
||||
}
|
||||
14
src/universal/types/view.d.ts
vendored
Normal file
14
src/universal/types/view.d.ts
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
interface ISettingForm {
|
||||
updateHelper: boolean
|
||||
showPicBedList: string[]
|
||||
autoStart: boolean
|
||||
rename: boolean
|
||||
autoRename: boolean
|
||||
uploadNotification: boolean
|
||||
miniWindowOntop: boolean
|
||||
logLevel: string[]
|
||||
}
|
||||
|
||||
interface ShortKeyMap {
|
||||
[propName: string]: string
|
||||
}
|
||||
31
src/universal/utils/pasteTemplate.ts
Normal file
31
src/universal/utils/pasteTemplate.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import db from '#/datastore'
|
||||
|
||||
const formatCustomLink = (customLink: string, item: ImgInfo) => {
|
||||
let fileName = item.fileName!.replace(new RegExp(`\\${item.extname}$`), '')
|
||||
let url = item.url || item.imgUrl
|
||||
const formatObj = {
|
||||
url,
|
||||
fileName
|
||||
}
|
||||
const keys = Object.keys(formatObj) as ['url', 'fileName']
|
||||
keys.forEach(item => {
|
||||
if (customLink.indexOf(`$${item}`) !== -1) {
|
||||
let reg = new RegExp(`\\$${item}`, 'g')
|
||||
customLink = customLink.replace(reg, formatObj[item])
|
||||
}
|
||||
})
|
||||
return customLink
|
||||
}
|
||||
|
||||
export default (style: PasteStyle, item: ImgInfo) => {
|
||||
let url = item.url || item.imgUrl
|
||||
const customLink = db.get('settings.customLink') || '$url'
|
||||
const tpl = {
|
||||
'markdown': ``,
|
||||
'HTML': `<img src="${url}"/>`,
|
||||
'URL': url,
|
||||
'UBB': `[IMG]${url}[/IMG]`,
|
||||
'Custom': formatCustomLink(customLink, item)
|
||||
}
|
||||
return tpl[style]
|
||||
}
|
||||
Reference in New Issue
Block a user