Skip to content

Commit 64319c2

Browse files
committed
refactor: optimizi toolbar.
1 parent 7e7fd55 commit 64319c2

File tree

3 files changed

+65
-33
lines changed

3 files changed

+65
-33
lines changed

core/src/Editor.nohighlight.tsx

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useEffect, useReducer, useMemo, useRef, useImperativeHandle } from 'react';
22
import MarkdownPreview from '@uiw/react-markdown-preview/nohighlight';
33
import TextArea from './components/TextArea/index.nohighlight';
4-
import Toolbar from './components/Toolbar';
4+
import { ToolbarVisibility } from './components/Toolbar';
55
import DragBar from './components/DragBar';
66
import { getCommands, getExtraCommands, ICommand, TextState, TextAreaCommandOrchestrator } from './commands';
77
import { reducer, EditorContext, ContextStore } from './Context';
@@ -219,9 +219,13 @@ const InternalMDEditor = React.forwardRef<RefMDEditor, MDEditorProps>(
219219
return (
220220
<EditorContext.Provider value={{ ...state, dispatch }}>
221221
<div ref={container} className={cls} {...other} onClick={containerClick} style={containerStyle}>
222-
{!hideToolbar && !toolbarBottom && (
223-
<Toolbar prefixCls={prefixCls} overflow={overflow} toolbarBottom={toolbarBottom} />
224-
)}
222+
<ToolbarVisibility
223+
hideToolbar={hideToolbar}
224+
toolbarBottom={toolbarBottom}
225+
prefixCls={prefixCls}
226+
overflow={overflow}
227+
placement="top"
228+
/>
225229
<div className={`${prefixCls}-content`}>
226230
{/(edit|live)/.test(state.preview || '') && (
227231
<TextArea
@@ -245,9 +249,13 @@ const InternalMDEditor = React.forwardRef<RefMDEditor, MDEditorProps>(
245249
onChange={dragBarChange}
246250
/>
247251
)}
248-
{!hideToolbar && toolbarBottom && (
249-
<Toolbar prefixCls={prefixCls} overflow={overflow} toolbarBottom={toolbarBottom} />
250-
)}
252+
<ToolbarVisibility
253+
hideToolbar={hideToolbar}
254+
toolbarBottom={toolbarBottom}
255+
prefixCls={prefixCls}
256+
overflow={overflow}
257+
placement="bottom"
258+
/>
251259
</div>
252260
</EditorContext.Provider>
253261
);

core/src/Editor.tsx

+31-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React, { useEffect, useReducer, useMemo, useRef, useImperativeHandle } from 'react';
1+
import React, { useEffect, useReducer, useMemo, useRef, useImperativeHandle, Fragment } from 'react';
22
import MarkdownPreview from '@uiw/react-markdown-preview';
33
import TextArea from './components/TextArea';
4-
import Toolbar from './components/Toolbar';
4+
import { ToolbarVisibility } from './components/Toolbar';
55
import DragBar from './components/DragBar';
66
import { getCommands, getExtraCommands, ICommand, TextState, TextAreaCommandOrchestrator } from './commands';
77
import { reducer, EditorContext, ContextStore } from './Context';
@@ -216,26 +216,31 @@ const InternalMDEditor = React.forwardRef<RefMDEditor, MDEditorProps>(
216216
});
217217
}
218218
};
219+
const contentView = /(live|preview)/.test(state.preview || '') && (
220+
<Fragment>
221+
<TextArea
222+
className={`${prefixCls}-input`}
223+
prefixCls={prefixCls}
224+
autoFocus={autoFocus}
225+
{...textareaProps}
226+
onChange={changeHandle}
227+
renderTextarea={components?.textarea || renderTextarea}
228+
onScroll={(e) => handleScroll(e, 'text')}
229+
/>
230+
{mdPreview}
231+
</Fragment>
232+
);
219233
return (
220234
<EditorContext.Provider value={{ ...state, dispatch }}>
221235
<div ref={container} className={cls} {...other} onClick={containerClick} style={containerStyle}>
222-
{!hideToolbar && !toolbarBottom && (
223-
<Toolbar prefixCls={prefixCls} overflow={overflow} toolbarBottom={toolbarBottom} />
224-
)}
225-
<div className={`${prefixCls}-content`}>
226-
{/(edit|live)/.test(state.preview || '') && (
227-
<TextArea
228-
className={`${prefixCls}-input`}
229-
prefixCls={prefixCls}
230-
autoFocus={autoFocus}
231-
{...textareaProps}
232-
onChange={changeHandle}
233-
renderTextarea={components?.textarea || renderTextarea}
234-
onScroll={(e) => handleScroll(e, 'text')}
235-
/>
236-
)}
237-
{/(live|preview)/.test(state.preview || '') && mdPreview}
238-
</div>
236+
<ToolbarVisibility
237+
hideToolbar={hideToolbar}
238+
toolbarBottom={toolbarBottom}
239+
prefixCls={prefixCls}
240+
overflow={overflow}
241+
placement="top"
242+
/>
243+
<div className={`${prefixCls}-content`}>{contentView}</div>
239244
{visibleDragbar && !state.fullscreen && (
240245
<DragBar
241246
prefixCls={prefixCls}
@@ -245,9 +250,13 @@ const InternalMDEditor = React.forwardRef<RefMDEditor, MDEditorProps>(
245250
onChange={dragBarChange}
246251
/>
247252
)}
248-
{!hideToolbar && toolbarBottom && (
249-
<Toolbar prefixCls={prefixCls} overflow={overflow} toolbarBottom={toolbarBottom} />
250-
)}
253+
<ToolbarVisibility
254+
hideToolbar={hideToolbar}
255+
toolbarBottom={toolbarBottom}
256+
prefixCls={prefixCls}
257+
overflow={overflow}
258+
placement="bottom"
259+
/>
251260
</div>
252261
</EditorContext.Provider>
253262
);

core/src/components/Toolbar/index.tsx

+19-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import './index.less';
77

88
export interface IToolbarProps extends IProps {
99
overflow?: boolean;
10-
toolbarBottom?: boolean;
1110
onCommand?: (command: ICommand<string>, groupName?: string) => void;
1211
commands?: ICommand<string>[];
1312
isChild?: boolean;
@@ -125,13 +124,29 @@ export function ToolbarItems(props: IToolbarProps) {
125124
}
126125

127126
export default function Toolbar(props: IToolbarProps = {}) {
128-
const { prefixCls, toolbarBottom, isChild } = props;
127+
const { prefixCls, isChild, className } = props;
129128
const { commands, extraCommands } = useContext(EditorContext);
130-
const bottomClassName = toolbarBottom ? 'bottom' : '';
131129
return (
132-
<div className={`${prefixCls}-toolbar ${bottomClassName}`}>
130+
<div className={`${prefixCls}-toolbar ${className}`}>
133131
<ToolbarItems {...props} commands={props.commands || commands || []} />
134132
{!isChild && <ToolbarItems {...props} commands={extraCommands || []} />}
135133
</div>
136134
);
137135
}
136+
137+
interface ToolbarVisibilityProps {
138+
hideToolbar?: boolean;
139+
toolbarBottom: boolean;
140+
placement: 'bottom' | 'top';
141+
overflow: boolean;
142+
prefixCls: string;
143+
}
144+
145+
export function ToolbarVisibility(props: ToolbarVisibilityProps) {
146+
const { hideToolbar, toolbarBottom, placement, overflow, prefixCls } = props;
147+
if (hideToolbar || (placement === 'bottom' && !toolbarBottom) || (placement === 'top' && toolbarBottom)) {
148+
return null;
149+
}
150+
const cls = toolbarBottom ? 'bottom' : '';
151+
return <Toolbar prefixCls={prefixCls} overflow={overflow} className={cls} />;
152+
}

0 commit comments

Comments
 (0)