@uiw/react-md-editor 3.8.3 → 3.8.4

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 (38) hide show
  1. package/esm/commands/title1.js +5 -6
  2. package/esm/commands/title1.js.map +9 -5
  3. package/esm/commands/title2.js +5 -6
  4. package/esm/commands/title2.js.map +9 -5
  5. package/esm/commands/title3.js +5 -6
  6. package/esm/commands/title3.js.map +9 -5
  7. package/esm/commands/title4.js +5 -6
  8. package/esm/commands/title4.js.map +9 -5
  9. package/esm/commands/title5.js +5 -6
  10. package/esm/commands/title5.js.map +9 -5
  11. package/esm/commands/title6.js +5 -6
  12. package/esm/commands/title6.js.map +9 -5
  13. package/esm/utils/InsertTextAtPosition.d.ts +7 -0
  14. package/esm/utils/InsertTextAtPosition.js +27 -1
  15. package/esm/utils/InsertTextAtPosition.js.map +14 -6
  16. package/lib/commands/title1.js +6 -6
  17. package/lib/commands/title1.js.map +8 -5
  18. package/lib/commands/title2.js +6 -6
  19. package/lib/commands/title2.js.map +8 -5
  20. package/lib/commands/title3.js +6 -6
  21. package/lib/commands/title3.js.map +8 -5
  22. package/lib/commands/title4.js +6 -6
  23. package/lib/commands/title4.js.map +8 -5
  24. package/lib/commands/title5.js +6 -6
  25. package/lib/commands/title5.js.map +8 -5
  26. package/lib/commands/title6.js +6 -6
  27. package/lib/commands/title6.js.map +8 -5
  28. package/lib/utils/InsertTextAtPosition.d.ts +7 -0
  29. package/lib/utils/InsertTextAtPosition.js +30 -0
  30. package/lib/utils/InsertTextAtPosition.js.map +14 -6
  31. package/package.json +1 -1
  32. package/src/commands/title1.tsx +5 -4
  33. package/src/commands/title2.tsx +5 -4
  34. package/src/commands/title3.tsx +5 -4
  35. package/src/commands/title4.tsx +5 -4
  36. package/src/commands/title5.tsx +5 -4
  37. package/src/commands/title6.tsx +5 -4
  38. package/src/utils/InsertTextAtPosition.ts +29 -2
@@ -1,4 +1,5 @@
1
1
  import React from 'react';
2
+ import { insertAtLineStart } from '../utils/InsertTextAtPosition';
2
3
  import { jsx as _jsx } from "react/jsx-runtime";
3
4
  export var title1 = {
4
5
  name: 'title1',
@@ -16,13 +17,11 @@ export var title1 = {
16
17
  children: "Title 1"
17
18
  }),
18
19
  execute: (state, api) => {
19
- var modifyText = "# " + state.selectedText + "\n";
20
-
21
- if (!state.selectedText) {
22
- modifyText = "# ";
20
+ if (state.selection.start === 0 || /\n$/.test(state.text)) {
21
+ api.replaceSelection('# ');
22
+ } else {
23
+ insertAtLineStart('# ', state.selection.start, api.textArea);
23
24
  }
24
-
25
- api.replaceSelection(modifyText);
26
25
  }
27
26
  };
28
27
  //# sourceMappingURL=title1.js.map
@@ -5,6 +5,7 @@
5
5
  ],
6
6
  "names": [
7
7
  "React",
8
+ "insertAtLineStart",
8
9
  "title1",
9
10
  "name",
10
11
  "keyCommand",
@@ -17,12 +18,15 @@
17
18
  "execute",
18
19
  "state",
19
20
  "api",
20
- "modifyText",
21
- "selectedText",
22
- "replaceSelection"
21
+ "selection",
22
+ "start",
23
+ "test",
24
+ "text",
25
+ "replaceSelection",
26
+ "textArea"
23
27
  ],
24
- "mappings": "AAAA,OAAOA,KAAP,MAAkB,OAAlB;;AAGA,OAAO,IAAMC,MAAgB,GAAG;AAC9BC,EAAAA,IAAI,EAAE,QADwB;AAE9BC,EAAAA,UAAU,EAAE,QAFkB;AAG9BC,EAAAA,SAAS,EAAE,WAHmB;AAI9BC,EAAAA,WAAW,EAAE;AAAE,kBAAc,gBAAhB;AAAkCC,IAAAA,KAAK,EAAE;AAAzC,GAJiB;AAK9BC,EAAAA,IAAI,eAAE;AAAK,IAAA,KAAK,EAAE;AAAEC,MAAAA,QAAQ,EAAE,EAAZ;AAAgBC,MAAAA,SAAS,EAAE;AAA3B,KAAZ;AAAA;AAAA,IALwB;AAM9BC,EAAAA,OAAO,EAAE,CAACC,KAAD,EAAmBC,GAAnB,KAA4C;AACnD,QAAIC,UAAU,UAAQF,KAAK,CAACG,YAAd,OAAd;;AACA,QAAI,CAACH,KAAK,CAACG,YAAX,EAAyB;AACvBD,MAAAA,UAAU,OAAV;AACD;;AACDD,IAAAA,GAAG,CAACG,gBAAJ,CAAqBF,UAArB;AACD;AAZ6B,CAAzB",
28
+ "mappings": "AAAA,OAAOA,KAAP,MAAkB,OAAlB;AACA,SAASC,iBAAT,QAAkC,+BAAlC;;AAGA,OAAO,IAAMC,MAAgB,GAAG;AAC9BC,EAAAA,IAAI,EAAE,QADwB;AAE9BC,EAAAA,UAAU,EAAE,QAFkB;AAG9BC,EAAAA,SAAS,EAAE,WAHmB;AAI9BC,EAAAA,WAAW,EAAE;AAAE,kBAAc,gBAAhB;AAAkCC,IAAAA,KAAK,EAAE;AAAzC,GAJiB;AAK9BC,EAAAA,IAAI,eAAE;AAAK,IAAA,KAAK,EAAE;AAAEC,MAAAA,QAAQ,EAAE,EAAZ;AAAgBC,MAAAA,SAAS,EAAE;AAA3B,KAAZ;AAAA;AAAA,IALwB;AAM9BC,EAAAA,OAAO,EAAE,CAACC,KAAD,EAAmBC,GAAnB,KAA4C;AACnD,QAAID,KAAK,CAACE,SAAN,CAAgBC,KAAhB,KAA0B,CAA1B,IAA+B,MAAMC,IAAN,CAAWJ,KAAK,CAACK,IAAjB,CAAnC,EAA2D;AACzDJ,MAAAA,GAAG,CAACK,gBAAJ,CAAqB,IAArB;AACD,KAFD,MAEO;AACLjB,MAAAA,iBAAiB,CAAC,IAAD,EAAOW,KAAK,CAACE,SAAN,CAAgBC,KAAvB,EAA8BF,GAAG,CAACM,QAAlC,CAAjB;AACD;AACF;AAZ6B,CAAzB",
25
29
  "sourcesContent": [
26
- "import React from 'react';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title1: ICommand = {\n name: 'title1',\n keyCommand: 'title1',\n shortcuts: 'ctrlcmd+1',\n buttonProps: { 'aria-label': 'Insert title 1', title: 'Insert title 1' },\n icon: <div style={{ fontSize: 18, textAlign: 'left' }}>Title 1</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n let modifyText = `# ${state.selectedText}\\n`;\n if (!state.selectedText) {\n modifyText = `# `;\n }\n api.replaceSelection(modifyText);\n },\n};\n"
30
+ "import React from 'react';\nimport { insertAtLineStart } from '../utils/InsertTextAtPosition';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title1: ICommand = {\n name: 'title1',\n keyCommand: 'title1',\n shortcuts: 'ctrlcmd+1',\n buttonProps: { 'aria-label': 'Insert title 1', title: 'Insert title 1' },\n icon: <div style={{ fontSize: 18, textAlign: 'left' }}>Title 1</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n if (state.selection.start === 0 || /\\n$/.test(state.text)) {\n api.replaceSelection('# ');\n } else {\n insertAtLineStart('# ', state.selection.start, api.textArea);\n }\n },\n};\n"
27
31
  ]
28
32
  }
@@ -1,4 +1,5 @@
1
1
  import * as React from 'react';
2
+ import { insertAtLineStart } from '../utils/InsertTextAtPosition';
2
3
  import { jsx as _jsx } from "react/jsx-runtime";
3
4
  export var title2 = {
4
5
  name: 'title2',
@@ -16,13 +17,11 @@ export var title2 = {
16
17
  children: "Title 2"
17
18
  }),
18
19
  execute: (state, api) => {
19
- var modifyText = "## " + state.selectedText + "\n";
20
-
21
- if (!state.selectedText) {
22
- modifyText = "## ";
20
+ if (state.selection.start === 0 || /\n$/.test(state.text)) {
21
+ api.replaceSelection('## ');
22
+ } else {
23
+ insertAtLineStart('## ', state.selection.start, api.textArea);
23
24
  }
24
-
25
- api.replaceSelection(modifyText);
26
25
  }
27
26
  };
28
27
  //# sourceMappingURL=title2.js.map
@@ -5,6 +5,7 @@
5
5
  ],
6
6
  "names": [
7
7
  "React",
8
+ "insertAtLineStart",
8
9
  "title2",
9
10
  "name",
10
11
  "keyCommand",
@@ -17,12 +18,15 @@
17
18
  "execute",
18
19
  "state",
19
20
  "api",
20
- "modifyText",
21
- "selectedText",
22
- "replaceSelection"
21
+ "selection",
22
+ "start",
23
+ "test",
24
+ "text",
25
+ "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AA4C;AACnD,QAAID,KAAK,CAACE,SAAN,CAAgBC,KAAhB,KAA0B,CAA1B,IAA+B,MAAMC,IAAN,CAAWJ,KAAK,CAACK,IAAjB,CAAnC,EAA2D;AACzDJ,MAAAA,GAAG,CAACK,gBAAJ,CAAqB,KAArB;AACD,KAFD,MAEO;AACLjB,MAAAA,iBAAiB,CAAC,KAAD,EAAQW,KAAK,CAACE,SAAN,CAAgBC,KAAxB,EAA+BF,GAAG,CAACM,QAAnC,CAAjB;AACD;AACF;AAZ6B,CAAzB",
25
29
  "sourcesContent": [
26
- "import * as React from 'react';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title2: ICommand = {\n name: 'title2',\n keyCommand: 'title2',\n shortcuts: 'ctrlcmd+2',\n buttonProps: { 'aria-label': 'Insert title2', title: 'Insert title 2' },\n icon: <div style={{ fontSize: 16, textAlign: 'left' }}>Title 2</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n let modifyText = `## ${state.selectedText}\\n`;\n if (!state.selectedText) {\n modifyText = `## `;\n }\n api.replaceSelection(modifyText);\n },\n};\n"
30
+ "import * as React from 'react';\nimport { insertAtLineStart } from '../utils/InsertTextAtPosition';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title2: ICommand = {\n name: 'title2',\n keyCommand: 'title2',\n shortcuts: 'ctrlcmd+2',\n buttonProps: { 'aria-label': 'Insert title2', title: 'Insert title 2' },\n icon: <div style={{ fontSize: 16, textAlign: 'left' }}>Title 2</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n if (state.selection.start === 0 || /\\n$/.test(state.text)) {\n api.replaceSelection('## ');\n } else {\n insertAtLineStart('## ', state.selection.start, api.textArea);\n }\n },\n};\n"
27
31
  ]
28
32
  }
@@ -1,4 +1,5 @@
1
1
  import * as React from 'react';
2
+ import { insertAtLineStart } from '../utils/InsertTextAtPosition';
2
3
  import { jsx as _jsx } from "react/jsx-runtime";
3
4
  export var title3 = {
4
5
  name: 'title3',
@@ -16,13 +17,11 @@ export var title3 = {
16
17
  children: "Title 3"
17
18
  }),
18
19
  execute: (state, api) => {
19
- var modifyText = "### " + state.selectedText + "\n";
20
-
21
- if (!state.selectedText) {
22
- modifyText = "### ";
20
+ if (state.selection.start === 0 || /\n$/.test(state.text)) {
21
+ api.replaceSelection('### ');
22
+ } else {
23
+ insertAtLineStart('### ', state.selection.start, api.textArea);
23
24
  }
24
-
25
- api.replaceSelection(modifyText);
26
25
  }
27
26
  };
28
27
  //# sourceMappingURL=title3.js.map
@@ -5,6 +5,7 @@
5
5
  ],
6
6
  "names": [
7
7
  "React",
8
+ "insertAtLineStart",
8
9
  "title3",
9
10
  "name",
10
11
  "keyCommand",
@@ -17,12 +18,15 @@
17
18
  "execute",
18
19
  "state",
19
20
  "api",
20
- "modifyText",
21
- "selectedText",
22
- "replaceSelection"
21
+ "selection",
22
+ "start",
23
+ "test",
24
+ "text",
25
+ "replaceSelection",
26
+ "textArea"
23
27
  ],
24
- "mappings": "AAAA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;;AAGA,OAAO,IAAMC,MAAgB,GAAG;AAC9BC,EAAAA,IAAI,EAAE,QADwB;AAE9BC,EAAAA,UAAU,EAAE,QAFkB;AAG9BC,EAAAA,SAAS,EAAE,WAHmB;AAI9BC,EAAAA,WAAW,EAAE;AAAE,kBAAc,eAAhB;AAAiCC,IAAAA,KAAK,EAAE;AAAxC,GAJiB;AAK9BC,EAAAA,IAAI,eAAE;AAAK,IAAA,KAAK,EAAE;AAAEC,MAAAA,QAAQ,EAAE,EAAZ;AAAgBC,MAAAA,SAAS,EAAE;AAA3B,KAAZ;AAAA;AAAA,IALwB;AAM9BC,EAAAA,OAAO,EAAE,CAACC,KAAD,EAAmBC,GAAnB,KAA4C;AACnD,QAAIC,UAAU,YAAUF,KAAK,CAACG,YAAhB,OAAd;;AACA,QAAI,CAACH,KAAK,CAACG,YAAX,EAAyB;AACvBD,MAAAA,UAAU,SAAV;AACD;;AACDD,IAAAA,GAAG,CAACG,gBAAJ,CAAqBF,UAArB;AACD;AAZ6B,CAAzB",
28
+ "mappings": "AAAA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;AACA,SAASC,iBAAT,QAAkC,+BAAlC;;AAGA,OAAO,IAAMC,MAAgB,GAAG;AAC9BC,EAAAA,IAAI,EAAE,QADwB;AAE9BC,EAAAA,UAAU,EAAE,QAFkB;AAG9BC,EAAAA,SAAS,EAAE,WAHmB;AAI9BC,EAAAA,WAAW,EAAE;AAAE,kBAAc,eAAhB;AAAiCC,IAAAA,KAAK,EAAE;AAAxC,GAJiB;AAK9BC,EAAAA,IAAI,eAAE;AAAK,IAAA,KAAK,EAAE;AAAEC,MAAAA,QAAQ,EAAE,EAAZ;AAAgBC,MAAAA,SAAS,EAAE;AAA3B,KAAZ;AAAA;AAAA,IALwB;AAM9BC,EAAAA,OAAO,EAAE,CAACC,KAAD,EAAmBC,GAAnB,KAA4C;AACnD,QAAID,KAAK,CAACE,SAAN,CAAgBC,KAAhB,KAA0B,CAA1B,IAA+B,MAAMC,IAAN,CAAWJ,KAAK,CAACK,IAAjB,CAAnC,EAA2D;AACzDJ,MAAAA,GAAG,CAACK,gBAAJ,CAAqB,MAArB;AACD,KAFD,MAEO;AACLjB,MAAAA,iBAAiB,CAAC,MAAD,EAASW,KAAK,CAACE,SAAN,CAAgBC,KAAzB,EAAgCF,GAAG,CAACM,QAApC,CAAjB;AACD;AACF;AAZ6B,CAAzB",
25
29
  "sourcesContent": [
26
- "import * as React from 'react';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title3: ICommand = {\n name: 'title3',\n keyCommand: 'title3',\n shortcuts: 'ctrlcmd+3',\n buttonProps: { 'aria-label': 'Insert title3', title: 'Insert title 3' },\n icon: <div style={{ fontSize: 15, textAlign: 'left' }}>Title 3</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n let modifyText = `### ${state.selectedText}\\n`;\n if (!state.selectedText) {\n modifyText = `### `;\n }\n api.replaceSelection(modifyText);\n },\n};\n"
30
+ "import * as React from 'react';\nimport { insertAtLineStart } from '../utils/InsertTextAtPosition';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title3: ICommand = {\n name: 'title3',\n keyCommand: 'title3',\n shortcuts: 'ctrlcmd+3',\n buttonProps: { 'aria-label': 'Insert title3', title: 'Insert title 3' },\n icon: <div style={{ fontSize: 15, textAlign: 'left' }}>Title 3</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n if (state.selection.start === 0 || /\\n$/.test(state.text)) {\n api.replaceSelection('### ');\n } else {\n insertAtLineStart('### ', state.selection.start, api.textArea);\n }\n },\n};\n"
27
31
  ]
28
32
  }
@@ -1,4 +1,5 @@
1
1
  import * as React from 'react';
2
+ import { insertAtLineStart } from '../utils/InsertTextAtPosition';
2
3
  import { jsx as _jsx } from "react/jsx-runtime";
3
4
  export var title4 = {
4
5
  name: 'title4',
@@ -16,13 +17,11 @@ export var title4 = {
16
17
  children: "Title 4"
17
18
  }),
18
19
  execute: (state, api) => {
19
- var modifyText = "#### " + state.selectedText + "\n";
20
-
21
- if (!state.selectedText) {
22
- modifyText = "#### ";
20
+ if (state.selection.start === 0 || /\n$/.test(state.text)) {
21
+ api.replaceSelection('#### ');
22
+ } else {
23
+ insertAtLineStart('#### ', state.selection.start, api.textArea);
23
24
  }
24
-
25
- api.replaceSelection(modifyText);
26
25
  }
27
26
  };
28
27
  //# sourceMappingURL=title4.js.map
@@ -5,6 +5,7 @@
5
5
  ],
6
6
  "names": [
7
7
  "React",
8
+ "insertAtLineStart",
8
9
  "title4",
9
10
  "name",
10
11
  "keyCommand",
@@ -17,12 +18,15 @@
17
18
  "execute",
18
19
  "state",
19
20
  "api",
20
- "modifyText",
21
- "selectedText",
22
- "replaceSelection"
21
+ "selection",
22
+ "start",
23
+ "test",
24
+ "text",
25
+ "replaceSelection",
26
+ "textArea"
23
27
  ],
24
- "mappings": "AAAA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;;AAGA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CAAWJ,KAAK,CAACK,IAAjB,CAAnC,EAA2D;AACzDJ,MAAAA,GAAG,CAACK,gBAAJ,CAAqB,OAArB;AACD,KAFD,MAEO;AACLjB,MAAAA,iBAAiB,CAAC,OAAD,EAAUW,KAAK,CAACE,SAAN,CAAgBC,KAA1B,EAAiCF,GAAG,CAACM,QAArC,CAAjB;AACD;AACF;AAZ6B,CAAzB",
25
29
  "sourcesContent": [
26
- "import * as React from 'react';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title4: ICommand = {\n name: 'title4',\n keyCommand: 'title4',\n shortcuts: 'ctrlcmd+4',\n buttonProps: { 'aria-label': 'Insert title4', title: 'Insert title 4' },\n icon: <div style={{ fontSize: 14, textAlign: 'left' }}>Title 4</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n let modifyText = `#### ${state.selectedText}\\n`;\n if (!state.selectedText) {\n modifyText = `#### `;\n }\n api.replaceSelection(modifyText);\n },\n};\n"
30
+ "import * as React from 'react';\nimport { insertAtLineStart } from '../utils/InsertTextAtPosition';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title4: ICommand = {\n name: 'title4',\n keyCommand: 'title4',\n shortcuts: 'ctrlcmd+4',\n buttonProps: { 'aria-label': 'Insert title4', title: 'Insert title 4' },\n icon: <div style={{ fontSize: 14, textAlign: 'left' }}>Title 4</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n if (state.selection.start === 0 || /\\n$/.test(state.text)) {\n api.replaceSelection('#### ');\n } else {\n insertAtLineStart('#### ', state.selection.start, api.textArea);\n }\n },\n};\n"
27
31
  ]
28
32
  }
@@ -1,4 +1,5 @@
1
1
  import * as React from 'react';
2
+ import { insertAtLineStart } from '../utils/InsertTextAtPosition';
2
3
  import { jsx as _jsx } from "react/jsx-runtime";
3
4
  export var title5 = {
4
5
  name: 'title5',
@@ -16,13 +17,11 @@ export var title5 = {
16
17
  children: "Title 5"
17
18
  }),
18
19
  execute: (state, api) => {
19
- var modifyText = "##### " + state.selectedText + "\n";
20
-
21
- if (!state.selectedText) {
22
- modifyText = "##### ";
20
+ if (state.selection.start === 0 || /\n$/.test(state.text)) {
21
+ api.replaceSelection('##### ');
22
+ } else {
23
+ insertAtLineStart('##### ', state.selection.start, api.textArea);
23
24
  }
24
-
25
- api.replaceSelection(modifyText);
26
25
  }
27
26
  };
28
27
  //# sourceMappingURL=title5.js.map
@@ -5,6 +5,7 @@
5
5
  ],
6
6
  "names": [
7
7
  "React",
8
+ "insertAtLineStart",
8
9
  "title5",
9
10
  "name",
10
11
  "keyCommand",
@@ -17,12 +18,15 @@
17
18
  "execute",
18
19
  "state",
19
20
  "api",
20
- "modifyText",
21
- "selectedText",
22
- "replaceSelection"
21
+ "selection",
22
+ "start",
23
+ "test",
24
+ "text",
25
+ "replaceSelection",
26
+ "textArea"
23
27
  ],
24
- "mappings": "AAAA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;;AAGA,OAAO,IAAMC,MAAgB,GAAG;AAC9BC,EAAAA,IAAI,EAAE,QADwB;AAE9BC,EAAAA,UAAU,EAAE,QAFkB;AAG9BC,EAAAA,SAAS,EAAE,WAHmB;AAI9BC,EAAAA,WAAW,EAAE;AAAE,kBAAc,eAAhB;AAAiCC,IAAAA,KAAK,EAAE;AAAxC,GAJiB;AAK9BC,EAAAA,IAAI,eAAE;AAAK,IAAA,KAAK,EAAE;AAAEC,MAAAA,QAAQ,EAAE,EAAZ;AAAgBC,MAAAA,SAAS,EAAE;AAA3B,KAAZ;AAAA;AAAA,IALwB;AAM9BC,EAAAA,OAAO,EAAE,CAACC,KAAD,EAAmBC,GAAnB,KAA4C;AACnD,QAAIC,UAAU,cAAYF,KAAK,CAACG,YAAlB,OAAd;;AACA,QAAI,CAACH,KAAK,CAACG,YAAX,EAAyB;AACvBD,MAAAA,UAAU,WAAV;AACD;;AACDD,IAAAA,GAAG,CAACG,gBAAJ,CAAqBF,UAArB;AACD;AAZ6B,CAAzB",
28
+ "mappings": "AAAA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;AACA,SAASC,iBAAT,QAAkC,+BAAlC;;AAGA,OAAO,IAAMC,MAAgB,GAAG;AAC9BC,EAAAA,IAAI,EAAE,QADwB;AAE9BC,EAAAA,UAAU,EAAE,QAFkB;AAG9BC,EAAAA,SAAS,EAAE,WAHmB;AAI9BC,EAAAA,WAAW,EAAE;AAAE,kBAAc,eAAhB;AAAiCC,IAAAA,KAAK,EAAE;AAAxC,GAJiB;AAK9BC,EAAAA,IAAI,eAAE;AAAK,IAAA,KAAK,EAAE;AAAEC,MAAAA,QAAQ,EAAE,EAAZ;AAAgBC,MAAAA,SAAS,EAAE;AAA3B,KAAZ;AAAA;AAAA,IALwB;AAM9BC,EAAAA,OAAO,EAAE,CAACC,KAAD,EAAmBC,GAAnB,KAA4C;AACnD,QAAID,KAAK,CAACE,SAAN,CAAgBC,KAAhB,KAA0B,CAA1B,IAA+B,MAAMC,IAAN,CAAWJ,KAAK,CAACK,IAAjB,CAAnC,EAA2D;AACzDJ,MAAAA,GAAG,CAACK,gBAAJ,CAAqB,QAArB;AACD,KAFD,MAEO;AACLjB,MAAAA,iBAAiB,CAAC,QAAD,EAAWW,KAAK,CAACE,SAAN,CAAgBC,KAA3B,EAAkCF,GAAG,CAACM,QAAtC,CAAjB;AACD;AACF;AAZ6B,CAAzB",
25
29
  "sourcesContent": [
26
- "import * as React from 'react';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title5: ICommand = {\n name: 'title5',\n keyCommand: 'title5',\n shortcuts: 'ctrlcmd+5',\n buttonProps: { 'aria-label': 'Insert title5', title: 'Insert title 5' },\n icon: <div style={{ fontSize: 12, textAlign: 'left' }}>Title 5</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n let modifyText = `##### ${state.selectedText}\\n`;\n if (!state.selectedText) {\n modifyText = `##### `;\n }\n api.replaceSelection(modifyText);\n },\n};\n"
30
+ "import * as React from 'react';\nimport { insertAtLineStart } from '../utils/InsertTextAtPosition';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title5: ICommand = {\n name: 'title5',\n keyCommand: 'title5',\n shortcuts: 'ctrlcmd+5',\n buttonProps: { 'aria-label': 'Insert title5', title: 'Insert title 5' },\n icon: <div style={{ fontSize: 12, textAlign: 'left' }}>Title 5</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n if (state.selection.start === 0 || /\\n$/.test(state.text)) {\n api.replaceSelection('##### ');\n } else {\n insertAtLineStart('##### ', state.selection.start, api.textArea);\n }\n },\n};\n"
27
31
  ]
28
32
  }
@@ -1,4 +1,5 @@
1
1
  import * as React from 'react';
2
+ import { insertAtLineStart } from '../utils/InsertTextAtPosition';
2
3
  import { jsx as _jsx } from "react/jsx-runtime";
3
4
  export var title6 = {
4
5
  name: 'title6',
@@ -16,13 +17,11 @@ export var title6 = {
16
17
  children: "Title 6"
17
18
  }),
18
19
  execute: (state, api) => {
19
- var modifyText = "###### " + state.selectedText + "\n";
20
-
21
- if (!state.selectedText) {
22
- modifyText = "###### ";
20
+ if (state.selection.start === 0 || /\n$/.test(state.text)) {
21
+ api.replaceSelection('###### ');
22
+ } else {
23
+ insertAtLineStart('###### ', state.selection.start, api.textArea);
23
24
  }
24
-
25
- api.replaceSelection(modifyText);
26
25
  }
27
26
  };
28
27
  //# sourceMappingURL=title6.js.map
@@ -5,6 +5,7 @@
5
5
  ],
6
6
  "names": [
7
7
  "React",
8
+ "insertAtLineStart",
8
9
  "title6",
9
10
  "name",
10
11
  "keyCommand",
@@ -17,12 +18,15 @@
17
18
  "execute",
18
19
  "state",
19
20
  "api",
20
- "modifyText",
21
- "selectedText",
22
- "replaceSelection"
21
+ "selection",
22
+ "start",
23
+ "test",
24
+ "text",
25
+ "replaceSelection",
26
+ "textArea"
23
27
  ],
24
- "mappings": "AAAA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;;AAGA,OAAO,IAAMC,MAAgB,GAAG;AAC9BC,EAAAA,IAAI,EAAE,QADwB;AAE9BC,E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BAAJ,CAAqB,SAArB;AACD,KAFD,MAEO;AACLjB,MAAAA,iBAAiB,CAAC,SAAD,EAAYW,KAAK,CAACE,SAAN,CAAgBC,KAA5B,EAAmCF,GAAG,CAACM,QAAvC,CAAjB;AACD;AACF;AAZ6B,CAAzB",
25
29
  "sourcesContent": [
26
- "import * as React from 'react';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title6: ICommand = {\n name: 'title6',\n keyCommand: 'title6',\n shortcuts: 'ctrlcmd+6',\n buttonProps: { 'aria-label': 'Insert title6', title: 'Insert title 6' },\n icon: <div style={{ fontSize: 12, textAlign: 'left' }}>Title 6</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n let modifyText = `###### ${state.selectedText}\\n`;\n if (!state.selectedText) {\n modifyText = `###### `;\n }\n api.replaceSelection(modifyText);\n },\n};\n"
30
+ "import * as React from 'react';\nimport { insertAtLineStart } from '../utils/InsertTextAtPosition';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title6: ICommand = {\n name: 'title6',\n keyCommand: 'title6',\n shortcuts: 'ctrlcmd+6',\n buttonProps: { 'aria-label': 'Insert title6', title: 'Insert title 6' },\n icon: <div style={{ fontSize: 12, textAlign: 'left' }}>Title 6</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n if (state.selection.start === 0 || /\\n$/.test(state.text)) {\n api.replaceSelection('###### ');\n } else {\n insertAtLineStart('###### ', state.selection.start, api.textArea);\n }\n },\n};\n"
27
31
  ]
28
32
  }
@@ -3,6 +3,13 @@
3
3
  * Copyright (c) 2018 Dmitriy Kubyshkin
4
4
  * Copied from https://github.com/grassator/insert-text-at-cursor
5
5
  */
6
+ /**
7
+ * @param {string} val
8
+ * @param {number} cursorIdx
9
+ * @param {HTMLTextAreaElement|HTMLInputElement} input
10
+ * @return {void}
11
+ */
12
+ export declare const insertAtLineStart: (val: string, cursorIdx: number, input: HTMLTextAreaElement | HTMLInputElement) => void;
6
13
  /**
7
14
  * @param {HTMLTextAreaElement|HTMLInputElement} input
8
15
  * @param {string} text
@@ -22,13 +22,39 @@ function canManipulateViaTextNodes(input) {
22
22
 
23
23
  return browserSupportsTextareaTextNodes;
24
24
  }
25
+ /**
26
+ * @param {string} val
27
+ * @param {number} cursorIdx
28
+ * @param {HTMLTextAreaElement|HTMLInputElement} input
29
+ * @return {void}
30
+ */
31
+
32
+
33
+ export var insertAtLineStart = (val, cursorIdx, input) => {
34
+ var content = input.value;
35
+ var startIdx = 0;
36
+
37
+ while (cursorIdx--) {
38
+ var char = content[cursorIdx];
39
+
40
+ if (char === '\n') {
41
+ startIdx = cursorIdx + 1;
42
+ break;
43
+ }
44
+ }
45
+
46
+ input.focus();
47
+ input.setRangeText(val, startIdx, startIdx);
48
+ input.dispatchEvent(new Event('input', {
49
+ bubbles: true
50
+ }));
51
+ };
25
52
  /**
26
53
  * @param {HTMLTextAreaElement|HTMLInputElement} input
27
54
  * @param {string} text
28
55
  * @returns {void}
29
56
  */
30
57
 
31
-
32
58
  export function insertTextAtPosition(input, text) {
33
59
  // Most of the used APIs only work with the field selected
34
60
  input.focus(); // IE 8-10
@@ -13,9 +13,19 @@
13
13
  "createElement",
14
14
  "value",
15
15
  "firstChild",
16
+ "insertAtLineStart",
17
+ "val",
18
+ "cursorIdx",
19
+ "content",
20
+ "startIdx",
21
+ "char",
22
+ "focus",
23
+ "setRangeText",
24
+ "dispatchEvent",
25
+ "Event",
26
+ "bubbles",
16
27
  "insertTextAtPosition",
17
28
  "text",
18
- "focus",
19
29
  "selection",
20
30
  "ieRange",
21
31
  "createRange",
@@ -27,7 +37,6 @@
27
37
  "selectionStart",
28
38
  "end",
29
39
  "selectionEnd",
30
- "setRangeText",
31
40
  "range",
32
41
  "textNode",
33
42
  "createTextNode",
@@ -49,11 +58,10 @@
49
58
  "setSelectionRange",
50
59
  "e",
51
60
  "createEvent",
52
- "initEvent",
53
- "dispatchEvent"
61
+ "initEvent"
54
62
  ],
55
- "mappings": "AAAA;AACA;AACA;AACA;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AK,KAAKE,GAAd,EAAmB;AACjBE,YAAAA,KAAK,CAACc,cAAN;AACD;AACF;AACF,OAvCI,CAyCL;AACA;AACA;AACA;AACA;;;AACA,UAAI9C,yBAAyB,CAACC,KAAD,CAAzB,IAAoC+B,KAAK,CAACe,uBAAN,CAA8B7C,QAA9B,KAA2C,OAAnF,EAA4F;AAC1F;AACA8B,QAAAA,KAAK,CAACgB,UAAN,CAAiBf,QAAjB;AACD,OAHD,MAGO;AACL;AACA,YAAM3B,KAAK,GAAGL,KAAK,CAACK,KAApB;AACAL,QAAAA,KAAK,CAACK,KAAN,GAAcA,KAAK,CAAC2C,KAAN,CAAY,CAAZ,EAAerB,KAAf,IAAwBR,IAAxB,GAA+Bd,KAAK,CAAC2C,KAAN,CAAYnB,GAAZ,CAA7C;AACD;AACF,KA5Da,CA8Dd;;;AACA7B,IAAAA,KAAK,CAACiD,iBAAN,CAAwBtB,KAAK,GAAGR,IAAI,CAACsB,MAArC,EAA6Cd,KAAK,GAAGR,IAAI,CAACsB,MAA1D,EA/Dc,CAiEd;;AACA,QAAMS,CAAC,GAAG/C,QAAQ,CAACgD,WAAT,CAAqB,SAArB,CAAV;AACAD,IAAAA,CAAC,CAACE,SAAF,CAAY,OAAZ,EAAqB,IAArB,EAA2B,KAA3B;AACApD,IAAAA,KAAK,CAACe,aAAN,CAAoBmC,CAApB;AACD;AACF",
56
64
  "sourcesContent": [
57
- "/**\n * The MIT License\n * Copyright (c) 2018 Dmitriy Kubyshkin\n * Copied from https://github.com/grassator/insert-text-at-cursor\n */\n\nlet browserSupportsTextareaTextNodes: any;\n\n/**\n * @param {HTMLElement} input\n * @return {boolean}\n */\nfunction canManipulateViaTextNodes(input: HTMLTextAreaElement | HTMLInputElement) {\n if (input.nodeName !== 'TEXTAREA') {\n return false;\n }\n if (typeof browserSupportsTextareaTextNodes === 'undefined') {\n const textarea: HTMLTextAreaElement = document.createElement('textarea');\n textarea.value = '1';\n browserSupportsTextareaTextNodes = !!textarea.firstChild;\n }\n return browserSupportsTextareaTextNodes;\n}\n\n/**\n * @param {HTMLTextAreaElement|HTMLInputElement} input\n * @param {string} text\n * @returns {void}\n */\nexport function insertTextAtPosition(input: HTMLTextAreaElement | HTMLInputElement, text: string) {\n // Most of the used APIs only work with the field selected\n input.focus();\n\n // IE 8-10\n if ((document as any).selection) {\n const ieRange = (document as any).selection.createRange();\n ieRange.text = text;\n\n // Move cursor after the inserted text\n ieRange.collapse(false /* to the end */);\n ieRange.select();\n\n return;\n }\n\n // Webkit + Edge\n const isSuccess = document.execCommand && document.execCommand('insertText', false, text);\n if (!isSuccess) {\n const start = input.selectionStart!;\n const end = input.selectionEnd!;\n // Firefox (non-standard method)\n if (typeof input.setRangeText === 'function') {\n input.setRangeText(text);\n } else {\n // To make a change we just need a Range, not a Selection\n const range = document.createRange();\n const textNode = document.createTextNode(text);\n\n if (canManipulateViaTextNodes(input)) {\n let node = input.firstChild;\n\n // If textarea is empty, just insert the text\n if (!node) {\n input.appendChild(textNode);\n } else {\n // Otherwise we need to find a nodes for start and end\n let offset = 0;\n let startNode = null;\n let endNode = null;\n\n while (node && (startNode === null || endNode === null)) {\n const nodeLength = node.nodeValue!.length;\n\n // if start of the selection falls into current node\n if (start >= offset && start <= offset + nodeLength) {\n range.setStart((startNode = node), start - offset);\n }\n\n // if end of the selection falls into current node\n if (end >= offset && end <= offset + nodeLength) {\n range.setEnd((endNode = node), end - offset);\n }\n\n offset += nodeLength;\n node = node.nextSibling;\n }\n\n // If there is some text selected, remove it as we should replace it\n if (start !== end) {\n range.deleteContents();\n }\n }\n }\n\n // If the node is a textarea and the range doesn't span outside the element\n //\n // Get the commonAncestorContainer of the selected range and test its type\n // If the node is of type `#text` it means that we're still working with text nodes within our textarea element\n // otherwise, if it's of type `#document` for example it means our selection spans outside the textarea.\n if (canManipulateViaTextNodes(input) && range.commonAncestorContainer.nodeName === '#text') {\n // Finally insert a new node. The browser will automatically split start and end nodes into two if necessary\n range.insertNode(textNode);\n } else {\n // If the node is not a textarea or the range spans outside a textarea the only way is to replace the whole value\n const value = input.value;\n input.value = value.slice(0, start) + text + value.slice(end);\n }\n }\n\n // Correct the cursor position to be at the end of the insertion\n input.setSelectionRange(start + text.length, start + text.length);\n\n // Notify any possible listeners of the change\n const e = document.createEvent('UIEvent');\n e.initEvent('input', true, false);\n input.dispatchEvent(e);\n }\n}\n"
65
+ "/**\n * The MIT License\n * Copyright (c) 2018 Dmitriy Kubyshkin\n * Copied from https://github.com/grassator/insert-text-at-cursor\n */\n\nlet browserSupportsTextareaTextNodes: any;\n\n/**\n * @param {HTMLElement} input\n * @return {boolean}\n */\nfunction canManipulateViaTextNodes(input: HTMLTextAreaElement | HTMLInputElement): boolean {\n if (input.nodeName !== 'TEXTAREA') {\n return false;\n }\n if (typeof browserSupportsTextareaTextNodes === 'undefined') {\n const textarea: HTMLTextAreaElement = document.createElement('textarea');\n textarea.value = '1';\n browserSupportsTextareaTextNodes = !!textarea.firstChild;\n }\n return browserSupportsTextareaTextNodes;\n}\n\n/**\n * @param {string} val\n * @param {number} cursorIdx\n * @param {HTMLTextAreaElement|HTMLInputElement} input\n * @return {void}\n */\nexport const insertAtLineStart = (\n val: string,\n cursorIdx: number,\n input: HTMLTextAreaElement | HTMLInputElement,\n): void => {\n const content = input.value;\n let startIdx = 0;\n\n while (cursorIdx--) {\n let char = content[cursorIdx];\n if (char === '\\n') {\n startIdx = cursorIdx + 1;\n break;\n }\n }\n\n input.focus();\n input.setRangeText(val, startIdx, startIdx);\n input.dispatchEvent(new Event('input', { bubbles: true }));\n};\n\n/**\n * @param {HTMLTextAreaElement|HTMLInputElement} input\n * @param {string} text\n * @returns {void}\n */\nexport function insertTextAtPosition(input: HTMLTextAreaElement | HTMLInputElement, text: string): void {\n // Most of the used APIs only work with the field selected\n input.focus();\n\n // IE 8-10\n if ((document as any).selection) {\n const ieRange = (document as any).selection.createRange();\n ieRange.text = text;\n\n // Move cursor after the inserted text\n ieRange.collapse(false /* to the end */);\n ieRange.select();\n\n return;\n }\n\n // Webkit + Edge\n const isSuccess = document.execCommand && document.execCommand('insertText', false, text);\n if (!isSuccess) {\n const start = input.selectionStart!;\n const end = input.selectionEnd!;\n // Firefox (non-standard method)\n if (typeof input.setRangeText === 'function') {\n input.setRangeText(text);\n } else {\n // To make a change we just need a Range, not a Selection\n const range = document.createRange();\n const textNode = document.createTextNode(text);\n\n if (canManipulateViaTextNodes(input)) {\n let node = input.firstChild;\n\n // If textarea is empty, just insert the text\n if (!node) {\n input.appendChild(textNode);\n } else {\n // Otherwise we need to find a nodes for start and end\n let offset = 0;\n let startNode = null;\n let endNode = null;\n\n while (node && (startNode === null || endNode === null)) {\n const nodeLength = node.nodeValue!.length;\n\n // if start of the selection falls into current node\n if (start >= offset && start <= offset + nodeLength) {\n range.setStart((startNode = node), start - offset);\n }\n\n // if end of the selection falls into current node\n if (end >= offset && end <= offset + nodeLength) {\n range.setEnd((endNode = node), end - offset);\n }\n\n offset += nodeLength;\n node = node.nextSibling;\n }\n\n // If there is some text selected, remove it as we should replace it\n if (start !== end) {\n range.deleteContents();\n }\n }\n }\n\n // If the node is a textarea and the range doesn't span outside the element\n //\n // Get the commonAncestorContainer of the selected range and test its type\n // If the node is of type `#text` it means that we're still working with text nodes within our textarea element\n // otherwise, if it's of type `#document` for example it means our selection spans outside the textarea.\n if (canManipulateViaTextNodes(input) && range.commonAncestorContainer.nodeName === '#text') {\n // Finally insert a new node. The browser will automatically split start and end nodes into two if necessary\n range.insertNode(textNode);\n } else {\n // If the node is not a textarea or the range spans outside a textarea the only way is to replace the whole value\n const value = input.value;\n input.value = value.slice(0, start) + text + value.slice(end);\n }\n }\n\n // Correct the cursor position to be at the end of the insertion\n input.setSelectionRange(start + text.length, start + text.length);\n\n // Notify any possible listeners of the change\n const e = document.createEvent('UIEvent');\n e.initEvent('input', true, false);\n input.dispatchEvent(e);\n }\n}\n"
58
66
  ]
59
67
  }
@@ -9,6 +9,8 @@ exports.title1 = void 0;
9
9
 
10
10
  var _react = _interopRequireDefault(require("react"));
11
11
 
12
+ var _InsertTextAtPosition = require("../utils/InsertTextAtPosition");
13
+
12
14
  var _jsxRuntime = require("react/jsx-runtime");
13
15
 
14
16
  var title1 = {
@@ -27,13 +29,11 @@ var title1 = {
27
29
  children: "Title 1"
28
30
  }),
29
31
  execute: function execute(state, api) {
30
- var modifyText = "# ".concat(state.selectedText, "\n");
31
-
32
- if (!state.selectedText) {
33
- modifyText = "# ";
32
+ if (state.selection.start === 0 || /\n$/.test(state.text)) {
33
+ api.replaceSelection('# ');
34
+ } else {
35
+ (0, _InsertTextAtPosition.insertAtLineStart)('# ', state.selection.start, api.textArea);
34
36
  }
35
-
36
- api.replaceSelection(modifyText);
37
37
  }
38
38
  };
39
39
  exports.title1 = title1;
@@ -16,12 +16,15 @@
16
16
  "execute",
17
17
  "state",
18
18
  "api",
19
- "modifyText",
20
- "selectedText",
21
- "replaceSelection"
19
+ "selection",
20
+ "start",
21
+ "test",
22
+ "text",
23
+ "replaceSelection",
24
+ "textArea"
22
25
  ],
23
- "mappings": ";;;;;;;;;AAAA;;;;AAGO,IAAMA,MAAgB,GAAG;AAC9BC,EAAAA,IAAI,EAAE,QADwB;AAE9BC,EAAAA,UAAU,EAAE,QAFkB;AAG9BC,EAAAA,SAAS,EAAE,WAHmB;AAI9BC,EAAAA,WAAW,EAAE;AAAE,kBAAc,gBAAhB;AAAkCC,IAAAA,KAAK,EAAE;AAAzC,GAJiB;AAK9BC,EAAAA,IAAI,eAAE;AAAK,IAAA,KAAK,EAAE;AAAEC,MAAAA,QAAQ,EAAE,EAAZ;AAAgBC,MAAAA,SAAS,EAAE;AAA3B,KAAZ;AAAA;AAAA,IALwB;AAM9BC,EAAAA,OAAO,EAAE,iBAACC,KAAD,EAAmBC,GAAnB,EAA4C;AACnD,QAAIC,UAAU,eAAQF,KAAK,CAACG,YAAd,OAAd;;AACA,QAAI,CAACH,KAAK,CAACG,YAAX,EAAyB;AACvBD,MAAAA,UAAU,OAAV;AACD;;AACDD,IAAAA,GAAG,CAACG,gBAAJ,CAAqBF,UAArB;AACD;AAZ6B,CAAzB",
26
+ "mappings": ";;;;;;;;;AAAA;;AACA;;;;AAGO,IAAMA,MAAgB,GAAG;AAC9BC,EAAAA,IAAI,EAAE,QADwB;AAE9BC,EAAAA,UAAU,EAAE,QAFkB;AAG9BC,EAAAA,SAAS,EAAE,WAHmB;AAI9BC,EAAAA,WAAW,EAAE;AAAE,kBAAc,gBAAhB;AAAkCC,IAAAA,KAAK,EAAE;AAAzC,GAJiB;AAK9BC,EAAAA,IAAI,eAAE;AAAK,IAAA,KAAK,EAAE;AAAEC,MAAAA,QAAQ,EAAE,EAAZ;AAAgBC,MAAAA,SAAS,EAAE;AAA3B,KAAZ;AAAA;AAAA,IALwB;AAM9BC,EAAAA,OAAO,EAAE,iBAACC,KAAD,EAAmBC,GAAnB,EAA4C;AACnD,QAAID,KAAK,CAACE,SAAN,CAAgBC,KAAhB,KAA0B,CAA1B,IAA+B,MAAMC,IAAN,CAAWJ,KAAK,CAACK,IAAjB,CAAnC,EAA2D;AACzDJ,MAAAA,GAAG,CAACK,gBAAJ,CAAqB,IAArB;AACD,KAFD,MAEO;AACL,mDAAkB,IAAlB,EAAwBN,KAAK,CAACE,SAAN,CAAgBC,KAAxC,EAA+CF,GAAG,CAACM,QAAnD;AACD;AACF;AAZ6B,CAAzB",
24
27
  "sourcesContent": [
25
- "import React from 'react';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title1: ICommand = {\n name: 'title1',\n keyCommand: 'title1',\n shortcuts: 'ctrlcmd+1',\n buttonProps: { 'aria-label': 'Insert title 1', title: 'Insert title 1' },\n icon: <div style={{ fontSize: 18, textAlign: 'left' }}>Title 1</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n let modifyText = `# ${state.selectedText}\\n`;\n if (!state.selectedText) {\n modifyText = `# `;\n }\n api.replaceSelection(modifyText);\n },\n};\n"
28
+ "import React from 'react';\nimport { insertAtLineStart } from '../utils/InsertTextAtPosition';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title1: ICommand = {\n name: 'title1',\n keyCommand: 'title1',\n shortcuts: 'ctrlcmd+1',\n buttonProps: { 'aria-label': 'Insert title 1', title: 'Insert title 1' },\n icon: <div style={{ fontSize: 18, textAlign: 'left' }}>Title 1</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n if (state.selection.start === 0 || /\\n$/.test(state.text)) {\n api.replaceSelection('# ');\n } else {\n insertAtLineStart('# ', state.selection.start, api.textArea);\n }\n },\n};\n"
26
29
  ]
27
30
  }
@@ -9,6 +9,8 @@ exports.title2 = void 0;
9
9
 
10
10
  var React = _interopRequireWildcard(require("react"));
11
11
 
12
+ var _InsertTextAtPosition = require("../utils/InsertTextAtPosition");
13
+
12
14
  var _jsxRuntime = require("react/jsx-runtime");
13
15
 
14
16
  var title2 = {
@@ -27,13 +29,11 @@ var title2 = {
27
29
  children: "Title 2"
28
30
  }),
29
31
  execute: function execute(state, api) {
30
- var modifyText = "## ".concat(state.selectedText, "\n");
31
-
32
- if (!state.selectedText) {
33
- modifyText = "## ";
32
+ if (state.selection.start === 0 || /\n$/.test(state.text)) {
33
+ api.replaceSelection('## ');
34
+ } else {
35
+ (0, _InsertTextAtPosition.insertAtLineStart)('## ', state.selection.start, api.textArea);
34
36
  }
35
-
36
- api.replaceSelection(modifyText);
37
37
  }
38
38
  };
39
39
  exports.title2 = title2;
@@ -16,12 +16,15 @@
16
16
  "execute",
17
17
  "state",
18
18
  "api",
19
- "modifyText",
20
- "selectedText",
21
- "replaceSelection"
19
+ "selection",
20
+ "start",
21
+ "test",
22
+ "text",
23
+ "replaceSelection",
24
+ "textArea"
22
25
  ],
23
- "mappings": ";;;;;;;;;AAAA;;;;AAGO,IAAMA,MAAgB,GAAG;AAC9BC,EAAAA,IAAI,EAAE,QADwB;AAE9BC,EAAAA,UAAU,EAAE,QAFkB;AAG9BC,EAAAA,SAAS,EAAE,WAHmB;AAI9BC,EAAAA,WAAW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;AAZ6B,CAAzB",
24
27
  "sourcesContent": [
25
- "import * as React from 'react';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title2: ICommand = {\n name: 'title2',\n keyCommand: 'title2',\n shortcuts: 'ctrlcmd+2',\n buttonProps: { 'aria-label': 'Insert title2', title: 'Insert title 2' },\n icon: <div style={{ fontSize: 16, textAlign: 'left' }}>Title 2</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n let modifyText = `## ${state.selectedText}\\n`;\n if (!state.selectedText) {\n modifyText = `## `;\n }\n api.replaceSelection(modifyText);\n },\n};\n"
28
+ "import * as React from 'react';\nimport { insertAtLineStart } from '../utils/InsertTextAtPosition';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title2: ICommand = {\n name: 'title2',\n keyCommand: 'title2',\n shortcuts: 'ctrlcmd+2',\n buttonProps: { 'aria-label': 'Insert title2', title: 'Insert title 2' },\n icon: <div style={{ fontSize: 16, textAlign: 'left' }}>Title 2</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n if (state.selection.start === 0 || /\\n$/.test(state.text)) {\n api.replaceSelection('## ');\n } else {\n insertAtLineStart('## ', state.selection.start, api.textArea);\n }\n },\n};\n"
26
29
  ]
27
30
  }
@@ -9,6 +9,8 @@ exports.title3 = void 0;
9
9
 
10
10
  var React = _interopRequireWildcard(require("react"));
11
11
 
12
+ var _InsertTextAtPosition = require("../utils/InsertTextAtPosition");
13
+
12
14
  var _jsxRuntime = require("react/jsx-runtime");
13
15
 
14
16
  var title3 = {
@@ -27,13 +29,11 @@ var title3 = {
27
29
  children: "Title 3"
28
30
  }),
29
31
  execute: function execute(state, api) {
30
- var modifyText = "### ".concat(state.selectedText, "\n");
31
-
32
- if (!state.selectedText) {
33
- modifyText = "### ";
32
+ if (state.selection.start === 0 || /\n$/.test(state.text)) {
33
+ api.replaceSelection('### ');
34
+ } else {
35
+ (0, _InsertTextAtPosition.insertAtLineStart)('### ', state.selection.start, api.textArea);
34
36
  }
35
-
36
- api.replaceSelection(modifyText);
37
37
  }
38
38
  };
39
39
  exports.title3 = title3;
@@ -16,12 +16,15 @@
16
16
  "execute",
17
17
  "state",
18
18
  "api",
19
- "modifyText",
20
- "selectedText",
21
- "replaceSelection"
19
+ "selection",
20
+ "start",
21
+ "test",
22
+ "text",
23
+ "replaceSelection",
24
+ "textArea"
22
25
  ],
23
- "mappings": ";;;;;;;;;AAAA;;;;AAGO,IAAMA,MAAgB,GAAG;AAC9BC,EAAAA,IAAI,EAAE,QADwB;AAE9BC,EAAAA,UAAU,EAAE,QAFkB;AAG9BC,EAAAA,SAAS,EAAE,WAHmB;AAI9BC,EAAAA,WAAW,EAAE;AAAE,kBAAc,eAAhB;AAAiCC,IAAAA,KAAK,EAAE;AAAxC,GAJiB;AAK9BC,EAAAA,IAAI,eAAE;AAAK,IAAA,KAAK,EAAE;AAAEC,MAAAA,QAAQ,EAAE,EAAZ;AAAgBC,MAAAA,SAAS,EAAE;AAA3B,KAAZ;AAAA;AAAA,IALwB;AAM9BC,EAAAA,OAAO,EAAE,iBAACC,KAAD,EAAmBC,GAAnB,EAA4C;AACnD,QAAIC,UAAU,iBAAUF,KAAK,CAACG,YAAhB,OAAd;;AACA,QAAI,CAACH,KAAK,CAACG,YAAX,EAAyB;AACvBD,MAAAA,UAAU,SAAV;AACD;;AACDD,IAAAA,GAAG,CAACG,gBAAJ,CAAqBF,UAArB;AACD;AAZ6B,CAAzB",
26
+ "mappings": ";;;;;;;;;AAAA;;AACA;;;;AAGO,IAAMA,MAAgB,GAAG;AAC9BC,EAAAA,IAAI,EAAE,QADwB;AAE9BC,EAAAA,UAAU,EAAE,QAFkB;AAG9BC,EAAAA,SAAS,EAAE,WAHmB;AAI9BC,EAAAA,WAAW,EAAE;AAAE,kBAAc,eAAhB;AAAiCC,IAAAA,KAAK,EAAE;AAAxC,GAJiB;AAK9BC,EAAAA,IAAI,eAAE;AAAK,IAAA,KAAK,EAAE;AAAEC,MAAAA,QAAQ,EAAE,EAAZ;AAAgBC,MAAAA,SAAS,EAAE;AAA3B,KAAZ;AAAA;AAAA,IALwB;AAM9BC,EAAAA,OAAO,EAAE,iBAACC,KAAD,EAAmBC,GAAnB,EAA4C;AACnD,QAAID,KAAK,CAACE,SAAN,CAAgBC,KAAhB,KAA0B,CAA1B,IAA+B,MAAMC,IAAN,CAAWJ,KAAK,CAACK,IAAjB,CAAnC,EAA2D;AACzDJ,MAAAA,GAAG,CAACK,gBAAJ,CAAqB,MAArB;AACD,KAFD,MAEO;AACL,mDAAkB,MAAlB,EAA0BN,KAAK,CAACE,SAAN,CAAgBC,KAA1C,EAAiDF,GAAG,CAACM,QAArD;AACD;AACF;AAZ6B,CAAzB",
24
27
  "sourcesContent": [
25
- "import * as React from 'react';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title3: ICommand = {\n name: 'title3',\n keyCommand: 'title3',\n shortcuts: 'ctrlcmd+3',\n buttonProps: { 'aria-label': 'Insert title3', title: 'Insert title 3' },\n icon: <div style={{ fontSize: 15, textAlign: 'left' }}>Title 3</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n let modifyText = `### ${state.selectedText}\\n`;\n if (!state.selectedText) {\n modifyText = `### `;\n }\n api.replaceSelection(modifyText);\n },\n};\n"
28
+ "import * as React from 'react';\nimport { insertAtLineStart } from '../utils/InsertTextAtPosition';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title3: ICommand = {\n name: 'title3',\n keyCommand: 'title3',\n shortcuts: 'ctrlcmd+3',\n buttonProps: { 'aria-label': 'Insert title3', title: 'Insert title 3' },\n icon: <div style={{ fontSize: 15, textAlign: 'left' }}>Title 3</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n if (state.selection.start === 0 || /\\n$/.test(state.text)) {\n api.replaceSelection('### ');\n } else {\n insertAtLineStart('### ', state.selection.start, api.textArea);\n }\n },\n};\n"
26
29
  ]
27
30
  }
@@ -9,6 +9,8 @@ exports.title4 = void 0;
9
9
 
10
10
  var React = _interopRequireWildcard(require("react"));
11
11
 
12
+ var _InsertTextAtPosition = require("../utils/InsertTextAtPosition");
13
+
12
14
  var _jsxRuntime = require("react/jsx-runtime");
13
15
 
14
16
  var title4 = {
@@ -27,13 +29,11 @@ var title4 = {
27
29
  children: "Title 4"
28
30
  }),
29
31
  execute: function execute(state, api) {
30
- var modifyText = "#### ".concat(state.selectedText, "\n");
31
-
32
- if (!state.selectedText) {
33
- modifyText = "#### ";
32
+ if (state.selection.start === 0 || /\n$/.test(state.text)) {
33
+ api.replaceSelection('#### ');
34
+ } else {
35
+ (0, _InsertTextAtPosition.insertAtLineStart)('#### ', state.selection.start, api.textArea);
34
36
  }
35
-
36
- api.replaceSelection(modifyText);
37
37
  }
38
38
  };
39
39
  exports.title4 = title4;
@@ -16,12 +16,15 @@
16
16
  "execute",
17
17
  "state",
18
18
  "api",
19
- "modifyText",
20
- "selectedText",
21
- "replaceSelection"
19
+ "selection",
20
+ "start",
21
+ "test",
22
+ "text",
23
+ "replaceSelection",
24
+ "textArea"
22
25
  ],
23
- "mappings": ";;;;;;;;;AAAA;;;;AAGO,IAAMA,MAAgB,GAAG;AAC9BC,EAAAA,IAAI,EAAE,QADwB;AAE9BC,EAAAA,UAAU,EAAE,QAFkB;AAG9BC,EAAAA,SAAS,EAAE,WAHmB;AAI9BC,EAAAA,WAAW,EAAE;AAAE,kBAAc,eAAhB;AAAiCC,IAAAA,KAAK,EAAE;AAAxC,GAJiB;AAK9BC,EAAAA,IAAI,eAAE;AAAK,IAAA,KAAK,EAAE;AAAEC,MAAAA,QAAQ,EAAE,EAAZ;AAAgBC,MAAAA,SAAS,EAAE;AAA3B,KAAZ;AAAA;AAAA,IALwB;AAM9BC,EAAAA,OAAO,EAAE,iBAACC,KAAD,EAAmBC,GAAnB,EAA4C;AACnD,QAAIC,UAAU,kBAAWF,KAAK,CAACG,YAAjB,OAAd;;AACA,QAAI,CAACH,KAAK,CAACG,YAAX,EAAyB;AACvBD,MAAAA,UAAU,UAAV;AACD;;AACDD,IAAAA,GAAG,CAACG,gBAAJ,CAAqBF,UAArB;AACD;AAZ6B,CAAzB",
26
+ "mappings": ";;;;;;;;;AAAA;;AACA;;;;AAGO,IAAMA,MAAgB,GAAG;AAC9BC,EAAAA,IAAI,EAAE,QADwB;AAE9BC,EAAAA,UAAU,EAAE,QAFkB;AAG9BC,EAAAA,SAAS,EAAE,WAHmB;AAI9BC,EAAAA,WAAW,EAAE;AAAE,kBAAc,eAAhB;AAAiCC,IAAAA,KAAK,EAAE;AAAxC,GAJiB;AAK9BC,EAAAA,IAAI,eAAE;AAAK,IAAA,KAAK,EAAE;AAAEC,MAAAA,QAAQ,EAAE,EAAZ;AAAgBC,MAAAA,SAAS,EAAE;AAA3B,KAAZ;AAAA;AAAA,IALwB;AAM9BC,EAAAA,OAAO,EAAE,iBAACC,KAAD,EAAmBC,GAAnB,EAA4C;AACnD,QAAID,KAAK,CAACE,SAAN,CAAgBC,KAAhB,KAA0B,CAA1B,IAA+B,MAAMC,IAAN,CAAWJ,KAAK,CAACK,IAAjB,CAAnC,EAA2D;AACzDJ,MAAAA,GAAG,CAACK,gBAAJ,CAAqB,OAArB;AACD,KAFD,MAEO;AACL,mDAAkB,OAAlB,EAA2BN,KAAK,CAACE,SAAN,CAAgBC,KAA3C,EAAkDF,GAAG,CAACM,QAAtD;AACD;AACF;AAZ6B,CAAzB",
24
27
  "sourcesContent": [
25
- "import * as React from 'react';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title4: ICommand = {\n name: 'title4',\n keyCommand: 'title4',\n shortcuts: 'ctrlcmd+4',\n buttonProps: { 'aria-label': 'Insert title4', title: 'Insert title 4' },\n icon: <div style={{ fontSize: 14, textAlign: 'left' }}>Title 4</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n let modifyText = `#### ${state.selectedText}\\n`;\n if (!state.selectedText) {\n modifyText = `#### `;\n }\n api.replaceSelection(modifyText);\n },\n};\n"
28
+ "import * as React from 'react';\nimport { insertAtLineStart } from '../utils/InsertTextAtPosition';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title4: ICommand = {\n name: 'title4',\n keyCommand: 'title4',\n shortcuts: 'ctrlcmd+4',\n buttonProps: { 'aria-label': 'Insert title4', title: 'Insert title 4' },\n icon: <div style={{ fontSize: 14, textAlign: 'left' }}>Title 4</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n if (state.selection.start === 0 || /\\n$/.test(state.text)) {\n api.replaceSelection('#### ');\n } else {\n insertAtLineStart('#### ', state.selection.start, api.textArea);\n }\n },\n};\n"
26
29
  ]
27
30
  }
@@ -9,6 +9,8 @@ exports.title5 = void 0;
9
9
 
10
10
  var React = _interopRequireWildcard(require("react"));
11
11
 
12
+ var _InsertTextAtPosition = require("../utils/InsertTextAtPosition");
13
+
12
14
  var _jsxRuntime = require("react/jsx-runtime");
13
15
 
14
16
  var title5 = {
@@ -27,13 +29,11 @@ var title5 = {
27
29
  children: "Title 5"
28
30
  }),
29
31
  execute: function execute(state, api) {
30
- var modifyText = "##### ".concat(state.selectedText, "\n");
31
-
32
- if (!state.selectedText) {
33
- modifyText = "##### ";
32
+ if (state.selection.start === 0 || /\n$/.test(state.text)) {
33
+ api.replaceSelection('##### ');
34
+ } else {
35
+ (0, _InsertTextAtPosition.insertAtLineStart)('##### ', state.selection.start, api.textArea);
34
36
  }
35
-
36
- api.replaceSelection(modifyText);
37
37
  }
38
38
  };
39
39
  exports.title5 = title5;
@@ -16,12 +16,15 @@
16
16
  "execute",
17
17
  "state",
18
18
  "api",
19
- "modifyText",
20
- "selectedText",
21
- "replaceSelection"
19
+ "selection",
20
+ "start",
21
+ "test",
22
+ "text",
23
+ "replaceSelection",
24
+ "textArea"
22
25
  ],
23
- "mappings": ";;;;;;;;;AAAA;;;;AAGO,IAAMA,MAAgB,GAAG;AAC9BC,EAAAA,IAAI,EAAE,QADwB;AAE9BC,EAAAA,UAAU,EAAE,QAFkB;AAG9BC,EAAAA,SAAS,EAAE,W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F,GAAG,CAACM,QAAvD;AACD;AACF;AAZ6B,CAAzB",
24
27
  "sourcesContent": [
25
- "import * as React from 'react';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title5: ICommand = {\n name: 'title5',\n keyCommand: 'title5',\n shortcuts: 'ctrlcmd+5',\n buttonProps: { 'aria-label': 'Insert title5', title: 'Insert title 5' },\n icon: <div style={{ fontSize: 12, textAlign: 'left' }}>Title 5</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n let modifyText = `##### ${state.selectedText}\\n`;\n if (!state.selectedText) {\n modifyText = `##### `;\n }\n api.replaceSelection(modifyText);\n },\n};\n"
28
+ "import * as React from 'react';\nimport { insertAtLineStart } from '../utils/InsertTextAtPosition';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title5: ICommand = {\n name: 'title5',\n keyCommand: 'title5',\n shortcuts: 'ctrlcmd+5',\n buttonProps: { 'aria-label': 'Insert title5', title: 'Insert title 5' },\n icon: <div style={{ fontSize: 12, textAlign: 'left' }}>Title 5</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n if (state.selection.start === 0 || /\\n$/.test(state.text)) {\n api.replaceSelection('##### ');\n } else {\n insertAtLineStart('##### ', state.selection.start, api.textArea);\n }\n },\n};\n"
26
29
  ]
27
30
  }
@@ -9,6 +9,8 @@ exports.title6 = void 0;
9
9
 
10
10
  var React = _interopRequireWildcard(require("react"));
11
11
 
12
+ var _InsertTextAtPosition = require("../utils/InsertTextAtPosition");
13
+
12
14
  var _jsxRuntime = require("react/jsx-runtime");
13
15
 
14
16
  var title6 = {
@@ -27,13 +29,11 @@ var title6 = {
27
29
  children: "Title 6"
28
30
  }),
29
31
  execute: function execute(state, api) {
30
- var modifyText = "###### ".concat(state.selectedText, "\n");
31
-
32
- if (!state.selectedText) {
33
- modifyText = "###### ";
32
+ if (state.selection.start === 0 || /\n$/.test(state.text)) {
33
+ api.replaceSelection('###### ');
34
+ } else {
35
+ (0, _InsertTextAtPosition.insertAtLineStart)('###### ', state.selection.start, api.textArea);
34
36
  }
35
-
36
- api.replaceSelection(modifyText);
37
37
  }
38
38
  };
39
39
  exports.title6 = title6;
@@ -16,12 +16,15 @@
16
16
  "execute",
17
17
  "state",
18
18
  "api",
19
- "modifyText",
20
- "selectedText",
21
- "replaceSelection"
19
+ "selection",
20
+ "start",
21
+ "test",
22
+ "text",
23
+ "replaceSelection",
24
+ "textArea"
22
25
  ],
23
- "mappings": ";;;;;;;;;AAAA;;;;AAGO,IAAMA,MAAgB,GAAG;AAC9BC,EAAAA,IAAI,EAAE,QADwB;AAE9BC,EAAAA,UAAU,EAAE,QAFkB;AAG9BC,EAAAA,SAAS,EAAE,WAHmB;AAI9BC,EAAAA,WAAW,EAAE;AAAE,kBAAc,eAAhB;AAAiCC,IAAAA,KAAK,EAAE;AAAxC,GAJiB;AAK9BC,EAAAA,IAAI,eAAE;AAAK,IAAA,KAAK,EAAE;AAAEC,MAAAA,QAAQ,EAAE,EAAZ;AAAgBC,MAAAA,SAAS,EAAE;AAA3B,KAAZ;AAAA;AAAA,IALwB;AAM9BC,EAAAA,OAAO,EAAE,iBAACC,KAAD,EAAmBC,GAAnB,EAA4C;AACnD,QAAIC,UAAU,oBAAaF,KAAK,CAACG,YAAnB,OAAd;;AACA,QAAI,CAACH,KAAK,CAACG,YAAX,EAAyB;AACvBD,MAAAA,UAAU,YAAV;AACD;;AACDD,IAAAA,GAAG,CAACG,gBAAJ,CAAqBF,UAArB;AACD;AAZ6B,CAAzB",
26
+ "mappings": ";;;;;;;;;AAAA;;AACA;;;;AAGO,IAAMA,MAAgB,GAAG;AAC9BC,EAAAA,IAAI,EAAE,QADwB;AAE9BC,EAAAA,UAAU,EAAE,QAFkB;AAG9BC,EAAAA,SAAS,EAAE,WAHmB;AAI9BC,EAAAA,WAAW,EAAE;AAAE,kBAAc,eAAhB;AAAiCC,IAAAA,KAAK,EAAE;AAAxC,GAJiB;AAK9BC,EAAAA,IAAI,eAAE;AAAK,IAAA,KAAK,EAAE;AAAEC,MAAAA,QAAQ,EAAE,EAAZ;AAAgBC,MAAAA,SAAS,EAAE;AAA3B,KAAZ;AAAA;AAAA,IALwB;AAM9BC,EAAAA,OAAO,EAAE,iBAACC,KAAD,EAAmBC,GAAnB,EAA4C;AACnD,QAAID,KAAK,CAACE,SAAN,CAAgBC,KAAhB,KAA0B,CAA1B,IAA+B,MAAMC,IAAN,CAAWJ,KAAK,CAACK,IAAjB,CAAnC,EAA2D;AACzDJ,MAAAA,GAAG,CAACK,gBAAJ,CAAqB,SAArB;AACD,KAFD,MAEO;AACL,mDAAkB,SAAlB,EAA6BN,KAAK,CAACE,SAAN,CAAgBC,KAA7C,EAAoDF,GAAG,CAACM,QAAxD;AACD;AACF;AAZ6B,CAAzB",
24
27
  "sourcesContent": [
25
- "import * as React from 'react';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title6: ICommand = {\n name: 'title6',\n keyCommand: 'title6',\n shortcuts: 'ctrlcmd+6',\n buttonProps: { 'aria-label': 'Insert title6', title: 'Insert title 6' },\n icon: <div style={{ fontSize: 12, textAlign: 'left' }}>Title 6</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n let modifyText = `###### ${state.selectedText}\\n`;\n if (!state.selectedText) {\n modifyText = `###### `;\n }\n api.replaceSelection(modifyText);\n },\n};\n"
28
+ "import * as React from 'react';\nimport { insertAtLineStart } from '../utils/InsertTextAtPosition';\nimport { ICommand, TextState, TextAreaTextApi } from './';\n\nexport const title6: ICommand = {\n name: 'title6',\n keyCommand: 'title6',\n shortcuts: 'ctrlcmd+6',\n buttonProps: { 'aria-label': 'Insert title6', title: 'Insert title 6' },\n icon: <div style={{ fontSize: 12, textAlign: 'left' }}>Title 6</div>,\n execute: (state: TextState, api: TextAreaTextApi) => {\n if (state.selection.start === 0 || /\\n$/.test(state.text)) {\n api.replaceSelection('###### ');\n } else {\n insertAtLineStart('###### ', state.selection.start, api.textArea);\n }\n },\n};\n"
26
29
  ]
27
30
  }
@@ -3,6 +3,13 @@
3
3
  * Copyright (c) 2018 Dmitriy Kubyshkin
4
4
  * Copied from https://github.com/grassator/insert-text-at-cursor
5
5
  */
6
+ /**
7
+ * @param {string} val
8
+ * @param {number} cursorIdx
9
+ * @param {HTMLTextAreaElement|HTMLInputElement} input
10
+ * @return {void}
11
+ */
12
+ export declare const insertAtLineStart: (val: string, cursorIdx: number, input: HTMLTextAreaElement | HTMLInputElement) => void;
6
13
  /**
7
14
  * @param {HTMLTextAreaElement|HTMLInputElement} input
8
15
  * @param {string} text
@@ -3,6 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.insertAtLineStart = void 0;
6
7
  exports.insertTextAtPosition = insertTextAtPosition;
7
8
 
8
9
  /**
@@ -29,6 +30,33 @@ function canManipulateViaTextNodes(input) {
29
30
 
30
31
  return browserSupportsTextareaTextNodes;
31
32
  }
33
+ /**
34
+ * @param {string} val
35
+ * @param {number} cursorIdx
36
+ * @param {HTMLTextAreaElement|HTMLInputElement} input
37
+ * @return {void}
38
+ */
39
+
40
+
41
+ var insertAtLineStart = function insertAtLineStart(val, cursorIdx, input) {
42
+ var content = input.value;
43
+ var startIdx = 0;
44
+
45
+ while (cursorIdx--) {
46
+ var char = content[cursorIdx];
47
+
48
+ if (char === '\n') {
49
+ startIdx = cursorIdx + 1;
50
+ break;
51
+ }
52
+ }
53
+
54
+ input.focus();
55
+ input.setRangeText(val, startIdx, startIdx);
56
+ input.dispatchEvent(new Event('input', {
57
+ bubbles: true
58
+ }));
59
+ };
32
60
  /**
33
61
  * @param {HTMLTextAreaElement|HTMLInputElement} input
34
62
  * @param {string} text
@@ -36,6 +64,8 @@ function canManipulateViaTextNodes(input) {
36
64
  */
37
65
 
38
66
 
67
+ exports.insertAtLineStart = insertAtLineStart;
68
+
39
69
  function insertTextAtPosition(input, text) {
40
70
  // Most of the used APIs only work with the field selected
41
71
  input.focus(); // IE 8-10
@@ -13,9 +13,19 @@
13
13
  "createElement",
14
14
  "value",
15
15
  "firstChild",
16
+ "insertAtLineStart",
17
+ "val",
18
+ "cursorIdx",
19
+ "content",
20
+ "startIdx",
21
+ "char",
22
+ "focus",
23
+ "setRangeText",
24
+ "dispatchEvent",
25
+ "Event",
26
+ "bubbles",
16
27
  "insertTextAtPosition",
17
28
  "text",
18
- "focus",
19
29
  "selection",
20
30
  "ieRange",
21
31
  "createRange",
@@ -27,7 +37,6 @@
27
37
  "selectionStart",
28
38
  "end",
29
39
  "selectionEnd",
30
- "setRangeText",
31
40
  "range",
32
41
  "textNode",
33
42
  "createTextNode",
@@ -49,11 +58,10 @@
49
58
  "setSelectionRange",
50
59
  "e",
51
60
  "createEvent",
52
- "initEvent",
53
- "dispatchEvent"
61
+ "initEvent"
54
62
  ],
55
- "mappings": ";;;;;;;AAAA;AACA;AACA;AACA;AACA;AAEA,IAAIA,gCAAJ;AAEA;AACA;AACA;AACA;;AACA,SAASC,yBAAT,CAAmCC,KAAnC,EAAkF;AAChF,MAAIA,KAAK,CAACC,QAAN,KAAmB,UAAvB,EAAmC;AACjC,WAAO,KAAP;AACD;;AACD,MAAI,OAAOH,gCAAP,KAA4C,WAAhD,EAA6D;AAC3D,QAAMI,QAA6B,GAAGC,QAAQ,CAACC,aAAT,CAAuB,UAAvB,CAAtC;AACAF,IAAAA,QAAQ,CAACG,KAAT,GAAiB,GAAjB;AACAP,IAAAA,gCAAgC,GAAG,CAAC,CAACI,QAAQ,CAACI,UAA9C;AACD;;AACD,SAAOR,gCAAP;AACD;AAED;AACA;AACA;AACA;AACA;;;AACO,SAASS,oBAAT,CAA8BP,KAA9B,EAA6EQ,IAA7E,EAA2F;AAChG;AACAR,EAAAA,KAAK,CAACS,KAAN,GAFgG,CAIhG;;AACA,MAAKN,QAAD,CAAkBO,SAAtB,EAAiC;AAC/B,QAAMC,OAAO,GAAIR,QAAD,CAAkBO,SAAlB,CAA4BE,WAA5B,EAAhB;AACAD,IAAAA,OAAO,CAACH,IAAR,GAAeA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,WAAT,CAAqB,SAArB,CAAV;AACAD,IAAAA,CAAC,CAACE,SAAF,CAAY,OAAZ,EAAqB,IAArB,EAA2B,KAA3B;AACApD,IAAAA,KAAK,CAACe,aAAN,CAAoBmC,CAApB;AACD;AACF",
56
64
  "sourcesContent": [
57
- "/**\n * The MIT License\n * Copyright (c) 2018 Dmitriy Kubyshkin\n * Copied from https://github.com/grassator/insert-text-at-cursor\n */\n\nlet browserSupportsTextareaTextNodes: any;\n\n/**\n * @param {HTMLElement} input\n * @return {boolean}\n */\nfunction canManipulateViaTextNodes(input: HTMLTextAreaElement | HTMLInputElement) {\n if (input.nodeName !== 'TEXTAREA') {\n return false;\n }\n if (typeof browserSupportsTextareaTextNodes === 'undefined') {\n const textarea: HTMLTextAreaElement = document.createElement('textarea');\n textarea.value = '1';\n browserSupportsTextareaTextNodes = !!textarea.firstChild;\n }\n return browserSupportsTextareaTextNodes;\n}\n\n/**\n * @param {HTMLTextAreaElement|HTMLInputElement} input\n * @param {string} text\n * @returns {void}\n */\nexport function insertTextAtPosition(input: HTMLTextAreaElement | HTMLInputElement, text: string) {\n // Most of the used APIs only work with the field selected\n input.focus();\n\n // IE 8-10\n if ((document as any).selection) {\n const ieRange = (document as any).selection.createRange();\n ieRange.text = text;\n\n // Move cursor after the inserted text\n ieRange.collapse(false /* to the end */);\n ieRange.select();\n\n return;\n }\n\n // Webkit + Edge\n const isSuccess = document.execCommand && document.execCommand('insertText', false, text);\n if (!isSuccess) {\n const start = input.selectionStart!;\n const end = input.selectionEnd!;\n // Firefox (non-standard method)\n if (typeof input.setRangeText === 'function') {\n input.setRangeText(text);\n } else {\n // To make a change we just need a Range, not a Selection\n const range = document.createRange();\n const textNode = document.createTextNode(text);\n\n if (canManipulateViaTextNodes(input)) {\n let node = input.firstChild;\n\n // If textarea is empty, just insert the text\n if (!node) {\n input.appendChild(textNode);\n } else {\n // Otherwise we need to find a nodes for start and end\n let offset = 0;\n let startNode = null;\n let endNode = null;\n\n while (node && (startNode === null || endNode === null)) {\n const nodeLength = node.nodeValue!.length;\n\n // if start of the selection falls into current node\n if (start >= offset && start <= offset + nodeLength) {\n range.setStart((startNode = node), start - offset);\n }\n\n // if end of the selection falls into current node\n if (end >= offset && end <= offset + nodeLength) {\n range.setEnd((endNode = node), end - offset);\n }\n\n offset += nodeLength;\n node = node.nextSibling;\n }\n\n // If there is some text selected, remove it as we should replace it\n if (start !== end) {\n range.deleteContents();\n }\n }\n }\n\n // If the node is a textarea and the range doesn't span outside the element\n //\n // Get the commonAncestorContainer of the selected range and test its type\n // If the node is of type `#text` it means that we're still working with text nodes within our textarea element\n // otherwise, if it's of type `#document` for example it means our selection spans outside the textarea.\n if (canManipulateViaTextNodes(input) && range.commonAncestorContainer.nodeName === '#text') {\n // Finally insert a new node. The browser will automatically split start and end nodes into two if necessary\n range.insertNode(textNode);\n } else {\n // If the node is not a textarea or the range spans outside a textarea the only way is to replace the whole value\n const value = input.value;\n input.value = value.slice(0, start) + text + value.slice(end);\n }\n }\n\n // Correct the cursor position to be at the end of the insertion\n input.setSelectionRange(start + text.length, start + text.length);\n\n // Notify any possible listeners of the change\n const e = document.createEvent('UIEvent');\n e.initEvent('input', true, false);\n input.dispatchEvent(e);\n }\n}\n"
65
+ "/**\n * The MIT License\n * Copyright (c) 2018 Dmitriy Kubyshkin\n * Copied from https://github.com/grassator/insert-text-at-cursor\n */\n\nlet browserSupportsTextareaTextNodes: any;\n\n/**\n * @param {HTMLElement} input\n * @return {boolean}\n */\nfunction canManipulateViaTextNodes(input: HTMLTextAreaElement | HTMLInputElement): boolean {\n if (input.nodeName !== 'TEXTAREA') {\n return false;\n }\n if (typeof browserSupportsTextareaTextNodes === 'undefined') {\n const textarea: HTMLTextAreaElement = document.createElement('textarea');\n textarea.value = '1';\n browserSupportsTextareaTextNodes = !!textarea.firstChild;\n }\n return browserSupportsTextareaTextNodes;\n}\n\n/**\n * @param {string} val\n * @param {number} cursorIdx\n * @param {HTMLTextAreaElement|HTMLInputElement} input\n * @return {void}\n */\nexport const insertAtLineStart = (\n val: string,\n cursorIdx: number,\n input: HTMLTextAreaElement | HTMLInputElement,\n): void => {\n const content = input.value;\n let startIdx = 0;\n\n while (cursorIdx--) {\n let char = content[cursorIdx];\n if (char === '\\n') {\n startIdx = cursorIdx + 1;\n break;\n }\n }\n\n input.focus();\n input.setRangeText(val, startIdx, startIdx);\n input.dispatchEvent(new Event('input', { bubbles: true }));\n};\n\n/**\n * @param {HTMLTextAreaElement|HTMLInputElement} input\n * @param {string} text\n * @returns {void}\n */\nexport function insertTextAtPosition(input: HTMLTextAreaElement | HTMLInputElement, text: string): void {\n // Most of the used APIs only work with the field selected\n input.focus();\n\n // IE 8-10\n if ((document as any).selection) {\n const ieRange = (document as any).selection.createRange();\n ieRange.text = text;\n\n // Move cursor after the inserted text\n ieRange.collapse(false /* to the end */);\n ieRange.select();\n\n return;\n }\n\n // Webkit + Edge\n const isSuccess = document.execCommand && document.execCommand('insertText', false, text);\n if (!isSuccess) {\n const start = input.selectionStart!;\n const end = input.selectionEnd!;\n // Firefox (non-standard method)\n if (typeof input.setRangeText === 'function') {\n input.setRangeText(text);\n } else {\n // To make a change we just need a Range, not a Selection\n const range = document.createRange();\n const textNode = document.createTextNode(text);\n\n if (canManipulateViaTextNodes(input)) {\n let node = input.firstChild;\n\n // If textarea is empty, just insert the text\n if (!node) {\n input.appendChild(textNode);\n } else {\n // Otherwise we need to find a nodes for start and end\n let offset = 0;\n let startNode = null;\n let endNode = null;\n\n while (node && (startNode === null || endNode === null)) {\n const nodeLength = node.nodeValue!.length;\n\n // if start of the selection falls into current node\n if (start >= offset && start <= offset + nodeLength) {\n range.setStart((startNode = node), start - offset);\n }\n\n // if end of the selection falls into current node\n if (end >= offset && end <= offset + nodeLength) {\n range.setEnd((endNode = node), end - offset);\n }\n\n offset += nodeLength;\n node = node.nextSibling;\n }\n\n // If there is some text selected, remove it as we should replace it\n if (start !== end) {\n range.deleteContents();\n }\n }\n }\n\n // If the node is a textarea and the range doesn't span outside the element\n //\n // Get the commonAncestorContainer of the selected range and test its type\n // If the node is of type `#text` it means that we're still working with text nodes within our textarea element\n // otherwise, if it's of type `#document` for example it means our selection spans outside the textarea.\n if (canManipulateViaTextNodes(input) && range.commonAncestorContainer.nodeName === '#text') {\n // Finally insert a new node. The browser will automatically split start and end nodes into two if necessary\n range.insertNode(textNode);\n } else {\n // If the node is not a textarea or the range spans outside a textarea the only way is to replace the whole value\n const value = input.value;\n input.value = value.slice(0, start) + text + value.slice(end);\n }\n }\n\n // Correct the cursor position to be at the end of the insertion\n input.setSelectionRange(start + text.length, start + text.length);\n\n // Notify any possible listeners of the change\n const e = document.createEvent('UIEvent');\n e.initEvent('input', true, false);\n input.dispatchEvent(e);\n }\n}\n"
58
66
  ]
59
67
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uiw/react-md-editor",
3
- "version": "3.8.3",
3
+ "version": "3.8.4",
4
4
  "description": "A markdown editor with preview, implemented with React.js and TypeScript.",
5
5
  "homepage": "https://uiwjs.github.io/react-md-editor/",
6
6
  "author": "kenny wang <wowohoo@qq.com>",
@@ -1,4 +1,5 @@
1
1
  import React from 'react';
2
+ import { insertAtLineStart } from '../utils/InsertTextAtPosition';
2
3
  import { ICommand, TextState, TextAreaTextApi } from './';
3
4
 
4
5
  export const title1: ICommand = {
@@ -8,10 +9,10 @@ export const title1: ICommand = {
8
9
  buttonProps: { 'aria-label': 'Insert title 1', title: 'Insert title 1' },
9
10
  icon: <div style={{ fontSize: 18, textAlign: 'left' }}>Title 1</div>,
10
11
  execute: (state: TextState, api: TextAreaTextApi) => {
11
- let modifyText = `# ${state.selectedText}\n`;
12
- if (!state.selectedText) {
13
- modifyText = `# `;
12
+ if (state.selection.start === 0 || /\n$/.test(state.text)) {
13
+ api.replaceSelection('# ');
14
+ } else {
15
+ insertAtLineStart('# ', state.selection.start, api.textArea);
14
16
  }
15
- api.replaceSelection(modifyText);
16
17
  },
17
18
  };
@@ -1,4 +1,5 @@
1
1
  import * as React from 'react';
2
+ import { insertAtLineStart } from '../utils/InsertTextAtPosition';
2
3
  import { ICommand, TextState, TextAreaTextApi } from './';
3
4
 
4
5
  export const title2: ICommand = {
@@ -8,10 +9,10 @@ export const title2: ICommand = {
8
9
  buttonProps: { 'aria-label': 'Insert title2', title: 'Insert title 2' },
9
10
  icon: <div style={{ fontSize: 16, textAlign: 'left' }}>Title 2</div>,
10
11
  execute: (state: TextState, api: TextAreaTextApi) => {
11
- let modifyText = `## ${state.selectedText}\n`;
12
- if (!state.selectedText) {
13
- modifyText = `## `;
12
+ if (state.selection.start === 0 || /\n$/.test(state.text)) {
13
+ api.replaceSelection('## ');
14
+ } else {
15
+ insertAtLineStart('## ', state.selection.start, api.textArea);
14
16
  }
15
- api.replaceSelection(modifyText);
16
17
  },
17
18
  };
@@ -1,4 +1,5 @@
1
1
  import * as React from 'react';
2
+ import { insertAtLineStart } from '../utils/InsertTextAtPosition';
2
3
  import { ICommand, TextState, TextAreaTextApi } from './';
3
4
 
4
5
  export const title3: ICommand = {
@@ -8,10 +9,10 @@ export const title3: ICommand = {
8
9
  buttonProps: { 'aria-label': 'Insert title3', title: 'Insert title 3' },
9
10
  icon: <div style={{ fontSize: 15, textAlign: 'left' }}>Title 3</div>,
10
11
  execute: (state: TextState, api: TextAreaTextApi) => {
11
- let modifyText = `### ${state.selectedText}\n`;
12
- if (!state.selectedText) {
13
- modifyText = `### `;
12
+ if (state.selection.start === 0 || /\n$/.test(state.text)) {
13
+ api.replaceSelection('### ');
14
+ } else {
15
+ insertAtLineStart('### ', state.selection.start, api.textArea);
14
16
  }
15
- api.replaceSelection(modifyText);
16
17
  },
17
18
  };
@@ -1,4 +1,5 @@
1
1
  import * as React from 'react';
2
+ import { insertAtLineStart } from '../utils/InsertTextAtPosition';
2
3
  import { ICommand, TextState, TextAreaTextApi } from './';
3
4
 
4
5
  export const title4: ICommand = {
@@ -8,10 +9,10 @@ export const title4: ICommand = {
8
9
  buttonProps: { 'aria-label': 'Insert title4', title: 'Insert title 4' },
9
10
  icon: <div style={{ fontSize: 14, textAlign: 'left' }}>Title 4</div>,
10
11
  execute: (state: TextState, api: TextAreaTextApi) => {
11
- let modifyText = `#### ${state.selectedText}\n`;
12
- if (!state.selectedText) {
13
- modifyText = `#### `;
12
+ if (state.selection.start === 0 || /\n$/.test(state.text)) {
13
+ api.replaceSelection('#### ');
14
+ } else {
15
+ insertAtLineStart('#### ', state.selection.start, api.textArea);
14
16
  }
15
- api.replaceSelection(modifyText);
16
17
  },
17
18
  };
@@ -1,4 +1,5 @@
1
1
  import * as React from 'react';
2
+ import { insertAtLineStart } from '../utils/InsertTextAtPosition';
2
3
  import { ICommand, TextState, TextAreaTextApi } from './';
3
4
 
4
5
  export const title5: ICommand = {
@@ -8,10 +9,10 @@ export const title5: ICommand = {
8
9
  buttonProps: { 'aria-label': 'Insert title5', title: 'Insert title 5' },
9
10
  icon: <div style={{ fontSize: 12, textAlign: 'left' }}>Title 5</div>,
10
11
  execute: (state: TextState, api: TextAreaTextApi) => {
11
- let modifyText = `##### ${state.selectedText}\n`;
12
- if (!state.selectedText) {
13
- modifyText = `##### `;
12
+ if (state.selection.start === 0 || /\n$/.test(state.text)) {
13
+ api.replaceSelection('##### ');
14
+ } else {
15
+ insertAtLineStart('##### ', state.selection.start, api.textArea);
14
16
  }
15
- api.replaceSelection(modifyText);
16
17
  },
17
18
  };
@@ -1,4 +1,5 @@
1
1
  import * as React from 'react';
2
+ import { insertAtLineStart } from '../utils/InsertTextAtPosition';
2
3
  import { ICommand, TextState, TextAreaTextApi } from './';
3
4
 
4
5
  export const title6: ICommand = {
@@ -8,10 +9,10 @@ export const title6: ICommand = {
8
9
  buttonProps: { 'aria-label': 'Insert title6', title: 'Insert title 6' },
9
10
  icon: <div style={{ fontSize: 12, textAlign: 'left' }}>Title 6</div>,
10
11
  execute: (state: TextState, api: TextAreaTextApi) => {
11
- let modifyText = `###### ${state.selectedText}\n`;
12
- if (!state.selectedText) {
13
- modifyText = `###### `;
12
+ if (state.selection.start === 0 || /\n$/.test(state.text)) {
13
+ api.replaceSelection('###### ');
14
+ } else {
15
+ insertAtLineStart('###### ', state.selection.start, api.textArea);
14
16
  }
15
- api.replaceSelection(modifyText);
16
17
  },
17
18
  };
@@ -10,7 +10,7 @@ let browserSupportsTextareaTextNodes: any;
10
10
  * @param {HTMLElement} input
11
11
  * @return {boolean}
12
12
  */
13
- function canManipulateViaTextNodes(input: HTMLTextAreaElement | HTMLInputElement) {
13
+ function canManipulateViaTextNodes(input: HTMLTextAreaElement | HTMLInputElement): boolean {
14
14
  if (input.nodeName !== 'TEXTAREA') {
15
15
  return false;
16
16
  }
@@ -22,12 +22,39 @@ function canManipulateViaTextNodes(input: HTMLTextAreaElement | HTMLInputElement
22
22
  return browserSupportsTextareaTextNodes;
23
23
  }
24
24
 
25
+ /**
26
+ * @param {string} val
27
+ * @param {number} cursorIdx
28
+ * @param {HTMLTextAreaElement|HTMLInputElement} input
29
+ * @return {void}
30
+ */
31
+ export const insertAtLineStart = (
32
+ val: string,
33
+ cursorIdx: number,
34
+ input: HTMLTextAreaElement | HTMLInputElement,
35
+ ): void => {
36
+ const content = input.value;
37
+ let startIdx = 0;
38
+
39
+ while (cursorIdx--) {
40
+ let char = content[cursorIdx];
41
+ if (char === '\n') {
42
+ startIdx = cursorIdx + 1;
43
+ break;
44
+ }
45
+ }
46
+
47
+ input.focus();
48
+ input.setRangeText(val, startIdx, startIdx);
49
+ input.dispatchEvent(new Event('input', { bubbles: true }));
50
+ };
51
+
25
52
  /**
26
53
  * @param {HTMLTextAreaElement|HTMLInputElement} input
27
54
  * @param {string} text
28
55
  * @returns {void}
29
56
  */
30
- export function insertTextAtPosition(input: HTMLTextAreaElement | HTMLInputElement, text: string) {
57
+ export function insertTextAtPosition(input: HTMLTextAreaElement | HTMLInputElement, text: string): void {
31
58
  // Most of the used APIs only work with the field selected
32
59
  input.focus();
33
60