Action

Insert Highlight

Posted by sylumer, Last update over 2 years ago

Wrap the current selection in highlight markers. There are two “modes” for this, controlled by the template definition steps.

By default, the WRAP tag is enabled, and double equality symbols will be applied to the selection for simple highlighting - e.g. “==selection==”.

If you wish to apply full critic markup, then disable the WRAP tag, and enable the PREFIX and SUFFIX custom template tags in the action steps. This will work as for WRAP, but include the additional braces - e.g. “{==selection==}”

If no selection has been made, the cursor will be positioned between the markers. If a selection has been made, the cursor will be placed after the selection and the suffix.

If the text is already wrapped in the highlight markers, and you select the text or the text and highlight markers, running this action will strip the markers leaving just the unhighlighted text, with the cursor positioned after the text.

Steps

  • defineTemplateTag

    name
    WRAP
    template
    ==
  • defineTemplateTag (disabled)

    name
    PREFIX
    template
    {==
  • defineTemplateTag (disabled)

    name
    SUFFIX
    template
    ==}
  • script

    // Set up variables for prefix and suffix
    let strPrefix;
    let strSuffix;
    
    if(draft.getTemplateTag("WRAP") == "")
    {
    	// No WRAP tag defined, so we must use PREFIX and SUFFIX
    	// One of both of them may not be defined in which case the prefix 
    	// and/or suffix will be an empty string, allowing just for prefixes
    	// or just for suffixes.
    	strPrefix = draft.getTemplateTag("PREFIX");
    	strSuffix = draft.getTemplateTag("SUFFIX");
    }
    else
    {
    	// Wrap tag is defined, so prefix and suffix are identical
    	strPrefix = draft.getTemplateTag("WRAP");
    	strSuffix = strPrefix;
    }
    
    // Get selection range details from editor
    let rngSelected = editor.getSelectedRange();
    
    // Check if we are adding or undoing the insertion
    // If the text before and after is the prefix/suffix, extend the selection
    let strBeforeSelection = editor.getTextInRange(rngSelected[0] - strPrefix.length, strPrefix.length);
    let strAfterSelection = editor.getTextInRange(rngSelected[0] + rngSelected[1], strSuffix.length);
    if(strBeforeSelection == strPrefix && strAfterSelection == strSuffix)
    {
    	// Extend the range and update thje variable for the selection range
    	editor.setSelectedRange(rngSelected[0] - strPrefix.length, strPrefix.length + rngSelected[1] + strSuffix.length);
    	rngSelected = editor.getSelectedRange();
    }
    
    // Get selection content
    let strSelection = editor.getSelectedText();
    
    if(strSelection.startsWith(strPrefix) && strSelection.endsWith(strSuffix))
    {
    	// Remove the selection and position the cursor
    	editor.setSelectedText(strSelection.substring(strPrefix.length, strSelection.length - strSuffix.length ));
    	rngSelected = editor.getSelectedRange();
    	editor.setSelectedRange(rngSelected[0] + rngSelected[1], 0);
    }
    else
    {
    	//Add the selection
    
    	// Replace the selection and position the cursor.
    	if (!strSelection || strSelection.length == 0)
    	{
    		editor.setSelectedText(strPrefix + strSuffix);
    		editor.setSelectedRange(rngSelected[0] + strPrefix.length, 0);
    	}
    	else
    	{
    		editor.setSelectedText(strPrefix + strSelection + strSuffix);
    		editor.setSelectedRange(rngSelected[0] + rngSelected[1] + strPrefix.length + strSuffix.length, 0);
    	}
    }
    
    // Activate the editor for the selection
    editor.activate();
    

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.