x-star-editor 0.3.11 → 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 +6 -0
- 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 -1
- package/dist/x-star-editor/index.js +18 -15
- package/dist/x-star-md-editor/index.d.ts +8 -0
- package/dist/x-star-md-editor/index.js +23 -8
- 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
|
@@ -242,6 +242,12 @@
|
|
|
242
242
|
.x-star-editor-md-editor > :last-child {
|
|
243
243
|
margin-bottom: 0.5em;
|
|
244
244
|
}
|
|
245
|
+
.x-star-editor-md-editor.x-star-editor-empty::before {
|
|
246
|
+
content: attr(placeholder);
|
|
247
|
+
position: absolute;
|
|
248
|
+
margin-top: 0.5em;
|
|
249
|
+
color: rgba(0, 0, 0, 0.28);
|
|
250
|
+
}
|
|
245
251
|
|
|
246
252
|
.x-star-editor-md-viewer {
|
|
247
253
|
position: relative;
|
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' | 'readOnly' | 'onChange' | 'onInsertFile'>;
|
|
47
|
+
editorProps?: Omit<XStarMdEditorProps, 'height' | 'locale' | 'placeholder' | 'initialValue' | 'value' | 'readOnly' | 'onChange' | 'onInsertFile'>;
|
|
40
48
|
/**
|
|
41
49
|
* 查看器属性
|
|
42
50
|
*/
|
|
@@ -41,8 +41,10 @@ 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,
|
|
@@ -164,20 +166,16 @@ var XStarEditor = /*#__PURE__*/React.forwardRef(function (_ref2, ref) {
|
|
|
164
166
|
viewer.removeEventListener('render', render);
|
|
165
167
|
};
|
|
166
168
|
}, []);
|
|
167
|
-
var _useState = useState(
|
|
169
|
+
var _useState = useState(['editor', 'viewer']),
|
|
168
170
|
_useState2 = _slicedToArray(_useState, 2),
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
var _useState3 = useState(['editor', 'viewer']),
|
|
172
|
-
_useState4 = _slicedToArray(_useState3, 2),
|
|
173
|
-
layout = _useState4[0],
|
|
174
|
-
setLayout = _useState4[1];
|
|
171
|
+
layout = _useState2[0],
|
|
172
|
+
setLayout = _useState2[1];
|
|
175
173
|
var editOnly = layout.includes('editor') && !layout.includes('viewer');
|
|
176
174
|
var viewOnly = !layout.includes('editor') && layout.includes('viewer');
|
|
177
|
-
var
|
|
178
|
-
|
|
179
|
-
fullscreen =
|
|
180
|
-
setFullscreen =
|
|
175
|
+
var _useState3 = useState(false),
|
|
176
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
177
|
+
fullscreen = _useState4[0],
|
|
178
|
+
setFullscreen = _useState4[1];
|
|
181
179
|
var editorPlugins = useMemo(function () {
|
|
182
180
|
var _editorProps$plugins;
|
|
183
181
|
return [function (ctx) {
|
|
@@ -237,6 +235,10 @@ var XStarEditor = /*#__PURE__*/React.forwardRef(function (_ref2, ref) {
|
|
|
237
235
|
};
|
|
238
236
|
}
|
|
239
237
|
}, [fullscreen]);
|
|
238
|
+
var _useState5 = useState(initialValue),
|
|
239
|
+
_useState6 = _slicedToArray(_useState5, 2),
|
|
240
|
+
sourceCode = _useState6[0],
|
|
241
|
+
setSourceCode = _useState6[1];
|
|
240
242
|
return /*#__PURE__*/React.createElement("div", {
|
|
241
243
|
ref: containerRef,
|
|
242
244
|
className: classNames("".concat(prefix, "editor"), _defineProperty({}, "".concat(prefix, "fullscreen"), fullscreen), className),
|
|
@@ -247,11 +249,12 @@ var XStarEditor = /*#__PURE__*/React.forwardRef(function (_ref2, ref) {
|
|
|
247
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),
|
|
248
250
|
height: height,
|
|
249
251
|
locale: locale,
|
|
250
|
-
|
|
252
|
+
placeholder: placeholder,
|
|
253
|
+
value: value !== null && value !== void 0 ? value : sourceCode,
|
|
251
254
|
readOnly: viewOnly || readOnly,
|
|
252
255
|
plugins: editorPlugins,
|
|
253
256
|
onChange: function onChange(value) {
|
|
254
|
-
|
|
257
|
+
setSourceCode(value);
|
|
255
258
|
_onChange === null || _onChange === void 0 ? void 0 : _onChange(value);
|
|
256
259
|
},
|
|
257
260
|
onInsertFile: onInsertFile
|
|
@@ -260,12 +263,12 @@ var XStarEditor = /*#__PURE__*/React.forwardRef(function (_ref2, ref) {
|
|
|
260
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),
|
|
261
264
|
style: viewerProps === null || viewerProps === void 0 ? void 0 : viewerProps.style,
|
|
262
265
|
height: height
|
|
263
|
-
}, viewerRender(value)) : /*#__PURE__*/React.createElement(XStarMdViewer, _extends({
|
|
266
|
+
}, viewerRender(value !== null && value !== void 0 ? value : sourceCode)) : /*#__PURE__*/React.createElement(XStarMdViewer, _extends({
|
|
264
267
|
ref: viewerRef
|
|
265
268
|
}, viewerProps, {
|
|
266
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),
|
|
267
270
|
height: height,
|
|
268
|
-
value: value,
|
|
271
|
+
value: value !== null && value !== void 0 ? value : sourceCode,
|
|
269
272
|
enableWebWorker: enableWebWorker
|
|
270
273
|
})));
|
|
271
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
|
*/
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
2
3
|
import classNames from 'classnames';
|
|
3
4
|
import React, { useEffect, useImperativeHandle, useMemo, useRef } from 'react';
|
|
4
5
|
import Toolbar, { getDefaultToolbarItemMap, getDefaultToolbarItems } from "../components/Toolbar";
|
|
@@ -14,8 +15,10 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
|
14
15
|
toolbarClassName = _ref.toolbarClassName,
|
|
15
16
|
toolbarStyle = _ref.toolbarStyle,
|
|
16
17
|
locale = _ref.locale,
|
|
18
|
+
placeholder = _ref.placeholder,
|
|
17
19
|
_ref$initialValue = _ref.initialValue,
|
|
18
20
|
initialValue = _ref$initialValue === void 0 ? '' : _ref$initialValue,
|
|
21
|
+
value = _ref.value,
|
|
19
22
|
readOnly = _ref.readOnly,
|
|
20
23
|
plugins = _ref.plugins,
|
|
21
24
|
onChange = _ref.onChange,
|
|
@@ -50,6 +53,20 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
|
50
53
|
ignoreNext.current = false;
|
|
51
54
|
initialized.current = true;
|
|
52
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]);
|
|
53
70
|
var historyLatest = useRef(history);
|
|
54
71
|
historyLatest.current = history;
|
|
55
72
|
var sourceCodeLatest = useRef(sourceCode);
|
|
@@ -84,15 +101,12 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
|
84
101
|
return containerRef.current;
|
|
85
102
|
},
|
|
86
103
|
getValue: function getValue() {
|
|
87
|
-
return
|
|
88
|
-
var sourceCode = _ref2.sourceCode;
|
|
89
|
-
return sourceCode;
|
|
90
|
-
});
|
|
104
|
+
return sourceCodeLatest.current;
|
|
91
105
|
},
|
|
92
106
|
setValue: function setValue(value) {
|
|
93
|
-
return exec(function (
|
|
94
|
-
var selection =
|
|
95
|
-
dispatch =
|
|
107
|
+
return exec(function (_ref2) {
|
|
108
|
+
var selection = _ref2.selection,
|
|
109
|
+
dispatch = _ref2.dispatch;
|
|
96
110
|
return dispatch({
|
|
97
111
|
type: 'set',
|
|
98
112
|
payload: {
|
|
@@ -387,10 +401,11 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
|
387
401
|
exec: exec
|
|
388
402
|
}), /*#__PURE__*/React.createElement("div", {
|
|
389
403
|
ref: containerRef,
|
|
390
|
-
className: classNames("".concat(prefix, "md-editor"), className),
|
|
404
|
+
className: classNames("".concat(prefix, "md-editor"), _defineProperty({}, "".concat(prefix, "empty"), !sourceCode), className),
|
|
391
405
|
style: _objectSpread(_objectSpread({}, style), {}, {
|
|
392
406
|
height: height
|
|
393
407
|
}),
|
|
408
|
+
placeholder: placeholder,
|
|
394
409
|
contentEditable: true,
|
|
395
410
|
onBlur: focusEventHandler,
|
|
396
411
|
onCompositionEnd: compositionEventHandler,
|