x-star-editor 0.1.2 → 0.2.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/types/worker.js.d.ts +3 -0
- package/dist/utils/handler.js +229 -84
- package/dist/x-star-editor/index.d.ts +9 -3
- package/dist/x-star-editor/index.js +10 -18
- package/dist/x-star-md-editor/index.d.ts +5 -1
- package/dist/x-star-md-editor/index.js +73 -41
- package/dist/x-star-md-viewer/index.d.ts +5 -1
- package/dist/x-star-md-viewer/index.js +14 -6
- package/package.json +4 -2
- /package/dist/{utils → types}/jsx-runtime.d.ts +0 -0
- /package/{workers → workers-dist}/markdown.worker.js +0 -0
package/dist/utils/handler.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
|
|
2
|
+
import { createKeybindingsHandler } from 'tinykeys';
|
|
2
3
|
import { createSelection, getRange } from "./container";
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -90,111 +91,255 @@ export var selectHandler = function selectHandler(selection) {
|
|
|
90
91
|
*/
|
|
91
92
|
|
|
92
93
|
export var getDefaultKeyboardEventHandlers = function getDefaultKeyboardEventHandlers() {
|
|
93
|
-
return [
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
var selection = _ref8.selection,
|
|
94
|
+
return [function (_ref8) {
|
|
95
|
+
var sourceCode = _ref8.sourceCode,
|
|
96
|
+
selection = _ref8.selection,
|
|
97
97
|
dispatch = _ref8.dispatch,
|
|
98
98
|
e = _ref8.e;
|
|
99
|
-
|
|
100
|
-
e
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
e
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
99
|
+
return createKeybindingsHandler({
|
|
100
|
+
Tab: function Tab(e) {
|
|
101
|
+
e.preventDefault();
|
|
102
|
+
dispatch({
|
|
103
|
+
type: 'insert',
|
|
104
|
+
payload: {
|
|
105
|
+
text: '\t'
|
|
106
|
+
},
|
|
107
|
+
selection: selection
|
|
108
|
+
});
|
|
109
|
+
},
|
|
110
|
+
Backspace: function Backspace(e) {
|
|
111
|
+
e.preventDefault();
|
|
112
|
+
dispatch({
|
|
113
|
+
type: 'delete',
|
|
114
|
+
payload: {
|
|
115
|
+
direction: 'backward',
|
|
116
|
+
boundary: 'char'
|
|
117
|
+
},
|
|
118
|
+
selection: selection
|
|
119
|
+
});
|
|
120
|
+
},
|
|
121
|
+
'$mod+Backspace': function $modBackspace(e) {
|
|
122
|
+
e.preventDefault();
|
|
123
|
+
dispatch({
|
|
124
|
+
type: 'delete',
|
|
125
|
+
payload: {
|
|
126
|
+
direction: 'backward',
|
|
127
|
+
boundary: 'line'
|
|
128
|
+
},
|
|
129
|
+
selection: selection
|
|
130
|
+
});
|
|
131
|
+
},
|
|
132
|
+
'Shift+Backspace': function ShiftBackspace(e) {
|
|
133
|
+
e.preventDefault();
|
|
134
|
+
dispatch({
|
|
135
|
+
type: 'delete',
|
|
136
|
+
payload: {
|
|
137
|
+
direction: 'backward',
|
|
138
|
+
boundary: 'char'
|
|
139
|
+
},
|
|
140
|
+
selection: selection
|
|
141
|
+
});
|
|
142
|
+
},
|
|
143
|
+
'Meta+Backspace': function MetaBackspace(e) {
|
|
144
|
+
return e.preventDefault();
|
|
145
|
+
},
|
|
146
|
+
'Alt+Backspace': function AltBackspace(e) {
|
|
147
|
+
return e.preventDefault();
|
|
148
|
+
},
|
|
149
|
+
'Control+Backspace': function ControlBackspace(e) {
|
|
150
|
+
return e.preventDefault();
|
|
151
|
+
},
|
|
152
|
+
'Shift+Meta+Backspace': function ShiftMetaBackspace(e) {
|
|
153
|
+
return e.preventDefault();
|
|
154
|
+
},
|
|
155
|
+
'Shift+Alt+Backspace': function ShiftAltBackspace(e) {
|
|
156
|
+
return e.preventDefault();
|
|
157
|
+
},
|
|
158
|
+
'Shift+Control+Backspace': function ShiftControlBackspace(e) {
|
|
159
|
+
return e.preventDefault();
|
|
160
|
+
},
|
|
161
|
+
'Meta+Alt+Backspace': function MetaAltBackspace(e) {
|
|
162
|
+
return e.preventDefault();
|
|
163
|
+
},
|
|
164
|
+
'Meta+Control+Backspace': function MetaControlBackspace(e) {
|
|
165
|
+
return e.preventDefault();
|
|
166
|
+
},
|
|
167
|
+
'Alt+Control+Backspace': function AltControlBackspace(e) {
|
|
168
|
+
return e.preventDefault();
|
|
169
|
+
},
|
|
170
|
+
'Shift+Meta+Alt+Backspace': function ShiftMetaAltBackspace(e) {
|
|
171
|
+
return e.preventDefault();
|
|
172
|
+
},
|
|
173
|
+
'Shift+Meta+Control+Backspace': function ShiftMetaControlBackspace(e) {
|
|
174
|
+
return e.preventDefault();
|
|
175
|
+
},
|
|
176
|
+
'Shift+Alt+Control+Backspace': function ShiftAltControlBackspace(e) {
|
|
177
|
+
return e.preventDefault();
|
|
178
|
+
},
|
|
179
|
+
'Meta+Alt+Control+Backspace': function MetaAltControlBackspace(e) {
|
|
180
|
+
return e.preventDefault();
|
|
181
|
+
},
|
|
182
|
+
Delete: function Delete(e) {
|
|
183
|
+
e.preventDefault();
|
|
184
|
+
dispatch({
|
|
185
|
+
type: 'delete',
|
|
186
|
+
payload: {
|
|
187
|
+
direction: 'forward',
|
|
188
|
+
boundary: 'char'
|
|
189
|
+
},
|
|
190
|
+
selection: selection
|
|
191
|
+
});
|
|
192
|
+
},
|
|
193
|
+
'$mod+Delete': function $modDelete(e) {
|
|
194
|
+
e.preventDefault();
|
|
195
|
+
dispatch({
|
|
196
|
+
type: 'delete',
|
|
197
|
+
payload: {
|
|
198
|
+
direction: 'forward',
|
|
199
|
+
boundary: 'line'
|
|
200
|
+
},
|
|
201
|
+
selection: selection
|
|
202
|
+
});
|
|
203
|
+
},
|
|
204
|
+
'Shift+Delete': function ShiftDelete(e) {
|
|
205
|
+
e.preventDefault();
|
|
206
|
+
dispatch({
|
|
207
|
+
type: 'delete',
|
|
208
|
+
payload: {
|
|
209
|
+
direction: 'forward',
|
|
210
|
+
boundary: 'char'
|
|
211
|
+
},
|
|
212
|
+
selection: selection
|
|
213
|
+
});
|
|
214
|
+
},
|
|
215
|
+
'Meta+Delete': function MetaDelete(e) {
|
|
216
|
+
return e.preventDefault();
|
|
217
|
+
},
|
|
218
|
+
'Alt+Delete': function AltDelete(e) {
|
|
219
|
+
return e.preventDefault();
|
|
220
|
+
},
|
|
221
|
+
'Control+Delete': function ControlDelete(e) {
|
|
222
|
+
return e.preventDefault();
|
|
223
|
+
},
|
|
224
|
+
'Shift+Meta+Delete': function ShiftMetaDelete(e) {
|
|
225
|
+
return e.preventDefault();
|
|
226
|
+
},
|
|
227
|
+
'Shift+Alt+Delete': function ShiftAltDelete(e) {
|
|
228
|
+
return e.preventDefault();
|
|
229
|
+
},
|
|
230
|
+
'Shift+Control+Delete': function ShiftControlDelete(e) {
|
|
231
|
+
return e.preventDefault();
|
|
232
|
+
},
|
|
233
|
+
'Meta+Alt+Delete': function MetaAltDelete(e) {
|
|
234
|
+
return e.preventDefault();
|
|
235
|
+
},
|
|
236
|
+
'Meta+Control+Delete': function MetaControlDelete(e) {
|
|
237
|
+
return e.preventDefault();
|
|
238
|
+
},
|
|
239
|
+
'Alt+Control+Delete': function AltControlDelete(e) {
|
|
240
|
+
return e.preventDefault();
|
|
241
|
+
},
|
|
242
|
+
'Shift+Meta+Alt+Delete': function ShiftMetaAltDelete(e) {
|
|
243
|
+
return e.preventDefault();
|
|
244
|
+
},
|
|
245
|
+
'Shift+Meta+Control+Delete': function ShiftMetaControlDelete(e) {
|
|
246
|
+
return e.preventDefault();
|
|
247
|
+
},
|
|
248
|
+
'Shift+Alt+Control+Delete': function ShiftAltControlDelete(e) {
|
|
249
|
+
return e.preventDefault();
|
|
250
|
+
},
|
|
251
|
+
'Meta+Alt+Control+Delete': function MetaAltControlDelete(e) {
|
|
252
|
+
return e.preventDefault();
|
|
253
|
+
},
|
|
254
|
+
'$mod+KeyI': function $modKeyI(e) {
|
|
255
|
+
e.preventDefault();
|
|
256
|
+
dispatch({
|
|
257
|
+
type: 'toggle',
|
|
258
|
+
payload: {
|
|
259
|
+
type: 'emphasis'
|
|
260
|
+
},
|
|
261
|
+
selection: selection
|
|
262
|
+
});
|
|
263
|
+
},
|
|
264
|
+
'$mod+KeyB': function $modKeyB(e) {
|
|
265
|
+
e.preventDefault();
|
|
266
|
+
dispatch({
|
|
267
|
+
type: 'toggle',
|
|
268
|
+
payload: {
|
|
269
|
+
type: 'strong'
|
|
270
|
+
},
|
|
271
|
+
selection: selection
|
|
272
|
+
});
|
|
273
|
+
},
|
|
274
|
+
'$mod+KeyZ': function $modKeyZ(e) {
|
|
275
|
+
e.preventDefault();
|
|
276
|
+
dispatch({
|
|
277
|
+
type: 'undo'
|
|
278
|
+
});
|
|
279
|
+
},
|
|
280
|
+
'$mod+KeyY': function $modKeyY(e) {
|
|
281
|
+
e.preventDefault();
|
|
282
|
+
dispatch({
|
|
283
|
+
type: 'redo'
|
|
284
|
+
});
|
|
285
|
+
},
|
|
286
|
+
ArrowLeft: function ArrowLeft(e) {
|
|
287
|
+
e.preventDefault();
|
|
288
|
+
var _getRange = getRange(selection),
|
|
289
|
+
startOffset = _getRange.startOffset,
|
|
290
|
+
endOffset = _getRange.endOffset;
|
|
291
|
+
if (startOffset < endOffset) {
|
|
169
292
|
dispatch({
|
|
170
293
|
type: 'select',
|
|
171
|
-
selection: createSelection(
|
|
294
|
+
selection: createSelection(startOffset)
|
|
172
295
|
});
|
|
173
|
-
} else if (
|
|
296
|
+
} else if (startOffset) {
|
|
174
297
|
dispatch({
|
|
175
298
|
type: 'select',
|
|
176
|
-
selection: createSelection(
|
|
299
|
+
selection: createSelection(startOffset - 1)
|
|
177
300
|
});
|
|
178
301
|
}
|
|
179
|
-
}
|
|
180
|
-
|
|
302
|
+
},
|
|
303
|
+
'Shift+ArrowLeft': function ShiftArrowLeft(e) {
|
|
304
|
+
e.preventDefault();
|
|
305
|
+
var anchorOffset = selection.anchorOffset,
|
|
306
|
+
focusOffset = selection.focusOffset;
|
|
307
|
+
if (focusOffset) {
|
|
181
308
|
dispatch({
|
|
182
309
|
type: 'select',
|
|
183
|
-
selection: createSelection(
|
|
310
|
+
selection: createSelection(anchorOffset, focusOffset - 1)
|
|
184
311
|
});
|
|
185
|
-
}
|
|
312
|
+
}
|
|
313
|
+
},
|
|
314
|
+
ArrowRight: function ArrowRight(e) {
|
|
315
|
+
e.preventDefault();
|
|
316
|
+
var _getRange2 = getRange(selection),
|
|
317
|
+
startOffset = _getRange2.startOffset,
|
|
318
|
+
endOffset = _getRange2.endOffset;
|
|
319
|
+
if (startOffset < endOffset) {
|
|
186
320
|
dispatch({
|
|
187
321
|
type: 'select',
|
|
188
|
-
selection: createSelection(
|
|
322
|
+
selection: createSelection(endOffset)
|
|
189
323
|
});
|
|
190
|
-
} else if (
|
|
324
|
+
} else if (startOffset < sourceCode.length) {
|
|
191
325
|
dispatch({
|
|
192
326
|
type: 'select',
|
|
193
327
|
selection: createSelection(startOffset + 1)
|
|
194
328
|
});
|
|
195
329
|
}
|
|
330
|
+
},
|
|
331
|
+
'Shift+ArrowRight': function ShiftArrowRight(e) {
|
|
332
|
+
e.preventDefault();
|
|
333
|
+
var anchorOffset = selection.anchorOffset,
|
|
334
|
+
focusOffset = selection.focusOffset;
|
|
335
|
+
if (focusOffset < sourceCode.length) {
|
|
336
|
+
dispatch({
|
|
337
|
+
type: 'select',
|
|
338
|
+
selection: createSelection(anchorOffset, focusOffset + 1)
|
|
339
|
+
});
|
|
340
|
+
}
|
|
196
341
|
}
|
|
197
|
-
}
|
|
342
|
+
})(e.nativeEvent);
|
|
198
343
|
}];
|
|
199
344
|
};
|
|
200
345
|
|
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import type { XStarMdEditorHandle, XStarMdEditorProps } from '../x-star-md-editor';
|
|
3
3
|
import type { XStarMdViewerHandle, XStarMdViewerProps } from '../x-star-md-viewer';
|
|
4
|
-
export interface XStarEditorHandle {
|
|
5
|
-
getEditor: () => XStarMdEditorHandle | null;
|
|
6
|
-
getViewer: () => XStarMdViewerHandle | null;
|
|
4
|
+
export interface XStarEditorHandle extends XStarMdEditorHandle, XStarMdViewerHandle {
|
|
7
5
|
}
|
|
8
6
|
export interface XStarEditorProps {
|
|
7
|
+
/**
|
|
8
|
+
* CSS 类名
|
|
9
|
+
*/
|
|
10
|
+
className?: string;
|
|
11
|
+
/**
|
|
12
|
+
* CSS 样式
|
|
13
|
+
*/
|
|
14
|
+
style?: React.CSSProperties;
|
|
9
15
|
/**
|
|
10
16
|
* 编辑器和浏览器的高度(不包括工具栏)
|
|
11
17
|
*/
|
|
@@ -7,7 +7,9 @@ import { prefix } from "../utils/global";
|
|
|
7
7
|
import XStarMdEditor, { useMdEditorRef } from "../x-star-md-editor";
|
|
8
8
|
import XStarMdViewer, { useMdViewerRef } from "../x-star-md-viewer";
|
|
9
9
|
var XStarEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
10
|
-
var
|
|
10
|
+
var className = _ref.className,
|
|
11
|
+
style = _ref.style,
|
|
12
|
+
height = _ref.height,
|
|
11
13
|
locale = _ref.locale,
|
|
12
14
|
initialValue = _ref.initialValue,
|
|
13
15
|
enableWebWorker = _ref.enableWebWorker,
|
|
@@ -22,21 +24,14 @@ var XStarEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
|
22
24
|
var editorRef = useMdEditorRef();
|
|
23
25
|
var viewerRef = useMdViewerRef();
|
|
24
26
|
useImperativeHandle(ref, function () {
|
|
25
|
-
return {
|
|
26
|
-
getEditor: function getEditor() {
|
|
27
|
-
return editorRef.current;
|
|
28
|
-
},
|
|
29
|
-
getViewer: function getViewer() {
|
|
30
|
-
return viewerRef.current;
|
|
31
|
-
}
|
|
32
|
-
};
|
|
27
|
+
return _objectSpread(_objectSpread({}, editorRef.current), viewerRef.current);
|
|
33
28
|
}, []);
|
|
34
29
|
|
|
35
30
|
// 同步滚动
|
|
36
31
|
useEffect(function () {
|
|
37
32
|
var _editorRef$current, _viewerRef$current;
|
|
38
|
-
var editor = (_editorRef$current = editorRef.current) === null || _editorRef$current === void 0 ? void 0 : _editorRef$current.
|
|
39
|
-
var viewer = (_viewerRef$current = viewerRef.current) === null || _viewerRef$current === void 0 ? void 0 : _viewerRef$current.
|
|
33
|
+
var editor = (_editorRef$current = editorRef.current) === null || _editorRef$current === void 0 ? void 0 : _editorRef$current.getEditorContainer();
|
|
34
|
+
var viewer = (_viewerRef$current = viewerRef.current) === null || _viewerRef$current === void 0 ? void 0 : _viewerRef$current.getViewerContainer();
|
|
40
35
|
|
|
41
36
|
/**
|
|
42
37
|
* 获取两个父元素的子元素到各自父元素顶部的距离
|
|
@@ -135,13 +130,12 @@ var XStarEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
|
135
130
|
};
|
|
136
131
|
}, []);
|
|
137
132
|
return /*#__PURE__*/React.createElement("div", {
|
|
138
|
-
className: classNames("".concat(prefix, "editor"))
|
|
133
|
+
className: classNames("".concat(prefix, "editor"), className),
|
|
134
|
+
style: style
|
|
139
135
|
}, /*#__PURE__*/React.createElement(XStarMdEditor, _extends({
|
|
140
136
|
ref: editorRef
|
|
141
137
|
}, editorProps, {
|
|
142
|
-
|
|
143
|
-
height: height
|
|
144
|
-
}),
|
|
138
|
+
height: height,
|
|
145
139
|
locale: locale,
|
|
146
140
|
initialValue: initialValue,
|
|
147
141
|
onChange: function onChange(value) {
|
|
@@ -152,9 +146,7 @@ var XStarEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
|
152
146
|
})), /*#__PURE__*/React.createElement(XStarMdViewer, _extends({
|
|
153
147
|
ref: viewerRef
|
|
154
148
|
}, viewerProps, {
|
|
155
|
-
|
|
156
|
-
height: height
|
|
157
|
-
}),
|
|
149
|
+
height: height,
|
|
158
150
|
value: value,
|
|
159
151
|
enableWebWorker: enableWebWorker
|
|
160
152
|
})));
|
|
@@ -20,7 +20,7 @@ export interface XStarMdEditorPlugin {
|
|
|
20
20
|
}
|
|
21
21
|
export interface XStarMdEditorHandle {
|
|
22
22
|
exec: Executor;
|
|
23
|
-
|
|
23
|
+
getEditorContainer: () => HTMLDivElement;
|
|
24
24
|
}
|
|
25
25
|
export interface XStarMdEditorProps {
|
|
26
26
|
/**
|
|
@@ -31,6 +31,10 @@ export interface XStarMdEditorProps {
|
|
|
31
31
|
* CSS 样式
|
|
32
32
|
*/
|
|
33
33
|
style?: React.CSSProperties;
|
|
34
|
+
/**
|
|
35
|
+
* 编辑器的高度(不包括工具栏)
|
|
36
|
+
*/
|
|
37
|
+
height?: React.CSSProperties['height'];
|
|
34
38
|
/**
|
|
35
39
|
* 工具栏 CSS 类名
|
|
36
40
|
*/
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
1
2
|
import classNames from 'classnames';
|
|
2
3
|
import React, { useEffect, useImperativeHandle, useMemo, useRef } from 'react';
|
|
3
4
|
import Toolbar, { getDefaultToolbarItemMap, getDefaultToolbarItems } from "../components/Toolbar";
|
|
@@ -9,6 +10,7 @@ import { useHistory } from "../utils/history";
|
|
|
9
10
|
var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
10
11
|
var className = _ref.className,
|
|
11
12
|
style = _ref.style,
|
|
13
|
+
height = _ref.height,
|
|
12
14
|
toolbarClassName = _ref.toolbarClassName,
|
|
13
15
|
toolbarStyle = _ref.toolbarStyle,
|
|
14
16
|
locale = _ref.locale,
|
|
@@ -75,7 +77,7 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
|
75
77
|
useImperativeHandle(ref, function () {
|
|
76
78
|
return {
|
|
77
79
|
exec: exec,
|
|
78
|
-
|
|
80
|
+
getEditorContainer: function getEditorContainer() {
|
|
79
81
|
return container.ref.current;
|
|
80
82
|
}
|
|
81
83
|
};
|
|
@@ -128,29 +130,55 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
|
128
130
|
* 开始组合时的选区
|
|
129
131
|
*/
|
|
130
132
|
var compositionStartSelection = useRef();
|
|
131
|
-
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* 容器同步控制器
|
|
136
|
+
*/
|
|
137
|
+
var syncController = useMemo(function () {
|
|
138
|
+
var timer = 0;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 将最新的文本和选区同步到容器
|
|
142
|
+
*/
|
|
143
|
+
var sync = function sync() {
|
|
144
|
+
if (compositionStartSelection.current) {
|
|
145
|
+
var _selection = getSelection();
|
|
146
|
+
container.setText(sourceCodeLatest.current);
|
|
147
|
+
container.setSelection(_selection);
|
|
148
|
+
compositionStartSelection.current = undefined;
|
|
149
|
+
}
|
|
150
|
+
timer = 0;
|
|
151
|
+
};
|
|
152
|
+
return {
|
|
153
|
+
/**
|
|
154
|
+
* 在下个事件循环中同步
|
|
155
|
+
*/
|
|
156
|
+
start: function start() {
|
|
157
|
+
if (!timer) {
|
|
158
|
+
timer = window.setTimeout(sync);
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
/**
|
|
162
|
+
* 立即执行下个事件循环中的同步
|
|
163
|
+
*/
|
|
164
|
+
flush: function flush() {
|
|
165
|
+
if (timer) {
|
|
166
|
+
window.clearTimeout(timer);
|
|
167
|
+
sync();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
}, []);
|
|
132
172
|
var compositionEventHandler = function compositionEventHandler(e) {
|
|
133
173
|
switch (e.type) {
|
|
134
174
|
case 'compositionend':
|
|
135
175
|
{
|
|
136
|
-
|
|
137
|
-
dispatch({
|
|
138
|
-
type: 'insert',
|
|
139
|
-
payload: {
|
|
140
|
-
text: e.data
|
|
141
|
-
},
|
|
142
|
-
selection: compositionStartSelection.current
|
|
143
|
-
});
|
|
144
|
-
// 确保组合事件屏蔽表单和键盘事件
|
|
145
|
-
compositionEndTimer.current = window.setTimeout(function () {
|
|
146
|
-
return compositionStartSelection.current = undefined;
|
|
147
|
-
});
|
|
148
|
-
}
|
|
176
|
+
syncController.start();
|
|
149
177
|
break;
|
|
150
178
|
}
|
|
151
179
|
case 'compositionstart':
|
|
152
180
|
{
|
|
153
|
-
|
|
181
|
+
syncController.flush();
|
|
154
182
|
compositionStartSelection.current = getSelection();
|
|
155
183
|
break;
|
|
156
184
|
}
|
|
@@ -160,7 +188,7 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
|
160
188
|
switch (e.type) {
|
|
161
189
|
case 'drop':
|
|
162
190
|
{
|
|
163
|
-
var
|
|
191
|
+
var _selection2 = getSelection();
|
|
164
192
|
window.setTimeout(function () {
|
|
165
193
|
container.normalize();
|
|
166
194
|
dispatch({
|
|
@@ -169,7 +197,7 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
|
169
197
|
sourceCode: container.getText(),
|
|
170
198
|
selection: getSelection()
|
|
171
199
|
},
|
|
172
|
-
selection:
|
|
200
|
+
selection: _selection2
|
|
173
201
|
});
|
|
174
202
|
});
|
|
175
203
|
break;
|
|
@@ -181,58 +209,60 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
|
181
209
|
case 'blur':
|
|
182
210
|
{
|
|
183
211
|
blurSelection.current = getSelection();
|
|
184
|
-
window.setTimeout(function () {
|
|
185
|
-
compositionStartSelection.current = undefined;
|
|
186
|
-
container.setText(sourceCodeLatest.current);
|
|
187
|
-
});
|
|
188
212
|
break;
|
|
189
213
|
}
|
|
190
214
|
}
|
|
191
215
|
};
|
|
192
216
|
var formEventHandler = function formEventHandler(e) {
|
|
193
|
-
if (compositionStartSelection.current) {
|
|
194
|
-
e.preventDefault();
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
217
|
switch (e.type) {
|
|
198
218
|
case 'beforeinput':
|
|
199
219
|
{
|
|
200
220
|
e.preventDefault();
|
|
201
221
|
if ('data' in e && typeof e.data === 'string') {
|
|
222
|
+
var _compositionStartSele;
|
|
202
223
|
dispatch({
|
|
203
224
|
type: 'insert',
|
|
204
225
|
payload: {
|
|
205
226
|
text: e.data
|
|
206
227
|
},
|
|
207
|
-
selection: getSelection()
|
|
228
|
+
selection: (_compositionStartSele = compositionStartSelection.current) !== null && _compositionStartSele !== void 0 ? _compositionStartSele : getSelection()
|
|
208
229
|
});
|
|
230
|
+
compositionStartSelection.current = undefined;
|
|
209
231
|
}
|
|
210
232
|
break;
|
|
211
233
|
}
|
|
212
234
|
}
|
|
213
235
|
};
|
|
236
|
+
var onInsertFileLatest = useRef(onInsertFile);
|
|
237
|
+
onInsertFileLatest.current = onInsertFile;
|
|
214
238
|
var options = useMemo(function () {
|
|
215
239
|
return composeHandlers(plugins)({
|
|
216
|
-
toolbarItemMap: getDefaultToolbarItemMap(locale,
|
|
240
|
+
toolbarItemMap: getDefaultToolbarItemMap(locale, function () {
|
|
241
|
+
var _onInsertFileLatest$c;
|
|
242
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
243
|
+
args[_key] = arguments[_key];
|
|
244
|
+
}
|
|
245
|
+
return (_onInsertFileLatest$c = onInsertFileLatest.current) === null || _onInsertFileLatest$c === void 0 ? void 0 : _onInsertFileLatest$c.call.apply(_onInsertFileLatest$c, [onInsertFileLatest].concat(args));
|
|
246
|
+
}),
|
|
217
247
|
toolbarItems: getDefaultToolbarItems(),
|
|
218
248
|
keyboardEventHandlers: getDefaultKeyboardEventHandlers()
|
|
219
249
|
});
|
|
220
|
-
}, [plugins]);
|
|
250
|
+
}, [locale, plugins]);
|
|
221
251
|
var keyboardEventHandler = function keyboardEventHandler(e) {
|
|
222
|
-
if (compositionStartSelection.current) {
|
|
223
|
-
e.preventDefault();
|
|
224
|
-
return;
|
|
225
|
-
}
|
|
226
252
|
switch (e.type) {
|
|
227
253
|
case 'keydown':
|
|
228
254
|
{
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
255
|
+
if (compositionStartSelection.current) {
|
|
256
|
+
e.preventDefault();
|
|
257
|
+
} else {
|
|
258
|
+
composeHandlers(options.keyboardEventHandlers)({
|
|
259
|
+
history: history,
|
|
260
|
+
sourceCode: sourceCode,
|
|
261
|
+
selection: getSelection(),
|
|
262
|
+
dispatch: dispatch,
|
|
263
|
+
e: e
|
|
264
|
+
});
|
|
265
|
+
}
|
|
236
266
|
break;
|
|
237
267
|
}
|
|
238
268
|
}
|
|
@@ -248,7 +278,9 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
|
248
278
|
}), /*#__PURE__*/React.createElement("div", {
|
|
249
279
|
ref: container.ref,
|
|
250
280
|
className: classNames("".concat(prefix, "md-editor"), className),
|
|
251
|
-
style: style,
|
|
281
|
+
style: _objectSpread(_objectSpread({}, style), {}, {
|
|
282
|
+
height: height
|
|
283
|
+
}),
|
|
252
284
|
contentEditable: true,
|
|
253
285
|
onBeforeInput: formEventHandler,
|
|
254
286
|
onBlur: focusEventHandler,
|
|
@@ -21,7 +21,7 @@ export interface XStarMdViewerPlugin {
|
|
|
21
21
|
(ctx: ViewerOptions): void;
|
|
22
22
|
}
|
|
23
23
|
export interface XStarMdViewerHandle {
|
|
24
|
-
|
|
24
|
+
getViewerContainer: () => HTMLDivElement;
|
|
25
25
|
}
|
|
26
26
|
export interface XStarMdViewerProps {
|
|
27
27
|
/**
|
|
@@ -32,6 +32,10 @@ export interface XStarMdViewerProps {
|
|
|
32
32
|
* CSS 样式
|
|
33
33
|
*/
|
|
34
34
|
style?: React.CSSProperties;
|
|
35
|
+
/**
|
|
36
|
+
* 浏览器的高度
|
|
37
|
+
*/
|
|
38
|
+
height?: React.CSSProperties['height'];
|
|
35
39
|
/**
|
|
36
40
|
* 文本
|
|
37
41
|
*/
|