Action

Insert Text Element - easier maintenance

Posted by davenicholls, Last update over 6 years ago

Based on @nahumck’s original, this version holds the replacement options in a JavaScript Object, which allows entries to be added/changed without the need to modify the main code.

This action allows prompts for a selection of text to insert. You can modify this to be a single insert or expand it to contain multiple prompts.

Steps

  • script

    //Insert Text Elements
    //Script by @nahumck
    // Modified by @davenicholls
    
    // Create an object containing all the text element choices
    // Format 'property':'value'
    // property is the text that will appear in the prompt
    // value is the value that will be returned
    //
    // Value can be text (enclosed in "s) or a javascript function name
    // Functions are used where the inserted value is to be calculated at runtime
    // 
    // Add extra items as required. Each item should have a comma on the end, apart from the last one (although it should work if the last one has a comma as well
     
    myOptions = {
    	'break':"<br>",
    	'divider':"----",
    	'script divider':"\/* --------------------- *\/",
    	'more':"<!--more-->!",
    	'website':"https://nahumck.me",
    	'current draft link':getDraftLink
    }
    
    // Function to calculate and return the correct URL
    // Functions need to return a string
    function getDraftLink() {
    
    	var dtext = draft.processTemplate("[[uuid]]");
    	return "drafts5://x-callback-url/open?uuid="+dtext;
    }
    
    var selRange = editor.getSelectedRange();
    
    var p = Prompt.create();
    p.title = "Text Element";
    p.message = "What do you want to insert?";
    
    // Iterate all properties in the myOptions object adding a button for each one
    for (const [key, value] of Object.entries(myOptions)) {
    	p.addButton(key)
    }
    
    var con = p.show();
    
    if (con) {
    
      var option = myOptions[p.buttonPressed]
    
    	// Set text to a blank string. Assumes that no entry in MyOptions will ever return like this
    	var text = ""
    	// we need to check the type of the value so we know how to handle it
    	switch (typeof(option)) {
    
    		// For strings just use the value as is
    		case 'string':
      			text=option;
      			break;
    
    		// Run the function and set text to the return value
      		case 'function':
      			text=option();
      			break;
    
    		// Used to catch any problems with myOptions
    		default:
    			console.log("Item error - "+ p.buttonPressed + "<->" + option + "<->" + typeof(option));
      			context.fail("Bad entry in myOptions");
    			break;
    		  		
    	}
      if (text!=="") {
    		editor.setSelectedText(text);
      		editor.setSelectedRange(selRange[0]+text.length,0);
      		editor.focus();
      }
     		
    }
    else {
      context.cancel();
    }
    
    
    
    

Options

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