2016-08-11 17:06:01 -03:00
|
|
|
import React, { PropTypes } from 'react';
|
2016-10-21 22:52:41 -07:00
|
|
|
import Toolbar from './Toolbar';
|
2016-10-21 23:16:19 -07:00
|
|
|
import MediaProxy from '../../../../valueObjects/MediaProxy';
|
|
|
|
|
2016-08-17 09:52:06 -03:00
|
|
|
|
2016-10-21 22:52:41 -07:00
|
|
|
function processUrl(url) {
|
|
|
|
if (url.match(/^(https?:\/\/|mailto:|\/)/)) {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
if (url.match(/^[^\/]+\.[^\/]+/)) {
|
|
|
|
return `https://${ url }`;
|
|
|
|
}
|
|
|
|
return `/${ url }`;
|
|
|
|
}
|
2016-08-17 09:52:06 -03:00
|
|
|
|
2016-10-21 23:16:19 -07:00
|
|
|
function preventDefault(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
|
2016-10-21 22:52:41 -07:00
|
|
|
export default class RawEditor extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {};
|
|
|
|
this.shortcuts = {
|
|
|
|
meta: {
|
|
|
|
b: this.handleBold,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
componentDidMount() {
|
|
|
|
this.updateHeight();
|
2016-10-21 23:16:19 -07:00
|
|
|
this.element.addEventListener('dragenter', preventDefault, false);
|
|
|
|
this.element.addEventListener('dragover', preventDefault, false);
|
|
|
|
this.element.addEventListener('drop', this.handleDrop, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
console.log('unmounting');
|
|
|
|
this.element.removeEventListener('dragenter', preventDefault);
|
|
|
|
this.element.removeEventListener('dragover', preventDefault);
|
|
|
|
this.element.removeEventListener('drop', this.handleDrop);
|
2016-10-21 22:52:41 -07:00
|
|
|
}
|
2016-08-17 09:52:06 -03:00
|
|
|
|
2016-10-21 22:52:41 -07:00
|
|
|
componentDidUpdate() {
|
|
|
|
if (this.newSelection) {
|
|
|
|
this.element.selectionStart = this.newSelection.start;
|
|
|
|
this.element.selectionEnd = this.newSelection.end;
|
|
|
|
this.newSelection = null;
|
|
|
|
}
|
2016-08-17 09:52:06 -03:00
|
|
|
}
|
|
|
|
|
2016-10-21 22:52:41 -07:00
|
|
|
handleChange = (e) => {
|
|
|
|
this.props.onChange(e.target.value);
|
|
|
|
this.updateHeight();
|
|
|
|
};
|
2016-08-17 09:52:06 -03:00
|
|
|
|
2016-10-21 23:16:19 -07:00
|
|
|
handleDrop = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
let data;
|
|
|
|
|
|
|
|
if (e.dataTransfer.files && e.dataTransfer.files.length) {
|
|
|
|
data = Array.from(e.dataTransfer.files).map((file) => {
|
|
|
|
const mediaProxy = new MediaProxy(file.name, file);
|
|
|
|
this.props.onAddMedia(mediaProxy);
|
|
|
|
const link = `[${ file.name }](${ mediaProxy.public_path })`;
|
|
|
|
if (file.type.split('/')[0] === 'image') {
|
|
|
|
return `!${ link }`;
|
|
|
|
}
|
|
|
|
return link;
|
|
|
|
}).join('\n\n');
|
|
|
|
} else {
|
|
|
|
data = e.dataTransfer.getData('text/plain');
|
|
|
|
}
|
|
|
|
this.replaceSelection(data);
|
|
|
|
};
|
|
|
|
|
2016-10-21 22:52:41 -07:00
|
|
|
updateHeight() {
|
|
|
|
if (this.element.scrollHeight > this.element.clientHeight) {
|
|
|
|
this.element.style.height = `${ this.element.scrollHeight }px`;
|
|
|
|
}
|
|
|
|
}
|
2016-08-17 09:52:06 -03:00
|
|
|
|
2016-10-21 22:52:41 -07:00
|
|
|
handleRef = (ref) => {
|
|
|
|
this.element = ref;
|
|
|
|
};
|
2016-10-03 16:57:48 +02:00
|
|
|
|
2016-10-21 22:52:41 -07:00
|
|
|
handleToolbarRef = (ref) => {
|
|
|
|
this.toolbar = ref;
|
2016-10-03 16:57:48 +02:00
|
|
|
};
|
2016-08-11 17:06:01 -03:00
|
|
|
|
2016-10-21 22:52:41 -07:00
|
|
|
handleKey = (e) => {
|
|
|
|
if (e.metaKey) {
|
|
|
|
const action = this.shortcuts.meta[e.key];
|
|
|
|
if (action) {
|
|
|
|
e.preventDefault();
|
|
|
|
action();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-08-11 17:06:01 -03:00
|
|
|
|
2016-10-21 22:52:41 -07:00
|
|
|
handleBold = () => {
|
|
|
|
this.surroundSelection('**');
|
|
|
|
};
|
2016-08-11 17:06:01 -03:00
|
|
|
|
2016-10-21 22:52:41 -07:00
|
|
|
handleItalic = () => {
|
|
|
|
this.surroundSelection('*');
|
|
|
|
};
|
2016-08-11 17:06:01 -03:00
|
|
|
|
2016-10-21 22:52:41 -07:00
|
|
|
handleLink = () => {
|
|
|
|
const url = prompt('URL:');
|
|
|
|
const selection = this.getSelection();
|
|
|
|
this.replaceSelection(`[${ selection.selected }](${ processUrl(url) })`);
|
2016-10-03 14:25:27 +02:00
|
|
|
};
|
2016-08-11 17:06:01 -03:00
|
|
|
|
2016-10-21 22:52:41 -07:00
|
|
|
handleSelection = () => {
|
|
|
|
const selection = this.getSelection();
|
|
|
|
this.setState({ showToolbar: selection.start !== selection.end });
|
2016-10-03 14:25:27 +02:00
|
|
|
};
|
2016-08-11 17:06:01 -03:00
|
|
|
|
2016-10-21 22:52:41 -07:00
|
|
|
getSelection() {
|
|
|
|
const start = this.element.selectionStart;
|
|
|
|
const end = this.element.selectionEnd;
|
|
|
|
const selected = (this.props.value || '').substr(start, end - start);
|
|
|
|
return { start, end, selected };
|
|
|
|
}
|
|
|
|
|
|
|
|
surroundSelection(chars) {
|
|
|
|
const selection = this.getSelection();
|
|
|
|
const newSelection = Object.assign({}, selection);
|
|
|
|
const { value } = this.props;
|
|
|
|
const escapedChars = chars.replace(/\*/g, '\\*');
|
|
|
|
const regexp = new RegExp(`^${ escapedChars }.*${ escapedChars }$`);
|
|
|
|
let changed = chars + selection.selected + chars;
|
|
|
|
|
|
|
|
if (regexp.test(selection.selected)) {
|
|
|
|
changed = selection.selected.substr(chars.length, selection.selected.length - (chars.length * 2));
|
|
|
|
newSelection.end = selection.end - (chars.length * 2);
|
|
|
|
} else if (
|
|
|
|
value.substr(selection.start - chars.length, chars.length) === chars &&
|
|
|
|
value.substr(selection.end, chars.length) === chars
|
|
|
|
) {
|
|
|
|
newSelection.start = selection.start - chars.length;
|
|
|
|
newSelection.end = selection.end + chars.length;
|
|
|
|
changed = selection.selected;
|
|
|
|
} else {
|
|
|
|
newSelection.end = selection.end + (chars.length * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
const beforeSelection = value.substr(0, selection.start);
|
|
|
|
const afterSelection = value.substr(selection.end);
|
|
|
|
|
|
|
|
this.newSelection = newSelection;
|
|
|
|
this.props.onChange(beforeSelection + changed + afterSelection);
|
|
|
|
}
|
|
|
|
|
|
|
|
replaceSelection(chars) {
|
|
|
|
const { value } = this.props;
|
|
|
|
const selection = this.getSelection();
|
|
|
|
const newSelection = Object.assign({}, selection);
|
|
|
|
const beforeSelection = value.substr(0, selection.start);
|
|
|
|
const afterSelection = value.substr(selection.end);
|
|
|
|
newSelection.end = selection.start + chars.length;
|
|
|
|
this.newSelection = newSelection;
|
|
|
|
this.props.onChange(beforeSelection + chars + afterSelection);
|
|
|
|
}
|
|
|
|
|
2016-08-11 17:06:01 -03:00
|
|
|
render() {
|
2016-10-21 22:52:41 -07:00
|
|
|
const { showToolbar } = this.state;
|
|
|
|
return (<div>
|
|
|
|
<Toolbar
|
|
|
|
ref={this.handleToolbarRef}
|
|
|
|
isOpen={showToolbar}
|
|
|
|
onBold={this.handleBold}
|
|
|
|
onItalic={this.handleItalic}
|
|
|
|
onLink={this.handleLink}
|
|
|
|
/>
|
|
|
|
<textarea
|
|
|
|
ref={this.handleRef}
|
|
|
|
value={this.props.value || ''}
|
|
|
|
onKeyDown={this.handleKey}
|
2016-09-28 12:46:39 +02:00
|
|
|
onChange={this.handleChange}
|
2016-10-21 22:52:41 -07:00
|
|
|
onSelect={this.handleSelection}
|
2016-08-11 17:06:01 -03:00
|
|
|
/>
|
2016-10-21 22:52:41 -07:00
|
|
|
</div>);
|
2016-08-11 17:06:01 -03:00
|
|
|
}
|
|
|
|
}
|
2016-10-21 22:52:41 -07:00
|
|
|
|
|
|
|
RawEditor.propTypes = {
|
2016-10-21 23:16:19 -07:00
|
|
|
onAddMedia: PropTypes.func.isRequired,
|
2016-10-21 22:52:41 -07:00
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
value: PropTypes.node,
|
|
|
|
};
|