-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
157 lines (140 loc) · 4.13 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
import debounce from 'lodash/debounce';
import React, { useState } from 'react';
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
className={this.props.isSelected ? 'ProseMirror-selectednode' : ''}
src={`https://www.youtube.com/embed/${videoId}?modestbranding=1`}
/>
);
}
}
const Example = () => {
const [readOnly, setReadOnly] = React.useState(false);
const [template, setTemplate] = useState(false);
const [dark, setDarkMode] = useState(localStorage.getItem('dark') === 'enabled');
const [value, setValue] = useState(undefined);
const [imageUrl, setImageUrl] = useState();
const [imageHandler, setHandler] = useState();
const handleToggleReadOnly = () => {
setReadOnly(!readOnly);
};
const handleToggleTemplate = () => {
setTemplate(!template);
};
const handleToggleDark = () => {
setDarkMode({ dark: !dark });
localStorage.setItem('dark', dark ? 'enabled' : 'disabled');
};
const handleUpdateValue = () => {
const existing = localStorage.getItem('saved') || '';
const updatedValue = `${existing}\n\nedit!`;
localStorage.setItem('saved', updatedValue);
setValue(updatedValue);
};
const handleChange = debounce(value => {
const text = value();
localStorage.setItem('saved', text);
}, 250);
const { body } = document;
if (body) body.style.backgroundColor = dark ? '#181A1B' : '#FFF';
const handleChangeOfInput = e => {
setImageUrl(e.target.value);
};
const callImageAdd = () => {
if (imageUrl && imageHandler) {
imageHandler(imageUrl);
} else {
console.log('imageUrl', imageUrl);
console.log('imageHandler', imageHandler);
}
};
return (
<div>
<div>
<input onChange={handleChangeOfInput} onBlur={callImageAdd}></input>
</div>
<div>
<br />
<button type="button" onClick={handleToggleReadOnly}>
{readOnly ? 'Switch to Editable' : 'Switch to Read-only'}
</button>{' '}
<button type="button" onClick={handleToggleDark}>
{dark ? 'Switch to Light' : 'Switch to Dark'}
</button>{' '}
<button type="button" onClick={handleToggleTemplate}>
{template ? 'Switch to Document' : 'Switch to Template'}
</button>{' '}
<button type="button" onClick={handleUpdateValue}>
Update value
</button>
</div>
<br />
<br />
<Editor
id="example"
readOnly={readOnly}
readOnlyWriteCheckboxes
value={value}
template={template}
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={handleChange}
// onClickLink={(href, event) => console.log('Clicked link: ', href, event)}
onHoverLink={event => {
console.log('Hovered link: ', event.target.href);
return false;
}}
onShowToast={(message, type) => window.alert(`${type}: ${message}`)}
insertImageHandler={handler => {
setHandler(() => handler);
// handler('https://picsum.photos/600/600');
}}
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={dark}
autoFocus
/>
</div>
);
};
if (element) {
ReactDOM.render(<Example />, element);
}