Last active
March 21, 2020 16:01
-
-
Save zentrope/d0457ed96d06e554c9cc47d0ac069944 to your computer and use it in GitHub Desktop.
Drafts to OO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let blurb = { "text": "", "date": "", "link": "", "title": "" } | |
const isValidDate = (d) => d instanceof Date && !isNaN(d) | |
draft.content.split("\n").forEach((text) => { | |
const line = text.trim() | |
if (line.startsWith("text:")) { | |
blurb.text = line.replace("text:", "").trim() | |
if (blurb.text.trim() == "") { | |
blurb.text = undefined; | |
} | |
} else if (line.startsWith("date:")) { | |
blurb.date = line.replace("date:", "").trim() | |
let raw = blurb.date + 'T00:00:00-08:00' | |
blurb.date = isValidDate(new Date(raw)) ? raw : undefined | |
} else if (line.startsWith("link:")) { | |
blurb.link = line.replace("link:", "").trim() | |
} else if (line.startsWith("title:")) { | |
blurb.title = line.replace("title:", "").trim() | |
} | |
}) | |
function updateRow(blurb) { | |
try { | |
const dateCol = document.outline.columns.byTitle("Date") | |
const topicCol = document.outline.columns.byTitle("Topic") | |
const newItem = rootItem.addChild(null, function (item) { | |
if (blurb.date) { item.setValueForColumn(new Date(blurb.date), dateCol) } | |
item.setValueForColumn(blurb.title, topicCol) | |
if (blurb.text) { | |
item.addChild(null, function (quote) { quote.topic = blurb.text }) | |
} | |
}) | |
editor = document.editors[0] | |
node = editor.nodeForItem(newItem) | |
textObj = node.valueForColumn(topicCol) | |
textObj.styleForRange(textObj.range).set(Style.Attribute.Link, URL.fromString(blurb.link)) | |
} catch (error) { | |
console.log(error.message) | |
} | |
} | |
let code = "(" + updateRow + ")" + "(" + JSON.stringify(blurb) + ")" | |
let callback = CallbackURL.create() | |
callback.baseURL = 'omnioutliner://localhost/omnijs-run' | |
callback.addParameter("script", code); | |
let success = callback.open(); | |
if (success) { | |
draft.addTag("completed") | |
} else { | |
draft.addTag("failed") | |
} | |
draft.update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment