mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-30 18:05:55 +01:00 
			
		
		
		
	saving selections adds to the existing date note instead of creating new one each time
This commit is contained in:
		| @@ -4,22 +4,70 @@ const noteService = require('../../services/notes'); | |||||||
| const dateNoteService = require('../../services/date_notes'); | const dateNoteService = require('../../services/date_notes'); | ||||||
| const dateUtils = require('../../services/date_utils'); | const dateUtils = require('../../services/date_utils'); | ||||||
| const imageService = require('../../services/image'); | const imageService = require('../../services/image'); | ||||||
|  | const appInfo = require('../../services/app_info'); | ||||||
| const messagingService = require('../../services/messaging'); | const messagingService = require('../../services/messaging'); | ||||||
| const log = require('../../services/log'); | const log = require('../../services/log'); | ||||||
| const path = require('path'); | const path = require('path'); | ||||||
| const Link = require('../../entities/link'); | const Link = require('../../entities/link'); | ||||||
|  |  | ||||||
|  | async function findClippingNote(todayNote, pageUrl) { | ||||||
|  |     const notes = await todayNote.getDescendantNotesWithLabel('pageUrl', pageUrl); | ||||||
|  |  | ||||||
|  |     for (const note of notes) { | ||||||
|  |         if (await note.getLabelValue('clipType') === 'clippings') { | ||||||
|  |             return note; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     return null; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | async function addClipping(req) { | ||||||
|  |     const {title, content, pageUrl, images} = req.body; | ||||||
|  |  | ||||||
|  |     const todayNote = await dateNoteService.getDateNote(dateUtils.localNowDate()); | ||||||
|  |  | ||||||
|  |     let clippingNote = await findClippingNote(todayNote, pageUrl); | ||||||
|  |  | ||||||
|  |     if (!clippingNote) { | ||||||
|  |         clippingNote = (await noteService.createNote(todayNote.noteId, title, '')).note; | ||||||
|  |  | ||||||
|  |         await clippingNote.setLabel('clipType', 'clippings'); | ||||||
|  |         await clippingNote.setLabel('pageUrl', pageUrl); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     const rewrittenContent = await addImagesToNote(images, clippingNote, content); | ||||||
|  |  | ||||||
|  |     await clippingNote.setContent(await clippingNote.getContent() + rewrittenContent); | ||||||
|  |  | ||||||
|  |     return { | ||||||
|  |         noteId: clippingNote.noteId | ||||||
|  |     }; | ||||||
|  | } | ||||||
|  |  | ||||||
| async function createNote(req) { | async function createNote(req) { | ||||||
|     const {title, content, url, images} = req.body; |     const {title, content, pageUrl, images} = req.body; | ||||||
|  |  | ||||||
|     const todayNote = await dateNoteService.getDateNote(dateUtils.localNowDate()); |     const todayNote = await dateNoteService.getDateNote(dateUtils.localNowDate()); | ||||||
|  |  | ||||||
|     const {note} = await noteService.createNote(todayNote.noteId, title, content); |     const {note} = await noteService.createNote(todayNote.noteId, title, content); | ||||||
|  |  | ||||||
|     if (url) { |     await note.setLabel('clipType', 'note'); | ||||||
|         await note.setLabel('sourceUrl', url); |  | ||||||
|  |     if (pageUrl) { | ||||||
|  |         await note.setLabel('pageUrl', pageUrl); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     const rewrittenContent = await addImagesToNote(images, note, content); | ||||||
|  |  | ||||||
|  |     await note.setContent(rewrittenContent); | ||||||
|  |  | ||||||
|  |     return { | ||||||
|  |         noteId: note.noteId | ||||||
|  |     }; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | async function addImagesToNote(images, note, content) { | ||||||
|     let rewrittenContent = content; |     let rewrittenContent = content; | ||||||
|  |  | ||||||
|     if (images) { |     if (images) { | ||||||
| @@ -47,19 +95,16 @@ async function createNote(req) { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     await note.setContent(rewrittenContent); |     return rewrittenContent; | ||||||
|  |  | ||||||
|     return { |  | ||||||
|         noteId: note.noteId |  | ||||||
|     }; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| async function createImage(req) { | async function createImage(req) { | ||||||
|     let {dataUrl, title, sourceUrl, pageUrl} = req.body; |     let {dataUrl, title, imageUrl, pageUrl} = req.body; | ||||||
|  |  | ||||||
|     if (!dataUrl) { |     if (!dataUrl) { | ||||||
|         dataUrl = sourceUrl; |         // this is image inlined into the src attribute so there isn't any source image URL | ||||||
|         sourceUrl = null; |         dataUrl = imageUrl; | ||||||
|  |         imageUrl = null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (!dataUrl.startsWith("data:image/")) { |     if (!dataUrl.startsWith("data:image/")) { | ||||||
| @@ -69,8 +114,8 @@ async function createImage(req) { | |||||||
|         return [400, message]; |         return [400, message]; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (!title && sourceUrl) { |     if (!title && imageUrl) { | ||||||
|         title = path.basename(sourceUrl); |         title = path.basename(imageUrl); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (!title) { |     if (!title) { | ||||||
| @@ -83,8 +128,10 @@ async function createImage(req) { | |||||||
|  |  | ||||||
|     const {note} = await imageService.saveImage(buffer, title, todayNote.noteId, true); |     const {note} = await imageService.saveImage(buffer, title, todayNote.noteId, true); | ||||||
|  |  | ||||||
|     if (sourceUrl) { |     await note.setLabel('clipType', 'image'); | ||||||
|         await note.setLabel('sourceUrl', sourceUrl); |  | ||||||
|  |     if (imageUrl) { | ||||||
|  |         await note.setLabel('imageUrl', imageUrl); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (pageUrl) { |     if (pageUrl) { | ||||||
| @@ -105,15 +152,17 @@ async function openNote(req) { | |||||||
|     return {}; |     return {}; | ||||||
| } | } | ||||||
|  |  | ||||||
| async function ping(req, res) { | async function handshake() { | ||||||
|     console.log("PING!!!!"); |     return { | ||||||
|  |         appName: "trilium", | ||||||
|     res.status(200).send("TriliumClipperServer"); |         appVersion: appInfo.appVersion | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|     createNote, |     createNote, | ||||||
|     createImage, |     createImage, | ||||||
|  |     addClipping, | ||||||
|     openNote, |     openNote, | ||||||
|     ping |     handshake | ||||||
| }; | }; | ||||||
| @@ -225,10 +225,11 @@ function register(app) { | |||||||
|     apiRoute(POST, '/api/login/protected', loginApiRoute.loginToProtectedSession); |     apiRoute(POST, '/api/login/protected', loginApiRoute.loginToProtectedSession); | ||||||
|     route(POST, '/api/login/token', [], loginApiRoute.token, apiResultHandler); |     route(POST, '/api/login/token', [], loginApiRoute.token, apiResultHandler); | ||||||
|  |  | ||||||
|  |     route(GET, '/api/clipper/handshake', [], clipperRoute.handshake, apiResultHandler); | ||||||
|  |     route(POST, '/api/clipper/clippings', [], clipperRoute.addClipping, apiResultHandler); | ||||||
|     route(POST, '/api/clipper/notes', [], clipperRoute.createNote, apiResultHandler); |     route(POST, '/api/clipper/notes', [], clipperRoute.createNote, apiResultHandler); | ||||||
|     route(POST, '/api/clipper/image', [], clipperRoute.createImage, apiResultHandler); |     route(POST, '/api/clipper/image', [], clipperRoute.createImage, apiResultHandler); | ||||||
|     route(POST, '/api/clipper/open/:noteId', [], clipperRoute.openNote, apiResultHandler); |     route(POST, '/api/clipper/open/:noteId', [], clipperRoute.openNote, apiResultHandler); | ||||||
|     route(GET, '/api/clipper/ping', [], clipperRoute.ping); |  | ||||||
|  |  | ||||||
|     app.use('', router); |     app.use('', router); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -6,7 +6,6 @@ const {TRILIUM_DATA_DIR} = require('./data_dir'); | |||||||
|  |  | ||||||
| const APP_DB_VERSION = 136; | const APP_DB_VERSION = 136; | ||||||
| const SYNC_VERSION = 9; | const SYNC_VERSION = 9; | ||||||
| const CLIPPER_VERSION = 1; |  | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|     appVersion: packageJson.version, |     appVersion: packageJson.version, | ||||||
|   | |||||||
| @@ -29,6 +29,7 @@ async function getNoteStartingWith(parentNoteId, startsWith) { | |||||||
|                                     AND branches.isDeleted = 0`, [parentNoteId]); |                                     AND branches.isDeleted = 0`, [parentNoteId]); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | /** @return {Promise<Note>} */ | ||||||
| async function getRootCalendarNote() { | async function getRootCalendarNote() { | ||||||
|     // some caching here could be useful (e.g. in CLS) |     // some caching here could be useful (e.g. in CLS) | ||||||
|     let rootNote = await attributeService.getNoteWithLabel(CALENDAR_ROOT_LABEL); |     let rootNote = await attributeService.getNoteWithLabel(CALENDAR_ROOT_LABEL); | ||||||
| @@ -47,6 +48,7 @@ async function getRootCalendarNote() { | |||||||
|     return rootNote; |     return rootNote; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | /** @return {Promise<Note>} */ | ||||||
| async function getYearNote(dateStr, rootNote) { | async function getYearNote(dateStr, rootNote) { | ||||||
|     if (!rootNote) { |     if (!rootNote) { | ||||||
|         rootNote = await getRootCalendarNote(); |         rootNote = await getRootCalendarNote(); | ||||||
| @@ -79,6 +81,7 @@ async function getMonthNoteTitle(rootNote, monthNumber, dateObj) { | |||||||
|         .replace(/{month}/g, monthName); |         .replace(/{month}/g, monthName); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | /** @return {Promise<Note>} */ | ||||||
| async function getMonthNote(dateStr, rootNote) { | async function getMonthNote(dateStr, rootNote) { | ||||||
|     const monthStr = dateStr.substr(0, 7); |     const monthStr = dateStr.substr(0, 7); | ||||||
|     const monthNumber = dateStr.substr(5, 2); |     const monthNumber = dateStr.substr(5, 2); | ||||||
| @@ -116,6 +119,7 @@ async function getDateNoteTitle(rootNote, dayNumber, dateObj) { | |||||||
|         .replace(/{weekDay2}/g, weekDay.substr(0, 2)); |         .replace(/{weekDay2}/g, weekDay.substr(0, 2)); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | /** @return {Promise<Note>} */ | ||||||
| async function getDateNote(dateStr) { | async function getDateNote(dateStr) { | ||||||
|     const rootNote = await getRootCalendarNote(); |     const rootNote = await getRootCalendarNote(); | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user