@uiw/react-md-editor 3.23.5 → 3.24.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 (109) hide show
  1. package/README.md +56 -38
  2. package/dist/mdeditor.css +2 -0
  3. package/dist/mdeditor.js +289 -287
  4. package/dist/mdeditor.min.css +1 -1
  5. package/dist/mdeditor.min.js +1 -1
  6. package/dist/mdeditor.min.js.LICENSE.txt +1 -1
  7. package/esm/commands/bold.js +9 -13
  8. package/esm/commands/code.d.ts +1 -1
  9. package/esm/commands/code.js +75 -68
  10. package/esm/commands/comment.js +20 -18
  11. package/esm/commands/help.d.ts +2 -0
  12. package/esm/commands/help.js +22 -0
  13. package/esm/commands/hr.js +33 -2
  14. package/esm/commands/image.js +39 -16
  15. package/esm/commands/index.d.ts +3 -1
  16. package/esm/commands/index.js +3 -1
  17. package/esm/commands/issue.d.ts +2 -0
  18. package/esm/commands/issue.js +41 -0
  19. package/esm/commands/italic.js +9 -13
  20. package/esm/commands/link.js +40 -14
  21. package/esm/commands/list.d.ts +3 -10
  22. package/esm/commands/list.js +39 -41
  23. package/esm/commands/quote.js +8 -8
  24. package/esm/commands/strikeThrough.js +9 -13
  25. package/esm/commands/table.d.ts +2 -0
  26. package/esm/commands/table.js +57 -0
  27. package/esm/commands/title.d.ts +7 -1
  28. package/esm/commands/title.js +21 -0
  29. package/esm/commands/title1.js +9 -7
  30. package/esm/commands/title2.js +9 -7
  31. package/esm/commands/title3.js +9 -7
  32. package/esm/commands/title4.js +9 -7
  33. package/esm/commands/title5.js +9 -7
  34. package/esm/commands/title6.js +9 -7
  35. package/esm/components/DragBar/index.js +7 -7
  36. package/esm/components/TextArea/handleKeyDown.js +83 -3
  37. package/esm/utils/InsertTextAtPosition.d.ts +0 -7
  38. package/esm/utils/InsertTextAtPosition.js +6 -24
  39. package/esm/utils/markdownUtils.d.ts +23 -1
  40. package/esm/utils/markdownUtils.js +83 -4
  41. package/lib/Context.js +2 -3
  42. package/lib/Editor.js +1 -2
  43. package/lib/commands/bold.js +10 -15
  44. package/lib/commands/code.d.ts +1 -1
  45. package/lib/commands/code.js +75 -70
  46. package/lib/commands/comment.js +21 -20
  47. package/lib/commands/divider.js +2 -3
  48. package/lib/commands/fullscreen.js +2 -3
  49. package/lib/commands/group.js +2 -3
  50. package/lib/commands/help.d.ts +2 -0
  51. package/lib/commands/help.js +29 -0
  52. package/lib/commands/hr.js +35 -5
  53. package/lib/commands/image.js +40 -18
  54. package/lib/commands/index.d.ts +3 -1
  55. package/lib/commands/index.js +8 -10
  56. package/lib/commands/issue.d.ts +2 -0
  57. package/lib/commands/issue.js +48 -0
  58. package/lib/commands/italic.js +10 -15
  59. package/lib/commands/link.js +41 -16
  60. package/lib/commands/list.d.ts +3 -10
  61. package/lib/commands/list.js +43 -52
  62. package/lib/commands/preview.js +4 -7
  63. package/lib/commands/quote.js +9 -10
  64. package/lib/commands/strikeThrough.js +10 -15
  65. package/lib/commands/table.d.ts +2 -0
  66. package/lib/commands/table.js +64 -0
  67. package/lib/commands/title.d.ts +7 -1
  68. package/lib/commands/title.js +23 -3
  69. package/lib/commands/title1.js +11 -10
  70. package/lib/commands/title2.js +11 -10
  71. package/lib/commands/title3.js +11 -10
  72. package/lib/commands/title4.js +11 -10
  73. package/lib/commands/title5.js +11 -10
  74. package/lib/commands/title6.js +11 -10
  75. package/lib/components/DragBar/index.js +8 -9
  76. package/lib/components/TextArea/handleKeyDown.js +84 -4
  77. package/lib/index.js +1 -2
  78. package/lib/utils/InsertTextAtPosition.d.ts +0 -7
  79. package/lib/utils/InsertTextAtPosition.js +6 -26
  80. package/lib/utils/markdownUtils.d.ts +23 -1
  81. package/lib/utils/markdownUtils.js +84 -4
  82. package/package.json +4 -1
  83. package/src/commands/bold.tsx +13 -12
  84. package/src/commands/code.tsx +72 -71
  85. package/src/commands/comment.tsx +20 -15
  86. package/src/commands/help.tsx +19 -0
  87. package/src/commands/hr.tsx +33 -2
  88. package/src/commands/image.tsx +38 -15
  89. package/src/commands/index.ts +8 -1
  90. package/src/commands/issue.tsx +36 -0
  91. package/src/commands/italic.tsx +13 -12
  92. package/src/commands/link.tsx +39 -12
  93. package/src/commands/list.tsx +35 -53
  94. package/src/commands/quote.tsx +14 -13
  95. package/src/commands/strikeThrough.tsx +13 -12
  96. package/src/commands/table.tsx +52 -0
  97. package/src/commands/title.tsx +18 -1
  98. package/src/commands/title1.tsx +6 -9
  99. package/src/commands/title2.tsx +6 -9
  100. package/src/commands/title3.tsx +6 -9
  101. package/src/commands/title4.tsx +6 -9
  102. package/src/commands/title5.tsx +6 -9
  103. package/src/commands/title6.tsx +6 -9
  104. package/src/components/TextArea/handleKeyDown.tsx +50 -3
  105. package/src/utils/InsertTextAtPosition.ts +7 -28
  106. package/src/utils/markdownUtils.ts +94 -4
  107. package/src/__test__/commands.test.tsx +0 -512
  108. package/src/__test__/editor.test.tsx +0 -88
  109. package/src/__test__/utils/getSurroundingWord.test.tsx +0 -22
@@ -7,7 +7,7 @@ Object.defineProperty(exports, "__esModule", {
7
7
  exports.fullscreen = void 0;
8
8
  var _react = _interopRequireDefault(require("react"));
9
9
  var _jsxRuntime = require("react/jsx-runtime");
10
- var fullscreen = {
10
+ var fullscreen = exports.fullscreen = {
11
11
  name: 'fullscreen',
12
12
  keyCommand: 'fullscreen',
13
13
  shortcuts: 'ctrlcmd+0',
@@ -33,5 +33,4 @@ var fullscreen = {
33
33
  });
34
34
  }
35
35
  }
36
- };
37
- exports.fullscreen = fullscreen;
36
+ };
@@ -10,7 +10,7 @@ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")
10
10
  var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
11
11
  var _react = _interopRequireDefault(require("react"));
12
12
  var _jsxRuntime = require("react/jsx-runtime");
13
- var group = function group(arr, options) {
13
+ var group = exports.group = function group(arr, options) {
14
14
  var data = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({
15
15
  children: arr,
16
16
  icon: /*#__PURE__*/(0, _jsxRuntime.jsx)("svg", {
@@ -34,5 +34,4 @@ var group = function group(arr, options) {
34
34
  });
35
35
  }
36
36
  return data;
37
- };
38
- exports.group = group;
37
+ };
@@ -0,0 +1,2 @@
1
+ import { ICommand } from './';
2
+ export declare const help: ICommand;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.help = void 0;
8
+ var _react = _interopRequireDefault(require("react"));
9
+ var _jsxRuntime = require("react/jsx-runtime");
10
+ var help = exports.help = {
11
+ name: 'help',
12
+ keyCommand: 'help',
13
+ buttonProps: {
14
+ 'aria-label': 'Open help',
15
+ title: 'Open help'
16
+ },
17
+ icon: /*#__PURE__*/(0, _jsxRuntime.jsx)("svg", {
18
+ viewBox: "0 0 16 16",
19
+ width: "12px",
20
+ height: "12px",
21
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)("path", {
22
+ d: "M8 0C3.6 0 0 3.6 0 8s3.6 8 8 8 8-3.6 8-8-3.6-8-8-8Zm.9 13H7v-1.8h1.9V13Zm-.1-3.6v.5H7.1v-.6c.2-2.1 2-1.9 1.9-3.2.1-.7-.3-1.1-1-1.1-.8 0-1.2.7-1.2 1.6H5c0-1.7 1.2-3 2.9-3 2.3 0 3 1.4 3 2.3.1 2.3-1.9 2-2.1 3.5Z",
23
+ fill: "currentColor"
24
+ })
25
+ }),
26
+ execute: function execute() {
27
+ window.open('https://www.markdownguide.org/basic-syntax/', '_blank', 'noreferrer');
28
+ }
29
+ };
@@ -6,12 +6,14 @@ Object.defineProperty(exports, "__esModule", {
6
6
  });
7
7
  exports.hr = void 0;
8
8
  var _react = _interopRequireDefault(require("react"));
9
+ var _markdownUtils = require("../utils/markdownUtils");
9
10
  var _jsxRuntime = require("react/jsx-runtime");
10
- var hr = {
11
+ var hr = exports.hr = {
11
12
  name: 'hr',
12
13
  keyCommand: 'hr',
13
14
  shortcuts: 'ctrlcmd+h',
14
- value: '----------',
15
+ prefix: '\n\n---\n',
16
+ suffix: '',
15
17
  buttonProps: {
16
18
  'aria-label': 'Insert HR (ctrl + h)',
17
19
  title: 'Insert HR (ctrl + h)'
@@ -27,7 +29,35 @@ var hr = {
27
29
  })
28
30
  }),
29
31
  execute: function execute(state, api) {
30
- api.replaceSelection("".concat(state.selectedText, "\n\n").concat(state.command.value || '', "-\n\n"));
32
+ var newSelectionRange = (0, _markdownUtils.selectWord)({
33
+ text: state.text,
34
+ selection: state.selection,
35
+ prefix: state.command.prefix,
36
+ suffix: state.command.suffix
37
+ });
38
+ var state1 = api.setSelectionRange(newSelectionRange);
39
+ if (state1.selectedText.length >= state.command.prefix.length && state1.selectedText.startsWith(state.command.prefix)) {
40
+ // Remove
41
+ (0, _markdownUtils.executeCommand)({
42
+ api: api,
43
+ selectedText: state1.selectedText,
44
+ selection: state.selection,
45
+ prefix: state.command.prefix,
46
+ suffix: state.command.suffix
47
+ });
48
+ } else {
49
+ // Add
50
+ state1 = api.setSelectionRange({
51
+ start: state.selection.start,
52
+ end: state.selection.start
53
+ });
54
+ (0, _markdownUtils.executeCommand)({
55
+ api: api,
56
+ selectedText: state1.selectedText,
57
+ selection: state.selection,
58
+ prefix: state.command.prefix,
59
+ suffix: state.command.suffix
60
+ });
61
+ }
31
62
  }
32
- };
33
- exports.hr = hr;
63
+ };
@@ -8,11 +8,12 @@ exports.image = void 0;
8
8
  var _react = _interopRequireDefault(require("react"));
9
9
  var _markdownUtils = require("../utils/markdownUtils");
10
10
  var _jsxRuntime = require("react/jsx-runtime");
11
- var image = {
11
+ var image = exports.image = {
12
12
  name: 'image',
13
13
  keyCommand: 'image',
14
14
  shortcuts: 'ctrlcmd+k',
15
- value: '![image]({{text}})',
15
+ prefix: '![image](',
16
+ suffix: ')',
16
17
  buttonProps: {
17
18
  'aria-label': 'Add image (ctrl + k)',
18
19
  title: 'Add image (ctrl + k)'
@@ -27,25 +28,46 @@ var image = {
27
28
  })
28
29
  }),
29
30
  execute: function execute(state, api) {
30
- // Select everything
31
31
  var newSelectionRange = (0, _markdownUtils.selectWord)({
32
32
  text: state.text,
33
- selection: state.selection
33
+ selection: state.selection,
34
+ prefix: state.command.prefix,
35
+ suffix: state.command.suffix
34
36
  });
35
37
  var state1 = api.setSelectionRange(newSelectionRange);
36
- // Replaces the current selection with the image
37
- var imageTemplate = state1.selectedText || 'https://example.com/your-image.png';
38
- var val = state.command.value || '';
39
- api.replaceSelection(val.replace(/({{text}})/gi, imageTemplate));
40
- var start = state1.selection.start + val.indexOf('{{text}}');
41
- var end = state1.selection.start + val.indexOf('{{text}}') + (state1.selection.end - state1.selection.start);
42
- if (!state1.selectedText) {
43
- end = end + imageTemplate.length;
38
+ if (state1.selectedText.includes('http') || state1.selectedText.includes('www')) {
39
+ (0, _markdownUtils.executeCommand)({
40
+ api: api,
41
+ selectedText: state1.selectedText,
42
+ selection: state.selection,
43
+ prefix: state.command.prefix,
44
+ suffix: state.command.suffix
45
+ });
46
+ } else {
47
+ newSelectionRange = (0, _markdownUtils.selectWord)({
48
+ text: state.text,
49
+ selection: state.selection,
50
+ prefix: '![',
51
+ suffix: ']()'
52
+ });
53
+ state1 = api.setSelectionRange(newSelectionRange);
54
+ if (state1.selectedText.length === 0) {
55
+ (0, _markdownUtils.executeCommand)({
56
+ api: api,
57
+ selectedText: state1.selectedText,
58
+ selection: state.selection,
59
+ prefix: '![image',
60
+ suffix: '](url)'
61
+ });
62
+ } else {
63
+ (0, _markdownUtils.executeCommand)({
64
+ api: api,
65
+ selectedText: state1.selectedText,
66
+ selection: state.selection,
67
+ prefix: '![',
68
+ suffix: ']()'
69
+ });
70
+ }
44
71
  }
45
- api.setSelectionRange({
46
- start: start,
47
- end: end
48
- });
49
72
  }
50
- };
51
- exports.image = image;
73
+ };
@@ -43,7 +43,9 @@ export interface ICommandBase<T> {
43
43
  shortcuts?: string;
44
44
  groupName?: string;
45
45
  icon?: React.ReactElement;
46
- value?: T;
46
+ value?: string;
47
+ prefix?: string;
48
+ suffix?: string;
47
49
  position?: 'right';
48
50
  liProps?: React.LiHTMLAttributes<HTMLLIElement>;
49
51
  buttonProps?: React.ButtonHTMLAttributes<HTMLButtonElement> | null;
@@ -189,7 +189,9 @@ var _title4 = require("./title3");
189
189
  var _title5 = require("./title4");
190
190
  var _title6 = require("./title5");
191
191
  var _title7 = require("./title6");
192
- var getCommands = function getCommands() {
192
+ var _table = require("./table");
193
+ var _help = require("./help");
194
+ var getCommands = exports.getCommands = function getCommands() {
193
195
  return [_bold.bold, _italic.italic, _strikeThrough.strikethrough, _hr.hr, (0, _group.group)([_title2.title1, _title3.title2, _title4.title3, _title5.title4, _title6.title5, _title7.title6], {
194
196
  name: 'title',
195
197
  groupName: 'title',
@@ -197,13 +199,11 @@ var getCommands = function getCommands() {
197
199
  'aria-label': 'Insert title',
198
200
  title: 'Insert title'
199
201
  }
200
- }), _divider.divider, _link.link, _quote.quote, _code.code, _code.codeBlock, _comment.comment, _image.image, _divider.divider, _list.unorderedListCommand, _list.orderedListCommand, _list.checkedListCommand];
202
+ }), _divider.divider, _link.link, _quote.quote, _code.code, _code.codeBlock, _comment.comment, _image.image, _table.table, _divider.divider, _list.unorderedListCommand, _list.orderedListCommand, _list.checkedListCommand, _divider.divider, _help.help];
201
203
  };
202
- exports.getCommands = getCommands;
203
- var getExtraCommands = function getExtraCommands() {
204
+ var getExtraCommands = exports.getExtraCommands = function getExtraCommands() {
204
205
  return [_preview.codeEdit, _preview.codeLive, _preview.codePreview, _divider.divider, _fullscreen.fullscreen];
205
206
  };
206
- exports.getExtraCommands = getExtraCommands;
207
207
  function getStateFromTextArea(textArea) {
208
208
  var _textArea$value;
209
209
  return {
@@ -215,7 +215,7 @@ function getStateFromTextArea(textArea) {
215
215
  selectedText: (_textArea$value = textArea.value) === null || _textArea$value === void 0 ? void 0 : _textArea$value.slice(textArea.selectionStart, textArea.selectionEnd)
216
216
  };
217
217
  }
218
- var TextAreaTextApi = /*#__PURE__*/function () {
218
+ var TextAreaTextApi = exports.TextAreaTextApi = /*#__PURE__*/function () {
219
219
  function TextAreaTextApi(textArea) {
220
220
  (0, _classCallCheck2["default"])(this, TextAreaTextApi);
221
221
  (0, _defineProperty2["default"])(this, "textArea", void 0);
@@ -249,8 +249,7 @@ var TextAreaTextApi = /*#__PURE__*/function () {
249
249
  }]);
250
250
  return TextAreaTextApi;
251
251
  }();
252
- exports.TextAreaTextApi = TextAreaTextApi;
253
- var TextAreaCommandOrchestrator = /*#__PURE__*/function () {
252
+ var TextAreaCommandOrchestrator = exports.TextAreaCommandOrchestrator = /*#__PURE__*/function () {
254
253
  function TextAreaCommandOrchestrator(textArea) {
255
254
  (0, _classCallCheck2["default"])(this, TextAreaCommandOrchestrator);
256
255
  (0, _defineProperty2["default"])(this, "textArea", void 0);
@@ -273,5 +272,4 @@ var TextAreaCommandOrchestrator = /*#__PURE__*/function () {
273
272
  }
274
273
  }]);
275
274
  return TextAreaCommandOrchestrator;
276
- }();
277
- exports.TextAreaCommandOrchestrator = TextAreaCommandOrchestrator;
275
+ }();
@@ -0,0 +1,2 @@
1
+ import { ICommand } from './';
2
+ export declare const issue: ICommand;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.issue = void 0;
8
+ var _react = _interopRequireDefault(require("react"));
9
+ var _markdownUtils = require("../utils/markdownUtils");
10
+ var _jsxRuntime = require("react/jsx-runtime");
11
+ var issue = exports.issue = {
12
+ name: 'issue',
13
+ keyCommand: 'issue',
14
+ prefix: '#',
15
+ suffix: '',
16
+ buttonProps: {
17
+ 'aria-label': 'Add issue',
18
+ title: 'Add issue'
19
+ },
20
+ icon: /*#__PURE__*/(0, _jsxRuntime.jsx)("svg", {
21
+ role: "img",
22
+ width: "12",
23
+ height: "12",
24
+ viewBox: "0 0 448 512",
25
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)("path", {
26
+ fill: "currentColor",
27
+ d: "M181.3 32.4c17.4 2.9 29.2 19.4 26.3 36.8L197.8 128h95.1l11.5-69.3c2.9-17.4 19.4-29.2 36.8-26.3s29.2 19.4 26.3 36.8L357.8 128H416c17.7 0 32 14.3 32 32s-14.3 32-32 32H347.1L325.8 320H384c17.7 0 32 14.3 32 32s-14.3 32-32 32H315.1l-11.5 69.3c-2.9 17.4-19.4 29.2-36.826.3s-29.2-19.4-26.3-36.8l9.8-58.7H155.1l-11.5 69.3c-2.9 17.4-19.4 29.2-36.8 26.3s-29.2-19.4-26.3-36.8L90.2 384H32c-17.7 0-32-14.3-32-32s14.3-32 32-32h68.9l21.3-128H64c-17.7 0-32-14.3-32-32s14.3-32 32-32h68.9l11.5-69.3c2.9-17.4 19.4-29.2 36.8-26.3zM187.1 192L165.8320h95.1l21.3-128H187.1z"
28
+ //Font Awesome Free 6.4.2 by @fontawesome - https://fontawesome.com
29
+ })
30
+ }),
31
+
32
+ execute: function execute(state, api) {
33
+ var newSelectionRange = (0, _markdownUtils.selectWord)({
34
+ text: state.text,
35
+ selection: state.selection,
36
+ prefix: state.command.prefix,
37
+ suffix: state.command.suffix
38
+ });
39
+ var state1 = api.setSelectionRange(newSelectionRange);
40
+ (0, _markdownUtils.executeCommand)({
41
+ api: api,
42
+ selectedText: state1.selectedText,
43
+ selection: state.selection,
44
+ prefix: state.command.prefix,
45
+ suffix: state.command.suffix
46
+ });
47
+ }
48
+ };
@@ -8,11 +8,11 @@ exports.italic = void 0;
8
8
  var _react = _interopRequireDefault(require("react"));
9
9
  var _markdownUtils = require("../utils/markdownUtils");
10
10
  var _jsxRuntime = require("react/jsx-runtime");
11
- var italic = {
11
+ var italic = exports.italic = {
12
12
  name: 'italic',
13
13
  keyCommand: 'italic',
14
14
  shortcuts: 'ctrlcmd+i',
15
- value: '*{{text}}*',
15
+ prefix: '*',
16
16
  buttonProps: {
17
17
  'aria-label': 'Add italic text (ctrl + i)',
18
18
  title: 'Add italic text (ctrl + i)'
@@ -29,22 +29,17 @@ var italic = {
29
29
  })
30
30
  }),
31
31
  execute: function execute(state, api) {
32
- // Adjust the selection to encompass the whole word if the caret is inside one
33
32
  var newSelectionRange = (0, _markdownUtils.selectWord)({
34
33
  text: state.text,
35
- selection: state.selection
34
+ selection: state.selection,
35
+ prefix: state.command.prefix
36
36
  });
37
37
  var state1 = api.setSelectionRange(newSelectionRange);
38
- // Replaces the current selection with the bold mark up
39
- var val = state.command.value || '';
40
- api.replaceSelection(val.replace(/({{text}})/gi, state1.selectedText));
41
- var start = state1.selection.start + val.indexOf('{{text}}');
42
- var end = state1.selection.start + val.indexOf('{{text}}') + (state1.selection.end - state1.selection.start);
43
- // Adjust the selection to not contain the **
44
- api.setSelectionRange({
45
- start: start,
46
- end: end
38
+ (0, _markdownUtils.executeCommand)({
39
+ api: api,
40
+ selectedText: state1.selectedText,
41
+ selection: state.selection,
42
+ prefix: state.command.prefix
47
43
  });
48
44
  }
49
- };
50
- exports.italic = italic;
45
+ };
@@ -8,11 +8,12 @@ exports.link = void 0;
8
8
  var _react = _interopRequireDefault(require("react"));
9
9
  var _markdownUtils = require("../utils/markdownUtils");
10
10
  var _jsxRuntime = require("react/jsx-runtime");
11
- var link = {
11
+ var link = exports.link = {
12
12
  name: 'link',
13
13
  keyCommand: 'link',
14
14
  shortcuts: 'ctrlcmd+l',
15
- value: '[{{text}}](URL Here)',
15
+ prefix: '[',
16
+ suffix: '](url)',
16
17
  buttonProps: {
17
18
  'aria-label': 'Add a link (ctrl + l)',
18
19
  title: 'Add a link (ctrl + l)'
@@ -29,22 +30,46 @@ var link = {
29
30
  })
30
31
  }),
31
32
  execute: function execute(state, api) {
32
- // Adjust the selection to encompass the whole word if the caret is inside one
33
33
  var newSelectionRange = (0, _markdownUtils.selectWord)({
34
34
  text: state.text,
35
- selection: state.selection
35
+ selection: state.selection,
36
+ prefix: state.command.prefix,
37
+ suffix: state.command.suffix
36
38
  });
37
39
  var state1 = api.setSelectionRange(newSelectionRange);
38
- var val = state.command.value || '';
39
- // Replaces the current selection with the bold mark up
40
- api.replaceSelection(val.replace(/({{text}})/gi, state1.selectedText));
41
- var start = state1.selection.start + val.indexOf('{{text}}');
42
- var end = state1.selection.start + val.indexOf('{{text}}') + (state1.selection.end - state1.selection.start);
43
- // Adjust the selection to not contain the **
44
- api.setSelectionRange({
45
- start: start,
46
- end: end
47
- });
40
+ if (state1.selectedText.includes('http') || state1.selectedText.includes('www')) {
41
+ newSelectionRange = (0, _markdownUtils.selectWord)({
42
+ text: state.text,
43
+ selection: state.selection,
44
+ prefix: '[](',
45
+ suffix: ')'
46
+ });
47
+ state1 = api.setSelectionRange(newSelectionRange);
48
+ (0, _markdownUtils.executeCommand)({
49
+ api: api,
50
+ selectedText: state1.selectedText,
51
+ selection: state.selection,
52
+ prefix: '[](',
53
+ suffix: ')'
54
+ });
55
+ } else {
56
+ if (state1.selectedText.length === 0) {
57
+ (0, _markdownUtils.executeCommand)({
58
+ api: api,
59
+ selectedText: state1.selectedText,
60
+ selection: state.selection,
61
+ prefix: '[title',
62
+ suffix: '](url)'
63
+ });
64
+ } else {
65
+ (0, _markdownUtils.executeCommand)({
66
+ api: api,
67
+ selectedText: state1.selectedText,
68
+ selection: state.selection,
69
+ prefix: state.command.prefix,
70
+ suffix: state.command.suffix
71
+ });
72
+ }
73
+ }
48
74
  }
49
- };
50
- exports.link = link;
75
+ };
@@ -1,13 +1,6 @@
1
- import { ICommand, TextState, TextAreaTextApi } from './';
2
- export type AlterLineFunction = (line: string, index: number) => string;
3
- /**
4
- * Inserts insertionString before each line
5
- */
6
- export declare function insertBeforeEachLine(selectedText: string, insertBefore: string | AlterLineFunction): {
7
- modifiedText: string;
8
- insertionLength: number;
9
- };
10
- export declare const makeList: (state: TextState, api: TextAreaTextApi, insertBefore: string | AlterLineFunction) => void;
1
+ import { ICommand, ExecuteState, TextAreaTextApi } from './';
2
+ import { AlterLineFunction } from '../utils/markdownUtils';
3
+ export declare const makeList: (state: ExecuteState, api: TextAreaTextApi, insertBefore: string | AlterLineFunction) => void;
11
4
  export declare const unorderedListCommand: ICommand;
12
5
  export declare const orderedListCommand: ICommand;
13
6
  export declare const checkedListCommand: ICommand;
@@ -4,65 +4,59 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- exports.checkedListCommand = void 0;
8
- exports.insertBeforeEachLine = insertBeforeEachLine;
9
- exports.unorderedListCommand = exports.orderedListCommand = exports.makeList = void 0;
7
+ exports.unorderedListCommand = exports.orderedListCommand = exports.makeList = exports.checkedListCommand = void 0;
10
8
  var _react = _interopRequireDefault(require("react"));
11
9
  var _markdownUtils = require("../utils/markdownUtils");
12
10
  var _jsxRuntime = require("react/jsx-runtime");
13
- /**
14
- * Inserts insertionString before each line
15
- */
16
- function insertBeforeEachLine(selectedText, insertBefore) {
17
- var lines = selectedText.split(/\n/);
18
- var insertionLength = 0;
19
- var modifiedText = lines.map(function (item, index) {
20
- if (typeof insertBefore === 'string') {
21
- insertionLength += insertBefore.length;
22
- return insertBefore + item;
23
- } else if (typeof insertBefore === 'function') {
24
- var insertionResult = insertBefore(item, index);
25
- insertionLength += insertionResult.length;
26
- return insertBefore(item, index) + item;
27
- }
28
- throw Error('insertion is expected to be either a string or a function');
29
- }).join('\n');
30
- return {
31
- modifiedText: modifiedText,
32
- insertionLength: insertionLength
33
- };
34
- }
35
- var makeList = function makeList(state, api, insertBefore) {
36
- // Adjust the selection to encompass the whole word if the caret is inside one
11
+ var makeList = exports.makeList = function makeList(state, api, insertBefore) {
37
12
  var newSelectionRange = (0, _markdownUtils.selectWord)({
38
13
  text: state.text,
39
- selection: state.selection
14
+ selection: state.selection,
15
+ prefix: state.command.prefix
40
16
  });
41
17
  var state1 = api.setSelectionRange(newSelectionRange);
42
18
  var breaksBeforeCount = (0, _markdownUtils.getBreaksNeededForEmptyLineBefore)(state1.text, state1.selection.start);
43
19
  var breaksBefore = Array(breaksBeforeCount + 1).join('\n');
44
20
  var breaksAfterCount = (0, _markdownUtils.getBreaksNeededForEmptyLineAfter)(state1.text, state1.selection.end);
45
21
  var breaksAfter = Array(breaksAfterCount + 1).join('\n');
46
- var modifiedText = insertBeforeEachLine(state1.selectedText, insertBefore);
47
- api.replaceSelection("".concat(breaksBefore).concat(modifiedText.modifiedText).concat(breaksAfter));
48
-
49
- // Specifically when the text has only one line, we can exclude the "- ", for example, from the selection
50
- var oneLinerOffset = state1.selectedText.indexOf('\n') === -1 ? modifiedText.insertionLength : 0;
51
- var selectionStart = state1.selection.start + breaksBeforeCount + oneLinerOffset;
52
- var selectionEnd = selectionStart + modifiedText.modifiedText.length - oneLinerOffset;
53
-
54
- // Adjust the selection to not contain the **
55
- api.setSelectionRange({
56
- start: selectionStart,
57
- end: selectionEnd
58
- });
22
+ var _insertBeforeEachLine = (0, _markdownUtils.insertBeforeEachLine)(state1.selectedText, insertBefore),
23
+ modifiedText = _insertBeforeEachLine.modifiedText,
24
+ insertionLength = _insertBeforeEachLine.insertionLength;
25
+ if (insertionLength < 0) {
26
+ // Remove
27
+ var selectionStart = state1.selection.start;
28
+ var selectionEnd = state1.selection.end;
29
+ if (state1.selection.start > 0 && state.text.slice(state1.selection.start - 1, state1.selection.start) === '\n') {
30
+ selectionStart -= 1;
31
+ }
32
+ if (state1.selection.end < state.text.length - 1 && state.text.slice(state1.selection.end, state1.selection.end + 1) === '\n') {
33
+ selectionEnd += 1;
34
+ }
35
+ api.setSelectionRange({
36
+ start: selectionStart,
37
+ end: selectionEnd
38
+ });
39
+ api.replaceSelection("".concat(modifiedText));
40
+ api.setSelectionRange({
41
+ start: selectionStart,
42
+ end: selectionStart + modifiedText.length
43
+ });
44
+ } else {
45
+ // Add
46
+ api.replaceSelection("".concat(breaksBefore).concat(modifiedText).concat(breaksAfter));
47
+ var _selectionStart = state1.selection.start + breaksBeforeCount;
48
+ var _selectionEnd = _selectionStart + modifiedText.length;
49
+ api.setSelectionRange({
50
+ start: _selectionStart,
51
+ end: _selectionEnd
52
+ });
53
+ }
59
54
  };
60
- exports.makeList = makeList;
61
- var unorderedListCommand = {
55
+ var unorderedListCommand = exports.unorderedListCommand = {
62
56
  name: 'unordered-list',
63
57
  keyCommand: 'list',
64
58
  shortcuts: 'ctrl+shift+u',
65
- value: '- ',
59
+ prefix: '- ',
66
60
  buttonProps: {
67
61
  'aria-label': 'Add unordered list (ctrl + shift + u)',
68
62
  title: 'Add unordered list (ctrl + shift + u)'
@@ -81,12 +75,11 @@ var unorderedListCommand = {
81
75
  makeList(state, api, '- ');
82
76
  }
83
77
  };
84
- exports.unorderedListCommand = unorderedListCommand;
85
- var orderedListCommand = {
78
+ var orderedListCommand = exports.orderedListCommand = {
86
79
  name: 'ordered-list',
87
80
  keyCommand: 'list',
88
81
  shortcuts: 'ctrl+shift+o',
89
- value: '1. ',
82
+ prefix: '1. ',
90
83
  buttonProps: {
91
84
  'aria-label': 'Add ordered list (ctrl + shift + o)',
92
85
  title: 'Add ordered list (ctrl + shift + o)'
@@ -108,12 +101,11 @@ var orderedListCommand = {
108
101
  });
109
102
  }
110
103
  };
111
- exports.orderedListCommand = orderedListCommand;
112
- var checkedListCommand = {
104
+ var checkedListCommand = exports.checkedListCommand = {
113
105
  name: 'checked-list',
114
106
  keyCommand: 'list',
115
107
  shortcuts: 'ctrl+shift+c',
116
- value: '- [x] ',
108
+ prefix: '- [ ] ',
117
109
  buttonProps: {
118
110
  'aria-label': 'Add checked list (ctrl + shift + c)',
119
111
  title: 'Add checked list (ctrl + shift + c)'
@@ -134,5 +126,4 @@ var checkedListCommand = {
134
126
  return "- [ ] ";
135
127
  });
136
128
  }
137
- };
138
- exports.checkedListCommand = checkedListCommand;
129
+ };
@@ -7,7 +7,7 @@ Object.defineProperty(exports, "__esModule", {
7
7
  exports.codePreview = exports.codeLive = exports.codeEdit = void 0;
8
8
  var _react = _interopRequireDefault(require("react"));
9
9
  var _jsxRuntime = require("react/jsx-runtime");
10
- var codePreview = {
10
+ var codePreview = exports.codePreview = {
11
11
  name: 'preview',
12
12
  keyCommand: 'preview',
13
13
  value: 'preview',
@@ -37,8 +37,7 @@ var codePreview = {
37
37
  }
38
38
  }
39
39
  };
40
- exports.codePreview = codePreview;
41
- var codeEdit = {
40
+ var codeEdit = exports.codeEdit = {
42
41
  name: 'edit',
43
42
  keyCommand: 'preview',
44
43
  value: 'edit',
@@ -68,8 +67,7 @@ var codeEdit = {
68
67
  }
69
68
  }
70
69
  };
71
- exports.codeEdit = codeEdit;
72
- var codeLive = {
70
+ var codeLive = exports.codeLive = {
73
71
  name: 'live',
74
72
  keyCommand: 'preview',
75
73
  value: 'live',
@@ -98,5 +96,4 @@ var codeLive = {
98
96
  });
99
97
  }
100
98
  }
101
- };
102
- exports.codeLive = codeLive;
99
+ };