feat: modify logic to gen cursor index

This commit is contained in:
Rohit
2025-02-03 18:13:45 +05:30
parent 80356b5600
commit 9ff7813ad9

View File

@@ -429,28 +429,54 @@ export class WorkflowGenerator {
if ((elementInfo?.tagName === 'INPUT' || elementInfo?.tagName === 'TEXTAREA') && selector) { if ((elementInfo?.tagName === 'INPUT' || elementInfo?.tagName === 'TEXTAREA') && selector) {
// Calculate the exact position within the element // Calculate the exact position within the element
const positionAndCursor = await page.evaluate((selector) => { const positionAndCursor = await page.evaluate(
const element = document.querySelector(selector); ({ selector, coords }) => {
if (!element) return null; const element = document.querySelector(selector);
if (!element) return null;
const getCursorPosition = (inputElement: HTMLInputElement | HTMLTextAreaElement, clickCoords: Coordinates) => {
const rect = inputElement.getBoundingClientRect();
const clickX = clickCoords.x - rect.left;
// Get the input's text content
const text = inputElement.value;
// Create a temporary element to measure text
const measurer = document.createElement('span');
measurer.style.font = window.getComputedStyle(inputElement).font;
measurer.style.position = 'absolute';
measurer.style.whiteSpace = 'pre';
measurer.style.visibility = 'hidden';
document.body.appendChild(measurer);
// Find the position where the click occurred
let position = 0;
for (let i = 0; i <= text.length; i++) {
measurer.textContent = text.slice(0, i);
const width = measurer.getBoundingClientRect().width;
if (width >= clickX) {
position = i;
break;
}
}
document.body.removeChild(measurer);
return position;
};
// Get element position for the relative click coordinates const rect = element.getBoundingClientRect();
const rect = element.getBoundingClientRect(); const cursorIndex = getCursorPosition(element as HTMLInputElement | HTMLTextAreaElement, coords);
// Get cursor position index return {
let cursorIndex = 0; rect: {
if ('selectionStart' in element) { x: rect.left,
// selectionStart gives us the exact index where the cursor is y: rect.top
cursorIndex = (element as HTMLInputElement | HTMLTextAreaElement).selectionStart || 0; },
} cursorIndex
};
return { },
rect: { { selector, coords: coordinates }
x: rect.left, );
y: rect.top
},
cursorIndex
};
}, selector);
if (positionAndCursor) { if (positionAndCursor) {
const relativeX = coordinates.x - positionAndCursor.rect.x; const relativeX = coordinates.x - positionAndCursor.rect.x;