This repository has been archived by the owner on Oct 20, 2021. It is now read-only.
forked from outline/rich-markdown-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
119 lines (106 loc) · 3.24 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
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 GoogleEmbed extends React.Component {
render() {
const { attrs } = this.props;
return <p>Google Embed ({attrs.href})</p>;
}
}
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
);
});
}}
getLinkComponent={href => {
if (href.match(/google/)) {
return GoogleEmbed;
}
}}
dark={this.state.dark}
autoFocus
/>
</div>
);
}
}
if (element) {
ReactDOM.render(<Example />, element);
}