import { Query } from '@kevisual/query'; import type { Result, DataOpts } from '@kevisual/query/query'; export type SimpleObject = Record; export const markType = ['simple', 'md', 'mdx', 'wallnote', 'excalidraw', 'chat'] as const; export type MarkType = (typeof markType)[number]; export type MarkData = { nodes?: any[]; edges?: any[]; elements?: any[]; permission?: any; [key: string]: any; }; export type Mark = { id: string; title: string; description: string; markType: MarkType; link: string; data?: MarkData; uid: string; puid: string; summary: string; thumbnail?: string; tags: string[]; createdAt: string; updatedAt: string; version: number; }; export type ShowMarkPick = Pick; export type SearchOpts = { page?: number; pageSize?: number; search?: string; sort?: string; // DESC, ASC markType?: MarkType; // 类型 [key: string]: any; }; export type QueryMarkOpts = { query?: Query; isBrowser?: boolean; onLoad?: () => void; } & T; export type ResultMarkList = { list: Mark[]; pagination: { pageSize: number; current: number; total: number; }; }; export type QueryMarkData = { id?: string; title?: string; description?: string; [key: string]: any; }; export type QueryMarkResult = { accessToken: string; refreshToken: string; }; export class QueryMarkBase { query: Query; isBrowser: boolean; load?: boolean; storage?: Storage; onLoad?: () => void; constructor(opts?: QueryMarkOpts) { this.query = opts?.query || new Query(); this.isBrowser = opts?.isBrowser ?? true; this.init(); this.onLoad = opts?.onLoad; } setQuery(query: Query) { this.query = query; } private async init() { this.load = true; this.onLoad?.(); } async post>(data: any, opts?: DataOpts): Promise { try { return this.query.post({ path: 'mark', ...data }, opts) as Promise; } catch (error) { console.log('error', error); return { code: 400, } as any; } } async getMarkList(search: SearchOpts, opts?: DataOpts) { return this.post>({ key: 'list', ...search }, opts); } async getMark(id: string, opts?: DataOpts) { return this.post>({ key: 'get', id }, opts); } async getVersion(id: string, opts?: DataOpts) { return this.post>({ key: 'getVersion', id }, opts); } /** * 检查版本 * 当需要更新时,返回true * @param id * @param version * @param opts * @returns */ async checkVersion(id: string, version?: number, opts?: DataOpts) { if (!version) { return true; } const res = await this.getVersion(id, opts); if (res.code === 200) { if (res.data!.version > version) { return true; } return false; } return true; } async updateMark(data: any, opts?: DataOpts) { return this.post>({ key: 'update', data }, opts); } async deleteMark(id: string, opts?: DataOpts) { return this.post>({ key: 'delete', id }, opts); } } export class QueryMark extends QueryMarkBase { markType: string; constructor(opts?: QueryMarkOpts & { markType?: MarkType }) { super(opts); this.markType = opts?.markType || 'simple'; } async getMarkList(search?: SearchOpts, opts?: DataOpts) { return this.post>({ key: 'list', ...search, markType: this.markType }, opts); } async updateMark(data: any, opts?: DataOpts) { if (!data.id) { data.markType = this.markType || 'simple'; } return super.updateMark(data, opts); } }