Action

Split Draft

Posted by RoyRogers, Last update over 4 years ago

Splits a draft at cursor position
- Both parts to in Inbox.
- Sets Workspace to ‘Inbox’ if that exists.
- Option to split to two new drafts and leave original untouched (change: ‘isTwoNewDrafts = true` in script).

Steps

  • script

    // Split at Cursor v.1.1
    // RV 2020-03-16 at 15:54
    
    // Splitting a draft at cursor position
    
    'use strict';
    
    // Set to 'true' to leave original unchanged,
    // and split to two new drafts: 
    const isTwoNewDrafts = false;
    
    (function main() {
      let sel = editor.getSelectedRange();
      if (sel[1] != 0) {
        app.displayInfoMessage('Do not use with selection!');    
        context.cancel('canceled by user.');
        return;
      }
      
      let len = draft.content.length;
      let pos = sel[0];
      let firstPart = editor.getTextInRange(0, pos);
      let lastPart = editor.getTextInRange(pos, len);
      
      newDraft(lastPart);
      editor.setSelectedRange(0, 0);
      
      if (isTwoNewDrafts) {
        // Leave original unchanged and make two new parts:
        newDraft(firstPart, false);
      } else {
    	  halfDraft(firstPart);
      }
    
      
      let workspace = Workspace.find("Inbox");
      if (workspace) {
        app.applyWorkspace(workspace);
      }
    
      app.displayInfoMessage('Draft is split in two parts!');  
    })();
    
    function halfDraft(text) {
      draft.saveVersion();
      draft.content = text;
      draft.addTag('split');
      draft.isArchived = false;
      draft.update();
    }
    
    function newDraft(text, isLoad=true) {
      let d = Draft.create();
      d.content = text;
      d.languageGrammar = draft.languageGrammar;
      d.update();
      for (const tag of draft.tags) {
        d.addTag(tag);
      }
    
      d.isFlagged = draft.isFlagged;
      d.addTag('split');
      d.update();
      if (isLoad) editor.load(d);
    }
    
    

Options

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