UNPKG

devextreme

Version:

HTML5 JavaScript Component Suite for Responsive Web Development

57 lines (56 loc) 1.59 kB
/** * DevExtreme (esm/__internal/ui/text_box/m_utils.caret.js) * Version: 24.2.6 * Build date: Mon Mar 17 2025 * * Copyright (c) 2012 - 2025 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; try { range = { start: input.selectionStart, end: input.selectionEnd } } catch (e) { range = { start: 0, end: 0 } } return range }; const setCaret = (input, position) => { const body = domAdapter.getBody(); if (!body.contains(input) && !body.contains(input.getRootNode().host)) { return } try { input.selectionStart = position.start; input.selectionEnd = position.end } catch (e) {} }; const caret = function(input, position) { let force = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : false; input = $(input).get(0); if (!isDefined(position)) { return getCaret(input) } if (!force && isFocusingOnCaretChange && domAdapter.getActiveElement(input) !== input) { return } setCaret(input, position) }; export default caret;