forked from outline/rich-markdown-editor
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
139 lines (125 loc) · 3.89 KB
/
Copy pathindex.js
File metadata and controls
139 lines (125 loc) · 3.89 KB
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
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;
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,
dark: localStorage.getItem("dark") === "enabled",
value: undefined,
};
handleToggleReadOnly = () => {
this.setState({ readOnly: !this.state.readOnly });
};
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();
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 ? "Editable" : "Read only"}
</button>{" "}
<button type="button" onClick={this.handleToggleDark}>
{this.state.dark ? "Light theme" : "Dark theme"}
</button>{" "}
<button type="button" onClick={this.handleUpdateValue}>
Update value
</button>
</div>
<br />
<br />
<Editor
id="example"
readOnly={this.state.readOnly}
value={this.state.value}
defaultValue={defaultValue}
onSave={options => console.log("Save triggered", options)}
onCancel={() => console.log("Cancel triggered")}
onChange={this.handleChange}
onClickLink={href => console.log("Clicked link: ", href)}
onClickHashtag={tag => console.log("Clicked hashtag: ", tag)}
onShowToast={message => window.alert(message)}
onSearchLink={async term => {
console.log("Searched link: ", term);
return [
{
title: term,
url: "localhost",
},
];
}}
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,
},
]}
dark={this.state.dark}
autoFocus
/>
</div>
);
}
}
if (element) {
ReactDOM.render(<Example />, element);
}