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
Markdown
# ๐ฆ `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