Skip to content

Commit ee17cc5

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

2 files changed

Lines changed: 37 additions & 37 deletions

File tree

docs/pwa_share_image.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ The `/share` handler in `src/serviceworker.js` processes incoming share requests
3535
* The file blob is stored in the `IMAGE_STORE` IndexedDB using `addImageDB`.
3636
* A markdown image reference `![Shared Image](/images/<id>)` is created.
3737
## 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.
38+
* **Images**: Each shared image now creates an individual note titled 'Shared Image <timestamp>' with the tag `image`. Any accompanying text/link content is included within the note's content.
3939
* **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.
4140
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: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -294,48 +294,49 @@ self.addEventListener('fetch', (event) => {
294294
let targetNote;
295295

296296
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);
297+
// Create a new individual note for the shared image
298+
const now = new Date();
299+
const timestamp = `${String(now.getFullYear()).slice(-2)}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}-${String(now.getHours()).padStart(2, '0')}${String(now.getMinutes()).padStart(2, '0')}${String(now.getSeconds()).padStart(2, '0')}`;
300+
301+
const imageNote = {
302+
id: 'shared-image-' + generateUniqueId(),
303+
title: 'Shared Image ' + timestamp,
304+
content: imageReference + (newItem.trim() !== '*' ? '\n\n' + newItem : ''),
305+
createdAt: new Date().toISOString(),
306+
updatedAt: new Date().toISOString(),
307+
tags: ['shared', 'image'],
308+
};
309+
await addNoteDB(imageNote);
310+
noteIdToRedirect = imageNote.id;
300311
} else {
301312
targetNoteId = await getMetaDB('shared_inbox_id');
302313
targetNote = targetNoteId ? await getNoteDB(targetNoteId) : null;
303314
if (!targetNote) targetNote = await getNoteByTitleDB(TITLE_SHARED);
304-
}
305-
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');
309-
310-
if (targetNote) {
311-
if (targetNote.content) {
312-
targetNote.content = finalContent + '\n' + targetNote.content;
313-
} else {
314-
targetNote.content = finalContent;
315-
}
316-
targetNote.updatedAt = new Date().toISOString();
317-
await updateNoteDB(targetNote);
318-
noteIdToRedirect = targetNote.id;
319315

320-
if (imageFile instanceof File) {
321-
if (targetNote.id !== await getMetaDB('shared_images_id')) await setMetaDB('shared_images_id', targetNote.id);
316+
const finalContent = newItem;
317+
if (targetNote) {
318+
if (targetNote.content) {
319+
targetNote.content = finalContent + '\n' + targetNote.content;
320+
} else {
321+
targetNote.content = finalContent;
322+
}
323+
targetNote.updatedAt = new Date().toISOString();
324+
await updateNoteDB(targetNote);
325+
noteIdToRedirect = targetNote.id;
326+
if (targetNote.id !== targetNoteId) await setMetaDB('shared_inbox_id', targetNote.id);
322327
} else {
323-
if (targetNote.id !== await getMetaDB('shared_inbox_id')) await setMetaDB('shared_inbox_id', targetNote.id);
328+
const newNote = {
329+
id: 'shared-inbox-' + generateUniqueId(),
330+
title: TITLE_SHARED,
331+
content: finalContent,
332+
createdAt: new Date().toISOString(),
333+
updatedAt: new Date().toISOString(),
334+
tags: ['shared', 'inbox'],
335+
};
336+
await addNoteDB(newNote);
337+
noteIdToRedirect = newNote.id;
338+
await setMetaDB('shared_inbox_id', newNote.id);
324339
}
325-
} else {
326-
// Create a new note
327-
const newNote = {
328-
id: (imageFile instanceof File ? 'shared-images-' : 'shared-inbox-') + generateUniqueId(),
329-
title: imageFile instanceof File ? TITLE_SHARED_IMAGES : TITLE_SHARED,
330-
content: finalContent,
331-
createdAt: new Date().toISOString(),
332-
updatedAt: new Date().toISOString(),
333-
tags: ['shared', imageFile instanceof File ? 'images' : 'inbox'],
334-
};
335-
await addNoteDB(newNote);
336-
noteIdToRedirect = newNote.id;
337-
if (imageFile instanceof File) await setMetaDB('shared_images_id', newNote.id);
338-
else await setMetaDB('shared_inbox_id', newNote.id);
339340
}
340341
}
341342
} catch (criticalError) {

0 commit comments

Comments
 (0)