diff --git a/apps/desktop/main/src/fetch.ts b/apps/desktop/main/src/fetch.ts index f174934..3a5b1d6 100644 --- a/apps/desktop/main/src/fetch.ts +++ b/apps/desktop/main/src/fetch.ts @@ -1,5 +1,5 @@ import type { IPCFetch } from '@types' -import { userInfo } from '@core/services' +import { userDetail, userInfo } from '@core/services' import { IPCMain } from '../../utils' const channel = 'fetch' @@ -8,4 +8,5 @@ export function setupFetchMainIPC() { const IPC = new IPCMain(channel) IPC.on('userInfo', userInfo) + IPC.on('userDetail', userDetail) } diff --git a/apps/desktop/preload/src/index.ts b/apps/desktop/preload/src/index.ts index b850188..2c74196 100644 --- a/apps/desktop/preload/src/index.ts +++ b/apps/desktop/preload/src/index.ts @@ -1,6 +1,7 @@ import customTitlebar from 'custom-electron-titlebar' +import { contextBridge } from 'electron' import type { DBMethods } from '@database' -import type { AppConfig, IPCFetch, IPCFile } from '@types' +import type { IPCFetch, IPCFile } from '@types' import { config } from '@core/utils/config' import { IPCRenderer } from '../../utils' @@ -21,8 +22,7 @@ export const dbIPC = (() => { } })() -// TODO: fileIPC -export const FileIPC: IPCFile = (() => { +export const fileIPC: IPCFile = (() => { const IPC = new IPCRenderer('files') return { @@ -32,19 +32,20 @@ export const FileIPC: IPCFile = (() => { } })() -export const FetchIPC: IPCFetch = (() => { +export const fetchIPC: IPCFetch = (() => { const IPC = new IPCRenderer('fetch') return { - userInfo: (id, name) => IPC.send('userInfo', id, name), + userInfo: options => IPC.send('userInfo', options), + userDetail: uid => IPC.send('userDetail', uid), } })() -type OnChange = (callback: (newValue: AppConfig, oldValue: AppConfig) => void) => Function -export const _config = { +// unplugin-auto-expose 在 hmr 时总是会导致 undefined 的问题 +contextBridge.exposeInMainWorld('config', { set: config.set.bind(config), get: config.get.bind(config), - onChange: config.onDidAnyChange.bind(config) as OnChange, + onChange: config.onDidAnyChange.bind(config), path: config.path, data: config.store, -} +}) diff --git a/apps/desktop/renderer/src/App.vue b/apps/desktop/renderer/src/App.vue index e1b4ca2..f8287f8 100644 --- a/apps/desktop/renderer/src/App.vue +++ b/apps/desktop/renderer/src/App.vue @@ -1,6 +1,6 @@ diff --git a/apps/desktop/renderer/src/components/File.vue b/apps/desktop/renderer/src/components/File.vue deleted file mode 100644 index 883b592..0000000 --- a/apps/desktop/renderer/src/components/File.vue +++ /dev/null @@ -1,53 +0,0 @@ - - - diff --git a/apps/desktop/renderer/src/components/Sqlite.vue b/apps/desktop/renderer/src/components/Sqlite.vue deleted file mode 100644 index 94a5459..0000000 --- a/apps/desktop/renderer/src/components/Sqlite.vue +++ /dev/null @@ -1,50 +0,0 @@ - - - diff --git a/apps/desktop/renderer/src/composables/config.ts b/apps/desktop/renderer/src/composables/config.ts index 18789ad..dc3debf 100644 --- a/apps/desktop/renderer/src/composables/config.ts +++ b/apps/desktop/renderer/src/composables/config.ts @@ -1,12 +1,5 @@ -import { _config } from '#preload' +export const configRef = ref(window.config.data) -const configRef = ref(_config.data) - -_config.onChange((newVal) => { +window.config.onChange((newVal) => { configRef.value = newVal }) - -export { - configRef, - _config as config, -} diff --git a/apps/desktop/renderer/src/pages/index.vue b/apps/desktop/renderer/src/pages/index.vue index dfaea62..56b339d 100644 --- a/apps/desktop/renderer/src/pages/index.vue +++ b/apps/desktop/renderer/src/pages/index.vue @@ -1,17 +1,22 @@ diff --git a/packages/core/src/constants/index.ts b/packages/core/src/constants/index.ts index 7bd1e2b..a784122 100644 --- a/packages/core/src/constants/index.ts +++ b/packages/core/src/constants/index.ts @@ -2,3 +2,8 @@ * 占位图 */ export const imgViewSrc = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7' + +/** + * 默认的图片 CDN + */ +export const imgCdn = 'https://cdn.ipfsscan.io/weibo' diff --git a/packages/core/src/services/userService.ts b/packages/core/src/services/userService.ts index 61fa4c6..7754595 100644 --- a/packages/core/src/services/userService.ts +++ b/packages/core/src/services/userService.ts @@ -42,10 +42,10 @@ export async function userDetail(uid?: string) { const detail = { createdAt: data.created_at, - birsday: data.birthday, + birthday: data.birthday, } as { createdAt: string - birsday: string + birthday: string } const info = await userInfo({ id: _uid }) diff --git a/packages/core/src/utils/config.ts b/packages/core/src/utils/config.ts index e76c4fe..277f1e0 100644 --- a/packages/core/src/utils/config.ts +++ b/packages/core/src/utils/config.ts @@ -19,7 +19,9 @@ const config = new Config({ osSep: '/', version, theme: 'light', + useCdn: false, fetchOptions: { + now, uid: '', name: '', cookie: '', @@ -37,7 +39,9 @@ const config = new Config({ const path = config.path.replace(/config\.json$/, '') config.set('configPath', path) -// config.set('fetchOptions.cookie', cookie ?? '') +config.set('version', version) +config.set('fetchOptions.now', now) +config.set('fetchOptions.dateRange', [now, now]) export default config export { diff --git a/packages/core/src/utils/dom.ts b/packages/core/src/utils/dom.ts index 3daddbc..8a35b50 100644 --- a/packages/core/src/utils/dom.ts +++ b/packages/core/src/utils/dom.ts @@ -23,7 +23,9 @@ export function waitForElement( }) } -export function lazyLoadImage() { +export function lazyLoadImage( + imgs?: NodeListOf | HTMLImageElement[], +) { const observer = new IntersectionObserver((entries) => { entries.forEach((entry) => { if (entry.isIntersecting) { @@ -39,10 +41,15 @@ export function lazyLoadImage() { }) }) - const imgs = document.querySelectorAll('.n-image img') + if (imgs) { + imgs.forEach((img) => { + if (img.src.endsWith('/placeholder.webp')) + observer.observe(img) + }) + } - imgs.forEach((img) => { - if (img.src.endsWith('/placeholder.webp')) - observer.observe(img) - }) + return { + observe: (img: HTMLImageElement) => observer.observe(img), + disconnect: () => observer.disconnect(), + } } diff --git a/packages/core/src/utils/parse.ts b/packages/core/src/utils/parse.ts index 9232f9d..5905660 100644 --- a/packages/core/src/utils/parse.ts +++ b/packages/core/src/utils/parse.ts @@ -1,7 +1,8 @@ import type { CardInfo, Comment, FetchOptions, ParseResult, PicInfo, Post } from '@types' import { fetchComments, fetchLongText } from '../services' import { imgViewSrc } from '../constants' -import { getOptions, isInMonkey } from '.' +import { imgCdn } from './../constants/index' +import { getOptions } from '.' export const weibo = 'https://weibo.com' @@ -41,8 +42,9 @@ export function replaceImg(img?: string) { if (!img) return imgViewSrc - if (isInMonkey || img.includes('data:image')) + if (img.includes('data:image') || img.startsWith(imgCdn)) return img + const name = img.split('/').pop()?.replace(/\?.+/, '') // 同时去除 params const prefix = img.match(/^(?:https?:\/\/)?([^:\/\n]+)/im)?.[1] // 域名 diff --git a/packages/ui/src/MainImage.vue b/packages/ui/src/MainImage.vue index 9b815df..f5ec6b8 100644 --- a/packages/ui/src/MainImage.vue +++ b/packages/ui/src/MainImage.vue @@ -14,28 +14,41 @@ const props = withDefaults(defineProps<{ }) const realSrc = ref(props.src) -const inElectron = computed(() => import.meta.env.PROD && isElectron && !props.src.startsWith('/')) +const imgRef = ref() +const disconnectFn = ref<() => void>() -// TODO: build:web 的时候注释下面这段 -onBeforeMount(async () => { - if (!inElectron.value) +onMounted(() => { + const img = imgRef.value.imageRef as HTMLImageElement + const { disconnect } = lazyLoadImage([img]) + disconnectFn.value = disconnect + + if (!isElectron) { + realSrc.value = replaceImg(props.src) + return + } + + if (import.meta.env.DEV || !props.src.startsWith('/')) return - const { _config } = await import('#preload') - const { publicPath } = _config.data - const src = `file://${publicPath}${props.src.slice(1)}` + const config = window.config.data + const src = `file://${config.publicPath}${props.src.slice(1)}` realSrc.value = src }) + +onUnmounted(() => { + disconnectFn.value?.() +})