|
| 1 | +import QtQml 2.0 |
| 2 | +import QOwnNotesTypes 1.0 |
| 3 | + |
| 4 | +/** |
| 5 | + * This script creates some easy to access commands that leverage the autocomplete functionalities to add more predefined |
| 6 | + * strings or formatted dates |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +Script { |
| 11 | + // DateFormats |
| 12 | + readonly property string _YYYYMMDD : "YYYY-MM-DD"; |
| 13 | + readonly property string _DDMMYYYY : "DD-MM-YYYY"; |
| 14 | + readonly property string _DDMM : "DD-MM"; |
| 15 | + readonly property string _WWYYYY : "WW-YYYY"; |
| 16 | + readonly property string _WEEKWWYYYY : "wWW-YYYY"; |
| 17 | + readonly property string _FULL : "YYYY-MM-DDTHH:mm:ss"; |
| 18 | + |
| 19 | + readonly property int _MILLI_DAY : 86400000; |
| 20 | + property var commands; |
| 21 | + property string customCommands; |
| 22 | + |
| 23 | + property variant settingsVariables: [ |
| 24 | + { |
| 25 | + "identifier": "customCommands", |
| 26 | + "name": "Custom Commands", |
| 27 | + "description": "Custom quick commands. Each line is a separate command, with the options split by space and the first word being the command name. For example: 'myName first first-last last-first'", |
| 28 | + "type": "text", |
| 29 | + "default": "", |
| 30 | + } |
| 31 | + ] |
| 32 | + |
| 33 | + function init() { |
| 34 | + commands = new Object(); |
| 35 | + reInitCommands(); |
| 36 | + } |
| 37 | + |
| 38 | + function buildTimeList(date) { |
| 39 | + var timeList = []; |
| 40 | + timeList.push(formatDate(date, _DDMM)); |
| 41 | + timeList.push(formatDate(date, _YYYYMMDD)); |
| 42 | + timeList.push(formatDate(date, _DDMMYYYY)); |
| 43 | + timeList.push(formatDate(date, _FULL)); |
| 44 | + return timeList; |
| 45 | + } |
| 46 | + |
| 47 | + function reInitCommands(){ |
| 48 | + var today = new Date(); |
| 49 | + var todayMillis = today.getTime(); |
| 50 | + var yesterday = new Date(todayMillis - _MILLI_DAY); |
| 51 | + var tomorrow = new Date(todayMillis + _MILLI_DAY); |
| 52 | + |
| 53 | + commands["today"] = buildTimeList(today); |
| 54 | + commands["tomorrow"] = buildTimeList(tomorrow); |
| 55 | + commands["yesterday"] = buildTimeList(yesterday); |
| 56 | + commands["week"] = [formatDate(today, _WWYYYY), formatDate(today, _WEEKWWYYYY)]; |
| 57 | + commands["now"] = [formatDate(today, _FULL)]; |
| 58 | + |
| 59 | + |
| 60 | + var customRows = customCommands.split("\n"); |
| 61 | + for (let i = 0; i < customRows.length; i++) { |
| 62 | + var customCommandDetails = customRows[i].split(" "); |
| 63 | + var customCommandName = customCommandDetails[0]; |
| 64 | + var customCommandValues = []; |
| 65 | + for (let j = 1; j < customCommandDetails.length; j++) { |
| 66 | + customCommandValues.push(customCommandDetails[j]); |
| 67 | + } |
| 68 | + |
| 69 | + commands[customCommandName] = customCommandValues; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + function autocompletionHook() { |
| 74 | + var word = script.noteTextEditCurrentWord(true); |
| 75 | + |
| 76 | + if (!word.startsWith("\\")) { |
| 77 | + return []; |
| 78 | + } |
| 79 | + |
| 80 | + // Have to re-init since reloading the script engine/restarting might not happen daily |
| 81 | + reInitCommands(); |
| 82 | + |
| 83 | + var command = word.substr(1); |
| 84 | + |
| 85 | + var availableCommands = commands[command]; |
| 86 | + if (availableCommands == null){ |
| 87 | + return []; |
| 88 | + } |
| 89 | + |
| 90 | + return availableCommands; |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + // Taken from https://github.com/qownnotes/scripts/blob/master/journal-entry/journal-entry.qml |
| 95 | + function getWeekNumber(d) { |
| 96 | + // Copy date so don't modify original |
| 97 | + d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate())); |
| 98 | + d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay()||7)); |
| 99 | + var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1)); |
| 100 | + var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7); |
| 101 | + return weekNo; |
| 102 | + } |
| 103 | + |
| 104 | + // Taken from https://github.com/qownnotes/scripts/blob/master/journal-entry/journal-entry.qml |
| 105 | + function formatDate(date, format) { |
| 106 | + let day = date.getDate(); |
| 107 | + let month = date.getMonth() + 1; //getMonth() returns 0-11 so we must add 1 |
| 108 | + let week = getWeekNumber(date); |
| 109 | + let year = date.getFullYear(); |
| 110 | + let hours = date.getHours(); |
| 111 | + let minutes = date.getMinutes(); |
| 112 | + let seconds = date.getSeconds(); |
| 113 | + |
| 114 | + // If day and month are less than 10, add a leading zero |
| 115 | + day = (day < 10) ? '0' + day : day; |
| 116 | + month = (month < 10) ? '0' + month : month; |
| 117 | + week = (week < 10) ? '0' + week : week; |
| 118 | + hours = (hours < 10) ? '0' + hours : hours; |
| 119 | + minutes = (minutes < 10) ? '0' + minutes : minutes; |
| 120 | + seconds = (seconds < 10) ? '0' + seconds : seconds; |
| 121 | + |
| 122 | + // Replace format placeholders by actual values |
| 123 | + format = format.replace('WW', week); |
| 124 | + format = format.replace('MM', month); |
| 125 | + format = format.replace('DD', day); |
| 126 | + format = format.replace('YYYY', year); |
| 127 | + format = format.replace('HH', hours); |
| 128 | + format = format.replace('mm', minutes); |
| 129 | + format = format.replace('ss', seconds); |
| 130 | + |
| 131 | + return format; |
| 132 | + } |
| 133 | +} |
0 commit comments