2016-08-11 17:06:01 -03:00
|
|
|
import React, { PropTypes } from 'react';
|
2016-10-26 18:48:47 +02:00
|
|
|
import MarkupIt from 'markup-it';
|
|
|
|
import markdownSyntax from 'markup-it/syntaxes/markdown';
|
|
|
|
import htmlSyntax from 'markup-it/syntaxes/html';
|
2016-10-22 04:37:22 -07:00
|
|
|
import CaretPosition from 'textarea-caret-position';
|
2016-10-22 23:12:21 +03:00
|
|
|
import registry from '../../../../lib/registry';
|
2017-01-10 22:23:22 -02:00
|
|
|
import { createAssetProxy } from '../../../../valueObjects/AssetProxy';
|
2016-11-01 16:55:21 -07:00
|
|
|
import Toolbar from '../Toolbar';
|
2017-03-16 20:45:46 -04:00
|
|
|
import ToolbarPlugins from '../ToolbarPlugins';
|
2017-03-23 17:15:48 -04:00
|
|
|
import { Sticky } from '../../../UI/Sticky/Sticky';
|
2016-10-22 04:37:22 -07:00
|
|
|
import styles from './index.css';
|
2016-10-21 23:16:19 -07:00
|
|
|
|
2016-10-22 04:37:22 -07:00
|
|
|
const HAS_LINE_BREAK = /\n/m;
|
2016-08-17 09:52:06 -03:00
|
|
|
|
2016-10-26 18:48:47 +02:00
|
|
|
const markdown = new MarkupIt(markdownSyntax);
|
|
|
|
const html = new MarkupIt(htmlSyntax);
|
|
|
|
|
2016-10-21 22:52:41 -07:00
|
|
|
function processUrl(url) {
|
|
|
|
if (url.match(/^(https?:\/\/|mailto:|\/)/)) {
|
|
|
|
return url;
|
|
|
|
}
|
2017-03-16 20:45:46 -04:00
|
|
|
if (url.match(/^[^/]+\.[^/]+/)) {
|
2016-10-21 22:52:41 -07:00
|
|
|
return `https://${ url }`;
|
|
|
|
}
|
|
|
|
return `/${ url }`;
|
|
|
|
}
|
2016-08-17 09:52:06 -03:00
|
|
|
|
2016-10-26 18:48:47 +02:00
|
|
|
function cleanupPaste(paste) {
|
|
|
|
const content = html.toContent(paste);
|
|
|
|
return markdown.toText(content);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCleanPaste(e) {
|
|
|
|
const transfer = e.clipboardData;
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
const isHTML = !!Array.from(transfer.types).find(type => type === 'text/html');
|
|
|
|
|
|
|
|
if (isHTML) {
|
|
|
|
const data = transfer.getData('text/html');
|
|
|
|
// Avoid trying to clean up full HTML documents with head/body/etc
|
|
|
|
if (!data.match(/^\s*<!doctype/i)) {
|
|
|
|
e.preventDefault();
|
|
|
|
resolve(cleanupPaste(data));
|
|
|
|
} else {
|
|
|
|
// Handle complex pastes by stealing focus with a contenteditable div
|
|
|
|
const div = document.createElement('div');
|
|
|
|
div.contentEditable = true;
|
2017-03-16 20:45:46 -04:00
|
|
|
div.setAttribute(
|
|
|
|
'style', 'opacity: 0; overflow: hidden; width: 1px; height: 1px; position: fixed; top: 50%; left: 0;'
|
|
|
|
);
|
2016-10-26 18:48:47 +02:00
|
|
|
document.body.appendChild(div);
|
|
|
|
div.focus();
|
|
|
|
setTimeout(() => {
|
|
|
|
resolve(cleanupPaste(div.innerHTML));
|
|
|
|
document.body.removeChild(div);
|
|
|
|
}, 50);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
return resolve(transfer.getData(transfer.types[0]));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-21 22:52:41 -07:00
|
|
|
export default class RawEditor extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-10-22 23:12:21 +03:00
|
|
|
const plugins = registry.getEditorComponents();
|
2016-11-01 23:31:20 -07:00
|
|
|
this.state = { plugins };
|
2016-10-21 22:52:41 -07:00
|
|
|
this.shortcuts = {
|
|
|
|
meta: {
|
|
|
|
b: this.handleBold,
|
2016-10-27 11:34:22 +02:00
|
|
|
i: this.handleItalic,
|
2016-10-21 22:52:41 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2016-12-27 23:18:37 -08:00
|
|
|
|
2016-10-21 22:52:41 -07:00
|
|
|
componentDidMount() {
|
|
|
|
this.updateHeight();
|
2016-10-26 18:48:47 +02:00
|
|
|
this.element.addEventListener('paste', this.handlePaste, false);
|
2016-10-21 23:16:19 -07: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 23:19:23 -07:00
|
|
|
componentWillUnmount() {
|
2016-12-27 23:18:37 -08:00
|
|
|
this.element.removeEventListener('paste', this.handlePaste);
|
2016-10-21 23:19:23 -07:00
|
|
|
}
|
2016-08-17 09:52:06 -03:00
|
|
|
|
2016-10-21 23:19:23 -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 };
|
|
|
|
}
|
2016-10-21 23:16:19 -07:00
|
|
|
|
2016-10-21 23:19:23 -07:00
|
|
|
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;
|
2016-10-21 23:16:19 -07:00
|
|
|
} else {
|
2016-10-21 23:19:23 -07:00
|
|
|
newSelection.end = selection.end + (chars.length * 2);
|
2016-10-21 23:16:19 -07:00
|
|
|
}
|
2016-10-21 23:19:23 -07:00
|
|
|
|
|
|
|
const beforeSelection = value.substr(0, selection.start);
|
|
|
|
const afterSelection = value.substr(selection.end);
|
|
|
|
|
|
|
|
this.newSelection = newSelection;
|
|
|
|
this.props.onChange(beforeSelection + changed + afterSelection);
|
|
|
|
}
|
|
|
|
|
|
|
|
replaceSelection(chars) {
|
2016-10-30 23:38:12 -07:00
|
|
|
const value = this.props.value || '';
|
2016-10-21 23:19:23 -07:00
|
|
|
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-10-21 23:16:19 -07:00
|
|
|
|
2016-10-26 18:48:47 +02:00
|
|
|
toggleHeader(header) {
|
2016-10-30 23:38:12 -07:00
|
|
|
const value = this.props.value || '';
|
2016-10-26 18:48:47 +02:00
|
|
|
const selection = this.getSelection();
|
|
|
|
const newSelection = Object.assign({}, selection);
|
|
|
|
const lastNewline = value.lastIndexOf('\n', selection.start);
|
|
|
|
const currentMatch = value.substr(lastNewline + 1).match(/^(#+)\s/);
|
|
|
|
const beforeHeader = value.substr(0, lastNewline + 1);
|
|
|
|
let afterHeader;
|
|
|
|
let chars;
|
|
|
|
if (currentMatch) {
|
|
|
|
afterHeader = value.substr(lastNewline + 1 + currentMatch[0].length);
|
|
|
|
chars = currentMatch[1] === header ? '' : `${ header } `;
|
|
|
|
const diff = chars.length - currentMatch[0].length;
|
|
|
|
newSelection.start += diff;
|
|
|
|
newSelection.end += diff;
|
|
|
|
} else {
|
|
|
|
afterHeader = value.substr(lastNewline + 1);
|
|
|
|
chars = `${ header } `;
|
|
|
|
newSelection.start += header.length + 1;
|
|
|
|
newSelection.end += header.length + 1;
|
|
|
|
}
|
|
|
|
this.newSelection = newSelection;
|
|
|
|
this.props.onChange(beforeHeader + chars + afterHeader);
|
|
|
|
}
|
|
|
|
|
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-22 04:37:22 -07:00
|
|
|
if (ref) {
|
|
|
|
this.caretPosition = new CaretPosition(ref);
|
|
|
|
}
|
2016-10-21 22:52:41 -07:00
|
|
|
};
|
2016-10-03 16:57:48 +02: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 = () => {
|
2017-03-16 20:45:46 -04:00
|
|
|
const url = prompt('URL:'); // eslint-disable-line no-alert
|
2016-10-21 22:52:41 -07:00
|
|
|
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 = () => {
|
2016-10-30 23:38:12 -07:00
|
|
|
const value = this.props.value || '';
|
2016-10-21 22:52:41 -07:00
|
|
|
const selection = this.getSelection();
|
2016-10-22 04:37:22 -07:00
|
|
|
if (selection.start !== selection.end && !HAS_LINE_BREAK.test(selection.selected)) {
|
|
|
|
try {
|
2016-11-01 16:55:21 -07:00
|
|
|
const selectionPosition = this.caretPosition.get(selection.start, selection.end);
|
2017-03-16 20:45:46 -04:00
|
|
|
this.setState({ selectionPosition });
|
2016-10-22 04:37:22 -07:00
|
|
|
} catch (e) {
|
2017-03-16 20:45:46 -04:00
|
|
|
console.log(e); // eslint-disable-line no-console
|
2016-10-22 23:12:21 +03:00
|
|
|
}
|
|
|
|
} else if (selection.start === selection.end) {
|
|
|
|
const newBlock =
|
|
|
|
(
|
2016-11-11 18:36:11 -02:00
|
|
|
(selection.start === 0 && value.substr(0, 1).match(/^\n?$/)) ||
|
2016-10-22 23:12:21 +03:00
|
|
|
value.substr(selection.start - 2, 2) === '\n\n') &&
|
|
|
|
(
|
|
|
|
selection.end === (value.length - 1) ||
|
|
|
|
value.substr(selection.end, 2) === '\n\n' ||
|
|
|
|
value.substr(selection.end).match(/\n*$/m)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (newBlock) {
|
|
|
|
const position = this.caretPosition.get(selection.start, selection.end);
|
2017-03-16 20:45:46 -04:00
|
|
|
this.setState({ selectionPosition: position });
|
2016-10-22 04:37:22 -07:00
|
|
|
}
|
|
|
|
}
|
2016-10-03 14:25:27 +02:00
|
|
|
};
|
2016-08-11 17:06:01 -03:00
|
|
|
|
2016-10-21 23:19:23 -07:00
|
|
|
handleChange = (e) => {
|
|
|
|
this.props.onChange(e.target.value);
|
|
|
|
this.updateHeight();
|
|
|
|
};
|
2016-10-21 22:52:41 -07:00
|
|
|
|
2017-03-16 20:45:46 -04:00
|
|
|
handlePlugin = (plugin, data) => {
|
2016-11-01 23:31:20 -07:00
|
|
|
const toBlock = plugin.get('toBlock');
|
|
|
|
this.replaceSelection(toBlock.call(toBlock, data.toJS()));
|
2016-10-22 23:12:21 +03:00
|
|
|
};
|
|
|
|
|
2016-10-26 18:48:47 +02:00
|
|
|
handleHeader(header) {
|
|
|
|
return () => {
|
|
|
|
this.toggleHeader(header);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-27 23:18:37 -08:00
|
|
|
handleDragEnter = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
this.setState({ dragging: true });
|
|
|
|
};
|
|
|
|
|
|
|
|
handleDragLeave = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
this.setState({ dragging: false });
|
|
|
|
};
|
|
|
|
|
|
|
|
handleDragOver = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
};
|
|
|
|
|
2016-10-21 23:19:23 -07:00
|
|
|
handleDrop = (e) => {
|
|
|
|
e.preventDefault();
|
2016-12-27 23:18:37 -08:00
|
|
|
|
|
|
|
this.setState({ dragging: false });
|
|
|
|
|
2016-10-21 23:19:23 -07:00
|
|
|
let data;
|
2016-10-21 22:52:41 -07:00
|
|
|
|
2016-10-21 23:19:23 -07:00
|
|
|
if (e.dataTransfer.files && e.dataTransfer.files.length) {
|
|
|
|
data = Array.from(e.dataTransfer.files).map((file) => {
|
2017-01-10 22:23:22 -02:00
|
|
|
const link = `[Uploading ${ file.name }...]()`;
|
2016-10-21 23:19:23 -07:00
|
|
|
if (file.type.split('/')[0] === 'image') {
|
|
|
|
return `!${ link }`;
|
|
|
|
}
|
2017-01-10 22:23:22 -02:00
|
|
|
|
|
|
|
createAssetProxy(file.name, file)
|
|
|
|
.then((assetProxy) => {
|
|
|
|
this.props.onAddAsset(assetProxy);
|
|
|
|
// TODO: Change the link text
|
|
|
|
});
|
2016-10-21 23:19:23 -07:00
|
|
|
return link;
|
|
|
|
}).join('\n\n');
|
2016-10-21 22:52:41 -07:00
|
|
|
} else {
|
2016-10-21 23:19:23 -07:00
|
|
|
data = e.dataTransfer.getData('text/plain');
|
2016-10-21 22:52:41 -07:00
|
|
|
}
|
2016-10-21 23:19:23 -07:00
|
|
|
this.replaceSelection(data);
|
|
|
|
};
|
2016-10-21 22:52:41 -07:00
|
|
|
|
2016-10-26 18:48:47 +02:00
|
|
|
handlePaste = (e) => {
|
|
|
|
const { value, onChange } = this.props;
|
|
|
|
const selection = this.getSelection();
|
|
|
|
const beforeSelection = value.substr(0, selection.start);
|
|
|
|
const afterSelection = value.substr(selection.end);
|
|
|
|
|
|
|
|
getCleanPaste(e).then((paste) => {
|
|
|
|
const newSelection = Object.assign({}, selection);
|
|
|
|
newSelection.start = newSelection.end = beforeSelection.length + paste.length;
|
|
|
|
this.newSelection = newSelection;
|
|
|
|
onChange(beforeSelection + paste + afterSelection);
|
|
|
|
});
|
2016-11-01 16:55:21 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
handleToggle = () => {
|
|
|
|
this.props.onMode('visual');
|
|
|
|
};
|
2016-10-26 18:48:47 +02:00
|
|
|
|
2016-08-11 17:06:01 -03:00
|
|
|
render() {
|
2017-03-17 23:17:14 -04:00
|
|
|
const { onAddAsset, onRemoveAsset, getAsset, rawMode } = this.props;
|
2017-03-16 20:45:46 -04:00
|
|
|
const { plugins, selectionPosition, dragging } = this.state;
|
2016-12-27 23:18:37 -08:00
|
|
|
const classNames = [styles.root];
|
|
|
|
if (dragging) {
|
|
|
|
classNames.push(styles.dragging);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (<div
|
|
|
|
className={classNames.join(' ')}
|
|
|
|
onDragEnter={this.handleDragEnter}
|
|
|
|
onDragLeave={this.handleDragLeave}
|
|
|
|
onDragOver={this.handleDragOver}
|
|
|
|
onDrop={this.handleDrop}
|
|
|
|
>
|
2017-03-23 17:15:48 -04:00
|
|
|
<Sticky className={styles.editorControlBar} fillContainerWidth={true}>
|
2017-03-16 20:45:46 -04:00
|
|
|
<Toolbar
|
|
|
|
selectionPosition={selectionPosition}
|
|
|
|
onH1={this.handleHeader('#')}
|
|
|
|
onH2={this.handleHeader('##')}
|
|
|
|
onBold={this.handleBold}
|
|
|
|
onItalic={this.handleItalic}
|
|
|
|
onLink={this.handleLink}
|
|
|
|
onToggleMode={this.handleToggle}
|
2017-03-17 23:17:14 -04:00
|
|
|
rawMode={rawMode}
|
2017-03-16 20:45:46 -04:00
|
|
|
/>
|
|
|
|
<ToolbarPlugins
|
|
|
|
selectionPosition={selectionPosition}
|
|
|
|
plugins={plugins}
|
|
|
|
onPlugin={this.handlePlugin}
|
|
|
|
onAddAsset={onAddAsset}
|
|
|
|
onRemoveAsset={onRemoveAsset}
|
|
|
|
getAsset={getAsset}
|
|
|
|
rawMode
|
|
|
|
/>
|
2017-03-23 17:15:48 -04:00
|
|
|
</Sticky>
|
2016-10-21 22:52:41 -07:00
|
|
|
<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
|
|
|
/>
|
2017-01-10 22:23:22 -02:00
|
|
|
<div className={styles.shim} />
|
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 = {
|
2017-01-10 22:23:22 -02:00
|
|
|
onAddAsset: PropTypes.func.isRequired,
|
|
|
|
onRemoveAsset: PropTypes.func.isRequired,
|
|
|
|
getAsset: PropTypes.func.isRequired,
|
2016-10-21 22:52:41 -07:00
|
|
|
onChange: PropTypes.func.isRequired,
|
2016-11-01 16:55:21 -07:00
|
|
|
onMode: PropTypes.func.isRequired,
|
2017-03-17 23:17:14 -04:00
|
|
|
rawMode: PropTypes.bool,
|
2016-10-21 22:52:41 -07:00
|
|
|
value: PropTypes.node,
|
|
|
|
};
|