@uiw/react-md-editor 3.15.0 → 3.17.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.
Files changed (48) hide show
  1. package/README.md +69 -2
  2. package/dist/mdeditor.css +15 -5
  3. package/dist/mdeditor.js +70 -653
  4. package/dist/mdeditor.min.css +1 -1
  5. package/dist/mdeditor.min.js +1 -1
  6. package/esm/Context.d.ts +15 -0
  7. package/esm/Context.js.map +2 -2
  8. package/esm/Editor.d.ts +19 -1
  9. package/esm/Editor.js +4 -8
  10. package/esm/Editor.js.map +4 -3
  11. package/esm/commands/index.d.ts +2 -0
  12. package/esm/commands/index.js.map +2 -2
  13. package/esm/components/DragBar/index.css +1 -0
  14. package/esm/components/DragBar/index.less +1 -0
  15. package/esm/components/Toolbar/Child.js +0 -1
  16. package/esm/components/Toolbar/Child.js.map +2 -2
  17. package/esm/components/Toolbar/index.css +2 -1
  18. package/esm/components/Toolbar/index.d.ts +0 -2
  19. package/esm/components/Toolbar/index.js +4 -5
  20. package/esm/components/Toolbar/index.js.map +7 -3
  21. package/esm/components/Toolbar/index.less +2 -1
  22. package/esm/index.css +10 -3
  23. package/esm/index.less +9 -3
  24. package/lib/Context.d.ts +15 -0
  25. package/lib/Context.js.map +2 -2
  26. package/lib/Editor.d.ts +19 -1
  27. package/lib/Editor.js +4 -9
  28. package/lib/Editor.js.map +4 -3
  29. package/lib/commands/index.d.ts +2 -0
  30. package/lib/commands/index.js.map +2 -2
  31. package/lib/components/DragBar/index.less +1 -0
  32. package/lib/components/Toolbar/Child.js +0 -1
  33. package/lib/components/Toolbar/Child.js.map +2 -2
  34. package/lib/components/Toolbar/index.d.ts +0 -2
  35. package/lib/components/Toolbar/index.js +4 -6
  36. package/lib/components/Toolbar/index.js.map +8 -4
  37. package/lib/components/Toolbar/index.less +2 -1
  38. package/lib/index.less +9 -3
  39. package/markdown-editor.css +13 -4
  40. package/package.json +2 -2
  41. package/src/Context.tsx +2 -0
  42. package/src/Editor.tsx +25 -11
  43. package/src/commands/index.ts +7 -0
  44. package/src/components/DragBar/index.less +1 -0
  45. package/src/components/Toolbar/Child.tsx +1 -1
  46. package/src/components/Toolbar/index.less +2 -1
  47. package/src/components/Toolbar/index.tsx +11 -6
  48. package/src/index.less +9 -3
package/src/Context.tsx CHANGED
@@ -1,9 +1,11 @@
1
1
  import React from 'react';
2
2
  import { ICommand, TextAreaCommandOrchestrator } from './commands';
3
+ import { MDEditorProps } from './Editor';
3
4
 
4
5
  export type PreviewType = 'live' | 'edit' | 'preview';
5
6
 
6
7
  export type ContextStore = {
8
+ components?: MDEditorProps['components'];
7
9
  commands?: ICommand<string>[];
8
10
  extraCommands?: ICommand<string>[];
9
11
  markdown?: string;
package/src/Editor.tsx CHANGED
@@ -39,6 +39,9 @@ export interface MDEditorProps extends Omit<React.HTMLAttributes<HTMLDivElement>
39
39
  /**
40
40
  * Custom toolbar heigth
41
41
  * @default 29px
42
+ *
43
+ * @deprecated toolbar height adaptive: https://github.com/uiwjs/react-md-editor/issues/427
44
+ *
42
45
  */
43
46
  toolbarHeight?: number;
44
47
  /**
@@ -77,8 +80,23 @@ export interface MDEditorProps extends Omit<React.HTMLAttributes<HTMLDivElement>
77
80
  * Set the `textarea` related props.
78
81
  */
79
82
  textareaProps?: ITextAreaProps;
80
- /** Use div to replace TextArea or re-render TextArea */
83
+ /**
84
+ * Use div to replace TextArea or re-render TextArea
85
+ * @deprecated Please use ~~`renderTextarea`~~ -> `components`
86
+ */
81
87
  renderTextarea?: ITextAreaProps['renderTextarea'];
88
+ /**
89
+ * re-render element
90
+ */
91
+ components?: {
92
+ /** Use div to replace TextArea or re-render TextArea */
93
+ textarea?: ITextAreaProps['renderTextarea'];
94
+ /**
95
+ * Override the default command element
96
+ * _`toolbar`_ < _`command[].render`_
97
+ */
98
+ toolbar?: ICommand['render'];
99
+ };
82
100
  /**
83
101
  * Disable editing area code highlighting. The value is `false`, which increases the editing speed.
84
102
  * @default true
@@ -135,7 +153,6 @@ const InternalMDEditor = (
135
153
  commandsFilter,
136
154
  extraCommands = getExtraCommands(),
137
155
  height = 200,
138
- toolbarHeight = 29,
139
156
  enableScroll = true,
140
157
  visibleDragbar = typeof props.visiableDragbar === 'boolean' ? props.visiableDragbar : true,
141
158
  highlightEnable = true,
@@ -153,6 +170,7 @@ const InternalMDEditor = (
153
170
  onHeightChange,
154
171
  hideToolbar,
155
172
  toolbarBottom = false,
173
+ components,
156
174
  renderTextarea,
157
175
  ...other
158
176
  } = props || {};
@@ -165,6 +183,7 @@ const InternalMDEditor = (
165
183
  let [state, dispatch] = useReducer(reducer, {
166
184
  markdown: propsValue,
167
185
  preview: previewType,
186
+ components,
168
187
  height,
169
188
  highlightEnable,
170
189
  tabSize,
@@ -303,14 +322,9 @@ const InternalMDEditor = (
303
322
  }}
304
323
  >
305
324
  {!hideToolbar && !toolbarBottom && (
306
- <Toolbar prefixCls={prefixCls} height={toolbarHeight} overflow={overflow} toolbarBottom={toolbarBottom} />
325
+ <Toolbar prefixCls={prefixCls} overflow={overflow} toolbarBottom={toolbarBottom} />
307
326
  )}
308
- <div
309
- className={`${prefixCls}-content`}
310
- style={{
311
- height: `calc(100% - ${toolbarHeight}px)`,
312
- }}
313
- >
327
+ <div className={`${prefixCls}-content`}>
314
328
  {/(edit|live)/.test(state.preview || '') && (
315
329
  <TextArea
316
330
  className={`${prefixCls}-input`}
@@ -323,7 +337,7 @@ const InternalMDEditor = (
323
337
  textareaProps.onChange(evn);
324
338
  }
325
339
  }}
326
- renderTextarea={renderTextarea}
340
+ renderTextarea={components?.textarea || renderTextarea}
327
341
  onScroll={(e) => handleScroll(e, 'text')}
328
342
  />
329
343
  )}
@@ -341,7 +355,7 @@ const InternalMDEditor = (
341
355
  />
342
356
  )}
343
357
  {!hideToolbar && toolbarBottom && (
344
- <Toolbar prefixCls={prefixCls} height={toolbarHeight} overflow={overflow} toolbarBottom={toolbarBottom} />
358
+ <Toolbar prefixCls={prefixCls} overflow={overflow} toolbarBottom={toolbarBottom} />
345
359
  )}
346
360
  </div>
347
361
  </EditorContext.Provider>
@@ -1,3 +1,4 @@
1
+ import React from 'react';
1
2
  import { bold } from './bold';
2
3
  import { code, codeBlock } from './code';
3
4
  import { italic } from './italic';
@@ -50,6 +51,12 @@ export interface ICommandBase<T> {
50
51
  position?: 'right';
51
52
  liProps?: React.LiHTMLAttributes<HTMLLIElement>;
52
53
  buttonProps?: React.ButtonHTMLAttributes<HTMLButtonElement> | null;
54
+ render?: (
55
+ command: ICommand<T>,
56
+ disabled: boolean,
57
+ executeCommand: (command: ICommand<T>, name?: string) => void,
58
+ index: number,
59
+ ) => void | undefined | null | React.ReactElement;
53
60
  execute?: (
54
61
  state: TextState,
55
62
  api: TextAreaTextApi,
@@ -5,6 +5,7 @@
5
5
  position: absolute;
6
6
  cursor: s-resize;
7
7
  right: 0;
8
+ bottom: 0;
8
9
  margin-top: -11px;
9
10
  margin-right: 0;
10
11
  width: 14px;
@@ -17,7 +17,7 @@ export default function Child(props: ChildProps) {
17
17
  className={`${prefixCls}-toolbar-child ${groupName && barPopup[groupName] ? 'active' : ''}`}
18
18
  onClick={(e) => e.stopPropagation()}
19
19
  >
20
- {Array.isArray(commands) ? <Toolbar commands={commands} {...props} height="" isChild /> : children}
20
+ {Array.isArray(commands) ? <Toolbar commands={commands} {...props} isChild /> : children}
21
21
  </div>
22
22
  ),
23
23
  // eslint-disable-next-line react-hooks/exhaustive-deps
@@ -4,12 +4,13 @@
4
4
  &-toolbar {
5
5
  border-bottom: 1px solid var(--color-border-default);
6
6
  background-color: var(--color-canvas-default);
7
- padding: 0 5px 0 5px;
7
+ padding: 5px;
8
8
  display: flex;
9
9
  justify-content: space-between;
10
10
  align-items: center;
11
11
  border-radius: 3px 3px 0 0;
12
12
  user-select: none;
13
+ flex-wrap: wrap;
13
14
  &.bottom {
14
15
  border-bottom: 0px;
15
16
  border-top: 1px solid var(--color-border-default);
@@ -7,7 +7,6 @@ import './index.less';
7
7
 
8
8
  export interface IToolbarProps extends IProps {
9
9
  overflow?: boolean;
10
- height?: React.CSSProperties['height'];
11
10
  toolbarBottom?: boolean;
12
11
  onCommand?: (command: ICommand<string>, groupName?: string) => void;
13
12
  commands?: ICommand<string>[];
@@ -16,7 +15,7 @@ export interface IToolbarProps extends IProps {
16
15
 
17
16
  export function ToolbarItems(props: IToolbarProps) {
18
17
  const { prefixCls, overflow } = props;
19
- const { fullscreen, preview, barPopup = {}, commandOrchestrator, dispatch } = useContext(EditorContext);
18
+ const { fullscreen, preview, barPopup = {}, components, commandOrchestrator, dispatch } = useContext(EditorContext);
20
19
  const originalOverflow = useRef('');
21
20
 
22
21
  function handleClick(command: ICommand<string>, name?: string) {
@@ -83,10 +82,16 @@ export function ToolbarItems(props: IToolbarProps) {
83
82
  })
84
83
  : undefined;
85
84
  const disabled = barPopup && preview && preview === 'preview' && !/(preview|fullscreen)/.test(item.keyCommand);
85
+ const render = components?.toolbar || item.render;
86
+ const com = (
87
+ render && typeof render === 'function' ? render(item, !!disabled, handleClick, idx) : null
88
+ ) as React.ReactElement;
86
89
  return (
87
90
  <li key={idx} {...item.liProps} className={activeBtn ? `active` : ''}>
88
- {!item.buttonProps && item.icon}
89
- {item.buttonProps &&
91
+ {com && React.isValidElement(com) && com}
92
+ {!com && !item.buttonProps && item.icon}
93
+ {!com &&
94
+ item.buttonProps &&
90
95
  React.createElement(
91
96
  'button',
92
97
  {
@@ -119,11 +124,11 @@ export function ToolbarItems(props: IToolbarProps) {
119
124
  }
120
125
 
121
126
  export default function Toolbar(props: IToolbarProps = {}) {
122
- const { prefixCls, height = 29, toolbarBottom, isChild } = props;
127
+ const { prefixCls, toolbarBottom, isChild } = props;
123
128
  const { commands, extraCommands } = useContext(EditorContext);
124
129
  const bottomClassName = toolbarBottom ? 'bottom' : '';
125
130
  return (
126
- <div className={`${prefixCls}-toolbar ${bottomClassName}`} style={{ height }}>
131
+ <div className={`${prefixCls}-toolbar ${bottomClassName}`}>
127
132
  <ToolbarItems {...props} commands={props.commands || commands || []} />
128
133
  {!isChild && <ToolbarItems {...props} commands={extraCommands || []} />}
129
134
  </div>
package/src/index.less CHANGED
@@ -10,13 +10,19 @@
10
10
  0 1px 1px var(--color-border-default);
11
11
  background-color: var(--color-canvas-default);
12
12
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
13
- .copied {
14
- display: none !important;
13
+ display: flex;
14
+ flex-direction: column;
15
+ &-toolbar {
16
+ height: fit-content;
15
17
  }
16
18
  &-content {
19
+ height: 100%;
20
+ overflow: auto;
17
21
  position: relative;
18
22
  border-radius: 0 0 3px 0;
19
- height: calc(100% - 39.1px);
23
+ }
24
+ .copied {
25
+ display: none !important;
20
26
  }
21
27
  &-input {
22
28
  width: 50%;