ucbuilder
Version:
:Shree Ganeshay Namah: new way app design
187 lines • 6.05 kB
JavaScript
import { ATTR_OF } from "./runtimeOpt.js";
export class ucUtil {
/** old one
selectAllText(elem: HTMLElement): void {
if ((elem as HTMLInputElement).select) (elem as HTMLInputElement).select();
else selectElementContents(elem);
function selectElementContents(el: HTMLElement) {
if (!el.isContentEditable) return;
var range = document.createRange();
range.selectNodeContents(el);
var sel = window.getSelection();
sel.removeAllRanges();
try {
sel.addRange(range);
} catch (exp) {
console.log(exp);
}
}
}
*/
//
static selectAllText(elem) {
if (!elem)
return;
// Input & textarea
if (elem instanceof HTMLInputElement ||
elem instanceof HTMLTextAreaElement) {
elem.select();
return;
}
// Contenteditable or normal elements
if (elem.isContentEditable) {
const range = document.createRange();
range.selectNodeContents(elem);
const sel = window.getSelection();
if (!sel)
return;
sel.removeAllRanges();
sel.addRange(range);
}
}
static getArray(obj) {
if (obj == null)
return [];
// Single DOM element (HTML or SVG)
if (obj instanceof Element) {
return [obj];
}
// DOM collections
if (obj instanceof HTMLCollection ||
obj instanceof NodeList) {
return Array.from(obj);
}
// Already an array
if (Array.isArray(obj)) {
return obj;
}
// Any other iterable (Map, Set, arguments, etc.)
if (typeof obj[Symbol.iterator] === "function") {
return Array.from(obj);
}
// Fallback: wrap single value
return [obj];
}
static setProp = function (obj, path, value) {
try {
const keys = path.split('.');
let o = obj;
for (let i = 0; i < keys.length - 1; i++) {
o = o[keys[i]] ||= {}; // Create nested object if missing
}
o[keys.at(-1)] = value;
return true;
}
catch (ex) {
return false;
}
};
static getProp = (obj, path) => {
const keys = path.split('.');
let o = obj;
for (let key of keys) {
if (o == null)
return undefined;
o = o[key];
}
return o;
};
static _getSelectedValuee = (_txt) => {
let child = _txt;
if (child.tagName === "TEXTAREA" ||
(child.tagName === "INPUT" && child.type === "text")) {
return child.value.substring(child.selectionStart, child.selectionEnd);
// or return the return value of Tim Down's selection code here
}
else
return child.innerText.substring(child.selectionStart, child.selectionEnd);
};
static currentStyles(htmlEle) {
let ele = htmlEle;
let css = ele["#data"](ATTR_OF.UC.CSSStyleDeclarations);
if (css == undefined) {
css = window.getComputedStyle(ele);
ele["#data"](ATTR_OF.UC.CSSStyleDeclarations, css);
}
return css;
}
static parseUc(ele, val) {
if (val) {
val.ucExtends.passElement(ele);
}
return ele;
}
static equalIgnoreCase(s1, s2) {
return s1.toLowerCase() === s2.toLowerCase();
}
static parseStrByUc(content, val) {
var div = document.createElement('pre');
div.innerHTML = content;
if (val) {
val.ucExtends.passElement(div);
return div.innerHTML;
}
return content;
}
static GetType(obj) { return Object.getPrototypeOf(obj).constructor.name; }
static PHP_REMOVE(text) {
return text.replace(/<\?(=|php| )(.*?)\?>/gm, '<!--?$1$2?-->');
}
static PHP_ADD(text) {
return text.replace(/<!--\?(=|php| )(.*?)\?-->/gm, '<?$1$2?>');
}
static trimPath(pth) {
return pth.replace(/^\.?\/*|\/*$/g, '');
}
static JsonCopy(obj) {
return JSON.parse(JSON.stringify(obj));
}
static distinct(ar) {
return [...new Set(ar)];
}
static resolveSubNode(path) {
return path.replace(/(?:\.\.[\/\\])+node_modules[\/\\]/i, '');
}
static changeExtension(path, fromExt, toExt) {
// Ensure extensions start with a dot
if (!fromExt.startsWith('.'))
fromExt = '.' + fromExt;
if (!toExt.startsWith('.'))
toExt = '.' + toExt;
// Replace the extension only if it matches
if (path.endsWith(fromExt)) {
return path.slice(0, -fromExt.length) + toExt;
}
// Otherwise, just append the new one
return path; // + toExt;
}
static toFilePath(path, trim = true) {
let ns = path.replace(/[\\\/]+/gi, "/");
return trim ? this._trim_(ns, "/") : ns;
}
static _trim_ = (str, charlist) => {
if (charlist === undefined)
charlist = "\s";
charlist = this.escapeRegs(charlist);
return str.replace(new RegExp("^[" + charlist + "]+$", 'ig'), "");
};
static escapeRegs = (str) => {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
};
static devEsc = (str) => {
// debugger;
return str.replace(/(.{0,1}){:(.*?)}/gm, (m, fchar, url) => {
// console.log([m,fchar,url]);
let rtrn = (fchar == "\\") ? `{:${url}}` : (fchar ?? '') + "" + url;
return rtrn;
});
};
}
export var SpecialExtEnum;
(function (SpecialExtEnum) {
SpecialExtEnum["none"] = "none";
SpecialExtEnum["uc"] = ".uc";
SpecialExtEnum["tpt"] = ".tpt";
// t = ".t"
})(SpecialExtEnum || (SpecialExtEnum = {}));
//# sourceMappingURL=ucUtil.js.map