65 lines
3.0 KiB
JavaScript
65 lines
3.0 KiB
JavaScript
/* ui-modal-drag.js —— 弹窗拖动 + 外观实时预览(v3.25.76)
|
|
*
|
|
* 为什么单独成模块:src 里单组件行数有上限(SettingsModal 已接近上限),
|
|
* 这两个都是与具体弹窗无关的通用行为,抽出来也便于别处复用。
|
|
*/
|
|
;(function (global) {
|
|
'use strict'
|
|
const React = global.React || {}
|
|
const el = (t, p, ...c) => React.createElement(t, p, ...c)
|
|
|
|
// 拖动标题栏移动弹窗;位置限制在视口内(至少留 120×60 可见)
|
|
function useDraggableModal () {
|
|
const [pos, setPos] = React.useState(null)
|
|
const dragRef = React.useRef(null)
|
|
const onHeadPointerDown = (e) => {
|
|
if (e.button !== 0) return
|
|
const host = e.currentTarget && e.currentTarget.closest ? e.currentTarget.closest('.iwb-modal') : null
|
|
if (!host) return
|
|
const r = host.getBoundingClientRect()
|
|
dragRef.current = { x: e.clientX, y: e.clientY, left: r.left, top: r.top }
|
|
const move = (ev) => {
|
|
const d = dragRef.current
|
|
if (!d) return
|
|
const left = Math.max(8, Math.min((global.innerWidth || 1200) - 120, d.left + (ev.clientX - d.x)))
|
|
const top = Math.max(8, Math.min((global.innerHeight || 800) - 60, d.top + (ev.clientY - d.y)))
|
|
setPos({ left, top })
|
|
}
|
|
const up = () => {
|
|
dragRef.current = null
|
|
global.removeEventListener('pointermove', move)
|
|
global.removeEventListener('pointerup', up)
|
|
}
|
|
global.addEventListener('pointermove', move)
|
|
global.addEventListener('pointerup', up)
|
|
e.preventDefault()
|
|
}
|
|
const style = pos ? { position: 'fixed', left: pos.left + 'px', top: pos.top + 'px', margin: 0, transform: 'none' } : undefined
|
|
return { pos, style, onHeadPointerDown }
|
|
}
|
|
|
|
// 外观实时预览:draft 变化 → 立刻写 CSS 变量 + 通知画布重渲染;关闭未保存则回滚
|
|
function useAppearancePreview (opts) {
|
|
const o = opts || {}
|
|
const savedRef = React.useRef(o.savedConfig || o.config || null)
|
|
const themeRef = React.useRef(o.dark ? 'dark' : 'light')
|
|
React.useEffect(() => { themeRef.current = o.dark ? 'dark' : 'light' }, [o.dark])
|
|
React.useEffect(() => {
|
|
if (!o.enabled) return
|
|
const A = global.IWBUIApearance
|
|
if (!A || typeof A.apply !== 'function') return
|
|
try { A.apply(o.config, !!(o.dark)) } catch (e) { /* ignore */ }
|
|
try { global.dispatchEvent(new Event('iwb:appearance')) } catch (e) { /* ignore */ }
|
|
}, [o.enabled, o.dark, o.config])
|
|
React.useEffect(() => () => {
|
|
const A = global.IWBUIApearance
|
|
if (!A || typeof A.apply !== 'function') return
|
|
try { A.apply(savedRef.current, themeRef.current === 'dark') } catch (e) { /* ignore */ }
|
|
try { global.dispatchEvent(new Event('iwb:appearance')) } catch (e) { /* ignore */ }
|
|
}, [])
|
|
return { markSaved: () => { savedRef.current = o.config || null } }
|
|
}
|
|
|
|
global.IWBUIModalDrag = { useDraggableModal, useAppearancePreview }
|
|
})(typeof window !== 'undefined' ? window : globalThis)
|