Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 158 159 160 161 162 163 164 165 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | // @flow
import * as React from 'react';
import type {IconProps} from 'shared/src/types';
import {Modal, Button, Form, Input} from 'antd';
import ToolbarIcon from '@canner/slate-icon-shared';
import links from '@canner/slate-helper-inline-links';
import {haveInlines} from '@canner/slate-util-have';
import {LINK} from '@canner/slate-constant/lib/inlines';
import linkNode from '@canner/slate-editor-renderer/lib/linkNode';
export const LinkPlugin = (opt) => {
const options = Object.assign({
type: LINK,
getHref: (node) => node.data.get('href')
}, opt);
return {
renderNode: (props) => {
if (props.node.type === options.type)
return linkNode(options)(props);
}
}
}
const FormItem = Form.Item;
type State = {
showModal: boolean,
addLinkText: boolean
}
type Props = IconProps & {
form: any
}
@Form.create()
export default class Link extends React.Component<Props, State> {
typeName: string
constructor(props: Props) {
super(props);
this.state = {
showModal: false,
addLinkText: false
};
this.typeName = this.props.type || LINK;
}
static defaultProps = {
hrefKey: 'href',
textKey: 'text'
}
onClick = (e: Event) => {
let {change, onChange} = this.props;
let haveLinks = haveInlines(change, this.typeName);
e.preventDefault();
if (haveLinks) {
onChange(links(change, this.typeName));
} else if (change.value.isExpanded) {
// prompt for ask url
this.setState({
showModal: true,
addLinkText: false
});
} else {
// prompt for url and text
this.setState({
showModal: true,
addLinkText: true
});
}
}
handleCancel = () => {
this.props.form.resetFields();
this.setState({
showModal: false,
addLinkText: false
});
}
handleOk = (e: Event) => {
e.preventDefault();
const {onChange, change, hrefKey, textKey} = this.props;
const that = this;
this.props.form.validateFields((err, values) => {
if (!err) {
const href = values.href;
const text = values.text;
if (href && text) {
onChange(links(change, this.typeName, {[hrefKey]: href, [textKey]: text}));
} else if (href) {
onChange(links(change, this.typeName, {[hrefKey]: href}));
}
that.handleCancel();
}
});
}
render() {
const {getFieldDecorator} = this.props.form;
const {change, icon, ...rest} = this.props;
const {addLinkText, showModal} = this.state;
const onClick = e => this.onClick(e);
return (
<div style={{display: 'inline-block'}}>
<ToolbarIcon
type={this.typeName}
icon={icon || 'Link'}
onClick={onClick}
isActive={haveInlines(change, this.typeName)}
{...rest}
/>
<Modal
visible={showModal}
title="Add Link"
onCancel={this.handleCancel}
footer={[
<Button key="back" type="ghost" size="large" onClick={this.handleCancel}>
Cancel
</Button>,
<Button key="submit" type="primary" size="large" onClick={this.handleOk}>
Ok
</Button>
]}
>
<Form horizontal="true">
<FormItem
label="Url"
hasFeedback
>
{getFieldDecorator('href', {
rules: [{
type: 'url', message: 'The input is not valid url!'
}, {
required: true, message: 'Please input your url'
}]
})(
<Input onClick={e => e.preventDefault()}/>
)}
</FormItem>
{
addLinkText ? (
<FormItem
label="Text"
hasFeedback
>
{getFieldDecorator('text')(
<Input onClick={e => e.preventDefault()}/>
)}
</FormItem>
) : null
}
</Form>
</Modal>
</div>
);
}
}
|