x-star-editor 0.3.12 → 0.4.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/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/styles/index.css +3 -8
- package/dist/themes/lark.css +2 -2
- package/dist/utils/handler.d.ts +4 -3
- package/dist/utils/handler.js +25 -11
- package/dist/utils/history.d.ts +13 -3
- package/dist/utils/history.js +14 -4
- package/dist/utils/markdown.js +21 -10
- package/dist/utils/rehype/rehype-custom.d.ts +6 -0
- package/dist/utils/rehype/rehype-custom.js +20 -0
- package/dist/x-star-editor/index.d.ts +9 -5
- package/dist/x-star-editor/index.js +20 -19
- package/dist/x-star-md-editor/index.d.ts +8 -4
- package/dist/x-star-md-editor/index.js +23 -11
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export type { ToolbarItem } from './components/Toolbar';
|
|
2
2
|
export { createSelection, getRange } from './utils/container';
|
|
3
|
-
export { batchHandler, insertHandler, redoHandler, selectHandler, setHandler, toggleHandler, undoHandler, } from './utils/handler';
|
|
3
|
+
export { batchHandler, insertHandler, mutateHandler, redoHandler, selectHandler, setHandler, toggleHandler, undoHandler, } from './utils/handler';
|
|
4
4
|
export type { Executor, Handler, KeyboardEventHandler } from './utils/handler';
|
|
5
5
|
export { toHTML, toMarkdown, toText } from './utils/markdown';
|
|
6
6
|
export { default as XStarEditor, useEditorRef } from './x-star-editor';
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { createSelection, getRange } from "./utils/container";
|
|
2
|
-
export { batchHandler, insertHandler, redoHandler, selectHandler, setHandler, toggleHandler, undoHandler } from "./utils/handler";
|
|
2
|
+
export { batchHandler, insertHandler, mutateHandler, redoHandler, selectHandler, setHandler, toggleHandler, undoHandler } from "./utils/handler";
|
|
3
3
|
export { toHTML, toMarkdown, toText } from "./utils/markdown";
|
|
4
4
|
export { default as XStarEditor, useEditorRef } from "./x-star-editor";
|
|
5
5
|
export { default as XStarMdEditor, useMdEditorRef } from "./x-star-md-editor";
|
package/dist/styles/index.css
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
@charset "UTF-8";
|
|
2
1
|
.x-star-editor-file-input {
|
|
3
2
|
margin: 0.5em 1em 1em;
|
|
4
3
|
font-size: 0.9em;
|
|
@@ -243,15 +242,11 @@
|
|
|
243
242
|
.x-star-editor-md-editor > :last-child {
|
|
244
243
|
margin-bottom: 0.5em;
|
|
245
244
|
}
|
|
246
|
-
|
|
247
|
-
.x-star-editor-empty {
|
|
248
|
-
/* 输入框为空时显示 placeholder */
|
|
249
|
-
}
|
|
250
|
-
.x-star-editor-empty::before {
|
|
251
|
-
position: absolute;
|
|
245
|
+
.x-star-editor-md-editor.x-star-editor-empty::before {
|
|
252
246
|
content: attr(placeholder);
|
|
253
|
-
|
|
247
|
+
position: absolute;
|
|
254
248
|
margin-top: 0.5em;
|
|
249
|
+
color: rgba(0, 0, 0, 0.28);
|
|
255
250
|
}
|
|
256
251
|
|
|
257
252
|
.x-star-editor-md-viewer {
|
package/dist/themes/lark.css
CHANGED
|
@@ -331,7 +331,7 @@
|
|
|
331
331
|
.x-star-editor-md-viewer.x-star-editor-theme-lark .task-list-item > input:hover {
|
|
332
332
|
border-color: #3370ff;
|
|
333
333
|
}
|
|
334
|
-
.x-star-editor-md-viewer.x-star-editor-theme-lark .task-list-item > input
|
|
334
|
+
.x-star-editor-md-viewer.x-star-editor-theme-lark .task-list-item > input:checked::before {
|
|
335
335
|
content: "";
|
|
336
336
|
position: absolute;
|
|
337
337
|
top: 20%;
|
|
@@ -341,7 +341,7 @@
|
|
|
341
341
|
transform: rotate(40deg);
|
|
342
342
|
background: #3370ff;
|
|
343
343
|
}
|
|
344
|
-
.x-star-editor-md-viewer.x-star-editor-theme-lark .task-list-item > input
|
|
344
|
+
.x-star-editor-md-viewer.x-star-editor-theme-lark .task-list-item > input:checked::after {
|
|
345
345
|
content: "";
|
|
346
346
|
position: absolute;
|
|
347
347
|
top: 46%;
|
package/dist/utils/handler.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import type { InsertAction, SelectAction, SetAction, ToggleAction, useHistory } from './history';
|
|
2
|
+
import type { BatchAction, InsertAction, MutateAction, SelectAction, SetAction, ToggleAction, useHistory } from './history';
|
|
3
3
|
/**
|
|
4
4
|
* 处理函数上下文
|
|
5
5
|
*/
|
|
@@ -15,8 +15,9 @@ export declare const toggleHandler: (payload: ToggleAction['payload']) => Handle
|
|
|
15
15
|
export declare const setHandler: (payload: SetAction['payload']) => Handler;
|
|
16
16
|
export declare const undoHandler: () => Handler;
|
|
17
17
|
export declare const redoHandler: () => Handler;
|
|
18
|
-
export declare const batchHandler: () => Handler;
|
|
19
|
-
export declare const
|
|
18
|
+
export declare const batchHandler: (payload?: BatchAction['payload']) => Handler;
|
|
19
|
+
export declare const mutateHandler: (payload: MutateAction['payload']) => Handler;
|
|
20
|
+
export declare const selectHandler: (payload: SelectAction['payload']) => Handler;
|
|
20
21
|
/**
|
|
21
22
|
* 执行函数
|
|
22
23
|
*
|
package/dist/utils/handler.js
CHANGED
|
@@ -59,20 +59,30 @@ export var redoHandler = function redoHandler() {
|
|
|
59
59
|
});
|
|
60
60
|
};
|
|
61
61
|
};
|
|
62
|
-
export var batchHandler = function batchHandler() {
|
|
62
|
+
export var batchHandler = function batchHandler(payload) {
|
|
63
63
|
return function (_ref6) {
|
|
64
64
|
var dispatch = _ref6.dispatch;
|
|
65
65
|
return dispatch({
|
|
66
|
-
type: 'batch'
|
|
66
|
+
type: 'batch',
|
|
67
|
+
payload: payload
|
|
67
68
|
});
|
|
68
69
|
};
|
|
69
70
|
};
|
|
70
|
-
export var
|
|
71
|
+
export var mutateHandler = function mutateHandler(payload) {
|
|
71
72
|
return function (_ref7) {
|
|
72
73
|
var dispatch = _ref7.dispatch;
|
|
74
|
+
return dispatch({
|
|
75
|
+
type: 'mutate',
|
|
76
|
+
payload: payload
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
export var selectHandler = function selectHandler(payload) {
|
|
81
|
+
return function (_ref8) {
|
|
82
|
+
var dispatch = _ref8.dispatch;
|
|
73
83
|
return dispatch({
|
|
74
84
|
type: 'select',
|
|
75
|
-
|
|
85
|
+
payload: payload
|
|
76
86
|
});
|
|
77
87
|
};
|
|
78
88
|
};
|
|
@@ -88,11 +98,11 @@ export var selectHandler = function selectHandler(selection) {
|
|
|
88
98
|
*/
|
|
89
99
|
|
|
90
100
|
export var getDefaultKeyboardEventHandlers = function getDefaultKeyboardEventHandlers() {
|
|
91
|
-
return [function (
|
|
92
|
-
var sourceCode =
|
|
93
|
-
selection =
|
|
94
|
-
dispatch =
|
|
95
|
-
e =
|
|
101
|
+
return [function (_ref9) {
|
|
102
|
+
var sourceCode = _ref9.sourceCode,
|
|
103
|
+
selection = _ref9.selection,
|
|
104
|
+
dispatch = _ref9.dispatch,
|
|
105
|
+
e = _ref9.e;
|
|
96
106
|
return createKeybindingsHandler({
|
|
97
107
|
Tab: function Tab(e) {
|
|
98
108
|
e.preventDefault();
|
|
@@ -123,7 +133,9 @@ export var getDefaultKeyboardEventHandlers = function getDefaultKeyboardEventHan
|
|
|
123
133
|
e.preventDefault();
|
|
124
134
|
dispatch({
|
|
125
135
|
type: 'select',
|
|
126
|
-
|
|
136
|
+
payload: {
|
|
137
|
+
selection: createSelection(focusOffset + 1)
|
|
138
|
+
}
|
|
127
139
|
});
|
|
128
140
|
}
|
|
129
141
|
},
|
|
@@ -134,7 +146,9 @@ export var getDefaultKeyboardEventHandlers = function getDefaultKeyboardEventHan
|
|
|
134
146
|
e.preventDefault();
|
|
135
147
|
dispatch({
|
|
136
148
|
type: 'select',
|
|
137
|
-
|
|
149
|
+
payload: {
|
|
150
|
+
selection: createSelection(anchorOffset, focusOffset + 1)
|
|
151
|
+
}
|
|
138
152
|
});
|
|
139
153
|
}
|
|
140
154
|
}
|
package/dist/utils/history.d.ts
CHANGED
|
@@ -59,13 +59,23 @@ export interface RedoAction {
|
|
|
59
59
|
}
|
|
60
60
|
export interface BatchAction {
|
|
61
61
|
type: 'batch';
|
|
62
|
-
|
|
62
|
+
payload?: {
|
|
63
|
+
selection: ContainerSelection;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export interface MutateAction {
|
|
67
|
+
type: 'mutate';
|
|
68
|
+
payload: {
|
|
69
|
+
sourceCode: string;
|
|
70
|
+
};
|
|
63
71
|
}
|
|
64
72
|
export interface SelectAction {
|
|
65
73
|
type: 'select';
|
|
66
|
-
|
|
74
|
+
payload: {
|
|
75
|
+
selection: ContainerSelection;
|
|
76
|
+
};
|
|
67
77
|
}
|
|
68
|
-
type HistoryAction = UndoAction | RedoAction | BatchAction | SelectAction;
|
|
78
|
+
type HistoryAction = UndoAction | RedoAction | BatchAction | MutateAction | SelectAction;
|
|
69
79
|
export declare const useHistory: (initialSourceCode: string, readOnly?: boolean) => {
|
|
70
80
|
history: History;
|
|
71
81
|
sourceCode: string;
|
package/dist/utils/history.js
CHANGED
|
@@ -228,16 +228,26 @@ var historyReducer = function historyReducer(_ref2, action) {
|
|
|
228
228
|
}
|
|
229
229
|
case 'batch':
|
|
230
230
|
{
|
|
231
|
-
var _action$
|
|
231
|
+
var _action$payload$selec, _action$payload4, _action$payload$selec2, _action$payload5;
|
|
232
232
|
return {
|
|
233
233
|
states: [].concat(_toConsumableArray(states.slice(0, index)), [_objectSpread(_objectSpread({}, states[index]), {}, {
|
|
234
|
-
selection: (_action$
|
|
234
|
+
selection: (_action$payload$selec = (_action$payload4 = action.payload) === null || _action$payload4 === void 0 ? void 0 : _action$payload4.selection) !== null && _action$payload$selec !== void 0 ? _action$payload$selec : states[index].selection,
|
|
235
235
|
action: _objectSpread(_objectSpread({}, states[index].action), {}, {
|
|
236
236
|
batch: false
|
|
237
237
|
})
|
|
238
238
|
})]),
|
|
239
239
|
index: index,
|
|
240
|
-
selection: (_action$
|
|
240
|
+
selection: (_action$payload$selec2 = (_action$payload5 = action.payload) === null || _action$payload5 === void 0 ? void 0 : _action$payload5.selection) !== null && _action$payload$selec2 !== void 0 ? _action$payload$selec2 : selection
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
case 'mutate':
|
|
244
|
+
{
|
|
245
|
+
return {
|
|
246
|
+
states: [].concat(_toConsumableArray(states.slice(0, index)), [_objectSpread(_objectSpread({}, states[index]), {}, {
|
|
247
|
+
sourceCode: action.payload.sourceCode
|
|
248
|
+
})], _toConsumableArray(states.slice(index + 1))),
|
|
249
|
+
index: index,
|
|
250
|
+
selection: selection
|
|
241
251
|
};
|
|
242
252
|
}
|
|
243
253
|
case 'select':
|
|
@@ -245,7 +255,7 @@ var historyReducer = function historyReducer(_ref2, action) {
|
|
|
245
255
|
return {
|
|
246
256
|
states: states,
|
|
247
257
|
index: index,
|
|
248
|
-
selection: action.selection
|
|
258
|
+
selection: action.payload.selection
|
|
249
259
|
};
|
|
250
260
|
}
|
|
251
261
|
default:
|
package/dist/utils/markdown.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
1
2
|
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
2
3
|
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
3
4
|
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
|
|
4
5
|
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
5
6
|
import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
|
|
6
7
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
8
|
+
var _excluded = ["checked"];
|
|
7
9
|
import { toJsxRuntime } from 'hast-util-to-jsx-runtime';
|
|
8
10
|
import { toText as hastToText } from 'hast-util-to-text';
|
|
9
11
|
import { Fragment, jsx, jsxs } from 'react/jsx-runtime';
|
|
@@ -23,6 +25,7 @@ import remarkStringify from 'remark-stringify';
|
|
|
23
25
|
import strip from 'strip-markdown';
|
|
24
26
|
import { unified } from 'unified';
|
|
25
27
|
import { prefix } from "./global";
|
|
28
|
+
import rehypeCustom from "./rehype/rehype-custom";
|
|
26
29
|
import rehypeLine from "./rehype/rehype-line";
|
|
27
30
|
import rehypeMath, { isMathNode } from "./rehype/rehype-math";
|
|
28
31
|
import rehypeRawPositions from "./rehype/rehype-raw-positions";
|
|
@@ -332,6 +335,20 @@ export var getDefaultSchema = function getDefaultSchema() {
|
|
|
332
335
|
export var viewerRender = function viewerRender(root, schema) {
|
|
333
336
|
return unified().use(rehypeRawPositions).use(rehypeRaw).use(rehypeMath).use(rehypeSanitize, schema).use(rehypeLine).runSync(root);
|
|
334
337
|
};
|
|
338
|
+
var Input = function Input(_ref2) {
|
|
339
|
+
var checked = _ref2.checked,
|
|
340
|
+
props = _objectWithoutProperties(_ref2, _excluded);
|
|
341
|
+
return jsx('input', _objectSpread({
|
|
342
|
+
checked: !!checked
|
|
343
|
+
}, props));
|
|
344
|
+
};
|
|
345
|
+
var Custom = function Custom(_ref3) {
|
|
346
|
+
var component = _ref3.component,
|
|
347
|
+
value = _ref3.value;
|
|
348
|
+
return jsx(component, {
|
|
349
|
+
children: value
|
|
350
|
+
});
|
|
351
|
+
};
|
|
335
352
|
|
|
336
353
|
/**
|
|
337
354
|
* 将 Hast 树映射到 React 虚拟 DOM 树
|
|
@@ -343,17 +360,11 @@ export var viewerRender = function viewerRender(root, schema) {
|
|
|
343
360
|
export var postViewerRender = function postViewerRender(root, options) {
|
|
344
361
|
try {
|
|
345
362
|
var components = _objectSpread(_objectSpread({}, options.customHTMLElements), {}, {
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
custom: function custom(props) {
|
|
350
|
-
var _options$customBlocks;
|
|
351
|
-
return jsx((_options$customBlocks = options.customBlocks[props.meta]) !== null && _options$customBlocks !== void 0 ? _options$customBlocks : 'div', {
|
|
352
|
-
children: props.value
|
|
353
|
-
});
|
|
354
|
-
}
|
|
363
|
+
// 直接在这里写函数会有重复渲染问题,因此用全局组件
|
|
364
|
+
input: Input,
|
|
365
|
+
custom: Custom
|
|
355
366
|
});
|
|
356
|
-
return toJsxRuntime(unified().use(rehypeKatex).runSync(root), {
|
|
367
|
+
return toJsxRuntime(unified().use(rehypeKatex).use(rehypeCustom, options.customBlocks).runSync(root), {
|
|
357
368
|
Fragment: Fragment,
|
|
358
369
|
jsx: jsx,
|
|
359
370
|
jsxs: jsxs,
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { SKIP, visit } from 'unist-util-visit';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 为 Custom 节点添加组件属性
|
|
5
|
+
*/
|
|
6
|
+
var rehypeCustom = function rehypeCustom(customBlocks) {
|
|
7
|
+
return function (root) {
|
|
8
|
+
return visit(root, {
|
|
9
|
+
tagName: 'custom'
|
|
10
|
+
}, function (node) {
|
|
11
|
+
var _node$properties;
|
|
12
|
+
if (typeof ((_node$properties = node.properties) === null || _node$properties === void 0 ? void 0 : _node$properties.meta) === 'string') {
|
|
13
|
+
var _customBlocks$node$pr;
|
|
14
|
+
node.properties.component = (_customBlocks$node$pr = customBlocks[node.properties.meta]) !== null && _customBlocks$node$pr !== void 0 ? _customBlocks$node$pr : 'div';
|
|
15
|
+
}
|
|
16
|
+
return SKIP;
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
export default rehypeCustom;
|
|
@@ -21,10 +21,18 @@ export interface XStarEditorProps {
|
|
|
21
21
|
* 语言
|
|
22
22
|
*/
|
|
23
23
|
locale?: XStarMdEditorProps['locale'];
|
|
24
|
+
/**
|
|
25
|
+
* 配置未输入时的提示语
|
|
26
|
+
*/
|
|
27
|
+
placeholder?: string;
|
|
24
28
|
/**
|
|
25
29
|
* 初始文本
|
|
26
30
|
*/
|
|
27
31
|
initialValue?: string;
|
|
32
|
+
/**
|
|
33
|
+
* 文本
|
|
34
|
+
*/
|
|
35
|
+
value?: string;
|
|
28
36
|
/**
|
|
29
37
|
* 是否只读
|
|
30
38
|
*/
|
|
@@ -36,7 +44,7 @@ export interface XStarEditorProps {
|
|
|
36
44
|
/**
|
|
37
45
|
* 编辑器属性
|
|
38
46
|
*/
|
|
39
|
-
editorProps?: Omit<XStarMdEditorProps, 'height' | 'locale' | 'initialValue' | '
|
|
47
|
+
editorProps?: Omit<XStarMdEditorProps, 'height' | 'locale' | 'placeholder' | 'initialValue' | 'value' | 'readOnly' | 'onChange' | 'onInsertFile'>;
|
|
40
48
|
/**
|
|
41
49
|
* 查看器属性
|
|
42
50
|
*/
|
|
@@ -53,10 +61,6 @@ export interface XStarEditorProps {
|
|
|
53
61
|
* 文件插入回调函数
|
|
54
62
|
*/
|
|
55
63
|
onInsertFile?: XStarMdEditorProps['onInsertFile'];
|
|
56
|
-
/**
|
|
57
|
-
* 配置未输入时的提示语
|
|
58
|
-
*/
|
|
59
|
-
placeholder?: string;
|
|
60
64
|
}
|
|
61
65
|
declare const XStarEditor: React.ForwardRefExoticComponent<XStarEditorProps & React.RefAttributes<XStarEditorHandle>>;
|
|
62
66
|
export default XStarEditor;
|
|
@@ -41,16 +41,17 @@ var XStarEditor = /*#__PURE__*/React.forwardRef(function (_ref2, ref) {
|
|
|
41
41
|
style = _ref2.style,
|
|
42
42
|
height = _ref2.height,
|
|
43
43
|
locale = _ref2.locale,
|
|
44
|
+
placeholder = _ref2.placeholder,
|
|
44
45
|
_ref2$initialValue = _ref2.initialValue,
|
|
45
46
|
initialValue = _ref2$initialValue === void 0 ? '' : _ref2$initialValue,
|
|
47
|
+
value = _ref2.value,
|
|
46
48
|
readOnly = _ref2.readOnly,
|
|
47
49
|
enableWebWorker = _ref2.enableWebWorker,
|
|
48
50
|
editorProps = _ref2.editorProps,
|
|
49
51
|
viewerProps = _ref2.viewerProps,
|
|
50
52
|
viewerRender = _ref2.viewerRender,
|
|
51
53
|
_onChange = _ref2.onChange,
|
|
52
|
-
onInsertFile = _ref2.onInsertFile
|
|
53
|
-
placeholder = _ref2.placeholder;
|
|
54
|
+
onInsertFile = _ref2.onInsertFile;
|
|
54
55
|
var editorRef = useMdEditorRef();
|
|
55
56
|
var viewerRef = useMdViewerRef();
|
|
56
57
|
var containerRef = useRef(null);
|
|
@@ -165,20 +166,16 @@ var XStarEditor = /*#__PURE__*/React.forwardRef(function (_ref2, ref) {
|
|
|
165
166
|
viewer.removeEventListener('render', render);
|
|
166
167
|
};
|
|
167
168
|
}, []);
|
|
168
|
-
var _useState = useState(
|
|
169
|
+
var _useState = useState(['editor', 'viewer']),
|
|
169
170
|
_useState2 = _slicedToArray(_useState, 2),
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
var _useState3 = useState(['editor', 'viewer']),
|
|
173
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
174
|
-
layout = _useState4[0],
|
|
175
|
-
setLayout = _useState4[1];
|
|
171
|
+
layout = _useState2[0],
|
|
172
|
+
setLayout = _useState2[1];
|
|
176
173
|
var editOnly = layout.includes('editor') && !layout.includes('viewer');
|
|
177
174
|
var viewOnly = !layout.includes('editor') && layout.includes('viewer');
|
|
178
|
-
var
|
|
179
|
-
|
|
180
|
-
fullscreen =
|
|
181
|
-
setFullscreen =
|
|
175
|
+
var _useState3 = useState(false),
|
|
176
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
177
|
+
fullscreen = _useState4[0],
|
|
178
|
+
setFullscreen = _useState4[1];
|
|
182
179
|
var editorPlugins = useMemo(function () {
|
|
183
180
|
var _editorProps$plugins;
|
|
184
181
|
return [function (ctx) {
|
|
@@ -238,6 +235,10 @@ var XStarEditor = /*#__PURE__*/React.forwardRef(function (_ref2, ref) {
|
|
|
238
235
|
};
|
|
239
236
|
}
|
|
240
237
|
}, [fullscreen]);
|
|
238
|
+
var _useState5 = useState(initialValue),
|
|
239
|
+
_useState6 = _slicedToArray(_useState5, 2),
|
|
240
|
+
sourceCode = _useState6[0],
|
|
241
|
+
setSourceCode = _useState6[1];
|
|
241
242
|
return /*#__PURE__*/React.createElement("div", {
|
|
242
243
|
ref: containerRef,
|
|
243
244
|
className: classNames("".concat(prefix, "editor"), _defineProperty({}, "".concat(prefix, "fullscreen"), fullscreen), className),
|
|
@@ -248,26 +249,26 @@ var XStarEditor = /*#__PURE__*/React.forwardRef(function (_ref2, ref) {
|
|
|
248
249
|
className: classNames((_classNames4 = {}, _defineProperty(_classNames4, "".concat(prefix, "active"), editOnly), _defineProperty(_classNames4, "".concat(prefix, "hidden"), viewOnly), _classNames4), editorProps === null || editorProps === void 0 ? void 0 : editorProps.className),
|
|
249
250
|
height: height,
|
|
250
251
|
locale: locale,
|
|
251
|
-
|
|
252
|
+
placeholder: placeholder,
|
|
253
|
+
value: value !== null && value !== void 0 ? value : sourceCode,
|
|
252
254
|
readOnly: viewOnly || readOnly,
|
|
253
255
|
plugins: editorPlugins,
|
|
254
256
|
onChange: function onChange(value) {
|
|
255
|
-
|
|
257
|
+
setSourceCode(value);
|
|
256
258
|
_onChange === null || _onChange === void 0 ? void 0 : _onChange(value);
|
|
257
259
|
},
|
|
258
|
-
onInsertFile: onInsertFile
|
|
259
|
-
placeholder: placeholder
|
|
260
|
+
onInsertFile: onInsertFile
|
|
260
261
|
})), viewerRender ? /*#__PURE__*/React.createElement(ViewerRenderWrapper, {
|
|
261
262
|
ref: viewerRef,
|
|
262
263
|
className: classNames((_classNames5 = {}, _defineProperty(_classNames5, "".concat(prefix, "active"), viewOnly), _defineProperty(_classNames5, "".concat(prefix, "hidden"), editOnly), _classNames5), viewerProps === null || viewerProps === void 0 ? void 0 : viewerProps.className),
|
|
263
264
|
style: viewerProps === null || viewerProps === void 0 ? void 0 : viewerProps.style,
|
|
264
265
|
height: height
|
|
265
|
-
}, viewerRender(value)) : /*#__PURE__*/React.createElement(XStarMdViewer, _extends({
|
|
266
|
+
}, viewerRender(value !== null && value !== void 0 ? value : sourceCode)) : /*#__PURE__*/React.createElement(XStarMdViewer, _extends({
|
|
266
267
|
ref: viewerRef
|
|
267
268
|
}, viewerProps, {
|
|
268
269
|
className: classNames((_classNames6 = {}, _defineProperty(_classNames6, "".concat(prefix, "active"), viewOnly), _defineProperty(_classNames6, "".concat(prefix, "hidden"), editOnly), _classNames6), viewerProps === null || viewerProps === void 0 ? void 0 : viewerProps.className),
|
|
269
270
|
height: height,
|
|
270
|
-
value: value,
|
|
271
|
+
value: value !== null && value !== void 0 ? value : sourceCode,
|
|
271
272
|
enableWebWorker: enableWebWorker
|
|
272
273
|
})));
|
|
273
274
|
});
|
|
@@ -49,10 +49,18 @@ export interface XStarMdEditorProps {
|
|
|
49
49
|
* 语言
|
|
50
50
|
*/
|
|
51
51
|
locale?: string;
|
|
52
|
+
/**
|
|
53
|
+
* 配置未输入时的提示语
|
|
54
|
+
*/
|
|
55
|
+
placeholder?: string;
|
|
52
56
|
/**
|
|
53
57
|
* 初始文本
|
|
54
58
|
*/
|
|
55
59
|
initialValue?: string;
|
|
60
|
+
/**
|
|
61
|
+
* 文本
|
|
62
|
+
*/
|
|
63
|
+
value?: string;
|
|
56
64
|
/**
|
|
57
65
|
* 是否只读
|
|
58
66
|
*/
|
|
@@ -72,10 +80,6 @@ export interface XStarMdEditorProps {
|
|
|
72
80
|
description: string;
|
|
73
81
|
image: boolean;
|
|
74
82
|
}) => void;
|
|
75
|
-
/**
|
|
76
|
-
* 配置未输入时的提示语
|
|
77
|
-
*/
|
|
78
|
-
placeholder?: string;
|
|
79
83
|
}
|
|
80
84
|
declare const XStarMdEditor: React.ForwardRefExoticComponent<XStarMdEditorProps & React.RefAttributes<XStarMdEditorHandle>>;
|
|
81
85
|
export default XStarMdEditor;
|
|
@@ -15,13 +15,14 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
|
15
15
|
toolbarClassName = _ref.toolbarClassName,
|
|
16
16
|
toolbarStyle = _ref.toolbarStyle,
|
|
17
17
|
locale = _ref.locale,
|
|
18
|
+
placeholder = _ref.placeholder,
|
|
18
19
|
_ref$initialValue = _ref.initialValue,
|
|
19
20
|
initialValue = _ref$initialValue === void 0 ? '' : _ref$initialValue,
|
|
21
|
+
value = _ref.value,
|
|
20
22
|
readOnly = _ref.readOnly,
|
|
21
23
|
plugins = _ref.plugins,
|
|
22
24
|
onChange = _ref.onChange,
|
|
23
|
-
onInsertFile = _ref.onInsertFile
|
|
24
|
-
placeholder = _ref.placeholder;
|
|
25
|
+
onInsertFile = _ref.onInsertFile;
|
|
25
26
|
var containerRef = useRef(null);
|
|
26
27
|
var container = useContainer(containerRef);
|
|
27
28
|
var _useHistory = useHistory(initialValue, readOnly),
|
|
@@ -52,6 +53,20 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
|
52
53
|
ignoreNext.current = false;
|
|
53
54
|
initialized.current = true;
|
|
54
55
|
}, [sourceCode, selection]);
|
|
56
|
+
|
|
57
|
+
// 受控时将内部文本与外部文本同步
|
|
58
|
+
useEffect(function () {
|
|
59
|
+
if (value !== undefined && sourceCode !== value) {
|
|
60
|
+
// 不触发 onChange 事件
|
|
61
|
+
initialized.current = false;
|
|
62
|
+
dispatch({
|
|
63
|
+
type: 'mutate',
|
|
64
|
+
payload: {
|
|
65
|
+
sourceCode: value
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}, [value]);
|
|
55
70
|
var historyLatest = useRef(history);
|
|
56
71
|
historyLatest.current = history;
|
|
57
72
|
var sourceCodeLatest = useRef(sourceCode);
|
|
@@ -86,15 +101,12 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
|
86
101
|
return containerRef.current;
|
|
87
102
|
},
|
|
88
103
|
getValue: function getValue() {
|
|
89
|
-
return
|
|
90
|
-
var sourceCode = _ref2.sourceCode;
|
|
91
|
-
return sourceCode;
|
|
92
|
-
});
|
|
104
|
+
return sourceCodeLatest.current;
|
|
93
105
|
},
|
|
94
106
|
setValue: function setValue(value) {
|
|
95
|
-
return exec(function (
|
|
96
|
-
var selection =
|
|
97
|
-
dispatch =
|
|
107
|
+
return exec(function (_ref2) {
|
|
108
|
+
var selection = _ref2.selection,
|
|
109
|
+
dispatch = _ref2.dispatch;
|
|
98
110
|
return dispatch({
|
|
99
111
|
type: 'set',
|
|
100
112
|
payload: {
|
|
@@ -393,14 +405,14 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
|
393
405
|
style: _objectSpread(_objectSpread({}, style), {}, {
|
|
394
406
|
height: height
|
|
395
407
|
}),
|
|
408
|
+
placeholder: placeholder,
|
|
396
409
|
contentEditable: true,
|
|
397
410
|
onBlur: focusEventHandler,
|
|
398
411
|
onCompositionEnd: compositionEventHandler,
|
|
399
412
|
onCopy: clipboardEventHandler,
|
|
400
413
|
onCut: clipboardEventHandler,
|
|
401
414
|
onDragStart: dragEventHandler,
|
|
402
|
-
onKeyDown: keyboardEventHandler
|
|
403
|
-
placeholder: placeholder
|
|
415
|
+
onKeyDown: keyboardEventHandler
|
|
404
416
|
}));
|
|
405
417
|
});
|
|
406
418
|
if (process.env.NODE_ENV !== 'production') {
|