x-star-editor 0.0.9 → 0.1.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.
Files changed (46) hide show
  1. package/dist/components/Fade.d.ts +9 -0
  2. package/dist/components/Fade.js +38 -0
  3. package/dist/components/FileInput.js +11 -8
  4. package/dist/components/Toolbar.d.ts +9 -5
  5. package/dist/components/Toolbar.js +154 -97
  6. package/dist/index.d.ts +7 -7
  7. package/dist/index.js +4 -4
  8. package/dist/locales/en_US.d.ts +27 -0
  9. package/dist/locales/en_US.js +26 -0
  10. package/dist/locales/index.d.ts +115 -0
  11. package/dist/locales/index.js +41 -0
  12. package/dist/locales/zh_CN.d.ts +27 -0
  13. package/dist/locales/zh_CN.js +26 -0
  14. package/dist/styles/icons/blockquote.svg +18 -0
  15. package/dist/styles/icons/code.svg +18 -1
  16. package/dist/styles/icons/delete.svg +18 -0
  17. package/dist/styles/icons/emphasis.svg +18 -0
  18. package/dist/styles/icons/heading.svg +16 -0
  19. package/dist/styles/icons/image.svg +18 -1
  20. package/dist/styles/icons/inline-code.svg +19 -0
  21. package/dist/styles/icons/inline-math.svg +20 -0
  22. package/dist/styles/icons/link.svg +18 -1
  23. package/dist/styles/icons/math.svg +17 -0
  24. package/dist/styles/icons/redo.svg +18 -0
  25. package/dist/styles/icons/strong.svg +18 -0
  26. package/dist/styles/icons/thematic-break.svg +18 -0
  27. package/dist/styles/icons/undo.svg +18 -0
  28. package/dist/styles/index.css +353 -389
  29. package/dist/utils/container.js +23 -1
  30. package/dist/utils/global.d.ts +1 -1
  31. package/dist/utils/global.js +1 -1
  32. package/dist/utils/history.d.ts +18 -5
  33. package/dist/utils/history.js +80 -68
  34. package/dist/utils/markdown.d.ts +7 -0
  35. package/dist/utils/markdown.js +25 -25
  36. package/dist/{XStarEditor → x-star-editor}/index.d.ts +22 -3
  37. package/dist/{XStarEditor → x-star-editor}/index.js +30 -20
  38. package/dist/{XStarMdEditor → x-star-md-editor}/index.d.ts +31 -3
  39. package/dist/{XStarMdEditor → x-star-md-editor}/index.js +23 -14
  40. package/dist/{XStarMdViewer → x-star-md-viewer}/index.d.ts +12 -0
  41. package/dist/{XStarMdViewer → x-star-md-viewer}/index.js +3 -1
  42. package/package.json +6 -4
  43. package/dist/styles/icons/bold.svg +0 -1
  44. package/dist/styles/icons/code-library.svg +0 -1
  45. package/dist/styles/icons/italic.svg +0 -1
  46. package/dist/styles/icons/strikethrough.svg +0 -1
@@ -49,6 +49,16 @@ export var useContainer = function useContainer() {
49
49
  */
50
50
  var ref = useRef(null);
51
51
 
52
+ /**
53
+ * 是否在遍历时忽略节点及其子节点
54
+ *
55
+ * @param node 节点
56
+ * @returns 是否忽略
57
+ */
58
+ var isIgnore = function isIgnore(node) {
59
+ return node instanceof HTMLElement && node.dataset.ignore !== undefined;
60
+ };
61
+
52
62
  /**
53
63
  * 将 DOM 树规范化
54
64
  *
@@ -59,6 +69,9 @@ export var useContainer = function useContainer() {
59
69
  return;
60
70
  }
61
71
  var traverse = function traverse(node, depth) {
72
+ if (isIgnore(node)) {
73
+ return;
74
+ }
62
75
  if (depth === 2) {
63
76
  var _node$lastChild;
64
77
  // 检查行节点的末尾
@@ -102,6 +115,9 @@ export var useContainer = function useContainer() {
102
115
  }
103
116
  var text = '';
104
117
  var traverse = function traverse(node) {
118
+ if (isIgnore(node)) {
119
+ return;
120
+ }
105
121
  if (node.nodeType === Node.TEXT_NODE) {
106
122
  if (node.nodeValue) {
107
123
  text += node.nodeValue;
@@ -152,7 +168,7 @@ export var useContainer = function useContainer() {
152
168
  }
153
169
  } else {
154
170
  // 节点名称相同,更新属性和子节点
155
- if (newChild instanceof Element && oldChild instanceof Element) {
171
+ if (newChild instanceof HTMLElement && oldChild instanceof HTMLElement) {
156
172
  var _iterator = _createForOfIteratorHelper(oldChild.getAttributeNames()),
157
173
  _step;
158
174
  try {
@@ -234,6 +250,9 @@ export var useContainer = function useContainer() {
234
250
  }
235
251
  var offset = 0;
236
252
  var traverse = function traverse(node) {
253
+ if (isIgnore(node)) {
254
+ return;
255
+ }
237
256
  if (node === targetNode) {
238
257
  if (node.nodeType === Node.TEXT_NODE) {
239
258
  if (node.nodeValue) {
@@ -296,6 +315,9 @@ export var useContainer = function useContainer() {
296
315
  }
297
316
  var offset = targetOffset;
298
317
  var traverse = function traverse(node) {
318
+ if (isIgnore(node)) {
319
+ return;
320
+ }
299
321
  if (node.nodeType === Node.TEXT_NODE) {
300
322
  if (node.nodeValue) {
301
323
  if (offset < node.nodeValue.length || offset === node.nodeValue.length && !node.nodeValue.endsWith("\u200B")) {
@@ -1 +1 @@
1
- export declare const prefix = "x-star-";
1
+ export declare const prefix = "x-star-editor-";
@@ -1 +1 @@
1
- export var prefix = 'x-star-';
1
+ export var prefix = 'x-star-editor-';
@@ -25,15 +25,15 @@ export interface DeleteAction {
25
25
  export interface ToggleAction {
26
26
  type: 'toggle';
27
27
  payload: {
28
- type: 'heading';
29
- depth: 1 | 2 | 3 | 4 | 5 | 6;
28
+ type: 'blockquote' | 'delete' | 'emphasis' | 'inlineCode' | 'inlineMath' | 'math' | 'strong' | 'thematicBreak';
30
29
  } | {
31
30
  type: 'code';
32
31
  language: string;
33
32
  } | {
34
- type: 'thematicBreak' | 'blockquote' | 'emphasis' | 'strong' | 'inlineCode' | 'delete' | 'math' | 'inlineMath';
33
+ type: 'heading';
34
+ depth: 1 | 2 | 3 | 4 | 5 | 6;
35
35
  } | {
36
- type: 'link' | 'image';
36
+ type: 'image' | 'link';
37
37
  url: string;
38
38
  description: string;
39
39
  };
@@ -44,6 +44,17 @@ export interface SetAction {
44
44
  payload: State;
45
45
  selection: ContainerSelection;
46
46
  }
47
+ type StateAction = InsertAction | DeleteAction | ToggleAction | SetAction;
48
+ /**
49
+ * 编辑器历史
50
+ */
51
+ interface History {
52
+ states: (State & {
53
+ action: StateAction;
54
+ })[];
55
+ index: number;
56
+ selection: ContainerSelection;
57
+ }
47
58
  export interface UndoAction {
48
59
  type: 'undo';
49
60
  }
@@ -54,9 +65,11 @@ export interface SelectAction {
54
65
  type: 'select';
55
66
  selection: ContainerSelection;
56
67
  }
68
+ type HistoryAction = UndoAction | RedoAction | SelectAction;
57
69
  export declare const useHistory: (initialSourceCode: string) => {
70
+ history: History;
58
71
  sourceCode: string;
59
72
  selection: ContainerSelection;
60
- dispatch: import("react").Dispatch<InsertAction | DeleteAction | ToggleAction | SetAction | UndoAction | RedoAction | SelectAction>;
73
+ dispatch: import("react").Dispatch<StateAction | HistoryAction>;
61
74
  };
62
75
  export {};
@@ -84,26 +84,22 @@ var stateReducer = function stateReducer(_ref, action) {
84
84
  var lineBefore = sourceCode.slice(0, lineStartOffset);
85
85
  var lineAfter = sourceCode.slice(lineEndOffset);
86
86
  switch (action.payload.type) {
87
- case 'heading':
87
+ case 'blockquote':
88
88
  {
89
- var depth = action.payload.depth;
90
89
  var lines = sourceCode.slice(lineStartOffset, lineEndOffset).split('\n');
91
90
  var values = lines.map(function (line) {
92
- return [1, 2, 3, 4, 5, 6].find(function (value) {
93
- return new RegExp("^ {0,3}#{".concat(value, "} +")).test(line);
94
- });
91
+ return /^ {0,3}> */.test(line);
95
92
  });
96
93
  for (var i = 0; i < values.length; i++) {
97
- var value = values[i];
98
- if (value) {
99
- lines[i] = lines[i].replace(new RegExp("^ {0,3}#{".concat(value, "} +")), '');
94
+ if (values[i]) {
95
+ lines[i] = lines[i].replace(/^ {0,3}> */, '');
100
96
  }
101
97
  }
102
98
  if (values.some(function (value) {
103
- return value !== depth;
99
+ return !value;
104
100
  })) {
105
101
  for (var _i = 0; _i < lines.length; _i++) {
106
- lines[_i] = "".concat('#'.repeat(depth), " ").concat(lines[_i]);
102
+ lines[_i] = "> ".concat(lines[_i]);
107
103
  }
108
104
  }
109
105
  var _text = lines.join('\n');
@@ -120,59 +116,21 @@ var stateReducer = function stateReducer(_ref, action) {
120
116
  selection: createSelection(lineEndOffset + language.length + 21)
121
117
  };
122
118
  }
123
- case 'thematicBreak':
124
- {
125
- return {
126
- sourceCode: "".concat(sourceCode.slice(0, lineEndOffset), "\n***\n").concat(lineAfter),
127
- selection: createSelection(lineEndOffset + 5)
128
- };
129
- }
130
- case 'blockquote':
131
- {
132
- var _lines = sourceCode.slice(lineStartOffset, lineEndOffset).split('\n');
133
- var _values = _lines.map(function (line) {
134
- return /^ {0,3}> */.test(line);
135
- });
136
- for (var _i2 = 0; _i2 < _values.length; _i2++) {
137
- if (_values[_i2]) {
138
- _lines[_i2] = _lines[_i2].replace(/^ {0,3}> */, '');
139
- }
140
- }
141
- if (_values.some(function (value) {
142
- return !value;
143
- })) {
144
- for (var _i3 = 0; _i3 < _lines.length; _i3++) {
145
- _lines[_i3] = "> ".concat(_lines[_i3]);
146
- }
147
- }
148
- var _text2 = _lines.join('\n');
149
- return {
150
- sourceCode: "".concat(lineBefore).concat(_text2).concat(lineAfter),
151
- selection: createSelection(lineStartOffset + _text2.length)
152
- };
153
- }
154
- case 'math':
155
- {
156
- return {
157
- sourceCode: "".concat(sourceCode.slice(0, lineEndOffset), "\n$$\n\n$$\n").concat(lineAfter),
158
- selection: createSelection(lineEndOffset + 4)
159
- };
160
- }
119
+ case 'delete':
161
120
  case 'emphasis':
162
- case 'strong':
163
121
  case 'inlineCode':
164
- case 'delete':
165
122
  case 'inlineMath':
123
+ case 'strong':
166
124
  {
167
125
  var type = action.payload.type;
168
126
  var delimiters = type === 'emphasis' ? ['*', '_'] : type === 'strong' ? ['**', '__'] : type === 'inlineCode' ? ['`'] : type === 'delete' ? ['~~'] : ['$'];
169
- var _text3 = sourceCode.slice(startOffset, endOffset);
170
- for (var _i4 = 0, _delimiters = delimiters; _i4 < _delimiters.length; _i4++) {
171
- var _delimiter = _delimiters[_i4];
127
+ var _text2 = sourceCode.slice(startOffset, endOffset);
128
+ for (var _i2 = 0, _delimiters = delimiters; _i2 < _delimiters.length; _i2++) {
129
+ var _delimiter = _delimiters[_i2];
172
130
  if (before.endsWith(_delimiter) && after.startsWith(_delimiter)) {
173
131
  // 删除分隔文本
174
132
  return {
175
- sourceCode: "".concat(before.slice(0, before.length - _delimiter.length)).concat(_text3).concat(after.slice(_delimiter.length)),
133
+ sourceCode: "".concat(before.slice(0, before.length - _delimiter.length)).concat(_text2).concat(after.slice(_delimiter.length)),
176
134
  selection: createSelection(anchorOffset - _delimiter.length, focusOffset - _delimiter.length)
177
135
  };
178
136
  }
@@ -180,12 +138,40 @@ var stateReducer = function stateReducer(_ref, action) {
180
138
  var delimiter = delimiters[0];
181
139
  // 插入分隔文本
182
140
  return {
183
- sourceCode: "".concat(before).concat(delimiter).concat(_text3).concat(delimiter).concat(after),
141
+ sourceCode: "".concat(before).concat(delimiter).concat(_text2).concat(delimiter).concat(after),
184
142
  selection: createSelection(anchorOffset + delimiter.length, focusOffset + delimiter.length)
185
143
  };
186
144
  }
187
- case 'link':
145
+ case 'heading':
146
+ {
147
+ var depth = action.payload.depth;
148
+ var _lines = sourceCode.slice(lineStartOffset, lineEndOffset).split('\n');
149
+ var _values = _lines.map(function (line) {
150
+ return [1, 2, 3, 4, 5, 6].find(function (value) {
151
+ return new RegExp("^ {0,3}#{".concat(value, "} +")).test(line);
152
+ });
153
+ });
154
+ for (var _i3 = 0; _i3 < _values.length; _i3++) {
155
+ var value = _values[_i3];
156
+ if (value) {
157
+ _lines[_i3] = _lines[_i3].replace(new RegExp("^ {0,3}#{".concat(value, "} +")), '');
158
+ }
159
+ }
160
+ if (_values.some(function (value) {
161
+ return value !== depth;
162
+ })) {
163
+ for (var _i4 = 0; _i4 < _lines.length; _i4++) {
164
+ _lines[_i4] = "".concat('#'.repeat(depth), " ").concat(_lines[_i4]);
165
+ }
166
+ }
167
+ var _text3 = _lines.join('\n');
168
+ return {
169
+ sourceCode: "".concat(lineBefore).concat(_text3).concat(lineAfter),
170
+ selection: createSelection(lineStartOffset + _text3.length)
171
+ };
172
+ }
188
173
  case 'image':
174
+ case 'link':
189
175
  {
190
176
  var _action$payload2 = action.payload,
191
177
  _type = _action$payload2.type,
@@ -196,6 +182,20 @@ var stateReducer = function stateReducer(_ref, action) {
196
182
  selection: createSelection(startOffset + url.length + description.length + 4 + (_type === 'link' ? 0 : 1))
197
183
  };
198
184
  }
185
+ case 'math':
186
+ {
187
+ return {
188
+ sourceCode: "".concat(sourceCode.slice(0, lineEndOffset), "\n$$\n\n$$\n").concat(lineAfter),
189
+ selection: createSelection(lineEndOffset + 4)
190
+ };
191
+ }
192
+ case 'thematicBreak':
193
+ {
194
+ return {
195
+ sourceCode: "".concat(sourceCode.slice(0, lineEndOffset), "\n***\n").concat(lineAfter),
196
+ selection: createSelection(lineEndOffset + 5)
197
+ };
198
+ }
199
199
  default:
200
200
  {
201
201
  var _ = action.payload;
@@ -242,7 +242,7 @@ var historyReducer = function historyReducer(_ref2, action) {
242
242
  return {
243
243
  states: states,
244
244
  index: index - 1,
245
- selection: states[index].actionSelection
245
+ selection: states[index].action.selection
246
246
  };
247
247
  }
248
248
  case 'redo':
@@ -280,29 +280,41 @@ var historyReducer = function historyReducer(_ref2, action) {
280
280
  };
281
281
  }
282
282
  return {
283
- states: [].concat(_toConsumableArray(states.slice(index < 32 ? 0 : index - 31, index + 1)), [_objectSpread(_objectSpread({}, state), {}, {
284
- actionSelection: action.selection
283
+ states: [].concat(_toConsumableArray(states.slice(index < 100 ? 0 : index - 99, index + 1)), [_objectSpread(_objectSpread({}, state), {}, {
284
+ action: action
285
285
  })]),
286
- index: index < 32 ? index + 1 : 32,
286
+ index: index < 100 ? index + 1 : 100,
287
287
  selection: state.selection
288
288
  };
289
289
  }
290
290
  }
291
291
  };
292
292
  export var useHistory = function useHistory(initialSourceCode) {
293
- var _useReducer = useReducer(historyReducer, {
294
- states: [{
295
- sourceCode: initialSourceCode,
296
- selection: createSelection(0),
297
- actionSelection: createSelection(0)
298
- }],
299
- index: 0,
300
- selection: createSelection(0)
293
+ var _useReducer = useReducer(historyReducer, initialSourceCode, function (text) {
294
+ var action = {
295
+ type: 'insert',
296
+ payload: {
297
+ text: text
298
+ },
299
+ selection: createSelection(0)
300
+ };
301
+ var state = stateReducer({
302
+ sourceCode: '',
303
+ selection: createSelection(0)
304
+ }, action);
305
+ return {
306
+ states: [_objectSpread(_objectSpread({}, state), {}, {
307
+ action: action
308
+ })],
309
+ index: 0,
310
+ selection: state.selection
311
+ };
301
312
  }),
302
313
  _useReducer2 = _slicedToArray(_useReducer, 2),
303
314
  history = _useReducer2[0],
304
315
  dispatch = _useReducer2[1];
305
316
  return {
317
+ history: history,
306
318
  sourceCode: history.states[history.index].sourceCode,
307
319
  selection: history.selection,
308
320
  dispatch: dispatch
@@ -56,4 +56,11 @@ export declare const toHTML: (sourceCode: string) => string;
56
56
  * @returns Markdown 文本
57
57
  */
58
58
  export declare const toMarkdown: (sourceCode: string) => string;
59
+ /**
60
+ * 将 Markdown 文本转成纯文本
61
+ *
62
+ * @param sourceCode Markdown 文本
63
+ * @returns 纯文本
64
+ */
65
+ export declare const toText: (sourceCode: string) => string;
59
66
  export {};
@@ -25,6 +25,7 @@ import remarkMath from 'remark-math';
25
25
  import remarkParse from 'remark-parse';
26
26
  import remarkRehype from 'remark-rehype';
27
27
  import remarkStringify from 'remark-stringify';
28
+ import strip from 'strip-markdown';
28
29
  import { unified } from 'unified';
29
30
  import { prefix } from "./global";
30
31
  export var getDefaultSchema = function getDefaultSchema() {
@@ -78,7 +79,7 @@ export var editorRender = function editorRender(sourceCode) {
78
79
  /**
79
80
  * CSS 类名前缀
80
81
  */
81
- var classNamePrefix = "".concat(prefix, "md-editor-");
82
+ var mdPrefix = "".concat(prefix, "md-");
82
83
 
83
84
  /**
84
85
  * 扫描偏移量,表示已经添加到 DOM 树的文本
@@ -86,7 +87,7 @@ export var editorRender = function editorRender(sourceCode) {
86
87
  var scanOffset = 0;
87
88
 
88
89
  /**
89
- * 遍历 Mdast 树时的栈,保存 Mdast 节点、对应的 DOM 元素、是否为 Mdast 节点的第一个子元素
90
+ * 遍历 Mdast 树时的栈,保存 Mdast 节点、对应的 DOM 元素、为第几个子元素
90
91
  */
91
92
  var stack = [];
92
93
 
@@ -107,47 +108,35 @@ export var editorRender = function editorRender(sourceCode) {
107
108
  * @returns 子节点是否为流内容
108
109
  */
109
110
  var isFlowContent = function isFlowContent(node) {
110
- return node.type === 'blockquote' || node.type === 'listItem' || node.type === 'footnoteDefinition';
111
+ return node.type === 'blockquote' || node.type === 'footnoteDefinition' || node.type === 'listItem';
111
112
  };
112
113
 
113
114
  /**
114
115
  * 将栈中指定索引的 DOM 元素推到对应的父元素中
115
116
  *
116
117
  * @param index 索引
117
- * @param last 是否为 Mdast 节点的最后一个子元素,默认为 `false`
118
118
  */
119
119
  var pushElement = function pushElement(index) {
120
- var last = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
121
- var _stack$index = stack[index],
122
- node = _stack$index.node,
123
- first = _stack$index.first;
120
+ var node = stack[index].node;
124
121
 
125
122
  // 如果节点为流内容,则将样式添加到该行的 `<div>` 元素上,否则添加到该节点的元素上
126
123
  var styleElement = stack[index && !isFlowContent(stack[index - 1].node) ? index : 0].el;
127
- styleElement.classList.add("".concat(classNamePrefix).concat(node.type));
124
+ styleElement.classList.add("".concat(mdPrefix).concat(node.type));
128
125
  if (node.type === 'heading') {
129
- styleElement.classList.add("".concat(classNamePrefix, "heading").concat(node.depth));
130
- } else if (node.type === 'html' || node.type === 'code' || node.type === 'math') {
131
- if (first) {
132
- styleElement.classList.add("".concat(classNamePrefix, "first"));
133
- }
134
- if (last) {
135
- styleElement.classList.add("".concat(classNamePrefix, "last"));
136
- }
126
+ styleElement.classList.add("".concat(mdPrefix, "heading").concat(node.depth));
137
127
  }
138
128
 
139
129
  // 如果索引为 0,则父节点为块级元素,否则为前一个索引的 DOM 元素
140
130
  var parentElement = index ? stack[index - 1].el : block;
141
131
  parentElement.append(stack[index].el);
142
132
  stack[index].el = document.createElement(index ? 'span' : 'div');
143
- stack[index].first = false;
133
+ stack[index].counter++;
144
134
  };
145
135
 
146
136
  /**
147
137
  * 将块级元素推到 DOM 树根元素中
148
138
  */
149
139
  var pushBlock = function pushBlock() {
150
- block.classList.add("".concat(classNamePrefix, "block"));
151
140
  el.append(block);
152
141
  block = document.createElement('div');
153
142
  };
@@ -161,7 +150,7 @@ export var editorRender = function editorRender(sourceCode) {
161
150
  var createDelimiter = function createDelimiter(text) {
162
151
  var temp = document.createElement('span');
163
152
  temp.append(text);
164
- temp.classList.add("".concat(classNamePrefix, "delimiter"));
153
+ temp.classList.add("".concat(mdPrefix, "delimiter"));
165
154
  return temp;
166
155
  };
167
156
 
@@ -247,7 +236,7 @@ export var editorRender = function editorRender(sourceCode) {
247
236
  stack.push({
248
237
  node: node,
249
238
  el: document.createElement(stack.length ? 'span' : 'div'),
250
- first: true
239
+ counter: 0
251
240
  });
252
241
  if ('children' in node) {
253
242
  getChildren(node.children, endOffset);
@@ -255,13 +244,13 @@ export var editorRender = function editorRender(sourceCode) {
255
244
  addText(sourceCode.slice(scanOffset, endOffset));
256
245
  }
257
246
  if (stack.length > 1) {
258
- pushElement(stack.length - 1, true);
247
+ pushElement(stack.length - 1);
259
248
  } else {
260
249
  var _node$position3;
261
250
  // 如果栈大小为 1,说明为块级元素终点
262
251
  stack[0].el.append("\u200B");
263
- pushElement(0, true);
264
- block.setAttribute('line', "".concat((_node$position3 = node.position) === null || _node$position3 === void 0 ? void 0 : _node$position3.start.line));
252
+ pushElement(0);
253
+ block.dataset.line = "".concat((_node$position3 = node.position) === null || _node$position3 === void 0 ? void 0 : _node$position3.start.line);
265
254
  pushBlock();
266
255
  scanOffset++;
267
256
  }
@@ -301,7 +290,7 @@ export var viewerRender = function viewerRender(sourceCode, options) {
301
290
  var node = _step2.value;
302
291
  if ('properties' in node && node.properties) {
303
292
  var _node$position4;
304
- node.properties.line = (_node$position4 = node.position) === null || _node$position4 === void 0 ? void 0 : _node$position4.start.line;
293
+ node.properties['data-line'] = (_node$position4 = node.position) === null || _node$position4 === void 0 ? void 0 : _node$position4.start.line;
305
294
  }
306
295
  }
307
296
  } catch (err) {
@@ -350,4 +339,15 @@ var toMarkdownProcessor = unified().use(rehypeRemark).use(remarkGfm).use(remarkB
350
339
  */
351
340
  export var toMarkdown = function toMarkdown(sourceCode) {
352
341
  return toMarkdownProcessor.stringify(toMarkdownProcessor.runSync(toHastProcessor.runSync(toHastProcessor.parse(sourceCode))));
342
+ };
343
+ var toTextProcessor = unified().use(strip).freeze();
344
+
345
+ /**
346
+ * 将 Markdown 文本转成纯文本
347
+ *
348
+ * @param sourceCode Markdown 文本
349
+ * @returns 纯文本
350
+ */
351
+ export var toText = function toText(sourceCode) {
352
+ return toTextProcessor.processSync(sourceCode).toString();
353
353
  };
@@ -1,15 +1,34 @@
1
1
  import React from 'react';
2
- import type { XStarMdEditorHandle, XStarMdEditorProps } from '../XStarMdEditor';
3
- import type { XStarMdViewerHandle, XStarMdViewerProps } from '../XStarMdViewer';
2
+ import type { XStarMdEditorHandle, XStarMdEditorProps } from '../x-star-md-editor';
3
+ import type { XStarMdViewerHandle, XStarMdViewerProps } from '../x-star-md-viewer';
4
4
  export interface XStarEditorHandle {
5
5
  getEditor: () => XStarMdEditorHandle | null;
6
6
  getViewer: () => XStarMdViewerHandle | null;
7
7
  }
8
8
  export interface XStarEditorProps {
9
+ /**
10
+ * 编辑器和浏览器的高度(不包括工具栏)
11
+ */
9
12
  height?: React.CSSProperties['height'];
13
+ /**
14
+ * 语言
15
+ */
16
+ locale?: string;
17
+ /**
18
+ * 初始文本
19
+ */
10
20
  initialValue?: string;
11
- editorProps?: Omit<XStarMdEditorProps, 'initialValue' | 'onChange'>;
21
+ /**
22
+ * 编辑器属性
23
+ */
24
+ editorProps?: Omit<XStarMdEditorProps, 'locale' | 'initialValue' | 'onChange'>;
25
+ /**
26
+ * 浏览器属性
27
+ */
12
28
  viewerProps?: Omit<XStarMdViewerProps, 'value'>;
29
+ /**
30
+ * 文本变化回调函数
31
+ */
13
32
  onChange?: (value: string) => void;
14
33
  }
15
34
  declare const XStarEditor: React.ForwardRefExoticComponent<XStarEditorProps & React.RefAttributes<XStarEditorHandle>>;
@@ -13,11 +13,12 @@ function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefine
13
13
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
14
14
  import classNames from 'classnames';
15
15
  import React, { useEffect, useImperativeHandle, useRef, useState } from 'react';
16
- import XStarMdEditor, { useMdEditorRef } from "../XStarMdEditor";
17
- import XStarMdViewer, { useMdViewerRef } from "../XStarMdViewer";
18
16
  import { prefix } from "../utils/global";
17
+ import XStarMdEditor, { useMdEditorRef } from "../x-star-md-editor";
18
+ import XStarMdViewer, { useMdViewerRef } from "../x-star-md-viewer";
19
19
  var XStarEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
20
20
  var height = _ref.height,
21
+ locale = _ref.locale,
21
22
  initialValue = _ref.initialValue,
22
23
  editorProps = _ref.editorProps,
23
24
  viewerProps = _ref.viewerProps,
@@ -59,24 +60,30 @@ var XStarEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
59
60
  var toLimit = toElement.scrollHeight - toElement.clientHeight;
60
61
  if (fromLimit > 0 && toLimit > 0) {
61
62
  for (var i = 0, j = 0; i < fromElement.children.length && j < toElement.children.length;) {
62
- var _fromChild$getAttribu, _toChild$getAttribute;
63
63
  var fromChild = fromElement.children[i];
64
64
  var toChild = toElement.children[j];
65
- var fromLine = parseInt((_fromChild$getAttribu = fromChild.getAttribute('line')) !== null && _fromChild$getAttribu !== void 0 ? _fromChild$getAttribu : '');
66
- var toLine = parseInt((_toChild$getAttribute = toChild.getAttribute('line')) !== null && _toChild$getAttribute !== void 0 ? _toChild$getAttribute : '');
67
- if (!(fromChild instanceof HTMLElement) || !fromLine) {
65
+ if (!(fromChild instanceof HTMLElement)) {
68
66
  i++;
69
- } else if (!(toChild instanceof HTMLElement) || !toLine) {
70
- j++;
71
- } else if (fromLine < toLine) {
72
- i++;
73
- } else if (fromLine > toLine) {
67
+ } else if (!(toChild instanceof HTMLElement)) {
74
68
  j++;
75
69
  } else {
76
- fromOffsetTops.push(fromChild.offsetTop);
77
- toOffsetTops.push(toChild.offsetTop);
78
- i++;
79
- j++;
70
+ var _fromChild$dataset$li, _toChild$dataset$line;
71
+ var fromLine = parseInt((_fromChild$dataset$li = fromChild.dataset.line) !== null && _fromChild$dataset$li !== void 0 ? _fromChild$dataset$li : '');
72
+ var toLine = parseInt((_toChild$dataset$line = toChild.dataset.line) !== null && _toChild$dataset$line !== void 0 ? _toChild$dataset$line : '');
73
+ if (!fromLine) {
74
+ i++;
75
+ } else if (!toLine) {
76
+ j++;
77
+ } else if (fromLine < toLine) {
78
+ i++;
79
+ } else if (fromLine > toLine) {
80
+ j++;
81
+ } else {
82
+ fromOffsetTops.push(fromChild.offsetTop);
83
+ toOffsetTops.push(toChild.offsetTop);
84
+ i++;
85
+ j++;
86
+ }
80
87
  }
81
88
  }
82
89
  for (; fromOffsetTops[fromOffsetTops.length - 1] >= fromLimit || toOffsetTops[toOffsetTops.length - 1] >= toLimit;) {
@@ -140,9 +147,10 @@ var XStarEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
140
147
  }, /*#__PURE__*/React.createElement(XStarMdEditor, _extends({
141
148
  ref: editorRef
142
149
  }, editorProps, {
143
- style: _objectSpread({
150
+ style: _objectSpread(_objectSpread({}, editorProps === null || editorProps === void 0 ? void 0 : editorProps.style), {}, {
144
151
  height: height
145
- }, editorProps === null || editorProps === void 0 ? void 0 : editorProps.style),
152
+ }),
153
+ locale: locale,
146
154
  initialValue: initialValue,
147
155
  onChange: function onChange(value) {
148
156
  setValue(value);
@@ -151,13 +159,15 @@ var XStarEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
151
159
  })), /*#__PURE__*/React.createElement(XStarMdViewer, _extends({
152
160
  ref: viewerRef
153
161
  }, viewerProps, {
154
- style: _objectSpread({
162
+ style: _objectSpread(_objectSpread({}, viewerProps === null || viewerProps === void 0 ? void 0 : viewerProps.style), {}, {
155
163
  height: height
156
- }, viewerProps === null || viewerProps === void 0 ? void 0 : viewerProps.style),
164
+ }),
157
165
  value: value
158
166
  })));
159
167
  });
160
- XStarEditor.displayName = 'XStarEditor';
168
+ if (process.env.NODE_ENV !== 'production') {
169
+ XStarEditor.displayName = 'XStarEditor';
170
+ }
161
171
  export default XStarEditor;
162
172
  export var useEditorRef = function useEditorRef() {
163
173
  return useRef(null);