Action

Toggle bullet

Posted by @jsamlarose, Last update over 1 year ago

Toggles through bullet points

  • + ↳ >

To render “↳” as a list bullet correctly, you’ll need an appropriately equipped syntax (https://actions.getdrafts.com/s/1xC)

Steps

  • script

    // See online documentation for examples
    // https://docs.getdrafts.com/docs/actions/scripting
    
    let [loc,lth] = editor.getSelectedLineRange()
    var lineText = editor.getTextInRange(loc,lth)
    var tab = ""
    var bulletRegex = /^\t*[-+>↳] /gm;
    var bullets = ["+", "↳", ">", "-"]
    var newText = ""
    //if (lineText.includes("- [ ]")){lth = lth-1}
    
    if (lineText.length <= 1) {
    
    	newText = "+ \n"
    
    } else if (/^\t*#* /.test(lineText)) {
     	newText = "\t" + "+ \n"
     	loc = loc+lth
     	lth = 0
      
    } else {
    
    
    	lineText = lineText.split("\n").filter((a) => a) // the filter prevents empty elements... 
    
    	for (let line of lineText){
        
    		var output = bullets.indexOf(line.trim().charAt(0)) + 1
    		if (line.startsWith("\t")){tab = "\t".repeat(((line.match(/\t/g) || []).length));}
    		if (output > bullets.length-1){output=0}
    
    		if (line.trim().startsWith("- [ ]")){
    			newText += line.replace("- [ ] ","+ ") + "\n"
    		} else if (!line.trim().startsWith("-")&&!line.trim().startsWith("+")&&!line.trim().startsWith(">")&&!line.trim().startsWith("↳")){
    			newText += tab + "+ " + line.trim() + "\n"
    		} else {
    			newText += tab+line.replace(bulletRegex,bullets[output]+" ") + "\n"
    		}
    	
    	}
    }
    
    
    lineText = newText // .slice(0, -1)
    editor.setTextInRange(loc,lth,lineText)
    lth = lineText.length
    if (((lineText.match(/\n/g) || []).length)>=2) {
    	editor.setSelectedRange(loc,lth)
    } else {
    	editor.setSelectedRange(loc+lth-1,0)
    }
    draft.update()

Options

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