@medyll/idae-be
Version:
A modern, lightweight, and extensible DOM manipulation library built with TypeScript. Designed for precise element targeting and manipulation using a callback-based approach. Features include advanced DOM traversal, event handling, style management, attri
221 lines (220 loc) • 7.3 kB
JavaScript
import { Be, be } from '../be.js';
import { BeUtils } from '../utils.js';
var textMethods;
(function (textMethods) {
textMethods["update"] = "update";
textMethods["append"] = "append";
textMethods["prepend"] = "prepend";
textMethods["remove"] = "remove";
textMethods["wrap"] = "wrap";
textMethods["normalize"] = "normalize";
textMethods["replace"] = "replace";
textMethods["clear"] = "clear";
})(textMethods || (textMethods = {}));
export class TextHandler {
beElement;
static methods = Object.values(textMethods);
constructor(element) {
this.beElement = element;
}
methods = TextHandler.methods;
get text() {
if (this.beElement.isWhat !== 'element')
return null;
return this.beElement.inputNode.textContent;
}
handle(actions) {
Object.entries(actions).forEach(([method, props]) => {
if (method in this) {
this[method](props);
}
});
return this.beElement;
}
/**
* Updates the text content of the element(s).
* @param content - The new text content to set.
* @param callback - Optional callback function.
* @returns The Be instance for method chaining.
* @example
* // HTML: <div id="test">Original</div>
* const beInstance = be('#test');
* beInstance.updateText('Updated'); // Updates the text content to "Updated"
*/
update(content, callback) {
this.beElement.eachNode((el) => {
if (typeof content === 'string') {
el.innerText = content;
}
callback?.({
fragment: content,
be: be(el),
root: this.beElement
});
});
return this.beElement;
}
/**
* Appends text content to the element(s).
* @param content - The text content to append.
* @param callback - Optional callback function.
* @returns The Be instance for method chaining.
* @example
* // HTML: <div id="test">Original</div>
* const beInstance = be('#test');
* beInstance.appendText(' Appended'); // Appends " Appended" to the text content
*/
append(content, callback) {
this.beElement.eachNode((el) => {
if (typeof content === 'string') {
el.insertAdjacentText('beforeend', content);
}
callback?.({
fragment: content,
be: be(el),
root: this.beElement
});
});
return this.beElement;
}
/**
* Prepends text content to the element(s).
* @param content - The text content to prepend.
* @param callback - Optional callback function.
* @returns The Be instance for method chaining.
* @example
* // HTML: <div id="test">Original</div>
* const beInstance = be('#test');
* beInstance.prependText('Prepended '); // Prepends "Prepended " to the text content
*/
prepend(content, callback) {
this.beElement.eachNode((el) => {
if (typeof content === 'string') {
el.insertAdjacentText('afterbegin', content);
}
callback?.({
fragment: content,
be: be(el),
root: this.beElement
});
});
return this.beElement;
}
/**
* Replaces the text content of the element(s).
* @param content - The new text content to replace with.
* @param callback - Optional callback function.
* @returns The Be instance for method chaining.
* @example
* // HTML: <div id="test">Original</div>
* const beInstance = be('#test');
* beInstance.replaceText('Replaced'); // Replaces the text content with "Replaced"
*/
replace(content, callback) {
this.beElement.eachNode((el) => {
if (typeof content === 'string') {
el.textContent = content;
}
callback?.({
fragment: content,
be: be(el),
root: this.beElement
});
});
return this.beElement;
}
/**
* Removes the element(s) from the DOM.
* @param callback - Optional callback function.
* @returns The Be instance for method chaining.
* @example
* // HTML: <div id="test">To be removed</div>
* const beInstance = be('#test');
* beInstance.removeText(); // Removes the element
*/
remove(callback) {
this.beElement.eachNode((el) => {
el.remove();
callback?.({
fragment: undefined,
be: be(el),
root: this.beElement
});
});
return this.beElement;
}
/**
* Clears the text content of the element(s).
* @param callback - Optional callback function.
* @returns The Be instance for method chaining.
* @example
* // HTML: <div id="test">Content</div>
* const beInstance = be('#test');
* beInstance.clearText(); // Clears the text content
*/
clear(callback) {
this.beElement.eachNode((el) => {
el.innerHTML = '';
callback?.({
fragment: undefined,
be: be(el),
root: this.beElement
});
});
return this.beElement;
}
/**
* Normalizes the text content of the element(s).
* @param callback - Optional callback function.
* @returns The Be instance for method chaining.
* @example
* // HTML: <div id="test">Text <span>Fragment</span> Text</div>
* const beInstance = be('#test');
* beInstance.normalizeText(); // Normalizes the text content
*/
normalize(callback) {
this.beElement.eachNode((el) => {
el.normalize();
callback?.({
fragment: undefined,
be: be(el),
root: this.beElement
});
});
return this.beElement;
}
/**
* Wraps the element(s) with a new element.
* @param content - The wrapper element as a string or HTMLElement.
* @param callback - Optional callback function.
* @returns The Be instance for method chaining.
* @example
* // HTML: <div id="test">Content</div>
* const beInstance = be('#test');
* beInstance.wrapText('<div class="wrapper"></div>'); // Wraps the element with a <div>
*/
wrap(content, callback) {
this.beElement.eachNode((el) => {
if (typeof content === 'string') {
const wrapper = document.createElement('div');
wrapper.innerHTML = content.trim();
const parent = wrapper.firstElementChild;
if (parent) {
el.parentNode?.insertBefore(parent, el);
parent.appendChild(el);
}
}
callback?.({
fragment: content,
be: be(el),
root: this.beElement
});
});
return this.beElement;
}
valueOf() {
if (this.beElement.isWhat !== 'element')
return null;
return this.beElement.inputNode.innerText;
}
}