-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
mention.js
234 lines (221 loc) · 7.17 KB
/
mention.js
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
166
167
168
169
170
171
172
173
174
175
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module mention/mention
*/
import { Plugin } from 'ckeditor5/src/core';
import MentionEditing, { _toMentionAttribute } from './mentionediting';
import MentionUI from './mentionui';
import '../theme/mention.css';
/**
* The mention plugin.
*
* For a detailed overview, check the {@glink features/mentions Mention feature documentation}.
*
* @extends module:core/plugin~Plugin
*/
export default class Mention extends Plugin {
/**
* Creates a mention attribute value from the provided view element and optional data.
*
* editor.plugins.get( 'Mention' ).toMentionAttribute( viewElement, { userId: '1234' } );
*
* // For a view element: <span data-mention="@joe">@John Doe</span>
* // it will return:
* // { id: '@joe', userId: '1234', uid: '7a7bc7...', _text: '@John Doe' }
*
* @param {module:engine/view/element~Element} viewElement
* @param {String|Object} [data] Additional data to be stored in the mention attribute.
* @returns {module:mention/mention~MentionAttribute}
*/
toMentionAttribute( viewElement, data ) {
return _toMentionAttribute( viewElement, data );
}
/**
* @inheritDoc
*/
static get pluginName() {
return 'Mention';
}
/**
* @inheritDoc
*/
static get requires() {
return [ MentionEditing, MentionUI ];
}
}
/**
* The configuration of the {@link module:mention/mention~Mention} feature.
*
* Read more in {@link module:mention/mention~MentionConfig}.
*
* @member {module:mention/mention~MentionConfig} module:core/editor/editorconfig~EditorConfig#mention
* @type {Array.<module/mention~MentionFeed>}
*/
/**
* The configuration of the mention feature.
*
* Read more about {@glink features/mentions#configuration configuring the mention feature}.
*
* ClassicEditor
* .create( editorElement, {
* mention: ... // Mention feature options.
* } )
* .then( ... )
* .catch( ... );
*
* See {@link module:core/editor/editorconfig~EditorConfig all editor options}.
*
* @interface MentionConfig
*/
/**
* The list of mention feeds supported by the editor.
*
* ClassicEditor
* .create( editorElement, {
* plugins: [ Mention, ... ],
* mention: {
* feeds: [
* {
* marker: '@',
* feed: [ '@Barney', '@Lily', '@Marshall', '@Robin', '@Ted' ]
* },
* ...
* ]
* }
* } )
* .then( ... )
* .catch( ... );
*
* You can provide many mention feeds but they must use different `marker`s.
* For example, you can use `'@'` to autocomplete people and `'#'` to autocomplete tags.
*
* @member {Array.<module:mention/mention~MentionFeed>} module:mention/mention~MentionConfig#feeds
*/
/**
* The mention feed descriptor. Used in {@link module:mention/mention~MentionConfig `config.mention`}.
*
* See {@link module:mention/mention~MentionConfig} to learn more.
*
* // Static configuration.
* const mentionFeedPeople = {
* marker: '@',
* feed: [ '@Alice', '@Bob', ... ],
* minimumCharacters: 2
* };
*
* // Simple synchronous callback.
* const mentionFeedTags = {
* marker: '#',
* feed: searchString => {
* return tags
* // Filter the tags list.
* .filter( tag => {
* return tag.toLowerCase().includes( queryText.toLowerCase() );
* } )
* // Return 10 items max - needed for generic queries when the list may contain hundreds of elements.
* .slice( 0, 10 );
* }
* };
*
* const tags = [ 'wysiwyg', 'rte', 'rich-text-edior', 'collaboration', 'real-time', ... ];
*
* // Asynchronous callback.
* const mentionFeedPlaceholders = {
* marker: '$',
* feed: searchString => {
* return getMatchingPlaceholders( searchString );
* }
* };
*
* function getMatchingPlaceholders( searchString ) {
* return new Promise( resolve => {
* doSomeXHRQuery( result => {
* // console.log( result );
* // -> [ '$name', '$surname', '$postal', ... ]
*
* resolve( result );
* } );
* } );
* }
*
* @typedef {Object} module:mention/mention~MentionFeed
* @property {String} [marker] The character which triggers autocompletion for mention. It must be a single character.
* @property {Array.<module:mention/mention~MentionFeedItem>|Function} feed Autocomplete items. Provide an array for
* a static configuration (the mention feature will show matching items automatically) or a function which returns an array of
* matching items (directly, or via a promise). If a function is passed, it is executed in the context of the editor instance.
* @property {Number} [minimumCharacters=0] Specifies after how many characters the autocomplete panel should be shown.
* @property {Function} [itemRenderer] A function that renders a {@link module:mention/mention~MentionFeedItem}
* to the autocomplete panel.
*/
/**
* The mention feed item. It may be defined as a string or a plain object.
*
* When defining a feed item as a plain object, the `id` property is obligatory. Additional properties
* can be used when customizing the mention feature bahavior
* (see {@glink features/mentions#customizing-the-autocomplete-list "Customizing the autocomplete list"}
* and {@glink features/mentions#customizing-the-output "Customizing the output"} sections).
*
* ClassicEditor
* .create( editorElement, {
* plugins: [ Mention, ... ],
* mention: {
* feeds: [
* // Feed items as objects.
* {
* marker: '@',
* feed: [
* {
* id: '@Barney',
* fullName: 'Barney Bloom'
* },
* {
* id: '@Lily',
* fullName: 'Lily Smith'
* },
* {
* id: '@Marshall',
* fullName: 'Marshall McDonald'
* },
* {
* id: '@Robin',
* fullName: 'Robin Hood'
* },
* {
* id: '@Ted',
* fullName: 'Ted Cruze'
* },
* // ...
* ]
* },
*
* // Feed items as plain strings.
* {
* marker: '#',
* feed: [ 'wysiwyg', 'rte', 'rich-text-edior', 'collaboration', 'real-time', ... ]
* },
* ]
* }
* } )
* .then( ... )
* .catch( ... );
*
* @typedef {Object|String} module:mention/mention~MentionFeedItem
* @property {String} id A unique ID of the mention. It must start with the marker character.
* @property {String} [text] Text inserted into the editor when creating a mention.
*/
/**
* Represents a mention in the model.
*
* See {@link module:mention/mention~Mention#toMentionAttribute `Mention#toMentionAttribute()`}.
*
* @interface module:mention/mention~MentionAttribute
* @property {String} id The ID of a mention. It identifies the mention item in the mention feed. There can be multiple mentions
* in the document with the same ID (e.g. the same hashtag being mentioned).
* @property {String} uid A unique ID of this mention instance. Should be passed as an `option.id` when using
* {@link module:engine/view/downcastwriter~DowncastWriter#createAttributeElement writer.createAttributeElement()}.
* @property {String} _text Helper property that stores the text of the inserted mention. Used for detecting a broken mention
* in the editing area.
*/