forked from outline/rich-markdown-editor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
333 lines (313 loc) · 8.82 KB
/
index.js
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import * as React from "react";
import debounce from "lodash/debounce";
import ReactDOM from "react-dom";
import Editor from "../../src";
// to publish, build with node 10, then publish with node 15
const element = document.getElementById("main");
const savedText = localStorage.getItem("saved");
const exampleText = `
# Welcome
This is example content. It is persisted between reloads in localStorage.
`;
const defaultValue = savedText || exampleText;
const docSearchResults = [
{
title: "Hiring",
subtitle: "Created by Jane",
url: "/doc/hiring",
},
{
title: "Hiring1",
url: "/doc/hiring",
},
{
title: "Hiring2",
url: "/doc/hiring",
},
{
title: "Hiring3",
url: "/doc/hiring",
},
{
title: "Hiring4",
url: "/doc/hiring",
},
{
title: "Hiring5",
url: "/doc/hiring",
},
{
title: "Hiring6",
url: "/doc/hiring",
},
{
title: "Hiring7",
url: "/doc/hiring",
},
{
title: "Hiring8",
url: "/doc/hiring",
},
{
title: "Hiring9",
url: "/doc/hiring",
},
{
title: "Hiring10",
url: "/doc/hiring",
},
{
title: "Hiring11",
url: "/doc/hiring",
},
{
title: "Hiring12",
url: "/doc/hiring",
},
{
title: "Hiring13",
url: "/doc/hiring",
},
{
title: "Hiring14",
url: "/doc/hiring",
},
{
title: "Hiring15",
url: "/doc/hiring",
},
{
title: "Hiring16",
url: "/doc/hiring",
},
{
title: "Hiring17",
url: "/doc/hiring",
},
{
title: "Hiring18",
url: "/doc/hiring",
},
{
title: "Hiring19",
url: "/doc/hiring",
},
{
title: "Hiring20",
url: "/doc/hiring",
},
{
title: "Product Roadmap",
url: "/doc/product-roadmap",
},
{
title: "Finances",
url: "/doc/finances",
},
{
title: "Super secret stuff",
url: "/doc/secret-stuff",
},
{
title: "Meeting notes",
url: "/doc/meeting-notes",
},
];
const onCreateFlashcard = (txt, surroundingTxt) =>
console.log(`txt`, txt, surroundingTxt);
class YoutubeEmbed extends React.Component {
render() {
const { attrs } = this.props;
const videoId = attrs.matches[1];
return (
<iframe
className={this.props.isSelected ? "ProseMirror-selectednode" : ""}
src={`https://www.youtube.com/embed/${videoId}?modestbranding=1`}
/>
);
}
}
class AudioEmbed extends React.Component {
render() {
const { attrs } = this.props;
const mp3 = attrs.href;
return <audio src={mp3} id={mp3} controls />;
}
}
class Example extends React.Component {
state = {
readOnly: false,
template: false,
dark: localStorage.getItem("dark") === "enabled",
value: undefined,
};
handleToggleReadOnly = () => {
this.setState({ readOnly: !this.state.readOnly });
};
handleToggleTemplate = () => {
this.setState({ template: !this.state.template });
};
handleToggleDark = () => {
const dark = !this.state.dark;
this.setState({ dark });
localStorage.setItem("dark", dark ? "enabled" : "disabled");
};
handleUpdateValue = () => {
const existing = localStorage.getItem("saved") || "";
const value = `${existing}\n\nedit!`;
localStorage.setItem("saved", value);
this.setState({ value });
};
handleChange = debounce(value => {
const text = value();
localStorage.setItem("saved", text);
console.log(`SAVED`, text);
}, 250);
render() {
const { body } = document;
if (body) body.style.backgroundColor = this.state.dark ? "#181A1B" : "#FFF";
return (
<div>
<div style={{ overflowY: "auto" }}>
<br />
<button type="button" onClick={this.handleToggleReadOnly}>
{this.state.readOnly ? "Switch to Editable" : "Switch to Read-only"}
</button>{" "}
<button type="button" onClick={this.handleToggleDark}>
{this.state.dark ? "Switch to Light" : "Switch to Dark"}
</button>{" "}
<button type="button" onClick={this.handleToggleTemplate}>
{this.state.template ? "Switch to Document" : "Switch to Template"}
</button>{" "}
<button type="button" onClick={this.handleUpdateValue}>
Update value
</button>
</div>
<br />
<br />
<Editor
id="example"
onCreateFlashcard={onCreateFlashcard}
onMakeAnswer={txt => console.log(`make answer`, txt)}
editorMinHeight="400px"
readOnly={this.state.readOnly}
newLinePlaceholder={"Hello"}
readOnlyWriteCheckboxes
value={this.state.value}
enableTemplatePlaceholder={true}
template={this.state.template}
defaultValue={defaultValue}
scrollTo={window.location.hash}
getPlaceHolderLink={title => `/test/${title}`}
handleDOMEvents={{
focus: () => console.log("FOCUS"),
blur: () => console.log("BLUR"),
paste: (view, event) => console.log("PASTE", view, event),
touchstart: () => console.log("TOUCH START"),
}}
onSave={options => console.log("Save triggered", options)}
onCancel={() => console.log("Cancel triggered")}
onChange={this.handleChange}
onClickLink={(href, e) => console.log("Clicked link: ", href, e)}
onHoverLink={event => {
console.log("Hovered link: ", event.target.href);
return false;
}}
onClickHashtag={tag => console.log("Clicked hashtag: ", tag)}
onCreateLink={title => {
// Delay to simulate time taken for remote API request to complete
return new Promise((resolve, reject) => {
setTimeout(() => {
if (title !== "error") {
return resolve(
`/doc/${encodeURIComponent(title.toLowerCase())}`
);
} else {
reject("500 error");
}
}, 1500);
});
}}
LinkFinder={() => <div>HELLO</div>}
onShowToast={message => window.alert(message)}
onSearchLink={async (term, setter) => {
console.log("Searched link: ", term);
// Delay to simulate time taken for remote API request to complete
return new Promise(resolve => {
setTimeout(() => {
const result = docSearchResults.filter(result =>
result.title.toLowerCase().includes(term.toLowerCase())
);
setter({ user: result });
}, Math.random() * 500);
});
}}
uploadImage={file => {
console.log("File upload triggered: ", file);
// Delay to simulate time taken to upload
return new Promise(resolve => {
setTimeout(
() => resolve("https://loremflickr.com/1000/1000"),
1500
);
});
}}
uploadAudio={file => {
console.log("File upload triggered: ", file);
// Delay to simulate time taken to upload
return new Promise(resolve => {
setTimeout(
() => resolve("https://www.purpleculture.net/mp3/yuan1.mp3"),
1500
);
});
}}
embeds={[
{
title: "YouTube",
keywords: "youtube video tube google",
icon: () => (
<img
src="https://upload.wikimedia.org/wikipedia/commons/7/75/YouTube_social_white_squircle_%282017%29.svg"
width={24}
height={24}
/>
),
matcher: url => {
return url.match(
/(?:https?:\/\/)?(?:www\.)?youtu\.?be(?:\.com)?\/?.*(?:watch|embed)?(?:.*v=|v\/|\/)([a-zA-Z0-9_-]{11})$/i
);
},
component: YoutubeEmbed,
},
{
title: "Audio link",
keywords: "audio mp3",
icon: () => (
<img
src="https://upload.wikimedia.org/wikipedia/commons/7/75/YouTube_social_white_squircle_%282017%29.svg"
width={24}
height={24}
/>
),
matcher: url => {
// FIXME support audio commonly used in language learning, like google translate, baidu in general
const isAudio = url.match(
/(?:https?:\/\/)?.*.(?:wav|mp3|ogg|m4a|&spd=2|mp3)(\?alt.*|)$/i
);
return isAudio;
},
component: AudioEmbed,
},
]}
dark={this.state.dark}
autoFocus
placeholders={"Write stuff"}
/>
</div>
);
}
}
if (element) {
ReactDOM.render(<Example />, element);
}