8000 HTML Comment Action · Issue #99 · extratone/drafts · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
HTML Comment Action #99
Open
Open
@extratone

Description

@extratone

Updated 10112022-132913


Social

<script async="" src="https://telegram.org/js/telegram-widget.js?1" data-telegram-post="draftsapp/345" data-width="100%"></script>

hey Drafts users! give this a whirl for me before I list it publicly, would ya? https://t.co/UOmODGtkFP

— 𝗗 𝗔 𝗩 𝗢 𝗗 (@NeoYokel) October 10, 2022
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

hey Drafts users! give this a whirl for me before I list it publicly, would ya? https://directory.getdrafts.com/a/2Dj

Before I list it publicly, I'd love it if any of y'all get a chance to try this HTML Comment Action.

Description

Wraps selected text in HTML comment tags (''.) If no text is selected, the cursor is placed between the same.

Directory Description

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!)


JS

 // 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();

Install

Metadata

Metadata

Assignees

Labels

Projects

Status

Maintaining

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0