Action

HTML Comment

Posted by David Blue, Last update over 1 year ago

UPDATES

over 1 year ago

-Listed publicly on the Directory

Wraps selected text in HTML comment tags (‘<!– –>’.) If no text is selected, the cursor is placed between the same.

This action was made possible by @sylumer’s Insert Highlight Action. You’ll note, in fact - at least as of initial release - it includes his Script step, entirely unmodified (including the comments!)

Steps

  • defineTemplateTag

    name
    PREFIX
    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 Nothing
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.