diff --git a/src/routes/(app)/pad/+page.svelte b/src/routes/(app)/pad/+page.svelte
index 84394f4..46676a7 100644
--- a/src/routes/(app)/pad/+page.svelte
+++ b/src/routes/(app)/pad/+page.svelte
@@ -50,6 +50,18 @@
suggestions = await datamuseApi.getSuggestions(word);
}, 300);
+ async function handleSelectionChange() {
+ const selection = editor?.view.state.selection;
+ if (selection && !selection.empty) {
+ const selectedText = editor?.view.state.doc.textBetween(selection.from, selection.to, ' ');
+
+ if (selectedText) {
+ rhymes = await datamuseApi.getRhymes(selectedText);
+ updateCursorPosition();
+ }
+ }
+ }
+
function updateCursorPosition() {
const selection = window.getSelection();
if (!selection?.rangeCount) return;
@@ -57,8 +69,10 @@
const range = selection.getRangeAt(0);
const rect = range.getBoundingClientRect();
+ const x = Math.min(Math.max(rect.left + window.scrollX, 100), window.innerWidth - 100);
+
cursorPosition = {
- x: rect.left + window.scrollX,
+ x,
y: rect.bottom + window.scrollY + 10
};
}
@@ -71,7 +85,6 @@
const { doc, selection } = state || {};
if (!doc || !selection) return;
-
const pos = selection.from;
const currentChar = doc.textBetween(Math.max(0, pos - 1), pos);
@@ -99,18 +112,8 @@
updateCursorPosition();
}
- async function handleMouseUp() {
- const selection = editor?.view.state.selection;
- if (selection && !selection.empty) {
- const selectedText = editor?.view.state.doc.textBetween(selection.from, selection.to, ' ');
-
- if (selectedText) {
- rhymes = await datamuseApi.getRhymes(selectedText);
- }
- } else {
- rhymes = [];
- }
- updateCursorPosition();
+ async function handleMouseUp(event: MouseEvent | TouchEvent) {
+ handleSelectionChange();
}
async function handleMouseDown() {
@@ -158,10 +161,14 @@
+