UNPKG

uicore-ts

Version:

UICore is a library to build native-like user interfaces using pure Typescript. No HTML is needed at all. Components are described as TS classes and all user interactions are handled explicitly. This library is strongly inspired by the UIKit framework tha

252 lines (251 loc) 9.09 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var UITextView_exports = {}; __export(UITextView_exports, { UITextView: () => UITextView }); module.exports = __toCommonJS(UITextView_exports); var import_UIColor = require("./UIColor"); var import_UIObject = require("./UIObject"); var import_UIRectangle = require("./UIRectangle"); var import_UIView = require("./UIView"); const _UITextView = class extends import_UIView.UIView { constructor(elementID, textViewType = _UITextView.type.paragraph, viewHTMLElement = null) { super(elementID, viewHTMLElement, textViewType); this._textColor = _UITextView.defaultTextColor; this._isSingleLine = import_UIObject.YES; this.textPrefix = ""; this.textSuffix = ""; this._notificationAmount = 0; this._automaticFontSizeSelection = import_UIObject.NO; this.changesOften = import_UIObject.NO; this._intrinsicHeightCache = new import_UIObject.UIObject(); this._intrinsicWidthCache = new import_UIObject.UIObject(); this.text = ""; this.style.overflow = "hidden"; this.style.textOverflow = "ellipsis"; this.isSingleLine = import_UIObject.YES; this.textColor = this.textColor; this.userInteractionEnabled = import_UIObject.YES; if (textViewType == _UITextView.type.textArea) { this.pausesPointerEvents = import_UIObject.YES; this.addTargetForControlEvent( import_UIView.UIView.controlEvent.PointerUpInside, (sender, event) => sender.focus() ); } } static _determinePXAndPTRatios() { if (_UITextView._ptToPx) { return; } const o = document.createElement("div"); o.style.width = "1000pt"; document.body.appendChild(o); _UITextView._ptToPx = o.clientWidth / 1e3; document.body.removeChild(o); _UITextView._pxToPt = 1 / _UITextView._ptToPx; } get textAlignment() { return this.style.textAlign; } set textAlignment(textAlignment) { this._textAlignment = textAlignment; this.style.textAlign = textAlignment; } get textColor() { return this._textColor; } set textColor(color) { this._textColor = color || _UITextView.defaultTextColor; this.style.color = this._textColor.stringValue; } get isSingleLine() { return this._isSingleLine; } set isSingleLine(isSingleLine) { this._isSingleLine = isSingleLine; this._intrinsicHeightCache = new import_UIObject.UIObject(); this._intrinsicWidthCache = new import_UIObject.UIObject(); if (isSingleLine) { this.style.whiteSpace = "pre"; return; } this.style.whiteSpace = "pre-wrap"; } get notificationAmount() { return this._notificationAmount; } set notificationAmount(notificationAmount) { if (this._notificationAmount == notificationAmount) { return; } this._notificationAmount = notificationAmount; this.text = this.text; this.setNeedsLayoutUpToRootView(); this.notificationAmountDidChange(notificationAmount); } notificationAmountDidChange(notificationAmount) { } get text() { return this._text || this.viewHTMLElement.innerHTML; } set text(text) { this._text = text; var notificationText = ""; if (this.notificationAmount) { notificationText = '<span style="color: ' + _UITextView.notificationTextColor.stringValue + ';">' + (" (" + this.notificationAmount + ")").bold() + "</span>"; } if (this.viewHTMLElement.innerHTML != this.textPrefix + text + this.textSuffix + notificationText) { this.viewHTMLElement.innerHTML = this.textPrefix + (0, import_UIObject.FIRST)(text, "") + this.textSuffix + notificationText; } if (this.changesOften) { this._intrinsicHeightCache = new import_UIObject.UIObject(); this._intrinsicWidthCache = new import_UIObject.UIObject(); } this.setNeedsLayout(); } set innerHTML(innerHTML) { this.text = innerHTML; } get innerHTML() { return this.viewHTMLElement.innerHTML; } setText(key, defaultString, parameters) { this.setInnerHTML(key, defaultString, parameters); } get fontSize() { const style = window.getComputedStyle(this.viewHTMLElement, null).fontSize; const result = parseFloat(style) * _UITextView._pxToPt; return result; } set fontSize(fontSize) { this.style.fontSize = "" + fontSize + "pt"; this._intrinsicHeightCache = new import_UIObject.UIObject(); this._intrinsicWidthCache = new import_UIObject.UIObject(); } useAutomaticFontSize(minFontSize = import_UIObject.nil, maxFontSize = import_UIObject.nil) { this._automaticFontSizeSelection = import_UIObject.YES; this._minFontSize = minFontSize; this._maxFontSize = maxFontSize; this.setNeedsLayout(); } static automaticallyCalculatedFontSize(bounds, currentSize, currentFontSize, minFontSize, maxFontSize) { minFontSize = (0, import_UIObject.FIRST)(minFontSize, 1); maxFontSize = (0, import_UIObject.FIRST)(maxFontSize, 1e11); const heightMultiplier = bounds.height / (currentSize.height + 1); const widthMultiplier = bounds.width / (currentSize.width + 1); var multiplier = heightMultiplier; if (heightMultiplier > widthMultiplier) { multiplier = widthMultiplier; } const maxFittingFontSize = currentFontSize * multiplier; if (maxFittingFontSize > maxFontSize) { return maxFontSize; } if (minFontSize > maxFittingFontSize) { return minFontSize; } return maxFittingFontSize; } didReceiveBroadcastEvent(event) { super.didReceiveBroadcastEvent(event); } willMoveToSuperview(superview) { super.willMoveToSuperview(superview); } layoutSubviews() { super.layoutSubviews(); if (this._automaticFontSizeSelection) { this.fontSize = _UITextView.automaticallyCalculatedFontSize( new import_UIRectangle.UIRectangle(0, 0, 1 * this.viewHTMLElement.offsetHeight, 1 * this.viewHTMLElement.offsetWidth), this.intrinsicContentSize(), this.fontSize, this._minFontSize, this._maxFontSize ); } } intrinsicContentHeight(constrainingWidth = 0) { const keyPath = (this.viewHTMLElement.innerHTML + "_csf_" + this.computedStyle.font).replace(new RegExp( "\\.", "g" ), "_") + "." + ("" + constrainingWidth).replace(new RegExp("\\.", "g"), "_"); let cacheObject = _UITextView._intrinsicHeightCache; if (this.changesOften) { cacheObject = this._intrinsicHeightCache; } var result = cacheObject.valueForKeyPath(keyPath); if ((0, import_UIObject.IS_LIKE_NULL)(result)) { result = super.intrinsicContentHeight(constrainingWidth); cacheObject.setValueForKeyPath(keyPath, result); } return result; } intrinsicContentWidth(constrainingHeight = 0) { const keyPath = (this.viewHTMLElement.innerHTML + "_csf_" + this.computedStyle.font).replace(new RegExp( "\\.", "g" ), "_") + "." + ("" + constrainingHeight).replace(new RegExp("\\.", "g"), "_"); let cacheObject = _UITextView._intrinsicWidthCache; if (this.changesOften) { cacheObject = this._intrinsicWidthCache; } var result = cacheObject.valueForKeyPath(keyPath); if ((0, import_UIObject.IS_LIKE_NULL)(result)) { result = super.intrinsicContentWidth(constrainingHeight); cacheObject.setValueForKeyPath(keyPath, result); } return result; } intrinsicContentSize() { const result = this.intrinsicContentSizeWithConstraints(import_UIObject.nil, import_UIObject.nil); return result; } }; let UITextView = _UITextView; UITextView.defaultTextColor = import_UIColor.UIColor.blackColor; UITextView.notificationTextColor = import_UIColor.UIColor.redColor; UITextView._intrinsicHeightCache = new import_UIObject.UIObject(); UITextView._intrinsicWidthCache = new import_UIObject.UIObject(); UITextView.type = { "paragraph": "p", "header1": "h1", "header2": "h2", "header3": "h3", "header4": "h4", "header5": "h5", "header6": "h6", "textArea": "textarea", "textField": "input", "span": "span", "label": "label" }; UITextView.textAlignment = { "left": "left", "center": "center", "right": "right", "justify": "justify" }; UITextView._determinePXAndPTRatios(); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { UITextView }); //# sourceMappingURL=UITextView.js.map