()
| 11 | * @returns |
| 12 | */ |
| 13 | export default function RenderSubWindows() { |
| 14 | const subWindows = SubWindow.use(); |
| 15 | |
| 16 | const onClickInner = (win: SubWindow.Window) => { |
| 17 | if (win.closeWhenClickInside) { |
| 18 | SubWindow.close(win.id); |
| 19 | } |
| 20 | }; |
| 21 | |
| 22 | return ( |
| 23 | <div className="pointer-events-none fixed left-0 top-0 z-40 h-full w-full"> |
| 24 | {subWindows.map((win: SubWindow.Window) => ( |
| 25 | // transition 组件可以让关闭流程更平滑 |
| 26 | <Transition key={win.id} appear={true} show={!win.closing}> |
| 27 | <SimpleCard |
| 28 | data-pg-window-id={win.id} |
| 29 | style={{ |
| 30 | top: win.rect.top + "px", |
| 31 | left: win.rect.left + "px", |
| 32 | zIndex: win.zIndex, |
| 33 | width: win.rect.width + "px", |
| 34 | height: win.rect.height + "px", |
| 35 | }} |
| 36 | className={cn( |
| 37 | "pointer-events-auto absolute flex flex-col overflow-hidden outline-0 transition", |
| 38 | "data-closed:scale-90 data-closed:opacity-0", |
| 39 | )} |
| 40 | onClick={() => onClickInner(win)} |
| 41 | onMouseDown={(e) => { |
| 42 | SubWindow.focus(win.id); |
| 43 | // 如果按到的元素的父元素都没有data-pg-drag-region属性,就不移动窗口 |
| 44 | if (!(e.target as HTMLElement).closest("[data-pg-drag-region]")) { |
| 45 | return; |
| 46 | } |
| 47 | const start = new Vector(e.clientX, e.clientY); |
| 48 | const onMouseUp = () => { |
| 49 | window.removeEventListener("mouseup", onMouseUp); |
| 50 | window.removeEventListener("mousemove", onMouseMove); |
| 51 | }; |
| 52 | const onMouseMove = (e: MouseEvent) => { |
| 53 | const delta = new Vector(e.clientX, e.clientY).subtract(start); |
| 54 | const newRect = win.rect.translate(delta); |
| 55 | SubWindow.update(win.id, { rect: newRect }); |
| 56 | }; |
| 57 | window.addEventListener("mouseup", onMouseUp); |
| 58 | window.addEventListener("mousemove", onMouseMove); |
| 59 | }} |
| 60 | onTouchStart={(e) => { |
| 61 | SubWindow.focus(win.id); |
| 62 | if (e.touches.length > 1) return; |
| 63 | const touch = e.touches[0]; |
| 64 | // 如果按到的元素的父元素都没有data-pg-drag-region属性,就不移动窗口 |
| 65 | if (!(e.target as HTMLElement).closest("[data-pg-drag-region]")) { |
| 66 | return; |
| 67 | } |
| 68 | const start = new Vector(touch.clientX, touch.clientY); |
| 69 | const onTouchEnd = () => { |
| 70 | window.removeEventListener("touchend", onTouchEnd); |
nothing calls this directly
no test coverage detected