-
Notifications
You must be signed in to change notification settings - Fork 536
/
MarkdownEditor.stories.tsx
288 lines (271 loc) · 9.55 KB
/
MarkdownEditor.stories.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import {DiffIcon} from '@primer/octicons-react'
import React, {Meta} from '@storybook/react'
import {useState} from 'react'
import BaseStyles from '../../BaseStyles'
import Box from '../../Box'
import MarkdownEditor, {Emoji, Mentionable, Reference, SavedReply} from '.'
import ThemeProvider from '../../ThemeProvider'
const meta: Meta = {
title: 'Forms/MarkdownEditor',
decorators: [
Story => {
return (
<ThemeProvider>
<BaseStyles>
<Box sx={{maxWidth: 800}}>{Story()}</Box>
</BaseStyles>
</ThemeProvider>
)
}
],
parameters: {
controls: {
include: [
'Disabled',
'Full Height',
'Monospace Font',
'Minimum Height (Lines)',
'Maximum Height (Lines)',
'Hide Label',
'Required',
'Enable File Uploads',
'Enable Saved Replies'
]
}
},
component: MarkdownEditor,
argTypes: {
disabled: {
name: 'Disabled',
defaultValue: false,
control: {
type: 'boolean'
}
},
fullHeight: {
name: 'Full Height',
defaultValue: false,
control: {
type: 'boolean'
}
},
monospace: {
name: 'Monospace Font',
defaultValue: false,
control: {
type: 'boolean'
}
},
minHeightLines: {
name: 'Minimum Height (Lines)',
defaultValue: 5,
control: {
type: 'number'
}
},
maxHeightLines: {
name: 'Maximum Height (Lines)',
defaultValue: 35,
control: {
type: 'number'
}
},
hideLabel: {
name: 'Hide Label',
defaultValue: false,
control: {
type: 'boolean'
}
},
required: {
name: 'Required',
defaultValue: false,
control: {
type: 'boolean'
}
},
fileUploadsEnabled: {
name: 'Enable File Uploads',
defaultValue: true,
control: {
type: 'boolean'
}
},
savedRepliesEnabled: {
name: 'Enable Saved Replies',
defaultValue: true,
control: {
type: 'boolean'
}
},
onSubmit: {
name: 'onSubmit',
action: 'submitted'
},
onDiffClick: {
name: 'onDiffClick',
action: 'diff-clicked'
}
}
}
export default meta
type ArgProps = {
disabled: boolean
fullHeight: boolean
monospace: boolean
minHeightLines: number
maxHeightLines: number
hideLabel: boolean
required: boolean
fileUploadsEnabled: boolean
savedRepliesEnabled: boolean
onSubmit: () => void
onDiffClick: () => void
}
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
const fakeFileUrl = (file: File) => `https://image-store.example/file/${encodeURIComponent(file.name)}`
const mentionables: Mentionable[] = [
{identifier: 'monalisa', description: 'Monalisa Octocat'},
{identifier: 'github', description: 'GitHub'},
{identifier: 'primer', description: 'Primer'}
]
const emojis: Emoji[] = [
{name: '+1', character: '👍'},
{name: '-1', character: '👎'},
{name: 'heart', character: '❤️'},
{name: 'wave', character: '👋'},
{name: 'raised_hands', character: '🙌'},
{name: 'pray', character: '🙏'},
{name: 'clap', character: '👏'},
{name: 'ok_hand', character: '👌'},
{name: 'point_up', character: '☝️'},
{name: 'point_down', character: '👇'},
{name: 'point_left', character: '👈'},
{name: 'point_right', character: '👉'},
{name: 'raised_hand', character: '✋'},
{name: 'thumbsup', character: '👍'},
{name: 'thumbsdown', character: '👎'}
]
const references: Reference[] = [
{id: '1', titleText: 'Add logging functionality', titleHtml: 'Add logging functionality'},
{
id: '2',
titleText: 'Error: `Failed to install` when installing',
titleHtml: 'Error: <code>Failed to install</code> when installing'
},
{id: '3', titleText: 'Add error-handling functionality', titleHtml: 'Add error-handling functionality'}
]
const savedReplies: SavedReply[] = [
{name: 'Duplicate', content: 'Duplicate of #'},
{name: 'Welcome', content: 'Welcome to the project!\n\nPlease be sure to read the contributor guidelines.'},
{name: 'Thanks', content: 'Thanks for your contribution!'},
{
name: 'Long Lorem Ipsum',
content:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam sodales ligula commodo ex venenatis molestie. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Curabitur vulputate elementum dolor ac sollicitudin. Duis tellus quam, hendrerit sit amet metus quis, pharetra consectetur eros. Duis purus justo, convallis nec velit nec, feugiat pharetra nibh. Aenean vulputate urna sollicitudin vehicula fermentum. Vestibulum semper iaculis metus, quis ullamcorper dui feugiat a. Donec nulla sapien, tincidunt ut arcu sit amet, ultrices fringilla massa. Integer ac justo lacus.\n\nFusce sed pharetra sem. Nulla rutrum turpis magna, sit amet sodales dui vehicula in. Cras lacinia, dui sit amet dictum lobortis, arcu erat semper lectus, placerat accumsan diam dolor nec quam. Vivamus accumsan ut magna eget maximus. Integer scelerisque justo et quam pharetra, nec placerat nibh auctor. Vestibulum cursus, mauris id euismod convallis, justo sapien faucibus dolor, nec dictum erat urna at velit. Quisque egestas massa eget odio consectetur vehicula. Aliquam a imperdiet lacus, eu facilisis mauris. Etiam tempor neque vitae erat elementum bibendum. Fusce ultricies nunc tortor.\n\nQuisque in posuere sapien. Nulla ornare sagittis tellus eu laoreet. Sed molestie sem in turpis blandit pretium. Vivamus gravida dui id gravida aliquam. Vestibulum vestibulum, justo vitae cursus mattis, urna mauris pulvinar dolor, eu suscipit magna libero eget diam. Praesent id rutrum libero, a feugiat nulla. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Curabitur ornare libero id augue fringilla maximus sed sed ante. Quisque finibus accumsan lorem ut lobortis. Maecenas lobortis lacus sed mattis rutrum. Aliquam a mi sodales, blandit nisi ut, volutpat ex. Duis tristique, erat quis fermentum ultricies, leo ipsum placerat nunc, eu aliquam nibh mauris vitae lectus. Proin vitae tellus nec lorem vulputate faucibus. In hac habitasse platea dictumst. Suspendisse dictum odio in faucibus mattis.'
}
]
const onUploadFile = async (file: File) => {
const wait = 0.0002 * file.size + 500
await delay(wait / 2)
// to demo file rejections:
if (file.name.toLowerCase().startsWith('a')) throw new Error("Rejected file for starting with the letter 'a'")
// 0.5 - 5 seconds depending on file size up to about 20 MB
await delay(wait / 2)
return {file, url: fakeFileUrl(file)}
}
const renderPreview = async () => {
await delay(500)
return 'Previewing Markdown is not supported in this example.'
}
export const Default = ({
disabled,
fullHeight,
monospace,
minHeightLines,
maxHeightLines,
hideLabel,
required,
fileUploadsEnabled,
onSubmit,
savedRepliesEnabled
}: ArgProps) => {
const [value, setValue] = useState('')
return (
<>
<MarkdownEditor
value={value}
onChange={setValue}
onPrimaryAction={onSubmit}
disabled={disabled}
fullHeight={fullHeight}
monospace={monospace}
minHeightLines={minHeightLines}
maxHeightLines={maxHeightLines}
placeholder="Enter some Markdown..."
onRenderPreview={renderPreview}
onUploadFile={fileUploadsEnabled ? onUploadFile : undefined}
emojiSuggestions={emojis}
mentionSuggestions={mentionables}
referenceSuggestions={references}
savedReplies={savedRepliesEnabled ? savedReplies : undefined}
required={required}
>
<MarkdownEditor.Label visuallyHidden={hideLabel}>Markdown Editor Example</MarkdownEditor.Label>
</MarkdownEditor>
<p>Note: for demo purposes, files starting with "A" will be rejected.</p>
</>
)
}
export const CustomButtons = ({
disabled,
fullHeight,
monospace,
minHeightLines,
maxHeightLines,
hideLabel,
required,
fileUploadsEnabled,
onSubmit,
onDiffClick,
savedRepliesEnabled
}: ArgProps) => {
const [value, setValue] = useState('')
return (
<>
<MarkdownEditor
value={value}
onChange={setValue}
onPrimaryAction={onSubmit}
disabled={disabled}
fullHeight={fullHeight}
monospace={monospace}
minHeightLines={minHeightLines}
maxHeightLines={maxHeightLines}
placeholder="Enter some Markdown..."
onRenderPreview={renderPreview}
onUploadFile={fileUploadsEnabled ? onUploadFile : undefined}
emojiSuggestions={emojis}
mentionSuggestions={mentionables}
referenceSuggestions={references}
required={required}
savedReplies={savedRepliesEnabled ? savedReplies : undefined}
>
<MarkdownEditor.Label visuallyHidden={hideLabel}>Markdown Editor Example</MarkdownEditor.Label>
<MarkdownEditor.Toolbar>
<MarkdownEditor.ToolbarButton icon={DiffIcon} onClick={onDiffClick} aria-label="Custom Button" />
<MarkdownEditor.DefaultToolbarButtons />
</MarkdownEditor.Toolbar>
<MarkdownEditor.Actions>
<MarkdownEditor.ActionButton variant="danger" onClick={() => setValue('')}>
Reset
</MarkdownEditor.ActionButton>
<MarkdownEditor.ActionButton variant="primary" onClick={onSubmit}>
Submit
</MarkdownEditor.ActionButton>
</MarkdownEditor.Actions>
</MarkdownEditor>
<p>Note: for demo purposes, files starting with "A" will be rejected.</p>
</>
)
}