Skip to content

Commit

Permalink
refactor: remove joplinData
Browse files Browse the repository at this point in the history
  • Loading branch information
ylc395 committed Nov 1, 2021
1 parent fafcf76 commit 9a9ad44
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 110 deletions.
32 changes: 11 additions & 21 deletions src/domain/repository/JoplinDataRepository.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,36 @@
import { container, InjectionToken } from 'tsyringe';
import type joplin from 'api';
import { container } from 'tsyringe';
import type { Note, Tag, Resource, File } from '../model/JoplinData';
import { joplinToken } from '../service/AppService';

export type JoplinGetParams = Parameters<typeof joplin['data']['get']>;

interface JoplinFetcher {
fetchData: <T>(...args: JoplinGetParams) => Promise<T | null>;
fetchAllData: <T>(...args: JoplinGetParams) => Promise<T[]>;
fetchPluginSetting: <T>(key: string) => Promise<T | null>;
}

export const token: InjectionToken<JoplinFetcher> = Symbol('joplinData');
export class JoplinDataRepository {
private joplinFetcher = container.resolve(token);
private joplin = container.resolve(joplinToken);
searchNotes(query: string) {
const fields = 'id,title';
return this.joplinFetcher.fetchAllData<Note>(['search'], { query, fields });
return this.joplin.fetchAllData<Note>(['search'], { query, fields });
}

getTagsOf(noteId: string) {
return this.joplinFetcher.fetchAllData<Tag>(['notes', noteId, 'tags']);
return this.joplin.fetchAllData<Tag>(['notes', noteId, 'tags']);
}

async getFilesOf(noteId: string) {
const resources = await this.joplinFetcher.fetchAllData<Resource>(
['notes', noteId, 'resources'],
{ fields: 'id,mime' },
);
const resources = await this.joplin.fetchAllData<Resource>(['notes', noteId, 'resources'], {
fields: 'id,mime',
});

if (resources.length === 0) {
return [];
}

const files = await Promise.all(
resources.map(({ id }) => this.joplinFetcher.fetchData<File>(['resources', id, 'file'])),
resources.map(({ id }) => this.joplin.fetchData<File>(['resources', id, 'file'])),
);

return files;
}

async getNote(noteId: string) {
const note = await this.joplinFetcher.fetchData<Required<Note>>(['notes', noteId], {
const note = await this.joplin.fetchData<Required<Note>>(['notes', noteId], {
fields: 'id,title,user_created_time,user_updated_time,body',
});

Expand All @@ -49,7 +39,7 @@ export class JoplinDataRepository {

async getGithubToken() {
try {
return await this.joplinFetcher.fetchPluginSetting<string>('githubToken');
return await this.joplin.fetchPluginSetting<string>('githubToken');
} catch {
return '';
}
Expand Down
6 changes: 6 additions & 0 deletions src/domain/service/AppService.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type joplin from 'api';
import { InjectionKey, reactive } from 'vue';
import { container, InjectionToken, singleton } from 'tsyringe';
import { isEqual, last, pull } from 'lodash';
Expand All @@ -19,7 +20,12 @@ export interface UI {
resizeWindow: (width: number, height: number) => void;
}

export type JoplinGetParams = Parameters<typeof joplin['data']['get']>;

export interface JoplinApp {
fetchData: <T>(...args: JoplinGetParams) => Promise<T | null>;
fetchAllData: <T>(...args: JoplinGetParams) => Promise<T[]>;
fetchPluginSetting: <T>(key: string) => Promise<T | null>;
quit: () => never;
openNote: (noteId: string) => Promise<void>;
getInstallationDir: () => Promise<string>;
Expand Down
2 changes: 1 addition & 1 deletion src/driver/generator/joplinPlugin/MarkdownRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Article } from 'domain/model/Article';
import type { Resource } from 'domain/model/JoplinData';
import type { File } from 'domain/model/JoplinData';
import fs from 'driver/fs/joplinPlugin';
import { fetchData, fetchAllData } from 'driver/joplinData/joplinPlugin';
import { fetchData, fetchAllData } from 'driver/joplin/joplinPlugin';
import { getMarkdownPluginAssetsDir, getOutputDir } from './pathHelper';
import type { RenderResultPluginAsset, ResourceMap } from '../type';
import { sanitizeMarkdownHtml } from './htmlProcess';
Expand Down
40 changes: 39 additions & 1 deletion src/driver/joplin/joplinPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { SettingItemType, ViewHandle } from 'api/types';
import type JoplinViewsPanels from 'api/JoplinViewsPanels';
import type JoplinViewsDialogs from 'api/JoplinViewsDialogs';
import { container } from 'tsyringe';
import { isNumber } from 'lodash';

import type { JoplinGetParams } from 'domain/service/AppService';
import { Db } from 'driver/db/joplinPlugin';
import webviewBridge from 'driver/webview/webviewBridge';
import { isNumber } from 'lodash';

const OPEN_PAGES_PUBLISHER_COMMAND = 'openPagesPublisher';
const db = container.resolve(Db);
Expand All @@ -14,6 +16,11 @@ enum UIType {
Panel,
}

interface JoplinResponse<T> {
items: T[];
has_more: boolean;
}

const UI_TYPE_SETTING = 'uiType';
const UI_SIZE_SETTING = 'uiSize';
const DEFAULT_UI_SIZE = '600*640';
Expand All @@ -22,9 +29,40 @@ const isValidUISize = (size: unknown): size is [number, number] =>

const IS_NEW_USER_SETTING = 'isNewUser';

export function fetchData<T>(...args: JoplinGetParams) {
return joplinApi.data.get(...args) as Promise<T>;
}

export async function fetchAllData<T>(...[path, query]: JoplinGetParams) {
let result: T[] = [];
let page = 1;
let hasMore = true;

while (hasMore) {
const { items, has_more } = await fetchData<JoplinResponse<T>>(path, {
...query,
page: page++,
});

result = result.concat(items);
hasMore = has_more;
}

return result;
}

export default class Joplin {
private windowHandler?: ViewHandle;
private uiType?: UIType;

fetchData(...args: JoplinGetParams) {
return fetchData(...args);
}

fetchAllData(...args: JoplinGetParams) {
return fetchAllData(...args);
}

get ui() {
if (this.uiType === undefined) {
throw new Error('no ui type');
Expand Down
46 changes: 44 additions & 2 deletions src/driver/joplin/webviewApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { container } from 'tsyringe';
import { joplinToken } from 'domain/service/AppService';
import { joplinToken, JoplinGetParams } from 'domain/service/AppService';

export interface JoplinAction {
event:
| 'quit'
Expand All @@ -12,11 +13,52 @@ export interface JoplinAction {
payload?: any;
}

export interface JoplinDataRequest {
event: 'getJoplinData' | 'getJoplinDataAll';
args: JoplinGetParams;
}

export interface JoplinPluginSettingRequest {
event: 'getJoplinPluginSetting';
key: string;
}

declare const webviewApi: {
postMessage: <T = void>(payload: JoplinAction) => Promise<T>;
postMessage: <T = void>(
payload: JoplinAction | JoplinDataRequest | JoplinPluginSettingRequest,
) => Promise<T>;
};

const joplin = {
fetchData<T>(...args: JoplinGetParams) {
return webviewApi
.postMessage<T>({
event: 'getJoplinData',
args,
})
.catch(() => null);
},

fetchAllData<T>(...args: JoplinGetParams) {
return webviewApi
.postMessage<T[]>({
event: 'getJoplinDataAll',
args,
})
.catch(() => [] as T[]);
},

async fetchPluginSetting<T>(key: string) {
try {
return await webviewApi.postMessage<T>({
event: 'getJoplinPluginSetting',
key,
});
} catch {
return null;
}
},

quit() {
return webviewApi.postMessage({ event: 'quit' }) as never;
},
Expand Down
29 changes: 0 additions & 29 deletions src/driver/joplinData/joplinPlugin.ts

This file was deleted.

50 changes: 0 additions & 50 deletions src/driver/joplinData/webviewApi.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/driver/webview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { container } from 'tsyringe';
import 'driver/db/webviewApi';
import 'driver/themeLoader/webviewApi';
import 'driver/generator/webviewApi';
import 'driver/joplinData/webviewApi';
import 'driver/joplin/webviewApi';
import 'driver/git/webviewApi';
import 'driver/github/webviewApi';
Expand Down
12 changes: 7 additions & 5 deletions src/driver/webview/webviewBridge.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { container } from 'tsyringe';
import { Db } from 'driver/db/joplinPlugin';
import type { DbReadRequest, DbWriteRequest } from 'driver/db/webviewApi';
import type { JoplinDataRequest, JoplinPluginSettingRequest } from 'driver/joplinData/webviewApi';
import type { JoplinAction } from 'driver/joplin/webviewApi';
import type {
JoplinAction,
JoplinDataRequest,
JoplinPluginSettingRequest,
} from 'driver/joplin/webviewApi';
import type { GitRequest } from 'driver/git/webviewApi';
import type { FsRequest } from 'driver/fs/webviewApi';
import type { GeneratorRequest } from 'driver/generator/webviewApi';
Expand All @@ -14,7 +17,6 @@ import type {
import { loadTheme, loadThemes } from 'driver/themeLoader/joplinPlugin';
import { generateSite, getProgress } from 'driver/generator/joplinPlugin';
import { getOutputDir, getGitRepositoryDir } from 'driver/generator/joplinPlugin/pathHelper';
import { fetchData, fetchAllData } from 'driver/joplinData/joplinPlugin';
import { mockNodeFsCall } from 'driver/fs/joplinPlugin';
import type Joplin from 'driver/joplin/joplinPlugin';

Expand All @@ -40,9 +42,9 @@ export default (joplin: Joplin) => {
case 'dbSave':
return db.save(...request.args);
case 'getJoplinData':
return fetchData(...request.args);
return joplin.fetchData(...request.args);
case 'getJoplinDataAll':
return fetchAllData(...request.args);
return joplin.fetchAllData(...request.args);
case 'loadThemeConfig':
return loadTheme(request.themeName);
case 'loadThemeConfigs':
Expand Down

0 comments on commit 9a9ad44

Please sign in to comment.