diff --git a/README.md b/README.md index 228ed0f..ca6b1b0 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,19 @@ Plugin to combine one or more notes to a new one. - Select multiple notes to be combined into a new one - Click on `Tools > Combine selected notes` or use the command `Combine selected notes` from the context menu +## Options + +Go to `Tools > Options > Combine notes` + +- `Create combined note as to-do`: New note is created as To-Do. Default `false` +- `Delete combined notes`: Delete combined notes, after note creation. Default `false` +- `Preserve Source URL`: The source URL will be added to the new note. Default `false` +- `Preserve Created Date`: The Created Date will be added to the new note. Default `false` +- `Preserve Updated Date`: The Updated Date will be added to the new note. Default `false` +- `Preserve Location`: The Location (Latitude, Longitude, Altitude) will be added to the new note. Default `false` +- `Metadata Prefix`: The entered text is output before the metadata (URL, Date, Location). +- `Metadata Suffix`: The entered text is output after the metadata (URL, Date, Location). + ## Keyboard Shortcus Under `Options > Keyboard Shortcus` you can assign a keyboard shortcut for the following commands: @@ -41,6 +54,12 @@ To update the plugin framework, run `npm run update`. ## Changelog +### v0.2.0 (2021-01-09) + +- Option added to create the combined note as a to-do +- Option added to preserve metadata (Source URL, Creation Date, Updated Date, Location) +- Option added to delete combined notes + ### v0.1.0 (2021-01-08) - First version diff --git a/package-lock.json b/package-lock.json index 526c44d..b3123f7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "joplin-plugin-combine", - "version": "1.0.0", + "version": "0.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index f5db4df..0720000 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "joplin-plugin-combine-notes", - "version": "0.0.1", + "version": "0.2.0", "scripts": { "dist": "webpack", "prepare": "npm run dist", diff --git a/src/index.ts b/src/index.ts index 982a130..1da50a2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,10 +1,89 @@ import joplin from "api"; -import { MenuItemLocation } from "api/types"; +import { MenuItemLocation, SettingItemType } from "api/types"; joplin.plugins.register({ onStart: async function () { console.info("Combine plugin started"); + await joplin.settings.registerSection("combineNoteSection", { + label: "Combine notes", + iconName: "fas fa-layer-group", + }); + + await joplin.settings.registerSetting("asToDo", { + value: false, + type: SettingItemType.Bool, + section: "combineNoteSection", + public: true, + label: "Create combined note as to-do", + }); + + await joplin.settings.registerSetting("deleteCombinedNotes", { + value: false, + type: SettingItemType.Bool, + section: "combineNoteSection", + public: true, + label: "Delete combined notes", + }); + + await joplin.settings.registerSetting("preserveMetadataSourceUrl", { + value: false, + type: SettingItemType.Bool, + section: "combineNoteSection", + public: true, + label: "Preserve Source URL", + description: + "Preserve the Source by inserting them under the header from the note.", + }); + + await joplin.settings.registerSetting("preserveMetadataCreatedDate", { + value: false, + type: SettingItemType.Bool, + section: "combineNoteSection", + public: true, + label: "Preserve Created Date", + description: + "Preserve the Created Date by inserting them under the header from the note.", + }); + + await joplin.settings.registerSetting("preserveMetadataUpdatedDate", { + value: false, + type: SettingItemType.Bool, + section: "combineNoteSection", + public: true, + label: "Preserve Updated Date", + description: + "Preserve the Updated Date by inserting them under the header from the note.", + }); + + await joplin.settings.registerSetting("preserveMetadataLocation", { + value: false, + type: SettingItemType.Bool, + section: "combineNoteSection", + public: true, + label: "Preserve Location", + description: + "Preserve the Location Date by inserting them under the header from the note.", + }); + + await joplin.settings.registerSetting("preserveMetadataPrefix", { + value: "", + type: SettingItemType.String, + section: "combineNoteSection", + public: true, + label: "Metadata Prefix", + description: "Prefix for the Metadata section.", + }); + + await joplin.settings.registerSetting("preserveMetadataSuffix", { + value: "", + type: SettingItemType.String, + section: "combineNoteSection", + public: true, + label: "Metadata Suffix", + description: "Suffix for the Metadata section.", + }); + await joplin.commands.register({ name: "CombineNotes", label: "Combine selected notes", @@ -14,13 +93,93 @@ joplin.plugins.register({ const newNoteBody = []; let notebookId = null; const newTags = []; + let createdDate = null; + let updatedDate = null; + let preserveMetadata = []; + const preserveUrl = await joplin.settings.value( + "preserveMetadataSourceUrl" + ); + const preserveCreatedDate = await joplin.settings.value( + "preserveMetadataCreatedDate" + ); + const preserveUpdatedDate = await joplin.settings.value( + "preserveMetadataUpdatedDate" + ); + const preserveMetadataLocation = await joplin.settings.value( + "preserveMetadataLocation" + ); + const preserveMetadataPrefix = await joplin.settings.value( + "preserveMetadataPrefix" + ); + const preserveMetadataSuffix = await joplin.settings.value( + "preserveMetadataSuffix" + ); // collect note data for (const noteId of ids) { + preserveMetadata = []; const note = await joplin.data.get(["notes", noteId], { - fields: ["title", "body", "parent_id"], + fields: [ + "title", + "body", + "parent_id", + "source_url", + "created_time", + "updated_time", + "latitude", + "longitude", + "altitude", + ], }); - newNoteBody.push(["# " + note.title, "", note.body].join("\n")); + newNoteBody.push("# " + note.title + "\n"); + + // Preserve metadata + if (preserveUrl === true && note.source_url != "") { + preserveMetadata.push("[Source](" + note.source_url + ")"); + } + + if (preserveCreatedDate === true) { + createdDate = new Date(note.created_time); + preserveMetadata.push("Created: " + createdDate + ")"); + } + + if (preserveUpdatedDate === true) { + updatedDate = new Date(note.updated_time); + preserveMetadata.push("Updated: " + updatedDate + ")"); + } + + if ( + preserveMetadataLocation === true && + (note.latitude != "0.00000000" || + note.longitude != "0.00000000" || + note.altitude != "0.0000") + ) { + preserveMetadata.push( + [ + "Location:", + "Lat:", + note.latitude, + "Lon:", + note.longitude, + "Altitude:", + note.altitude, + ].join(" ") + ); + } + + if (preserveMetadata.length > 0) { + if (preserveMetadataPrefix != "") { + newNoteBody.push(preserveMetadataPrefix + "\n"); + } + + newNoteBody.push(preserveMetadata.join("\n") + "\n"); + + if (preserveMetadataSuffix != "") { + newNoteBody.push(preserveMetadataSuffix + "\n"); + } + } + + newNoteBody.push(note.body + "\n"); let pageNum = 0; do { @@ -30,33 +189,44 @@ joplin.plugins.register({ page: pageNum++, }); for (const tag of noteTags.items) { - if(newTags.indexOf(tag.id) === -1) { - newTags.push(tag.id) - } + if (newTags.indexOf(tag.id) === -1) { + newTags.push(tag.id); + } } } while (noteTags.has_more); if (!notebookId) notebookId = note.parent_id; } + const asToDo = await joplin.settings.value("asToDo"); + // create new note const newNoteData = { title: "Combined note", - body: newNoteBody.join("\n\n"), + body: newNoteBody.join("\n"), parent_id: notebookId, + is_todo: asToDo, }; const newNote = await joplin.data.post(["notes"], null, newNoteData); // Add Tags for (const tag of newTags) { - await joplin.data.post( - ["tags", tag, "notes"], - null, - { id: newNote.id } - ); + await joplin.data.post(["tags", tag, "notes"], null, { + id: newNote.id, + }); + } + + // delete combined notes + const deleteNotes = await joplin.settings.value( + "deleteCombinedNotes" + ); + if (deleteNotes === true) { + for (const noteId of ids) { + await joplin.data.delete(["notes", noteId]); + } } - await joplin.commands.execute('openNote', newNote.id); + await joplin.commands.execute("openNote", newNote.id); } }, }); @@ -67,8 +237,8 @@ joplin.plugins.register({ MenuItemLocation.Tools ); await joplin.views.menuItems.create( - 'contextMenuItemconcatCombineNotes', - 'CombineNotes', + "contextMenuItemconcatCombineNotes", + "CombineNotes", MenuItemLocation.NoteListContextMenu ); }, diff --git a/src/manifest.json b/src/manifest.json index 5ce8361..712411c 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 1, "id": "io.github.jackgruber.combine-notes", "app_min_version": "1.6", - "version": "1.0.0", + "version": "0.2.0", "name": "combine", "description": "Combine one or more notes", "author": "JackGruber",