x-star-editor 0.0.2

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 (100) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +40 -0
  3. package/dist/XStarEditor/index.d.ts +12 -0
  4. package/dist/XStarEditor/index.js +114 -0
  5. package/dist/XStarEditor/index.module.css +19 -0
  6. package/dist/XStarMdEditor/index.d.ts +31 -0
  7. package/dist/XStarMdEditor/index.js +263 -0
  8. package/dist/XStarMdEditor/index.module.css +13 -0
  9. package/dist/XStarMdViewer/index.d.ts +18 -0
  10. package/dist/XStarMdViewer/index.js +33 -0
  11. package/dist/XStarMdViewer/index.module.css +10 -0
  12. package/dist/components/FileInput.d.ts +15 -0
  13. package/dist/components/FileInput.js +107 -0
  14. package/dist/components/FileInput.module.css +81 -0
  15. package/dist/components/Toolbar.d.ts +22 -0
  16. package/dist/components/Toolbar.js +294 -0
  17. package/dist/components/Toolbar.module.css +116 -0
  18. package/dist/components/icons/bold.svg +1 -0
  19. package/dist/components/icons/code-library.svg +1 -0
  20. package/dist/components/icons/code.svg +1 -0
  21. package/dist/components/icons/image.svg +1 -0
  22. package/dist/components/icons/italic.svg +1 -0
  23. package/dist/components/icons/link.svg +1 -0
  24. package/dist/components/icons/strikethrough.svg +1 -0
  25. package/dist/index.d.ts +9 -0
  26. package/dist/index.js +5 -0
  27. package/dist/styles/fonts/KaTeX_AMS-Regular.ttf +0 -0
  28. package/dist/styles/fonts/KaTeX_AMS-Regular.woff +0 -0
  29. package/dist/styles/fonts/KaTeX_AMS-Regular.woff2 +0 -0
  30. package/dist/styles/fonts/KaTeX_Caligraphic-Bold.ttf +0 -0
  31. package/dist/styles/fonts/KaTeX_Caligraphic-Bold.woff +0 -0
  32. package/dist/styles/fonts/KaTeX_Caligraphic-Bold.woff2 +0 -0
  33. package/dist/styles/fonts/KaTeX_Caligraphic-Regular.ttf +0 -0
  34. package/dist/styles/fonts/KaTeX_Caligraphic-Regular.woff +0 -0
  35. package/dist/styles/fonts/KaTeX_Caligraphic-Regular.woff2 +0 -0
  36. package/dist/styles/fonts/KaTeX_Fraktur-Bold.ttf +0 -0
  37. package/dist/styles/fonts/KaTeX_Fraktur-Bold.woff +0 -0
  38. package/dist/styles/fonts/KaTeX_Fraktur-Bold.woff2 +0 -0
  39. package/dist/styles/fonts/KaTeX_Fraktur-Regular.ttf +0 -0
  40. package/dist/styles/fonts/KaTeX_Fraktur-Regular.woff +0 -0
  41. package/dist/styles/fonts/KaTeX_Fraktur-Regular.woff2 +0 -0
  42. package/dist/styles/fonts/KaTeX_Main-Bold.ttf +0 -0
  43. package/dist/styles/fonts/KaTeX_Main-Bold.woff +0 -0
  44. package/dist/styles/fonts/KaTeX_Main-Bold.woff2 +0 -0
  45. package/dist/styles/fonts/KaTeX_Main-BoldItalic.ttf +0 -0
  46. package/dist/styles/fonts/KaTeX_Main-BoldItalic.woff +0 -0
  47. package/dist/styles/fonts/KaTeX_Main-BoldItalic.woff2 +0 -0
  48. package/dist/styles/fonts/KaTeX_Main-Italic.ttf +0 -0
  49. package/dist/styles/fonts/KaTeX_Main-Italic.woff +0 -0
  50. package/dist/styles/fonts/KaTeX_Main-Italic.woff2 +0 -0
  51. package/dist/styles/fonts/KaTeX_Main-Regular.ttf +0 -0
  52. package/dist/styles/fonts/KaTeX_Main-Regular.woff +0 -0
  53. package/dist/styles/fonts/KaTeX_Main-Regular.woff2 +0 -0
  54. package/dist/styles/fonts/KaTeX_Math-BoldItalic.ttf +0 -0
  55. package/dist/styles/fonts/KaTeX_Math-BoldItalic.woff +0 -0
  56. package/dist/styles/fonts/KaTeX_Math-BoldItalic.woff2 +0 -0
  57. package/dist/styles/fonts/KaTeX_Math-Italic.ttf +0 -0
  58. package/dist/styles/fonts/KaTeX_Math-Italic.woff +0 -0
  59. package/dist/styles/fonts/KaTeX_Math-Italic.woff2 +0 -0
  60. package/dist/styles/fonts/KaTeX_SansSerif-Bold.ttf +0 -0
  61. package/dist/styles/fonts/KaTeX_SansSerif-Bold.woff +0 -0
  62. package/dist/styles/fonts/KaTeX_SansSerif-Bold.woff2 +0 -0
  63. package/dist/styles/fonts/KaTeX_SansSerif-Italic.ttf +0 -0
  64. package/dist/styles/fonts/KaTeX_SansSerif-Italic.woff +0 -0
  65. package/dist/styles/fonts/KaTeX_SansSerif-Italic.woff2 +0 -0
  66. package/dist/styles/fonts/KaTeX_SansSerif-Regular.ttf +0 -0
  67. package/dist/styles/fonts/KaTeX_SansSerif-Regular.woff +0 -0
  68. package/dist/styles/fonts/KaTeX_SansSerif-Regular.woff2 +0 -0
  69. package/dist/styles/fonts/KaTeX_Script-Regular.ttf +0 -0
  70. package/dist/styles/fonts/KaTeX_Script-Regular.woff +0 -0
  71. package/dist/styles/fonts/KaTeX_Script-Regular.woff2 +0 -0
  72. package/dist/styles/fonts/KaTeX_Size1-Regular.ttf +0 -0
  73. package/dist/styles/fonts/KaTeX_Size1-Regular.woff +0 -0
  74. package/dist/styles/fonts/KaTeX_Size1-Regular.woff2 +0 -0
  75. package/dist/styles/fonts/KaTeX_Size2-Regular.ttf +0 -0
  76. package/dist/styles/fonts/KaTeX_Size2-Regular.woff +0 -0
  77. package/dist/styles/fonts/KaTeX_Size2-Regular.woff2 +0 -0
  78. package/dist/styles/fonts/KaTeX_Size3-Regular.ttf +0 -0
  79. package/dist/styles/fonts/KaTeX_Size3-Regular.woff +0 -0
  80. package/dist/styles/fonts/KaTeX_Size3-Regular.woff2 +0 -0
  81. package/dist/styles/fonts/KaTeX_Size4-Regular.ttf +0 -0
  82. package/dist/styles/fonts/KaTeX_Size4-Regular.woff +0 -0
  83. package/dist/styles/fonts/KaTeX_Size4-Regular.woff2 +0 -0
  84. package/dist/styles/fonts/KaTeX_Typewriter-Regular.ttf +0 -0
  85. package/dist/styles/fonts/KaTeX_Typewriter-Regular.woff +0 -0
  86. package/dist/styles/fonts/KaTeX_Typewriter-Regular.woff2 +0 -0
  87. package/dist/styles/katex.css +1120 -0
  88. package/dist/styles/markdown.css +129 -0
  89. package/dist/styles/prism.css +182 -0
  90. package/dist/utils/container.d.ts +45 -0
  91. package/dist/utils/container.js +366 -0
  92. package/dist/utils/handler.d.ts +44 -0
  93. package/dist/utils/handler.js +227 -0
  94. package/dist/utils/history.d.ts +59 -0
  95. package/dist/utils/history.js +305 -0
  96. package/dist/utils/jsx-runtime.d.ts +7 -0
  97. package/dist/utils/markdown.d.ts +38 -0
  98. package/dist/utils/markdown.js +292 -0
  99. package/dist/utils/module-css.d.ts +3 -0
  100. package/package.json +88 -0
@@ -0,0 +1,44 @@
1
+ /// <reference types="react" />
2
+ import type { DeleteAction, InsertAction, SelectAction, SetAction, ToggleAction, useHistory } from './history';
3
+ /**
4
+ * 处理函数上下文
5
+ */
6
+ type HandlerContext = ReturnType<typeof useHistory>;
7
+ /**
8
+ * 处理函数
9
+ */
10
+ export interface Handler {
11
+ (ctx: HandlerContext): void;
12
+ }
13
+ export declare const insertHandler: (payload: InsertAction['payload']) => Handler;
14
+ export declare const deleteHandler: (payload: DeleteAction['payload']) => Handler;
15
+ export declare const toggleHandler: (payload: ToggleAction['payload']) => Handler;
16
+ export declare const setHandler: (payload: SetAction['payload']) => Handler;
17
+ export declare const undoHandler: () => Handler;
18
+ export declare const redoHandler: () => Handler;
19
+ export declare const selectHandler: (selection: SelectAction['selection']) => Handler;
20
+ /**
21
+ * 执行函数
22
+ *
23
+ * 接收一个处理函数,获取上下文后执行它
24
+ */
25
+ export interface Executor {
26
+ (handler: Handler): void;
27
+ }
28
+ /**
29
+ * 键盘事件处理函数
30
+ */
31
+ export interface KeyboardEventHandler {
32
+ (ctx: HandlerContext & {
33
+ e: React.KeyboardEvent;
34
+ }): void;
35
+ }
36
+ export declare const getDefaultKeyboardEventHandlers: () => KeyboardEventHandler[];
37
+ /**
38
+ * 组装处理函数
39
+ *
40
+ * @param handlers 处理函数数组
41
+ * @returns 组装好的处理函数
42
+ */
43
+ export declare const composeHandlers: <T>(handlers?: ((ctx: T) => void)[] | undefined) => (ctx: T) => T;
44
+ export {};
@@ -0,0 +1,227 @@
1
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
2
+ 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); }
3
+ 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; }
4
+ import { createSelection, getRange } from "./container";
5
+
6
+ /**
7
+ * 处理函数上下文
8
+ */
9
+
10
+ /**
11
+ * 处理函数
12
+ */
13
+
14
+ export var insertHandler = function insertHandler(payload) {
15
+ return function (_ref) {
16
+ var selection = _ref.selection,
17
+ dispatch = _ref.dispatch;
18
+ return dispatch({
19
+ type: 'insert',
20
+ payload: payload,
21
+ selection: selection
22
+ });
23
+ };
24
+ };
25
+ export var deleteHandler = function deleteHandler(payload) {
26
+ return function (_ref2) {
27
+ var selection = _ref2.selection,
28
+ dispatch = _ref2.dispatch;
29
+ return dispatch({
30
+ type: 'delete',
31
+ payload: payload,
32
+ selection: selection
33
+ });
34
+ };
35
+ };
36
+ export var toggleHandler = function toggleHandler(payload) {
37
+ return function (_ref3) {
38
+ var selection = _ref3.selection,
39
+ dispatch = _ref3.dispatch;
40
+ return dispatch({
41
+ type: 'toggle',
42
+ payload: payload,
43
+ selection: selection
44
+ });
45
+ };
46
+ };
47
+ export var setHandler = function setHandler(payload) {
48
+ return function (_ref4) {
49
+ var selection = _ref4.selection,
50
+ dispatch = _ref4.dispatch;
51
+ return dispatch({
52
+ type: 'set',
53
+ payload: payload,
54
+ selection: selection
55
+ });
56
+ };
57
+ };
58
+ export var undoHandler = function undoHandler() {
59
+ return function (_ref5) {
60
+ var dispatch = _ref5.dispatch;
61
+ return dispatch({
62
+ type: 'undo'
63
+ });
64
+ };
65
+ };
66
+ export var redoHandler = function redoHandler() {
67
+ return function (_ref6) {
68
+ var dispatch = _ref6.dispatch;
69
+ return dispatch({
70
+ type: 'redo'
71
+ });
72
+ };
73
+ };
74
+ export var selectHandler = function selectHandler(selection) {
75
+ return function (_ref7) {
76
+ var dispatch = _ref7.dispatch;
77
+ return dispatch({
78
+ type: 'select',
79
+ selection: selection
80
+ });
81
+ };
82
+ };
83
+
84
+ /**
85
+ * 执行函数
86
+ *
87
+ * 接收一个处理函数,获取上下文后执行它
88
+ */
89
+
90
+ /**
91
+ * 键盘事件处理函数
92
+ */
93
+
94
+ export var getDefaultKeyboardEventHandlers = function getDefaultKeyboardEventHandlers() {
95
+ return [
96
+ // 处理 Enter 和 Tab
97
+ function (_ref8) {
98
+ var selection = _ref8.selection,
99
+ dispatch = _ref8.dispatch,
100
+ e = _ref8.e;
101
+ if (!e.altKey && !e.ctrlKey && !e.metaKey && (e.key === 'Enter' || e.key === 'Tab')) {
102
+ e.preventDefault();
103
+ dispatch({
104
+ type: 'insert',
105
+ payload: {
106
+ text: e.key === 'Enter' ? '\n' : '\t'
107
+ },
108
+ selection: selection
109
+ });
110
+ }
111
+ },
112
+ // 处理 Backspace 和 Delete
113
+ function (_ref9) {
114
+ var selection = _ref9.selection,
115
+ dispatch = _ref9.dispatch,
116
+ e = _ref9.e;
117
+ if (!e.altKey && !e.metaKey && (e.key === 'Backspace' || e.key === 'Delete')) {
118
+ e.preventDefault();
119
+ dispatch({
120
+ type: 'delete',
121
+ payload: {
122
+ direction: e.key === 'Backspace' ? 'backward' : 'forward',
123
+ boundary: !e.ctrlKey ? 'char' : 'line'
124
+ },
125
+ selection: selection
126
+ });
127
+ }
128
+ },
129
+ // 处理斜体和粗体
130
+ function (_ref10) {
131
+ var selection = _ref10.selection,
132
+ dispatch = _ref10.dispatch,
133
+ e = _ref10.e;
134
+ if (!e.altKey && e.ctrlKey && !e.metaKey && (e.key === 'i' || e.key === 'b')) {
135
+ e.preventDefault();
136
+ dispatch({
137
+ type: 'toggle',
138
+ payload: {
139
+ type: e.key === 'i' ? 'emphasis' : 'strong'
140
+ },
141
+ selection: selection
142
+ });
143
+ }
144
+ },
145
+ // 处理撤销和恢复
146
+ function (_ref11) {
147
+ var dispatch = _ref11.dispatch,
148
+ e = _ref11.e;
149
+ if (!e.altKey && e.ctrlKey && !e.metaKey && (e.key === 'z' || e.key === 'y')) {
150
+ e.preventDefault();
151
+ dispatch({
152
+ type: e.key === 'z' ? 'undo' : 'redo'
153
+ });
154
+ }
155
+ },
156
+ // 处理左键和右键
157
+ function (_ref12) {
158
+ var sourceCode = _ref12.sourceCode,
159
+ selection = _ref12.selection,
160
+ dispatch = _ref12.dispatch,
161
+ e = _ref12.e;
162
+ if (!e.altKey && !e.ctrlKey && !e.metaKey && (e.key === 'ArrowLeft' || e.key === 'ArrowRight')) {
163
+ e.preventDefault();
164
+ var anchorOffset = selection.anchorOffset,
165
+ focusOffset = selection.focusOffset;
166
+ var _getRange = getRange(selection),
167
+ startOffset = _getRange.startOffset,
168
+ endOffset = _getRange.endOffset;
169
+ if (e.shiftKey) {
170
+ if (e.key === 'ArrowLeft' && focusOffset) {
171
+ dispatch({
172
+ type: 'select',
173
+ selection: createSelection(anchorOffset, focusOffset - 1)
174
+ });
175
+ } else if (e.key === 'ArrowRight' && focusOffset < sourceCode.length) {
176
+ dispatch({
177
+ type: 'select',
178
+ selection: createSelection(anchorOffset, focusOffset + 1)
179
+ });
180
+ }
181
+ } else {
182
+ if (startOffset < endOffset) {
183
+ dispatch({
184
+ type: 'select',
185
+ selection: createSelection(e.key === 'ArrowLeft' ? startOffset : endOffset)
186
+ });
187
+ } else if (e.key === 'ArrowLeft' && startOffset) {
188
+ dispatch({
189
+ type: 'select',
190
+ selection: createSelection(startOffset - 1)
191
+ });
192
+ } else if (e.key === 'ArrowRight' && startOffset < sourceCode.length) {
193
+ dispatch({
194
+ type: 'select',
195
+ selection: createSelection(startOffset + 1)
196
+ });
197
+ }
198
+ }
199
+ }
200
+ }];
201
+ };
202
+
203
+ /**
204
+ * 组装处理函数
205
+ *
206
+ * @param handlers 处理函数数组
207
+ * @returns 组装好的处理函数
208
+ */
209
+ export var composeHandlers = function composeHandlers(handlers) {
210
+ return function (ctx) {
211
+ if (handlers) {
212
+ var _iterator = _createForOfIteratorHelper(handlers),
213
+ _step;
214
+ try {
215
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
216
+ var _handler = _step.value;
217
+ _handler(ctx);
218
+ }
219
+ } catch (err) {
220
+ _iterator.e(err);
221
+ } finally {
222
+ _iterator.f();
223
+ }
224
+ }
225
+ return ctx;
226
+ };
227
+ };
@@ -0,0 +1,59 @@
1
+ /// <reference types="react" />
2
+ import type { ContainerSelection } from './container';
3
+ /**
4
+ * 编辑器状态
5
+ */
6
+ interface State {
7
+ sourceCode: string;
8
+ selection: ContainerSelection;
9
+ }
10
+ export interface InsertAction {
11
+ type: 'insert';
12
+ payload: {
13
+ text: string;
14
+ };
15
+ selection: ContainerSelection;
16
+ }
17
+ export interface DeleteAction {
18
+ type: 'delete';
19
+ payload?: {
20
+ direction: 'backward' | 'forward';
21
+ boundary: 'char' | 'line';
22
+ };
23
+ selection: ContainerSelection;
24
+ }
25
+ export interface ToggleAction {
26
+ type: 'toggle';
27
+ payload: {
28
+ type: 'heading';
29
+ depth: 1 | 2 | 3 | 4 | 5 | 6;
30
+ } | {
31
+ type: 'thematicBreak' | 'blockquote' | 'code' | 'emphasis' | 'strong' | 'inlineCode' | 'delete' | 'math' | 'inlineMath';
32
+ } | {
33
+ type: 'link' | 'image';
34
+ url: string;
35
+ description: string;
36
+ };
37
+ selection: ContainerSelection;
38
+ }
39
+ export interface SetAction {
40
+ type: 'set';
41
+ payload: State;
42
+ selection: ContainerSelection;
43
+ }
44
+ export interface UndoAction {
45
+ type: 'undo';
46
+ }
47
+ export interface RedoAction {
48
+ type: 'redo';
49
+ }
50
+ export interface SelectAction {
51
+ type: 'select';
52
+ selection: ContainerSelection;
53
+ }
54
+ export declare const useHistory: (initialSourceCode: string) => {
55
+ sourceCode: string;
56
+ selection: ContainerSelection;
57
+ dispatch: import("react").Dispatch<InsertAction | DeleteAction | ToggleAction | SetAction | UndoAction | RedoAction | SelectAction>;
58
+ };
59
+ export {};
@@ -0,0 +1,305 @@
1
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
2
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
3
+ 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."); }
4
+ 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; } }
5
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
6
+ 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; }
7
+ 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; }
8
+ 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; }
9
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
10
+ 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); }
11
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
12
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
13
+ 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); }
14
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
15
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
16
+ 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; }
17
+ import { useReducer } from 'react';
18
+ import { createSelection, getRange } from "./container";
19
+
20
+ /**
21
+ * 编辑器状态
22
+ */
23
+
24
+ var stateReducer = function stateReducer(_ref, action) {
25
+ var sourceCode = _ref.sourceCode;
26
+ var _action$selection = action.selection,
27
+ anchorOffset = _action$selection.anchorOffset,
28
+ focusOffset = _action$selection.focusOffset;
29
+ var _getRange = getRange(action.selection),
30
+ startOffset = _getRange.startOffset,
31
+ endOffset = _getRange.endOffset;
32
+ var before = sourceCode.slice(0, startOffset);
33
+ var after = sourceCode.slice(endOffset);
34
+ switch (action.type) {
35
+ case 'insert':
36
+ {
37
+ var text = action.payload.text.replace(/\r\n?/g, '\n');
38
+ return {
39
+ sourceCode: "".concat(before).concat(text).concat(after),
40
+ selection: createSelection(startOffset + text.length)
41
+ };
42
+ }
43
+ case 'delete':
44
+ {
45
+ // 选区不为空
46
+ if (startOffset < endOffset) {
47
+ return {
48
+ sourceCode: "".concat(before).concat(after),
49
+ selection: createSelection(startOffset)
50
+ };
51
+ }
52
+
53
+ // 选区为空,根据删除方向和边界生成新状态
54
+ if (action.payload) {
55
+ var _action$payload = action.payload,
56
+ direction = _action$payload.direction,
57
+ boundary = _action$payload.boundary;
58
+ if (direction === 'backward' && startOffset) {
59
+ var index = before.lastIndexOf('\n');
60
+ var length = boundary === 'char' || index === before.length - 1 ? 1 : before.length - 1 - index;
61
+ return {
62
+ sourceCode: "".concat(before.slice(0, before.length - length)).concat(after),
63
+ selection: createSelection(startOffset - length)
64
+ };
65
+ }
66
+ if (direction === 'forward' && startOffset < sourceCode.length) {
67
+ var _index = "".concat(after, "\n").indexOf('\n');
68
+ var _length = boundary === 'char' || !_index ? 1 : _index;
69
+ return {
70
+ sourceCode: "".concat(before).concat(after.slice(_length)),
71
+ selection: createSelection(startOffset)
72
+ };
73
+ }
74
+ }
75
+ return {
76
+ sourceCode: sourceCode,
77
+ selection: action.selection
78
+ };
79
+ }
80
+ case 'toggle':
81
+ {
82
+ var lineStartOffset = before.lastIndexOf('\n') + 1;
83
+ var lineEndOffset = endOffset + "".concat(after, "\n").indexOf('\n');
84
+ var lineBefore = sourceCode.slice(0, lineStartOffset);
85
+ var lineAfter = sourceCode.slice(lineEndOffset);
86
+ switch (action.payload.type) {
87
+ case 'heading':
88
+ {
89
+ var depth = action.payload.depth;
90
+ var lines = sourceCode.slice(lineStartOffset, lineEndOffset).split('\n');
91
+ 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
+ });
95
+ });
96
+ 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, "} +")), '');
100
+ }
101
+ }
102
+ if (values.some(function (value) {
103
+ return value !== depth;
104
+ })) {
105
+ for (var _i = 0; _i < lines.length; _i++) {
106
+ lines[_i] = "".concat('#'.repeat(depth), " ").concat(lines[_i]);
107
+ }
108
+ }
109
+ var _text = lines.join('\n');
110
+ return {
111
+ sourceCode: "".concat(lineBefore).concat(_text).concat(lineAfter),
112
+ selection: createSelection(lineStartOffset + _text.length)
113
+ };
114
+ }
115
+ case 'thematicBreak':
116
+ {
117
+ return {
118
+ sourceCode: "".concat(sourceCode.slice(0, lineEndOffset), "\n***\n").concat(lineAfter),
119
+ selection: createSelection(lineEndOffset + 5)
120
+ };
121
+ }
122
+ case 'blockquote':
123
+ {
124
+ var _lines = sourceCode.slice(lineStartOffset, lineEndOffset).split('\n');
125
+ var _values = _lines.map(function (line) {
126
+ return /^ {0,3}> */.test(line);
127
+ });
128
+ for (var _i2 = 0; _i2 < _values.length; _i2++) {
129
+ if (_values[_i2]) {
130
+ _lines[_i2] = _lines[_i2].replace(/^ {0,3}> */, '');
131
+ }
132
+ }
133
+ if (_values.some(function (value) {
134
+ return !value;
135
+ })) {
136
+ for (var _i3 = 0; _i3 < _lines.length; _i3++) {
137
+ _lines[_i3] = "> ".concat(_lines[_i3]);
138
+ }
139
+ }
140
+ var _text2 = _lines.join('\n');
141
+ return {
142
+ sourceCode: "".concat(lineBefore).concat(_text2).concat(lineAfter),
143
+ selection: createSelection(lineStartOffset + _text2.length)
144
+ };
145
+ }
146
+ case 'code':
147
+ case 'math':
148
+ {
149
+ var type = action.payload.type;
150
+ var delimiter = type === 'code' ? '```' : '$$';
151
+ return {
152
+ sourceCode: "".concat(sourceCode.slice(0, lineEndOffset), "\n").concat(delimiter, "\n\n").concat(delimiter, "\n").concat(lineAfter),
153
+ selection: createSelection(lineEndOffset + delimiter.length + 2)
154
+ };
155
+ }
156
+ case 'emphasis':
157
+ case 'strong':
158
+ case 'inlineCode':
159
+ case 'delete':
160
+ case 'inlineMath':
161
+ {
162
+ var _type = action.payload.type;
163
+ var delimiters = _type === 'emphasis' ? ['*', '_'] : _type === 'strong' ? ['**', '__'] : _type === 'inlineCode' ? ['`'] : _type === 'delete' ? ['~~'] : ['$'];
164
+ var _text3 = sourceCode.slice(startOffset, endOffset);
165
+ for (var _i4 = 0, _delimiters = delimiters; _i4 < _delimiters.length; _i4++) {
166
+ var _delimiter = _delimiters[_i4];
167
+ if (before.endsWith(_delimiter) && after.startsWith(_delimiter)) {
168
+ // 删除分隔文本
169
+ return {
170
+ sourceCode: "".concat(before.slice(0, before.length - _delimiter.length)).concat(_text3).concat(after.slice(_delimiter.length)),
171
+ selection: createSelection(anchorOffset - _delimiter.length, focusOffset - _delimiter.length)
172
+ };
173
+ }
174
+ }
175
+ var _delimiter2 = delimiters[0];
176
+ // 插入分隔文本
177
+ return {
178
+ sourceCode: "".concat(before).concat(_delimiter2).concat(_text3).concat(_delimiter2).concat(after),
179
+ selection: createSelection(anchorOffset + _delimiter2.length, focusOffset + _delimiter2.length)
180
+ };
181
+ }
182
+ case 'link':
183
+ case 'image':
184
+ {
185
+ var _action$payload2 = action.payload,
186
+ _type2 = _action$payload2.type,
187
+ url = _action$payload2.url,
188
+ description = _action$payload2.description;
189
+ return {
190
+ sourceCode: "".concat(before).concat(_type2 === 'link' ? '' : '!', "[").concat(description, "](").concat(url, ")").concat(after),
191
+ selection: createSelection(startOffset + url.length + description.length + 4 + (_type2 === 'link' ? 0 : 1))
192
+ };
193
+ }
194
+ default:
195
+ {
196
+ var _ = action.payload;
197
+ return _;
198
+ }
199
+ }
200
+ }
201
+ case 'set':
202
+ {
203
+ var _action$payload3 = action.payload,
204
+ _sourceCode = _action$payload3.sourceCode,
205
+ selection = _action$payload3.selection;
206
+ return {
207
+ sourceCode: _sourceCode.replace(/\r\n?/g, '\n'),
208
+ selection: selection
209
+ };
210
+ }
211
+ default:
212
+ {
213
+ var _2 = action;
214
+ return _2;
215
+ }
216
+ }
217
+ };
218
+
219
+ /**
220
+ * 编辑器历史
221
+ */
222
+
223
+ var historyReducer = function historyReducer(_ref2, action) {
224
+ var states = _ref2.states,
225
+ index = _ref2.index,
226
+ selection = _ref2.selection;
227
+ switch (action.type) {
228
+ case 'undo':
229
+ {
230
+ if (!index) {
231
+ return {
232
+ states: states,
233
+ index: index,
234
+ selection: selection
235
+ };
236
+ }
237
+ return {
238
+ states: states,
239
+ index: index - 1,
240
+ selection: states[index].actionSelection
241
+ };
242
+ }
243
+ case 'redo':
244
+ {
245
+ if (index === states.length - 1) {
246
+ return {
247
+ states: states,
248
+ index: index,
249
+ selection: selection
250
+ };
251
+ }
252
+ return {
253
+ states: states,
254
+ index: index + 1,
255
+ selection: states[index + 1].selection
256
+ };
257
+ }
258
+ case 'select':
259
+ {
260
+ return {
261
+ states: states,
262
+ index: index,
263
+ selection: action.selection
264
+ };
265
+ }
266
+ default:
267
+ {
268
+ var state = stateReducer(states[index], action);
269
+ // 只有源代码或选区改变时才记录历史
270
+ if (state.sourceCode === states[index].sourceCode && state.selection === action.selection) {
271
+ return {
272
+ states: states,
273
+ index: index,
274
+ selection: selection
275
+ };
276
+ }
277
+ return {
278
+ states: [].concat(_toConsumableArray(states.slice(index < 32 ? 0 : index - 31, index + 1)), [_objectSpread(_objectSpread({}, state), {}, {
279
+ actionSelection: action.selection
280
+ })]),
281
+ index: index < 32 ? index + 1 : 32,
282
+ selection: state.selection
283
+ };
284
+ }
285
+ }
286
+ };
287
+ export var useHistory = function useHistory(initialSourceCode) {
288
+ var _useReducer = useReducer(historyReducer, {
289
+ states: [{
290
+ sourceCode: initialSourceCode,
291
+ selection: createSelection(0),
292
+ actionSelection: createSelection(0)
293
+ }],
294
+ index: 0,
295
+ selection: createSelection(0)
296
+ }),
297
+ _useReducer2 = _slicedToArray(_useReducer, 2),
298
+ history = _useReducer2[0],
299
+ dispatch = _useReducer2[1];
300
+ return {
301
+ sourceCode: history.states[history.index].sourceCode,
302
+ selection: history.selection,
303
+ dispatch: dispatch
304
+ };
305
+ };
@@ -0,0 +1,7 @@
1
+ import type { Fragment, Jsx } from 'hast-util-to-jsx-runtime';
2
+
3
+ declare module 'react/jsx-runtime' {
4
+ export const Fragment: Fragment;
5
+ export const jsx: Jsx;
6
+ export const jsxs: Jsx;
7
+ }
@@ -0,0 +1,38 @@
1
+ /// <reference types="react" />
2
+ import type { Components } from 'hast-util-to-jsx-runtime';
3
+ /**
4
+ * 将 Markdown 文本解析为 Mdast 树并转换为 Hast 树的处理器
5
+ */
6
+ declare const processor: import("unified").FrozenProcessor<import("mdast").Root, import("hast").Root, import("hast").Root, void>;
7
+ /**
8
+ * Mdast 节点
9
+ */
10
+ export type MdastNode = ReturnType<(typeof processor)['parse']>['children'][number];
11
+ /**
12
+ * 将 Markdown 文本映射到 DOM 树,第一层为 `<div>` 块元素,第二层为 `<div>` 行元素,之后均为 `<span>` 元素
13
+ *
14
+ * @param sourceCode Markdown 文本
15
+ * @returns DOM 树
16
+ */
17
+ export declare const editorRender: (sourceCode: string) => HTMLDivElement;
18
+ export interface ViewerOptions {
19
+ /**
20
+ * 自定义 HTML 元素
21
+ */
22
+ customHTMLElements: Partial<Components>;
23
+ /**
24
+ * 自定义块
25
+ */
26
+ customBlocks: Partial<Record<string, React.ComponentType<{
27
+ children: string;
28
+ }>>>;
29
+ }
30
+ /**
31
+ * 将 Markdown 文本映射到 React 虚拟 DOM 树
32
+ *
33
+ * @param sourceCode Markdown 文本
34
+ * @param options 配置项
35
+ * @returns React 虚拟 DOM 树
36
+ */
37
+ export declare const viewerRender: (sourceCode: string, options: ViewerOptions) => JSX.Element;
38
+ export {};