From 5e7440aaeed3179ac4748e08516017cf12d2bd7d Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sun, 14 Mar 2021 10:20:23 -0700 Subject: [PATCH] feat: accept files in web share target (#1992) fixes #1009 --- bin/build-vercel-json.js | 2 +- .../_actions/showShareDialogIfNecessary.js | 38 +++++++-- src/routes/_database/databaseApis.js | 1 + src/routes/_database/webShare.js | 18 ++++ src/routes/_pages/search.html | 1 - src/routes/_static/share.js | 1 + src/routes/_store/store.js | 3 +- .../_utils/decodeURIComponentWithPluses.js | 7 -- src/routes/_utils/lifecycle.js | 4 +- src/routes/share.html | 29 ------- src/service-worker.js | 13 +++ static/manifest.json | 17 +++- tests/spec/027-share-target.js | 58 ------------- vercel.json | 83 ++++++++----------- webpack/client.config.js | 3 +- webpack/server.config.js | 3 +- webpack/service-worker.config.js | 3 +- 17 files changed, 122 insertions(+), 162 deletions(-) create mode 100644 src/routes/_database/webShare.js create mode 100644 src/routes/_static/share.js delete mode 100644 src/routes/_utils/decodeURIComponentWithPluses.js delete mode 100644 src/routes/share.html delete mode 100644 tests/spec/027-share-target.js diff --git a/bin/build-vercel-json.js b/bin/build-vercel-json.js index 756d927c..fdf1eb8e 100644 --- a/bin/build-vercel-json.js +++ b/bin/build-vercel-json.js @@ -79,7 +79,7 @@ const HTML_HEADERS = { "frame-ancestors 'none'", "object-src 'none'", "manifest-src 'self'", - "form-action 'none'", + "form-action 'self'", // we need form-action for the Web Share Target API "base-uri 'self'" ].join(';'), 'referrer-policy': 'no-referrer', diff --git a/src/routes/_actions/showShareDialogIfNecessary.js b/src/routes/_actions/showShareDialogIfNecessary.js index dcf0f231..f86aeed5 100644 --- a/src/routes/_actions/showShareDialogIfNecessary.js +++ b/src/routes/_actions/showShareDialogIfNecessary.js @@ -1,11 +1,37 @@ import { store } from '../_store/store' -import { importShowComposeDialog } from '../_components/dialog/asyncDialogs/importShowComposeDialog.js' +import { importShowComposeDialog } from '../_components/dialog/asyncDialogs/importShowComposeDialog' +import { database } from '../_database/database' +import { doMediaUpload } from './media' export async function showShareDialogIfNecessary () { - const { isUserLoggedIn, openShareDialog } = store.get() - store.set({ openShareDialog: false }) - if (isUserLoggedIn && openShareDialog) { - const showComposeDialog = await importShowComposeDialog() - showComposeDialog() + const { isUserLoggedIn } = store.get() + if (!isUserLoggedIn) { + return + } + const data = await database.getWebShareData() + if (!data) { + return + } + + // delete from IDB and import the dialog in parallel + const [showComposeDialog] = await Promise.all([ + importShowComposeDialog(), + database.deleteWebShareData() + ]) + + console.log('share data', data) + const { title, text, url, file } = data + + // url is currently ignored on Android, but one can dream + // https://web.dev/web-share-target/#verifying-shared-content + const composeText = [title, text, url].filter(Boolean).join('\n\n') + + store.clearComposeData('dialog') + store.setComposeData('dialog', { text: composeText }) + store.save() + + showComposeDialog() + if (file) { // start the upload once the dialog is in view so it shows the loading spinner and everything + /* no await */ doMediaUpload('dialog', file) } } diff --git a/src/routes/_database/databaseApis.js b/src/routes/_database/databaseApis.js index 6f9bf298..4a39760a 100644 --- a/src/routes/_database/databaseApis.js +++ b/src/routes/_database/databaseApis.js @@ -9,3 +9,4 @@ export * from './timelines/deletion' export { insertTimelineItems, insertStatus } from './timelines/insertion' export * from './meta' export * from './relationships' +export * from './webShare' diff --git a/src/routes/_database/webShare.js b/src/routes/_database/webShare.js new file mode 100644 index 00000000..2d54f130 --- /dev/null +++ b/src/routes/_database/webShare.js @@ -0,0 +1,18 @@ +import { get, set, close, del } from '../_thirdparty/idb-keyval/idb-keyval' +import { WEB_SHARE_TARGET_DATA_IDB_KEY } from '../_static/share' + +export function deleteWebShareData () { + return del(WEB_SHARE_TARGET_DATA_IDB_KEY) +} + +export function setWebShareData (data) { + return set(WEB_SHARE_TARGET_DATA_IDB_KEY, data) +} + +export function getWebShareData () { + return get(WEB_SHARE_TARGET_DATA_IDB_KEY) +} + +export function closeKeyValIDBConnection () { + return close() +} diff --git a/src/routes/_pages/search.html b/src/routes/_pages/search.html index 9145b390..0edd6f4e 100644 --- a/src/routes/_pages/search.html +++ b/src/routes/_pages/search.html @@ -19,7 +19,6 @@ {intl.favorites} {intl.directMessages} {intl.bookmarks} - {intl.shareStatus} {/if}