UNPKG

@nativescript-community/ui-material-textview

Version:
337 lines 14.7 kB
import { themer } from '@nativescript-community/ui-material-core'; import { buttonColorProperty, counterMaxLengthProperty, errorColorProperty, errorProperty, floatingColorProperty, floatingInactiveColorProperty, floatingProperty, helperColorProperty, helperProperty, strokeColorProperty, strokeInactiveColorProperty } from '@nativescript-community/ui-material-core/textbase/cssproperties'; import { Color, Screen, backgroundInternalProperty, editableProperty, hintProperty, placeholderColorProperty } from '@nativescript/core'; import { TextViewBase } from './textview.common'; var MDCMultilineTextInputLayoutDelegateImpl = /** @class */ (function (_super) { __extends(MDCMultilineTextInputLayoutDelegateImpl, _super); function MDCMultilineTextInputLayoutDelegateImpl() { return _super !== null && _super.apply(this, arguments) || this; } MDCMultilineTextInputLayoutDelegateImpl.initWithOwner = function (owner) { var delegate = MDCMultilineTextInputLayoutDelegateImpl.new(); delegate._owner = new WeakRef(owner); return delegate; }; MDCMultilineTextInputLayoutDelegateImpl.prototype.multilineTextFieldDidChangeContentSize = function (multilineTextField, size) { // called when clicked on background var owner = this._owner ? this._owner.get() : null; if (owner) { owner._onTextFieldDidChangeContentSize && owner._onTextFieldDidChangeContentSize(size); } }; MDCMultilineTextInputLayoutDelegateImpl.ObjCProtocols = [MDCMultilineTextInputLayoutDelegate]; return MDCMultilineTextInputLayoutDelegateImpl; }(NSObject)); var TextViewInputControllerUnderlineImpl = /** @class */ (function (_super) { __extends(TextViewInputControllerUnderlineImpl, _super); function TextViewInputControllerUnderlineImpl() { return _super !== null && _super.apply(this, arguments) || this; } TextViewInputControllerUnderlineImpl.initWithOwner = function (owner) { var delegate = TextViewInputControllerUnderlineImpl.new(); delegate._owner = new WeakRef(owner); return delegate; }; TextViewInputControllerUnderlineImpl.prototype.textInsetsWithSizeThatFitsWidthHint = function (defaultValue, widthHint) { var result = _super.prototype.textInsetsWithSizeThatFitsWidthHint.call(this, defaultValue, widthHint); var owner = this._owner ? this._owner.get() : null; if (owner) { result = owner._getTextInsetsForBounds(result); } return result; }; return TextViewInputControllerUnderlineImpl; }(MDCTextInputControllerUnderline)); var TextViewInputControllerImpl = /** @class */ (function (_super) { __extends(TextViewInputControllerImpl, _super); function TextViewInputControllerImpl() { return _super !== null && _super.apply(this, arguments) || this; } TextViewInputControllerImpl.initWithOwner = function (owner) { var delegate = TextViewInputControllerImpl.new(); delegate._owner = new WeakRef(owner); return delegate; }; TextViewInputControllerImpl.prototype.textInsetsWithSizeThatFitsWidthHint = function (defaultValue, widthHint) { var result = _super.prototype.textInsetsWithSizeThatFitsWidthHint.call(this, defaultValue, widthHint); var owner = this._owner ? this._owner.get() : null; if (owner) { result = owner._getTextInsetsForBounds(result); } return result; }; return TextViewInputControllerImpl; }(MDCTextInputControllerBase)); var TextViewInputControllerOutlinedImpl = /** @class */ (function (_super) { __extends(TextViewInputControllerOutlinedImpl, _super); function TextViewInputControllerOutlinedImpl() { return _super !== null && _super.apply(this, arguments) || this; } TextViewInputControllerOutlinedImpl.initWithOwner = function (owner) { var delegate = TextViewInputControllerOutlinedImpl.new(); delegate._owner = new WeakRef(owner); return delegate; }; TextViewInputControllerOutlinedImpl.prototype.textInsetsWithSizeThatFitsWidthHint = function (defaultValue, widthHint) { var result = _super.prototype.textInsetsWithSizeThatFitsWidthHint.call(this, defaultValue, widthHint); var owner = this._owner ? this._owner.get() : null; if (owner) { result = owner._getTextInsetsForBounds(result); } return result; }; return TextViewInputControllerOutlinedImpl; }(MDCTextInputControllerOutlinedTextArea)); var TextViewInputControllerFilledImpl = /** @class */ (function (_super) { __extends(TextViewInputControllerFilledImpl, _super); function TextViewInputControllerFilledImpl() { return _super !== null && _super.apply(this, arguments) || this; } TextViewInputControllerFilledImpl.initWithOwner = function (owner) { var delegate = TextViewInputControllerFilledImpl.new(); delegate._owner = new WeakRef(owner); return delegate; }; TextViewInputControllerFilledImpl.prototype.textInsetsWithSizeThatFitsWidthHint = function (defaultValue, widthHint) { var result = _super.prototype.textInsetsWithSizeThatFitsWidthHint.call(this, defaultValue, widthHint); var owner = this._owner ? this._owner.get() : null; if (owner) { result = owner._getTextInsetsForBounds(result); } return result; }; return TextViewInputControllerFilledImpl; }(MDCTextInputControllerFilled)); export class TextView extends TextViewBase { clearFocus() { this.dismissSoftInput(); } requestFocus() { this.focus(); } // N bug fix // @ts-ignore get ios() { return this.nativeTextViewProtected; } // @ts-ignore get nativeTextViewProtected() { return this.nativeViewProtected.textView; } // get nativeRealTextViewProtected() { // return this.nativeViewProtected.textView; // } _getTextInsetsForBounds(insets) { const scale = Screen.mainScreen.scale; if (this.variant === 'underline' && this._controller.underlineHeightNormal === 0) { // if no underline/custom background, remove all insets like on android insets.top = 0; insets.bottom = 0; } insets.left += (this.effectiveBorderLeftWidth + this.effectivePaddingLeft) / scale; insets.top += (this.effectiveBorderTopWidth + this.effectivePaddingTop) / scale; insets.right += (this.effectivePaddingRight + this.effectiveBorderRightWidth) / scale; insets.bottom += (this.effectivePaddingBottom + this.effectiveBorderBottomWidth) / scale; return insets; } _onTextFieldDidChangeContentSize(size) { if (this.height === 'auto') { this.requestLayout(); } } createNativeView() { const view = MDCMultilineTextField.new(); view._owner = new WeakRef(this); // disable it for now view.clearButtonMode = 0 /* UITextFieldViewMode.Never */; const scheme = MDCContainerScheme.new(); scheme.colorScheme = themer.getAppColorScheme(); if (this.style.variant === 'filled') { this._controller = TextViewInputControllerFilledImpl.initWithOwner(this); } else if (this.style.variant === 'outline') { this._controller = TextViewInputControllerOutlinedImpl.initWithOwner(this); } else if (this.style.variant === 'underline') { this._controller = TextViewInputControllerUnderlineImpl.initWithOwner(this); } else { this._controller = TextViewInputControllerImpl.initWithOwner(this); } this._controller.textInput = view; if (this.style.variant === 'filled') { this._controller.applyThemeWithScheme(scheme); } else if (this.style.variant === 'outline') { // (this._controller as TextViewInputControllerOutlinedImpl).applyThemeWithScheme(scheme); } else if (this.style.variant === 'underline') { this._controller.applyThemeWithScheme(scheme); } else { this._controller.underlineHeightActive = 0; this._controller.underlineHeightNormal = 0; } view.textView.font = UIFont.systemFontOfSize(19); this._controller.inlinePlaceholderFont = UIFont.systemFontOfSize(16); // view.textInsetsMode = MDCTextInputTextInsetsMode.IfContent; return view; } initNativeView() { super.initNativeView(); const view = this.nativeViewProtected; view.layoutDelegate = MDCMultilineTextInputLayoutDelegateImpl.initWithOwner(this); this.layoutDelegate = view.layoutDelegate; } disposeNativeView() { const view = this.nativeViewProtected; view.layoutDelegate = null; this.layoutDelegate = null; super.disposeNativeView(); } // TODO: check why i was checking for isFirstResponder // with it the blur event is not fired anymore // public dismissSoftInput() { // if (this.nativeViewProtected.isFirstResponder) { // super.dismissSoftInput(); // } // } blur() { this.dismissSoftInput(); } setSelection(start, stop) { const view = this.nativeTextViewProtected; if (stop !== undefined) { const begin = view.beginningOfDocument; view.selectedTextRange = view.textRangeFromPositionToPosition(view.positionFromPositionOffset(begin, start), view.positionFromPositionOffset(begin, stop)); } else { const begin = view.beginningOfDocument; const pos = view.positionFromPositionOffset(begin, start); view.selectedTextRange = view.textRangeFromPositionToPosition(pos, pos); } } _refreshColor() { if (this._isShowingHint) { const placeholderColor = this.style.placeholderColor; const color = this.style.color; if (placeholderColor) { this.nativeViewProtected.textColor = placeholderColor.ios; } else if (color) { // Use semi-transparent version of color for back-compatibility this.nativeViewProtected.textColor = color.ios.colorWithAlphaComponent(0.22); } else { this.nativeViewProtected.textColor = this._hintColor; } } else { const color = this.style.color; if (color) { this.nativeViewProtected.textColor = color.ios; this.nativeViewProtected.tintColor = color.ios; } else { this.nativeViewProtected.textColor = this._textColor; this.nativeViewProtected.tintColor = this._textColor; } } } [hintProperty.getDefault]() { return ''; } [hintProperty.setNative](value) { this._controller.placeholderText = value; } [editableProperty.setNative](value) { this.nativeTextViewProtected.editable = value; this.nativeTextViewProtected.selectable = value; } [floatingColorProperty.setNative](value) { const color = value instanceof Color ? value.ios : value; this._controller.floatingPlaceholderActiveColor = color; } [floatingInactiveColorProperty.setNative](value) { const color = value instanceof Color ? value.ios : value; this._controller.inlinePlaceholderColor = color; this._controller.floatingPlaceholderNormalColor = color; } [placeholderColorProperty.setNative](value) { const color = value instanceof Color ? value.ios : value; this._controller.inlinePlaceholderColor = color; if (!this.floatingColor) { this._controller.floatingPlaceholderActiveColor = color; } } [errorColorProperty.setNative](value) { const color = value instanceof Color ? value.ios : value; this._controller.errorColor = color; } [strokeColorProperty.setNative](value) { const color = value instanceof Color ? value.ios : value; this._controller.activeColor = color; } [strokeInactiveColorProperty.setNative](value) { const color = value instanceof Color ? value.ios : value; this._controller.normalColor = color; } [buttonColorProperty.setNative](value) { const color = value instanceof Color ? value.ios : value; this._controller.textInputClearButtonTintColor = color; } [helperProperty.setNative](value) { this._controller.helperText = value; } [helperColorProperty.setNative](value) { const temp = typeof value === 'string' ? new Color(value) : value; const color = temp.ios; this._controller.leadingUnderlineLabelTextColor = color; } [counterMaxLengthProperty.setNative](value) { this._controller.characterCountMax = value; } [floatingProperty.setNative](value) { this._controller.floatingEnabled = value; } [errorProperty.setNative](value) { this._controller.setErrorTextErrorAccessibilityValue(value, value); } [backgroundInternalProperty.setNative](value) { switch (this.variant) { case 'none': case 'filled': super[backgroundInternalProperty.setNative](value); break; case 'outline': case 'underline': { if (value.color) { this._controller.borderFillColor = value.color.ios; } if (value.borderTopColor) { this._controller.normalColor = value.borderTopColor.ios; } break; } } } setFormattedTextDecorationAndTransform() { // this.returnRealTextView = false; super.setFormattedTextDecorationAndTransform(); const attributedText = this.nativeTextViewProtected.attributedText; this.nativeTextViewProtected.text = null; this.nativeViewProtected.attributedText = attributedText; } setTextDecorationAndTransform() { super.setTextDecorationAndTransform(); if (this.nativeTextViewProtected.attributedText) { const attributedText = this.nativeTextViewProtected.attributedText; this.nativeViewProtected.attributedText = attributedText; } else { const text = this.nativeTextViewProtected.text; this.nativeViewProtected.text = text; } } } //# sourceMappingURL=textview.ios.js.map