UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

57 lines (56 loc) 1.58 kB
/** * DevExtreme (esm/__internal/ui/text_box/utils.caret.js) * Version: 25.2.7 * Build date: Tue May 05 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import devices from "../../../core/devices"; import domAdapter from "../../../core/dom_adapter"; import $ from "../../../core/renderer"; import { isDefined } from "../../../core/utils/type"; const { ios: ios, mac: mac } = devices.real(); const isFocusingOnCaretChange = ios || mac; const getCaret = input => { let range = { start: 0, end: 0 }; try { range = { start: input.selectionStart ?? 0, end: input.selectionEnd ?? 0 } } catch (e) { range = { start: 0, end: 0 } } return range }; export const setCaret = (input, selection) => { try { input.selectionStart = selection.start ?? null; input.selectionEnd = selection.end ?? null } catch {} }; const caret = function(input, selection) { let force = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : false; const inputElement = $(input).get(0); if (!isDefined(selection)) { return getCaret(inputElement) } if (!force && isFocusingOnCaretChange && domAdapter.getActiveElement(inputElement) !== inputElement) { return } setCaret(inputElement, selection); return }; export default caret;