Skip to content

Commit

Permalink
♻️ 重构导入导出
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Jan 5, 2022
1 parent a4263fb commit 6cd369e
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 114 deletions.
12 changes: 6 additions & 6 deletions src/apps/script/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { ResourceManager } from '../resource';
import { compileScriptCode } from '@App/pkg/sandbox/compile';
import { SubscribeModel } from '@App/model/subscribe';
import { Subscribe, SUBSCRIBE_STATUS_DISABLE, SUBSCRIBE_STATUS_ENABLE } from '@App/model/do/subscribe';
import { File } from '@App/model/do/backup';
import { ExportFile } from '@App/model/do/backup';

// 脚本控制器,发送或者接收来自管理器的消息,并不对脚本数据做实际的处理
export class ScriptController {
Expand Down Expand Up @@ -180,10 +180,10 @@ export class ScriptController {
});
}

public getImportFile(uuid: string): Promise<File> {
public getImportFile(uuid: string): Promise<ExportFile> {
return new Promise(resolve => {
MsgCenter.sendMessage(RequestImportFile, uuid, resp => {
resolve(<File>resp);
resolve(<ExportFile>resp);
});
});
}
Expand Down Expand Up @@ -565,8 +565,8 @@ export class ScriptController {
})
}

public parseBackFile(str: string): { data?: File, err?: string } {
const data = <File>JSON.parse(str);
public parseBackFile(str: string): { data?: ExportFile, err?: string } {
const data = <ExportFile>JSON.parse(str);
if (!data.created_by) {
return { err: '错误的格式' };
}
Expand All @@ -576,7 +576,7 @@ export class ScriptController {
return { data: data };
}

public openImportFileWindow(file: File): Promise<any> {
public openImportFileWindow(file: ExportFile): Promise<any> {
return new Promise(resolve => {
MsgCenter.sendMessage(OpenImportFileWindow, file, (resp) => {
resolve(resp);
Expand Down
8 changes: 4 additions & 4 deletions src/apps/script/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Subscribe } from '@App/model/do/subscribe';
import { SubscribeModel } from '@App/model/subscribe';
import { SyncModel } from '@App/model/sync';
import { SyncAction, SyncData } from '@App/model/do/sync';
import { File } from '@App/model/do/backup';
import { ExportFile } from '@App/model/do/backup';
import { v4 as uuidv4 } from 'uuid';
import { Manager } from '@App/pkg/apps/manager';

Expand Down Expand Up @@ -196,7 +196,7 @@ export class ScriptManager extends Manager {
});
}

public openImportFileWindow(file: File): Promise<any> {
public openImportFileWindow(file: ExportFile): Promise<any> {
return new Promise(resolve => {
// 打开导入窗口
const uuid = uuidv4()
Expand All @@ -209,15 +209,15 @@ export class ScriptManager extends Manager {
});
}

public importFile(file: File): Promise<any> {
public importFile(file: ExportFile): Promise<any> {
return new Promise(resolve => {


resolve(true);
});
}

public requestImportFile(uuid: string): Promise<File> {
public requestImportFile(uuid: string): Promise<ExportFile> {
return new Promise(resolve => {
const file = App.Cache.get('import:info:' + uuid);
resolve(file);
Expand Down
74 changes: 29 additions & 45 deletions src/model/do/backup.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,51 @@
// 使用tampermonkey的备份模式

import { ResourceHash } from './resource';
import { Metadata, Script as InstallScript } from './script';
import { SubscribeScript, Subscribe as InstallSubscribe } from './subscribe';

export interface File {
created_by: string
version: string
scripts: Script[];
subscribes: Subscribe[];
settings: Settings
}

export interface Settings {

}

export interface Script {
export interface ExportScript {
name: string
options: Options
script: InstallScript
storage: Storage
enabled: boolean
position: number
uuid: string
file_url?: string
source: string
requires: Resource[]
requires_css: Resource[]
resources: Resource[]
self_metadata: Metadata
subscribe_url?: string
modified: number
// 导入用,需要解析source获得
metadata?: Metadata
script?: InstallScript
old?: InstallScript
error?: string
background?: boolean
requires: ExportResource[]
requires_css: ExportResource[]
resources: ExportResource[]
}

export interface Subscribe {
name: string
url: string
export interface ImportScript {
source: string
enabled: boolean
position: number
storage?: Storage
requires?: ImportResource[]
requires_css?: ImportResource[]
resources?: ImportResource[]
}

export interface ImportResource {
meta: { name: string, url: string, ts: number, mimetype: string }
source: string
scripts: { [key: string]: SubscribeScript };
modified: number
// 导入用,解析source获得
metadata?: Metadata
subscribe?: InstallSubscribe
old?: InstallSubscribe
error?: string
base64: string
}

export interface Options {

export interface ExportSubscribe {
name: string
subscribe: InstallSubscribe
enabled: boolean
}

export interface ImportSubscribe {
source: string
enabled: boolean
scripts: { [key: string]: SubscribeScript };
}

export interface Resource {
export interface ExportResource {
meta: { name: string, url: string, ts: number, mimetype: string }
source: string
base64?: string
hash: ResourceHash
base64: string
}

export interface Storage {
Expand Down
Loading

0 comments on commit 6cd369e

Please sign in to comment.