ui.text_editor.mask.strategy.default.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /**
  2. * DevExtreme (ui/text_box/ui.text_editor.mask.strategy.default.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. var _utils = require("../../events/utils");
  24. var _promise = require("../../core/polyfills/promise");
  25. var _promise2 = _interopRequireDefault(_promise);
  26. function _interopRequireDefault(obj) {
  27. return obj && obj.__esModule ? obj : {
  28. "default": obj
  29. }
  30. }
  31. function _toConsumableArray(arr) {
  32. return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread()
  33. }
  34. function _nonIterableSpread() {
  35. throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
  36. }
  37. function _unsupportedIterableToArray(o, minLen) {
  38. if (!o) {
  39. return
  40. }
  41. if ("string" === typeof o) {
  42. return _arrayLikeToArray(o, minLen)
  43. }
  44. var n = Object.prototype.toString.call(o).slice(8, -1);
  45. if ("Object" === n && o.constructor) {
  46. n = o.constructor.name
  47. }
  48. if ("Map" === n || "Set" === n) {
  49. return Array.from(o)
  50. }
  51. if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) {
  52. return _arrayLikeToArray(o, minLen)
  53. }
  54. }
  55. function _iterableToArray(iter) {
  56. if ("undefined" !== typeof Symbol && null != iter[Symbol.iterator] || null != iter["@@iterator"]) {
  57. return Array.from(iter)
  58. }
  59. }
  60. function _arrayWithoutHoles(arr) {
  61. if (Array.isArray(arr)) {
  62. return _arrayLikeToArray(arr)
  63. }
  64. }
  65. function _arrayLikeToArray(arr, len) {
  66. if (null == len || len > arr.length) {
  67. len = arr.length
  68. }
  69. for (var i = 0, arr2 = new Array(len); i < len; i++) {
  70. arr2[i] = arr[i]
  71. }
  72. return arr2
  73. }
  74. function _classCallCheck(instance, Constructor) {
  75. if (!(instance instanceof Constructor)) {
  76. throw new TypeError("Cannot call a class as a function")
  77. }
  78. }
  79. function _defineProperties(target, props) {
  80. for (var i = 0; i < props.length; i++) {
  81. var descriptor = props[i];
  82. descriptor.enumerable = descriptor.enumerable || false;
  83. descriptor.configurable = true;
  84. if ("value" in descriptor) {
  85. descriptor.writable = true
  86. }
  87. Object.defineProperty(target, descriptor.key, descriptor)
  88. }
  89. }
  90. function _createClass(Constructor, protoProps, staticProps) {
  91. if (protoProps) {
  92. _defineProperties(Constructor.prototype, protoProps)
  93. }
  94. if (staticProps) {
  95. _defineProperties(Constructor, staticProps)
  96. }
  97. Object.defineProperty(Constructor, "prototype", {
  98. writable: false
  99. });
  100. return Constructor
  101. }
  102. function _get() {
  103. if ("undefined" !== typeof Reflect && Reflect.get) {
  104. _get = Reflect.get.bind()
  105. } else {
  106. _get = function(target, property, receiver) {
  107. var base = _superPropBase(target, property);
  108. if (!base) {
  109. return
  110. }
  111. var desc = Object.getOwnPropertyDescriptor(base, property);
  112. if (desc.get) {
  113. return desc.get.call(arguments.length < 3 ? target : receiver)
  114. }
  115. return desc.value
  116. }
  117. }
  118. return _get.apply(this, arguments)
  119. }
  120. function _superPropBase(object, property) {
  121. while (!Object.prototype.hasOwnProperty.call(object, property)) {
  122. object = _getPrototypeOf(object);
  123. if (null === object) {
  124. break
  125. }
  126. }
  127. return object
  128. }
  129. function _inherits(subClass, superClass) {
  130. if ("function" !== typeof superClass && null !== superClass) {
  131. throw new TypeError("Super expression must either be null or a function")
  132. }
  133. subClass.prototype = Object.create(superClass && superClass.prototype, {
  134. constructor: {
  135. value: subClass,
  136. writable: true,
  137. configurable: true
  138. }
  139. });
  140. Object.defineProperty(subClass, "prototype", {
  141. writable: false
  142. });
  143. if (superClass) {
  144. _setPrototypeOf(subClass, superClass)
  145. }
  146. }
  147. function _setPrototypeOf(o, p) {
  148. _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
  149. o.__proto__ = p;
  150. return o
  151. };
  152. return _setPrototypeOf(o, p)
  153. }
  154. function _createSuper(Derived) {
  155. var hasNativeReflectConstruct = _isNativeReflectConstruct();
  156. return function() {
  157. var result, Super = _getPrototypeOf(Derived);
  158. if (hasNativeReflectConstruct) {
  159. var NewTarget = _getPrototypeOf(this).constructor;
  160. result = Reflect.construct(Super, arguments, NewTarget)
  161. } else {
  162. result = Super.apply(this, arguments)
  163. }
  164. return _possibleConstructorReturn(this, result)
  165. }
  166. }
  167. function _possibleConstructorReturn(self, call) {
  168. if (call && ("object" === _typeof(call) || "function" === typeof call)) {
  169. return call
  170. } else {
  171. if (void 0 !== call) {
  172. throw new TypeError("Derived constructors may only return object or undefined")
  173. }
  174. }
  175. return _assertThisInitialized(self)
  176. }
  177. function _assertThisInitialized(self) {
  178. if (void 0 === self) {
  179. throw new ReferenceError("this hasn't been initialised - super() hasn't been called")
  180. }
  181. return self
  182. }
  183. function _isNativeReflectConstruct() {
  184. if ("undefined" === typeof Reflect || !Reflect.construct) {
  185. return false
  186. }
  187. if (Reflect.construct.sham) {
  188. return false
  189. }
  190. if ("function" === typeof Proxy) {
  191. return true
  192. }
  193. try {
  194. Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
  195. return true
  196. } catch (e) {
  197. return false
  198. }
  199. }
  200. function _getPrototypeOf(o) {
  201. _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function(o) {
  202. return o.__proto__ || Object.getPrototypeOf(o)
  203. };
  204. return _getPrototypeOf(o)
  205. }
  206. var BACKSPACE_INPUT_TYPE = "deleteContentBackward";
  207. var EMPTY_CHAR = " ";
  208. var DefaultMaskStrategy = function(_BaseMaskStrategy) {
  209. _inherits(DefaultMaskStrategy, _BaseMaskStrategy);
  210. var _super = _createSuper(DefaultMaskStrategy);
  211. function DefaultMaskStrategy() {
  212. _classCallCheck(this, DefaultMaskStrategy);
  213. return _super.apply(this, arguments)
  214. }
  215. _createClass(DefaultMaskStrategy, [{
  216. key: "_getStrategyName",
  217. value: function() {
  218. return "default"
  219. }
  220. }, {
  221. key: "getHandleEventNames",
  222. value: function() {
  223. return [].concat(_toConsumableArray(_get(_getPrototypeOf(DefaultMaskStrategy.prototype), "getHandleEventNames", this).call(this)), ["keyPress"])
  224. }
  225. }, {
  226. key: "_keyPressHandler",
  227. value: function(event) {
  228. if (this._keyPressHandled) {
  229. return
  230. }
  231. this._keyPressHandled = true;
  232. if (this.editor._isControlKeyFired(event)) {
  233. return
  234. }
  235. var editor = this.editor;
  236. editor._maskKeyHandler(event, function() {
  237. return editor._handleKey((0, _utils.getChar)(event))
  238. })
  239. }
  240. }, {
  241. key: "_inputHandler",
  242. value: function(event) {
  243. if (this._backspaceInputHandled(event.originalEvent && event.originalEvent.inputType)) {
  244. this._handleBackspaceInput(event)
  245. }
  246. if (event.originalEvent) {
  247. this._autoFillHandler(event)
  248. }
  249. if (this._keyPressHandled) {
  250. return
  251. }
  252. this._keyPressHandled = true;
  253. var inputValue = this.editorInput().val();
  254. var caret = this.editorCaret();
  255. if (!caret.end) {
  256. return
  257. }
  258. caret.start = caret.end - 1;
  259. var oldValue = inputValue.substring(0, caret.start) + inputValue.substring(caret.end);
  260. var char = inputValue[caret.start];
  261. var editor = this.editor;
  262. this.editorInput().val(oldValue);
  263. editor._caret({
  264. start: caret.start,
  265. end: caret.start
  266. });
  267. editor._maskKeyHandler(event, function() {
  268. return editor._handleKey(char)
  269. })
  270. }
  271. }, {
  272. key: "_backspaceHandler",
  273. value: function(event) {
  274. var _this = this;
  275. var editor = this.editor;
  276. this._keyPressHandled = true;
  277. var afterBackspaceHandler = function(needAdjustCaret, callBack) {
  278. if (needAdjustCaret) {
  279. editor._direction(_this.DIRECTION.FORWARD);
  280. editor._adjustCaret()
  281. }
  282. var currentCaret = _this.editorCaret();
  283. return new _promise2.default(function(resolve) {
  284. clearTimeout(_this._backspaceHandlerTimeout);
  285. _this._backspaceHandlerTimeout = setTimeout(function() {
  286. callBack(currentCaret);
  287. resolve()
  288. })
  289. })
  290. };
  291. editor._maskKeyHandler(event, function() {
  292. if (editor._hasSelection()) {
  293. return afterBackspaceHandler(true, function(currentCaret) {
  294. editor._displayMask(currentCaret);
  295. editor._maskRulesChain.reset()
  296. })
  297. }
  298. if (editor._tryMoveCaretBackward()) {
  299. return afterBackspaceHandler(false, function(currentCaret) {
  300. _this.editorCaret(currentCaret)
  301. })
  302. }
  303. editor._handleKey(EMPTY_CHAR, _this.DIRECTION.BACKWARD);
  304. return afterBackspaceHandler(true, function(currentCaret) {
  305. editor._displayMask(currentCaret);
  306. editor._maskRulesChain.reset()
  307. })
  308. })
  309. }
  310. }, {
  311. key: "_backspaceInputHandled",
  312. value: function(inputType) {
  313. return inputType === BACKSPACE_INPUT_TYPE && !this._keyPressHandled
  314. }
  315. }, {
  316. key: "_handleBackspaceInput",
  317. value: function(event) {
  318. var _this$editorCaret = this.editorCaret(),
  319. start = _this$editorCaret.start,
  320. end = _this$editorCaret.end;
  321. this.editorCaret({
  322. start: start + 1,
  323. end: end + 1
  324. });
  325. this._backspaceHandler(event)
  326. }
  327. }]);
  328. return DefaultMaskStrategy
  329. }(_uiText_editorMaskStrategy2.default);
  330. exports.default = DefaultMaskStrategy;