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
@@ -1,15 +1,15 @@
1
1
  import React from 'react';
2
- import type { ToolbarItem } from '../components/Toolbar';
2
+ import type { ToolbarItemMap, ToolbarItems } from '../components/Toolbar';
3
3
  import type { Executor, KeyboardEventHandler } from '../utils/handler';
4
4
  interface EditorOptions {
5
5
  /**
6
6
  * 工具栏项映射
7
7
  */
8
- toolbarItemMap: Partial<Record<string, ToolbarItem>>;
8
+ toolbarItemMap: ToolbarItemMap;
9
9
  /**
10
10
  * 工具栏项
11
11
  */
12
- toolbarItems: (string | ToolbarItem)[][];
12
+ toolbarItems: ToolbarItems;
13
13
  /**
14
14
  * 键盘事件处理函数
15
15
  */
@@ -23,13 +23,41 @@ export interface XStarMdEditorHandle {
23
23
  getContainer: () => HTMLDivElement | null;
24
24
  }
25
25
  export interface XStarMdEditorProps {
26
+ /**
27
+ * CSS 类名
28
+ */
26
29
  className?: string;
30
+ /**
31
+ * CSS 样式
32
+ */
27
33
  style?: React.CSSProperties;
34
+ /**
35
+ * 工具栏 CSS 类名
36
+ */
28
37
  toolbarClassName?: string;
38
+ /**
39
+ * 工具栏 CSS 样式
40
+ */
29
41
  toolbarStyle?: React.CSSProperties;
42
+ /**
43
+ * 语言
44
+ */
45
+ locale?: string;
46
+ /**
47
+ * 初始文本
48
+ */
30
49
  initialValue?: string;
50
+ /**
51
+ * 插件
52
+ */
31
53
  plugins?: XStarMdEditorPlugin[];
54
+ /**
55
+ * 文本变化回调函数
56
+ */
32
57
  onChange?: (value: string) => void;
58
+ /**
59
+ * 文件插入回调函数
60
+ */
33
61
  onInsertFile?: (file: File, options: {
34
62
  description: string;
35
63
  image: boolean;
@@ -1,7 +1,8 @@
1
1
  import classNames from 'classnames';
2
2
  import React, { useEffect, useImperativeHandle, useRef } from 'react';
3
3
  import Toolbar, { getDefaultToolbarItemMap, getDefaultToolbarItems } from "../components/Toolbar";
4
- import { createSelection, getRange, useContainer } from "../utils/container";
4
+ import { LocaleProvider } from "../locales";
5
+ import { getRange, useContainer } from "../utils/container";
5
6
  import { prefix } from "../utils/global";
6
7
  import { composeHandlers, getDefaultKeyboardEventHandlers } from "../utils/handler";
7
8
  import { useHistory } from "../utils/history";
@@ -10,6 +11,7 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
10
11
  style = _ref.style,
11
12
  toolbarClassName = _ref.toolbarClassName,
12
13
  toolbarStyle = _ref.toolbarStyle,
14
+ locale = _ref.locale,
13
15
  _ref$initialValue = _ref.initialValue,
14
16
  initialValue = _ref$initialValue === void 0 ? '' : _ref$initialValue,
15
17
  plugins = _ref.plugins,
@@ -17,6 +19,7 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
17
19
  onInsertFile = _ref.onInsertFile;
18
20
  var container = useContainer();
19
21
  var _useHistory = useHistory(initialValue),
22
+ history = _useHistory.history,
20
23
  sourceCode = _useHistory.sourceCode,
21
24
  selection = _useHistory.selection,
22
25
  dispatch = _useHistory.dispatch;
@@ -47,13 +50,15 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
47
50
  initialized.current = false;
48
51
  };
49
52
  }, []);
53
+ var historyLatest = useRef(history);
54
+ historyLatest.current = history;
50
55
  var sourceCodeLatest = useRef(sourceCode);
51
56
  sourceCodeLatest.current = sourceCode;
52
57
 
53
58
  /**
54
59
  * 失焦时的选区
55
60
  */
56
- var blurSelection = useRef();
61
+ var blurSelection = useRef(selection);
57
62
 
58
63
  /**
59
64
  * 获取选区,如果焦点不在容器内,则返回失焦时的选区
@@ -61,12 +66,12 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
61
66
  * @returns 选区
62
67
  */
63
68
  var getSelection = function getSelection() {
64
- var _blurSelection$curren;
65
69
  var selection = container.getSelection();
66
- return selection.anchorOffset === -1 || selection.focusOffset === -1 ? (_blurSelection$curren = blurSelection.current) !== null && _blurSelection$curren !== void 0 ? _blurSelection$curren : createSelection(sourceCodeLatest.current.length) : selection;
70
+ return selection.anchorOffset === -1 || selection.focusOffset === -1 ? blurSelection.current : selection;
67
71
  };
68
72
  var exec = useRef(function (handler) {
69
73
  return handler({
74
+ history: historyLatest.current,
70
75
  sourceCode: sourceCodeLatest.current,
71
76
  selection: getSelection(),
72
77
  dispatch: dispatch
@@ -141,7 +146,8 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
141
146
  },
142
147
  selection: compositionStartSelection.current
143
148
  });
144
- setTimeout(function () {
149
+ // 确保组合事件屏蔽表单和键盘事件
150
+ window.setTimeout(function () {
145
151
  return compositionStartSelection.current = undefined;
146
152
  });
147
153
  }
@@ -159,7 +165,7 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
159
165
  case 'drop':
160
166
  {
161
167
  var _selection = getSelection();
162
- setTimeout(function () {
168
+ window.setTimeout(function () {
163
169
  container.normalize();
164
170
  dispatch({
165
171
  type: 'set',
@@ -179,7 +185,7 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
179
185
  case 'blur':
180
186
  {
181
187
  blurSelection.current = getSelection();
182
- setTimeout(function () {
188
+ window.setTimeout(function () {
183
189
  compositionStartSelection.current = undefined;
184
190
  container.setText(sourceCodeLatest.current);
185
191
  });
@@ -195,13 +201,11 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
195
201
  case 'beforeinput':
196
202
  {
197
203
  e.preventDefault();
198
- var _ref2 = e,
199
- data = _ref2.data;
200
- if (data) {
204
+ if ('data' in e && typeof e.data === 'string') {
201
205
  dispatch({
202
206
  type: 'insert',
203
207
  payload: {
204
- text: data
208
+ text: e.data
205
209
  },
206
210
  selection: getSelection()
207
211
  });
@@ -211,7 +215,7 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
211
215
  }
212
216
  };
213
217
  var _composeHandlers = composeHandlers(plugins)({
214
- toolbarItemMap: getDefaultToolbarItemMap(onInsertFile),
218
+ toolbarItemMap: getDefaultToolbarItemMap(locale, onInsertFileLatest.current),
215
219
  toolbarItems: getDefaultToolbarItems(),
216
220
  keyboardEventHandlers: getDefaultKeyboardEventHandlers()
217
221
  }),
@@ -226,6 +230,7 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
226
230
  case 'keydown':
227
231
  {
228
232
  composeHandlers(keyboardEventHandlers)({
233
+ history: historyLatest.current,
229
234
  sourceCode: sourceCodeLatest.current,
230
235
  selection: getSelection(),
231
236
  dispatch: dispatch,
@@ -235,7 +240,9 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
235
240
  }
236
241
  }
237
242
  };
238
- return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Toolbar, {
243
+ return /*#__PURE__*/React.createElement(LocaleProvider, {
244
+ locale: locale
245
+ }, /*#__PURE__*/React.createElement(Toolbar, {
239
246
  className: classNames(toolbarClassName),
240
247
  style: toolbarStyle,
241
248
  itemMap: toolbarItemMap,
@@ -257,7 +264,9 @@ var XStarMdEditor = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
257
264
  onPaste: clipboardEventHandler
258
265
  }));
259
266
  });
260
- XStarMdEditor.displayName = 'XStarMdEditor';
267
+ if (process.env.NODE_ENV !== 'production') {
268
+ XStarMdEditor.displayName = 'XStarMdEditor';
269
+ }
261
270
  export default XStarMdEditor;
262
271
  export var useMdEditorRef = function useMdEditorRef() {
263
272
  return useRef(null);
@@ -7,9 +7,21 @@ export interface XStarMdViewerHandle {
7
7
  getContainer: () => HTMLDivElement | null;
8
8
  }
9
9
  export interface XStarMdViewerProps {
10
+ /**
11
+ * CSS 类名
12
+ */
10
13
  className?: string;
14
+ /**
15
+ * CSS 样式
16
+ */
11
17
  style?: React.CSSProperties;
18
+ /**
19
+ * 文本
20
+ */
12
21
  value?: string;
22
+ /**
23
+ * 插件
24
+ */
13
25
  plugins?: XStarMdViewerPlugin[];
14
26
  }
15
27
  declare const XStarMdViewer: React.ForwardRefExoticComponent<XStarMdViewerProps & React.RefAttributes<XStarMdViewerHandle>>;
@@ -27,7 +27,9 @@ var XStarMdViewer = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
27
27
  customBlocks: {}
28
28
  })));
29
29
  });
30
- XStarMdViewer.displayName = 'XStarMdViewer';
30
+ if (process.env.NODE_ENV !== 'production') {
31
+ XStarMdViewer.displayName = 'XStarMdViewer';
32
+ }
31
33
  export default XStarMdViewer;
32
34
  export var useMdViewerRef = function useMdViewerRef() {
33
35
  return useRef(null);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "x-star-editor",
3
- "version": "0.0.9",
3
+ "version": "0.1.0",
4
4
  "description": "An editor developed by turingstar",
5
5
  "license": "MIT",
6
6
  "module": "dist/index.js",
@@ -9,13 +9,13 @@
9
9
  "dist"
10
10
  ],
11
11
  "scripts": {
12
- "build": "father build && sass src/styles/index.scss dist/styles/index.css --no-source-map",
12
+ "build": "father build && sass src:dist --no-source-map",
13
13
  "build:watch": "father dev",
14
14
  "dev": "dumi dev",
15
15
  "docs:build": "dumi build",
16
16
  "doctor": "father doctor",
17
17
  "lint": "npm run lint:es && npm run lint:css",
18
- "lint:css": "stylelint \"{src,test}/**/*.{css,less}\"",
18
+ "lint:css": "stylelint \"{src,test}/**/*.{css,scss}\"",
19
19
  "lint:es": "eslint \"{src,test}/**/*.{js,jsx,ts,tsx}\"",
20
20
  "prepare": "husky install && dumi setup",
21
21
  "prepublishOnly": "father doctor && npm run build",
@@ -30,7 +30,7 @@
30
30
  "*.{md,json}": [
31
31
  "prettier --write --no-error-on-unmatched-pattern"
32
32
  ],
33
- "*.{css,less}": [
33
+ "*.{css,scss}": [
34
34
  "stylelint --fix",
35
35
  "prettier --write"
36
36
  ],
@@ -58,6 +58,7 @@
58
58
  "remark-parse": "^10.0.2",
59
59
  "remark-rehype": "^10.1.0",
60
60
  "remark-stringify": "^10.0.3",
61
+ "strip-markdown": "^5.0.1",
61
62
  "unified": "^10.1.2"
62
63
  },
63
64
  "devDependencies": {
@@ -80,6 +81,7 @@
80
81
  "react-dom": "^18.0.0",
81
82
  "styled-components": "^6.0.7",
82
83
  "stylelint": "^14.9.1",
84
+ "stylelint-config-standard-scss": "^6.1.0",
83
85
  "typescript": "^5.1.6",
84
86
  "webpack": "^5.88.2"
85
87
  },
@@ -1 +0,0 @@
1
- <?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1681626310611" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="8363" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><path d="M697.8 481.4c33.6-35 54.2-82.3 54.2-134.3v-10.2C752 229.3 663.9 142 555.3 142H259.4c-15.1 0-27.4 12.3-27.4 27.4v679.1c0 16.3 13.2 29.5 29.5 29.5h318.7c117 0 211.8-94.2 211.8-210.5v-11c0-73-37.4-137.3-94.2-175.1zM328 238h224.7c57.1 0 103.3 44.4 103.3 99.3v9.5c0 54.8-46.3 99.3-103.3 99.3H328V238z m366.6 429.4c0 62.9-51.7 113.9-115.5 113.9H328V542.7h251.1c63.8 0 115.5 51 115.5 113.9v10.8z" p-id="8364"></path></svg>
@@ -1 +0,0 @@
1
- <?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1681627986714" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="9340" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><path d="M516 673c0 4.4 3.4 8 7.5 8h185c4.1 0 7.5-3.6 7.5-8v-48c0-4.4-3.4-8-7.5-8h-185c-4.1 0-7.5 3.6-7.5 8v48zM321.1 679.1l192-161c3.8-3.2 3.8-9.1 0-12.3l-192-160.9c-5.2-4.4-13.1-0.7-13.1 6.1v62.7c0 2.4 1 4.6 2.9 6.1L420.7 512l-109.8 92.2c-1.8 1.5-2.9 3.8-2.9 6.1V673c0 6.8 7.9 10.5 13.1 6.1z" p-id="9341"></path><path d="M880 112H144c-17.7 0-32 14.3-32 32v736c0 17.7 14.3 32 32 32h736c17.7 0 32-14.3 32-32V144c0-17.7-14.3-32-32-32z m-40 728H184V184h656v656z" p-id="9342"></path></svg>
@@ -1 +0,0 @@
1
- <?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1681626574917" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="8502" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><path d="M798 160H366c-4.4 0-8 3.6-8 8v64c0 4.4 3.6 8 8 8h181.2l-156 544H229c-4.4 0-8 3.6-8 8v64c0 4.4 3.6 8 8 8h432c4.4 0 8-3.6 8-8v-64c0-4.4-3.6-8-8-8H474.4l156-544H798c4.4 0 8-3.6 8-8v-64c0-4.4-3.6-8-8-8z" p-id="8503"></path></svg>
@@ -1 +0,0 @@
1
- <?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1681626588137" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="8781" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><path d="M952 474H569.9c-10-2-20.5-4-31.6-6-15.9-2.9-22.2-4.1-30.8-5.8-51.3-10-82.2-20-106.8-34.2-35.1-20.5-52.2-48.3-52.2-85.1 0-37 15.2-67.7 44-89 28.4-21 68.8-32.1 116.8-32.1 54.8 0 97.1 14.4 125.8 42.8 14.6 14.4 25.3 32.1 31.8 52.6 1.3 4.1 2.8 10 4.3 17.8 0.9 4.8 5.2 8.2 9.9 8.2h72.8c5.6 0 10.1-4.6 10.1-10.1v-1c-0.7-6.8-1.3-12.1-2-16-7.3-43.5-28-81.7-59.7-110.3-44.4-40.5-109.7-61.8-188.7-61.8-72.3 0-137.4 18.1-183.3 50.9-25.6 18.4-45.4 41.2-58.6 67.7-13.5 27.1-20.3 58.4-20.3 92.9 0 29.5 5.7 54.5 17.3 76.5 8.3 15.7 19.6 29.5 34.1 42H72c-4.4 0-8 3.6-8 8v60c0 4.4 3.6 8 8 8h433.2c2.1 0.4 3.9 0.8 5.9 1.2 30.9 6.2 49.5 10.4 66.6 15.2 23 6.5 40.6 13.3 55.2 21.5 35.8 20.2 53.3 49.2 53.3 89 0 35.3-15.5 66.8-43.6 88.8-30.5 23.9-75.6 36.4-130.5 36.4-43.7 0-80.7-8.5-110.2-25-29.1-16.3-49.1-39.8-59.7-69.5-0.8-2.2-1.7-5.2-2.7-9-1.2-4.4-5.3-7.5-9.7-7.5h-79.7c-5.6 0-10.1 4.6-10.1 10.1v1c0.2 2.3 0.4 4.2 0.6 5.7 6.5 48.8 30.3 88.8 70.7 118.8 47.1 34.8 113.4 53.2 191.8 53.2 84.2 0 154.8-19.8 204.2-57.3 25-18.9 44.2-42.2 57.1-69 13-27.1 19.7-57.9 19.7-91.5 0-31.8-5.8-58.4-17.8-81.4-5.8-11.2-13.1-21.5-21.8-30.8H952c4.4 0 8-3.6 8-8v-60c0-4.3-3.6-7.9-8-7.9z" p-id="8782"></path></svg>