@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.
@@ -1,17 +1,13 @@
1
1
  /**
2
- * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
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
- * const focus = editor.model.document.selection.focus;
18
+ * ```ts
19
+ * const focus = editor.model.document.selection.focus;
23
20
  *
24
- * // It will replace one character before the selection focus with the '#1234' text
25
- * // with the mention attribute filled with passed attributes.
26
- * editor.execute( 'mention', {
27
- * marker: '#',
28
- * mention: {
29
- * id: '#1234',
30
- * name: 'Foo',
31
- * title: 'Big Foo'
32
- * },
33
- * range: editor.model.createRange( focus.getShiftedBy( -1 ), focus )
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
- * // It will replace one character before the selection focus with the 'The "Big Foo"' text
37
- * // with the mention attribute filled with passed attributes.
38
- * editor.execute( 'mention', {
39
- * marker: '#',
40
- * mention: {
41
- * id: '#1234',
42
- * name: 'Foo',
43
- * title: 'Big Foo'
44
- * },
45
- * text: 'The "Big Foo"',
46
- * range: editor.model.createRange( focus.getShiftedBy( -1 ), focus )
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
- * @inheritDoc
54
- */
55
- refresh() {
56
- const model = this.editor.model;
57
- const doc = model.document;
58
-
59
- this.isEnabled = model.schema.checkAttributeInSelection( doc.selection, 'mention' );
60
- }
61
-
62
- /**
63
- * Executes the command.
64
- *
65
- * @param {Object} [options] Options for the executed command.
66
- * @param {Object|String} options.mention The mention object to insert. When a string is passed, it will be used to create a plain
67
- * object with the name attribute that equals the passed string.
68
- * @param {String} options.marker The marker character (e.g. `'@'`).
69
- * @param {String} [options.text] The text of the inserted mention. Defaults to the full mention string composed from `marker` and
70
- * `mention` string or `mention.id` if an object is passed.
71
- * @param {module:engine/model/range~Range} [options.range] The range to replace.
72
- * Note that the replaced range might be shorter than the inserted text with the mention attribute.
73
- * @fires execute
74
- */
75
- execute( options ) {
76
- const model = this.editor.model;
77
- const document = model.document;
78
- const selection = document.selection;
79
-
80
- const mentionData = typeof options.mention == 'string' ? { id: options.mention } : options.mention;
81
- const mentionID = mentionData.id;
82
-
83
- const range = options.range || selection.getFirstRange();
84
-
85
- const mentionText = options.text || mentionID;
86
-
87
- const mention = _addMentionAttributes( { _text: mentionText, id: mentionID }, mentionData );
88
-
89
- if ( options.marker.length != 1 ) {
90
- /**
91
- * The marker must be a single character.
92
- *
93
- * Correct markers: `'@'`, `'#'`.
94
- *
95
- * Incorrect markers: `'$$'`, `'[@'`.
96
- *
97
- * See {@link module:mention/mention~MentionConfig}.
98
- *
99
- * @error mentioncommand-incorrect-marker
100
- */
101
- throw new CKEditorError(
102
- 'mentioncommand-incorrect-marker',
103
- this
104
- );
105
- }
106
-
107
- if ( mentionID.charAt( 0 ) != options.marker ) {
108
- /**
109
- * The feed item ID must start with the marker character.
110
- *
111
- * Correct mention feed setting:
112
- *
113
- * mentions: [
114
- * {
115
- * marker: '@',
116
- * feed: [ '@Ann', '@Barney', ... ]
117
- * }
118
- * ]
119
- *
120
- * Incorrect mention feed setting:
121
- *
122
- * mentions: [
123
- * {
124
- * marker: '@',
125
- * feed: [ 'Ann', 'Barney', ... ]
126
- * }
127
- * ]
128
- *
129
- * See {@link module:mention/mention~MentionConfig}.
130
- *
131
- * @error mentioncommand-incorrect-id
132
- */
133
- throw new CKEditorError(
134
- 'mentioncommand-incorrect-id',
135
- this
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
  }