Action

Enter Anywhere

Posted by Zrapata, Last update 11 days ago

UPDATES

11 days ago

Typo corrections

show all updates...

11 days ago

Typo corrections

11 days ago

Removed action used for testing

An action that crates a new Line even if you are in the middle of a senates.
Same with ordered and undordered lists

Steps

  • script

    // See online documentation for examples
    // https://docs.getdrafts.com/docs/actions/scripting
    
    var tab = editor.preferredTabString
    var sel = editor.getSelectedRange()
    
    const selection = editor.getLineRange(sel[0] + sel[1], 0)
    var selectedText = editor.getTextInRange(selection[0], selection[1])
    var selectionOffset = [0, 0]
    
    var matchList = /(^[\t ]*)((\d*)\.|([-*+])) (.*)/g
    var isList = matchList.exec(selectedText)
    
    if (isList == null) {
    	drawNewLine(selectedText, selection[0] + selection[1])
    } else {
    	//alert(isList[0]+','+ isList[1]+','+ isList[2]+','+ isList[3]+','+ isList[4]+','+ isList[5])
    
    	var listText
    
    	if (isList[2].match(RegExp('[-*+]', 'g')) == null) {
    		listText = checkOrderedList()
    	} else {
    		listText = checkList()
    	}
    	drawNewLine(listText[0], selection[0] + selection[1], 0, listText[1])
    }
    
    function checkOrderedList() {
    	var previousLine = isList[0]
    	var number = parseInt(isList[3]) + 1
    	var textAfter = '\n' + isList[1] + number + '. '
    
    	if (isList[5].trim() == '') {
    		textAfter = isList[1].replace(tab, '')
    		if (textAfter == isList[1]) {
    			previousLine = ''
    			textAfter = ''
    			selectionOffset[0] -= isList[0].length
    		} else {
    			previousLine = textAfter + (number - 1) + '. '
    			textAfter = '¨d¨'
    			selectionOffset[0] = -tab.length - 1
    		}
    	}
    
    	return [previousLine, textAfter]
    }
    
    function checkList() {
    	var previousLine = isList[0]
    	var textAfter = '\n' + isList[1] + isList[2] + ' '
    
    	if (isList[5].trim() == '') {
    		textAfter = isList[1].replace(tab, '')
    		if (textAfter == isList[1]) {
    			previousLine = ''
    			textAfter = ''
    			selection[0] -= isList[0].length
    		} else {
    			previousLine = isList[0].replace(tab, '')
    			textAfter = '¨d¨'
    			selectionOffset[0] = -tab.length - 1
    		}
    	}
    	
    	return [previousLine, textAfter]
    }
    
    function drawNewLine(textToKeep, selectionIndex, selectionRange, textAfterLine) {
    	let newText
    	var indexOffset
    	var newLine = '\n'
    
    	if (textAfterLine == '') { newLine = '' }
    	else if (textAfterLine == '¨d¨') { newLine = '\n' }
    	
    	if (textAfterLine == null || textAfterLine == '¨d¨') {
    		indexOffset = 0
    		newText = textToKeep + newLine
    	}
    	else {
    		indexOffset = textAfterLine.length -1
    		newText = textToKeep + textAfterLine + newLine
    	}
    	
    	editor.setTextInRange(selection[0], selection[1], newText)
    	editor.setSelectedRange(selectionIndex + indexOffset + selectionOffset[0], selectionRange + selectionOffset[1])
    }
    
    //alert(selectedText.match(RegExp('^'+tab+'*(\d*)', 'g')))
    

Options

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