Action

JS Snippets

Posted by RoyRogers, Last update over 4 years ago

Inserts code from a draft with snippets.
- each snippet is headed by a title line and a comments line.
- both preceeded by ‘// ’ comment tag.
- Don’t include other comment lines inside the snippets!

Steps

  • script

    // JS Snippets
    // RV 2020-03-08 at 12:25 EST
    
    // Inserts code from a draft with snippets.
    // each snippet is headed by a title line and a comments line.
    // both preceeded by '// ' comment tag.
    // Don't include other comment lines inside the snippets!
    
    const uuid = '00DDC27C-9A81-4C28-BAAD-F2DF041E0A82';
    
    main: {
      try {
        var text = Draft.find(uuid).content
      } catch (e) {
        alert(`
        Wrong UUID!
        Insert UUID of your snippet 
        draft at line 9 of this script!`);
        // context.fail('Wrong UUID!');
        makeSnippetDraft();
        break main;
      }
      
      let snippets = text.split('\n\n// ');
      let titles = [];
      let snipDict = {};
      
      for (const item of snippets.slice(1)) { 
        // Skip draft title lines
        // make title array and snippet dictionary:
        let snip = item.split('\n');
        let title = snip[0];
        let body = snip.slice(1).join('\n');
        titles.push(title);
        snipDict[title] = body;
      }
    
      let p = Prompt.create();
      p.title = "Snippets Collection";
      p.message = "Insert JS snippet in code:";
      p.addSwitch("sw", "Include title", false);
      
      for (const button of titles) {
        p.addButton(button);
      }
    
      if (p.show()) {
        let btn = p.buttonPressed;
        let snippet = '';
        if (p.fieldValues['sw']) {
          snippet = `// ${btn}\n${snipDict[btn]}\n\n`;
        } else {
          snippet = `${snipDict[btn]}\n\n`;
        }
        // alert(snippet);
        editor.setSelectedText(snippet);
      } else {
        context.cancel();
      }
    }
    
    function makeSnippetDraft() {
      var d = Draft.create();
      const now = new Date();
      const dt = now.toString("yyyy-MM-dd, HH:mm");
      d.content = `// Snippet Collection
    // ${dt}
    // To be used with action: 'JS Snippets' \n` 
      + snippetSamples();
      d.languageGrammar = "JavaScript";
      d.update();
      d.addTag("script");
      d.update();
      app.setClipboard(d.uuid)
      alert(`New sample draft created UUID:
    ${d.uuid} 
    UUID aready in Clipboard :)
    Paste in line 9 of this script!`);
      // editor.load(d);
    }
    
    function snippetSamples() {
      let samples = `
    // for of loop
    // Makes a markdown numbered list:
    let myList = ['line 1', 'line 2', 'line 3'];
    let newList = [];
    i = 1
    for (const item of myList) {
      newList.push(i + '. ' + item)
      i += 1;
    }
    alert(newList.join('\\n'))
    
    // for i++ loop
    let text = ''
    for (i = 0; i < 5; i++) {
      text += 'Test ' + i + '\\n';
    }
    alert(text);
    
    // Prompt template
    // Delete or replace all vars, labels, etc to suit your needs:
    let p = Prompt.create();
    p.title = "title";
    p.message = "message";
    p.addTextField("field", "Label", "");
    p.addTextView("text", "Label", "", {"height":60,"keyboard":"default"});
    p.addSwitch("sw", "Label", false);
    p.addDatePicker("dt", "Label", new Date(), {"mode":"dateAndTime"});
    p.addPicker('pick', 'Label', [['pick1', 'pick2']], [0]);
    p.addSelect('select', 'Label', ['sel1', 'sel2'], ['sel2'], false);
    
    let buttons = ["btn1", "btn2"];
    for (var button of buttons) {
      p.addButton(button);
    }
    
    if (p.show()) {
      var field = p.fieldValues['field'];
      var myText = p.fieldValues['text'];
      var sw = p.fieldValues['sw'];
      var dt = p.fieldValues['dt'];
      var listIndex = p.fieldValues['pick']; // integer
      var selected = p.fieldValues['select'];
      
      switch (p.buttonPressed) {
        case 'btn1':
          
          break;
        case 'btn2':
          
          break;
        default:
          app.displayErrorMessage("switch/case not implemented!");
          break;
      }
    } else {
      context.cancel();
    }
    
    `;
      return samples;
    }
    
    

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.