@uiw/react-md-editor 4.1.1 → 4.1.2
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/README.md +3 -1
- package/dist/mdeditor.css +1 -1
- package/dist/mdeditor.js +1722 -1686
- package/dist/mdeditor.min.js +1 -1
- package/dist/mdeditor.min.js.LICENSE.txt +1 -1
- package/esm/index.css +0 -1
- package/esm/utils/markdownUtils.js +26 -1
- package/lib/utils/markdownUtils.js +26 -1
- package/markdown-editor.css +0 -1
- package/package.json +1 -1
- package/src/utils/markdownUtils.ts +37 -5
package/esm/index.css
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
+
// `*` is a prefix of `**`, so `**text**` is bold-only and `***text***` is bold+italic.
|
|
2
|
+
function isItalicInsideBoldOnly(text, start, end) {
|
|
3
|
+
var insideBold = start >= 2 && end + 2 <= text.length && text.slice(start - 2, start) === '**' && text.slice(end, end + 2) === '**';
|
|
4
|
+
if (!insideBold) {
|
|
5
|
+
return false;
|
|
6
|
+
}
|
|
7
|
+
var insideBoldItalic = start >= 3 && end + 3 <= text.length && text.slice(start - 3, start) === '***' && text.slice(end, end + 3) === '***';
|
|
8
|
+
return !insideBoldItalic;
|
|
9
|
+
}
|
|
10
|
+
function isAlreadyWrapped(selectedText, prefix, suffix) {
|
|
11
|
+
if (selectedText.length < prefix.length + suffix.length || !selectedText.startsWith(prefix) || !selectedText.endsWith(suffix)) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
if (prefix === '*' && suffix === '*') {
|
|
15
|
+
var isBold = selectedText.startsWith('**') && selectedText.endsWith('**');
|
|
16
|
+
var isBoldItalic = selectedText.startsWith('***') && selectedText.endsWith('***');
|
|
17
|
+
if (isBold && !isBoldItalic) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
1
23
|
export function selectWord(_ref) {
|
|
2
24
|
var text = _ref.text,
|
|
3
25
|
selection = _ref.selection,
|
|
@@ -11,6 +33,9 @@ export function selectWord(_ref) {
|
|
|
11
33
|
if (result.start >= prefix.length && result.end <= text.length - suffix.length) {
|
|
12
34
|
var selectedTextContext = text.slice(result.start - prefix.length, result.end + suffix.length);
|
|
13
35
|
if (selectedTextContext.startsWith(prefix) && selectedTextContext.endsWith(suffix)) {
|
|
36
|
+
if (prefix === '*' && suffix === '*' && isItalicInsideBoldOnly(text, result.start, result.end)) {
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
14
39
|
return {
|
|
15
40
|
start: result.start - prefix.length,
|
|
16
41
|
end: result.end + suffix.length
|
|
@@ -136,7 +161,7 @@ export function executeCommand(_ref3) {
|
|
|
136
161
|
prefix = _ref3.prefix,
|
|
137
162
|
_ref3$suffix = _ref3.suffix,
|
|
138
163
|
suffix = _ref3$suffix === void 0 ? prefix : _ref3$suffix;
|
|
139
|
-
if (selectedText
|
|
164
|
+
if (isAlreadyWrapped(selectedText, prefix, suffix)) {
|
|
140
165
|
api.replaceSelection(selectedText.slice(prefix.length, suffix.length ? -suffix.length : undefined));
|
|
141
166
|
api.setSelectionRange({
|
|
142
167
|
start: selection.start - prefix.length,
|
|
@@ -10,6 +10,28 @@ exports.getSurroundingWord = getSurroundingWord;
|
|
|
10
10
|
exports.insertBeforeEachLine = insertBeforeEachLine;
|
|
11
11
|
exports.selectLine = selectLine;
|
|
12
12
|
exports.selectWord = selectWord;
|
|
13
|
+
// `*` is a prefix of `**`, so `**text**` is bold-only and `***text***` is bold+italic.
|
|
14
|
+
function isItalicInsideBoldOnly(text, start, end) {
|
|
15
|
+
var insideBold = start >= 2 && end + 2 <= text.length && text.slice(start - 2, start) === '**' && text.slice(end, end + 2) === '**';
|
|
16
|
+
if (!insideBold) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
var insideBoldItalic = start >= 3 && end + 3 <= text.length && text.slice(start - 3, start) === '***' && text.slice(end, end + 3) === '***';
|
|
20
|
+
return !insideBoldItalic;
|
|
21
|
+
}
|
|
22
|
+
function isAlreadyWrapped(selectedText, prefix, suffix) {
|
|
23
|
+
if (selectedText.length < prefix.length + suffix.length || !selectedText.startsWith(prefix) || !selectedText.endsWith(suffix)) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
if (prefix === '*' && suffix === '*') {
|
|
27
|
+
var isBold = selectedText.startsWith('**') && selectedText.endsWith('**');
|
|
28
|
+
var isBoldItalic = selectedText.startsWith('***') && selectedText.endsWith('***');
|
|
29
|
+
if (isBold && !isBoldItalic) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
13
35
|
function selectWord(_ref) {
|
|
14
36
|
var text = _ref.text,
|
|
15
37
|
selection = _ref.selection,
|
|
@@ -23,6 +45,9 @@ function selectWord(_ref) {
|
|
|
23
45
|
if (result.start >= prefix.length && result.end <= text.length - suffix.length) {
|
|
24
46
|
var selectedTextContext = text.slice(result.start - prefix.length, result.end + suffix.length);
|
|
25
47
|
if (selectedTextContext.startsWith(prefix) && selectedTextContext.endsWith(suffix)) {
|
|
48
|
+
if (prefix === '*' && suffix === '*' && isItalicInsideBoldOnly(text, result.start, result.end)) {
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
26
51
|
return {
|
|
27
52
|
start: result.start - prefix.length,
|
|
28
53
|
end: result.end + suffix.length
|
|
@@ -148,7 +173,7 @@ function executeCommand(_ref3) {
|
|
|
148
173
|
prefix = _ref3.prefix,
|
|
149
174
|
_ref3$suffix = _ref3.suffix,
|
|
150
175
|
suffix = _ref3$suffix === void 0 ? prefix : _ref3$suffix;
|
|
151
|
-
if (selectedText
|
|
176
|
+
if (isAlreadyWrapped(selectedText, prefix, suffix)) {
|
|
152
177
|
api.replaceSelection(selectedText.slice(prefix.length, suffix.length ? -suffix.length : undefined));
|
|
153
178
|
api.setSelectionRange({
|
|
154
179
|
start: selection.start - prefix.length,
|
package/markdown-editor.css
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uiw/react-md-editor",
|
|
3
|
-
"version": "4.1.
|
|
3
|
+
"version": "4.1.2",
|
|
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
|
"funding": "https://jaywcjlove.github.io/#/sponsor",
|
|
@@ -5,6 +5,39 @@ export interface TextSection {
|
|
|
5
5
|
selection: TextRange;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
+
// `*` is a prefix of `**`, so `**text**` is bold-only and `***text***` is bold+italic.
|
|
9
|
+
function isItalicInsideBoldOnly(text: string, start: number, end: number): boolean {
|
|
10
|
+
const insideBold =
|
|
11
|
+
start >= 2 && end + 2 <= text.length && text.slice(start - 2, start) === '**' && text.slice(end, end + 2) === '**';
|
|
12
|
+
if (!insideBold) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
const insideBoldItalic =
|
|
16
|
+
start >= 3 &&
|
|
17
|
+
end + 3 <= text.length &&
|
|
18
|
+
text.slice(start - 3, start) === '***' &&
|
|
19
|
+
text.slice(end, end + 3) === '***';
|
|
20
|
+
return !insideBoldItalic;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function isAlreadyWrapped(selectedText: string, prefix: string, suffix: string): boolean {
|
|
24
|
+
if (
|
|
25
|
+
selectedText.length < prefix.length + suffix.length ||
|
|
26
|
+
!selectedText.startsWith(prefix) ||
|
|
27
|
+
!selectedText.endsWith(suffix)
|
|
28
|
+
) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
if (prefix === '*' && suffix === '*') {
|
|
32
|
+
const isBold = selectedText.startsWith('**') && selectedText.endsWith('**');
|
|
33
|
+
const isBoldItalic = selectedText.startsWith('***') && selectedText.endsWith('***');
|
|
34
|
+
if (isBold && !isBoldItalic) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
|
|
8
41
|
export function selectWord({
|
|
9
42
|
text,
|
|
10
43
|
selection,
|
|
@@ -23,6 +56,9 @@ export function selectWord({
|
|
|
23
56
|
if (result.start >= prefix.length && result.end <= text.length - suffix.length) {
|
|
24
57
|
const selectedTextContext = text.slice(result.start - prefix.length, result.end + suffix.length);
|
|
25
58
|
if (selectedTextContext.startsWith(prefix) && selectedTextContext.endsWith(suffix)) {
|
|
59
|
+
if (prefix === '*' && suffix === '*' && isItalicInsideBoldOnly(text, result.start, result.end)) {
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
26
62
|
return { start: result.start - prefix.length, end: result.end + suffix.length };
|
|
27
63
|
}
|
|
28
64
|
}
|
|
@@ -139,11 +175,7 @@ export function executeCommand({
|
|
|
139
175
|
prefix: string;
|
|
140
176
|
suffix?: string;
|
|
141
177
|
}) {
|
|
142
|
-
if (
|
|
143
|
-
selectedText.length >= prefix.length + suffix.length &&
|
|
144
|
-
selectedText.startsWith(prefix) &&
|
|
145
|
-
selectedText.endsWith(suffix)
|
|
146
|
-
) {
|
|
178
|
+
if (isAlreadyWrapped(selectedText, prefix, suffix)) {
|
|
147
179
|
api.replaceSelection(selectedText.slice(prefix.length, suffix.length ? -suffix.length : undefined));
|
|
148
180
|
api.setSelectionRange({ start: selection.start - prefix.length, end: selection.end - prefix.length });
|
|
149
181
|
} else {
|