Skip to content

Commit

Permalink
refactor: optimizi toolbar.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Nov 8, 2023
1 parent 7e7fd55 commit 64319c2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 33 deletions.
22 changes: 15 additions & 7 deletions core/src/Editor.nohighlight.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useReducer, useMemo, useRef, useImperativeHandle } from 'react';
import MarkdownPreview from '@uiw/react-markdown-preview/nohighlight';
import TextArea from './components/TextArea/index.nohighlight';
import Toolbar from './components/Toolbar';
import { ToolbarVisibility } from './components/Toolbar';
import DragBar from './components/DragBar';
import { getCommands, getExtraCommands, ICommand, TextState, TextAreaCommandOrchestrator } from './commands';
import { reducer, EditorContext, ContextStore } from './Context';
Expand Down Expand Up @@ -219,9 +219,13 @@ const InternalMDEditor = React.forwardRef<RefMDEditor, MDEditorProps>(
return (
<EditorContext.Provider value={{ ...state, dispatch }}>
<div ref={container} className={cls} {...other} onClick={containerClick} style={containerStyle}>
{!hideToolbar && !toolbarBottom && (
<Toolbar prefixCls={prefixCls} overflow={overflow} toolbarBottom={toolbarBottom} />
)}
<ToolbarVisibility
hideToolbar={hideToolbar}
toolbarBottom={toolbarBottom}
prefixCls={prefixCls}
overflow={overflow}
placement="top"
/>
<div className={`${prefixCls}-content`}>
{/(edit|live)/.test(state.preview || '') && (
<TextArea
Expand All @@ -245,9 +249,13 @@ const InternalMDEditor = React.forwardRef<RefMDEditor, MDEditorProps>(
onChange={dragBarChange}
/>
)}
{!hideToolbar && toolbarBottom && (
<Toolbar prefixCls={prefixCls} overflow={overflow} toolbarBottom={toolbarBottom} />
)}
<ToolbarVisibility
hideToolbar={hideToolbar}
toolbarBottom={toolbarBottom}
prefixCls={prefixCls}
overflow={overflow}
placement="bottom"
/>
</div>
</EditorContext.Provider>
);
Expand Down
53 changes: 31 additions & 22 deletions core/src/Editor.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useReducer, useMemo, useRef, useImperativeHandle } from 'react';
import React, { useEffect, useReducer, useMemo, useRef, useImperativeHandle, Fragment } from 'react';
import MarkdownPreview from '@uiw/react-markdown-preview';
import TextArea from './components/TextArea';
import Toolbar from './components/Toolbar';
import { ToolbarVisibility } from './components/Toolbar';
import DragBar from './components/DragBar';
import { getCommands, getExtraCommands, ICommand, TextState, TextAreaCommandOrchestrator } from './commands';
import { reducer, EditorContext, ContextStore } from './Context';
Expand Down Expand Up @@ -216,26 +216,31 @@ const InternalMDEditor = React.forwardRef<RefMDEditor, MDEditorProps>(
});
}
};
const contentView = /(live|preview)/.test(state.preview || '') && (
<Fragment>
<TextArea
className={`${prefixCls}-input`}
prefixCls={prefixCls}
autoFocus={autoFocus}
{...textareaProps}
onChange={changeHandle}
renderTextarea={components?.textarea || renderTextarea}
onScroll={(e) => handleScroll(e, 'text')}
/>
{mdPreview}
</Fragment>
);
return (
<EditorContext.Provider value={{ ...state, dispatch }}>
<div ref={container} className={cls} {...other} onClick={containerClick} style={containerStyle}>
{!hideToolbar && !toolbarBottom && (
<Toolbar prefixCls={prefixCls} overflow={overflow} toolbarBottom={toolbarBottom} />
)}
<div className={`${prefixCls}-content`}>
{/(edit|live)/.test(state.preview || '') && (
<TextArea
className={`${prefixCls}-input`}
prefixCls={prefixCls}
autoFocus={autoFocus}
{...textareaProps}
onChange={changeHandle}
renderTextarea={components?.textarea || renderTextarea}
onScroll={(e) => handleScroll(e, 'text')}
/>
)}
{/(live|preview)/.test(state.preview || '') && mdPreview}
</div>
<ToolbarVisibility
hideToolbar={hideToolbar}
toolbarBottom={toolbarBottom}
prefixCls={prefixCls}
overflow={overflow}
placement="top"
/>
<div className={`${prefixCls}-content`}>{contentView}</div>
{visibleDragbar && !state.fullscreen && (
<DragBar
prefixCls={prefixCls}
Expand All @@ -245,9 +250,13 @@ const InternalMDEditor = React.forwardRef<RefMDEditor, MDEditorProps>(
onChange={dragBarChange}
/>
)}
{!hideToolbar && toolbarBottom && (
<Toolbar prefixCls={prefixCls} overflow={overflow} toolbarBottom={toolbarBottom} />
)}
<ToolbarVisibility
hideToolbar={hideToolbar}
toolbarBottom={toolbarBottom}
prefixCls={prefixCls}
overflow={overflow}
placement="bottom"
/>
</div>
</EditorContext.Provider>
);
Expand Down
23 changes: 19 additions & 4 deletions core/src/components/Toolbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import './index.less';

export interface IToolbarProps extends IProps {
overflow?: boolean;
toolbarBottom?: boolean;
onCommand?: (command: ICommand<string>, groupName?: string) => void;
commands?: ICommand<string>[];
isChild?: boolean;
Expand Down Expand Up @@ -125,13 +124,29 @@ export function ToolbarItems(props: IToolbarProps) {
}

export default function Toolbar(props: IToolbarProps = {}) {
const { prefixCls, toolbarBottom, isChild } = props;
const { prefixCls, isChild, className } = props;
const { commands, extraCommands } = useContext(EditorContext);
const bottomClassName = toolbarBottom ? 'bottom' : '';
return (
<div className={`${prefixCls}-toolbar ${bottomClassName}`}>
<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} />;
}

1 comment on commit 64319c2

@vercel
Copy link

@vercel vercel bot commented on 64319c2 Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.