Multiple Selection In A Draft

If I were to make eg a slide from some long form text I’d like to select multiple chunks of text in that draft to extract and work on - in one action.

How close to that can I get?

You could do that by marking up your selection blocks with markers and then repeating the processing steps in the action for each block. At the end you could optionally remove the block markers.

You could of course have a second action to convert a single cursor-based text selection to a marked up block.

Drafts does not currently support multiple cursors, so multiple cursor-based text selections are not an option.

1 Like

Thanks!

When you say “marker” and “marked up” do you mean something like backticks? Or is there a better alternative? (I could contemplate something like an HTML span element but that is cumbersome.)

Whatever you want to use to uniquely mark the start and end of a section. Just like in Markdown or an old school word processor.

I think :arrow_forward: and :arrow_backward: would be nice extraction marks.

It should be possible to inject them with a keystroke. Then, later, to process them into fragments, probably as bullets. Optionally removing the marks from the draft. The number of them in a row might denote indentation level - but now I’m getting fancy. :slight_smile:

One nice thing about markers is that - leaving them in the draft indefinitely - I could adjust the extraction.

So here’s a very simple prototype:

I’ve set Ctrl+x up to run the following javascript single-step action:

startMarker = "▶"
stopMarker = "◀"

editor.setSelectedText(startMarker + editor.getSelectedText() + stopMarker)

draft.update()

It puts triangle markers around selected text - and keeps it selected.

You run the above until you’ve marked up the bits you want to extract.

I’ve set Ctrl+Alt+x up to run this javascript single step action:

// Prefix for each output line
outputPrefix = "* "
// Prime output text
outputText = ""

// Create array where the split character is right-pointing black arrow
fragments = draft.content.split(/\u25B6/)
for(f = 1; f < fragments.length; f++){
  fragmentPlus = fragments[f]
  
  // Find left-pointing black arrow
  endOfFragment = fragmentPlus.search(/\u25C0/)
  
  // Extract fragment
  fragment = fragmentPlus.substr(0, endOfFragment)

  // Append to new fragment to output text
  outputText += outputPrefix + fragment + "\n"
}

// Set clipboard with resulting text - if any
if(outputText != ""){
  app.setClipboard(outputText)
}

It takes any previously marked up text fragments and turns them into Markdown bulleted list items, separated by newlines. If there are any the resulting string is copied to the clipboard. The original draft is left unchanged.

Note: While the marking up action can be repeated on the same piece of text to create multiple levels of marking up, I haven’t taught the extracting action to indent accordingly.

So, this pair of actions gets me as far along as I need to go. I suppose I need an action to clear the markers.

My actual use case is to write a long form blog post in drafts but extract fragments of it to create a slide (or several) from (using Markdown syntax).

2 Likes

Here’s a basic action (I’ve bound to Ctrl+c) to clear all the marks set by the first action above.

I’ve deliberately separated each replaceAll out - in case I need to augment their function.

Note: In the action to extract I didn’t find a way to reuse the markers from the first action. So anyone adapting these will have to research their own Unicode code points.

startMarker = "▶"
stopMarker = "◀"

content = draft.content.replaceAll(startMarker, "")
content = content.replaceAll(stopMarker, "")

draft.content = content

draft.update()