Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hook): ✨ 添加光标位置保存与恢复功能,优化输入体验 #179

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 64 additions & 20 deletions src/hooks/useMsgInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@
topicKeyword
)

// 在文件顶部添加新的响应式变量
const lastCursorPosition = ref<{
range: Range | null
selection: Selection | null
}>({
range: null,
selection: null
})

watchEffect(() => {
chatKey.value = chat.value.sendKey
if (!ait.value && personList.value.length > 0) {
Expand Down Expand Up @@ -368,6 +377,12 @@
return
}

// 保存最后的光标位置
lastCursorPosition.value = {
range: range.cloneRange(),
selection
}

Check warning on line 384 in src/hooks/useMsgInput.ts

View check run for this annotation

Codecov / codecov/patch

src/hooks/useMsgInput.ts#L381-L384

Added lines #L381 - L384 were not covered by tests

/** 获取当前节点 */
const curNode = range.endContainer
/** 判断当前节点是否是文本节点 */
Expand Down Expand Up @@ -407,9 +422,9 @@
const isCtrlOrMetaKey = isWindows ? e.ctrlKey : e.metaKey

const sendKeyIsEnter = chat.value.sendKey === 'Enter'
const sendKeyIsCtrlEnter = chat.value.sendKey === `${isWindows ? 'Ctrl' : ''}+Enter`
const sendKeyIsCtrlEnter = chat.value.sendKey === `${isWindows ? 'Ctrl' : '?'}+Enter`

Check warning on line 425 in src/hooks/useMsgInput.ts

View check run for this annotation

Codecov / codecov/patch

src/hooks/useMsgInput.ts#L425

Added line #L425 was not covered by tests

// 如果当前的系统是mac,我需要判断当前的chat.value.sendKey是否是Enter,再判断当前是否是按下+Enter
// 如果当前的系统是mac,我需要判断当前的chat.value.sendKey是否是Enter,再判断当前是否是按下?+Enter
if (!isWindows && chat.value.sendKey === 'Enter' && e.metaKey && e.key === 'Enter') {
// 就进行换行操作
e.preventDefault()
Expand Down Expand Up @@ -437,9 +452,18 @@
if (isChinese.value) {
return
}

// 先确保输入框获得焦点
messageInputDom.value?.focus()
// 先获取并保存当前的编辑器范围

// 恢复上次保存的光标位置
if (lastCursorPosition.value.range && lastCursorPosition.value.selection) {
const selection = window.getSelection()
selection?.removeAllRanges()
selection?.addRange(lastCursorPosition.value.range)
}

Check warning on line 464 in src/hooks/useMsgInput.ts

View check run for this annotation

Codecov / codecov/patch

src/hooks/useMsgInput.ts#L460-L464

Added lines #L460 - L464 were not covered by tests

// 获取当前光标位置
const { range: currentRange, selection: currentSelection } = getEditorRange()!
editorRange.value = { range: currentRange, selection: currentSelection }

Expand Down Expand Up @@ -467,6 +491,15 @@
insertNode(MsgEnum.AIT, item.name, {} as HTMLElement)
triggerInputEvent(messageInputDom.value)
ait.value = false

// 更新最后的光标位置
const newRange = getEditorRange()
if (newRange) {
lastCursorPosition.value = {
range: newRange.range.cloneRange(),
selection: newRange.selection
}
}

Check warning on line 502 in src/hooks/useMsgInput.ts

View check run for this annotation

Codecov / codecov/patch

src/hooks/useMsgInput.ts#L496-L502

Added lines #L496 - L502 were not covered by tests
}

/** 处理点击 / 提及框事件 */
Expand Down Expand Up @@ -510,23 +543,23 @@
onMounted(async () => {
db.value = await Database.load('sqlite:sqlite.db')
await db.value.execute(`
CREATE TABLE IF NOT EXISTS message (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
room_id INTEGER NOT NULL,
from_uid INTEGER NOT NULL,
content TEXT(1024),
reply_msg_id INTEGER NOT NULL,
status INTEGER,
gap_count INTEGER,
"type" INTEGER DEFAULT (1),
extra TEXT,
create_time INTEGER,
update_time INTEGER
);
CREATE INDEX IF NOT EXISTS idx_room_id ON message (room_id);
CREATE INDEX IF NOT EXISTS idx_from_uid ON message (from_uid);
CREATE INDEX IF NOT EXISTS idx_create_time ON message (create_time);
CREATE INDEX IF NOT EXISTS idx_update_time ON message (update_time);
CREATE TABLE IF NOT EXISTS message (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
room_id INTEGER NOT NULL,
from_uid INTEGER NOT NULL,
content TEXT(1024),
reply_msg_id INTEGER NOT NULL,
status INTEGER,
gap_count INTEGER,
"type" INTEGER DEFAULT (1),
extra TEXT,
create_time INTEGER,
update_time INTEGER
);
CREATE INDEX IF NOT EXISTS idx_room_id ON message (room_id);
CREATE INDEX IF NOT EXISTS idx_from_uid ON message (from_uid);
CREATE INDEX IF NOT EXISTS idx_create_time ON message (create_time);
CREATE INDEX IF NOT EXISTS idx_update_time ON message (update_time);
`)
useMitt.on(MittEnum.RE_EDIT, async (event: string) => {
messageInputDom.value.focus()
Expand Down Expand Up @@ -588,6 +621,17 @@
})
}
})

// 添加失焦事件监听
messageInputDom.value?.addEventListener('blur', () => {
const { range, selection } = getEditorRange()!
if (range && selection) {
lastCursorPosition.value = {
range: range.cloneRange(),
selection
}
}
})

Check warning on line 634 in src/hooks/useMsgInput.ts

View check run for this annotation

Codecov / codecov/patch

src/hooks/useMsgInput.ts#L626-L634

Added lines #L626 - L634 were not covered by tests
})

return {
Expand Down
Loading