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.
@@ -1,6 +1,5 @@
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; }
1
+ import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
2
+ import { createKeybindingsHandler } from 'tinykeys';
4
3
  import { createSelection, getRange } from "./container";
5
4
 
6
5
  /**
@@ -92,111 +91,255 @@ export var selectHandler = function selectHandler(selection) {
92
91
  */
93
92
 
94
93
  export var getDefaultKeyboardEventHandlers = function getDefaultKeyboardEventHandlers() {
95
- return [
96
- // 处理 Enter 和 Tab
97
- function (_ref8) {
98
- var selection = _ref8.selection,
94
+ return [function (_ref8) {
95
+ var sourceCode = _ref8.sourceCode,
96
+ selection = _ref8.selection,
99
97
  dispatch = _ref8.dispatch,
100
98
  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) {
99
+ return createKeybindingsHandler({
100
+ Tab: function Tab(e) {
101
+ e.preventDefault();
102
+ dispatch({
103
+ type: 'insert',
104
+ payload: {
105
+ text: '\t'
106
+ },
107
+ selection: selection
108
+ });
109
+ },
110
+ Backspace: function Backspace(e) {
111
+ e.preventDefault();
112
+ dispatch({
113
+ type: 'delete',
114
+ payload: {
115
+ direction: 'backward',
116
+ boundary: 'char'
117
+ },
118
+ selection: selection
119
+ });
120
+ },
121
+ '$mod+Backspace': function $modBackspace(e) {
122
+ e.preventDefault();
123
+ dispatch({
124
+ type: 'delete',
125
+ payload: {
126
+ direction: 'backward',
127
+ boundary: 'line'
128
+ },
129
+ selection: selection
130
+ });
131
+ },
132
+ 'Shift+Backspace': function ShiftBackspace(e) {
133
+ e.preventDefault();
134
+ dispatch({
135
+ type: 'delete',
136
+ payload: {
137
+ direction: 'backward',
138
+ boundary: 'char'
139
+ },
140
+ selection: selection
141
+ });
142
+ },
143
+ 'Meta+Backspace': function MetaBackspace(e) {
144
+ return e.preventDefault();
145
+ },
146
+ 'Alt+Backspace': function AltBackspace(e) {
147
+ return e.preventDefault();
148
+ },
149
+ 'Control+Backspace': function ControlBackspace(e) {
150
+ return e.preventDefault();
151
+ },
152
+ 'Shift+Meta+Backspace': function ShiftMetaBackspace(e) {
153
+ return e.preventDefault();
154
+ },
155
+ 'Shift+Alt+Backspace': function ShiftAltBackspace(e) {
156
+ return e.preventDefault();
157
+ },
158
+ 'Shift+Control+Backspace': function ShiftControlBackspace(e) {
159
+ return e.preventDefault();
160
+ },
161
+ 'Meta+Alt+Backspace': function MetaAltBackspace(e) {
162
+ return e.preventDefault();
163
+ },
164
+ 'Meta+Control+Backspace': function MetaControlBackspace(e) {
165
+ return e.preventDefault();
166
+ },
167
+ 'Alt+Control+Backspace': function AltControlBackspace(e) {
168
+ return e.preventDefault();
169
+ },
170
+ 'Shift+Meta+Alt+Backspace': function ShiftMetaAltBackspace(e) {
171
+ return e.preventDefault();
172
+ },
173
+ 'Shift+Meta+Control+Backspace': function ShiftMetaControlBackspace(e) {
174
+ return e.preventDefault();
175
+ },
176
+ 'Shift+Alt+Control+Backspace': function ShiftAltControlBackspace(e) {
177
+ return e.preventDefault();
178
+ },
179
+ 'Meta+Alt+Control+Backspace': function MetaAltControlBackspace(e) {
180
+ return e.preventDefault();
181
+ },
182
+ Delete: function Delete(e) {
183
+ e.preventDefault();
184
+ dispatch({
185
+ type: 'delete',
186
+ payload: {
187
+ direction: 'forward',
188
+ boundary: 'char'
189
+ },
190
+ selection: selection
191
+ });
192
+ },
193
+ '$mod+Delete': function $modDelete(e) {
194
+ e.preventDefault();
195
+ dispatch({
196
+ type: 'delete',
197
+ payload: {
198
+ direction: 'forward',
199
+ boundary: 'line'
200
+ },
201
+ selection: selection
202
+ });
203
+ },
204
+ 'Shift+Delete': function ShiftDelete(e) {
205
+ e.preventDefault();
206
+ dispatch({
207
+ type: 'delete',
208
+ payload: {
209
+ direction: 'forward',
210
+ boundary: 'char'
211
+ },
212
+ selection: selection
213
+ });
214
+ },
215
+ 'Meta+Delete': function MetaDelete(e) {
216
+ return e.preventDefault();
217
+ },
218
+ 'Alt+Delete': function AltDelete(e) {
219
+ return e.preventDefault();
220
+ },
221
+ 'Control+Delete': function ControlDelete(e) {
222
+ return e.preventDefault();
223
+ },
224
+ 'Shift+Meta+Delete': function ShiftMetaDelete(e) {
225
+ return e.preventDefault();
226
+ },
227
+ 'Shift+Alt+Delete': function ShiftAltDelete(e) {
228
+ return e.preventDefault();
229
+ },
230
+ 'Shift+Control+Delete': function ShiftControlDelete(e) {
231
+ return e.preventDefault();
232
+ },
233
+ 'Meta+Alt+Delete': function MetaAltDelete(e) {
234
+ return e.preventDefault();
235
+ },
236
+ 'Meta+Control+Delete': function MetaControlDelete(e) {
237
+ return e.preventDefault();
238
+ },
239
+ 'Alt+Control+Delete': function AltControlDelete(e) {
240
+ return e.preventDefault();
241
+ },
242
+ 'Shift+Meta+Alt+Delete': function ShiftMetaAltDelete(e) {
243
+ return e.preventDefault();
244
+ },
245
+ 'Shift+Meta+Control+Delete': function ShiftMetaControlDelete(e) {
246
+ return e.preventDefault();
247
+ },
248
+ 'Shift+Alt+Control+Delete': function ShiftAltControlDelete(e) {
249
+ return e.preventDefault();
250
+ },
251
+ 'Meta+Alt+Control+Delete': function MetaAltControlDelete(e) {
252
+ return e.preventDefault();
253
+ },
254
+ '$mod+KeyI': function $modKeyI(e) {
255
+ e.preventDefault();
256
+ dispatch({
257
+ type: 'toggle',
258
+ payload: {
259
+ type: 'emphasis'
260
+ },
261
+ selection: selection
262
+ });
263
+ },
264
+ '$mod+KeyB': function $modKeyB(e) {
265
+ e.preventDefault();
266
+ dispatch({
267
+ type: 'toggle',
268
+ payload: {
269
+ type: 'strong'
270
+ },
271
+ selection: selection
272
+ });
273
+ },
274
+ '$mod+KeyZ': function $modKeyZ(e) {
275
+ e.preventDefault();
276
+ dispatch({
277
+ type: 'undo'
278
+ });
279
+ },
280
+ '$mod+KeyY': function $modKeyY(e) {
281
+ e.preventDefault();
282
+ dispatch({
283
+ type: 'redo'
284
+ });
285
+ },
286
+ ArrowLeft: function ArrowLeft(e) {
287
+ e.preventDefault();
288
+ var _getRange = getRange(selection),
289
+ startOffset = _getRange.startOffset,
290
+ endOffset = _getRange.endOffset;
291
+ if (startOffset < endOffset) {
171
292
  dispatch({
172
293
  type: 'select',
173
- selection: createSelection(anchorOffset, focusOffset - 1)
294
+ selection: createSelection(startOffset)
174
295
  });
175
- } else if (e.key === 'ArrowRight' && focusOffset < sourceCode.length) {
296
+ } else if (startOffset) {
176
297
  dispatch({
177
298
  type: 'select',
178
- selection: createSelection(anchorOffset, focusOffset + 1)
299
+ selection: createSelection(startOffset - 1)
179
300
  });
180
301
  }
181
- } else {
182
- if (startOffset < endOffset) {
302
+ },
303
+ 'Shift+ArrowLeft': function ShiftArrowLeft(e) {
304
+ e.preventDefault();
305
+ var anchorOffset = selection.anchorOffset,
306
+ focusOffset = selection.focusOffset;
307
+ if (focusOffset) {
183
308
  dispatch({
184
309
  type: 'select',
185
- selection: createSelection(e.key === 'ArrowLeft' ? startOffset : endOffset)
310
+ selection: createSelection(anchorOffset, focusOffset - 1)
186
311
  });
187
- } else if (e.key === 'ArrowLeft' && startOffset) {
312
+ }
313
+ },
314
+ ArrowRight: function ArrowRight(e) {
315
+ e.preventDefault();
316
+ var _getRange2 = getRange(selection),
317
+ startOffset = _getRange2.startOffset,
318
+ endOffset = _getRange2.endOffset;
319
+ if (startOffset < endOffset) {
188
320
  dispatch({
189
321
  type: 'select',
190
- selection: createSelection(startOffset - 1)
322
+ selection: createSelection(endOffset)
191
323
  });
192
- } else if (e.key === 'ArrowRight' && startOffset < sourceCode.length) {
324
+ } else if (startOffset < sourceCode.length) {
193
325
  dispatch({
194
326
  type: 'select',
195
327
  selection: createSelection(startOffset + 1)
196
328
  });
197
329
  }
330
+ },
331
+ 'Shift+ArrowRight': function ShiftArrowRight(e) {
332
+ e.preventDefault();
333
+ var anchorOffset = selection.anchorOffset,
334
+ focusOffset = selection.focusOffset;
335
+ if (focusOffset < sourceCode.length) {
336
+ dispatch({
337
+ type: 'select',
338
+ selection: createSelection(anchorOffset, focusOffset + 1)
339
+ });
340
+ }
198
341
  }
199
- }
342
+ })(e.nativeEvent);
200
343
  }];
201
344
  };
202
345
 
@@ -1,19 +1,6 @@
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; }
1
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
3
+ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
17
4
  import { useReducer } from 'react';
18
5
  import { createSelection, getRange } from "./container";
19
6
 
@@ -1,16 +1,6 @@
1
1
  /// <reference types="react" />
2
- import type { Components } from 'hast-util-to-jsx-runtime';
3
2
  import { defaultSchema } from 'rehype-sanitize';
4
- type Schema = typeof defaultSchema;
5
- export declare const getDefaultSchema: () => Schema;
6
- /**
7
- * 将 Markdown 文本解析为 Mdast 树并转换为 Hast 树的处理器
8
- */
9
- declare const processor: import("unified").FrozenProcessor<import("mdast").Root, import("hast").Root, import("hast").Root, void>;
10
- /**
11
- * Mdast 节点
12
- */
13
- export type MdastNode = ReturnType<(typeof processor)['parse']>['children'][number];
3
+ import type { ViewerOptions } from '../x-star-md-viewer';
14
4
  /**
15
5
  * 将 Markdown 文本映射到 DOM 树,第一层为 `<div>` 块元素,第二层为 `<div>` 行元素,之后均为 `<span>` 元素
16
6
  *
@@ -18,30 +8,42 @@ export type MdastNode = ReturnType<(typeof processor)['parse']>['children'][numb
18
8
  * @returns DOM 树
19
9
  */
20
10
  export declare const editorRender: (sourceCode: string) => HTMLDivElement;
21
- export interface ViewerOptions {
22
- /**
23
- * 自定义过滤模式
24
- */
25
- customSchema: Schema;
26
- /**
27
- * 自定义 HTML 元素
28
- */
29
- customHTMLElements: Partial<Components>;
30
- /**
31
- * 自定义块
32
- */
33
- customBlocks: Partial<Record<string, React.ComponentType<{
34
- children: string;
35
- }>>>;
36
- }
37
11
  /**
38
- * 将 Markdown 文本映射到 React 虚拟 DOM 树
12
+ * 将 Mdast 树转成 Hast 树的处理器
13
+ */
14
+ declare const toHastProcessor: import("unified").FrozenProcessor<import("mdast").Root, import("hast").Root, import("hast").Root, void>;
15
+ /**
16
+ * Hast 根节点
17
+ */
18
+ export type HastRoot = ReturnType<(typeof toHastProcessor)['runSync']>;
19
+ /**
20
+ * 将 Markdown 文本转成 Hast 树
39
21
  *
40
22
  * @param sourceCode Markdown 文本
23
+ * @returns Hast 树
24
+ */
25
+ export declare const preViewerRender: (sourceCode: string) => import("hast").Root;
26
+ /**
27
+ * 过滤模式
28
+ */
29
+ export type Schema = typeof defaultSchema;
30
+ export declare const getDefaultSchema: () => Schema;
31
+ /**
32
+ * 将 Hast 树转成新的 Hast 树
33
+ *
34
+ * @param root Hast 树
35
+ * @param schema 过滤模式
36
+ * @returns 新的 Hast 树
37
+ */
38
+ export declare const viewerRender: (root: HastRoot, schema: Schema) => import("hast").Root;
39
+ /**
40
+ * 将 Hast 树映射到 React 虚拟 DOM 树
41
+ *
42
+ * @param root Hast 树
41
43
  * @param options 配置项
42
44
  * @returns React 虚拟 DOM 树
43
45
  */
44
- export declare const viewerRender: (sourceCode: string, options: ViewerOptions) => JSX.Element;
46
+ export declare const postViewerRender: (root: HastRoot, options: ViewerOptions) => JSX.Element;
45
47
  /**
46
48
  * 将 Markdown 文本转成 HTML 文本
47
49
  *