-
-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathindex.tsx
152 lines (143 loc) · 5.49 KB
/
index.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import React, { Fragment, useContext, useEffect, useRef } from 'react';
import { IProps } from '../../Types';
import { EditorContext, PreviewType, ContextStore } from '../../Context';
import { ICommand } from '../../commands';
import Child from './Child';
import './index.less';
export interface IToolbarProps extends IProps {
overflow?: boolean;
onCommand?: (command: ICommand<string>, groupName?: string) => void;
commands?: ICommand<string>[];
isChild?: boolean;
}
export function ToolbarItems(props: IToolbarProps) {
const { prefixCls, overflow } = props;
const { fullscreen, preview, barPopup = {}, components, commandOrchestrator, dispatch } = useContext(EditorContext);
const originalOverflow = useRef('');
function handleClick(command: ICommand<string>, name?: string) {
if (!dispatch) return;
const state: ContextStore = { barPopup: { ...barPopup } };
if (command.keyCommand === 'preview') {
state.preview = command.value as PreviewType;
}
if (command.keyCommand === 'fullscreen') {
state.fullscreen = !fullscreen;
}
if (props.commands && command.keyCommand === 'group') {
props.commands.forEach((item) => {
if (name === item.groupName) {
state.barPopup![name!] = true;
} else if (item.keyCommand) {
state.barPopup![item.groupName!] = false;
}
});
} else if (name || command.parent) {
Object.keys(state.barPopup || {}).forEach((keyName) => {
state.barPopup![keyName] = false;
});
}
if (Object.keys(state).length) {
dispatch({ ...state });
}
commandOrchestrator && commandOrchestrator.executeCommand(command);
}
useEffect(() => {
if (document && overflow) {
if (fullscreen) {
// prevent scroll on fullscreen
document.body.style.overflow = 'hidden';
} else {
// get the original overflow only the first time
if (!originalOverflow.current) {
originalOverflow.current = window.getComputedStyle(document.body, null).overflow;
}
// reset to the original overflow
document.body.style.overflow = originalOverflow.current;
}
}
}, [fullscreen, originalOverflow, overflow]);
return (
<ul>
{(props.commands || []).map((item, idx) => {
if (item.keyCommand === 'divider') {
return <li key={idx} {...item.liProps} className={`${prefixCls}-toolbar-divider`} />;
}
if (!item.keyCommand) return <Fragment key={idx} />;
const activeBtn =
(fullscreen && item.keyCommand === 'fullscreen') || (item.keyCommand === 'preview' && preview === item.value);
const childNode =
item.children && typeof item.children === 'function'
? item.children({
getState: () => commandOrchestrator!.getState(),
textApi: commandOrchestrator ? commandOrchestrator!.textApi : undefined,
close: () => handleClick({}, item.groupName),
execute: () => handleClick({ execute: item.execute }),
dispatch,
})
: undefined;
const disabled = barPopup && preview && preview === 'preview' && !/(preview|fullscreen)/.test(item.keyCommand);
const render = components?.toolbar || item.render;
const com = (
render && typeof render === 'function' ? render(item, !!disabled, handleClick, idx) : null
) as React.ReactElement;
return (
<li key={idx} {...item.liProps} className={activeBtn ? `active` : ''}>
{com && React.isValidElement(com) && com}
{!com && !item.buttonProps && item.icon}
{!com &&
item.buttonProps &&
React.createElement(
'button',
{
type: 'button',
key: idx,
disabled,
'data-name': item.name,
...item.buttonProps,
onClick: (evn: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
evn.stopPropagation();
handleClick(item, item.groupName);
},
},
item.icon,
)}
{item.children && (
<Child
overflow={overflow}
groupName={item.groupName}
prefixCls={prefixCls}
children={childNode}
commands={Array.isArray(item.children) ? item.children : undefined}
/>
)}
</li>
);
})}
</ul>
);
}
export default function Toolbar(props: IToolbarProps = {}) {
const { prefixCls, isChild, className } = props;
const { commands, extraCommands } = useContext(EditorContext);
return (
<div className={`${prefixCls}-toolbar ${className}`}>
<ToolbarItems {...props} commands={props.commands || commands || []} />
{!isChild && <ToolbarItems {...props} commands={extraCommands || []} />}
</div>
);
}
interface ToolbarVisibilityProps {
hideToolbar?: boolean;
toolbarBottom: boolean;
placement: 'bottom' | 'top';
overflow: boolean;
prefixCls: string;
}
export function ToolbarVisibility(props: ToolbarVisibilityProps) {
const { hideToolbar, toolbarBottom, placement, overflow, prefixCls } = props;
if (hideToolbar || (placement === 'bottom' && !toolbarBottom) || (placement === 'top' && toolbarBottom)) {
return null;
}
const cls = toolbarBottom ? 'bottom' : '';
return <Toolbar prefixCls={prefixCls} overflow={overflow} className={cls} />;
}