x-star-editor 0.3.7 → 0.3.9

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.
@@ -1,4 +1,3 @@
1
- /// <reference types="react" />
2
1
  import type { Root as HastRoot } from 'hast';
3
2
  import { defaultSchema } from 'rehype-sanitize';
4
3
  import type { ViewerOptions } from '../x-star-md-viewer';
@@ -36,7 +35,7 @@ export declare const viewerRender: (root: HastRoot, schema: Schema) => HastRoot;
36
35
  * @param options 配置项
37
36
  * @returns React 虚拟 DOM 树
38
37
  */
39
- export declare const postViewerRender: (root: HastRoot, options: ViewerOptions) => JSX.Element;
38
+ export declare const postViewerRender: (root: HastRoot, options: ViewerOptions) => any;
40
39
  /**
41
40
  * 将 Markdown 文本转成 HTML 文本
42
41
  *
@@ -190,10 +190,10 @@ export var editorRender = function editorRender(sourceCode) {
190
190
  _step;
191
191
  try {
192
192
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
193
- var _node$position, _node$position2;
193
+ var _node$position, _node$position$start, _node$position2, _node$position2$end;
194
194
  var node = _step.value;
195
- var startOffset = (_node$position = node.position) === null || _node$position === void 0 ? void 0 : _node$position.start.offset;
196
- var endOffset = (_node$position2 = node.position) === null || _node$position2 === void 0 ? void 0 : _node$position2.end.offset;
195
+ var startOffset = (_node$position = node.position) === null || _node$position === void 0 ? void 0 : (_node$position$start = _node$position.start) === null || _node$position$start === void 0 ? void 0 : _node$position$start.offset;
196
+ var endOffset = (_node$position2 = node.position) === null || _node$position2 === void 0 ? void 0 : (_node$position2$end = _node$position2.end) === null || _node$position2$end === void 0 ? void 0 : _node$position2$end.offset;
197
197
  if (startOffset === undefined || endOffset === undefined) {
198
198
  continue;
199
199
  }
@@ -223,12 +223,12 @@ export var editorRender = function editorRender(sourceCode) {
223
223
  if (stack.length > 1) {
224
224
  pushElement(stack.length - 1);
225
225
  } else {
226
- var _node$position3;
226
+ var _node$position3, _node$position3$start;
227
227
  // 如果栈大小为 1,说明为块级元素终点
228
228
  stack[0].element.append("\u200B");
229
229
  pushElement(0);
230
230
  // 记录行号,用于同步滚动
231
- block.dataset.line = "".concat((_node$position3 = node.position) === null || _node$position3 === void 0 ? void 0 : _node$position3.start.line);
231
+ block.dataset.line = "".concat((_node$position3 = node.position) === null || _node$position3 === void 0 ? void 0 : (_node$position3$start = _node$position3.start) === null || _node$position3$start === void 0 ? void 0 : _node$position3$start.line);
232
232
  pushBlock();
233
233
  scanOffset++;
234
234
  }
@@ -341,23 +341,33 @@ export var viewerRender = function viewerRender(root, schema) {
341
341
  * @returns React 虚拟 DOM 树
342
342
  */
343
343
  export var postViewerRender = function postViewerRender(root, options) {
344
- var components = _objectSpread(_objectSpread({}, options.customHTMLElements), {}, {
345
- input: function input(props) {
346
- return jsx('input', props);
347
- },
348
- custom: function custom(props) {
349
- var _options$customBlocks;
350
- return jsx((_options$customBlocks = options.customBlocks[props.meta]) !== null && _options$customBlocks !== void 0 ? _options$customBlocks : 'div', {
351
- children: props.value
352
- });
353
- }
354
- });
355
- return toJsxRuntime(unified().use(rehypeKatex).runSync(root), {
356
- Fragment: Fragment,
357
- jsx: jsx,
358
- jsxs: jsxs,
359
- components: components
360
- });
344
+ try {
345
+ var components = _objectSpread(_objectSpread({}, options.customHTMLElements), {}, {
346
+ input: function input(props) {
347
+ return jsx('input', props);
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
+ }
355
+ });
356
+ return toJsxRuntime(unified().use(rehypeKatex).runSync(root), {
357
+ Fragment: Fragment,
358
+ jsx: jsx,
359
+ jsxs: jsxs,
360
+ components: components
361
+ });
362
+ } catch (_unused) {
363
+ return jsx('div', {
364
+ style: {
365
+ fontStyle: 'italic',
366
+ color: 'red'
367
+ },
368
+ children: 'Parse error!'
369
+ });
370
+ }
361
371
  };
362
372
  var toHTMLProcessor = unified().use(remarkParse).use(remarkGfm).use(remarkMath).use(remarkRehype, {
363
373
  allowDangerousHtml: true,
@@ -6,10 +6,10 @@ import { SKIP, visit } from 'unist-util-visit';
6
6
  var rehypeLine = function rehypeLine() {
7
7
  return function (root) {
8
8
  return visit(root, 'element', function (node) {
9
- var _node$properties, _node$position;
9
+ var _node$properties, _node$position, _node$position$start;
10
10
  (_node$properties = node.properties) !== null && _node$properties !== void 0 ? _node$properties : node.properties = {};
11
11
  // 记录行号,用于同步滚动
12
- node.properties['data-line'] = (_node$position = node.position) === null || _node$position === void 0 ? void 0 : _node$position.start.line;
12
+ node.properties['data-line'] = (_node$position = node.position) === null || _node$position === void 0 ? void 0 : (_node$position$start = _node$position.start) === null || _node$position$start === void 0 ? void 0 : _node$position$start.line;
13
13
  return SKIP;
14
14
  });
15
15
  };
@@ -156,8 +156,8 @@ var rehypeMath = function rehypeMath() {
156
156
  return SKIP;
157
157
  }
158
158
  if (node.type === 'text' && index !== null && parent) {
159
- var _node$position;
160
- var startOffset = (_node$position = node.position) === null || _node$position === void 0 ? void 0 : _node$position.start.offset;
159
+ var _node$position, _node$position$start;
160
+ var startOffset = (_node$position = node.position) === null || _node$position === void 0 ? void 0 : (_node$position$start = _node$position.start) === null || _node$position$start === void 0 ? void 0 : _node$position$start.offset;
161
161
  if (startOffset === undefined) {
162
162
  return;
163
163
  }
@@ -7,9 +7,9 @@ var rehypeRawPositions = function rehypeRawPositions() {
7
7
  return function (root) {
8
8
  var rawPositions = [];
9
9
  visit(root, 'raw', function (node) {
10
- var _node$position, _node$position2;
11
- var startOffset = (_node$position = node.position) === null || _node$position === void 0 ? void 0 : _node$position.start.offset;
12
- var endOffset = (_node$position2 = node.position) === null || _node$position2 === void 0 ? void 0 : _node$position2.end.offset;
10
+ var _node$position, _node$position$start, _node$position2, _node$position2$end;
11
+ var startOffset = (_node$position = node.position) === null || _node$position === void 0 ? void 0 : (_node$position$start = _node$position.start) === null || _node$position$start === void 0 ? void 0 : _node$position$start.offset;
12
+ var endOffset = (_node$position2 = node.position) === null || _node$position2 === void 0 ? void 0 : (_node$position2$end = _node$position2.end) === null || _node$position2$end === void 0 ? void 0 : _node$position2$end.offset;
13
13
  if (startOffset === undefined || endOffset === undefined) {
14
14
  return;
15
15
  }
@@ -125,11 +125,6 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
125
125
  });
126
126
  break;
127
127
  }
128
- case 'compositionstart':
129
- {
130
- container.normalizeSelection();
131
- break;
132
- }
133
128
  }
134
129
  };
135
130
  var dragEventHandler = function dragEventHandler(e) {
@@ -176,6 +171,7 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
176
171
  {
177
172
  // 组合时不触发快捷键
178
173
  if (!e.nativeEvent.isComposing) {
174
+ container.normalizeSelection();
179
175
  composeHandlers(options.keyboardEventHandlers)({
180
176
  history: history,
181
177
  sourceCode: sourceCode,
@@ -377,7 +373,6 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
377
373
  contentEditable: true,
378
374
  onBlur: focusEventHandler,
379
375
  onCompositionEnd: compositionEventHandler,
380
- onCompositionStart: compositionEventHandler,
381
376
  onCopy: clipboardEventHandler,
382
377
  onCut: clipboardEventHandler,
383
378
  onDragStart: dragEventHandler,