@uiw/react-md-editor 3.17.1 → 3.18.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -175,7 +175,7 @@
175
175
  .w-md-editor-toolbar {
176
176
  border-bottom: 1px solid var(--color-border-default);
177
177
  background-color: var(--color-canvas-default);
178
- padding: 5px;
178
+ padding: 5px 5px;
179
179
  display: flex;
180
180
  justify-content: space-between;
181
181
  align-items: center;
@@ -194,11 +194,15 @@
194
194
  margin: 0;
195
195
  padding: 0;
196
196
  list-style: none;
197
+ line-height: initial;
197
198
  }
198
199
  .w-md-editor-toolbar li {
199
200
  display: inline-block;
200
201
  font-size: 14px;
201
202
  }
203
+ .w-md-editor-toolbar li + li {
204
+ margin: 0;
205
+ }
202
206
  .w-md-editor-toolbar li > button {
203
207
  border: none;
204
208
  height: 20px;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uiw/react-md-editor",
3
- "version": "3.17.1",
3
+ "version": "3.18.1",
4
4
  "description": "A markdown editor with preview, implemented with React.js and TypeScript.",
5
5
  "homepage": "https://uiwjs.github.io/react-md-editor/",
6
6
  "author": "kenny wang <wowohoo@qq.com>",
package/src/Editor.tsx CHANGED
@@ -1,5 +1,5 @@
1
- import React, { useEffect, useReducer, useMemo, useRef, useImperativeHandle, CSSProperties } from 'react';
2
- import MarkdownPreview, { MarkdownPreviewProps, MarkdownPreviewRef } from '@uiw/react-markdown-preview';
1
+ import React, { useEffect, useReducer, useMemo, useRef, useImperativeHandle, CSSProperties, PropsWithRef } from 'react';
2
+ import MarkdownPreview, { MarkdownPreviewProps } from '@uiw/react-markdown-preview';
3
3
  import TextArea, { ITextAreaProps } from './components/TextArea';
4
4
  import Toolbar from './components/Toolbar';
5
5
  import DragBar from './components/DragBar';
@@ -96,6 +96,8 @@ export interface MDEditorProps extends Omit<React.HTMLAttributes<HTMLDivElement>
96
96
  * _`toolbar`_ < _`command[].render`_
97
97
  */
98
98
  toolbar?: ICommand['render'];
99
+ /** Custom markdown preview */
100
+ preview?: (source: string, state: ContextStore, dispath: React.Dispatch<ContextStore>) => JSX.Element;
99
101
  };
100
102
  /**
101
103
  * Disable editing area code highlighting. The value is `false`, which increases the editing speed.
@@ -196,7 +198,7 @@ const InternalMDEditor = (
196
198
  barPopup: {},
197
199
  });
198
200
  const container = useRef<HTMLDivElement>(null);
199
- const previewRef = useRef<MarkdownPreviewRef>(null);
201
+ const previewRef = useRef<HTMLDivElement>(null);
200
202
  const enableScrollRef = useRef(enableScroll);
201
203
 
202
204
  useImperativeHandle(ref, () => ({ ...state }));
@@ -271,7 +273,7 @@ const InternalMDEditor = (
271
273
  const handleScroll = (e: React.UIEvent<HTMLDivElement>, type: 'text' | 'preview') => {
272
274
  if (!enableScrollRef.current) return;
273
275
  const textareaDom = textareaDomRef.current;
274
- const previewDom = previewRef.current ? previewRef.current.mdp.current : undefined;
276
+ const previewDom = previewRef.current ? previewRef.current : undefined;
275
277
  if (!initScroll.current) {
276
278
  active.current = type;
277
279
  initScroll.current = true;
@@ -295,32 +297,32 @@ const InternalMDEditor = (
295
297
  }
296
298
  };
297
299
 
298
- const mdPreview = useMemo(
300
+ const previewClassName = `${prefixCls}-preview ${previewOptions.className || ''}`;
301
+ const handlePreviewScroll = (e: React.UIEvent<HTMLDivElement, UIEvent>) => handleScroll(e, 'preview');
302
+ let mdPreview = useMemo(
299
303
  () => (
300
- <MarkdownPreview
301
- {...previewOptions}
302
- onScroll={(e) => handleScroll(e, 'preview')}
303
- ref={previewRef}
304
- source={state.markdown || ''}
305
- className={`${prefixCls}-preview ${previewOptions.className || ''}`}
306
- />
304
+ <div ref={previewRef} className={previewClassName}>
305
+ <MarkdownPreview {...previewOptions} onScroll={handlePreviewScroll} source={state.markdown || ''} />
306
+ </div>
307
307
  ),
308
- [prefixCls, previewOptions, state.markdown],
308
+ [previewClassName, previewOptions, state.markdown],
309
309
  );
310
+ const preview = components?.preview && components?.preview(state.markdown || '', state, dispatch);
311
+ if (preview && React.isValidElement(preview)) {
312
+ mdPreview = (
313
+ <div className={previewClassName} ref={previewRef} onScroll={handlePreviewScroll}>
314
+ {preview}
315
+ </div>
316
+ );
317
+ }
318
+
319
+ const containerStyle = { ...other.style, height: state.height || '100%' };
320
+ const containerClick = () => dispatch({ barPopup: { ...setGroupPopFalse(state.barPopup) } });
321
+ const dragBarChange = (newHeight: number) => dispatch({ height: newHeight });
322
+
310
323
  return (
311
324
  <EditorContext.Provider value={{ ...state, dispatch }}>
312
- <div
313
- ref={container}
314
- className={cls}
315
- {...other}
316
- onClick={() => {
317
- dispatch({ barPopup: { ...setGroupPopFalse(state.barPopup) } });
318
- }}
319
- style={{
320
- ...other.style,
321
- height: state.height || '100%',
322
- }}
323
- >
325
+ <div ref={container} className={cls} {...other} onClick={containerClick} style={containerStyle}>
324
326
  {!hideToolbar && !toolbarBottom && (
325
327
  <Toolbar prefixCls={prefixCls} overflow={overflow} toolbarBottom={toolbarBottom} />
326
328
  )}
@@ -349,9 +351,7 @@ const InternalMDEditor = (
349
351
  height={state.height as number}
350
352
  maxHeight={maxHeight!}
351
353
  minHeight={minHeight!}
352
- onChange={(newHeight) => {
353
- dispatch({ height: newHeight });
354
- }}
354
+ onChange={dragBarChange}
355
355
  />
356
356
  )}
357
357
  {!hideToolbar && toolbarBottom && (
@@ -362,12 +362,10 @@ const InternalMDEditor = (
362
362
  );
363
363
  };
364
364
 
365
- const mdEditor = React.forwardRef<ContextStore, MDEditorProps>(InternalMDEditor);
365
+ type Editor = React.FC<PropsWithRef<MDEditorProps>> & { Markdown: typeof MarkdownPreview };
366
366
 
367
- type MDEditor = typeof mdEditor & {
368
- Markdown: typeof MarkdownPreview;
369
- };
367
+ const mdEditor: Editor = React.forwardRef(InternalMDEditor) as unknown as Editor;
370
368
 
371
- (mdEditor as MDEditor).Markdown = MarkdownPreview;
369
+ mdEditor.Markdown = MarkdownPreview;
372
370
 
373
- export default mdEditor as MDEditor;
371
+ export default mdEditor;
@@ -4,7 +4,7 @@
4
4
  &-toolbar {
5
5
  border-bottom: 1px solid var(--color-border-default);
6
6
  background-color: var(--color-canvas-default);
7
- padding: 5px;
7
+ padding: 5px 5px;
8
8
  display: flex;
9
9
  justify-content: space-between;
10
10
  align-items: center;
@@ -21,10 +21,14 @@
21
21
  margin: 0;
22
22
  padding: 0;
23
23
  list-style: none;
24
+ line-height: initial;
24
25
  }
25
26
  li {
26
27
  display: inline-block;
27
28
  font-size: 14px;
29
+ & + li {
30
+ margin: 0;
31
+ }
28
32
  > button {
29
33
  border: none;
30
34
  height: 20px;