Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add editable preview mode to Markdown editor #669

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/Context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { ICommand, TextAreaCommandOrchestrator } from './commands';
import { MDEditorProps } from './Types';

export type PreviewType = 'live' | 'edit' | 'preview';
export type PreviewType = 'live' | 'edit' | 'preview' | 'editablePreview';

export interface ContextStore {
components?: MDEditorProps['components'];
Expand Down
2 changes: 1 addition & 1 deletion core/src/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ const InternalMDEditor = React.forwardRef<RefMDEditor, MDEditorProps>(
placement="top"
/>
<div className={`${prefixCls}-content`}>
{/(edit|live)/.test(state.preview || '') && (
{/(edit|live|editablePreview)/.test(state.preview || '') && (
<TextArea
className={`${prefixCls}-input`}
prefixCls={prefixCls}
Expand Down
29 changes: 29 additions & 0 deletions core/src/commands/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,32 @@ export const codeLive: ICommand = {
}
},
};

export const editablePreview: ICommand = {
Copy link
Member

Choose a reason for hiding this comment

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

export * from './commands';
export * from './commands/group';
export * from './utils/markdownUtils';
export * from './utils/InsertTextAtPosition';

export { editablePreview } from './commands/preview';

@vishwamartur It is necessary to export editablePreview in the entry point.

name: 'editablePreview',
keyCommand: 'editablePreview',
value: 'editablePreview',
shortcuts: 'ctrlcmd+e',
buttonProps: { 'aria-label': 'Editable Preview (ctrl + e)', title: 'Editable Preview (ctrl + e)' },
icon: (
<svg width="12" height="12" viewBox="0 0 520 520">
<polygon fill="currentColor" points="0 71.293 0 122 179 122 179 397 0 397 0 449.707 232 449.413 232 71.293" />
<polygon
fill="currentColor"
points="289 71.293 520 71.293 520 122 341 123 341 396 520 396 520 449.707 289 449.413"
/>
</svg>
Comment on lines +101 to +108
Copy link
Member

Choose a reason for hiding this comment

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

@vishwamartur Do you need me to make a default icon for you?

),
execute: (
state: TextState,
api: TextAreaTextApi,
dispatch?: React.Dispatch<ContextStore>,
executeCommandState?: ExecuteCommandState,
shortcuts?: string[],
) => {
api.textArea.focus();
if (shortcuts && dispatch && executeCommandState) {
dispatch({ preview: 'editablePreview' });
}
},
};
8 changes: 6 additions & 2 deletions core/src/components/TextArea/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type TextAreaRef = {

export default function TextArea(props: ITextAreaProps) {
const { prefixCls, className, onScroll, renderTextarea, ...otherProps } = props || {};
const { markdown, scrollTop, commands, minHeight, highlightEnable, extraCommands, dispatch } =
const { markdown, scrollTop, commands, minHeight, highlightEnable, extraCommands, dispatch, preview } =
useContext(EditorContext);
const textRef = React.useRef<HTMLTextAreaElement>(null);
const executeRef = React.useRef<TextAreaCommandOrchestrator>();
Expand Down Expand Up @@ -103,7 +103,11 @@ export default function TextArea(props: ITextAreaProps) {
) : (
<Fragment>
{highlightEnable && <Markdown prefixCls={prefixCls} />}
<Textarea prefixCls={prefixCls} {...otherProps} style={textStyle} />
{preview === 'editablePreview' ? (
<Textarea prefixCls={prefixCls} {...otherProps} style={textStyle} />
) : (
<Textarea prefixCls={prefixCls} {...otherProps} style={textStyle} readOnly />
)}
Copy link
Member

Choose a reason for hiding this comment

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

@vishwamartur Can this code be optimized to the following example?

<Textarea prefixCls={prefixCls} {...otherProps} style={textStyle} readOnly={preview != 'editablePreview'} />

</Fragment>
)}
</div>
Expand Down