react-headless-mde 2.0.2 → 3.0.0-beta1
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.
- package/LICENSE +21 -0
- package/README.md +64 -23
- package/dist/cjs/index.js +338 -262
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/commands/base-command.d.ts +24 -0
- package/dist/esm/commands/line/heading-level1.d.ts +4 -0
- package/dist/esm/commands/line/heading-level2.d.ts +4 -0
- package/dist/esm/commands/line/heading-level3.d.ts +4 -0
- package/dist/esm/commands/line/heading-level4.d.ts +4 -0
- package/dist/esm/commands/line/heading-level5.d.ts +4 -0
- package/dist/esm/commands/line/heading-level6.d.ts +4 -0
- package/dist/esm/commands/line/line-prefix-command.d.ts +7 -0
- package/dist/esm/commands/line/quote.d.ts +4 -0
- package/dist/esm/commands/list/checked-list.d.ts +4 -0
- package/dist/esm/commands/list/list-command.d.ts +5 -0
- package/dist/esm/commands/list/ordered-list.d.ts +4 -0
- package/dist/esm/commands/list/unordered-list.d.ts +4 -0
- package/dist/esm/commands/word/bold.d.ts +5 -0
- package/dist/esm/commands/word/code-block.d.ts +4 -0
- package/dist/esm/commands/word/code.d.ts +5 -0
- package/dist/esm/commands/word/italic.d.ts +5 -0
- package/dist/esm/commands/word/strikethrough.d.ts +5 -0
- package/dist/esm/commands/word/wrap-command.d.ts +11 -0
- package/dist/esm/commands/word-complex/image.d.ts +4 -0
- package/dist/esm/commands/word-complex/link.d.ts +4 -0
- package/dist/esm/controllers/textarea-controller.d.ts +15 -0
- package/dist/esm/hooks/use-markdown-editor.d.ts +12 -0
- package/dist/esm/index.d.ts +27 -0
- package/dist/esm/index.js +319 -243
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/command.d.ts +10 -0
- package/dist/esm/types/text-controller.d.ts +18 -0
- package/dist/esm/utils/insert-text-to-selection.d.ts +8 -0
- package/dist/esm/utils/selection-and-text.d.ts +14 -0
- package/dist/index.d.ts +121 -66
- package/package.json +58 -46
- package/.eslintrc.js +0 -24
- package/.github/workflows/npm-publish.yml +0 -27
- package/.husky/pre-commit +0 -4
- package/.husky/pre-push +0 -4
- package/jest.config.js +0 -4
- package/lint-staged.config.js +0 -4
- package/prettier.config.js +0 -13
- package/rollup.config.js +0 -31
- 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
|
@@ -12,14 +12,14 @@ React-mde-headless has **no 3rd party dependencies**.
|
|
|
12
12
|
## Using
|
|
13
13
|
|
|
14
14
|
```jsx
|
|
15
|
-
import {
|
|
15
|
+
import { BoldCommand, ItalicCommand, LinkCommand, useTextAreaMarkdownEditor } from 'react-headless-mde';
|
|
16
16
|
|
|
17
17
|
export const MarkdownEditor = () => {
|
|
18
|
-
const { ref,
|
|
18
|
+
const { ref, commands } = useTextAreaMarkdownEditor({
|
|
19
19
|
commandMap: {
|
|
20
|
-
bold:
|
|
21
|
-
italic:
|
|
22
|
-
link:
|
|
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
|
-
|
|
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(``);
|
|
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,22 +100,23 @@ export const MarkdownEditor = () => {
|
|
|
59
100
|
- image
|
|
60
101
|
- link
|
|
61
102
|
|
|
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.
|
|
109
|
+
|
|
62
110
|
## Todo
|
|
63
111
|
|
|
64
|
-
-
|
|
65
|
-
- undo/redo
|
|
112
|
+
- Redo commands
|
|
66
113
|
- Check execution on SSR (For example, Next.js) and, if necessary, regenerate eslint config, taking into account execution on node.js
|
|
67
|
-
-
|
|
68
|
-
- undo for
|
|
69
|
-
- 1st priority
|
|
70
|
-
- all headings
|
|
71
|
-
- quote
|
|
72
|
-
- strikethrough
|
|
114
|
+
- Undo for
|
|
73
115
|
- 2nd priority
|
|
74
|
-
- orderedList
|
|
75
|
-
- unorderedList
|
|
76
|
-
- checkedList
|
|
77
|
-
- codeBlock
|
|
116
|
+
- `orderedList`
|
|
117
|
+
- `unorderedList`
|
|
118
|
+
- `checkedList`
|
|
119
|
+
- `codeBlock`
|
|
78
120
|
|
|
79
121
|
PR's are welcome!
|
|
80
122
|
|
|
@@ -93,12 +135,11 @@ this has been taken from [their documentation](<https://github.com/showdownjs/sh
|
|
|
93
135
|
> you should filter any suspicious content coming from user input. Showdown does not include an
|
|
94
136
|
> XSS filter, so you must provide your own. But be careful in how you do it…
|
|
95
137
|
|
|
96
|
-
You might want to take a look at
|
|
97
|
-
|
|
98
|
-
## Licence
|
|
138
|
+
You might want to take a look at
|
|
99
139
|
|
|
100
|
-
|
|
140
|
+
- [rehype-sanitize](https://github.com/rehypejs/rehype-sanitize).
|
|
141
|
+
- [showdown-xss-filter](https://github.com/VisionistInc/showdown-xss-filter).
|
|
101
142
|
|
|
102
143
|
## About the authors
|
|
103
144
|
|
|
104
|
-
|
|
145
|
+
The idea from [André Pena](https://github.com/andrerpena). Maintained and developed by Vitaliy Komarov https://github.com/webbrother.
|