QEditor.jsx 9.83 KB
Newer Older
Рамис's avatar
Рамис committed
1
2
3
4
import React, { Fragment } from 'react'
import ReactQuill, { Quill } from 'react-quill';
import './index.scss';
import 'react-quill/dist/quill.snow.css';
Рамис's avatar
Рамис committed
5
import ImageBlot from "./blots/ImageBlot";
Рамис's avatar
Рамис committed
6
7
8
9
10
import VideoBlot from "./blots/VideoBlot";
import VideoLocalBlot from "./blots/VideoLocalBlot";
import EditorModal from "./components/EditorModal";
import Uploader from "./components/Uploader";
import "katex/dist/katex.min.css";
Рамис's avatar
Рамис committed
11
12
import ImageResize from "quill-image-resize-module-react";
import BlotFormatter from 'quill-blot-formatter';
Рамис's avatar
Рамис committed
13
14
import katex from "katex";

Рамис's avatar
Рамис committed
15
16
17
18
19

Quill.register('modules/blotFormatter', BlotFormatter);



Рамис's avatar
Рамис committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
export default class QEditor extends React.Component {
    constructor (props) {
        super(props)
        this.state = {
            init: false,
            modalIsOpen: false,
            innerModalType: null,
            quillRef: null,
            uploadedPaths: [],
            uploaderUid: 'uid' + new Date(),
            embedContent: ''
        }

        Quill.register('formats/video', VideoBlot)
        Quill.register('formats/videoLocal', VideoLocalBlot)
Рамис's avatar
Рамис committed
35
36
        // Quill.register('modules/imageResize', ImageResize)
        Quill.register('formats/image', ImageBlot)
Рамис's avatar
fix bug    
Рамис committed
37

Рамис's avatar
Рамис committed
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
    }

    componentDidMount () {
        this.setState({
            init: true
        })
    }

    modules = {
        toolbar: {
            container: [
                [{header: [2, 3, 4, false]}],
                ['bold', 'italic', 'underline', 'strike', 'blockquote'],
                [
                    {list: 'ordered'},
                    {list: 'bullet'},
                    {indent: '-1'},
                    {indent: '+1'}
                ],
                [{align: []}, {color: []}, {background: []}],
                ['link', 'image', 'video', 'videoLocal'],
                ['formula'],
                ['clean']
            ],

            handlers: {
                video: (a) => {
                    this.setState(
                        {
                            innerModalType: 'video'
                        },
                        () => this.editorModal.show({title: 'Видео по ссылке'})
                    )
                },
                videoLocal: () => {
                    this.setState({
                        innerModalType: 'videoLocal'
                    }, () => this.editorModal.show({title: 'Загрузить видео'}));
                },
Рамис's avatar
Рамис committed
77
78
79
80
                image: (a) => {

                    console.log(a);

Рамис's avatar
Рамис committed
81
82
83
84
85
86
                    this.setState({
                        innerModalType: 'image'
                    }, () => this.editorModal.show({title: 'Загрузить изображение'}));
                }
            }
        },
Рамис's avatar
Рамис committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
        // imageResize: {
        //     parchment: Quill.import("parchment"),
        //     modules: ["Resize", "DisplaySize"]
        // },
        blotFormatter: {}
        /* clipboard: {
            matchers: [
                ['IMG', (node, delta)=>{

                    console.log(delta)



                    return delta.compose(new Delta().retain(delta.length(), { width: 100, title: 'ekwgon' }));
                }]
            ]
        } */
Рамис's avatar
Рамис committed
104
105
106
107
108
109
110
111
    }

    formats = [
        'header',
        'bold', 'italic', 'underline', 'strike', 'blockquote',
        'list', 'bullet', 'indent',
        'align',
        'link', 'image', 'video', 'color', 'background', 'videoLocal',
Рамис's avatar
Рамис committed
112
        'formula'
Рамис's avatar
Рамис committed
113
114
115
116
117
118
119
120
121
122
123
    ]

    buildActionsModal (buttons = []) {
        if (buttons.length === 0) {
            return null;
        }

        return (
            <div className={'atma-editor-modal-action'}>
                {
                    buttons.map((btn, i) => (
Рамис's avatar
Рамис committed
124
                        <button disabled={ btn.disabled } type={'button'} key={'mAction' + i} className={'atma-editor-btn' + btn.className}
Рамис's avatar
Рамис committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
                                onClick={btn.onClick}>{btn.title}</button>
                    ))
                }
            </div>
        )
    }

    getInnerModal () {
        switch (this.state.innerModalType) {
            case 'video':
                return (
                    <Fragment>
                        <input type="text" value={this.state.embedContent} placeholder={'https://'} onInput={(e) => {
                            this.setState({
                                embedContent: e.target.value
                            })
                        }}/>
                        <ul className={'atma-editor-soc-video'}>
                            <li className={'youtube'}/>
                            <li className={'vimeo'}/>
Рамис's avatar
Рамис committed
145
                            {/* <li className={'vk'}/> */}
Рамис's avatar
Рамис committed
146
147
148
149
150
151
152
153
154
155
                            <li className={'ok'}/>
                        </ul>
                    </Fragment>
                )
            case 'videoLocal':
                return (
                    <Fragment>
                        <Uploader
                            key={this.state.uploaderUid}
                            accept={'video/*'}
Рамис's avatar
Рамис committed
156
157
                            action={ this.props.uploadOptions.url }
                            errorMessage={ this.props.uploadOptions.errorMessage }
Рамис's avatar
Рамис committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
                            onSuccess={(file) => {
                                let uploadedPaths = this.state.uploadedPaths;

                                uploadedPaths.push(file);
                                this.setState({uploadedPaths});
                            }}
                            multiple={true}
                        />
                    </Fragment>
                )
            case 'image':
                return (
                    <Fragment>
                        <Uploader
                            key={this.state.uploaderUid}
                            accept={'image/*'}
Рамис's avatar
Рамис committed
174
175
                            action={ this.props.uploadOptions.url }
                            errorMessage={ this.props.uploadOptions.errorMessage }
Рамис's avatar
Рамис committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
                            onSuccess={(file) => {
                                let uploadedPaths = this.state.uploadedPaths;

                                uploadedPaths.push(file);
                                this.setState({uploadedPaths});
                            }}
                            multiple={true}
                        />
                    </Fragment>
                )
            default:
                return <div>Пусто</div>
        }
    }

    quillInsert () {
        const quill = this.quillRef.getEditor();
        let range = quill.getSelection();
        let index = range ? range.index : 0;

        switch (this.state.innerModalType) {

            case 'image':
            case 'videoLocal':
                this.state.uploadedPaths.map((file, i) => {
                    quill.insertEmbed(index, this.state.innerModalType, file.path, Quill.sources.USER);
                    quill.setSelection(index + 1);
Рамис's avatar
Рамис committed
203
                    quill.blur();
Рамис's avatar
Рамис committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
                });
                break

            default:
                quill.insertEmbed(index, this.state.innerModalType, this.state.embedContent, Quill.sources.USER);
                quill.setSelection(index + 1);
        }

        this.setState({
            uploaderUid: `uid${new Date()}`,
            embedContent: ''
        }, () => this.editorModal.hide());
    }

    render () {
        let {init, innerModalType, modalIsOpen} = this.state;
        let {maxWidth, maxHeight, value} = this.props;

        window.katex = katex;

Рамис's avatar
Рамис committed
224
        if ( !this.state.init) {
Рамис's avatar
Рамис committed
225
226
227
228
229
            return null;
        }
        return (
            <Fragment>
                <div className="atma-editor-wrap" style={{maxWidth}}>
Рамис's avatar
Рамис committed
230
                    <div className={'atma-editor'} data-lang={ this.props.lang ? this.props.lang : 'ru' }>
Рамис's avatar
Рамис committed
231
232
233
234
                        <ReactQuill
                            ref={ref => this.quillRef = ref}
                            defaultValue={value}
                            theme="snow"
Рамис's avatar
Рамис committed
235
236
237
                            onChange={value => {
                                this.props.onChange(value)
                            }}
Рамис's avatar
Рамис committed
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
                            modules={this.modules}
                            formats={this.formats}
                            onChangeSelection={(range, source, editor) => {
                                // console.log(range, source, editor.getText());
                            }}
                        />
                    </div>
                    <EditorModal
                        isOpen={modalIsOpen}
                        ref={ref => this.editorModal = ref}
                    >
                        {this.getInnerModal()}
                        {this.buildActionsModal([
                            {
                                title: 'Отмена',
                                className: ' atma-editor-cancel',
                                onClick: () => {
                                    this.setState(
                                        {
                                            uploaderUid: `uid${new Date()}`
                                        },
                                        () => this.editorModal.hide()
                                    )
                                }
                            },
                            {
                                title: 'Вставить',
                                className: ' atma-editor-complete',
Рамис's avatar
Рамис committed
266
267
                                onClick: () => this.quillInsert(),
                                disabled: (this.props.uploadOptions.url === null && (innerModalType === 'videoLocal' || innerModalType === 'image'))
Рамис's avatar
Рамис committed
268
269
270
271
272
273
274
275
276
277
278
279
                            }
                        ])
                        }
                    </EditorModal>
                </div>
            </Fragment>
        )
    }
}

QEditor.defaultProps = {
    maxWidth: 'auto',
Рамис's avatar
Рамис committed
280
281
    maxHeight: 1000,
    onChange: ()=>{},
Рамис's avatar
Рамис committed
282
283
284
    lang: 'ru',
    uploadOptions: {
        url: null,
Рамис's avatar
Рамис committed
285
        errorMessage: 'Загрузка временно невозможна'
Рамис's avatar
Рамис committed
286
    }
Рамис's avatar
Рамис committed
287
}