@snowui-design-system/components 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +82 -0
- package/package.json +47 -0
- package/src/App.svelte +103 -0
- package/src/components/Avatar.md +452 -0
- package/src/components/Avatar.svelte +593 -0
- package/src/components/Avatar.test.ts +231 -0
- package/src/components/AvatarGroup.md +246 -0
- package/src/components/AvatarGroup.svelte +691 -0
- package/src/components/AvatarGroup.test.ts +142 -0
- package/src/components/Badge.md +164 -0
- package/src/components/Badge.svelte +461 -0
- package/src/components/Badge.test.ts +293 -0
- package/src/components/Button.md +312 -0
- package/src/components/Button.svelte +796 -0
- package/src/components/Button.test.ts +631 -0
- package/src/components/Checkbox.md +313 -0
- package/src/components/Checkbox.svelte +466 -0
- package/src/components/Checkbox.test.ts +57 -0
- package/src/components/CodeBlock.md +240 -0
- package/src/components/CodeBlock.svelte +1287 -0
- package/src/components/CodeBlock.test.ts +192 -0
- package/src/components/Form.md +33 -0
- package/src/components/Form.test.ts +307 -0
- package/src/components/Frame.svelte +220 -0
- package/src/components/Icon.md +279 -0
- package/src/components/Icon.svelte +505 -0
- package/src/components/Icon.test.ts +82 -0
- package/src/components/Image.md +292 -0
- package/src/components/Image.svelte +634 -0
- package/src/components/Image.test.ts +42 -0
- package/src/components/ImageGroup.md +168 -0
- package/src/components/ImageGroup.svelte +151 -0
- package/src/components/ImageGroup.test.ts +46 -0
- package/src/components/InView.test.ts +55 -0
- package/src/components/Input.md +666 -0
- package/src/components/Input.svelte +2741 -0
- package/src/components/Input.test.ts +503 -0
- package/src/components/Kbd.md +100 -0
- package/src/components/Kbd.svelte +110 -0
- package/src/components/Kbd.test.ts +50 -0
- package/src/components/Listbox.md +245 -0
- package/src/components/Listbox.svelte +389 -0
- package/src/components/Listbox.test.ts +139 -0
- package/src/components/MenuItem.md +184 -0
- package/src/components/MenuItem.svelte +326 -0
- package/src/components/MenuItem.test.ts +167 -0
- package/src/components/Morph.test.ts +53 -0
- package/src/components/Popover.md +87 -0
- package/src/components/Popover.svelte +186 -0
- package/src/components/Popover.test.ts +60 -0
- package/src/components/QuickAction.md +146 -0
- package/src/components/QuickAction.svelte +161 -0
- package/src/components/QuickAction.test.ts +299 -0
- package/src/components/Radio.md +318 -0
- package/src/components/Radio.svelte +502 -0
- package/src/components/Radio.test.ts +76 -0
- package/src/components/Select.md +492 -0
- package/src/components/Select.svelte +535 -0
- package/src/components/Select.test.ts +146 -0
- package/src/components/Skeleton.md +323 -0
- package/src/components/Skeleton.svelte +791 -0
- package/src/components/Skeleton.test.ts +121 -0
- package/src/components/Spinner.md +136 -0
- package/src/components/Spinner.svelte +296 -0
- package/src/components/Spinner.test.ts +88 -0
- package/src/components/SplitText.test.ts +142 -0
- package/src/components/Stagger.test.ts +57 -0
- package/src/components/Test.md +72 -0
- package/src/components/Test.svelte +106 -0
- package/src/components/Test.test.ts +27 -0
- package/src/components/Text.md +450 -0
- package/src/components/Text.svelte +507 -0
- package/src/components/Text.test.ts +150 -0
- package/src/components/Textarea.md +240 -0
- package/src/components/Textarea.svelte +742 -0
- package/src/components/Textarea.test.ts +131 -0
- package/src/components/Toggle.md +196 -0
- package/src/components/Toggle.svelte +384 -0
- package/src/components/Toggle.test.ts +57 -0
- package/src/components/Tooltip.md +164 -0
- package/src/components/Tooltip.svelte +417 -0
- package/src/components/Tooltip.test.ts +64 -0
- package/src/context.ts +312 -0
- package/src/dev.ts +10 -0
- package/src/index.ts +29 -0
- package/src/keys.ts +26 -0
- package/src/lib/search-filter.md +82 -0
- package/src/lib/search-filter.ts +57 -0
- package/src/lib/truncate.md +25 -0
- package/src/lib/truncate.ts +234 -0
- package/src/logic/README.md +146 -0
- package/src/logic/collapse.svelte.ts +6 -0
- package/src/logic/frame-dom-runtime.md +19 -0
- package/src/logic/frame-dom-runtime.svelte.ts +286 -0
- package/src/logic/frame-field-control.md +38 -0
- package/src/logic/frame-field-control.ts +95 -0
- package/src/logic/frame-identity.md +68 -0
- package/src/logic/frame-identity.svelte.ts +25 -0
- package/src/logic/frame-layout.md +91 -0
- package/src/logic/frame-layout.svelte.ts +111 -0
- package/src/logic/frame-motion-defaults.md +180 -0
- package/src/logic/frame-motion-defaults.ts +711 -0
- package/src/logic/frame-profile.md +156 -0
- package/src/logic/frame-profile.ts +130 -0
- package/src/logic/frame-prop-filter.md +34 -0
- package/src/logic/frame-prop-filter.ts +254 -0
- package/src/logic/frame-responsive.md +91 -0
- package/src/logic/frame-responsive.svelte.ts +92 -0
- package/src/logic/frame-skeleton-runtime.md +20 -0
- package/src/logic/frame-skeleton-runtime.svelte.ts +160 -0
- package/src/logic/frame-truncation.md +127 -0
- package/src/logic/frame-truncation.svelte.ts +193 -0
- package/src/logic/frame-types.md +157 -0
- package/src/logic/frame-types.ts +434 -0
- package/src/logic/frame-utils.md +22 -0
- package/src/logic/frame-utils.ts +250 -0
- package/src/logic/geometry-cache.ts +120 -0
- package/src/logic/image-preview.md +24 -0
- package/src/logic/image-preview.ts +186 -0
- package/src/logic/layout-flip.ts +109 -0
- package/src/logic/motion-config.ts +102 -0
- package/src/logic/motion-types.md +47 -0
- package/src/logic/motion-types.ts +119 -0
- package/src/logic/observer.md +20 -0
- package/src/logic/observer.svelte.ts +53 -0
- package/src/logic/responsive.md +64 -0
- package/src/logic/responsive.svelte.ts +118 -0
- package/src/logic/text-collapse.md +22 -0
- package/src/logic/text-collapse.svelte.ts +507 -0
- package/src/logic/wc-adapter.ts +188 -0
- package/src/motion/NumberFlow.md +16 -0
- package/src/motion/README.md +87 -0
- package/src/motion/defaults/bridge.md +53 -0
- package/src/motion/defaults/bridge.ts +171 -0
- package/src/motion/defaults/content-crossfade.ts +22 -0
- package/src/motion/defaults/expand.md +66 -0
- package/src/motion/defaults/expand.ts +22 -0
- package/src/motion/defaults/fade-out.md +41 -0
- package/src/motion/defaults/fade-out.ts +14 -0
- package/src/motion/defaults/morph.md +42 -0
- package/src/motion/defaults/morph.ts +14 -0
- package/src/motion/defaults/move.md +81 -0
- package/src/motion/defaults/move.ts +31 -0
- package/src/motion/defaults/number.md +41 -0
- package/src/motion/defaults/number.ts +17 -0
- package/src/motion/defaults/replace.md +56 -0
- package/src/motion/defaults/replace.ts +22 -0
- package/src/motion/defaults/rotate.css +15 -0
- package/src/motion/defaults/rotate.md +60 -0
- package/src/motion/defaults/rotate.ts +20 -0
- package/src/motion/defaults/while-2xl.ts +9 -0
- package/src/motion/defaults/while-active.ts +12 -0
- package/src/motion/defaults/while-appear.ts +12 -0
- package/src/motion/defaults/while-click.ts +9 -0
- package/src/motion/defaults/while-collapsed.ts +12 -0
- package/src/motion/defaults/while-disabled.ts +13 -0
- package/src/motion/defaults/while-disappear.ts +12 -0
- package/src/motion/defaults/while-drag.ts +9 -0
- package/src/motion/defaults/while-editing.ts +9 -0
- package/src/motion/defaults/while-expanded.ts +12 -0
- package/src/motion/defaults/while-focus.ts +9 -0
- package/src/motion/defaults/while-hover.ts +13 -0
- package/src/motion/defaults/while-idle.ts +9 -0
- package/src/motion/defaults/while-key.ts +9 -0
- package/src/motion/defaults/while-lg.ts +9 -0
- package/src/motion/defaults/while-load.ts +11 -0
- package/src/motion/defaults/while-md.ts +9 -0
- package/src/motion/defaults/while-reload.ts +12 -0
- package/src/motion/defaults/while-scroll.ts +9 -0
- package/src/motion/defaults/while-sm.ts +9 -0
- package/src/motion/defaults/while-uploading.ts +9 -0
- package/src/motion/defaults/while-validating.ts +9 -0
- package/src/motion/defaults/while-xl.ts +9 -0
- package/src/motion/defaults/while-xs.ts +9 -0
- package/src/styles/README.md +95 -0
- package/src/styles/auto-layout.css +111 -0
- package/src/styles/index.css +7 -0
- package/src/styles/layout.css +319 -0
- package/src/styles/scale.css +106 -0
- package/src/styles/scale.md +218 -0
- package/src/styles/scrollbar.css +173 -0
- package/src/styles/tokens.css +192 -0
- package/src/vite-env.d.ts +9 -0
- package/src//344/273/243/347/240/201.txt +61 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Frame 逻辑通用工具函数
|
|
3
|
+
* v1.1.3
|
|
4
|
+
* 2026-03-07: 新增 loading / skeleton 过滤与动效识别支持,释放 whileLoad 关键字回归纯动效。
|
|
5
|
+
* 2026-03-03: 修复 transform 数组配置仅取终值的问题,支持 from->to 动画(用于 morph 自动动效)
|
|
6
|
+
* 2026-03-03: 补齐 rotateY 变换输出,支持 Flip 默认动效走 3D 翻转。
|
|
7
|
+
*/
|
|
8
|
+
import type { FrameProps } from "./frame-types";
|
|
9
|
+
import type { MotionConfig } from "./motion-config";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 检查是否包含动效配置
|
|
13
|
+
* 用于判断是否需要生成 ID 或注册到动效编排器
|
|
14
|
+
*/
|
|
15
|
+
export function hasMotionConfig(props: FrameProps): boolean {
|
|
16
|
+
return (
|
|
17
|
+
!!props.orchestrate ||
|
|
18
|
+
// 事件/状态触发器会激活默认 while* 动效
|
|
19
|
+
!!props.Click ||
|
|
20
|
+
!!props.Drag ||
|
|
21
|
+
!!props.Hover ||
|
|
22
|
+
!!props.Focus ||
|
|
23
|
+
!!props.Key ||
|
|
24
|
+
!!props.Scroll ||
|
|
25
|
+
!!props.StartLoad ||
|
|
26
|
+
!!props.Load ||
|
|
27
|
+
!!props.Reload ||
|
|
28
|
+
!!props.Appear ||
|
|
29
|
+
!!props.Disappear ||
|
|
30
|
+
!!props.Idle ||
|
|
31
|
+
!!props.Active ||
|
|
32
|
+
!!props.Disabled ||
|
|
33
|
+
!!props.Validating ||
|
|
34
|
+
!!props.Uploading ||
|
|
35
|
+
!!props.Editing ||
|
|
36
|
+
!!props.loading ||
|
|
37
|
+
!!props.whileLoad ||
|
|
38
|
+
!!props.whileReload ||
|
|
39
|
+
!!props.whileAppear ||
|
|
40
|
+
!!props.whileDisappear ||
|
|
41
|
+
!!props.whileIdle ||
|
|
42
|
+
!!props.whileActive ||
|
|
43
|
+
!!props.whileDisabled ||
|
|
44
|
+
!!props.whileClick ||
|
|
45
|
+
!!props.whileDrag ||
|
|
46
|
+
!!props.whileHover ||
|
|
47
|
+
!!props.whileFocus ||
|
|
48
|
+
!!props.whileKey ||
|
|
49
|
+
!!props.whileScroll ||
|
|
50
|
+
!!props.whileValidating ||
|
|
51
|
+
!!props.whileUploading ||
|
|
52
|
+
!!props.whileEditing ||
|
|
53
|
+
!!props.whileCollapsed ||
|
|
54
|
+
!!props.whileExpanded ||
|
|
55
|
+
!!props.whileXs ||
|
|
56
|
+
!!props.whileSm ||
|
|
57
|
+
!!props.whileMd ||
|
|
58
|
+
!!props.whileLg ||
|
|
59
|
+
!!props.whileXl ||
|
|
60
|
+
!!props.while2xl ||
|
|
61
|
+
!!props.timeline ||
|
|
62
|
+
!!props.variants ||
|
|
63
|
+
!!props.scroll ||
|
|
64
|
+
!!props.layoutId ||
|
|
65
|
+
!!props.itemId
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function motionConfigToStyle(config: MotionConfig | undefined): string {
|
|
70
|
+
if (!config || typeof config !== "object") return "";
|
|
71
|
+
const styles: string[] = [];
|
|
72
|
+
const toTransforms: string[] = [];
|
|
73
|
+
const fromTransforms: string[] = [];
|
|
74
|
+
let hasTransformFromState = false;
|
|
75
|
+
|
|
76
|
+
const resolveCssEasing = (easing: MotionConfig["easing"]) => {
|
|
77
|
+
if (!easing || typeof easing !== "string") return "ease-out";
|
|
78
|
+
const map: Record<string, string> = {
|
|
79
|
+
linear: "linear",
|
|
80
|
+
easeIn: "ease-in",
|
|
81
|
+
easeOut: "ease-out",
|
|
82
|
+
easeInOut: "ease-in-out",
|
|
83
|
+
};
|
|
84
|
+
return map[easing] || easing;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const toUnitValue = (value: string | number) =>
|
|
88
|
+
typeof value === "number" ? `${value}px` : value;
|
|
89
|
+
|
|
90
|
+
if (config.duration !== undefined || config.easing !== undefined) {
|
|
91
|
+
const duration = config.duration ?? 300;
|
|
92
|
+
styles.push(
|
|
93
|
+
`--su-motion-transition: all ${duration}ms ${resolveCssEasing(config.easing)}`,
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (config.scale !== undefined) {
|
|
98
|
+
if (Array.isArray(config.scale)) {
|
|
99
|
+
const from = config.scale[0];
|
|
100
|
+
const to = config.scale[1];
|
|
101
|
+
fromTransforms.push(`scale(${from})`);
|
|
102
|
+
toTransforms.push(`scale(${to})`);
|
|
103
|
+
hasTransformFromState = true;
|
|
104
|
+
} else {
|
|
105
|
+
toTransforms.push(`scale(${config.scale})`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (config.opacity !== undefined) {
|
|
109
|
+
const val = Array.isArray(config.opacity) ? config.opacity[1] : config.opacity;
|
|
110
|
+
styles.push(`--su-motion-opacity: ${val}`);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Width 和 Height 通常属于排版级,非动效强占对象,但若存在配置,也可以代理或保持内联。
|
|
114
|
+
// 为保持重构极简与最大向后兼容,宽高变形等暂时保留原样,重点降权 transform 和背景、透明度。
|
|
115
|
+
if (config.height !== undefined) {
|
|
116
|
+
const h = config.height;
|
|
117
|
+
if (typeof h === "string" && h.startsWith("+")) {
|
|
118
|
+
styles.push(`margin-top: -${h.slice(1)}`);
|
|
119
|
+
styles.push(`padding-bottom: ${h.slice(1)}`);
|
|
120
|
+
} else if (typeof h === "string" && h.startsWith("-")) {
|
|
121
|
+
styles.push(`margin-top: ${h.slice(1)}`);
|
|
122
|
+
styles.push(`padding-bottom: -${h.slice(1)}`);
|
|
123
|
+
} else {
|
|
124
|
+
styles.push(`height: ${h}`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (config.width !== undefined) {
|
|
128
|
+
styles.push(
|
|
129
|
+
`width: ${typeof config.width === "number" ? `${config.width}px` : config.width}`,
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (config.backgroundColor !== undefined) {
|
|
134
|
+
styles.push(`--su-motion-bg: ${config.backgroundColor}`);
|
|
135
|
+
}
|
|
136
|
+
if (config.color !== undefined) {
|
|
137
|
+
styles.push(`--su-motion-color: ${config.color}`);
|
|
138
|
+
}
|
|
139
|
+
if (config.y !== undefined) {
|
|
140
|
+
if (Array.isArray(config.y)) {
|
|
141
|
+
const from = config.y[0];
|
|
142
|
+
const to = config.y[1];
|
|
143
|
+
fromTransforms.push(`translateY(${toUnitValue(from)})`);
|
|
144
|
+
toTransforms.push(`translateY(${toUnitValue(to)})`);
|
|
145
|
+
hasTransformFromState = true;
|
|
146
|
+
} else {
|
|
147
|
+
toTransforms.push(`translateY(${toUnitValue(config.y)})`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (config.x !== undefined) {
|
|
151
|
+
if (Array.isArray(config.x)) {
|
|
152
|
+
const from = config.x[0];
|
|
153
|
+
const to = config.x[1];
|
|
154
|
+
fromTransforms.push(`translateX(${toUnitValue(from)})`);
|
|
155
|
+
toTransforms.push(`translateX(${toUnitValue(to)})`);
|
|
156
|
+
hasTransformFromState = true;
|
|
157
|
+
} else {
|
|
158
|
+
toTransforms.push(`translateX(${toUnitValue(config.x)})`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (config.rotate !== undefined) {
|
|
162
|
+
if (Array.isArray(config.rotate)) {
|
|
163
|
+
const from = config.rotate[0];
|
|
164
|
+
const to = config.rotate[1];
|
|
165
|
+
fromTransforms.push(`rotate(${from}deg)`);
|
|
166
|
+
toTransforms.push(`rotate(${to}deg)`);
|
|
167
|
+
hasTransformFromState = true;
|
|
168
|
+
} else {
|
|
169
|
+
toTransforms.push(`rotate(${config.rotate}deg)`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (config.rotateY !== undefined) {
|
|
173
|
+
if (Array.isArray(config.rotateY)) {
|
|
174
|
+
const from = config.rotateY[0];
|
|
175
|
+
const to = config.rotateY[1];
|
|
176
|
+
fromTransforms.push(`rotateY(${from}deg)`);
|
|
177
|
+
toTransforms.push(`rotateY(${to}deg)`);
|
|
178
|
+
hasTransformFromState = true;
|
|
179
|
+
} else {
|
|
180
|
+
toTransforms.push(`rotateY(${config.rotateY}deg)`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (toTransforms.length > 0) {
|
|
185
|
+
if (hasTransformFromState) {
|
|
186
|
+
const from = fromTransforms.length
|
|
187
|
+
? fromTransforms.join(" ")
|
|
188
|
+
: "none";
|
|
189
|
+
const to = toTransforms.join(" ");
|
|
190
|
+
styles.push(`--snowui-frame-transform-from: ${from}`);
|
|
191
|
+
styles.push(`--snowui-frame-transform-to: ${to}`);
|
|
192
|
+
// 解除硬编码 transform
|
|
193
|
+
styles.push("--su-motion-transform: var(--snowui-frame-transform-to)");
|
|
194
|
+
const duration = config.duration ?? 300;
|
|
195
|
+
// 动画本身由于需要挂载 keyframes 仍保留直写,但这里最好也转变量委派,
|
|
196
|
+
// 考虑 keyframes 可以作用于内联和代理的 transform 属性冲突情况,这里暂时保持 animation 实体
|
|
197
|
+
styles.push(
|
|
198
|
+
`animation: snowui-frame-transform-motion ${duration}ms ${resolveCssEasing(config.easing)} both`,
|
|
199
|
+
);
|
|
200
|
+
} else {
|
|
201
|
+
styles.push(`--su-motion-transform: ${toTransforms.join(" ")}`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return styles.join("; ");
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 创建 readonly 模式的事件阻断处理器
|
|
210
|
+
* @param access - 访问权限
|
|
211
|
+
* @returns 事件处理器或 undefined
|
|
212
|
+
*/
|
|
213
|
+
export function createReadonlyHandler(access: string | undefined) {
|
|
214
|
+
if (access !== "readonly") return undefined;
|
|
215
|
+
return (e: Event) => {
|
|
216
|
+
e.stopPropagation();
|
|
217
|
+
e.preventDefault();
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* 需要从 props 中过滤掉的属性键列表
|
|
223
|
+
* 这些属性不应透传到 DOM 元素
|
|
224
|
+
*/
|
|
225
|
+
export const FILTERED_PROP_KEYS = [
|
|
226
|
+
// 组件内部属性
|
|
227
|
+
"loading", "disabled", "skeleton", "renderHints",
|
|
228
|
+
// 动效属性
|
|
229
|
+
"whileHover", "whileClick", "whileFocus", "whileDrag", "whileScroll",
|
|
230
|
+
"whileKey", "whileLoad", "whileReload", "whileActive", "whileDisabled",
|
|
231
|
+
"whileAppear", "whileDisappear", "whileIdle", "whileValidating",
|
|
232
|
+
"whileUploading", "whileEditing", "whileXs", "whileSm", "whileMd",
|
|
233
|
+
"whileLg", "whileXl", "while2xl", "variants", "scroll", "timeline",
|
|
234
|
+
"orchestrate", "orchestrateMode", "orchestrateDelay", "orchestrateOrder",
|
|
235
|
+
"orchestrateGroup", "orchestrateTrigger", "orchestrateTarget",
|
|
236
|
+
"loopMotion",
|
|
237
|
+
// Frame 特有属性
|
|
238
|
+
"morph", "tag", "as", "use", "profile", "scope", "access",
|
|
239
|
+
// 状态机属性
|
|
240
|
+
"Click", "Hover", "Focus", "Key", "Active", "Load", "Reload",
|
|
241
|
+
"Drag", "Scroll", "StartLoad", "Appear", "Disappear", "Idle", "Disabled",
|
|
242
|
+
"Validating", "Uploading", "Editing", "onReload",
|
|
243
|
+
// 布局属性
|
|
244
|
+
"row", "column", "grid", "reverse", "collapsed", "expanded",
|
|
245
|
+
// 响应式布局
|
|
246
|
+
"xs", "sm", "md", "lg", "xl", "2xl",
|
|
247
|
+
// 其他
|
|
248
|
+
"layoutId", "customStates", "whileCollapsed", "whileExpanded", "itemId",
|
|
249
|
+
"responsive", "children"
|
|
250
|
+
] as const;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 几何信息缓存工具
|
|
3
|
+
* v1.0.0
|
|
4
|
+
*
|
|
5
|
+
* 用于缓存元素的 getBoundingClientRect() 结果,
|
|
6
|
+
* 避免在动效编排的 visual-* 排序中频繁调用导致性能问题。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
// 缓存存储:使用 WeakMap 允许元素被 GC 回收
|
|
10
|
+
const cache = new WeakMap<Element, { rect: DOMRect; timestamp: number }>();
|
|
11
|
+
|
|
12
|
+
// 缓存有效期(毫秒)
|
|
13
|
+
const DEFAULT_TTL = 100;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 获取缓存的元素位置信息
|
|
17
|
+
* 如果缓存过期或不存在,则重新获取并缓存
|
|
18
|
+
*
|
|
19
|
+
* @param el - 目标元素
|
|
20
|
+
* @param ttl - 缓存有效期(毫秒),默认 100ms
|
|
21
|
+
* @returns 元素的 DOMRect
|
|
22
|
+
*/
|
|
23
|
+
export function getCachedRect(el: Element, ttl: number = DEFAULT_TTL): DOMRect {
|
|
24
|
+
const cached = cache.get(el);
|
|
25
|
+
const now = Date.now();
|
|
26
|
+
|
|
27
|
+
if (cached && now - cached.timestamp < ttl) {
|
|
28
|
+
return cached.rect;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const rect = el.getBoundingClientRect();
|
|
32
|
+
cache.set(el, { rect, timestamp: now });
|
|
33
|
+
return rect;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 批量获取多个元素的位置信息
|
|
38
|
+
* 对于同批次的元素,共享同一时间戳避免重复计算
|
|
39
|
+
*
|
|
40
|
+
* @param elements - 元素数组
|
|
41
|
+
* @returns 位置信息映射
|
|
42
|
+
*/
|
|
43
|
+
export function batchGetRects(elements: Element[]): Map<Element, DOMRect> {
|
|
44
|
+
const result = new Map<Element, DOMRect>();
|
|
45
|
+
const now = Date.now();
|
|
46
|
+
|
|
47
|
+
for (const el of elements) {
|
|
48
|
+
const cached = cache.get(el);
|
|
49
|
+
|
|
50
|
+
if (cached && now - cached.timestamp < DEFAULT_TTL) {
|
|
51
|
+
result.set(el, cached.rect);
|
|
52
|
+
} else {
|
|
53
|
+
const rect = el.getBoundingClientRect();
|
|
54
|
+
cache.set(el, { rect, timestamp: now });
|
|
55
|
+
result.set(el, rect);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 使缓存失效
|
|
64
|
+
* 在布局变化后调用,强制下次获取时重新计算
|
|
65
|
+
*
|
|
66
|
+
* @param el - 目标元素,如不传则清空所有缓存
|
|
67
|
+
*/
|
|
68
|
+
export function invalidateCache(el?: Element): void {
|
|
69
|
+
if (el) {
|
|
70
|
+
cache.delete(el);
|
|
71
|
+
}
|
|
72
|
+
// 注意:WeakMap 不支持 clear(),如需清空所有缓存,
|
|
73
|
+
// 需要跟踪所有元素或重新创建 WeakMap(不推荐)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 使用 IntersectionObserver 延迟获取位置
|
|
78
|
+
* 仅当元素可见时才计算位置,用于大列表优化
|
|
79
|
+
*
|
|
80
|
+
* @param elements - 元素数组
|
|
81
|
+
* @param callback - 位置计算完成后的回调
|
|
82
|
+
* @param options - IntersectionObserver 选项
|
|
83
|
+
*/
|
|
84
|
+
export function lazyGetRects(
|
|
85
|
+
elements: Element[],
|
|
86
|
+
callback: (rects: Map<Element, DOMRect>) => void,
|
|
87
|
+
options: IntersectionObserverInit = {}
|
|
88
|
+
): () => void {
|
|
89
|
+
const rects = new Map<Element, DOMRect>();
|
|
90
|
+
const pending = new Set(elements);
|
|
91
|
+
|
|
92
|
+
const observer = new IntersectionObserver((entries) => {
|
|
93
|
+
for (const entry of entries) {
|
|
94
|
+
if (entry.isIntersecting && pending.has(entry.target)) {
|
|
95
|
+
const rect = getCachedRect(entry.target);
|
|
96
|
+
rects.set(entry.target, rect);
|
|
97
|
+
pending.delete(entry.target);
|
|
98
|
+
observer.unobserve(entry.target);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// 所有元素都已处理完毕
|
|
103
|
+
if (pending.size === 0) {
|
|
104
|
+
observer.disconnect();
|
|
105
|
+
callback(rects);
|
|
106
|
+
}
|
|
107
|
+
}, {
|
|
108
|
+
threshold: 0.1,
|
|
109
|
+
...options
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
for (const el of elements) {
|
|
113
|
+
observer.observe(el);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// 返回清理函数
|
|
117
|
+
return () => {
|
|
118
|
+
observer.disconnect();
|
|
119
|
+
};
|
|
120
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# image-preview.ts 文档
|
|
2
|
+
|
|
3
|
+
> **职责**: 页面级图片预览事件总线,负责协调 `Image` / `ImageGroup` / `ImageViewer` 之间的打开指令。
|
|
4
|
+
|
|
5
|
+
## 核心职责
|
|
6
|
+
|
|
7
|
+
1. 维护“页面里是否存在自动模式 `ImageViewer`”的计数。
|
|
8
|
+
2. 通过浏览器自定义事件把 `Image` / `ImageGroup` 的打开请求发送给页面级 `ImageViewer`。
|
|
9
|
+
3. 在无 `ImageViewer` 或显式禁用时,提供新窗口打开字符串媒体的回退能力。
|
|
10
|
+
4. 打开请求直接透传稳定媒体列表引用,不再在总线层复制数组。
|
|
11
|
+
|
|
12
|
+
## 输出能力
|
|
13
|
+
|
|
14
|
+
- `getImagePreviewOpenEventName()`
|
|
15
|
+
- `registerImageViewer()`
|
|
16
|
+
- `hasImageViewer()`
|
|
17
|
+
- `openImagePreview(items, index)`
|
|
18
|
+
- `openImageMediaInNewWindow(src)`
|
|
19
|
+
|
|
20
|
+
## 关联组件
|
|
21
|
+
|
|
22
|
+
- [../components/Image.svelte](../components/Image.svelte)
|
|
23
|
+
- [../components/ImageGroup.svelte](../components/ImageGroup.svelte)
|
|
24
|
+
- [../components/ImageViewer.svelte](../components/ImageViewer.svelte)
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import type { Component } from "svelte";
|
|
2
|
+
|
|
3
|
+
export interface ImagePreviewItem {
|
|
4
|
+
src?: string | Component;
|
|
5
|
+
alt?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type PreviewMode = "viewer" | "window" | "none";
|
|
9
|
+
|
|
10
|
+
export interface ImagePreviewRequest {
|
|
11
|
+
items: ImagePreviewItem[];
|
|
12
|
+
index: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const IMAGE_PREVIEW_OPEN_EVENT = "snowui:image-preview:open";
|
|
16
|
+
const IMAGE_VIEWER_COUNT_KEY = "__snowuiImageViewerCount";
|
|
17
|
+
const IMAGE_VIEWER_PRESENCE_EVENT = "snowui:image-viewer:presence";
|
|
18
|
+
const PREVIEW_MODE_DATA_ATTR = "data-snowui-image-viewer-mode";
|
|
19
|
+
const PREVIEW_SCOPE_ATTR = "data-snowui-image-viewer-scope";
|
|
20
|
+
|
|
21
|
+
type ImageViewerWindow = Window &
|
|
22
|
+
typeof globalThis & {
|
|
23
|
+
[IMAGE_VIEWER_COUNT_KEY]?: number;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export function getImagePreviewOpenEventName() {
|
|
27
|
+
return IMAGE_PREVIEW_OPEN_EVENT;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function getImageViewerPresenceEventName() {
|
|
31
|
+
return IMAGE_VIEWER_PRESENCE_EVENT;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function normalizePreviewMode(
|
|
35
|
+
value?: string | null,
|
|
36
|
+
): PreviewMode | undefined {
|
|
37
|
+
if (!value) return undefined;
|
|
38
|
+
if (value === "viewer" || value === "window" || value === "none") {
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function getPreviewModeAttrName() {
|
|
45
|
+
return PREVIEW_MODE_DATA_ATTR;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function getPreviewScopeAttrName() {
|
|
49
|
+
return PREVIEW_SCOPE_ATTR;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function getElementPreviewMode(
|
|
53
|
+
element?: Element | null,
|
|
54
|
+
): PreviewMode | undefined {
|
|
55
|
+
if (!element) return undefined;
|
|
56
|
+
return normalizePreviewMode(element.getAttribute(PREVIEW_MODE_DATA_ATTR));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function isPreviewScopedElement(element?: Element | null) {
|
|
60
|
+
return !!element?.closest(`[${PREVIEW_SCOPE_ATTR}]`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function getDomImagePreviewSrc(image: HTMLImageElement) {
|
|
64
|
+
const currentSrc = image.currentSrc?.trim();
|
|
65
|
+
if (currentSrc) return currentSrc;
|
|
66
|
+
const src = image.getAttribute("src")?.trim();
|
|
67
|
+
return src || undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function toDomImagePreviewItem(image: HTMLImageElement): ImagePreviewItem | null {
|
|
71
|
+
const src = getDomImagePreviewSrc(image);
|
|
72
|
+
if (!src) return null;
|
|
73
|
+
return {
|
|
74
|
+
src,
|
|
75
|
+
alt: image.getAttribute("alt") ?? undefined,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function collectScopedPreviewImages(scope: Element) {
|
|
80
|
+
return Array.from(scope.querySelectorAll("img")).filter((candidate) => {
|
|
81
|
+
if (!(candidate instanceof HTMLImageElement)) return false;
|
|
82
|
+
if (candidate.closest(`[${PREVIEW_SCOPE_ATTR}]`) !== scope) return false;
|
|
83
|
+
return getElementPreviewMode(candidate) === "viewer";
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function resolvePreviewImageElement(target: EventTarget | null) {
|
|
88
|
+
if (!(target instanceof Element)) return null;
|
|
89
|
+
const image = target.closest("img");
|
|
90
|
+
if (!(image instanceof HTMLImageElement)) return null;
|
|
91
|
+
return image;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function resolveDomImagePreviewRequest(
|
|
95
|
+
target: EventTarget | null,
|
|
96
|
+
): ImagePreviewRequest | null {
|
|
97
|
+
const image = resolvePreviewImageElement(target);
|
|
98
|
+
if (!image || getElementPreviewMode(image) !== "viewer") return null;
|
|
99
|
+
|
|
100
|
+
const scope = image.closest(`[${PREVIEW_SCOPE_ATTR}]`);
|
|
101
|
+
if (!scope) {
|
|
102
|
+
const singleItem = toDomImagePreviewItem(image);
|
|
103
|
+
if (!singleItem) return null;
|
|
104
|
+
return {
|
|
105
|
+
items: [singleItem],
|
|
106
|
+
index: 0,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const images = collectScopedPreviewImages(scope);
|
|
111
|
+
const items = images
|
|
112
|
+
.map((candidate) => toDomImagePreviewItem(candidate))
|
|
113
|
+
.filter((item): item is ImagePreviewItem => item !== null);
|
|
114
|
+
const index = images.indexOf(image);
|
|
115
|
+
if (!items.length || index < 0) return null;
|
|
116
|
+
return {
|
|
117
|
+
items,
|
|
118
|
+
index,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function dispatchImageViewerPresence(targetWindow: ImageViewerWindow) {
|
|
123
|
+
const count = targetWindow[IMAGE_VIEWER_COUNT_KEY] ?? 0;
|
|
124
|
+
window.dispatchEvent(
|
|
125
|
+
new CustomEvent<{ count: number; active: boolean }>(
|
|
126
|
+
IMAGE_VIEWER_PRESENCE_EVENT,
|
|
127
|
+
{
|
|
128
|
+
detail: {
|
|
129
|
+
count,
|
|
130
|
+
active: count > 0,
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
),
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function registerImageViewer() {
|
|
138
|
+
if (typeof window === "undefined") return () => { };
|
|
139
|
+
const targetWindow = window as ImageViewerWindow;
|
|
140
|
+
targetWindow[IMAGE_VIEWER_COUNT_KEY] =
|
|
141
|
+
(targetWindow[IMAGE_VIEWER_COUNT_KEY] ?? 0) + 1;
|
|
142
|
+
dispatchImageViewerPresence(targetWindow);
|
|
143
|
+
return () => {
|
|
144
|
+
targetWindow[IMAGE_VIEWER_COUNT_KEY] = Math.max(
|
|
145
|
+
0,
|
|
146
|
+
(targetWindow[IMAGE_VIEWER_COUNT_KEY] ?? 1) - 1,
|
|
147
|
+
);
|
|
148
|
+
dispatchImageViewerPresence(targetWindow);
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function hasImageViewer() {
|
|
153
|
+
if (typeof window === "undefined") return false;
|
|
154
|
+
const targetWindow = window as ImageViewerWindow;
|
|
155
|
+
return (targetWindow[IMAGE_VIEWER_COUNT_KEY] ?? 0) > 0;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
let lastCloseTimestamp = 0;
|
|
159
|
+
|
|
160
|
+
export function markImageViewerClosed() {
|
|
161
|
+
lastCloseTimestamp = Date.now();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function openImagePreview(items: ImagePreviewItem[], index = 0) {
|
|
165
|
+
if (typeof window === "undefined" || !items.length) return;
|
|
166
|
+
const now = Date.now();
|
|
167
|
+
if (now - lastCloseTimestamp < 400) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
const nextIndex = Math.min(Math.max(index, 0), items.length - 1);
|
|
171
|
+
const detail: ImagePreviewRequest = {
|
|
172
|
+
items,
|
|
173
|
+
index: nextIndex,
|
|
174
|
+
};
|
|
175
|
+
window.dispatchEvent(
|
|
176
|
+
new CustomEvent<ImagePreviewRequest>(IMAGE_PREVIEW_OPEN_EVENT, {
|
|
177
|
+
detail,
|
|
178
|
+
}),
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function openImageMediaInNewWindow(src?: string | Component) {
|
|
183
|
+
if (typeof window === "undefined") return;
|
|
184
|
+
if (typeof src !== "string" || src.trim() === "") return;
|
|
185
|
+
window.open(src, "_blank", "noopener,noreferrer");
|
|
186
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* layout-flip.ts
|
|
3
|
+
* v1.0.0
|
|
4
|
+
* 模块级 FLIP 布局转场工具。
|
|
5
|
+
* 独立于 Motion 编排上下文,无需 initMotion() 即可使用。
|
|
6
|
+
* Frame controller 通过此模块暴露 captureFlip / playFlipTo 能力,
|
|
7
|
+
* 供全屏放大、抽屉展开、列表↔详情等共享元素转场场景复用。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export interface FlipTransitionOptions {
|
|
11
|
+
duration?: number;
|
|
12
|
+
easing?: string;
|
|
13
|
+
onComplete?: () => void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const FLIP_DURATION = 300;
|
|
17
|
+
const FLIP_EASING = "ease-out";
|
|
18
|
+
|
|
19
|
+
/** 模块级 DOMRect 注册表,键为 layoutId,无需 initMotion 上下文。 */
|
|
20
|
+
const flipRegistry = new Map<string, DOMRect>();
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 捕获元素当前位置并写入注册表,供后续 beginFlipEnter 读取起点。
|
|
24
|
+
*/
|
|
25
|
+
export function captureFlipRect(id: string, element: HTMLElement): void {
|
|
26
|
+
const rect = element.getBoundingClientRect();
|
|
27
|
+
if (rect.width > 0 && rect.height > 0) {
|
|
28
|
+
flipRegistry.set(id, rect);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 在目标元素挂载后调用:若注册表中存在该 id 的起点 DOMRect,
|
|
34
|
+
* 则以 FLIP 方式从起点(如卡片位置)动画到元素当前位置(如全屏)。
|
|
35
|
+
* 注册表条目消费后自动删除,重复调用安全。
|
|
36
|
+
* 返回 true 表示找到起点并启动了动效。
|
|
37
|
+
*/
|
|
38
|
+
export function beginFlipEnter(id: string, element: HTMLElement, options?: FlipTransitionOptions): boolean {
|
|
39
|
+
const fromRect = flipRegistry.get(id);
|
|
40
|
+
if (!fromRect) return false;
|
|
41
|
+
flipRegistry.delete(id);
|
|
42
|
+
|
|
43
|
+
const toRect = element.getBoundingClientRect();
|
|
44
|
+
if (toRect.width <= 0 || toRect.height <= 0) return false;
|
|
45
|
+
|
|
46
|
+
const duration = options?.duration ?? FLIP_DURATION;
|
|
47
|
+
const easing = options?.easing ?? FLIP_EASING;
|
|
48
|
+
const dx = fromRect.left - toRect.left;
|
|
49
|
+
const dy = fromRect.top - toRect.top;
|
|
50
|
+
const scaleX = fromRect.width / toRect.width;
|
|
51
|
+
const scaleY = fromRect.height / toRect.height;
|
|
52
|
+
|
|
53
|
+
element.style.transformOrigin = "top left";
|
|
54
|
+
element.style.willChange = "transform";
|
|
55
|
+
element.style.transition = "none";
|
|
56
|
+
element.style.transform = `translate(${dx}px, ${dy}px) scale(${scaleX}, ${scaleY})`;
|
|
57
|
+
|
|
58
|
+
requestAnimationFrame(() => {
|
|
59
|
+
requestAnimationFrame(() => {
|
|
60
|
+
element.style.transition = `transform ${duration}ms ${easing}`;
|
|
61
|
+
element.style.transform = "none";
|
|
62
|
+
options?.onComplete?.();
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 将元素以 FLIP 方式过渡到目标位置(退出动效)。
|
|
71
|
+
* 动画结束后调用 options.onComplete,通常用于触发元素卸载。
|
|
72
|
+
*/
|
|
73
|
+
export function applyFlipExit(
|
|
74
|
+
element: HTMLElement,
|
|
75
|
+
target: HTMLElement | DOMRect,
|
|
76
|
+
options?: FlipTransitionOptions,
|
|
77
|
+
): void {
|
|
78
|
+
const fromRect = element.getBoundingClientRect();
|
|
79
|
+
const toRect = target instanceof HTMLElement ? target.getBoundingClientRect() : target;
|
|
80
|
+
if (fromRect.width <= 0 || toRect.width <= 0) {
|
|
81
|
+
options?.onComplete?.();
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const duration = options?.duration ?? FLIP_DURATION;
|
|
86
|
+
const easing = options?.easing ?? FLIP_EASING;
|
|
87
|
+
const dx = toRect.left - fromRect.left;
|
|
88
|
+
const dy = toRect.top - fromRect.top;
|
|
89
|
+
const scaleX = toRect.width / fromRect.width;
|
|
90
|
+
const scaleY = toRect.height / fromRect.height;
|
|
91
|
+
|
|
92
|
+
let done = false;
|
|
93
|
+
const finish = () => {
|
|
94
|
+
if (done) return;
|
|
95
|
+
done = true;
|
|
96
|
+
element.removeEventListener("transitionend", onEnd);
|
|
97
|
+
options?.onComplete?.();
|
|
98
|
+
};
|
|
99
|
+
const onEnd = (e: TransitionEvent) => {
|
|
100
|
+
if (e.target === element && e.propertyName === "transform") finish();
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
element.style.transformOrigin = "top left";
|
|
104
|
+
element.style.willChange = "transform";
|
|
105
|
+
element.style.transition = `transform ${duration}ms ${easing}`;
|
|
106
|
+
element.style.transform = `translate(${dx}px, ${dy}px) scale(${scaleX}, ${scaleY})`;
|
|
107
|
+
element.addEventListener("transitionend", onEnd);
|
|
108
|
+
setTimeout(finish, duration + 50);
|
|
109
|
+
}
|