Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML Comment Action #99

Open
extratone opened this issue Oct 10, 2022 · 1 comment
Open

HTML Comment Action #99

extratone opened this issue Oct 10, 2022 · 1 comment
Assignees
Labels
configuration documentation Improvements or additions to documentation

Comments

@extratone
Copy link
Owner

extratone commented Oct 10, 2022

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

@extratone extratone added configuration documentation Improvements or additions to documentation labels Oct 10, 2022
@extratone extratone self-assigned this Oct 10, 2022
@extratone
Copy link
Owner Author

extratone commented Oct 10, 2022

''

'''

'''

extratone added a commit that referenced this issue Oct 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
configuration documentation Improvements or additions to documentation
Projects
Status: Maintaining
Development

No branches or pull requests

1 participant