react-headless-mde 2.0.3 → 3.0.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 (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +56 -19
  3. package/dist/cjs/index.js +338 -262
  4. package/dist/cjs/index.js.map +1 -1
  5. package/dist/esm/commands/base-command.d.ts +24 -0
  6. package/dist/esm/commands/line/heading-level1.d.ts +4 -0
  7. package/dist/esm/commands/line/heading-level2.d.ts +4 -0
  8. package/dist/esm/commands/line/heading-level3.d.ts +4 -0
  9. package/dist/esm/commands/line/heading-level4.d.ts +4 -0
  10. package/dist/esm/commands/line/heading-level5.d.ts +4 -0
  11. package/dist/esm/commands/line/heading-level6.d.ts +4 -0
  12. package/dist/esm/commands/line/line-prefix-command.d.ts +7 -0
  13. package/dist/esm/commands/line/quote.d.ts +4 -0
  14. package/dist/esm/commands/list/checked-list.d.ts +4 -0
  15. package/dist/esm/commands/list/list-command.d.ts +5 -0
  16. package/dist/esm/commands/list/ordered-list.d.ts +4 -0
  17. package/dist/esm/commands/list/unordered-list.d.ts +4 -0
  18. package/dist/esm/commands/word/bold.d.ts +5 -0
  19. package/dist/esm/commands/word/code-block.d.ts +4 -0
  20. package/dist/esm/commands/word/code.d.ts +5 -0
  21. package/dist/esm/commands/word/italic.d.ts +5 -0
  22. package/dist/esm/commands/word/strikethrough.d.ts +5 -0
  23. package/dist/esm/commands/word/wrap-command.d.ts +11 -0
  24. package/dist/esm/commands/word-complex/image.d.ts +4 -0
  25. package/dist/esm/commands/word-complex/link.d.ts +4 -0
  26. package/dist/esm/controllers/textarea-controller.d.ts +15 -0
  27. package/dist/esm/hooks/use-markdown-editor.d.ts +12 -0
  28. package/dist/esm/index.d.ts +27 -0
  29. package/dist/esm/index.js +319 -243
  30. package/dist/esm/index.js.map +1 -1
  31. package/dist/esm/types/command.d.ts +10 -0
  32. package/dist/esm/types/text-controller.d.ts +18 -0
  33. package/dist/esm/utils/insert-text-to-selection.d.ts +8 -0
  34. package/dist/esm/utils/selection-and-text.d.ts +14 -0
  35. package/dist/index.d.ts +121 -66
  36. package/package.json +58 -46
  37. package/.eslintrc.js +0 -24
  38. package/.github/workflows/npm-publish.yml +0 -27
  39. package/.husky/pre-commit +0 -4
  40. package/.husky/pre-push +0 -4
  41. package/jest.config.js +0 -4
  42. package/lint-staged.config.js +0 -4
  43. package/prettier.config.js +0 -13
  44. package/rollup.config.js +0 -31
  45. package/vite.config.js +0 -7
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Vitaliy Komarov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  A simple yet powerful and extensible **React Markdown Editor** that aims to have feature parity with the Github Markdown editor.
4
4
  React-mde-headless has **no 3rd party dependencies**.
5
5
 
6
- ## [Demo](https://codesandbox.io/s/competent-jepsen-qyz51q?file=/src/index.tsx)
6
+ ## [Demo](https://codesandbox.io/p/sandbox/competent-jepsen-qyz51q)
7
7
 
8
8
  ## Installing
9
9
 
@@ -12,14 +12,14 @@ React-mde-headless has **no 3rd party dependencies**.
12
12
  ## Using
13
13
 
14
14
  ```jsx
15
- import { boldCommand, italicCommand, linkCommand, useTextAreaMarkdownEditor } from 'react-headless-mde';
15
+ import { BoldCommand, ItalicCommand, LinkCommand, useTextAreaMarkdownEditor } from 'react-headless-mde';
16
16
 
17
17
  export const MarkdownEditor = () => {
18
- const { ref, commandController } = useTextAreaMarkdownEditor({
18
+ const { ref, commands } = useTextAreaMarkdownEditor({
19
19
  commandMap: {
20
- bold: boldCommand,
21
- italic: italicCommand,
22
- link: linkCommand,
20
+ bold: BoldCommand,
21
+ italic: ItalicCommand,
22
+ link: LinkCommand,
23
23
  },
24
24
  });
25
25
 
@@ -27,7 +27,7 @@ export const MarkdownEditor = () => {
27
27
  <div>
28
28
  <button
29
29
  onClick={() => {
30
- commandController.executeCommand('bold');
30
+ commands.bold();
31
31
  }}
32
32
  >
33
33
  B
@@ -39,6 +39,47 @@ export const MarkdownEditor = () => {
39
39
  };
40
40
  ```
41
41
 
42
+ ## Custom commands
43
+
44
+ A command is a class extending `BaseCommand`. The hook instantiates it with the editor's
45
+ `TextController` and returns a typed executor. If a command needs a context
46
+ (e.g. an `attachment` command accepting an upload promise), extend `BaseCommand<Context>`
47
+ — the executor then requires the context argument:
48
+
49
+ ```tsx
50
+ import { BaseCommand, useTextAreaMarkdownEditor } from 'react-headless-mde';
51
+
52
+ class AttachmentCommand extends BaseCommand<Promise<string>> {
53
+ async do(upload: Promise<string>) {
54
+ const url = await upload;
55
+ this.textController.replaceSelection(`![](${url})`);
56
+ }
57
+ }
58
+
59
+ export const MarkdownEditor = () => {
60
+ const { ref, commands } = useTextAreaMarkdownEditor({
61
+ commandMap: {
62
+ attachment: AttachmentCommand,
63
+ bold: BoldCommand,
64
+ },
65
+ });
66
+
67
+ commands.attachment(uploadPromise); // OK
68
+ commands.attachment(); // Type error: 1 argument expected
69
+ commands.bold(42); // Type error: 0 arguments expected
70
+
71
+ // ...
72
+ };
73
+ ```
74
+
75
+ Working examples of commands with a context live in
76
+ [demo/attachment-command.ts](demo/attachment-command.ts) (`Promise<string>` context)
77
+ and [demo/wrap-text-command.ts](demo/wrap-text-command.ts) (a simple `string` context,
78
+ wraps the word at the caret with it on both sides), wired up in `demo/index.tsx`.
79
+
80
+ For undoable commands implement `shouldUndo`/`undo` — the command is then toggled:
81
+ executing it on already formatted text removes the formatting (see `WrapCommand`).
82
+
42
83
  ## Supported commands
43
84
 
44
85
  - headingLevel1
@@ -59,18 +100,18 @@ export const MarkdownEditor = () => {
59
100
  - image
60
101
  - link
61
102
 
62
- ## New API discussion [here](https://github.com/Webbrother/react-headless-mde/issues/22)
103
+ ## Command API
104
+
105
+ Since v3 commands are classes (see [issue #22](https://github.com/Webbrother/react-headless-mde/issues/22)):
106
+ the `commandMap` maps camelCase command names to command classes, and the hook returns
107
+ strictly typed `commands` executors. The pre-v3 object-based API
108
+ (`commandController.executeCommand('bold')`) was removed.
63
109
 
64
110
  ## Todo
65
111
 
66
- - Undo/Redo commands
112
+ - Redo commands
67
113
  - Check execution on SSR (For example, Next.js) and, if necessary, regenerate eslint config, taking into account execution on node.js
68
- - peerDependencies React?
69
114
  - Undo for
70
- - 1st priority
71
- - all headers
72
- - `quote`
73
- - `strikethrough`
74
115
  - 2nd priority
75
116
  - `orderedList`
76
117
  - `unorderedList`
@@ -99,10 +140,6 @@ You might want to take a look at
99
140
  - [rehype-sanitize](https://github.com/rehypejs/rehype-sanitize).
100
141
  - [showdown-xss-filter](https://github.com/VisionistInc/showdown-xss-filter).
101
142
 
102
- ## Licence
103
-
104
- React-mde-headless is [MIT licensed](https://github.com/andrerpena/react-mde/blob/master/LICENSE).
105
-
106
143
  ## About the authors
107
144
 
108
- Created by [André Pena](https://github.com/andrerpena). Maintained and developed by https://github.com/webbrother.
145
+ The idea from [André Pena](https://github.com/andrerpena). Maintained and developed by Vitaliy Komarov https://github.com/webbrother.