feat: 支持输入框内选择知识库和文件,优化选择交互体验

This commit is contained in:
wizardchen
2025-12-19 22:17:56 +08:00
committed by lyingbug
parent 485d8d7252
commit de1ffa7f7c
49 changed files with 2246 additions and 521 deletions
+58
View File
@@ -0,0 +1,58 @@
export interface CaretCoordinates {
top: number;
left: number;
height: number;
}
export function getCaretCoordinates(element: HTMLTextAreaElement, position: number): CaretCoordinates {
const div = document.createElement('div');
const style = window.getComputedStyle(element);
// Copy styles
const properties = [
'direction', 'boxSizing', 'width', 'height', 'overflowX', 'overflowY',
'borderTopWidth', 'borderRightWidth', 'borderBottomWidth', 'borderLeftWidth', 'borderStyle',
'paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft',
'fontStyle', 'fontVariant', 'fontWeight', 'fontStretch', 'fontSize', 'fontSizeAdjust', 'lineHeight', 'fontFamily',
'textAlign', 'textTransform', 'textIndent', 'textDecoration', 'letterSpacing', 'wordSpacing',
'tabSize', 'MozTabSize'
];
properties.forEach(prop => {
// @ts-ignore
div.style[prop] = style[prop];
});
div.style.position = 'absolute';
div.style.visibility = 'hidden';
div.style.whiteSpace = 'pre-wrap';
div.style.wordWrap = 'break-word';
div.style.top = '0';
div.style.left = '0';
// We append a special character to the end of the text to handle the case where the caret is at the end
const textContent = element.value.substring(0, position);
div.textContent = textContent;
const span = document.createElement('span');
// Use a zero-width space to simulate the caret position without adding visible width,
// but if it's at the end of a line, we might need something else.
// Standard trick is using a pipe or similar and measuring it.
span.textContent = '|';
div.appendChild(span);
document.body.appendChild(div);
const spanRect = span.getBoundingClientRect();
const divRect = div.getBoundingClientRect();
const coordinates = {
top: span.offsetTop + parseInt(style.borderTopWidth),
left: span.offsetLeft + parseInt(style.borderLeftWidth),
height: parseInt(style.lineHeight) || spanRect.height
};
document.body.removeChild(div);
return coordinates;
}