Action

Wrapper

Posted by @derekvan, Last update about 6 years ago

Presents menu for wrapping text in a variety of symbols, based on agile tortoise’s smart wrap actions (https://actions.getdrafts.com/a/1Bz)

Steps

  • script

    // based on greg pierce's original smart wrap action https://actions.getdrafts.com/a/1Bz
    
    var range = editor.getSelectedRange();
    var priorText = editor.getTextInRange(0, range[0]);
    
    var p = Prompt.create();
    
    p. title = "Wrap Text";
    p.message = "Choose symbol to wrap text";
    
    p.addButton("Parentheses");
    p.addButton("Braces");
    p.addButton("Double Quotes");
    p.addButton("Single Quotes");
    p.addButton("Square Brackets");
    p.addButton("Angle Brackets");
    
    p.show();
    
    if (p.buttonPressed == "Parentheses") {
      var open="(";
      var close=")";
      }
    else if (p.buttonPressed == "Braces") {
      var open = "{";
      var close = "}";
      }
    else if (p.buttonPressed == "Double Quotes") {
      var open = "\x22";
      var close = "\x22";
      }
    else if (p.buttonPressed == "Single Quotes") {
      var open = "\x27";
      var close = "\x27";
      }
    else if (p.buttonPressed == "Square Brackets") {
      var open = "[";
      var close = "]";
      }
    else if (p.buttonPressed == "Angle Brackets") {
      var open = "<";
      var close = ">";
    }
    
    if (range[1] > 0) {
     //text selected, wrap it!
     editor.setSelectedText(open + editor.getSelectedText() + close);
     editor.setSelectedRange(range[0] + range[1] + open.length + close.length, 0);
    }
    else {
     // no selection, find last
     var lastOpen = priorText.lastIndexOf(open);
     var lastClose = priorText.lastIndexOf(close);
    
     if	(lastOpen == -1 || lastClose > lastOpen) { // no close, use open
       editor.setSelectedText(open);
       editor.setSelectedRange(range[0]+open.length, 0);
     }
     else { // close last open
       editor.setSelectedText(close);
       editor.setSelectedRange(range[0]+close.length, 0);
     }
    }
    

Options

  • After Success Nothing
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.