@uiw/react-md-editor 3.21.0 → 3.22.0

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.
package/esm/Editor.d.ts CHANGED
@@ -1,13 +1,19 @@
1
1
  import React, { CSSProperties, PropsWithRef } from 'react';
2
2
  import MarkdownPreview, { MarkdownPreviewProps } from '@uiw/react-markdown-preview';
3
3
  import { ITextAreaProps } from './components/TextArea';
4
- import { ICommand } from './commands';
4
+ import { ICommand, TextState } from './commands';
5
5
  import { ContextStore, PreviewType } from './Context';
6
6
  import './index.less';
7
7
  export interface IProps {
8
8
  prefixCls?: string;
9
9
  className?: string;
10
10
  }
11
+ export interface Statistics extends TextState {
12
+ /** total length of the document */
13
+ length: number;
14
+ /** Get the number of lines in the editor. */
15
+ lineCount: number;
16
+ }
11
17
  export interface MDEditorProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'>, IProps {
12
18
  /**
13
19
  * The Markdown value.
@@ -21,6 +27,8 @@ export interface MDEditorProps extends Omit<React.HTMLAttributes<HTMLDivElement>
21
27
  * editor height change listener
22
28
  */
23
29
  onHeightChange?: (value?: CSSProperties['height'], oldValue?: CSSProperties['height'], state?: ContextStore) => void;
30
+ /** Some data on the statistics editor. */
31
+ onStatistics?: (data: Statistics) => void;
24
32
  /**
25
33
  * Can be used to make `Markdown Editor` focus itself on initialization. Defaults to on.
26
34
  * it will be set to true when either the source `textarea` is focused,
package/esm/Editor.js CHANGED
@@ -1,12 +1,12 @@
1
1
  import _extends from "@babel/runtime/helpers/extends";
2
2
  import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
3
- var _excluded = ["prefixCls", "className", "value", "commands", "commandsFilter", "direction", "extraCommands", "height", "enableScroll", "visibleDragbar", "highlightEnable", "preview", "fullscreen", "overflow", "previewOptions", "textareaProps", "maxHeight", "minHeight", "autoFocus", "tabSize", "defaultTabEnable", "onChange", "onHeightChange", "hideToolbar", "toolbarBottom", "components", "renderTextarea"];
3
+ var _excluded = ["prefixCls", "className", "value", "commands", "commandsFilter", "direction", "extraCommands", "height", "enableScroll", "visibleDragbar", "highlightEnable", "preview", "fullscreen", "overflow", "previewOptions", "textareaProps", "maxHeight", "minHeight", "autoFocus", "tabSize", "defaultTabEnable", "onChange", "onStatistics", "onHeightChange", "hideToolbar", "toolbarBottom", "components", "renderTextarea"];
4
4
  import React, { useEffect, useReducer, useMemo, useRef, useImperativeHandle } from 'react';
5
5
  import MarkdownPreview from '@uiw/react-markdown-preview';
6
6
  import TextArea from './components/TextArea';
7
7
  import Toolbar from './components/Toolbar';
8
8
  import DragBar from './components/DragBar';
9
- import { getCommands, getExtraCommands } from './commands';
9
+ import { getCommands, getExtraCommands, TextAreaCommandOrchestrator } from './commands';
10
10
  import { reducer, EditorContext } from './Context';
11
11
  import "./index.css";
12
12
  import { jsx as _jsx } from "react/jsx-runtime";
@@ -44,7 +44,8 @@ var InternalMDEditor = (props, ref) => {
44
44
  autoFocus,
45
45
  tabSize = 2,
46
46
  defaultTabEnable = false,
47
- onChange: _onChange,
47
+ onChange,
48
+ onStatistics,
48
49
  onHeightChange,
49
50
  hideToolbar,
50
51
  toolbarBottom = false,
@@ -197,6 +198,20 @@ var InternalMDEditor = (props, ref) => {
197
198
  var dragBarChange = newHeight => dispatch({
198
199
  height: newHeight
199
200
  });
201
+ var changeHandle = evn => {
202
+ onChange && onChange(evn.target.value, evn, state);
203
+ if (textareaProps && textareaProps.onChange) {
204
+ textareaProps.onChange(evn);
205
+ }
206
+ if (state.textarea && state.textarea instanceof HTMLTextAreaElement && onStatistics) {
207
+ var obj = new TextAreaCommandOrchestrator(state.textarea);
208
+ var objState = obj.getState() || {};
209
+ onStatistics(_extends({}, objState, {
210
+ lineCount: evn.target.value.split('\n').length,
211
+ length: evn.target.value.length
212
+ }));
213
+ }
214
+ };
200
215
  return /*#__PURE__*/_jsx(EditorContext.Provider, {
201
216
  value: _extends({}, state, {
202
217
  dispatch
@@ -218,12 +233,7 @@ var InternalMDEditor = (props, ref) => {
218
233
  prefixCls: prefixCls,
219
234
  autoFocus: autoFocus
220
235
  }, textareaProps, {
221
- onChange: evn => {
222
- _onChange && _onChange(evn.target.value, evn, state);
223
- if (textareaProps && textareaProps.onChange) {
224
- textareaProps.onChange(evn);
225
- }
226
- },
236
+ onChange: changeHandle,
227
237
  renderTextarea: (components == null ? void 0 : components.textarea) || renderTextarea,
228
238
  onScroll: e => handleScroll(e, 'text')
229
239
  })), /(live|preview)/.test(state.preview || '') && mdPreview]
@@ -54,7 +54,6 @@ export default function Textarea(props) {
54
54
  }, []);
55
55
  var onKeyDown = e => {
56
56
  handleKeyDown(e, tabSize, defaultTabEnable);
57
- console.log('otherStore:', otherStore);
58
57
  shortcuts(e, [...(commands || []), ...(extraCommands || [])], executeRef.current, dispatch, statesRef.current);
59
58
  };
60
59
  useEffect(() => {
package/lib/Editor.d.ts CHANGED
@@ -1,13 +1,19 @@
1
1
  import React, { CSSProperties, PropsWithRef } from 'react';
2
2
  import MarkdownPreview, { MarkdownPreviewProps } from '@uiw/react-markdown-preview';
3
3
  import { ITextAreaProps } from './components/TextArea';
4
- import { ICommand } from './commands';
4
+ import { ICommand, TextState } from './commands';
5
5
  import { ContextStore, PreviewType } from './Context';
6
6
  import './index.less';
7
7
  export interface IProps {
8
8
  prefixCls?: string;
9
9
  className?: string;
10
10
  }
11
+ export interface Statistics extends TextState {
12
+ /** total length of the document */
13
+ length: number;
14
+ /** Get the number of lines in the editor. */
15
+ lineCount: number;
16
+ }
11
17
  export interface MDEditorProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'>, IProps {
12
18
  /**
13
19
  * The Markdown value.
@@ -21,6 +27,8 @@ export interface MDEditorProps extends Omit<React.HTMLAttributes<HTMLDivElement>
21
27
  * editor height change listener
22
28
  */
23
29
  onHeightChange?: (value?: CSSProperties['height'], oldValue?: CSSProperties['height'], state?: ContextStore) => void;
30
+ /** Some data on the statistics editor. */
31
+ onStatistics?: (data: Statistics) => void;
24
32
  /**
25
33
  * Can be used to make `Markdown Editor` focus itself on initialization. Defaults to on.
26
34
  * it will be set to true when either the source `textarea` is focused,
package/lib/Editor.js CHANGED
@@ -17,7 +17,7 @@ var _DragBar = _interopRequireDefault(require("./components/DragBar"));
17
17
  var _commands = require("./commands");
18
18
  var _Context = require("./Context");
19
19
  var _jsxRuntime = require("react/jsx-runtime");
20
- var _excluded = ["prefixCls", "className", "value", "commands", "commandsFilter", "direction", "extraCommands", "height", "enableScroll", "visibleDragbar", "highlightEnable", "preview", "fullscreen", "overflow", "previewOptions", "textareaProps", "maxHeight", "minHeight", "autoFocus", "tabSize", "defaultTabEnable", "onChange", "onHeightChange", "hideToolbar", "toolbarBottom", "components", "renderTextarea"];
20
+ var _excluded = ["prefixCls", "className", "value", "commands", "commandsFilter", "direction", "extraCommands", "height", "enableScroll", "visibleDragbar", "highlightEnable", "preview", "fullscreen", "overflow", "previewOptions", "textareaProps", "maxHeight", "minHeight", "autoFocus", "tabSize", "defaultTabEnable", "onChange", "onStatistics", "onHeightChange", "hideToolbar", "toolbarBottom", "components", "renderTextarea"];
21
21
  function setGroupPopFalse() {
22
22
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
23
23
  Object.keys(data).forEach(function (keyname) {
@@ -63,7 +63,8 @@ var InternalMDEditor = function InternalMDEditor(props, ref) {
63
63
  tabSize = _ref$tabSize === void 0 ? 2 : _ref$tabSize,
64
64
  _ref$defaultTabEnable = _ref.defaultTabEnable,
65
65
  defaultTabEnable = _ref$defaultTabEnable === void 0 ? false : _ref$defaultTabEnable,
66
- _onChange = _ref.onChange,
66
+ onChange = _ref.onChange,
67
+ onStatistics = _ref.onStatistics,
67
68
  onHeightChange = _ref.onHeightChange,
68
69
  hideToolbar = _ref.hideToolbar,
69
70
  _ref$toolbarBottom = _ref.toolbarBottom,
@@ -255,6 +256,20 @@ var InternalMDEditor = function InternalMDEditor(props, ref) {
255
256
  height: newHeight
256
257
  });
257
258
  };
259
+ var changeHandle = function changeHandle(evn) {
260
+ onChange && onChange(evn.target.value, evn, state);
261
+ if (textareaProps && textareaProps.onChange) {
262
+ textareaProps.onChange(evn);
263
+ }
264
+ if (state.textarea && state.textarea instanceof HTMLTextAreaElement && onStatistics) {
265
+ var obj = new _commands.TextAreaCommandOrchestrator(state.textarea);
266
+ var objState = obj.getState() || {};
267
+ onStatistics((0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, objState), {}, {
268
+ lineCount: evn.target.value.split('\n').length,
269
+ length: evn.target.value.length
270
+ }));
271
+ }
272
+ };
258
273
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Context.EditorContext.Provider, {
259
274
  value: (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, state), {}, {
260
275
  dispatch: dispatch
@@ -276,12 +291,7 @@ var InternalMDEditor = function InternalMDEditor(props, ref) {
276
291
  prefixCls: prefixCls,
277
292
  autoFocus: autoFocus
278
293
  }, textareaProps), {}, {
279
- onChange: function onChange(evn) {
280
- _onChange && _onChange(evn.target.value, evn, state);
281
- if (textareaProps && textareaProps.onChange) {
282
- textareaProps.onChange(evn);
283
- }
284
- },
294
+ onChange: changeHandle,
285
295
  renderTextarea: (components === null || components === void 0 ? void 0 : components.textarea) || renderTextarea,
286
296
  onScroll: function onScroll(e) {
287
297
  return handleScroll(e, 'text');
@@ -58,7 +58,6 @@ function Textarea(props) {
58
58
  }, []);
59
59
  var onKeyDown = function onKeyDown(e) {
60
60
  (0, _handleKeyDown["default"])(e, tabSize, defaultTabEnable);
61
- console.log('otherStore:', otherStore);
62
61
  (0, _shortcuts["default"])(e, [].concat((0, _toConsumableArray2["default"])(commands || []), (0, _toConsumableArray2["default"])(extraCommands || [])), executeRef.current, dispatch, statesRef.current);
63
62
  };
64
63
  (0, _react.useEffect)(function () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uiw/react-md-editor",
3
- "version": "3.21.0",
3
+ "version": "3.22.0",
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
@@ -3,7 +3,7 @@ import MarkdownPreview, { MarkdownPreviewProps } from '@uiw/react-markdown-previ
3
3
  import TextArea, { ITextAreaProps } from './components/TextArea';
4
4
  import Toolbar from './components/Toolbar';
5
5
  import DragBar from './components/DragBar';
6
- import { getCommands, getExtraCommands, ICommand } from './commands';
6
+ import { getCommands, getExtraCommands, ICommand, TextState, TextAreaCommandOrchestrator } from './commands';
7
7
  import { reducer, EditorContext, ContextStore, PreviewType } from './Context';
8
8
  import './index.less';
9
9
 
@@ -12,6 +12,13 @@ export interface IProps {
12
12
  className?: string;
13
13
  }
14
14
 
15
+ export interface Statistics extends TextState {
16
+ /** total length of the document */
17
+ length: number;
18
+ /** Get the number of lines in the editor. */
19
+ lineCount: number;
20
+ }
21
+
15
22
  export interface MDEditorProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'>, IProps {
16
23
  /**
17
24
  * The Markdown value.
@@ -25,6 +32,8 @@ export interface MDEditorProps extends Omit<React.HTMLAttributes<HTMLDivElement>
25
32
  * editor height change listener
26
33
  */
27
34
  onHeightChange?: (value?: CSSProperties['height'], oldValue?: CSSProperties['height'], state?: ContextStore) => void;
35
+ /** Some data on the statistics editor. */
36
+ onStatistics?: (data: Statistics) => void;
28
37
  /**
29
38
  * Can be used to make `Markdown Editor` focus itself on initialization. Defaults to on.
30
39
  * it will be set to true when either the source `textarea` is focused,
@@ -180,6 +189,7 @@ const InternalMDEditor = (
180
189
  tabSize = 2,
181
190
  defaultTabEnable = false,
182
191
  onChange,
192
+ onStatistics,
183
193
  onHeightChange,
184
194
  hideToolbar,
185
195
  toolbarBottom = false,
@@ -336,6 +346,21 @@ const InternalMDEditor = (
336
346
  const containerClick = () => dispatch({ barPopup: { ...setGroupPopFalse(state.barPopup) } });
337
347
  const dragBarChange = (newHeight: number) => dispatch({ height: newHeight });
338
348
 
349
+ const changeHandle = (evn: React.ChangeEvent<HTMLTextAreaElement>) => {
350
+ onChange && onChange(evn.target.value, evn, state);
351
+ if (textareaProps && textareaProps.onChange) {
352
+ textareaProps.onChange(evn);
353
+ }
354
+ if (state.textarea && state.textarea instanceof HTMLTextAreaElement && onStatistics) {
355
+ const obj = new TextAreaCommandOrchestrator(state.textarea!);
356
+ const objState = (obj.getState() || {}) as TextState;
357
+ onStatistics({
358
+ ...objState,
359
+ lineCount: evn.target.value.split('\n').length,
360
+ length: evn.target.value.length,
361
+ });
362
+ }
363
+ };
339
364
  return (
340
365
  <EditorContext.Provider value={{ ...state, dispatch }}>
341
366
  <div ref={container} className={cls} {...other} onClick={containerClick} style={containerStyle}>
@@ -349,12 +374,7 @@ const InternalMDEditor = (
349
374
  prefixCls={prefixCls}
350
375
  autoFocus={autoFocus}
351
376
  {...textareaProps}
352
- onChange={(evn) => {
353
- onChange && onChange(evn.target.value, evn, state);
354
- if (textareaProps && textareaProps.onChange) {
355
- textareaProps.onChange(evn);
356
- }
357
- }}
377
+ onChange={changeHandle}
358
378
  renderTextarea={components?.textarea || renderTextarea}
359
379
  onScroll={(e) => handleScroll(e, 'text')}
360
380
  />
@@ -41,7 +41,6 @@ export default function Textarea(props: TextAreaProps) {
41
41
 
42
42
  const onKeyDown = (e: KeyboardEvent | React.KeyboardEvent<HTMLTextAreaElement>) => {
43
43
  handleKeyDown(e, tabSize, defaultTabEnable);
44
- console.log('otherStore:', otherStore);
45
44
  shortcuts(e, [...(commands || []), ...(extraCommands || [])], executeRef.current, dispatch, statesRef.current);
46
45
  };
47
46
  useEffect(() => {