forked from ant-design/md-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloatBar.tsx
133 lines (118 loc) · 3.71 KB
/
FloatBar.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { ConfigProvider } from 'antd';
import classNames from 'classnames';
import { observer } from 'mobx-react';
import React, { useCallback, useContext, useEffect } from 'react';
import ReactDOM from 'react-dom';
import { BaseRange, Editor, Range, Transforms } from 'slate';
import { IEditor } from '../../../index';
import { useEditorStore } from '../../store';
import { getSelRect } from '../../utils/dom';
import { useLocalState } from '../../utils/useLocalState';
import { BaseToolBar } from './BaseBar';
import { useStyle } from './floatBarStyle';
import { ReadonlyBaseBar } from './ReadonlyBaseBar';
const fileMap = new Map<string, IEditor>();
/**
* 浮动工具栏,用于设置文本样式
*/
export const FloatBar = observer((props: { readonly: boolean }) => {
const { store } = useEditorStore();
const [state, setState] = useLocalState({
open: false,
left: 0,
top: 0,
url: '',
});
const sel = React.useRef<BaseRange>();
const resize = useCallback(
(force = false) => {
if (store.domRect && !store.openLinkPanel) {
let left = store.domRect.x;
left = left - ((props.readonly ? 65 : 178) - store.domRect.width) / 2;
const container = store.container!;
if (left < 4) left = 4;
const barWidth = props.readonly ? 65 : 232;
if (left > container.clientWidth - barWidth)
left = container.clientWidth - barWidth / 2;
let top = state.open && !force ? state.top : store.domRect.top - 32;
setState({
open: true,
left: Math.max(left, 4),
top: Math.max(top, 4),
});
}
},
[props.readonly, state.open],
);
useEffect(() => {
if (store.domRect && store.editor) {
resize(true);
sel.current = store.editor.selection!;
} else {
setState({ open: false });
fileMap.clear();
}
}, [store.domRect]);
useEffect(() => {
store.setFloatBarOpen(state.open);
const close = (e: KeyboardEvent) => {
if (e.key === 'Escape' && !store.openLinkPanel) {
e.preventDefault();
setState({ open: false });
fileMap.clear();
if (!sel.current) return;
const end = Range.end(sel.current!).path;
if (Editor.hasPath(store?.editor, end)) {
Transforms.select(store?.editor, Editor.end(store?.editor, end));
}
store.setFloatBarOpen(false);
}
};
window.addEventListener('keydown', close);
return () => {
window.removeEventListener('keydown', close);
store.setFloatBarOpen(false);
};
}, [state.open]);
useEffect(() => {
const change = () => {
if (state.open) {
const rect = getSelRect();
if (rect) {
store.setState((state) => (state.domRect = rect));
}
resize(true);
}
};
window.addEventListener('resize', change);
return () => window.removeEventListener('resize', change);
}, []);
const context = useContext(ConfigProvider.ConfigContext);
const baseClassName = context.getPrefixCls(`md-editor-float-bar`);
const { wrapSSR, hashId } = useStyle(baseClassName);
if (!store.container) return null;
return ReactDOM.createPortal(
wrapSSR(
<div
style={{
left: state.left,
top: state.top,
position: 'fixed',
display: state.open ? undefined : 'none',
}}
onMouseDown={(e) => {
e.preventDefault();
e.stopPropagation();
}}
className={classNames(baseClassName, hashId)}
>
{props.readonly ? (
<ReadonlyBaseBar prefix={baseClassName} hashId={hashId} />
) : (
<BaseToolBar prefix={baseClassName} hashId={hashId} />
)}
</div>,
),
document.body,
);
});