ui.text_editor.mask.strategy.android.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /**
  2. * DevExtreme (ui/text_box/ui.text_editor.mask.strategy.android.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. function _typeof(obj) {
  11. "@babel/helpers - typeof";
  12. return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(obj) {
  13. return typeof obj
  14. } : function(obj) {
  15. return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj
  16. }, _typeof(obj)
  17. }
  18. Object.defineProperty(exports, "__esModule", {
  19. value: true
  20. });
  21. var _uiText_editorMaskStrategy = require("./ui.text_editor.mask.strategy.base");
  22. var _uiText_editorMaskStrategy2 = _interopRequireDefault(_uiText_editorMaskStrategy);
  23. function _interopRequireDefault(obj) {
  24. return obj && obj.__esModule ? obj : {
  25. "default": obj
  26. }
  27. }
  28. function _toConsumableArray(arr) {
  29. return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread()
  30. }
  31. function _nonIterableSpread() {
  32. throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
  33. }
  34. function _unsupportedIterableToArray(o, minLen) {
  35. if (!o) {
  36. return
  37. }
  38. if ("string" === typeof o) {
  39. return _arrayLikeToArray(o, minLen)
  40. }
  41. var n = Object.prototype.toString.call(o).slice(8, -1);
  42. if ("Object" === n && o.constructor) {
  43. n = o.constructor.name
  44. }
  45. if ("Map" === n || "Set" === n) {
  46. return Array.from(o)
  47. }
  48. if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) {
  49. return _arrayLikeToArray(o, minLen)
  50. }
  51. }
  52. function _iterableToArray(iter) {
  53. if ("undefined" !== typeof Symbol && null != iter[Symbol.iterator] || null != iter["@@iterator"]) {
  54. return Array.from(iter)
  55. }
  56. }
  57. function _arrayWithoutHoles(arr) {
  58. if (Array.isArray(arr)) {
  59. return _arrayLikeToArray(arr)
  60. }
  61. }
  62. function _arrayLikeToArray(arr, len) {
  63. if (null == len || len > arr.length) {
  64. len = arr.length
  65. }
  66. for (var i = 0, arr2 = new Array(len); i < len; i++) {
  67. arr2[i] = arr[i]
  68. }
  69. return arr2
  70. }
  71. function _classCallCheck(instance, Constructor) {
  72. if (!(instance instanceof Constructor)) {
  73. throw new TypeError("Cannot call a class as a function")
  74. }
  75. }
  76. function _defineProperties(target, props) {
  77. for (var i = 0; i < props.length; i++) {
  78. var descriptor = props[i];
  79. descriptor.enumerable = descriptor.enumerable || false;
  80. descriptor.configurable = true;
  81. if ("value" in descriptor) {
  82. descriptor.writable = true
  83. }
  84. Object.defineProperty(target, descriptor.key, descriptor)
  85. }
  86. }
  87. function _createClass(Constructor, protoProps, staticProps) {
  88. if (protoProps) {
  89. _defineProperties(Constructor.prototype, protoProps)
  90. }
  91. if (staticProps) {
  92. _defineProperties(Constructor, staticProps)
  93. }
  94. Object.defineProperty(Constructor, "prototype", {
  95. writable: false
  96. });
  97. return Constructor
  98. }
  99. function _get() {
  100. if ("undefined" !== typeof Reflect && Reflect.get) {
  101. _get = Reflect.get.bind()
  102. } else {
  103. _get = function(target, property, receiver) {
  104. var base = _superPropBase(target, property);
  105. if (!base) {
  106. return
  107. }
  108. var desc = Object.getOwnPropertyDescriptor(base, property);
  109. if (desc.get) {
  110. return desc.get.call(arguments.length < 3 ? target : receiver)
  111. }
  112. return desc.value
  113. }
  114. }
  115. return _get.apply(this, arguments)
  116. }
  117. function _superPropBase(object, property) {
  118. while (!Object.prototype.hasOwnProperty.call(object, property)) {
  119. object = _getPrototypeOf(object);
  120. if (null === object) {
  121. break
  122. }
  123. }
  124. return object
  125. }
  126. function _inherits(subClass, superClass) {
  127. if ("function" !== typeof superClass && null !== superClass) {
  128. throw new TypeError("Super expression must either be null or a function")
  129. }
  130. subClass.prototype = Object.create(superClass && superClass.prototype, {
  131. constructor: {
  132. value: subClass,
  133. writable: true,
  134. configurable: true
  135. }
  136. });
  137. Object.defineProperty(subClass, "prototype", {
  138. writable: false
  139. });
  140. if (superClass) {
  141. _setPrototypeOf(subClass, superClass)
  142. }
  143. }
  144. function _setPrototypeOf(o, p) {
  145. _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
  146. o.__proto__ = p;
  147. return o
  148. };
  149. return _setPrototypeOf(o, p)
  150. }
  151. function _createSuper(Derived) {
  152. var hasNativeReflectConstruct = _isNativeReflectConstruct();
  153. return function() {
  154. var result, Super = _getPrototypeOf(Derived);
  155. if (hasNativeReflectConstruct) {
  156. var NewTarget = _getPrototypeOf(this).constructor;
  157. result = Reflect.construct(Super, arguments, NewTarget)
  158. } else {
  159. result = Super.apply(this, arguments)
  160. }
  161. return _possibleConstructorReturn(this, result)
  162. }
  163. }
  164. function _possibleConstructorReturn(self, call) {
  165. if (call && ("object" === _typeof(call) || "function" === typeof call)) {
  166. return call
  167. } else {
  168. if (void 0 !== call) {
  169. throw new TypeError("Derived constructors may only return object or undefined")
  170. }
  171. }
  172. return _assertThisInitialized(self)
  173. }
  174. function _assertThisInitialized(self) {
  175. if (void 0 === self) {
  176. throw new ReferenceError("this hasn't been initialised - super() hasn't been called")
  177. }
  178. return self
  179. }
  180. function _isNativeReflectConstruct() {
  181. if ("undefined" === typeof Reflect || !Reflect.construct) {
  182. return false
  183. }
  184. if (Reflect.construct.sham) {
  185. return false
  186. }
  187. if ("function" === typeof Proxy) {
  188. return true
  189. }
  190. try {
  191. Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
  192. return true
  193. } catch (e) {
  194. return false
  195. }
  196. }
  197. function _getPrototypeOf(o) {
  198. _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function(o) {
  199. return o.__proto__ || Object.getPrototypeOf(o)
  200. };
  201. return _getPrototypeOf(o)
  202. }
  203. var DELETE_INPUT_TYPE = "deleteContentBackward";
  204. var AndroidMaskStrategy = function(_BaseMaskStrategy) {
  205. _inherits(AndroidMaskStrategy, _BaseMaskStrategy);
  206. var _super = _createSuper(AndroidMaskStrategy);
  207. function AndroidMaskStrategy() {
  208. _classCallCheck(this, AndroidMaskStrategy);
  209. return _super.apply(this, arguments)
  210. }
  211. _createClass(AndroidMaskStrategy, [{
  212. key: "_getStrategyName",
  213. value: function() {
  214. return "android"
  215. }
  216. }, {
  217. key: "getHandleEventNames",
  218. value: function() {
  219. return [].concat(_toConsumableArray(_get(_getPrototypeOf(AndroidMaskStrategy.prototype), "getHandleEventNames", this).call(this)), ["beforeInput"])
  220. }
  221. }, {
  222. key: "_beforeInputHandler",
  223. value: function() {
  224. this._prevCaret = this.editorCaret()
  225. }
  226. }, {
  227. key: "_inputHandler",
  228. value: function(_ref) {
  229. var originalEvent = _ref.originalEvent;
  230. if (!originalEvent) {
  231. return
  232. }
  233. var inputType = originalEvent.inputType,
  234. data = originalEvent.data;
  235. var currentCaret = this.editorCaret();
  236. if (inputType === DELETE_INPUT_TYPE) {
  237. var length = this._prevCaret.end - this._prevCaret.start || 1;
  238. this.editor.setBackwardDirection();
  239. this._updateEditorMask({
  240. start: currentCaret.start,
  241. length: length,
  242. text: this._getEmptyString(length)
  243. })
  244. } else {
  245. if (!currentCaret.end) {
  246. return
  247. }
  248. this._autoFillHandler(originalEvent);
  249. this.editorCaret(currentCaret);
  250. var _length = this._prevCaret.end - this._prevCaret.start;
  251. var newData = data + (_length ? this._getEmptyString(_length - data.length) : "");
  252. this.editor.setForwardDirection();
  253. var hasValidChars = this._updateEditorMask({
  254. start: this._prevCaret.start,
  255. length: _length || newData.length,
  256. text: newData
  257. });
  258. if (!hasValidChars) {
  259. this.editorCaret(this._prevCaret)
  260. }
  261. }
  262. }
  263. }, {
  264. key: "_getEmptyString",
  265. value: function(length) {
  266. return Array(length + 1).join(" ")
  267. }
  268. }, {
  269. key: "_updateEditorMask",
  270. value: function(args) {
  271. var textLength = args.text.length;
  272. var updatedCharsCount = this.editor._handleChain(args);
  273. if (this.editor.isForwardDirection()) {
  274. var _this$editorCaret = this.editorCaret(),
  275. start = _this$editorCaret.start,
  276. end = _this$editorCaret.end;
  277. var correction = updatedCharsCount - textLength;
  278. if (start <= updatedCharsCount && updatedCharsCount > 1) {
  279. this.editorCaret({
  280. start: start + correction,
  281. end: end + correction
  282. })
  283. }
  284. this.editor.isForwardDirection() && this.editor._adjustCaret()
  285. }
  286. this.editor._displayMask();
  287. return !!updatedCharsCount
  288. }
  289. }]);
  290. return AndroidMaskStrategy
  291. }(_uiText_editorMaskStrategy2.default);
  292. exports.default = AndroidMaskStrategy;