Merge branch 'master'

This commit is contained in:
Vasil Trifonov
2020-01-17 15:12:28 +02:00
parent 432a029499
commit b76893801d
3131 changed files with 418263 additions and 185769 deletions

View File

@@ -0,0 +1,69 @@
import { EditableTextBase as EditableTextBaseDefinition, KeyboardType, ReturnKeyType, UpdateTextTrigger, AutocapitalizationType } from ".";
import { TextBase, Property, CssProperty, Style, Color, booleanConverter, makeValidator, makeParser, PseudoClassHandler } from "../text-base";
export * from "../text-base";
export abstract class EditableTextBase extends TextBase implements EditableTextBaseDefinition {
public static blurEvent = "blur";
public static focusEvent = "focus";
public static textChangeEvent = "textChange";
public keyboardType: KeyboardType;
public returnKeyType: ReturnKeyType;
public updateTextTrigger: UpdateTextTrigger;
public autocapitalizationType: AutocapitalizationType;
public editable: boolean;
public autocorrect: boolean;
public hint: string;
public maxLength: number;
public abstract dismissSoftInput();
public abstract _setInputType(inputType: number): void;
private _focusHandler = () => this._goToVisualState("focus");
private _blurHandler = () => this._goToVisualState("blur");
@PseudoClassHandler("focus", "blur")
_updateTextBaseFocusStateHandler(subscribe) {
const method = subscribe ? "on" : "off";
this[method]("focus", this._focusHandler);
this[method]("blur", this._blurHandler);
}
}
// TODO: Why not name it - hintColor property??
// TODO: Or rename hintProperty to 'placeholder' and make it CSSProperty??
// https://developer.mozilla.org/en-US/docs/Web/CSS/:-moz-placeholder
export const placeholderColorProperty = new CssProperty<Style, Color>({ name: "placeholderColor", cssName: "placeholder-color", equalityComparer: Color.equals, valueConverter: (v) => new Color(v) });
placeholderColorProperty.register(Style);
const keyboardTypeConverter = makeParser<KeyboardType>(makeValidator<KeyboardType>("datetime", "phone", "number", "url", "email", "integer"));
export const keyboardTypeProperty = new Property<EditableTextBase, KeyboardType>({ name: "keyboardType", valueConverter: keyboardTypeConverter });
keyboardTypeProperty.register(EditableTextBase);
const returnKeyTypeConverter = makeParser<ReturnKeyType>(makeValidator<ReturnKeyType>("done", "next", "go", "search", "send"));
export const returnKeyTypeProperty = new Property<EditableTextBase, ReturnKeyType>({ name: "returnKeyType", valueConverter: returnKeyTypeConverter });
returnKeyTypeProperty.register(EditableTextBase);
export const editableProperty = new Property<EditableTextBase, boolean>({ name: "editable", defaultValue: true, valueConverter: booleanConverter });
editableProperty.register(EditableTextBase);
export const updateTextTriggerProperty = new Property<EditableTextBase, UpdateTextTrigger>({ name: "updateTextTrigger", defaultValue: "textChanged" });
updateTextTriggerProperty.register(EditableTextBase);
const autocapitalizationTypeConverter = makeParser<AutocapitalizationType>(makeValidator<AutocapitalizationType>("none", "words", "sentences", "allcharacters"));
export const autocapitalizationTypeProperty = new Property<EditableTextBase, AutocapitalizationType>({ name: "autocapitalizationType", defaultValue: "sentences", valueConverter: autocapitalizationTypeConverter });
autocapitalizationTypeProperty.register(EditableTextBase);
export const autocorrectProperty = new Property<EditableTextBase, boolean>({ name: "autocorrect", valueConverter: booleanConverter });
autocorrectProperty.register(EditableTextBase);
export const hintProperty = new Property<EditableTextBase, string>({ name: "hint", defaultValue: "" });
hintProperty.register(EditableTextBase);
export const maxLengthProperty = new Property<EditableTextBase, number>({ name: "maxLength", defaultValue: Number.POSITIVE_INFINITY, valueConverter: parseInt });
maxLengthProperty.register(EditableTextBase);

View File

@@ -0,0 +1,464 @@
import {
EditableTextBase as EditableTextBaseCommon, keyboardTypeProperty,
returnKeyTypeProperty, editableProperty,
autocapitalizationTypeProperty, autocorrectProperty, hintProperty, resetSymbol,
textProperty, placeholderColorProperty, Color, textTransformProperty, maxLengthProperty
} from "./editable-text-base-common";
import { ad } from "../../utils/utils";
export * from "./editable-text-base-common";
//https://github.com/NativeScript/NativeScript/issues/2942
export let dismissKeyboardTimeoutId: NodeJS.Timer;
export let dismissKeyboardOwner: WeakRef<EditableTextBase>;
interface EditTextListeners extends android.text.TextWatcher, android.view.View.OnFocusChangeListener, android.widget.TextView.OnEditorActionListener {
}
interface EditTextListenersClass {
prototype: EditTextListeners;
new(owner: EditableTextBase): EditTextListeners;
}
let EditTextListeners: EditTextListenersClass;
function clearDismissTimer(): void {
dismissKeyboardOwner = null;
if (dismissKeyboardTimeoutId) {
clearTimeout(dismissKeyboardTimeoutId);
dismissKeyboardTimeoutId = null;
}
}
function dismissSoftInput(owner: EditableTextBase): void {
clearDismissTimer();
if (!dismissKeyboardTimeoutId) {
dismissKeyboardTimeoutId = setTimeout(() => {
const owner = dismissKeyboardOwner && dismissKeyboardOwner.get();
const activity = (owner && owner._context) as androidx.appcompat.app.AppCompatActivity;
const nativeView = owner && owner.nativeViewProtected;
dismissKeyboardTimeoutId = null;
dismissKeyboardOwner = null;
const focused = activity && activity.getCurrentFocus();
if (!focused || !(focused instanceof android.widget.EditText)) {
ad.dismissSoftInput(nativeView);
}
}, 10);
}
}
function initializeEditTextListeners(): void {
if (EditTextListeners) {
return;
}
@Interfaces([android.text.TextWatcher, android.view.View.OnFocusChangeListener, android.widget.TextView.OnEditorActionListener])
class EditTextListenersImpl extends java.lang.Object implements android.text.TextWatcher, android.view.View.OnFocusChangeListener, android.widget.TextView.OnEditorActionListener {
constructor(private owner: EditableTextBase) {
super();
return global.__native(this);
}
public beforeTextChanged(text: string, start: number, count: number, after: number): void {
//
}
public onTextChanged(text: string, start: number, before: number, count: number): void {
// const owner = this.owner;
// let selectionStart = owner.android.getSelectionStart();
// owner.android.removeTextChangedListener(owner._editTextListeners);
// owner.android.addTextChangedListener(owner._editTextListeners);
// owner.android.setSelection(selectionStart);
}
public afterTextChanged(editable: android.text.Editable): void {
const owner = this.owner;
if (!owner || owner._changeFromCode) {
return;
}
switch (owner.updateTextTrigger) {
case "focusLost":
owner._dirtyTextAccumulator = editable.toString();
break;
case "textChanged":
textProperty.nativeValueChange(owner, editable.toString());
break;
default:
throw new Error("Invalid updateTextTrigger: " + owner.updateTextTrigger);
}
}
public onFocusChange(view: android.view.View, hasFocus: boolean): void {
const owner = this.owner;
if (!owner) {
return;
}
if (hasFocus) {
clearDismissTimer();
owner.notify({ eventName: EditableTextBase.focusEvent, object: owner });
} else {
if (owner._dirtyTextAccumulator || owner._dirtyTextAccumulator === "") {
textProperty.nativeValueChange(owner, owner._dirtyTextAccumulator);
owner._dirtyTextAccumulator = undefined;
}
owner.notify({ eventName: EditableTextBase.blurEvent, object: owner });
dismissSoftInput(owner);
}
}
public onEditorAction(textView: android.widget.TextView, actionId: number, event: android.view.KeyEvent): boolean {
const owner = this.owner;
if (!owner) {
return false;
}
if (actionId === android.view.inputmethod.EditorInfo.IME_ACTION_DONE ||
actionId === android.view.inputmethod.EditorInfo.IME_ACTION_UNSPECIFIED ||
(event && event.getKeyCode() === android.view.KeyEvent.KEYCODE_ENTER)) {
// If it is TextField, close the keyboard. If it is TextView, do not close it since the TextView is multiline
// https://github.com/NativeScript/NativeScript/issues/3111
if (textView.getMaxLines() === 1) {
owner.dismissSoftInput();
}
owner._onReturnPress();
} else if (actionId === android.view.inputmethod.EditorInfo.IME_ACTION_NEXT ||
actionId === android.view.inputmethod.EditorInfo.IME_ACTION_PREVIOUS) {
// do not close keyboard for ACTION_NEXT or ACTION_PREVIOUS
owner._onReturnPress();
}
return false;
}
}
EditTextListeners = EditTextListenersImpl;
}
export abstract class EditableTextBase extends EditableTextBaseCommon {
/* tslint:disable */
_dirtyTextAccumulator: string;
/* tslint:enable */
nativeViewProtected: android.widget.EditText;
private _keyListenerCache: android.text.method.KeyListener;
private _inputType: number;
public _changeFromCode: boolean;
public abstract _configureEditText(editText: android.widget.EditText): void;
public _onReturnPress(): void {
//
}
public createNativeView() {
return new android.widget.EditText(this._context);
}
public initNativeView(): void {
super.initNativeView();
const editText = this.nativeTextViewProtected;
this._configureEditText(editText);
initializeEditTextListeners();
const listeners = new EditTextListeners(this);
editText.addTextChangedListener(listeners);
editText.setOnFocusChangeListener(listeners);
editText.setOnEditorActionListener(listeners);
(<any>editText).listener = listeners;
this._inputType = editText.getInputType();
}
public disposeNativeView(): void {
(<any>this.nativeTextViewProtected).listener.owner = null;
this._keyListenerCache = null;
super.disposeNativeView();
}
public resetNativeView(): void {
super.resetNativeView();
this.nativeTextViewProtected.setInputType(this._inputType);
}
public onUnloaded() {
this.dismissSoftInput();
super.onUnloaded();
}
public dismissSoftInput() {
const nativeView = this.nativeTextViewProtected;
if (!nativeView) {
return;
}
ad.dismissSoftInput(nativeView);
}
public focus(): boolean {
const nativeView = this.nativeTextViewProtected;
if (!nativeView) {
return;
}
const result = super.focus();
if (result) {
ad.showSoftInput(this.nativeTextViewProtected);
}
return result;
}
public _setInputType(inputType: number): void {
const nativeView = this.nativeTextViewProtected;
try {
this._changeFromCode = true;
nativeView.setInputType(inputType);
} finally {
this._changeFromCode = false;
}
// setInputType will change the keyListener so we should cache it again
const listener = nativeView.getKeyListener();
if (listener) {
this._keyListenerCache = listener;
}
// clear the listener if editable is false
if (!this.editable) {
nativeView.setKeyListener(null);
}
}
[textProperty.getDefault](): number | symbol {
return resetSymbol;
}
[textProperty.setNative](value: string | number | symbol) {
try {
this._changeFromCode = true;
this._setNativeText(value === resetSymbol);
} finally {
this._changeFromCode = false;
}
}
[keyboardTypeProperty.getDefault](): number {
return this.nativeTextViewProtected.getInputType();
}
[keyboardTypeProperty.setNative](value: "datetime" | "phone" | "number" | "url" | "email" | "integer" | number) {
let newInputType;
switch (value) {
case "datetime":
newInputType = android.text.InputType.TYPE_CLASS_DATETIME | android.text.InputType.TYPE_DATETIME_VARIATION_NORMAL;
break;
case "phone":
newInputType = android.text.InputType.TYPE_CLASS_PHONE;
break;
case "number":
newInputType = android.text.InputType.TYPE_CLASS_NUMBER | android.text.InputType.TYPE_NUMBER_VARIATION_NORMAL | android.text.InputType.TYPE_NUMBER_FLAG_SIGNED | android.text.InputType.TYPE_NUMBER_FLAG_DECIMAL;
break;
case "url":
newInputType = android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_URI;
break;
case "email":
newInputType = android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
break;
case "integer":
newInputType = android.text.InputType.TYPE_CLASS_NUMBER;
break;
default:
newInputType = value;
break;
}
this._setInputType(newInputType);
}
[returnKeyTypeProperty.getDefault](): "done" | "next" | "go" | "search" | "send" | string {
let ime = this.nativeTextViewProtected.getImeOptions();
switch (ime) {
case android.view.inputmethod.EditorInfo.IME_ACTION_DONE:
return "done";
case android.view.inputmethod.EditorInfo.IME_ACTION_GO:
return "go";
case android.view.inputmethod.EditorInfo.IME_ACTION_NEXT:
return "next";
case android.view.inputmethod.EditorInfo.IME_ACTION_SEARCH:
return "search";
case android.view.inputmethod.EditorInfo.IME_ACTION_SEND:
return "send";
default:
return ime.toString();
}
}
[returnKeyTypeProperty.setNative](value: "done" | "next" | "go" | "search" | "send" | string) {
let newImeOptions;
switch (value) {
case "done":
newImeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_DONE;
break;
case "go":
newImeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_GO;
break;
case "next":
newImeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_NEXT;
break;
case "search":
newImeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_SEARCH;
break;
case "send":
newImeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_SEND;
break;
default:
let ime = +value;
if (!isNaN(ime)) {
newImeOptions = ime;
} else {
newImeOptions = android.view.inputmethod.EditorInfo.IME_ACTION_UNSPECIFIED;
}
break;
}
this.nativeTextViewProtected.setImeOptions(newImeOptions);
}
[editableProperty.setNative](value: boolean) {
const nativeView = this.nativeTextViewProtected;
if (value) {
nativeView.setKeyListener(this._keyListenerCache);
} else {
if (!this._keyListenerCache) {
this._keyListenerCache = nativeView.getKeyListener();
}
nativeView.setKeyListener(null);
}
}
[autocapitalizationTypeProperty.getDefault](): "none" | "words" | "sentences" | "allcharacters" | string {
let inputType = this.nativeTextViewProtected.getInputType();
if ((inputType & android.text.InputType.TYPE_TEXT_FLAG_CAP_WORDS) === android.text.InputType.TYPE_TEXT_FLAG_CAP_WORDS) {
return "words";
} else if ((inputType & android.text.InputType.TYPE_TEXT_FLAG_CAP_SENTENCES) === android.text.InputType.TYPE_TEXT_FLAG_CAP_SENTENCES) {
return "sentences";
} else if ((inputType & android.text.InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS) === android.text.InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS) {
return "allcharacters";
} else {
return inputType.toString();
}
}
[autocapitalizationTypeProperty.setNative](value: string) {
let inputType = this.nativeTextViewProtected.getInputType();
inputType = inputType & ~28672; //28672 (0x00070000) 13,14,15bits (111 0000 0000 0000)
switch (value) {
case "none":
//Do nothing, we have lowered the three bits above.
break;
case "words":
inputType = inputType | android.text.InputType.TYPE_TEXT_FLAG_CAP_WORDS; //8192 (0x00020000) 14th bit
break;
case "sentences":
inputType = inputType | android.text.InputType.TYPE_TEXT_FLAG_CAP_SENTENCES; //16384(0x00040000) 15th bit
break;
case "allcharacters":
inputType = inputType | android.text.InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS; //4096 (0x00010000) 13th bit
break;
default:
let number = +value;
// We set the default value.
if (!isNaN(number)) {
inputType = number;
} else {
inputType = inputType | android.text.InputType.TYPE_TEXT_FLAG_CAP_SENTENCES;
}
break;
}
this._setInputType(inputType);
}
[autocorrectProperty.getDefault](): boolean {
let autocorrect = this.nativeTextViewProtected.getInputType();
if ((autocorrect & android.text.InputType.TYPE_TEXT_FLAG_AUTO_CORRECT) === android.text.InputType.TYPE_TEXT_FLAG_AUTO_CORRECT) {
return true;
}
return false;
}
[autocorrectProperty.setNative](value: boolean) {
let inputType = this.nativeTextViewProtected.getInputType();
switch (value) {
case true:
inputType = inputType | android.text.InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE;
inputType = inputType | android.text.InputType.TYPE_TEXT_FLAG_AUTO_CORRECT;
inputType = inputType & ~android.text.InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
break;
case false:
inputType = inputType & ~android.text.InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE;
inputType = inputType & ~android.text.InputType.TYPE_TEXT_FLAG_AUTO_CORRECT;
inputType = inputType | android.text.InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
break;
default:
// We can't do anything.
break;
}
this._setInputType(inputType);
}
[hintProperty.getDefault](): string {
return this.nativeTextViewProtected.getHint();
}
[hintProperty.setNative](value: string) {
const text = (value === null || value === undefined) ? null : value.toString();
this.nativeTextViewProtected.setHint(text);
}
[placeholderColorProperty.getDefault](): android.content.res.ColorStateList {
return this.nativeTextViewProtected.getHintTextColors();
}
[placeholderColorProperty.setNative](value: Color | android.content.res.ColorStateList) {
const color = value instanceof Color ? value.android : value;
this.nativeTextViewProtected.setHintTextColor(<any>color);
}
[textTransformProperty.setNative](value: "default") {
//
}
[maxLengthProperty.setNative](value: number) {
if (value === Number.POSITIVE_INFINITY) {
this.nativeTextViewProtected.setFilters([]);
} else {
const lengthFilter = new android.text.InputFilter.LengthFilter(value);
const filters = this.nativeTextViewProtected.getFilters();
const newFilters = [];
// retain existing filters
for (let i = 0; i < filters.length; i++) {
const filter = filters[i];
if (!(filter instanceof android.text.InputFilter.LengthFilter)) {
newFilters.push(filter);
}
}
newFilters.push(lengthFilter);
this.nativeTextViewProtected.setFilters(newFilters);
}
}
}

View File

@@ -0,0 +1,90 @@
/**
* @module "ui/editor-text-base"
*/ /** */
import { TextBase, Property, CssProperty, Style, Color, FormattedString } from "../text-base";
/**
* Represents the base class for all editable text views.
*/
export class EditableTextBase extends TextBase {
public static blurEvent: string;
public static focusEvent: string;
public static textChangeEvent: string;
/**
* Gets or sets the soft keyboard type.
*/
keyboardType: KeyboardType;
/**
* Gets or sets the soft keyboard return key flavor.
*/
returnKeyType: ReturnKeyType;
/**
* Gets or sets a value indicating when the text property will be updated.
*/
updateTextTrigger: UpdateTextTrigger;
/**
* Gets or sets the autocapitalization type.
*/
autocapitalizationType: AutocapitalizationType;
/**
* Gets or sets whether the instance is editable.
*/
editable: boolean;
/**
* Enables or disables autocorrection.
*/
autocorrect: boolean;
/**
* Gets or sets the placeholder text.
*/
hint: string;
/**
* Limits input to a certain number of characters.
*/
maxLength: number;
/**
* Hides the soft input method, ususally a soft keyboard.
*/
dismissSoftInput(): void;
//@private
/**
* @private
*/
public _setInputType(inputType: number): void;
//@endprivate
}
export type KeyboardType = "datetime" | "phone" | "number" | "url" | "email" | "integer";
export type ReturnKeyType = "done" | "next" | "go" | "search" | "send";
export type UpdateTextTrigger = "focusLost" | "textChanged";
export type AutocapitalizationType = "none" | "words" | "sentences" | "allcharacters";
export const keyboardTypeProperty: Property<EditableTextBase, KeyboardType>;
export const returnKeyTypeProperty: Property<EditableTextBase, ReturnKeyType>;
export const editableProperty: Property<EditableTextBase, boolean>;
export const updateTextTriggerProperty: Property<EditableTextBase, UpdateTextTrigger>;
export const autocapitalizationTypeProperty: Property<EditableTextBase, AutocapitalizationType>;
export const autocorrectProperty: Property<EditableTextBase, boolean>;
export const hintProperty: Property<EditableTextBase, string>;
export const placeholderColorProperty: CssProperty<Style, Color>;
export const maxLengthProperty: Property<EditableTextBase, number>;
//@private
/**
* @private
*/
export function _updateCharactersInRangeReplacementString(formattedText: FormattedString, rangeLocation: number, rangeLength: number, replacementString: string): void;
//@endprivate
export * from "../text-base";

View File

@@ -0,0 +1,222 @@
import {
EditableTextBase as EditableTextBaseCommon, keyboardTypeProperty,
returnKeyTypeProperty,
autocapitalizationTypeProperty, autocorrectProperty, FormattedString
} from "./editable-text-base-common";
export * from "./editable-text-base-common";
export abstract class EditableTextBase extends EditableTextBaseCommon {
public nativeViewProtected: UITextField | UITextView;
public dismissSoftInput() {
this.nativeTextViewProtected.resignFirstResponder();
this.notify({ eventName: EditableTextBase.blurEvent, object: this });
}
[keyboardTypeProperty.getDefault](): "datetime" | "phone" | "number" | "url" | "email" | "integer" | string {
let keyboardType = this.nativeTextViewProtected.keyboardType;
switch (keyboardType) {
case UIKeyboardType.NumbersAndPunctuation:
return "number";
case UIKeyboardType.PhonePad:
return "phone";
case UIKeyboardType.URL:
return "url";
case UIKeyboardType.EmailAddress:
return "email";
case UIKeyboardType.NumberPad:
return "integer";
default:
return keyboardType.toString();
}
}
[keyboardTypeProperty.setNative](value: "datetime" | "phone" | "number" | "url" | "email" | "integer" | string) {
let newKeyboardType: UIKeyboardType;
switch (value) {
case "datetime":
newKeyboardType = UIKeyboardType.NumbersAndPunctuation;
break;
case "phone":
newKeyboardType = UIKeyboardType.PhonePad;
break;
case "number":
newKeyboardType = UIKeyboardType.NumbersAndPunctuation;
break;
case "url":
newKeyboardType = UIKeyboardType.URL;
break
;
case "email":
newKeyboardType = UIKeyboardType.EmailAddress;
break;
case "integer":
newKeyboardType = UIKeyboardType.NumberPad;
break;
default:
let kt = +value;
if (!isNaN(kt)) {
newKeyboardType = <UIKeyboardType>kt;
} else {
newKeyboardType = UIKeyboardType.Default;
}
break;
}
this.nativeTextViewProtected.keyboardType = newKeyboardType;
}
[returnKeyTypeProperty.getDefault](): "done" | "next" | "go" | "search" | "send" | string {
let returnKeyType = this.nativeTextViewProtected.returnKeyType;
switch (returnKeyType) {
case UIReturnKeyType.Done:
return "done";
case UIReturnKeyType.Go:
return "go";
case UIReturnKeyType.Next:
return "next";
case UIReturnKeyType.Search:
return "search";
case UIReturnKeyType.Send:
return "send";
default:
return returnKeyType.toString();
}
}
[returnKeyTypeProperty.setNative](value: "done" | "next" | "go" | "search" | "send" | string) {
let newValue;
switch (value) {
case "done":
newValue = UIReturnKeyType.Done;
break;
case "go":
newValue = UIReturnKeyType.Go;
break;
case "next":
newValue = UIReturnKeyType.Next;
break;
case "search":
newValue = UIReturnKeyType.Search;
break;
case "send":
newValue = UIReturnKeyType.Send;
break;
default:
let rkt = +value;
if (!isNaN(rkt)) {
newValue = <UIKeyboardType>rkt;
} else {
newValue = UIKeyboardType.Default;
}
break;
}
this.nativeTextViewProtected.returnKeyType = newValue;
}
[autocapitalizationTypeProperty.getDefault](): "none" | "words" | "sentences" | "allcharacters" {
let autocapitalizationType = this.nativeTextViewProtected.autocapitalizationType;
switch (autocapitalizationType) {
case UITextAutocapitalizationType.None:
return "none";
case UITextAutocapitalizationType.Words:
return "words";
case UITextAutocapitalizationType.Sentences:
return "sentences";
case UITextAutocapitalizationType.AllCharacters:
return "allcharacters";
default:
throw new Error("Invalid autocapitalizationType value:" + autocapitalizationType);
}
}
[autocapitalizationTypeProperty.setNative](value: "none" | "words" | "sentences" | "allcharacters") {
let newValue: UITextAutocapitalizationType;
switch (value) {
case "none":
newValue = UITextAutocapitalizationType.None;
break;
case "words":
newValue = UITextAutocapitalizationType.Words;
break;
case "sentences":
newValue = UITextAutocapitalizationType.Sentences;
break;
case "allcharacters":
newValue = UITextAutocapitalizationType.AllCharacters;
break;
default:
newValue = UITextAutocapitalizationType.Sentences;
break;
}
this.nativeTextViewProtected.autocapitalizationType = newValue;
}
[autocorrectProperty.getDefault](): boolean | number {
let autocorrectionType = this.nativeTextViewProtected.autocorrectionType;
switch (autocorrectionType) {
case UITextAutocorrectionType.Yes:
return true;
case UITextAutocorrectionType.No:
return false;
case UITextAutocorrectionType.Default:
return autocorrectionType;
}
}
[autocorrectProperty.setNative](value: boolean | number) {
let newValue: UITextAutocorrectionType;
if (typeof value === "number") {
newValue = UITextAutocorrectionType.Default;
} else if (value) {
newValue = UITextAutocorrectionType.Yes;
} else {
newValue = UITextAutocorrectionType.No;
}
this.nativeTextViewProtected.autocorrectionType = newValue;
}
}
export function _updateCharactersInRangeReplacementString(formattedText: FormattedString, rangeLocation: number, rangeLength: number, replacementString: string): void {
let deletingText = !replacementString;
let currentLocation = 0;
for (let i = 0, length = formattedText.spans.length; i < length; i++) {
let span = formattedText.spans.getItem(i);
if (currentLocation <= rangeLocation && rangeLocation < (currentLocation + span.text.length)) {
let newText = splice(span.text, rangeLocation - currentLocation, deletingText ? rangeLength : 0, replacementString);
span._setTextInternal(newText);
return;
}
currentLocation += span.text.length;
}
}
/*
* @param {String} value The string to splice.
* @param {number} start Index at which to start changing the string.
* @param {number} delCount An integer indicating the number of old chars to remove.
* @param {string} newSubStr The String that is spliced in.
* @return {string} A new string with the spliced substring.function splice(value: string, start: number, delCount: number, newSubStr: string) {
*/
function splice(value: string, start: number, delCount: number, newSubStr: string) {
return value.slice(0, start) + newSubStr + value.slice(start + Math.abs(delCount));
}

View File

@@ -0,0 +1,5 @@
{
"name": "editable-text-base",
"main": "editable-text-base",
"types": "editable-text-base.d.ts"
}