Skip to content

Commit b7a23c0

Browse files
author
tung@cloud.phamthanh.me
committed
.
1 parent 19b0f59 commit b7a23c0

2 files changed

Lines changed: 38 additions & 24 deletions

File tree

docs/pwa_share_image.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ The `/share` handler in `src/serviceworker.js` processes incoming share requests
3434
* A unique ID is generated via `generateUniqueId('img')`.
3535
* The file blob is stored in the `IMAGE_STORE` IndexedDB using `addImageDB`.
3636
* A markdown image reference `![Shared Image](/images/<id>)` is created.
37-
4. **Note Integration**:
38-
* The image reference and existing text/link content are combined into a final string using `filter(Boolean).join('\n\n')`.
39-
* This content is prepended to the existing 'Shared Inbox' note or used to create a new one if it doesn't exist.
37+
## Note Integration
38+
* **Images**: If a file is shared, it is stored in the 'Shared Images' note (or created if it doesn't exist). Any text/link content associated with the share is appended after the image markdown.
39+
* **Text/Links**: If only text or a URL is shared, it is stored in the 'Shared Inbox' note as before.
40+
* **Unique Destinations**: The app uses `shared_images_id` and `shared_inbox_id` meta keys to keep these destinations separate.
4041
5. **Backward Compatibility**: The code checks `imageFile instanceof File`. If no image is shared, the reference remains empty and the text/link sharing logic proceeds as it did previously.

src/serviceworker.js

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -288,41 +288,54 @@ self.addEventListener('fetch', (event) => {
288288

289289
if (newItem.trim() !== '*') {
290290
const TITLE_SHARED = 'Shared Inbox';
291-
let inboxNoteId = await getMetaDB('shared_inbox_id');
292-
let inboxNote = inboxNoteId ? await getNoteDB(inboxNoteId) : null;
293-
294-
if (!inboxNote) {
295-
inboxNote = await getNoteByTitleDB(TITLE_SHARED);
291+
const TITLE_SHARED_IMAGES = 'Shared Images';
292+
293+
let targetNoteId;
294+
let targetNote;
295+
296+
if (imageFile instanceof File) {
297+
targetNoteId = await getMetaDB('shared_images_id');
298+
targetNote = targetNoteId ? await getNoteDB(targetNoteId) : null;
299+
if (!targetNote) targetNote = await getNoteByTitleDB(TITLE_SHARED_IMAGES);
300+
} else {
301+
targetNoteId = await getMetaDB('shared_inbox_id');
302+
targetNote = targetNoteId ? await getNoteDB(targetNoteId) : null;
303+
if (!targetNote) targetNote = await getNoteByTitleDB(TITLE_SHARED);
296304
}
297305

298-
// Combine image reference and the new item content
299-
const finalContent = [imageReference, newItem].filter(Boolean).join('\n\n');
306+
const contentToStore = imageFile instanceof File ? imageReference : newItem;
307+
const additionalContent = imageFile instanceof File && newItem.trim() !== '*' ? newItem : '';
308+
const finalContent = [contentToStore, additionalContent].filter(Boolean).join('\n\n');
300309

301-
if (inboxNote) {
302-
if (inboxNote.content) {
303-
inboxNote.content = finalContent + '\n' + inboxNote.content;
310+
if (targetNote) {
311+
if (targetNote.content) {
312+
targetNote.content = finalContent + '\n' + targetNote.content;
304313
} else {
305-
inboxNote.content = finalContent;
314+
targetNote.content = finalContent;
306315
}
307-
inboxNote.updatedAt = new Date().toISOString();
308-
await updateNoteDB(inboxNote);
309-
noteIdToRedirect = inboxNote.id;
310-
if (inboxNote.id !== inboxNoteId) {
311-
await setMetaDB('shared_inbox_id', inboxNote.id);
316+
targetNote.updatedAt = new Date().toISOString();
317+
await updateNoteDB(targetNote);
318+
noteIdToRedirect = targetNote.id;
319+
320+
if (imageFile instanceof File) {
321+
if (targetNote.id !== await getMetaDB('shared_images_id')) await setMetaDB('shared_images_id', targetNote.id);
322+
} else {
323+
if (targetNote.id !== await getMetaDB('shared_inbox_id')) await setMetaDB('shared_inbox_id', targetNote.id);
312324
}
313325
} else {
314-
// Create a new inbox note
326+
// Create a new note
315327
const newNote = {
316-
id: 'shared-inbox-' + generateUniqueId(),
317-
title: TITLE_SHARED,
328+
id: (imageFile instanceof File ? 'shared-images-' : 'shared-inbox-') + generateUniqueId(),
329+
title: imageFile instanceof File ? TITLE_SHARED_IMAGES : TITLE_SHARED,
318330
content: finalContent,
319331
createdAt: new Date().toISOString(),
320332
updatedAt: new Date().toISOString(),
321-
tags: ['shared', 'inbox'],
333+
tags: ['shared', imageFile instanceof File ? 'images' : 'inbox'],
322334
};
323335
await addNoteDB(newNote);
324336
noteIdToRedirect = newNote.id;
325-
await setMetaDB('shared_inbox_id', newNote.id);
337+
if (imageFile instanceof File) await setMetaDB('shared_images_id', newNote.id);
338+
else await setMetaDB('shared_inbox_id', newNote.id);
326339
}
327340
}
328341
} catch (criticalError) {

0 commit comments

Comments
 (0)