Some checks failed
CI / build-deploy (push) Has been cancelled
- RBAC: 新增30个权限点、DataScope行级权限、requirePermission守卫,所有57+ Server Action接入权限校验 - UI拆分: exam-form(1623行→11文件)、textbook-reader(744行→7文件),均降至300行以内 - 测试: 新增5个单元测试文件(19用例),修复4个集成测试文件(38用例全部通过) - 架构文档: 新增架构影响地图(004/005)、标准功能清单(006)、差距审计报告(007) - 项目规则: 架构图优先规则,改码必同步图 - 安全: rehype-sanitize净化、AES加密API Key、权限路由守卫 - 无障碍: skip-link、aria-label、prefers-reduced-motion - 性能: next/font优化、next/image、代码分割
58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
"use client"
|
|
|
|
import { useState, useRef } from "react"
|
|
|
|
export function useTextSelection() {
|
|
const [selectedText, setSelectedText] = useState("")
|
|
const selectionRef = useRef("")
|
|
const contentRef = useRef<HTMLDivElement>(null)
|
|
const [createDialogOpen, setCreateDialogOpen] = useState(false)
|
|
const [isCreating, setIsCreating] = useState(false)
|
|
|
|
const handleContentPointerDown = (e: React.PointerEvent) => {
|
|
if (e.button !== 2) return
|
|
|
|
const selection = window.getSelection()
|
|
if (!selection || selection.isCollapsed) {
|
|
selectionRef.current = ""
|
|
return
|
|
}
|
|
|
|
if (contentRef.current && contentRef.current.contains(selection.anchorNode)) {
|
|
selectionRef.current = selection.toString().trim()
|
|
} else {
|
|
selectionRef.current = ""
|
|
}
|
|
}
|
|
|
|
const handleContextMenuChange = (open: boolean) => {
|
|
if (!open) return
|
|
|
|
if (selectionRef.current) {
|
|
setSelectedText(selectionRef.current)
|
|
} else {
|
|
const selection = window.getSelection()
|
|
if (selection && !selection.isCollapsed && contentRef.current && contentRef.current.contains(selection.anchorNode)) {
|
|
const text = selection.toString().trim()
|
|
selectionRef.current = text
|
|
setSelectedText(text)
|
|
} else {
|
|
setSelectedText("")
|
|
}
|
|
}
|
|
}
|
|
|
|
return {
|
|
selectedText,
|
|
setSelectedText,
|
|
selectionRef,
|
|
contentRef,
|
|
createDialogOpen,
|
|
setCreateDialogOpen,
|
|
isCreating,
|
|
setIsCreating,
|
|
handleContentPointerDown,
|
|
handleContextMenuChange,
|
|
}
|
|
}
|