x-star-editor 0.1.2 → 0.2.1
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/container.js +18 -32
- package/dist/utils/handler.js +229 -84
- package/dist/x-star-editor/index.d.ts +11 -5
- package/dist/x-star-editor/index.js +10 -18
- package/dist/x-star-md-editor/index.d.ts +6 -2
- 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 +5 -4
- package/workers/markdown.worker.js +0 -1
- /package/dist/{utils → types}/jsx-runtime.d.ts +0 -0
|
@@ -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
|
*/
|