x-star-editor 0.1.1 → 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/components/Fade.js +2 -7
- package/dist/components/FileInput.js +2 -10
- package/dist/components/Toolbar.js +17 -24
- package/dist/locales/index.js +6 -5
- package/dist/styles/index.css +52 -45
- package/dist/types/worker.js.d.ts +3 -0
- package/dist/utils/container.js +1 -3
- package/dist/utils/handler.js +230 -87
- package/dist/utils/history.js +3 -16
- package/dist/utils/markdown.d.ts +31 -29
- package/dist/utils/markdown.js +106 -62
- package/dist/x-star-editor/index.d.ts +21 -7
- package/dist/x-star-editor/index.js +20 -34
- package/dist/x-star-md-editor/index.d.ts +5 -1
- package/dist/x-star-md-editor/index.js +82 -53
- package/dist/x-star-md-viewer/index.d.ts +27 -2
- package/dist/x-star-md-viewer/index.js +52 -10
- package/package.json +6 -2
- package/workers-dist/markdown.worker.js +1 -0
- /package/dist/{utils → types}/jsx-runtime.d.ts +0 -0
package/dist/utils/markdown.js
CHANGED
|
@@ -1,16 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
6
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
7
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
8
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
9
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
10
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
11
|
-
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
12
|
-
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
13
|
-
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
1
|
+
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
2
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
3
|
+
import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
|
|
4
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
14
5
|
import { toJsxRuntime } from 'hast-util-to-jsx-runtime';
|
|
15
6
|
import { Fragment, jsx, jsxs } from 'react/jsx-runtime';
|
|
16
7
|
import rehypeKatex from 'rehype-katex';
|
|
@@ -28,44 +19,29 @@ import remarkStringify from 'remark-stringify';
|
|
|
28
19
|
import strip from 'strip-markdown';
|
|
29
20
|
import { unified } from 'unified';
|
|
30
21
|
import { prefix } from "./global";
|
|
31
|
-
export var getDefaultSchema = function getDefaultSchema() {
|
|
32
|
-
var _defaultSchema$attrib, _defaultSchema$attrib2, _defaultSchema$attrib3, _defaultSchema$attrib4, _defaultSchema$attrib5, _defaultSchema$attrib6, _defaultSchema$attrib7, _defaultSchema$attrib8, _defaultSchema$protoc, _defaultSchema$protoc2, _defaultSchema$tagNam;
|
|
33
|
-
return _objectSpread(_objectSpread({}, defaultSchema), {}, {
|
|
34
|
-
attributes: _objectSpread(_objectSpread({}, defaultSchema.attributes), {}, {
|
|
35
|
-
pre: [].concat(_toConsumableArray((_defaultSchema$attrib = (_defaultSchema$attrib2 = defaultSchema.attributes) === null || _defaultSchema$attrib2 === void 0 ? void 0 : _defaultSchema$attrib2.pre) !== null && _defaultSchema$attrib !== void 0 ? _defaultSchema$attrib : []), ['className', 'style']),
|
|
36
|
-
code: [].concat(_toConsumableArray((_defaultSchema$attrib3 = (_defaultSchema$attrib4 = defaultSchema.attributes) === null || _defaultSchema$attrib4 === void 0 ? void 0 : _defaultSchema$attrib4.code) !== null && _defaultSchema$attrib3 !== void 0 ? _defaultSchema$attrib3 : []), ['className', 'style']),
|
|
37
|
-
div: [].concat(_toConsumableArray((_defaultSchema$attrib5 = (_defaultSchema$attrib6 = defaultSchema.attributes) === null || _defaultSchema$attrib6 === void 0 ? void 0 : _defaultSchema$attrib6.div) !== null && _defaultSchema$attrib5 !== void 0 ? _defaultSchema$attrib5 : []), ['className', 'style']),
|
|
38
|
-
span: [].concat(_toConsumableArray((_defaultSchema$attrib7 = (_defaultSchema$attrib8 = defaultSchema.attributes) === null || _defaultSchema$attrib8 === void 0 ? void 0 : _defaultSchema$attrib8.span) !== null && _defaultSchema$attrib7 !== void 0 ? _defaultSchema$attrib7 : []), ['className', 'style', 'line']),
|
|
39
|
-
svg: ['xmlns', 'width', 'height', 'viewBox', 'preserveAspectRatio'],
|
|
40
|
-
path: ['d'],
|
|
41
|
-
custom: ['meta', 'value']
|
|
42
|
-
}),
|
|
43
|
-
protocols: _objectSpread(_objectSpread({}, defaultSchema.protocols), {}, {
|
|
44
|
-
src: [].concat(_toConsumableArray((_defaultSchema$protoc = (_defaultSchema$protoc2 = defaultSchema.protocols) === null || _defaultSchema$protoc2 === void 0 ? void 0 : _defaultSchema$protoc2.src) !== null && _defaultSchema$protoc !== void 0 ? _defaultSchema$protoc : []), ['data'])
|
|
45
|
-
}),
|
|
46
|
-
tagNames: [].concat(_toConsumableArray((_defaultSchema$tagNam = defaultSchema.tagNames) !== null && _defaultSchema$tagNam !== void 0 ? _defaultSchema$tagNam : []), ['svg', 'path', 'custom'])
|
|
47
|
-
});
|
|
48
|
-
};
|
|
49
22
|
|
|
50
23
|
/**
|
|
51
|
-
* 将 Markdown 文本解析为 Mdast
|
|
24
|
+
* 将 Markdown 文本解析为 Mdast 树的处理器
|
|
52
25
|
*/
|
|
53
|
-
var
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
26
|
+
var toMdastProcessor = unified().use(remarkParse).use(remarkGfm).use(remarkMath).freeze();
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Mdast 根节点
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 带缓存的解析函数
|
|
34
|
+
*
|
|
35
|
+
* @param sourceCode Markdown 文本
|
|
36
|
+
* @returns Mdast 树
|
|
37
|
+
*/
|
|
38
|
+
var cachedParse = function () {
|
|
39
|
+
var cache = {};
|
|
40
|
+
return function (sourceCode) {
|
|
41
|
+
var _cache$sourceCode;
|
|
42
|
+
return (_cache$sourceCode = cache[sourceCode]) !== null && _cache$sourceCode !== void 0 ? _cache$sourceCode : (cache = _defineProperty({}, sourceCode, toMdastProcessor.parse(sourceCode)))[sourceCode];
|
|
43
|
+
};
|
|
44
|
+
}();
|
|
69
45
|
|
|
70
46
|
/**
|
|
71
47
|
* Mdast 节点
|
|
@@ -125,7 +101,7 @@ export var editorRender = function editorRender(sourceCode) {
|
|
|
125
101
|
var styleElement = stack[index && !isFlowContent(stack[index - 1].node) ? index : 0].el;
|
|
126
102
|
styleElement.classList.add("".concat(mdPrefix).concat(node.type));
|
|
127
103
|
if (node.type === 'heading') {
|
|
128
|
-
styleElement.classList.add("".concat(mdPrefix, "heading").concat(node.depth));
|
|
104
|
+
styleElement.classList.add("".concat(mdPrefix, "heading-").concat(node.depth));
|
|
129
105
|
}
|
|
130
106
|
|
|
131
107
|
// 如果索引为 0,则父节点为块级元素,否则为前一个索引的 DOM 元素
|
|
@@ -267,7 +243,7 @@ export var editorRender = function editorRender(sourceCode) {
|
|
|
267
243
|
}
|
|
268
244
|
addText(sourceCode.slice(scanOffset, parentEndOffset), true);
|
|
269
245
|
};
|
|
270
|
-
getChildren(
|
|
246
|
+
getChildren(cachedParse(sourceCode).children, sourceCode.length);
|
|
271
247
|
if (scanOffset <= sourceCode.length) {
|
|
272
248
|
// 添加最后一行文本
|
|
273
249
|
block.append(createLine(sourceCode.slice(scanOffset)));
|
|
@@ -275,15 +251,73 @@ export var editorRender = function editorRender(sourceCode) {
|
|
|
275
251
|
}
|
|
276
252
|
return el;
|
|
277
253
|
};
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* 将 Mdast 树转成 Hast 树的处理器
|
|
257
|
+
*/
|
|
258
|
+
var toHastProcessor = unified().use(remarkBreaks).use(remarkRehype, {
|
|
259
|
+
allowDangerousHtml: true,
|
|
260
|
+
handlers: {
|
|
261
|
+
math: function math(h, node) {
|
|
262
|
+
return (
|
|
263
|
+
// 如果 Math 节点的 `meta` 属性不为空,则视为自定义块
|
|
264
|
+
node.meta ? h(node.position, 'custom', {
|
|
265
|
+
meta: node.meta,
|
|
266
|
+
value: node.value
|
|
267
|
+
}) : h(node, 'div')
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}).use(rehypePrism, {
|
|
272
|
+
ignoreMissing: true
|
|
273
|
+
}).use(rehypeKatex).freeze();
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Hast 根节点
|
|
277
|
+
*/
|
|
278
|
+
|
|
278
279
|
/**
|
|
279
|
-
* 将 Markdown
|
|
280
|
+
* 将 Markdown 文本转成 Hast 树
|
|
280
281
|
*
|
|
281
282
|
* @param sourceCode Markdown 文本
|
|
282
|
-
* @
|
|
283
|
-
|
|
283
|
+
* @returns Hast 树
|
|
284
|
+
*/
|
|
285
|
+
export var preViewerRender = function preViewerRender(sourceCode) {
|
|
286
|
+
return toHastProcessor.runSync(cachedParse(sourceCode));
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* 过滤模式
|
|
291
|
+
*/
|
|
292
|
+
|
|
293
|
+
export var getDefaultSchema = function getDefaultSchema() {
|
|
294
|
+
var _defaultSchema$attrib, _defaultSchema$attrib2, _defaultSchema$attrib3, _defaultSchema$attrib4, _defaultSchema$attrib5, _defaultSchema$attrib6, _defaultSchema$attrib7, _defaultSchema$attrib8, _defaultSchema$protoc, _defaultSchema$protoc2, _defaultSchema$tagNam;
|
|
295
|
+
return _objectSpread(_objectSpread({}, defaultSchema), {}, {
|
|
296
|
+
attributes: _objectSpread(_objectSpread({}, defaultSchema.attributes), {}, {
|
|
297
|
+
pre: [].concat(_toConsumableArray((_defaultSchema$attrib = (_defaultSchema$attrib2 = defaultSchema.attributes) === null || _defaultSchema$attrib2 === void 0 ? void 0 : _defaultSchema$attrib2.pre) !== null && _defaultSchema$attrib !== void 0 ? _defaultSchema$attrib : []), ['className', 'style']),
|
|
298
|
+
code: [].concat(_toConsumableArray((_defaultSchema$attrib3 = (_defaultSchema$attrib4 = defaultSchema.attributes) === null || _defaultSchema$attrib4 === void 0 ? void 0 : _defaultSchema$attrib4.code) !== null && _defaultSchema$attrib3 !== void 0 ? _defaultSchema$attrib3 : []), ['className', 'style']),
|
|
299
|
+
div: [].concat(_toConsumableArray((_defaultSchema$attrib5 = (_defaultSchema$attrib6 = defaultSchema.attributes) === null || _defaultSchema$attrib6 === void 0 ? void 0 : _defaultSchema$attrib6.div) !== null && _defaultSchema$attrib5 !== void 0 ? _defaultSchema$attrib5 : []), ['className', 'style']),
|
|
300
|
+
span: [].concat(_toConsumableArray((_defaultSchema$attrib7 = (_defaultSchema$attrib8 = defaultSchema.attributes) === null || _defaultSchema$attrib8 === void 0 ? void 0 : _defaultSchema$attrib8.span) !== null && _defaultSchema$attrib7 !== void 0 ? _defaultSchema$attrib7 : []), ['className', 'style', 'line']),
|
|
301
|
+
svg: ['xmlns', 'viewBox', 'preserveAspectRatio'],
|
|
302
|
+
path: ['d'],
|
|
303
|
+
custom: ['meta', 'value']
|
|
304
|
+
}),
|
|
305
|
+
protocols: _objectSpread(_objectSpread({}, defaultSchema.protocols), {}, {
|
|
306
|
+
src: [].concat(_toConsumableArray((_defaultSchema$protoc = (_defaultSchema$protoc2 = defaultSchema.protocols) === null || _defaultSchema$protoc2 === void 0 ? void 0 : _defaultSchema$protoc2.src) !== null && _defaultSchema$protoc !== void 0 ? _defaultSchema$protoc : []), ['data'])
|
|
307
|
+
}),
|
|
308
|
+
tagNames: [].concat(_toConsumableArray((_defaultSchema$tagNam = defaultSchema.tagNames) !== null && _defaultSchema$tagNam !== void 0 ? _defaultSchema$tagNam : []), ['svg', 'path', 'custom'])
|
|
309
|
+
});
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* 将 Hast 树转成新的 Hast 树
|
|
314
|
+
*
|
|
315
|
+
* @param root Hast 树
|
|
316
|
+
* @param schema 过滤模式
|
|
317
|
+
* @returns 新的 Hast 树
|
|
284
318
|
*/
|
|
285
|
-
export var viewerRender = function viewerRender(
|
|
286
|
-
return
|
|
319
|
+
export var viewerRender = function viewerRender(root, schema) {
|
|
320
|
+
return unified().use(rehypeRaw).use(rehypeSanitize, schema).use(function () {
|
|
287
321
|
return function (root) {
|
|
288
322
|
var _iterator2 = _createForOfIteratorHelper(root.children),
|
|
289
323
|
_step2;
|
|
@@ -301,7 +335,18 @@ export var viewerRender = function viewerRender(sourceCode, options) {
|
|
|
301
335
|
_iterator2.f();
|
|
302
336
|
}
|
|
303
337
|
};
|
|
304
|
-
}).runSync(
|
|
338
|
+
}).runSync(root);
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* 将 Hast 树映射到 React 虚拟 DOM 树
|
|
343
|
+
*
|
|
344
|
+
* @param root Hast 树
|
|
345
|
+
* @param options 配置项
|
|
346
|
+
* @returns React 虚拟 DOM 树
|
|
347
|
+
*/
|
|
348
|
+
export var postViewerRender = function postViewerRender(root, options) {
|
|
349
|
+
return toJsxRuntime(root, {
|
|
305
350
|
Fragment: Fragment,
|
|
306
351
|
jsx: jsx,
|
|
307
352
|
jsxs: jsxs,
|
|
@@ -317,10 +362,9 @@ export var viewerRender = function viewerRender(sourceCode, options) {
|
|
|
317
362
|
})
|
|
318
363
|
});
|
|
319
364
|
};
|
|
320
|
-
var
|
|
365
|
+
var toHTMLProcessor = unified().use(remarkParse).use(remarkGfm).use(remarkBreaks).use(remarkRehype, {
|
|
321
366
|
allowDangerousHtml: true
|
|
322
|
-
}).use(rehypeRaw).freeze();
|
|
323
|
-
var toHTMLProcessor = unified().use(rehypeStringify).freeze();
|
|
367
|
+
}).use(rehypeRaw).use(rehypeStringify).freeze();
|
|
324
368
|
|
|
325
369
|
/**
|
|
326
370
|
* 将 Markdown 文本转成 HTML 文本
|
|
@@ -329,9 +373,9 @@ var toHTMLProcessor = unified().use(rehypeStringify).freeze();
|
|
|
329
373
|
* @returns HTML 文本
|
|
330
374
|
*/
|
|
331
375
|
export var toHTML = function toHTML(sourceCode) {
|
|
332
|
-
return toHTMLProcessor.
|
|
376
|
+
return toHTMLProcessor.processSync(sourceCode).toString();
|
|
333
377
|
};
|
|
334
|
-
var toMarkdownProcessor = unified().use(rehypeRemark).use(remarkGfm).use(
|
|
378
|
+
var toMarkdownProcessor = unified().use(rehypeRemark).use(remarkGfm).use(remarkStringify).freeze();
|
|
335
379
|
|
|
336
380
|
/**
|
|
337
381
|
* 将带有 HTML 的 Markdown 文本转成 Markdown 文本
|
|
@@ -340,7 +384,7 @@ var toMarkdownProcessor = unified().use(rehypeRemark).use(remarkGfm).use(remarkB
|
|
|
340
384
|
* @returns Markdown 文本
|
|
341
385
|
*/
|
|
342
386
|
export var toMarkdown = function toMarkdown(sourceCode) {
|
|
343
|
-
return toMarkdownProcessor.stringify(toMarkdownProcessor.runSync(
|
|
387
|
+
return toMarkdownProcessor.stringify(toMarkdownProcessor.runSync(toHTMLProcessor.runSync(toHTMLProcessor.parse(sourceCode))));
|
|
344
388
|
};
|
|
345
389
|
var toTextProcessor = unified().use(strip).freeze();
|
|
346
390
|
|
|
@@ -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
|
*/
|
|
@@ -13,23 +19,31 @@ export interface XStarEditorProps {
|
|
|
13
19
|
/**
|
|
14
20
|
* 语言
|
|
15
21
|
*/
|
|
16
|
-
locale?:
|
|
22
|
+
locale?: XStarMdEditorProps['locale'];
|
|
17
23
|
/**
|
|
18
24
|
* 初始文本
|
|
19
25
|
*/
|
|
20
26
|
initialValue?: string;
|
|
27
|
+
/**
|
|
28
|
+
* 是否启用 Web Worker
|
|
29
|
+
*/
|
|
30
|
+
enableWebWorker?: XStarMdViewerProps['enableWebWorker'];
|
|
21
31
|
/**
|
|
22
32
|
* 编辑器属性
|
|
23
33
|
*/
|
|
24
|
-
editorProps?: Omit<XStarMdEditorProps, 'locale' | 'initialValue' | 'onChange'>;
|
|
34
|
+
editorProps?: Omit<XStarMdEditorProps, 'locale' | 'initialValue' | 'onChange' | 'onInsertFile'>;
|
|
25
35
|
/**
|
|
26
36
|
* 浏览器属性
|
|
27
37
|
*/
|
|
28
|
-
viewerProps?: Omit<XStarMdViewerProps, 'value'>;
|
|
38
|
+
viewerProps?: Omit<XStarMdViewerProps, 'value' | 'enableWebWorker'>;
|
|
29
39
|
/**
|
|
30
40
|
* 文本变化回调函数
|
|
31
41
|
*/
|
|
32
|
-
onChange?:
|
|
42
|
+
onChange?: XStarMdEditorProps['onChange'];
|
|
43
|
+
/**
|
|
44
|
+
* 文件插入回调函数
|
|
45
|
+
*/
|
|
46
|
+
onInsertFile?: XStarMdEditorProps['onInsertFile'];
|
|
33
47
|
}
|
|
34
48
|
declare const XStarEditor: React.ForwardRefExoticComponent<XStarEditorProps & React.RefAttributes<XStarEditorHandle>>;
|
|
35
49
|
export default XStarEditor;
|
|
@@ -1,28 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
5
|
-
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
6
|
-
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
7
|
-
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
8
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
9
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
10
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
11
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
12
|
-
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
|
|
13
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
1
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
3
|
+
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
14
4
|
import classNames from 'classnames';
|
|
15
5
|
import React, { useEffect, useImperativeHandle, useRef, useState } from 'react';
|
|
16
6
|
import { prefix } from "../utils/global";
|
|
17
7
|
import XStarMdEditor, { useMdEditorRef } from "../x-star-md-editor";
|
|
18
8
|
import XStarMdViewer, { useMdViewerRef } from "../x-star-md-viewer";
|
|
19
9
|
var XStarEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
20
|
-
var
|
|
10
|
+
var className = _ref.className,
|
|
11
|
+
style = _ref.style,
|
|
12
|
+
height = _ref.height,
|
|
21
13
|
locale = _ref.locale,
|
|
22
14
|
initialValue = _ref.initialValue,
|
|
15
|
+
enableWebWorker = _ref.enableWebWorker,
|
|
23
16
|
editorProps = _ref.editorProps,
|
|
24
17
|
viewerProps = _ref.viewerProps,
|
|
25
|
-
_onChange = _ref.onChange
|
|
18
|
+
_onChange = _ref.onChange,
|
|
19
|
+
onInsertFile = _ref.onInsertFile;
|
|
26
20
|
var _useState = useState(initialValue),
|
|
27
21
|
_useState2 = _slicedToArray(_useState, 2),
|
|
28
22
|
value = _useState2[0],
|
|
@@ -30,21 +24,14 @@ var XStarEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
|
30
24
|
var editorRef = useMdEditorRef();
|
|
31
25
|
var viewerRef = useMdViewerRef();
|
|
32
26
|
useImperativeHandle(ref, function () {
|
|
33
|
-
return {
|
|
34
|
-
getEditor: function getEditor() {
|
|
35
|
-
return editorRef.current;
|
|
36
|
-
},
|
|
37
|
-
getViewer: function getViewer() {
|
|
38
|
-
return viewerRef.current;
|
|
39
|
-
}
|
|
40
|
-
};
|
|
27
|
+
return _objectSpread(_objectSpread({}, editorRef.current), viewerRef.current);
|
|
41
28
|
}, []);
|
|
42
29
|
|
|
43
30
|
// 同步滚动
|
|
44
31
|
useEffect(function () {
|
|
45
32
|
var _editorRef$current, _viewerRef$current;
|
|
46
|
-
var editor = (_editorRef$current = editorRef.current) === null || _editorRef$current === void 0 ? void 0 : _editorRef$current.
|
|
47
|
-
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();
|
|
48
35
|
|
|
49
36
|
/**
|
|
50
37
|
* 获取两个父元素的子元素到各自父元素顶部的距离
|
|
@@ -143,26 +130,25 @@ var XStarEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
|
|
|
143
130
|
};
|
|
144
131
|
}, []);
|
|
145
132
|
return /*#__PURE__*/React.createElement("div", {
|
|
146
|
-
className: classNames("".concat(prefix, "editor"))
|
|
133
|
+
className: classNames("".concat(prefix, "editor"), className),
|
|
134
|
+
style: style
|
|
147
135
|
}, /*#__PURE__*/React.createElement(XStarMdEditor, _extends({
|
|
148
136
|
ref: editorRef
|
|
149
137
|
}, editorProps, {
|
|
150
|
-
|
|
151
|
-
height: height
|
|
152
|
-
}),
|
|
138
|
+
height: height,
|
|
153
139
|
locale: locale,
|
|
154
140
|
initialValue: initialValue,
|
|
155
141
|
onChange: function onChange(value) {
|
|
156
142
|
setValue(value);
|
|
157
143
|
_onChange === null || _onChange === void 0 ? void 0 : _onChange(value);
|
|
158
|
-
}
|
|
144
|
+
},
|
|
145
|
+
onInsertFile: onInsertFile
|
|
159
146
|
})), /*#__PURE__*/React.createElement(XStarMdViewer, _extends({
|
|
160
147
|
ref: viewerRef
|
|
161
148
|
}, viewerProps, {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
value: value
|
|
149
|
+
height: height,
|
|
150
|
+
value: value,
|
|
151
|
+
enableWebWorker: enableWebWorker
|
|
166
152
|
})));
|
|
167
153
|
});
|
|
168
154
|
if (process.env.NODE_ENV !== 'production') {
|
|
@@ -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
|
*/
|