@uiw/react-md-editor 3.24.0 → 3.25.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/README.md +50 -5
- package/dist/mdeditor.js +1835 -1807
- package/dist/mdeditor.min.js +1 -1
- package/esm/Context.d.ts +1 -1
- package/esm/Editor.d.ts +4 -149
- package/esm/Editor.js +0 -1
- package/esm/Editor.nohighlight.d.ts +12 -0
- package/esm/Editor.nohighlight.js +255 -0
- package/esm/Types.d.ts +148 -0
- package/esm/Types.js +1 -0
- package/esm/commands/index.d.ts +4 -1
- package/esm/commands/index.js +2 -1
- package/esm/components/DragBar/index.d.ts +1 -1
- package/esm/components/TextArea/Markdown.d.ts +1 -1
- package/esm/components/TextArea/Textarea.d.ts +1 -1
- package/esm/components/TextArea/handleKeyDown.js +1 -3
- package/esm/components/TextArea/index.d.ts +1 -1
- package/esm/components/TextArea/index.nohighlight.d.ts +27 -0
- package/esm/components/TextArea/index.nohighlight.js +93 -0
- package/esm/components/Toolbar/Child.d.ts +1 -1
- package/esm/components/Toolbar/index.d.ts +1 -1
- package/esm/index.d.ts +2 -0
- package/esm/index.js +2 -0
- package/esm/index.nohighlight.d.ts +13 -0
- package/esm/index.nohighlight.js +13 -0
- package/lib/Context.d.ts +1 -1
- package/lib/Editor.d.ts +4 -149
- package/lib/Editor.nohighlight.d.ts +12 -0
- package/lib/Editor.nohighlight.js +317 -0
- package/lib/Types.d.ts +148 -0
- package/lib/Types.js +1 -0
- package/lib/commands/index.d.ts +4 -1
- package/lib/commands/index.js +19 -0
- package/lib/components/DragBar/index.d.ts +1 -1
- package/lib/components/TextArea/Markdown.d.ts +1 -1
- package/lib/components/TextArea/Textarea.d.ts +1 -1
- package/lib/components/TextArea/handleKeyDown.js +1 -3
- package/lib/components/TextArea/index.d.ts +1 -1
- package/lib/components/TextArea/index.nohighlight.d.ts +27 -0
- package/lib/components/TextArea/index.nohighlight.js +98 -0
- package/lib/components/Toolbar/Child.d.ts +1 -1
- package/lib/components/Toolbar/index.d.ts +1 -1
- package/lib/index.d.ts +2 -0
- package/lib/index.js +12 -0
- package/lib/index.nohighlight.d.ts +13 -0
- package/lib/index.nohighlight.js +98 -0
- package/package.json +16 -2
- package/src/Context.tsx +1 -1
- package/src/Editor.nohighlight.tsx +264 -0
- package/src/Editor.tsx +5 -151
- package/src/Types.ts +151 -0
- package/src/commands/index.ts +4 -0
- package/src/components/DragBar/index.tsx +1 -1
- package/src/components/TextArea/Markdown.tsx +2 -2
- package/src/components/TextArea/Textarea.tsx +1 -1
- package/src/components/TextArea/handleKeyDown.tsx +5 -3
- package/src/components/TextArea/index.nohighlight.tsx +109 -0
- package/src/components/TextArea/index.tsx +1 -1
- package/src/components/Toolbar/Child.tsx +1 -1
- package/src/components/Toolbar/index.tsx +1 -1
- package/src/index.nohighlight.tsx +16 -0
- package/src/index.tsx +2 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
|
|
4
|
+
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports["default"] = TextArea;
|
|
9
|
+
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
10
|
+
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
11
|
+
var _react = _interopRequireWildcard(require("react"));
|
|
12
|
+
var _Context = require("../../Context");
|
|
13
|
+
var _shortcuts = _interopRequireDefault(require("./shortcuts"));
|
|
14
|
+
var _Textarea = _interopRequireDefault(require("./Textarea"));
|
|
15
|
+
var _commands = require("../../commands");
|
|
16
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
17
|
+
var _excluded = ["prefixCls", "className", "onScroll", "renderTextarea"];
|
|
18
|
+
function TextArea(props) {
|
|
19
|
+
var _ref = props || {},
|
|
20
|
+
prefixCls = _ref.prefixCls,
|
|
21
|
+
className = _ref.className,
|
|
22
|
+
onScroll = _ref.onScroll,
|
|
23
|
+
renderTextarea = _ref.renderTextarea,
|
|
24
|
+
otherProps = (0, _objectWithoutProperties2["default"])(_ref, _excluded);
|
|
25
|
+
var _useContext = (0, _react.useContext)(_Context.EditorContext),
|
|
26
|
+
markdown = _useContext.markdown,
|
|
27
|
+
scrollTop = _useContext.scrollTop,
|
|
28
|
+
commands = _useContext.commands,
|
|
29
|
+
extraCommands = _useContext.extraCommands,
|
|
30
|
+
dispatch = _useContext.dispatch;
|
|
31
|
+
var textRef = _react["default"].useRef(null);
|
|
32
|
+
var executeRef = _react["default"].useRef();
|
|
33
|
+
var warp = /*#__PURE__*/_react["default"].createRef();
|
|
34
|
+
(0, _react.useEffect)(function () {
|
|
35
|
+
var state = {};
|
|
36
|
+
if (warp.current) {
|
|
37
|
+
state.textareaWarp = warp.current || undefined;
|
|
38
|
+
warp.current.scrollTop = scrollTop || 0;
|
|
39
|
+
}
|
|
40
|
+
if (dispatch) {
|
|
41
|
+
dispatch((0, _objectSpread2["default"])({}, state));
|
|
42
|
+
}
|
|
43
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
44
|
+
}, []);
|
|
45
|
+
(0, _react.useEffect)(function () {
|
|
46
|
+
if (textRef.current && dispatch) {
|
|
47
|
+
var _commandOrchestrator = new _commands.TextAreaCommandOrchestrator(textRef.current);
|
|
48
|
+
executeRef.current = _commandOrchestrator;
|
|
49
|
+
dispatch({
|
|
50
|
+
textarea: textRef.current,
|
|
51
|
+
commandOrchestrator: _commandOrchestrator
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
55
|
+
}, []);
|
|
56
|
+
var textStyle = {
|
|
57
|
+
WebkitTextFillColor: 'initial',
|
|
58
|
+
overflow: 'auto'
|
|
59
|
+
};
|
|
60
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
|
|
61
|
+
ref: warp,
|
|
62
|
+
className: "".concat(prefixCls, "-area ").concat(className || ''),
|
|
63
|
+
onScroll: onScroll,
|
|
64
|
+
children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
|
|
65
|
+
className: "".concat(prefixCls, "-text"),
|
|
66
|
+
children: renderTextarea ? /*#__PURE__*/_react["default"].cloneElement(renderTextarea((0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, otherProps), {}, {
|
|
67
|
+
value: markdown,
|
|
68
|
+
autoComplete: 'off',
|
|
69
|
+
autoCorrect: 'off',
|
|
70
|
+
spellCheck: 'false',
|
|
71
|
+
autoCapitalize: 'off',
|
|
72
|
+
className: "".concat(prefixCls, "-text-input"),
|
|
73
|
+
style: {
|
|
74
|
+
WebkitTextFillColor: 'inherit',
|
|
75
|
+
overflow: 'auto'
|
|
76
|
+
}
|
|
77
|
+
}), {
|
|
78
|
+
dispatch: dispatch,
|
|
79
|
+
onChange: otherProps.onChange,
|
|
80
|
+
shortcuts: _shortcuts["default"],
|
|
81
|
+
useContext: {
|
|
82
|
+
commands: commands,
|
|
83
|
+
extraCommands: extraCommands,
|
|
84
|
+
commandOrchestrator: executeRef.current
|
|
85
|
+
}
|
|
86
|
+
}), {
|
|
87
|
+
ref: textRef
|
|
88
|
+
}) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_react.Fragment, {
|
|
89
|
+
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Textarea["default"], (0, _objectSpread2["default"])((0, _objectSpread2["default"])({
|
|
90
|
+
prefixCls: prefixCls
|
|
91
|
+
}, otherProps), {}, {
|
|
92
|
+
style: textStyle
|
|
93
|
+
}))
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
module.exports = exports.default;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import MDEditor from './Editor';
|
|
2
2
|
import * as commands from './commands';
|
|
3
3
|
import * as MarkdownUtil from './utils/markdownUtils';
|
|
4
|
+
import './index.less';
|
|
4
5
|
export * from './commands';
|
|
5
6
|
export * from './commands/group';
|
|
6
7
|
export * from './utils/markdownUtils';
|
|
7
8
|
export * from './utils/InsertTextAtPosition';
|
|
8
9
|
export * from './Editor';
|
|
9
10
|
export * from './Context';
|
|
11
|
+
export * from './Types';
|
|
10
12
|
export { MarkdownUtil, commands };
|
|
11
13
|
export default MDEditor;
|
package/lib/index.js
CHANGED
|
@@ -83,4 +83,16 @@ Object.keys(_Context).forEach(function (key) {
|
|
|
83
83
|
}
|
|
84
84
|
});
|
|
85
85
|
});
|
|
86
|
+
var _Types = require("./Types");
|
|
87
|
+
Object.keys(_Types).forEach(function (key) {
|
|
88
|
+
if (key === "default" || key === "__esModule") return;
|
|
89
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
90
|
+
if (key in exports && exports[key] === _Types[key]) return;
|
|
91
|
+
Object.defineProperty(exports, key, {
|
|
92
|
+
enumerable: true,
|
|
93
|
+
get: function get() {
|
|
94
|
+
return _Types[key];
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
});
|
|
86
98
|
var _default = exports["default"] = _Editor["default"];
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import MDEditor from './Editor.nohighlight';
|
|
2
|
+
import * as commands from './commands';
|
|
3
|
+
import * as MarkdownUtil from './utils/markdownUtils';
|
|
4
|
+
import './index.less';
|
|
5
|
+
export * from './commands';
|
|
6
|
+
export * from './commands/group';
|
|
7
|
+
export * from './utils/markdownUtils';
|
|
8
|
+
export * from './utils/InsertTextAtPosition';
|
|
9
|
+
export * from './Editor.nohighlight';
|
|
10
|
+
export * from './Context';
|
|
11
|
+
export * from './Types';
|
|
12
|
+
export { MarkdownUtil, commands };
|
|
13
|
+
export default MDEditor;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
var _exportNames = {
|
|
8
|
+
commands: true,
|
|
9
|
+
MarkdownUtil: true
|
|
10
|
+
};
|
|
11
|
+
exports["default"] = exports.commands = exports.MarkdownUtil = void 0;
|
|
12
|
+
var _Editor = _interopRequireWildcard(require("./Editor.nohighlight"));
|
|
13
|
+
Object.keys(_Editor).forEach(function (key) {
|
|
14
|
+
if (key === "default" || key === "__esModule") return;
|
|
15
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
16
|
+
if (key in exports && exports[key] === _Editor[key]) return;
|
|
17
|
+
Object.defineProperty(exports, key, {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
get: function get() {
|
|
20
|
+
return _Editor[key];
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
var commands = _interopRequireWildcard(require("./commands"));
|
|
25
|
+
exports.commands = commands;
|
|
26
|
+
Object.keys(commands).forEach(function (key) {
|
|
27
|
+
if (key === "default" || key === "__esModule") return;
|
|
28
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
29
|
+
if (key in exports && exports[key] === commands[key]) return;
|
|
30
|
+
Object.defineProperty(exports, key, {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
get: function get() {
|
|
33
|
+
return commands[key];
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
var MarkdownUtil = _interopRequireWildcard(require("./utils/markdownUtils"));
|
|
38
|
+
exports.MarkdownUtil = MarkdownUtil;
|
|
39
|
+
Object.keys(MarkdownUtil).forEach(function (key) {
|
|
40
|
+
if (key === "default" || key === "__esModule") return;
|
|
41
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
42
|
+
if (key in exports && exports[key] === MarkdownUtil[key]) return;
|
|
43
|
+
Object.defineProperty(exports, key, {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
get: function get() {
|
|
46
|
+
return MarkdownUtil[key];
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
var _group = require("./commands/group");
|
|
51
|
+
Object.keys(_group).forEach(function (key) {
|
|
52
|
+
if (key === "default" || key === "__esModule") return;
|
|
53
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
54
|
+
if (key in exports && exports[key] === _group[key]) return;
|
|
55
|
+
Object.defineProperty(exports, key, {
|
|
56
|
+
enumerable: true,
|
|
57
|
+
get: function get() {
|
|
58
|
+
return _group[key];
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
var _InsertTextAtPosition = require("./utils/InsertTextAtPosition");
|
|
63
|
+
Object.keys(_InsertTextAtPosition).forEach(function (key) {
|
|
64
|
+
if (key === "default" || key === "__esModule") return;
|
|
65
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
66
|
+
if (key in exports && exports[key] === _InsertTextAtPosition[key]) return;
|
|
67
|
+
Object.defineProperty(exports, key, {
|
|
68
|
+
enumerable: true,
|
|
69
|
+
get: function get() {
|
|
70
|
+
return _InsertTextAtPosition[key];
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
var _Context = require("./Context");
|
|
75
|
+
Object.keys(_Context).forEach(function (key) {
|
|
76
|
+
if (key === "default" || key === "__esModule") return;
|
|
77
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
78
|
+
if (key in exports && exports[key] === _Context[key]) return;
|
|
79
|
+
Object.defineProperty(exports, key, {
|
|
80
|
+
enumerable: true,
|
|
81
|
+
get: function get() {
|
|
82
|
+
return _Context[key];
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
var _Types = require("./Types");
|
|
87
|
+
Object.keys(_Types).forEach(function (key) {
|
|
88
|
+
if (key === "default" || key === "__esModule") return;
|
|
89
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
90
|
+
if (key in exports && exports[key] === _Types[key]) return;
|
|
91
|
+
Object.defineProperty(exports, key, {
|
|
92
|
+
enumerable: true,
|
|
93
|
+
get: function get() {
|
|
94
|
+
return _Types[key];
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
var _default = exports["default"] = _Editor["default"];
|
package/package.json
CHANGED
|
@@ -1,11 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uiw/react-md-editor",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.25.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>",
|
|
7
7
|
"main": "lib/index.js",
|
|
8
8
|
"module": "esm/index.js",
|
|
9
|
+
"exports": {
|
|
10
|
+
"./README.md": "./README.md",
|
|
11
|
+
"./package.json": "./package.json",
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./esm/index.js",
|
|
14
|
+
"types": "./lib/index.d.ts",
|
|
15
|
+
"require": "./lib/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./nohighlight": {
|
|
18
|
+
"import": "./esm/index.nohighlight.js",
|
|
19
|
+
"types": "./lib/index.nohighlight.d.ts",
|
|
20
|
+
"require": "./lib/index.nohighlight.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
9
23
|
"repository": {
|
|
10
24
|
"type": "git",
|
|
11
25
|
"url": "https://github.com/uiwjs/react-md-editor"
|
|
@@ -27,7 +41,7 @@
|
|
|
27
41
|
},
|
|
28
42
|
"dependencies": {
|
|
29
43
|
"@babel/runtime": "^7.14.6",
|
|
30
|
-
"@uiw/react-markdown-preview": "^4.1
|
|
44
|
+
"@uiw/react-markdown-preview": "^4.2.1",
|
|
31
45
|
"rehype": "~12.0.1",
|
|
32
46
|
"rehype-prism-plus": "~1.6.1"
|
|
33
47
|
},
|
package/src/Context.tsx
CHANGED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import React, { useEffect, useReducer, useMemo, useRef, useImperativeHandle } from 'react';
|
|
2
|
+
import MarkdownPreview from '@uiw/react-markdown-preview/nohighlight';
|
|
3
|
+
import TextArea from './components/TextArea/index.nohighlight';
|
|
4
|
+
import Toolbar from './components/Toolbar';
|
|
5
|
+
import DragBar from './components/DragBar';
|
|
6
|
+
import { getCommands, getExtraCommands, ICommand, TextState, TextAreaCommandOrchestrator } from './commands';
|
|
7
|
+
import { reducer, EditorContext, ContextStore } from './Context';
|
|
8
|
+
import type { MDEditorProps } from './Types';
|
|
9
|
+
|
|
10
|
+
function setGroupPopFalse(data: Record<string, boolean> = {}) {
|
|
11
|
+
Object.keys(data).forEach((keyname) => {
|
|
12
|
+
data[keyname] = false;
|
|
13
|
+
});
|
|
14
|
+
return data;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface RefMDEditor extends ContextStore {}
|
|
18
|
+
|
|
19
|
+
const InternalMDEditor = React.forwardRef<RefMDEditor, MDEditorProps>(
|
|
20
|
+
(props: MDEditorProps, ref: React.ForwardedRef<RefMDEditor>) => {
|
|
21
|
+
const {
|
|
22
|
+
prefixCls = 'w-md-editor',
|
|
23
|
+
className,
|
|
24
|
+
value: propsValue,
|
|
25
|
+
commands = getCommands(),
|
|
26
|
+
commandsFilter,
|
|
27
|
+
direction,
|
|
28
|
+
extraCommands = getExtraCommands(),
|
|
29
|
+
height = 200,
|
|
30
|
+
enableScroll = true,
|
|
31
|
+
visibleDragbar = typeof props.visiableDragbar === 'boolean' ? props.visiableDragbar : true,
|
|
32
|
+
highlightEnable = true,
|
|
33
|
+
preview: previewType = 'live',
|
|
34
|
+
fullscreen = false,
|
|
35
|
+
overflow = true,
|
|
36
|
+
previewOptions = {},
|
|
37
|
+
textareaProps,
|
|
38
|
+
maxHeight = 1200,
|
|
39
|
+
minHeight = 100,
|
|
40
|
+
autoFocus,
|
|
41
|
+
tabSize = 2,
|
|
42
|
+
defaultTabEnable = false,
|
|
43
|
+
onChange,
|
|
44
|
+
onStatistics,
|
|
45
|
+
onHeightChange,
|
|
46
|
+
hideToolbar,
|
|
47
|
+
toolbarBottom = false,
|
|
48
|
+
components,
|
|
49
|
+
renderTextarea,
|
|
50
|
+
...other
|
|
51
|
+
} = props || {};
|
|
52
|
+
const cmds = commands
|
|
53
|
+
.map((item) => (commandsFilter ? commandsFilter(item, false) : item))
|
|
54
|
+
.filter(Boolean) as ICommand[];
|
|
55
|
+
const extraCmds = extraCommands
|
|
56
|
+
.map((item) => (commandsFilter ? commandsFilter(item, true) : item))
|
|
57
|
+
.filter(Boolean) as ICommand[];
|
|
58
|
+
let [state, dispatch] = useReducer(reducer, {
|
|
59
|
+
markdown: propsValue,
|
|
60
|
+
preview: previewType,
|
|
61
|
+
components,
|
|
62
|
+
height,
|
|
63
|
+
highlightEnable,
|
|
64
|
+
tabSize,
|
|
65
|
+
defaultTabEnable,
|
|
66
|
+
scrollTop: 0,
|
|
67
|
+
scrollTopPreview: 0,
|
|
68
|
+
commands: cmds,
|
|
69
|
+
extraCommands: extraCmds,
|
|
70
|
+
fullscreen,
|
|
71
|
+
barPopup: {},
|
|
72
|
+
});
|
|
73
|
+
const container = useRef<HTMLDivElement>(null);
|
|
74
|
+
const previewRef = useRef<HTMLDivElement>(null);
|
|
75
|
+
const enableScrollRef = useRef(enableScroll);
|
|
76
|
+
|
|
77
|
+
useImperativeHandle(ref, () => ({ ...state, container: container.current, dispatch }));
|
|
78
|
+
useMemo(() => (enableScrollRef.current = enableScroll), [enableScroll]);
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
const stateInit: ContextStore = {};
|
|
81
|
+
if (container.current) {
|
|
82
|
+
stateInit.container = container.current || undefined;
|
|
83
|
+
}
|
|
84
|
+
stateInit.markdown = propsValue || '';
|
|
85
|
+
stateInit.barPopup = {};
|
|
86
|
+
if (dispatch) {
|
|
87
|
+
dispatch({ ...state, ...stateInit });
|
|
88
|
+
}
|
|
89
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
90
|
+
}, []);
|
|
91
|
+
|
|
92
|
+
const cls = [
|
|
93
|
+
className,
|
|
94
|
+
'wmde-markdown-var',
|
|
95
|
+
direction ? `${prefixCls}-${direction}` : null,
|
|
96
|
+
prefixCls,
|
|
97
|
+
state.preview ? `${prefixCls}-show-${state.preview}` : null,
|
|
98
|
+
state.fullscreen ? `${prefixCls}-fullscreen` : null,
|
|
99
|
+
]
|
|
100
|
+
.filter(Boolean)
|
|
101
|
+
.join(' ')
|
|
102
|
+
.trim();
|
|
103
|
+
|
|
104
|
+
useMemo(
|
|
105
|
+
() => propsValue !== state.markdown && dispatch({ markdown: propsValue || '' }),
|
|
106
|
+
[propsValue, state.markdown],
|
|
107
|
+
);
|
|
108
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
109
|
+
useMemo(() => previewType !== state.preview && dispatch({ preview: previewType }), [previewType]);
|
|
110
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
111
|
+
useMemo(() => tabSize !== state.tabSize && dispatch({ tabSize }), [tabSize]);
|
|
112
|
+
useMemo(
|
|
113
|
+
() => highlightEnable !== state.highlightEnable && dispatch({ highlightEnable }),
|
|
114
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
115
|
+
[highlightEnable],
|
|
116
|
+
);
|
|
117
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
118
|
+
useMemo(() => autoFocus !== state.autoFocus && dispatch({ autoFocus: autoFocus }), [autoFocus]);
|
|
119
|
+
useMemo(
|
|
120
|
+
() => fullscreen !== state.fullscreen && dispatch({ fullscreen: fullscreen }),
|
|
121
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
122
|
+
[fullscreen],
|
|
123
|
+
);
|
|
124
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
125
|
+
useMemo(() => height !== state.height && dispatch({ height: height }), [height]);
|
|
126
|
+
useMemo(
|
|
127
|
+
() => height !== state.height && onHeightChange && onHeightChange(state.height, height, state),
|
|
128
|
+
[height, onHeightChange, state],
|
|
129
|
+
);
|
|
130
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
131
|
+
useMemo(() => commands !== state.commands && dispatch({ commands: cmds }), [props.commands]);
|
|
132
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
133
|
+
useMemo(
|
|
134
|
+
() => extraCommands !== state.extraCommands && dispatch({ extraCommands: extraCmds }),
|
|
135
|
+
[props.extraCommands],
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
const textareaDomRef = useRef<HTMLDivElement>();
|
|
139
|
+
const active = useRef<'text' | 'preview'>('preview');
|
|
140
|
+
const initScroll = useRef(false);
|
|
141
|
+
|
|
142
|
+
useMemo(() => {
|
|
143
|
+
textareaDomRef.current = state.textareaWarp;
|
|
144
|
+
if (state.textareaWarp) {
|
|
145
|
+
state.textareaWarp.addEventListener('mouseover', () => {
|
|
146
|
+
active.current = 'text';
|
|
147
|
+
});
|
|
148
|
+
state.textareaWarp.addEventListener('mouseleave', () => {
|
|
149
|
+
active.current = 'preview';
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}, [state.textareaWarp]);
|
|
153
|
+
|
|
154
|
+
const handleScroll = (e: React.UIEvent<HTMLDivElement>, type: 'text' | 'preview') => {
|
|
155
|
+
if (!enableScrollRef.current) return;
|
|
156
|
+
const textareaDom = textareaDomRef.current;
|
|
157
|
+
const previewDom = previewRef.current ? previewRef.current : undefined;
|
|
158
|
+
if (!initScroll.current) {
|
|
159
|
+
active.current = type;
|
|
160
|
+
initScroll.current = true;
|
|
161
|
+
}
|
|
162
|
+
if (textareaDom && previewDom) {
|
|
163
|
+
const scale =
|
|
164
|
+
(textareaDom.scrollHeight - textareaDom.offsetHeight) / (previewDom.scrollHeight - previewDom.offsetHeight);
|
|
165
|
+
if (e.target === textareaDom && active.current === 'text') {
|
|
166
|
+
previewDom.scrollTop = textareaDom.scrollTop / scale;
|
|
167
|
+
}
|
|
168
|
+
if (e.target === previewDom && active.current === 'preview') {
|
|
169
|
+
textareaDom.scrollTop = previewDom.scrollTop * scale;
|
|
170
|
+
}
|
|
171
|
+
let scrollTop = 0;
|
|
172
|
+
if (active.current === 'text') {
|
|
173
|
+
scrollTop = textareaDom.scrollTop || 0;
|
|
174
|
+
} else if (active.current === 'preview') {
|
|
175
|
+
scrollTop = previewDom.scrollTop || 0;
|
|
176
|
+
}
|
|
177
|
+
dispatch({ scrollTop });
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const previewClassName = `${prefixCls}-preview ${previewOptions.className || ''}`;
|
|
182
|
+
const handlePreviewScroll = (e: React.UIEvent<HTMLDivElement, UIEvent>) => handleScroll(e, 'preview');
|
|
183
|
+
let mdPreview = useMemo(
|
|
184
|
+
() => (
|
|
185
|
+
<div ref={previewRef} className={previewClassName}>
|
|
186
|
+
<MarkdownPreview {...previewOptions} onScroll={handlePreviewScroll} source={state.markdown || ''} />
|
|
187
|
+
</div>
|
|
188
|
+
),
|
|
189
|
+
[previewClassName, previewOptions, state.markdown],
|
|
190
|
+
);
|
|
191
|
+
const preview = components?.preview && components?.preview(state.markdown || '', state, dispatch);
|
|
192
|
+
if (preview && React.isValidElement(preview)) {
|
|
193
|
+
mdPreview = (
|
|
194
|
+
<div className={previewClassName} ref={previewRef} onScroll={handlePreviewScroll}>
|
|
195
|
+
{preview}
|
|
196
|
+
</div>
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const containerStyle = { ...other.style, height: state.height || '100%' };
|
|
201
|
+
const containerClick = () => dispatch({ barPopup: { ...setGroupPopFalse(state.barPopup) } });
|
|
202
|
+
const dragBarChange = (newHeight: number) => dispatch({ height: newHeight });
|
|
203
|
+
|
|
204
|
+
const changeHandle = (evn: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
205
|
+
onChange && onChange(evn.target.value, evn, state);
|
|
206
|
+
if (textareaProps && textareaProps.onChange) {
|
|
207
|
+
textareaProps.onChange(evn);
|
|
208
|
+
}
|
|
209
|
+
if (state.textarea && state.textarea instanceof HTMLTextAreaElement && onStatistics) {
|
|
210
|
+
const obj = new TextAreaCommandOrchestrator(state.textarea!);
|
|
211
|
+
const objState = (obj.getState() || {}) as TextState;
|
|
212
|
+
onStatistics({
|
|
213
|
+
...objState,
|
|
214
|
+
lineCount: evn.target.value.split('\n').length,
|
|
215
|
+
length: evn.target.value.length,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
return (
|
|
220
|
+
<EditorContext.Provider value={{ ...state, dispatch }}>
|
|
221
|
+
<div ref={container} className={cls} {...other} onClick={containerClick} style={containerStyle}>
|
|
222
|
+
{!hideToolbar && !toolbarBottom && (
|
|
223
|
+
<Toolbar prefixCls={prefixCls} overflow={overflow} toolbarBottom={toolbarBottom} />
|
|
224
|
+
)}
|
|
225
|
+
<div className={`${prefixCls}-content`}>
|
|
226
|
+
{/(edit|live)/.test(state.preview || '') && (
|
|
227
|
+
<TextArea
|
|
228
|
+
className={`${prefixCls}-input`}
|
|
229
|
+
prefixCls={prefixCls}
|
|
230
|
+
autoFocus={autoFocus}
|
|
231
|
+
{...textareaProps}
|
|
232
|
+
onChange={changeHandle}
|
|
233
|
+
renderTextarea={components?.textarea || renderTextarea}
|
|
234
|
+
onScroll={(e) => handleScroll(e, 'text')}
|
|
235
|
+
/>
|
|
236
|
+
)}
|
|
237
|
+
{/(live|preview)/.test(state.preview || '') && mdPreview}
|
|
238
|
+
</div>
|
|
239
|
+
{visibleDragbar && !state.fullscreen && (
|
|
240
|
+
<DragBar
|
|
241
|
+
prefixCls={prefixCls}
|
|
242
|
+
height={state.height as number}
|
|
243
|
+
maxHeight={maxHeight!}
|
|
244
|
+
minHeight={minHeight!}
|
|
245
|
+
onChange={dragBarChange}
|
|
246
|
+
/>
|
|
247
|
+
)}
|
|
248
|
+
{!hideToolbar && toolbarBottom && (
|
|
249
|
+
<Toolbar prefixCls={prefixCls} overflow={overflow} toolbarBottom={toolbarBottom} />
|
|
250
|
+
)}
|
|
251
|
+
</div>
|
|
252
|
+
</EditorContext.Provider>
|
|
253
|
+
);
|
|
254
|
+
},
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
type EditorComponent = typeof InternalMDEditor & {
|
|
258
|
+
Markdown: typeof MarkdownPreview;
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
const Editor = InternalMDEditor as EditorComponent;
|
|
262
|
+
Editor.Markdown = MarkdownPreview;
|
|
263
|
+
|
|
264
|
+
export default Editor;
|