@ckeditor/ckeditor5-mention 35.3.2 → 36.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE.md +1 -1
- package/build/mention.js +2 -2
- package/package.json +26 -22
- package/src/index.js +1 -3
- package/src/mention.js +30 -280
- package/src/mentioncommand.js +112 -129
- package/src/mentionediting.js +196 -256
- package/src/mentionui.js +581 -814
- package/src/ui/domwrapperview.js +37 -64
- package/src/ui/mentionlistitemview.js +9 -15
- package/src/ui/mentionsview.js +90 -109
- package/theme/mention.css +1 -1
- package/theme/mentionui.css +1 -1
- package/build/mention.js.map +0 -1
package/src/mentioncommand.js
CHANGED
@@ -1,17 +1,13 @@
|
|
1
1
|
/**
|
2
|
-
* @license Copyright (c) 2003-
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
4
|
*/
|
5
|
-
|
6
5
|
/**
|
7
6
|
* @module mention/mentioncommand
|
8
7
|
*/
|
9
|
-
|
10
8
|
import { Command } from 'ckeditor5/src/core';
|
11
9
|
import { CKEditorError, toMap } from 'ckeditor5/src/utils';
|
12
|
-
|
13
10
|
import { _addMentionAttributes } from './mentionediting';
|
14
|
-
|
15
11
|
/**
|
16
12
|
* The mention command.
|
17
13
|
*
|
@@ -19,132 +15,119 @@ import { _addMentionAttributes } from './mentionediting';
|
|
19
15
|
*
|
20
16
|
* To insert a mention onto a range, execute the command and specify a mention object with a range to replace:
|
21
17
|
*
|
22
|
-
*
|
18
|
+
* ```ts
|
19
|
+
* const focus = editor.model.document.selection.focus;
|
23
20
|
*
|
24
|
-
*
|
25
|
-
*
|
26
|
-
*
|
27
|
-
*
|
28
|
-
*
|
29
|
-
*
|
30
|
-
*
|
31
|
-
*
|
32
|
-
*
|
33
|
-
*
|
34
|
-
*
|
21
|
+
* // It will replace one character before the selection focus with the '#1234' text
|
22
|
+
* // with the mention attribute filled with passed attributes.
|
23
|
+
* editor.execute( 'mention', {
|
24
|
+
* marker: '#',
|
25
|
+
* mention: {
|
26
|
+
* id: '#1234',
|
27
|
+
* name: 'Foo',
|
28
|
+
* title: 'Big Foo'
|
29
|
+
* },
|
30
|
+
* range: editor.model.createRange( focus.getShiftedBy( -1 ), focus )
|
31
|
+
* } );
|
35
32
|
*
|
36
|
-
*
|
37
|
-
*
|
38
|
-
*
|
39
|
-
*
|
40
|
-
*
|
41
|
-
*
|
42
|
-
*
|
43
|
-
*
|
44
|
-
*
|
45
|
-
*
|
46
|
-
*
|
47
|
-
*
|
48
|
-
*
|
49
|
-
* @extends module:core/command~Command
|
33
|
+
* // It will replace one character before the selection focus with the 'The "Big Foo"' text
|
34
|
+
* // with the mention attribute filled with passed attributes.
|
35
|
+
* editor.execute( 'mention', {
|
36
|
+
* marker: '#',
|
37
|
+
* mention: {
|
38
|
+
* id: '#1234',
|
39
|
+
* name: 'Foo',
|
40
|
+
* title: 'Big Foo'
|
41
|
+
* },
|
42
|
+
* text: 'The "Big Foo"',
|
43
|
+
* range: editor.model.createRange( focus.getShiftedBy( -1 ), focus )
|
44
|
+
* } );
|
45
|
+
* ```
|
50
46
|
*/
|
51
47
|
export default class MentionCommand extends Command {
|
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
|
-
model.change( writer => {
|
140
|
-
const currentAttributes = toMap( selection.getAttributes() );
|
141
|
-
const attributesWithMention = new Map( currentAttributes.entries() );
|
142
|
-
|
143
|
-
attributesWithMention.set( 'mention', mention );
|
144
|
-
|
145
|
-
// Replace a range with the text with a mention.
|
146
|
-
model.insertContent( writer.createText( mentionText, attributesWithMention ), range );
|
147
|
-
model.insertContent( writer.createText( ' ', currentAttributes ), range.start.getShiftedBy( mentionText.length ) );
|
148
|
-
} );
|
149
|
-
}
|
48
|
+
/**
|
49
|
+
* @inheritDoc
|
50
|
+
*/
|
51
|
+
refresh() {
|
52
|
+
const model = this.editor.model;
|
53
|
+
const doc = model.document;
|
54
|
+
this.isEnabled = model.schema.checkAttributeInSelection(doc.selection, 'mention');
|
55
|
+
}
|
56
|
+
/**
|
57
|
+
* Executes the command.
|
58
|
+
*
|
59
|
+
* @param options Options for the executed command.
|
60
|
+
* @param options.mention The mention object to insert. When a string is passed, it will be used to create a plain
|
61
|
+
* object with the name attribute that equals the passed string.
|
62
|
+
* @param options.marker The marker character (e.g. `'@'`).
|
63
|
+
* @param options.text The text of the inserted mention. Defaults to the full mention string composed from `marker` and
|
64
|
+
* `mention` string or `mention.id` if an object is passed.
|
65
|
+
* @param options.range The range to replace.
|
66
|
+
* Note that the replaced range might be shorter than the inserted text with the mention attribute.
|
67
|
+
* @fires execute
|
68
|
+
*/
|
69
|
+
execute(options) {
|
70
|
+
const model = this.editor.model;
|
71
|
+
const document = model.document;
|
72
|
+
const selection = document.selection;
|
73
|
+
const mentionData = typeof options.mention == 'string' ? { id: options.mention } : options.mention;
|
74
|
+
const mentionID = mentionData.id;
|
75
|
+
const range = options.range || selection.getFirstRange();
|
76
|
+
const mentionText = options.text || mentionID;
|
77
|
+
const mention = _addMentionAttributes({ _text: mentionText, id: mentionID }, mentionData);
|
78
|
+
if (options.marker.length != 1) {
|
79
|
+
/**
|
80
|
+
* The marker must be a single character.
|
81
|
+
*
|
82
|
+
* Correct markers: `'@'`, `'#'`.
|
83
|
+
*
|
84
|
+
* Incorrect markers: `'@@'`, `'[@'`.
|
85
|
+
*
|
86
|
+
* See {@link module:mention/mention~MentionConfig}.
|
87
|
+
*
|
88
|
+
* @error mentioncommand-incorrect-marker
|
89
|
+
*/
|
90
|
+
throw new CKEditorError('mentioncommand-incorrect-marker', this);
|
91
|
+
}
|
92
|
+
if (mentionID.charAt(0) != options.marker) {
|
93
|
+
/**
|
94
|
+
* The feed item ID must start with the marker character.
|
95
|
+
*
|
96
|
+
* Correct mention feed setting:
|
97
|
+
*
|
98
|
+
* ```ts
|
99
|
+
* mentions: [
|
100
|
+
* {
|
101
|
+
* marker: '@',
|
102
|
+
* feed: [ '@Ann', '@Barney', ... ]
|
103
|
+
* }
|
104
|
+
* ]
|
105
|
+
* ```
|
106
|
+
*
|
107
|
+
* Incorrect mention feed setting:
|
108
|
+
*
|
109
|
+
* ```ts
|
110
|
+
* mentions: [
|
111
|
+
* {
|
112
|
+
* marker: '@',
|
113
|
+
* feed: [ 'Ann', 'Barney', ... ]
|
114
|
+
* }
|
115
|
+
* ]
|
116
|
+
* ```
|
117
|
+
*
|
118
|
+
* See {@link module:mention/mention~MentionConfig}.
|
119
|
+
*
|
120
|
+
* @error mentioncommand-incorrect-id
|
121
|
+
*/
|
122
|
+
throw new CKEditorError('mentioncommand-incorrect-id', this);
|
123
|
+
}
|
124
|
+
model.change(writer => {
|
125
|
+
const currentAttributes = toMap(selection.getAttributes());
|
126
|
+
const attributesWithMention = new Map(currentAttributes.entries());
|
127
|
+
attributesWithMention.set('mention', mention);
|
128
|
+
// Replace a range with the text with a mention.
|
129
|
+
model.insertContent(writer.createText(mentionText, attributesWithMention), range);
|
130
|
+
model.insertContent(writer.createText(' ', currentAttributes), range.start.getShiftedBy(mentionText.length));
|
131
|
+
});
|
132
|
+
}
|
150
133
|
}
|