Action

Process draft as menu

Posted by @scripts4drafts, Last update almost 2 years ago

This action selects a draft and prompts its content as a menu. The selected item in the menu is then appended to the current line.

The selected draft is:
- the first draft in a drafts query, which title equals the current line, or, if there’s none,
- the draft selected in a menu prompting all the drafts which title contains the current line.

When the draft is picked, its content is parsed and all the lines beginning with ‘- ’ will be processed as items in the menu to be prompted.

Variables:
- JOIN is used to separate the current line and the selected item in the menu (default is ‘: ’ ; use ‘\n’ to insert a line separation).
- FOLDERFILTER is used to filter the drafts query (default is ‘all’ ; or use ‘inbox’, ‘archive’ or ‘flagged’).
- TAGFILTER is used to filter the queried drafts with a tag or several tags (default is “, ie no tag filter).

Steps

  • script

    const JOIN = ': ',
      TAGFILTER = '',
      FOLDERFILTER = 'all';
    
    let f = () => {
      let [loc, len] = editor.getSelectedLineRange(),
        title = editor.getTextInRange(loc, len).trim().split('\n').shift().trim();
      len = title.length;
      if (len < 3) {
        alert('Title is too short!');
        return;
      }
      let ws = Workspace.create();
      ws.tagFilter = TAGFILTER;
      ws.tagFilterRequireAll = true;
      ws.queryString = `title:"${title}"`;
      let drafts = ws.query(FOLDERFILTER),
        draftsLen = drafts.length, thisDraft;
      if (draftsLen > 0) {
        for (let d of drafts) {
          if (d.title.trim() == title) {
            thisDraft = d;
            break;
          }
        }
        if (!thisDraft) {
          let p = Prompt.create();
          p.title = 'Draft: ' + title;
          let draftsList = drafts.sort(function(a, b) {return a.title.localeCompare(b.title, 'en', {ignorePunctuation: true})}).map(d => d.title);
          p.addSelect('drafts', '', draftsList, [draftsList[0]]);
          p.addButton('OKAY');
          thisDraft = !p.show() ? false : drafts.filter(d => d.title == p.fieldValues['drafts'][0])[0];
        }
      }
      if (!thisDraft) {
        alert('Found no draft!');
        return;
      }
      title = thisDraft.title.trim();
      let lines = thisDraft.content.split('\n').filter(l => l.startsWith('- ')).map(l => l.replace(/^- /, '').trim());
        p = Prompt.create();
      p.title = title;
      p.addSelect('lines', '', lines, [lines[0]]);
      p.addButton('OKAY');
      if (!p.show()) {
        return;
      }
      let str = title + JOIN + p.fieldValues['lines'][0];
      editor.setTextInRange(loc, len, str);
      editor.setSelectedRange(loc + str.length, 0);
    }
    f();
    editor.activate();

Options

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