@teachingtextbooks/keyboard
Version:
Customizable TypeScript soft keyboard
164 lines (160 loc) • 6 kB
JavaScript
import { Platform } from "./const";
export default class TTUtils {
static isMobilePlatform(win) {
const cur = TTUtils.getPlatform(win);
return cur === Platform.IOS || cur === Platform.ANDROID;
}
static getPlatform(win) {
if (win.hasOwnProperty('Capacitor')) {
const source = win;
const capacitorWin = source;
return capacitorWin.Capacitor.platform;
}
else {
return Platform.WEB;
}
}
static getGroup(doc, classes) {
const el = doc.createElementNS('http://www.w3.org/2000/svg', 'g');
TTUtils.setClasses(el, classes);
return el;
}
static getText(doc, classes) {
const el = doc.createElementNS('http://www.w3.org/2000/svg', 'text');
TTUtils.setClasses(el, classes);
return el;
}
static getRect(doc, width, height, fill, opacity, corner, classes) {
const el = doc.createElementNS('http://www.w3.org/2000/svg', 'rect');
el.setAttributeNS(null, 'width', width.toString());
el.setAttributeNS(null, 'height', height.toString());
el.setAttributeNS(null, 'fill', fill);
el.setAttributeNS(null, 'fill-opacity', opacity.toString());
if (corner > 0) {
el.setAttributeNS(null, 'rx', TTUtils.num2px(corner));
el.setAttributeNS(null, 'ry', TTUtils.num2px(corner));
}
TTUtils.setClasses(el, classes);
return el;
}
static getClipPath(doc, id) {
const el = doc.createElementNS('http://www.w3.org/2000/svg', 'clipPath');
el.setAttributeNS(null, 'id', id);
const rect = doc.createElementNS('http://www.w3.org/2000/svg', 'rect');
el.appendChild(rect);
return el;
}
static showHideSVGElement(el, show) {
el.setAttributeNS(null, 'visibility', show ? '' : 'hidden');
}
static showHideHTMLElement(el, displayVal) {
el.style.display = displayVal;
}
static resizeRect(el, width = NaN, height = NaN) {
!isNaN(width) && el.setAttributeNS(null, 'width', width.toString());
!isNaN(height) && el.setAttributeNS(null, 'height', height.toString());
}
static moveSVGElement(el, dx = NaN, dy = NaN) {
!isNaN(dx) && el.setAttributeNS(null, 'x', dx.toString());
!isNaN(dy) && el.setAttributeNS(null, 'y', dy.toString());
}
static moveHTMLElement(el, dx = NaN, dy = NaN) {
if (!isNaN(dx))
el.style.left = TTUtils.num2px(dx);
if (!isNaN(dy))
el.style.top = TTUtils.num2px(dy);
}
static setOpacity(el, value) {
el.setAttributeNS(null, 'opacity', value.toString());
}
static transformElement(el, dx, dy, scale) {
if (!el)
return;
let tsf = dx !== 0 || dy !== 0 ? `translate(${dx}, ${dy}) ` : '';
scale !== 1 ? tsf += `scale(${scale})` : '';
tsf !== '' ?
el.setAttributeNS(null, 'transform', tsf) :
el.setAttributeNS(null, 'transform', '');
}
/*
static translateAnchorLayout(displayWidth: number, displayHeight: number, targetWidth: number, targetHeight: number, layout: IAnchorLayout): DOMPoint {
let cx: number = 0, cy: number = 0;
if (layout.horizontal !== undefined)
cx = (displayWidth - targetWidth) / 2 + layout.horizontal;
else if (layout.left !== undefined)
cx = layout.left;
else if (layout.right !== undefined)
cx = displayWidth - targetWidth - layout.right;
if (layout.vertical !== undefined)
cy = (displayHeight - targetHeight) / 2 + layout.vertical;
else if (layout.top !== undefined)
cy = layout.top;
else if (layout.bottom !== undefined)
cy = displayHeight - targetHeight - layout.bottom;
return new DOMPoint(cx, cy);
}
*/
static setClasses(el, classes) {
el.setAttributeNS(null, 'class', TTUtils.formatClasses(classes));
}
static formatClasses(values) {
return values.join(' ');
}
static randomInt(min, max) {
return Math.round(TTUtils.randomFloat(min, max));
}
static randomBoolean(trueChance = 0) {
if (trueChance > 0 && trueChance < 1) {
const ch = this.randomFloat(0, 1);
return ch <= trueChance;
}
else {
return Boolean(Math.round(Math.random()));
}
}
static randomFloat(min, max) {
return (max - min) * Math.random() + min;
}
static randomizeArray(src) {
const source = [...src];
const randArr = [];
let rand;
while (source.length) {
rand = TTUtils.randomInt(0, source.length - 1);
randArr.push(source[rand]);
source.splice(rand, 1);
}
return randArr;
}
static addLeadingZeros(val, size) {
let res = val.toString();
while (res.length < size) {
res = '0' + res;
}
return res;
}
static getRangeArray(min, max, step = 1) {
// return Array(max-min+1).fill(min).map((itm, ind) => itm+ind);
const len = Math.ceil((max - min + 1) / step);
return Array(len).fill(min).map((itm, ind) => itm + (ind * step));
}
static checkStringSpaces(val) {
return val.split(' ').length === (val.length + 1);
}
static getElWidth(el, offset = true) {
const hEl = el;
const style = getComputedStyle(el);
const origin = offset ? hEl.offsetWidth : hEl.clientWidth;
return origin - TTUtils.px2Num(style.paddingLeft) - TTUtils.px2Num(style.paddingRight);
}
static px2Num(source) {
return source === 'auto' || !source ?
0 :
parseFloat(source);
}
static num2px(source) {
return isNaN(source) ?
'0px' :
source + 'px';
}
}