forked from outline/rich-markdown-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
220 lines (202 loc) · 6.48 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
import * as React from "react";
import { debounce } from "lodash";
import ReactDOM from "react-dom";
import Editor from "../../src";
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",
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",
},
];
class YoutubeEmbed extends React.Component {
render() {
const { attrs } = this.props;
const videoId = attrs.matches[1];
return (
<iframe
src={`https://www.youtube.com/embed/${videoId}?modestbranding=1`}
/>
);
}
}
class Example extends React.Component {
state = {
readOnly: false,
template: false,
disableExtensions: false,
enterToSave: false,
dark: localStorage.getItem("dark") === "enabled",
value: undefined,
};
handleToggleReadOnly = () => {
this.setState({ readOnly: !this.state.readOnly });
};
handleToggleEnterToSave = () => {
this.setState({ enterToSave: !this.state.enterToSave });
};
handleToggleTemplate = () => {
this.setState({ template: !this.state.template });
};
handleToggleDark = () => {
const dark = !this.state.dark;
this.setState({ dark });
localStorage.setItem("dark", dark ? "enabled" : "disabled");
};
handleToggleDisabledExtensions = () => {
const disableExtensions = !this.state.disableExtensions;
this.setState({ disableExtensions });
};
handleUpdateValue = () => {
const existing = localStorage.getItem("saved") || "";
const value = `${existing}\n\nedit!`;
localStorage.setItem("saved", value);
this.setState({ value });
};
handleChange = debounce(value => {
const text = value();
console.log(text);
localStorage.setItem("saved", text);
}, 250);
render() {
const { body } = document;
if (body) body.style.backgroundColor = this.state.dark ? "#181A1B" : "#FFF";
return (
<div>
<div>
<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.handleToggleEnterToSave}>
{this.state.enterToSave
? "Switch to Save on Mod-Enter"
: "Switch to Save on Enter"}
</button>{" "}
<button type="button" onClick={this.handleToggleDisabledExtensions}>
{this.state.disableExtensions
? "Enable dividers and highlights"
: "Disable dividers and highlights"}
</button>{" "}
<button type="button" onClick={this.handleUpdateValue}>
Update value
</button>
</div>
<br />
<br />
<Editor
id="example"
key={this.state.enterToSave}
readOnly={this.state.readOnly}
readOnlyWriteCheckboxes
value={this.state.value}
template={this.state.template}
disabledExtensions={
this.state.disableExtensions ? ["highlight", "horizontal_rule"] : []
}
defaultValue={defaultValue}
scrollTo={window.location.hash}
handleDOMEvents={{
focus: () => console.log("FOCUS"),
blur: () => console.log("BLUR"),
paste: () => console.log("PASTE"),
touchstart: () => console.log("TOUCH START"),
}}
onSave={options => console.log("Save triggered", options)}
onCancel={() => console.log("Cancel triggered")}
onChange={this.handleChange}
onClickLink={href => console.log("Clicked link: ", href)}
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);
});
}}
onShowToast={message => window.alert(message)}
onSearchLink={async term => {
console.log("Searched link: ", term);
return docSearchResults.filter(result =>
result.title.toLowerCase().includes(term.toLowerCase())
);
}}
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
);
});
}}
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,
},
]}
enterToSave={this.state.enterToSave}
dark={this.state.dark}
autoFocus
/>
</div>
);
}
}
if (element) {
ReactDOM.render(<Example />, element);
}