Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%*
- const oldPlannerHeader = "## Day Planner"
- const newPlannerHeader = "# Day Planner"
- const file = tp.file.find_tfile("Day Planner-" + tp.date.now("YYYYMMDD"))
- var rxLogItem = /^([0-2][0-9]:[0-5][0-9] [a-zA-Z0-9]).*/
- var rxTaskItem = /^- \[[ |x]\] [0-2][0-9]:[0-5][0-9] [a-zA-Z0-9].*/
- var rxTime = /[0-2][0-9]:[0-5][0-9]/
- var insertPosition = -1
- var positionFound = -1
- var index = 0
- function isEarlier(t1, t2) {
- const r = t1 < t2 ? true : false
- return r
- }
- function isSame(t1, t2) {
- const r = t1.toString() == t2.toString() ? true : false;
- return r;
- }
- async function initFirstUse() {
- await app.vault.modify(file, newPlannerHeader)
- //await executeCommand("ICS: import events")
- }
- async function executeCommand(name) {
- // ICS plugin only works when file.name == YYYY-MM-DD
- // I will keep it for later
- const commandName = name;
- const commands = app.commands.listCommands();
- const collatorCompare = new Intl.Collator(["en"], {sensitivity: "base", usage: "search", ignorePunctuation: true}).compare;
- const command = commands.find(c => collatorCompare(c.name, commandName) === 0);
- await app.commands.executeCommandById(command.id);
- }
- if (file) {
- logItem = await tp.system.prompt("Event for Day Planner: (HH:mm event)")
- if (rxLogItem.test(logItem)) {
- const content = (await app.vault.read(file)).split("\n")
- const firstUse = content.indexOf(oldPlannerHeader)
- if (firstUse >= 0) {
- initFirstUse();
- index = 0
- } else {
- const index = await content.indexOf(newPlannerHeader)
- }
- // Find positionItem
- let timeLogItem = rxTime.exec(logItem)
- do {
- positionFound = -1;
- index++
- let row = content[index]
- if (rxTaskItem.test(row)) {
- let timeTask = rxTime.exec(row)
- if (isSame(timeLogItem, timeTask)) {
- new Notice("Entry already exists.")
- break;
- } else {
- if (isEarlier(timeLogItem, timeTask)) {
- positionFound = index
- }
- }
- } else {
- positionFound = index
- break;
- }
- }
- while (positionFound < 0)
- console.log('positionFound: ' + positionFound)
- if (positionFound > 0) {
- // insert entry
- let addContent = "- [ ] " + logItem
- content.splice(positionFound, 0 , addContent)
- await app.vault.modify(file, content.join("\n"))
- console.log('Task ' + logItem + ' added ' + ' in file ' + file + ' at position ' + positionFound)
- }
- } else {
- new Notice("Wrong task format (HH:MM event)")
- }
- } else {
- new Notice("No Day Planner note found.")
- }
- -%>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement