UNPKG

tiny-essentials

Version:

Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.

209 lines (118 loc) โ€ข 4.21 kB
# ๐Ÿ“ฆ `TinyTextRangeEditor` Documentation A lightweight but powerful text range utility class for `<input>` and `<textarea>` elements, ideal for building BBCode or other tag-based markup editors. --- ## ๐Ÿ›  Constructor ```js new TinyTextRangeEditor(element, options?) ``` ### Parameters: * `element` **(HTMLInputElement | HTMLTextAreaElement)** โ€“ Target editable element. * `options` **(Object)** *(optional)* โ€“ Customize tag delimiters. * `openTag` **(string)** โ€“ Default: `'['` * `closeTag` **(string)** โ€“ Default: `']'` --- ## ๐Ÿ”ง Core Methods ### `getOpenTag()` โ†’ `string` Returns the current **opening tag symbol**. ### `getCloseTag()` โ†’ `string` Returns the current **closing tag symbol**. ### `setOpenTag(tag: string)` Changes the **opening tag symbol**. ### `setCloseTag(tag: string)` Changes the **closing tag symbol**. --- ## ๐ŸŽฏ Selection & Focus ### `focus()` โ†’ `this` Focuses the target element. ### `ensureFocus()` โ†’ `this` Focuses the element only if not already focused. ### `getSelectionRange()` โ†’ `{ start: number, end: number }` Returns the current **text selection range**. ### `setSelectionRange(start, end, preserveScroll = true)` โ†’ `this` Sets the selection range. --- ## ๐Ÿ“œ Value Access ### `getValue()` โ†’ `string` Returns the entire value of the field. ### `setValue(value: string)` โ†’ `this` Sets the entire content. ### `getSelectedText()` โ†’ `string` Returns the currently selected text. --- ## โœ๏ธ Text Manipulation ### `insertText(text, options?)` โ†’ `this` Inserts (and replaces) text at selection. #### Options: * `newCursor`: `"start" | "end" | "preserve"` *(default: `'end'`)* * `autoSpacing`: *(boolean)* Add space if needed on both sides. * `autoSpaceLeft` / `autoSpaceRight`: *(boolean)* Fine-grained spacing control. --- ### `deleteSelection()` โ†’ `this` Deletes selected text. ### `transformSelection(fn)` โ†’ `this` Applies a transformation function to the selected text. ### `surroundSelection(prefix, suffix)` โ†’ `this` Wraps selected text with custom strings. --- ## ๐Ÿช„ Tag Operations ### `wrapWithTag(tagName, attributes?)` โ†’ `this` Wraps the selection in a custom tag. ```js editor.wrapWithTag("b"); editor.wrapWithTag("color", { value: "red" }); ``` ### `insertTag(tagName, content?, attributes?)` โ†’ `this` Inserts an opening and closing tag with optional content. ```js editor.insertTag("quote", "Hello!", { user: "Yasmin" }); ``` ### `insertSelfClosingTag(tagName, attributes?)` โ†’ `this` Inserts a self-closing tag. ```js editor.insertSelfClosingTag("br"); editor.insertSelfClosingTag("img", { src: "link.jpg", alt: "image" }); ``` ### `toggleTag(tagName, attributes?)` โ†’ `this` Wraps or **unwraps** the selection with a tag. ```js editor.toggleTag("b"); ``` ### `toggleCode(codeName)` โ†’ `this` Wraps or **unwraps** the selection with a code. ```js editor.toggleTag("**"); ``` --- ## ๐Ÿง  Utility ### `moveCaret(offset: number)` โ†’ `this` Moves the caret by an offset. ### `selectAll()` โ†’ `this` Selects all the content. ### `expandSelection(before, after)` โ†’ `this` Expands the selection range by the given number of characters. ### `replaceAll(regex, replacerFn)` โ†’ `this` Performs regex replacement over the full text. --- ### `replaceInSelection(regex, replacerFn)` Replaces all matches of a regular expression **only within the currently selected text** in the editor. --- ## ๐Ÿงฉ Internal Attribute Helper ### `_insertAttr(attributes)` โ†’ `string` Serializes tag attributes to string format. Supports: * โœ… Object: `{ color: "red" } โ†’ color="red"` * โœ… Array: `["checked", "readonly"] โ†’ checked readonly` --- ## ๐Ÿงช Example Use ```js const textarea = document.querySelector('textarea'); const editor = new TinyTextRangeEditor(textarea); editor.wrapWithTag("b"); editor.insertTag("color", "red text", { color: "red" }); editor.insertSelfClosingTag("br"); ``` --- ## ๐Ÿ“ Notes * All methods are **chainable** (`return this`) * Throws detailed **TypeErrors** if used incorrectly * Does **not** depend on any external library * Useful for BBCode editors, markdown-like syntax, or custom in-text macros