From 16094c72927546b72d2e07d97b331367c7b0f644 Mon Sep 17 00:00:00 2001 From: pipipi-pikachu Date: Tue, 31 May 2022 23:00:44 +0800 Subject: [PATCH] feat: file-handling API test --- src/App.vue | 13 +++++++++++++ src/hooks/useExport.ts | 8 +++++--- src/views/Editor/EditorHeader/index.vue | 1 + vue.config.js | 8 ++++++++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/App.vue b/src/App.vue index 087985a5..8b1b187a 100644 --- a/src/App.vue +++ b/src/App.vue @@ -10,6 +10,7 @@ import { storeToRefs } from 'pinia' import { useScreenStore, useMainStore, useSnapshotStore } from '@/store' import { LOCALSTORAGE_KEY_DISCARDED_DB } from '@/configs/storage' import { isPC } from './utils/common' +import useExport from './hooks/useExport' import Editor from './views/Editor/index.vue' import Screen from './views/Screen/index.vue' @@ -35,6 +36,18 @@ export default defineComponent({ onMounted(() => { snapshotStore.initSnapshotDatabase() mainStore.setAvailableFonts() + + const { importSpecificFile } = useExport() + + if ('launchQueue' in window) { + /* eslint-disable-next-line */ + (window as any).launchQueue.setConsumer(async (launchParams: any) => { + if (launchParams.files && launchParams.files.length) { + const files: File[] = launchParams.files + importSpecificFile(files, true) + } + }) + } }) // 应用注销时向 localStorage 中记录下本次 indexedDB 的数据库ID,用于之后清除数据库 diff --git a/src/hooks/useExport.ts b/src/hooks/useExport.ts index bb89f642..f21c366a 100644 --- a/src/hooks/useExport.ts +++ b/src/hooks/useExport.ts @@ -22,7 +22,8 @@ interface ExportImageConfig { } export default () => { - const { slides, theme, viewportRatio } = storeToRefs(useSlidesStore()) + const slidesStore = useSlidesStore() + const { slides, theme, viewportRatio } = storeToRefs(slidesStore) const { addSlidesFromData } = useAddSlidesOrElements() @@ -61,14 +62,15 @@ export default () => { } // 导入pptist文件 - const importSpecificFile = (files: File[]) => { + const importSpecificFile = (files: File[], cover = false) => { const file = files[0] const reader = new FileReader() reader.addEventListener('load', () => { try { const slides = JSON.parse(decrypt(reader.result as string)) - addSlidesFromData(slides) + if (cover) slidesStore.setSlides(slides) + else addSlidesFromData(slides) } catch { message.error('无法正确读取 / 解析该文件') diff --git a/src/views/Editor/EditorHeader/index.vue b/src/views/Editor/EditorHeader/index.vue index d376c0ec..d38e74df 100644 --- a/src/views/Editor/EditorHeader/index.vue +++ b/src/views/Editor/EditorHeader/index.vue @@ -10,6 +10,7 @@ 导出 PPTX 导出图片 + 导出 .pptist 文件 导出 JSON 打印 / 导出 PDF diff --git a/vue.config.js b/vue.config.js index ffbacf94..b451a2bc 100644 --- a/vue.config.js +++ b/vue.config.js @@ -67,6 +67,14 @@ module.exports = { type: 'image/png', purpose: 'maskable' }], + file_handlers: [ + { + action: '/', + accept: { + '*': ['.pptist'], + } + } + ], start_url: '.', display: 'standalone', background_color: '#000000',