2016-11-04 11:04:54 -07:00
|
|
|
import React, { Component, PropTypes } from 'react';
|
2017-07-13 21:33:50 -04:00
|
|
|
import ReactDOMServer from 'react-dom/server';
|
|
|
|
import { Map, List, fromJS } from 'immutable';
|
2017-07-18 19:14:40 -04:00
|
|
|
import { get, reduce, mapValues } from 'lodash';
|
2017-07-13 21:33:50 -04:00
|
|
|
import cn from 'classnames';
|
|
|
|
import { Editor as SlateEditor, Html as SlateHtml, Raw as SlateRaw, Text as SlateText, Block as SlateBlock, Selection as SlateSelection} from 'slate';
|
2017-07-12 19:22:17 -04:00
|
|
|
import EditList from 'slate-edit-list';
|
2017-07-18 19:14:40 -04:00
|
|
|
import EditTable from 'slate-edit-table';
|
|
|
|
import { markdownToRemark, remarkToMarkdown, slateToRemark, remarkToSlate, markdownToHtml, htmlToMarkdown } from '../../unified';
|
2017-06-23 14:42:40 -04:00
|
|
|
import registry from '../../../../../lib/registry';
|
|
|
|
import { createAssetProxy } from '../../../../../valueObjects/AssetProxy';
|
2017-05-22 14:04:24 -04:00
|
|
|
import Toolbar from '../Toolbar/Toolbar';
|
2017-06-23 14:42:40 -04:00
|
|
|
import { Sticky } from '../../../../UI/Sticky/Sticky';
|
2017-05-22 14:04:24 -04:00
|
|
|
import styles from './index.css';
|
2016-11-01 16:55:21 -07:00
|
|
|
|
2017-06-21 13:49:43 -04:00
|
|
|
|
2016-11-01 17:58:19 -07:00
|
|
|
function processUrl(url) {
|
|
|
|
if (url.match(/^(https?:\/\/|mailto:|\/)/)) {
|
|
|
|
return url;
|
|
|
|
}
|
2017-03-16 20:45:46 -04:00
|
|
|
if (url.match(/^[^/]+\.[^/]+/)) {
|
2016-11-01 17:58:19 -07:00
|
|
|
return `https://${ url }`;
|
|
|
|
}
|
|
|
|
return `/${ url }`;
|
|
|
|
}
|
|
|
|
|
2017-06-19 17:15:59 -04:00
|
|
|
const DEFAULT_NODE = 'paragraph';
|
2016-11-01 17:58:19 -07:00
|
|
|
|
2016-11-01 23:31:20 -07:00
|
|
|
function schemaWithPlugins(schema, plugins) {
|
|
|
|
let nodeSpec = schema.nodeSpec;
|
|
|
|
plugins.forEach((plugin) => {
|
|
|
|
const attrs = {};
|
2017-05-22 14:04:24 -04:00
|
|
|
plugin.get('fields').forEach((field) => {
|
|
|
|
attrs[field.get('name')] = { default: null };
|
2016-11-01 23:31:20 -07:00
|
|
|
});
|
2017-05-22 14:04:24 -04:00
|
|
|
nodeSpec = nodeSpec.addToEnd(`plugin_${ plugin.get('id') }`, {
|
2016-11-01 23:31:20 -07:00
|
|
|
attrs,
|
2017-05-22 14:04:24 -04:00
|
|
|
group: 'block',
|
2017-05-22 14:34:49 -04:00
|
|
|
parseDOM: [{
|
|
|
|
tag: 'div[data-plugin]',
|
|
|
|
getAttrs(dom) {
|
|
|
|
return JSON.parse(dom.getAttribute('data-plugin'));
|
2016-11-01 23:31:20 -07:00
|
|
|
},
|
2017-05-22 14:34:49 -04:00
|
|
|
}],
|
2016-11-01 23:31:20 -07:00
|
|
|
toDOM(node) {
|
2017-05-22 14:34:49 -04:00
|
|
|
return ['div', { 'data-plugin': JSON.stringify(node.attrs) }, plugin.get('label')];
|
2016-11-01 23:31:20 -07:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return new Schema({
|
|
|
|
nodes: nodeSpec,
|
|
|
|
marks: schema.markSpec,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function createSerializer(schema, plugins) {
|
|
|
|
const serializer = Object.create(defaultMarkdownSerializer);
|
|
|
|
plugins.forEach((plugin) => {
|
2017-05-22 14:04:24 -04:00
|
|
|
serializer.nodes[`plugin_${ plugin.get('id') }`] = (state, node) => {
|
|
|
|
const toBlock = plugin.get('toBlock');
|
2017-01-10 22:23:22 -02:00
|
|
|
state.write(`${ toBlock.call(plugin, node.attrs) }\n\n`);
|
2016-11-01 23:31:20 -07:00
|
|
|
};
|
|
|
|
});
|
|
|
|
return serializer;
|
|
|
|
}
|
|
|
|
|
2017-06-19 17:15:59 -04:00
|
|
|
const BLOCK_TAGS = {
|
|
|
|
p: 'paragraph',
|
|
|
|
li: 'list-item',
|
|
|
|
ul: 'bulleted-list',
|
|
|
|
ol: 'numbered-list',
|
|
|
|
blockquote: 'quote',
|
|
|
|
pre: 'code',
|
|
|
|
h1: 'heading-one',
|
|
|
|
h2: 'heading-two',
|
|
|
|
h3: 'heading-three',
|
|
|
|
h4: 'heading-four',
|
|
|
|
h5: 'heading-five',
|
2017-06-27 12:21:34 -04:00
|
|
|
h6: 'heading-six',
|
2017-06-19 17:15:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const MARK_TAGS = {
|
|
|
|
strong: 'bold',
|
|
|
|
em: 'italic',
|
|
|
|
u: 'underline',
|
|
|
|
s: 'strikethrough',
|
2017-06-27 12:21:34 -04:00
|
|
|
del: 'strikethrough',
|
2017-06-19 17:15:59 -04:00
|
|
|
code: 'code'
|
|
|
|
}
|
|
|
|
|
2017-06-21 16:53:54 -04:00
|
|
|
const BLOCK_COMPONENTS = {
|
2017-07-13 21:33:50 -04:00
|
|
|
'container': props => <div {...props.attributes}>{props.children}</div>,
|
|
|
|
'paragraph': props => <p {...props.attributes}>{props.children}</p>,
|
2017-06-21 16:53:54 -04:00
|
|
|
'list-item': props => <li {...props.attributes}>{props.children}</li>,
|
2017-07-18 19:14:40 -04:00
|
|
|
'numbered-list': props => {
|
|
|
|
const { data } = props.node;
|
|
|
|
const start = data.get('start') || 1;
|
|
|
|
return <ol {...props.attributes} start={start}>{props.children}</ol>;
|
|
|
|
},
|
2017-06-19 17:15:59 -04:00
|
|
|
'bulleted-list': props => <ul {...props.attributes}>{props.children}</ul>,
|
2017-06-21 16:53:54 -04:00
|
|
|
'quote': props => <blockquote {...props.attributes}>{props.children}</blockquote>,
|
2017-07-18 19:14:40 -04:00
|
|
|
'code': props => <pre><code {...props.attributes}>{props.children}</code></pre>,
|
2017-06-19 17:15:59 -04:00
|
|
|
'heading-one': props => <h1 {...props.attributes}>{props.children}</h1>,
|
|
|
|
'heading-two': props => <h2 {...props.attributes}>{props.children}</h2>,
|
|
|
|
'heading-three': props => <h3 {...props.attributes}>{props.children}</h3>,
|
|
|
|
'heading-four': props => <h4 {...props.attributes}>{props.children}</h4>,
|
|
|
|
'heading-five': props => <h5 {...props.attributes}>{props.children}</h5>,
|
|
|
|
'heading-six': props => <h6 {...props.attributes}>{props.children}</h6>,
|
2017-06-27 12:21:34 -04:00
|
|
|
'image': props => {
|
|
|
|
const data = props.node && props.node.get('data');
|
2017-07-13 21:33:50 -04:00
|
|
|
const src = data && data.get('src') || props.src;
|
|
|
|
const alt = data && data.get('alt') || props.alt;
|
2017-07-18 19:14:40 -04:00
|
|
|
const title = data && data.get('title') || props.title;
|
|
|
|
return <div><img src={src} alt={alt} title={title}{...props.attributes}/></div>;
|
2017-06-27 12:21:34 -04:00
|
|
|
},
|
2017-07-18 19:14:40 -04:00
|
|
|
'table': props => <table><tbody {...props.attributes}>{props.children}</tbody></table>,
|
|
|
|
'table-row': props => <tr {...props.attributes}>{props.children}</tr>,
|
|
|
|
'table-cell': props => <td {...props.attributes}>{props.children}</td>,
|
|
|
|
'thematic-break': props => <hr {...props.attributes}/>,
|
2017-06-19 17:15:59 -04:00
|
|
|
};
|
2017-07-13 21:33:50 -04:00
|
|
|
const getShortcodeId = props => {
|
|
|
|
if (props.node) {
|
|
|
|
const result = props.node.getIn(['data', 'shortcode', 'shortcodeId']);
|
|
|
|
return result || props.node.getIn(['data', 'shortcode']).shortcodeId;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const shortcodeStyles = {border: '2px solid black', padding: '8px', margin: '2px 0', cursor: 'pointer'};
|
2017-06-19 17:15:59 -04:00
|
|
|
|
2017-06-21 16:53:54 -04:00
|
|
|
const NODE_COMPONENTS = {
|
|
|
|
...BLOCK_COMPONENTS,
|
|
|
|
'link': props => {
|
2017-07-18 19:14:40 -04:00
|
|
|
const data = props.node.get('data');
|
|
|
|
const href = data && data.get('url') || props.href;
|
|
|
|
const title = data && data.get('title') || props.title;
|
|
|
|
return <a href={href} title={title} {...props.attributes}>{props.children}</a>;
|
2017-06-21 16:53:54 -04:00
|
|
|
},
|
2017-07-13 21:33:50 -04:00
|
|
|
'shortcode': props => {
|
|
|
|
const { attributes, node, state: editorState } = props;
|
2017-07-24 11:12:47 -04:00
|
|
|
const { data } = node;
|
2017-07-13 21:33:50 -04:00
|
|
|
const isSelected = editorState.selection.hasFocusIn(node);
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={cn(styles.shortcode, { [styles.shortcodeSelected]: isSelected })}
|
|
|
|
{...attributes}
|
|
|
|
draggable
|
|
|
|
>
|
2017-07-24 11:12:47 -04:00
|
|
|
{data.get('shortcode')}
|
2017-07-13 21:33:50 -04:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
2017-06-27 12:21:34 -04:00
|
|
|
};
|
2017-06-21 16:53:54 -04:00
|
|
|
|
2017-06-19 17:15:59 -04:00
|
|
|
const MARK_COMPONENTS = {
|
|
|
|
bold: props => <strong>{props.children}</strong>,
|
|
|
|
italic: props => <em>{props.children}</em>,
|
2017-06-21 16:53:54 -04:00
|
|
|
strikethrough: props => <s>{props.children}</s>,
|
|
|
|
code: props => <code>{props.children}</code>,
|
2017-06-19 17:15:59 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const RULES = [
|
2017-07-13 21:33:50 -04:00
|
|
|
{
|
|
|
|
deserialize(el, next) {
|
|
|
|
const shortcodeId = el.attribs && el.attribs['data-ncp'];
|
|
|
|
if (!shortcodeId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const plugin = registry.getEditorComponents().get(shortcodeId);
|
|
|
|
if (!plugin) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const shortcodeData = Map(el.attribs).reduce((acc, value, key) => {
|
|
|
|
if (key.startsWith('data-ncp-')) {
|
|
|
|
const dataKey = key.slice('data-ncp-'.length).toLowerCase();
|
|
|
|
if (dataKey) {
|
|
|
|
return acc.set(dataKey, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, Map({ shortcodeId }));
|
|
|
|
|
|
|
|
const result = {
|
|
|
|
kind: 'block',
|
|
|
|
isVoid: true,
|
|
|
|
type: 'shortcode',
|
|
|
|
data: { shortcode: shortcodeData },
|
|
|
|
};
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
serialize(entity, children) {
|
|
|
|
if (entity.type !== 'shortcode') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = Map(entity.data.get('shortcode'));
|
|
|
|
const shortcodeId = data.get('shortcodeId');
|
|
|
|
const plugin = registry.getEditorComponents().get(shortcodeId);
|
|
|
|
const dataAttrs = data.delete('shortcodeId').mapKeys(key => `data-ncp-${key}`).set('data-ncp', shortcodeId);
|
|
|
|
const preview = plugin.toPreview(data.toJS());
|
|
|
|
const component = typeof preview === 'string'
|
|
|
|
? <div { ...dataAttrs.toJS() } dangerouslySetInnerHTML={ { __html: preview } }></div>
|
|
|
|
: <div { ...dataAttrs.toJS() }>{preview}</div>;
|
|
|
|
return component;
|
|
|
|
},
|
|
|
|
},
|
2017-06-19 17:15:59 -04:00
|
|
|
{
|
|
|
|
deserialize(el, next) {
|
|
|
|
const block = BLOCK_TAGS[el.tagName]
|
|
|
|
if (!block) return
|
|
|
|
return {
|
|
|
|
kind: 'block',
|
|
|
|
type: block,
|
|
|
|
nodes: next(el.children)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
serialize(entity, children) {
|
2017-06-27 12:21:34 -04:00
|
|
|
if (['bulleted-list', 'numbered-list'].includes(entity.type)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-21 16:53:54 -04:00
|
|
|
const component = BLOCK_COMPONENTS[entity.type]
|
2017-06-19 17:15:59 -04:00
|
|
|
if (!component) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return component({ children });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
deserialize(el, next) {
|
|
|
|
const mark = MARK_TAGS[el.tagName]
|
|
|
|
if (!mark) return
|
|
|
|
return {
|
|
|
|
kind: 'mark',
|
|
|
|
type: mark,
|
|
|
|
nodes: next(el.children)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
serialize(entity, children) {
|
2017-07-12 18:51:28 -04:00
|
|
|
if (entity.kind !== 'mark') {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-19 17:15:59 -04:00
|
|
|
const component = MARK_COMPONENTS[entity.type]
|
|
|
|
return component({ children });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Special case for code blocks, which need to grab the nested children.
|
|
|
|
deserialize(el, next) {
|
|
|
|
if (el.tagName != 'pre') return
|
|
|
|
const code = el.children[0]
|
|
|
|
const children = code && code.tagName == 'code'
|
|
|
|
? code.children
|
|
|
|
: el.children
|
|
|
|
|
|
|
|
return {
|
|
|
|
kind: 'block',
|
|
|
|
type: 'code',
|
|
|
|
nodes: next(children)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2017-06-27 12:21:34 -04:00
|
|
|
{
|
|
|
|
deserialize(el, next) {
|
|
|
|
if (el.tagName != 'img') return
|
|
|
|
return {
|
2017-07-18 19:14:40 -04:00
|
|
|
kind: 'block',
|
2017-06-27 12:21:34 -04:00
|
|
|
type: 'image',
|
2017-07-05 15:21:12 -04:00
|
|
|
isVoid: true,
|
2017-06-27 12:21:34 -04:00
|
|
|
nodes: [],
|
|
|
|
data: {
|
|
|
|
src: el.attribs.src,
|
|
|
|
alt: el.attribs.alt,
|
2017-07-18 19:14:40 -04:00
|
|
|
title: el.attribs.title,
|
2017-06-27 12:21:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
serialize(entity, children) {
|
|
|
|
if (entity.type !== 'image') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const data = entity.get('data');
|
|
|
|
const props = {
|
|
|
|
src: data.get('src'),
|
|
|
|
alt: data.get('alt'),
|
2017-07-18 19:14:40 -04:00
|
|
|
title: data.get('title'),
|
2017-06-27 12:21:34 -04:00
|
|
|
};
|
2017-07-13 21:33:50 -04:00
|
|
|
const result = NODE_COMPONENTS.image(props);
|
|
|
|
return result;
|
2017-06-27 12:21:34 -04:00
|
|
|
}
|
|
|
|
},
|
2017-06-19 17:15:59 -04:00
|
|
|
{
|
|
|
|
// Special case for links, to grab their href.
|
|
|
|
deserialize(el, next) {
|
|
|
|
if (el.tagName != 'a') return
|
|
|
|
return {
|
|
|
|
kind: 'inline',
|
|
|
|
type: 'link',
|
|
|
|
nodes: next(el.children),
|
|
|
|
data: {
|
2017-07-18 19:14:40 -04:00
|
|
|
href: el.attribs.href,
|
|
|
|
title: el.attribs.title,
|
2017-06-19 17:15:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2017-06-21 16:53:54 -04:00
|
|
|
serialize(entity, children) {
|
|
|
|
if (entity.type !== 'link') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const data = entity.get('data');
|
|
|
|
const props = {
|
|
|
|
href: data.get('href'),
|
2017-07-18 19:14:40 -04:00
|
|
|
title: data.get('title'),
|
2017-06-21 16:53:54 -04:00
|
|
|
attributes: data.get('attributes'),
|
|
|
|
children,
|
|
|
|
};
|
|
|
|
return NODE_COMPONENTS.link(props);
|
|
|
|
}
|
2017-06-19 17:15:59 -04:00
|
|
|
},
|
2017-06-27 12:21:34 -04:00
|
|
|
{
|
|
|
|
serialize(entity, children) {
|
2017-07-09 00:17:10 -04:00
|
|
|
if (!['bulleted-list', 'numbered-list'].includes(entity.type)) {
|
2017-06-27 12:21:34 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return NODE_COMPONENTS[entity.type]({ children });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 17:15:59 -04:00
|
|
|
]
|
|
|
|
|
2017-07-18 19:14:40 -04:00
|
|
|
const htmlSerializer = new SlateHtml({ rules: RULES });
|
2017-06-19 17:15:59 -04:00
|
|
|
|
2017-07-11 18:56:32 -04:00
|
|
|
const SoftBreak = (options = {}) => ({
|
|
|
|
onKeyDown(e, data, state) {
|
|
|
|
if (data.key != 'enter') return;
|
|
|
|
if (options.shift && e.shiftKey == false) return;
|
|
|
|
|
|
|
|
const { onlyIn, ignoreIn, closeAfter, unwrapBlocks, defaultBlock = 'paragraph' } = options;
|
|
|
|
const { type, nodes } = state.startBlock;
|
|
|
|
if (onlyIn && !onlyIn.includes(type)) return;
|
|
|
|
if (ignoreIn && ignoreIn.includes(type)) return;
|
|
|
|
|
|
|
|
const shouldClose = nodes.last().characters.takeLast(closeAfter).every(c => c.text === '\n');
|
|
|
|
if (closeAfter && shouldClose) {
|
|
|
|
const trimmed = state.transform().deleteBackward(closeAfter);
|
|
|
|
const unwrapped = unwrapBlocks
|
|
|
|
? unwrapBlocks.reduce((acc, blockType) => acc.unwrapBlock(blockType), trimmed)
|
|
|
|
: trimmed;
|
|
|
|
return unwrapped.insertBlock(defaultBlock).apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
return state.transform().insertText('\n').apply();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-07-12 18:31:05 -04:00
|
|
|
const BackspaceCloseBlock = (options = {}) => ({
|
|
|
|
onKeyDown(e, data, state) {
|
2017-07-12 19:22:17 -04:00
|
|
|
if (data.key != 'backspace') return;
|
2017-07-12 18:31:05 -04:00
|
|
|
|
2017-07-12 19:22:17 -04:00
|
|
|
const { defaultBlock = 'paragraph', ignoreIn, onlyIn } = options;
|
2017-07-12 18:31:05 -04:00
|
|
|
const { startBlock } = state;
|
|
|
|
const { type } = startBlock;
|
|
|
|
|
2017-07-12 19:22:17 -04:00
|
|
|
if (onlyIn && !onlyIn.includes(type)) return;
|
|
|
|
if (ignoreIn && ignoreIn.includes(type)) return;
|
|
|
|
|
2017-07-12 18:31:05 -04:00
|
|
|
const characters = startBlock.getFirstText().characters;
|
|
|
|
const isEmpty = !characters || characters.isEmpty();
|
|
|
|
|
|
|
|
if (isEmpty) {
|
2017-07-12 19:22:17 -04:00
|
|
|
return state.transform().insertBlock(defaultBlock).focus().apply();
|
2017-07-12 18:31:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-07-11 18:56:32 -04:00
|
|
|
const slatePlugins = [
|
2017-07-24 13:36:14 -04:00
|
|
|
SoftBreak({ ignoreIn: ['paragraph', 'list-item', 'numbered-list', 'bulleted-list', 'table', 'table-row', 'table-cell'], closeAfter: 1 }),
|
2017-07-18 19:14:40 -04:00
|
|
|
BackspaceCloseBlock({ ignoreIn: ['paragraph', 'list-item', 'bulleted-list', 'numbered-list', 'table', 'table-row', 'table-cell'] }),
|
2017-07-12 19:22:17 -04:00
|
|
|
EditList({ types: ['bulleted-list', 'numbered-list'], typeItem: 'list-item' }),
|
2017-07-18 19:14:40 -04:00
|
|
|
EditTable({ typeTable: 'table', typeRow: 'table-row', typeCell: 'table-cell' }),
|
2017-07-11 18:56:32 -04:00
|
|
|
];
|
|
|
|
|
2016-11-01 16:55:21 -07:00
|
|
|
export default class Editor extends Component {
|
2016-08-11 11:27:09 -03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-11-01 23:31:20 -07:00
|
|
|
const plugins = registry.getEditorComponents();
|
2017-07-18 19:14:40 -04:00
|
|
|
const emptyRaw = {
|
|
|
|
nodes: [{ kind: 'block', type: 'paragraph', nodes: [
|
|
|
|
{ kind: 'text', ranges: [{ text: '' }] }
|
|
|
|
]}],
|
|
|
|
};
|
|
|
|
const remark = this.props.value && remarkToSlate(this.props.value);
|
|
|
|
const initialValue = get(remark, ['nodes', 'length']) ? remark : emptyRaw;
|
|
|
|
const editorState = SlateRaw.deserialize(initialValue, { terse: true });
|
2016-11-01 23:31:20 -07:00
|
|
|
this.state = {
|
2017-07-18 19:14:40 -04:00
|
|
|
editorState,
|
2017-06-19 17:15:59 -04:00
|
|
|
schema: {
|
|
|
|
nodes: NODE_COMPONENTS,
|
|
|
|
marks: MARK_COMPONENTS,
|
2017-07-25 21:45:33 -04:00
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
match: object => object.kind === 'document',
|
|
|
|
validate: doc => {
|
|
|
|
const hasBlocks = !doc.getBlocks().isEmpty();
|
|
|
|
return hasBlocks ? null : {};
|
|
|
|
},
|
|
|
|
normalize: transform => {
|
|
|
|
const block = SlateBlock.create({
|
|
|
|
type: 'paragraph',
|
|
|
|
nodes: [SlateText.createFromString('')],
|
|
|
|
});
|
|
|
|
const { key } = transform.state.document;
|
|
|
|
return transform.insertNodeByKey(key, 0, block).focus();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2017-06-19 17:15:59 -04:00
|
|
|
},
|
2016-11-01 23:31:20 -07:00
|
|
|
plugins,
|
|
|
|
};
|
2016-11-01 16:55:21 -07:00
|
|
|
}
|
2016-08-11 11:27:09 -03:00
|
|
|
|
2017-06-23 17:04:17 -04:00
|
|
|
handlePaste = (e, data, state) => {
|
|
|
|
if (data.type !== 'html' || data.isShift) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-29 17:56:20 -04:00
|
|
|
const markdown = htmlToMarkdown(data.html);
|
|
|
|
const html = markdownToHtml(markdown);
|
|
|
|
const fragment = serializer.deserialize(html).document;
|
2017-06-23 17:04:17 -04:00
|
|
|
return state.transform().insertFragment(fragment).apply();
|
|
|
|
}
|
|
|
|
|
2017-06-19 17:15:59 -04:00
|
|
|
handleDocumentChange = (doc, editorState) => {
|
2017-07-18 19:14:40 -04:00
|
|
|
const raw = SlateRaw.serialize(editorState, { terse: true });
|
|
|
|
const mdast = slateToRemark(raw);
|
|
|
|
this.props.onChange(mdast);
|
2017-06-19 17:15:59 -04:00
|
|
|
};
|
2017-07-23 19:38:05 +02:00
|
|
|
|
2017-06-19 17:15:59 -04:00
|
|
|
hasMark = type => this.state.editorState.marks.some(mark => mark.type === type);
|
|
|
|
hasBlock = type => this.state.editorState.blocks.some(node => node.type === type);
|
2017-07-23 19:38:05 +02:00
|
|
|
|
2017-06-19 17:15:59 -04:00
|
|
|
handleKeyDown = (e, data, state) => {
|
2017-07-26 17:11:23 -04:00
|
|
|
const createDefaultBlock = () => {
|
|
|
|
return SlateBlock.create({
|
|
|
|
type: 'paragraph',
|
|
|
|
nodes: [SlateText.createFromString('')]
|
|
|
|
});
|
2017-06-19 17:15:59 -04:00
|
|
|
};
|
2017-07-26 17:11:23 -04:00
|
|
|
if (data.key === 'enter') {
|
|
|
|
/**
|
|
|
|
* If a single void block is selected, and it's a direct descendant of the
|
|
|
|
* document (top level), a new paragraph should be added above or below it
|
|
|
|
* when 'Enter' is pressed, and the current selection should move to the
|
|
|
|
* new paragraph.
|
|
|
|
*
|
|
|
|
* If the selected block is the first block in the document, create the
|
|
|
|
* new block above it. If not, create the new block below it.
|
|
|
|
*/
|
|
|
|
const { document: doc, selection, anchorBlock, focusBlock } = state;
|
|
|
|
const focusBlockIndex = doc.nodes.indexOf(focusBlock);
|
|
|
|
const focusBlockIsTopLevel = focusBlockIndex > -1;
|
|
|
|
const focusBlockIsFirstChild = focusBlockIndex === 0;
|
|
|
|
const singleBlockSelected = anchorBlock === focusBlock;
|
|
|
|
|
|
|
|
if (focusBlock.isVoid && focusBlockIsTopLevel && singleBlockSelected) {
|
|
|
|
e.preventDefault();
|
|
|
|
const newBlock = createDefaultBlock();
|
|
|
|
const newBlockIndex = focusBlockIsFirstChild ? 0 : focusBlockIndex + 1;
|
|
|
|
return state.transform()
|
|
|
|
.insertNodeByKey(doc.key, newBlockIndex, newBlock)
|
|
|
|
.collapseToStartOf(newBlock)
|
|
|
|
.apply();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.isMod) {
|
2017-07-26 20:29:19 -04:00
|
|
|
|
|
|
|
if (data.key === 'y') {
|
|
|
|
e.preventDefault();
|
|
|
|
return state.transform().redo().focus().apply({ save: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.key === 'z') {
|
|
|
|
e.preventDefault();
|
|
|
|
return state.transform()[data.isShift ? 'redo' : 'undo']().focus().apply({ save: false });
|
|
|
|
}
|
|
|
|
|
2017-07-26 17:11:23 -04:00
|
|
|
const marks = {
|
|
|
|
b: 'bold',
|
|
|
|
i: 'italic',
|
|
|
|
u: 'underlined',
|
|
|
|
s: 'strikethrough',
|
|
|
|
'`': 'code',
|
|
|
|
};
|
2017-07-23 19:38:05 +02:00
|
|
|
|
2017-07-26 17:11:23 -04:00
|
|
|
const mark = marks[data.key];
|
2016-08-11 11:27:09 -03:00
|
|
|
|
2017-07-26 17:11:23 -04:00
|
|
|
if (mark) {
|
|
|
|
e.preventDefault();
|
|
|
|
return state.transform().toggleMark(mark).apply();
|
|
|
|
}
|
2016-08-11 11:27:09 -03:00
|
|
|
}
|
2016-10-03 14:25:27 +02:00
|
|
|
};
|
2016-08-11 11:27:09 -03:00
|
|
|
|
2017-06-19 17:15:59 -04:00
|
|
|
handleMarkClick = (event, type) => {
|
|
|
|
event.preventDefault();
|
2017-06-21 17:08:53 -04:00
|
|
|
const resolvedState = this.state.editorState.transform().focus().toggleMark(type).apply();
|
2017-06-19 17:15:59 -04:00
|
|
|
this.ref.onChange(resolvedState);
|
|
|
|
this.setState({ editorState: resolvedState });
|
2016-10-03 14:25:27 +02:00
|
|
|
};
|
2016-08-11 11:27:09 -03:00
|
|
|
|
2017-06-19 17:15:59 -04:00
|
|
|
handleBlockClick = (event, type) => {
|
|
|
|
event.preventDefault();
|
|
|
|
let { editorState } = this.state;
|
2017-07-26 20:06:53 -04:00
|
|
|
const transform = editorState.transform();
|
2017-06-19 17:15:59 -04:00
|
|
|
const doc = editorState.document;
|
|
|
|
const isList = this.hasBlock('list-item')
|
|
|
|
|
|
|
|
// Handle everything except list buttons.
|
|
|
|
if (!['bulleted-list', 'numbered-list'].includes(type)) {
|
|
|
|
const isActive = this.hasBlock(type);
|
|
|
|
const transformed = transform.setBlock(isActive ? DEFAULT_NODE : type);
|
|
|
|
|
|
|
|
if (isList) {
|
|
|
|
transformed
|
|
|
|
.unwrapBlock('bulleted-list')
|
|
|
|
.unwrapBlock('numbered-list');
|
2017-03-18 14:13:22 -07:00
|
|
|
}
|
2017-06-19 17:15:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the extra wrapping required for list buttons.
|
|
|
|
else {
|
|
|
|
const isType = editorState.blocks.some(block => {
|
|
|
|
return !!doc.getClosest(block.key, parent => parent.type === type);
|
|
|
|
});
|
2017-03-02 12:02:55 -08:00
|
|
|
|
2017-06-19 17:15:59 -04:00
|
|
|
if (isList && isType) {
|
|
|
|
transform
|
|
|
|
.setBlock(DEFAULT_NODE)
|
|
|
|
.unwrapBlock('bulleted-list')
|
|
|
|
.unwrapBlock('numbered-list');
|
|
|
|
} else if (isList) {
|
|
|
|
transform
|
|
|
|
.unwrapBlock(type === 'bulleted-list' ? 'numbered-list' : 'bulleted-list')
|
|
|
|
.wrapBlock(type);
|
|
|
|
} else {
|
|
|
|
transform
|
|
|
|
.setBlock('list-item')
|
|
|
|
.wrapBlock(type);
|
|
|
|
}
|
2017-05-22 14:34:49 -04:00
|
|
|
}
|
2016-08-11 11:27:09 -03:00
|
|
|
|
2017-07-26 20:06:53 -04:00
|
|
|
const resolvedState = transform.focus().apply();
|
2017-06-19 17:15:59 -04:00
|
|
|
this.ref.onChange(resolvedState);
|
|
|
|
this.setState({ editorState: resolvedState });
|
2016-10-03 14:25:27 +02:00
|
|
|
};
|
2016-08-11 11:27:09 -03:00
|
|
|
|
|
|
|
|
2016-11-01 17:58:19 -07:00
|
|
|
handleLink = () => {
|
|
|
|
let url = null;
|
2016-11-01 23:31:20 -07:00
|
|
|
if (!markActive(this.view.state, this.state.schema.marks.link)) {
|
2017-03-16 20:45:46 -04:00
|
|
|
url = prompt('Link URL:'); // eslint-disable-line no-alert
|
2016-11-01 17:58:19 -07:00
|
|
|
}
|
2017-05-22 14:34:49 -04:00
|
|
|
const command = toggleMark(this.state.schema.marks.link, { href: url ? processUrl(url) : null });
|
2016-11-01 17:58:19 -07:00
|
|
|
command(this.view.state, this.handleAction);
|
|
|
|
};
|
|
|
|
|
2017-07-25 21:45:33 -04:00
|
|
|
handlePluginSubmit = (plugin, shortcodeData) => {
|
2017-07-13 21:33:50 -04:00
|
|
|
const { editorState } = this.state;
|
2017-07-25 21:45:33 -04:00
|
|
|
const data = {
|
|
|
|
shortcode: plugin.id,
|
|
|
|
shortcodeValue: plugin.toBlock(shortcodeData.toJS()),
|
|
|
|
shortcodeData,
|
|
|
|
};
|
|
|
|
const nodes = [SlateText.createFromString('')];
|
|
|
|
const block = { kind: 'block', type: 'shortcode', data, isVoid: true, nodes };
|
2017-07-13 21:33:50 -04:00
|
|
|
const resolvedState = editorState.transform().insertBlock(block).apply();
|
|
|
|
this.ref.onChange(resolvedState);
|
|
|
|
this.setState({ editorState: resolvedState });
|
2016-12-27 23:18:37 -08:00
|
|
|
};
|
|
|
|
|
2016-11-01 16:55:21 -07:00
|
|
|
handleToggle = () => {
|
2017-05-22 14:04:24 -04:00
|
|
|
this.props.onMode('raw');
|
2016-10-03 14:25:27 +02:00
|
|
|
};
|
2016-08-11 11:27:09 -03:00
|
|
|
|
2017-06-19 17:15:59 -04:00
|
|
|
getButtonProps = (type, isBlock) => {
|
|
|
|
const handler = isBlock ? this.handleBlockClick: this.handleMarkClick;
|
|
|
|
const isActive = isBlock ? this.hasBlock : this.hasMark;
|
|
|
|
return { onAction: e => handler(e, type), active: isActive(type) };
|
|
|
|
};
|
|
|
|
|
2016-08-11 11:27:09 -03:00
|
|
|
render() {
|
2017-01-10 22:23:22 -02:00
|
|
|
const { onAddAsset, onRemoveAsset, getAsset } = this.props;
|
2017-03-16 20:45:46 -04:00
|
|
|
const { plugins, selectionPosition, dragging } = this.state;
|
2016-11-01 16:55:21 -07:00
|
|
|
|
2017-07-13 21:33:50 -04:00
|
|
|
return (
|
|
|
|
<div className={styles.editor}>
|
|
|
|
<Sticky
|
|
|
|
className={styles.editorControlBar}
|
|
|
|
classNameActive={styles.editorControlBarSticky}
|
|
|
|
fillContainerWidth
|
|
|
|
>
|
|
|
|
<Toolbar
|
|
|
|
selectionPosition={selectionPosition}
|
|
|
|
buttons={{
|
|
|
|
bold: this.getButtonProps('bold'),
|
|
|
|
italic: this.getButtonProps('italic'),
|
|
|
|
code: this.getButtonProps('code'),
|
|
|
|
link: this.getButtonProps('link'),
|
|
|
|
h1: this.getButtonProps('heading-one', true),
|
|
|
|
h2: this.getButtonProps('heading-two', true),
|
|
|
|
list: this.getButtonProps('bulleted-list', true),
|
|
|
|
listNumbered: this.getButtonProps('numbered-list', true),
|
|
|
|
codeBlock: this.getButtonProps('code', true),
|
|
|
|
}}
|
|
|
|
onToggleMode={this.handleToggle}
|
|
|
|
plugins={plugins}
|
|
|
|
onSubmit={this.handlePluginSubmit}
|
|
|
|
onAddAsset={onAddAsset}
|
|
|
|
onRemoveAsset={onRemoveAsset}
|
|
|
|
getAsset={getAsset}
|
|
|
|
/>
|
|
|
|
</Sticky>
|
|
|
|
<SlateEditor
|
|
|
|
className={styles.slateEditor}
|
|
|
|
state={this.state.editorState}
|
|
|
|
schema={this.state.schema}
|
|
|
|
plugins={slatePlugins}
|
|
|
|
onChange={editorState => this.setState({ editorState })}
|
|
|
|
onDocumentChange={this.handleDocumentChange}
|
2017-07-26 17:11:23 -04:00
|
|
|
onKeyDown={this.handleKeyDown}
|
2017-07-13 21:33:50 -04:00
|
|
|
onPaste={this.handlePaste}
|
|
|
|
ref={ref => this.ref = ref}
|
|
|
|
spellCheck
|
2017-05-22 14:34:49 -04:00
|
|
|
/>
|
2017-07-13 21:33:50 -04:00
|
|
|
</div>
|
|
|
|
);
|
2016-08-11 11:27:09 -03:00
|
|
|
}
|
|
|
|
}
|
2016-11-04 11:04:54 -07:00
|
|
|
|
|
|
|
Editor.propTypes = {
|
2017-01-10 22:23:22 -02:00
|
|
|
onAddAsset: PropTypes.func.isRequired,
|
|
|
|
onRemoveAsset: PropTypes.func.isRequired,
|
|
|
|
getAsset: PropTypes.func.isRequired,
|
2016-11-04 11:04:54 -07:00
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
onMode: PropTypes.func.isRequired,
|
2017-07-18 19:14:40 -04:00
|
|
|
value: PropTypes.object,
|
2016-11-04 11:04:54 -07:00
|
|
|
};
|