bunch of fixes and refactoring for suggestions/rhymes in the pad editor
This commit is contained in:
18
src/lib/utils/cursor.ts
Normal file
18
src/lib/utils/cursor.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import type { TipexEditor } from '@friendofsvelte/tipex';
|
||||
|
||||
export function calculateCursorPosition(editor: TipexEditor | undefined) {
|
||||
const selection = window.getSelection();
|
||||
if (!selection?.rangeCount) return { x: 0, y: 0 };
|
||||
|
||||
const range = selection.getRangeAt(0);
|
||||
const rect = range.getBoundingClientRect();
|
||||
const editorRect = editor?.view.dom.getBoundingClientRect() || { left: 0, top: 0 };
|
||||
|
||||
const x = Math.min(
|
||||
Math.max(rect.left - editorRect.left + window.scrollX, 100),
|
||||
window.innerWidth - 300
|
||||
);
|
||||
const y = rect.bottom - editorRect.top + window.scrollY + 10;
|
||||
|
||||
return { x, y };
|
||||
}
|
14
src/lib/utils/debounce.ts
Normal file
14
src/lib/utils/debounce.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
export function createDebounce() {
|
||||
let timer: NodeJS.Timeout | null = null;
|
||||
|
||||
return function debounce<T extends (...args: any[]) => any>(
|
||||
fn: T,
|
||||
delay: number
|
||||
): (...args: Parameters<T>) => void {
|
||||
return (...args: Parameters<T>) => {
|
||||
if (timer) clearTimeout(timer);
|
||||
timer = setTimeout(() => fn(...args), delay);
|
||||
};
|
||||
};
|
||||
}
|
24
src/lib/utils/text.ts
Normal file
24
src/lib/utils/text.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
/**
|
||||
* Extracts the last word (only alphabetical characters) from a given text
|
||||
*/
|
||||
function extractLastWord(text: string): string {
|
||||
const matches = text.match(/[a-zA-Z]+\s*$/);
|
||||
return matches ? matches[0].trim() : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the current word at cursor position from the document
|
||||
*/
|
||||
export function extractCurrentWord(doc: any, pos: number): string {
|
||||
const textBefore = doc.textBetween(Math.max(0, pos - 100), pos);
|
||||
return extractLastWord(textBefore);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the last word from a selected text range
|
||||
*/
|
||||
export function extractSelectedWord(doc: any, from: number, to: number): string {
|
||||
const selectedText = doc.textBetween(from, to, ' ');
|
||||
return extractLastWord(selectedText);
|
||||
}
|
Reference in New Issue
Block a user