-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathsplitter.tsx
77 lines (71 loc) · 2.46 KB
/
splitter.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { defineComponent, reactive, ref, provide, type VNode } from 'vue';
import DSplitterBar from './components/splitter-bar';
import { SplitterStore, type SplitterPane } from './splitter-store';
import { splitterProps, SplitterProps, SplitterState } from './splitter-types';
import { useNamespace } from '../../shared/hooks/use-namespace';
import { useResizeObserver } from '@vueuse/core';
import './splitter.scss';
export default defineComponent({
name: 'DSplitter',
components: {
DSplitterBar,
},
props: splitterProps,
emits: [],
setup(props: SplitterProps, ctx) {
const store: SplitterStore = new SplitterStore();
const state = reactive<SplitterState>({
panes: [], // 内嵌面板
});
const ns = useNamespace('splitter');
state.panes = ctx.slots.DSplitterPane?.() || [];
if (state.panes.length > 0) {
state.panes.forEach((item: VNode, index: number) => {
// 解决for循环 pane
if (item.children && item.children.length) {
state.panes.splice(index, 1);
state.panes = [...(item.children as VNode[]), ...state.panes];
}
});
}
store.setPanes({ panes: state.panes as unknown as SplitterPane[] });
provide('orientation', props.orientation);
provide('splitterStore', store);
const domRef = ref<HTMLElement>();
const refreshSplitterContainerSize = () => {
if (!domRef.value) {
return;
}
let containerSize = 0;
if (props.orientation === 'vertical') {
containerSize = domRef.value.clientHeight;
} else {
containerSize = domRef.value.clientWidth;
}
store.setSplitter({ containerSize });
};
useResizeObserver(domRef, refreshSplitterContainerSize);
return () => {
const { splitBarSize, orientation, showCollapseButton } = props;
const wrapperClass = [ns.b(), ns.m(orientation)];
return (
<div class={wrapperClass} ref={domRef}>
{state.panes}
{state.panes
.filter((pane, index, arr) => index !== arr.length - 1)
.map((pane, index) => {
return (
<d-splitter-bar
key={index}
style={`order: ${index * 2 + 1}`}
splitBarSize={splitBarSize}
orientation={orientation}
index={index}
showCollapseButton={showCollapseButton}></d-splitter-bar>
);
})}
</div>
);
};
},
});