OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
openproject/frontend/app/components/common/set-click-position/set-click-position.ts

34 lines
937 B

import {debugLog} from '../../../helpers/debug_output';
export namespace ClickPositionMapper {
/**
* Try to set the position on the given input element.
*
* @param element The element to set the cursor to
* @param offset The character offset retrieved from getPosition.
*/
export function setPosition(element:JQuery, offset:number):void {
try {
(element[0] as HTMLInputElement).setSelectionRange(offset, offset);
} catch (e) {
debugLog('Failed to set click position for edit field.', e);
}
}
/**
* Get the cursor offset from the click event.
*
* @param evt
* @return {number}
*/
export function getPosition(evt:JQueryEventObject):number {
try {
const range = document.caretRangeFromPoint(evt.clientX, evt.clientY);
return range.startOffset;
} catch (e) {
debugLog('Failed to get click position for edit field.', e);
return 0;
}
}
}