Skip to content

Commit 17cdc30

Browse files
committed
Impliment orderList support on markdown toolbar
1 parent aeb208d commit 17cdc30

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

components/MarkdownToolbar.tsx

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ type StyleArgs = {
2424
blockSuffix?: string
2525
multiline?: boolean
2626
surroundWithNewLines?: boolean
27+
orderedList?: boolean
2728
}
2829

2930
const applyMarkdown = (
30-
textarea: HTMLTextAreaElement,
31+
textarea: HTMLTextAreaElement | null,
3132
styleArgs: StyleArgs
3233
): void => {
3334
if (!textarea) return
@@ -38,7 +39,8 @@ const applyMarkdown = (
3839
blockPrefix = '',
3940
blockSuffix = '',
4041
multiline = false,
41-
surroundWithNewLines = false
42+
surroundWithNewLines = false,
43+
orderedList = false
4244
} = styleArgs
4345

4446
const { value, selectionStart, selectionEnd } = textarea
@@ -62,7 +64,8 @@ const applyMarkdown = (
6264
right,
6365
selectedPrefix,
6466
selectedSuffix,
65-
multiline
67+
multiline,
68+
orderedList
6669
})
6770

6871
console.log({ action })
@@ -96,6 +99,20 @@ const applyMarkdown = (
9699
)
97100
textarea.selectionStart = selectionStart
98101

102+
break
103+
case 'removeOrderedList':
104+
insertText(
105+
textarea,
106+
selectionLines.map(line => line.replace(/\d+\. /, '')).join('\n')
107+
)
108+
textarea.selectionStart = selectionStart
109+
break
110+
case 'addOrderedList':
111+
insertText(
112+
textarea,
113+
selectionLines.map((line, i) => `${i + 1}. ${line}`).join('\n')
114+
)
115+
textarea.selectionStart = selectionStart
99116
break
100117
default:
101118
// Handle both add cases 'addSingle' & 'addMulti'
@@ -141,17 +158,31 @@ type StyleDetectionInput = {
141158
selectedPrefix: string
142159
selectedSuffix: string
143160
multiline: boolean
161+
orderedList: boolean
144162
}
145-
type ActionTypes = 'addMulti' | 'addSingle' | 'removeSingle' | 'removeMulti'
163+
type ActionTypes =
164+
| 'addMulti'
165+
| 'addSingle'
166+
| 'addOrderedList'
167+
| 'removeOrderedList'
168+
| 'removeSingle'
169+
| 'removeMulti'
146170

147171
export const detectActionType = ({
148172
left,
149173
selectionLines,
150174
right,
151175
selectedPrefix,
152176
selectedSuffix,
153-
multiline
177+
multiline,
178+
orderedList
154179
}: StyleDetectionInput): ActionTypes => {
180+
if (orderedList) {
181+
return selectionLines.every(line => /^\d+\. /.test(line))
182+
? 'removeOrderedList'
183+
: 'addOrderedList'
184+
}
185+
155186
if (
156187
(!multiline || selectionLines.length === 1) &&
157188
left.endsWith(selectedPrefix) &&
@@ -253,7 +284,7 @@ const buttons = [
253284
},
254285
{
255286
tooltipTitle: 'Add a numbered list',
256-
styleArgs: { prefix: '1. ', surroundWithNewLines: true },
287+
styleArgs: { prefix: '1. ', surroundWithNewLines: true, orderedList: true },
257288
Icon: ListOrderedIcon
258289
},
259290
{

0 commit comments

Comments
 (0)