-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
EditorCK.tsx
144 lines (133 loc) · 4.05 KB
/
EditorCK.tsx
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
import { getEnv } from 'apolloClient';
import CKEditor from 'ckeditor4-react';
import { colors } from 'modules/common/styles';
import React from 'react';
import { IEditorProps } from '../types';
const { REACT_APP_API_URL } = getEnv();
export const getMentionedUserIds = (content: string) => {
const re = new RegExp('mentioned-user-id="(?<name>.+?)"', 'g');
const mentionedUserIds: string[] = (content.match(re) || []).map(m =>
m.replace(re, '$1')
);
return mentionedUserIds.filter((value, index, self) => {
return self.indexOf(value) === index;
});
};
class EditorCK extends React.Component<IEditorProps> {
constructor(props: IEditorProps) {
super(props);
CKEditor.editorUrl = '/ckeditor/ckeditor.js';
}
render() {
const {
onCtrlEnter,
content,
onChange,
height,
insertItems,
removeButtons,
removePlugins,
toolbar,
toolbarCanCollapse,
mentionUsers = [],
autoFocus
} = this.props;
const mentionDataFeed = (opts, callback) => {
if (mentionUsers.length <= 1) {
return;
}
const matchProperty = 'fullName';
const query = opts.query.toLowerCase();
const data = mentionUsers.filter(
item => item[matchProperty].toLowerCase().indexOf(query) >= 0
);
callback(data);
};
return (
<CKEditor
data={content}
onChange={onChange}
config={{
height,
startupFocus: autoFocus,
uiColor: colors.bgLight,
dialog_backgroundCoverColor: '#30435C',
allowedContent: true,
extraPlugins: 'codemirror,strinsert,onCtrlEnter',
strinsert: insertItems,
autoGrowOnStartup: true,
toolbar: toolbar || [
{
name: 'document',
groups: ['mode', 'document', 'doctools'],
items: ['Source', 'NewPage', 'Preview']
},
{
name: 'insert',
items: [
'Image',
'Table',
'HorizontalRule',
'EmojiPanel',
'SpecialChar'
]
},
{
name: 'paragraph',
groups: ['list', 'indent', 'blocks', 'align', 'bidi'],
items: [
'NumberedList',
'BulletedList',
'Outdent',
'Indent',
'Blockquote',
'CreateDiv',
'JustifyLeft',
'JustifyCenter',
'JustifyRight',
'JustifyBlock'
]
},
{
name: 'basicstyles',
groups: ['basicstyles', 'cleanup'],
items: ['Bold', 'Italic', 'Underline', 'Strike', 'RemoveFormat']
},
{ name: 'links', items: ['Link', 'Unlink', 'Anchor'] },
{ name: 'styles', items: ['Styles', 'Format', 'Font', 'FontSize'] },
{ name: 'others', items: [insertItems ? 'strinsert' : '-'] },
{ name: 'colors', items: ['TextColor', 'BGColor'] },
{ name: 'tools', items: ['Maximize'] }
],
mentions: [
{
feed: mentionDataFeed,
itemTemplate:
'<li data-id="{id}">' +
'<img class="editor-avatar" src="{avatar}"' +
'<strong>{fullName}</strong>' +
'</li>',
outputTemplate:
'<a mentioned-user-id="{id}">@{fullName}</a><span> </span>',
minChars: 0
}
],
removeButtons,
removePlugins,
codemirror: {
enableCodeFormatting: false,
enableCodeFolding: false,
showSearchButton: false,
showCommentButton: false,
showUncommentButton: false,
showFormatButton: false
},
onCtrlEnter,
toolbarCanCollapse,
filebrowserImageUploadUrl: `${REACT_APP_API_URL}/upload-file`
}}
/>
);
}
}
export default EditorCK;