@ckeditor/ckeditor5-mention 38.1.1 → 38.2.0-alpha.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,145 +1,145 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- /**
6
- * @module mention/mentioncommand
7
- */
8
- import { Command } from 'ckeditor5/src/core';
9
- import { CKEditorError, toMap } from 'ckeditor5/src/utils';
10
- import { _addMentionAttributes } from './mentionediting';
11
- /**
12
- * The mention command.
13
- *
14
- * The command is registered by {@link module:mention/mentionediting~MentionEditing} as `'mention'`.
15
- *
16
- * To insert a mention into a range, execute the command and specify a mention object with a range to replace:
17
- *
18
- * ```ts
19
- * const focus = editor.model.document.selection.focus;
20
- *
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
- * } );
32
- *
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
- * ```
46
- */
47
- export default class MentionCommand extends Command {
48
- /**
49
- * @inheritDoc
50
- */
51
- constructor(editor) {
52
- super(editor);
53
- // Since this command may pass range in execution parameters, it should be checked directly in execute block.
54
- this._isEnabledBasedOnSelection = false;
55
- }
56
- /**
57
- * @inheritDoc
58
- */
59
- refresh() {
60
- const model = this.editor.model;
61
- const doc = model.document;
62
- this.isEnabled = model.schema.checkAttributeInSelection(doc.selection, 'mention');
63
- }
64
- /**
65
- * Executes the command.
66
- *
67
- * @param options Options for the executed command.
68
- * @param options.mention The mention object to insert. When a string is passed, it will be used to create a plain
69
- * object with the name attribute that equals the passed string.
70
- * @param options.marker The marker character (e.g. `'@'`).
71
- * @param options.text The text of the inserted mention. Defaults to the full mention string composed from `marker` and
72
- * `mention` string or `mention.id` if an object is passed.
73
- * @param options.range The range to replace.
74
- * Note that the replaced range might be shorter than the inserted text with the mention attribute.
75
- * @fires execute
76
- */
77
- execute(options) {
78
- const model = this.editor.model;
79
- const document = model.document;
80
- const selection = document.selection;
81
- const mentionData = typeof options.mention == 'string' ? { id: options.mention } : options.mention;
82
- const mentionID = mentionData.id;
83
- const range = options.range || selection.getFirstRange();
84
- // Don't execute command if range is in non-editable place.
85
- if (!model.canEditAt(range)) {
86
- return;
87
- }
88
- const mentionText = options.text || mentionID;
89
- const mention = _addMentionAttributes({ _text: mentionText, id: mentionID }, mentionData);
90
- if (options.marker.length != 1) {
91
- /**
92
- * The marker must be a single character.
93
- *
94
- * Correct markers: `'@'`, `'#'`.
95
- *
96
- * Incorrect markers: `'@@'`, `'[@'`.
97
- *
98
- * See {@link module:mention/mentionconfig~MentionConfig}.
99
- *
100
- * @error mentioncommand-incorrect-marker
101
- */
102
- throw new CKEditorError('mentioncommand-incorrect-marker', this);
103
- }
104
- if (mentionID.charAt(0) != options.marker) {
105
- /**
106
- * The feed item ID must start with the marker character.
107
- *
108
- * Correct mention feed setting:
109
- *
110
- * ```ts
111
- * mentions: [
112
- * {
113
- * marker: '@',
114
- * feed: [ '@Ann', '@Barney', ... ]
115
- * }
116
- * ]
117
- * ```
118
- *
119
- * Incorrect mention feed setting:
120
- *
121
- * ```ts
122
- * mentions: [
123
- * {
124
- * marker: '@',
125
- * feed: [ 'Ann', 'Barney', ... ]
126
- * }
127
- * ]
128
- * ```
129
- *
130
- * See {@link module:mention/mentionconfig~MentionConfig}.
131
- *
132
- * @error mentioncommand-incorrect-id
133
- */
134
- throw new CKEditorError('mentioncommand-incorrect-id', this);
135
- }
136
- model.change(writer => {
137
- const currentAttributes = toMap(selection.getAttributes());
138
- const attributesWithMention = new Map(currentAttributes.entries());
139
- attributesWithMention.set('mention', mention);
140
- // Replace a range with the text with a mention.
141
- model.insertContent(writer.createText(mentionText, attributesWithMention), range);
142
- model.insertContent(writer.createText(' ', currentAttributes), range.start.getShiftedBy(mentionText.length));
143
- });
144
- }
145
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module mention/mentioncommand
7
+ */
8
+ import { Command } from 'ckeditor5/src/core.js';
9
+ import { CKEditorError, toMap } from 'ckeditor5/src/utils.js';
10
+ import { _addMentionAttributes } from './mentionediting.js';
11
+ /**
12
+ * The mention command.
13
+ *
14
+ * The command is registered by {@link module:mention/mentionediting~MentionEditing} as `'mention'`.
15
+ *
16
+ * To insert a mention into a range, execute the command and specify a mention object with a range to replace:
17
+ *
18
+ * ```ts
19
+ * const focus = editor.model.document.selection.focus;
20
+ *
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
+ * } );
32
+ *
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
+ * ```
46
+ */
47
+ export default class MentionCommand extends Command {
48
+ /**
49
+ * @inheritDoc
50
+ */
51
+ constructor(editor) {
52
+ super(editor);
53
+ // Since this command may pass range in execution parameters, it should be checked directly in execute block.
54
+ this._isEnabledBasedOnSelection = false;
55
+ }
56
+ /**
57
+ * @inheritDoc
58
+ */
59
+ refresh() {
60
+ const model = this.editor.model;
61
+ const doc = model.document;
62
+ this.isEnabled = model.schema.checkAttributeInSelection(doc.selection, 'mention');
63
+ }
64
+ /**
65
+ * Executes the command.
66
+ *
67
+ * @param options Options for the executed command.
68
+ * @param options.mention The mention object to insert. When a string is passed, it will be used to create a plain
69
+ * object with the name attribute that equals the passed string.
70
+ * @param options.marker The marker character (e.g. `'@'`).
71
+ * @param options.text The text of the inserted mention. Defaults to the full mention string composed from `marker` and
72
+ * `mention` string or `mention.id` if an object is passed.
73
+ * @param options.range The range to replace.
74
+ * Note that the replaced range might be shorter than the inserted text with the mention attribute.
75
+ * @fires execute
76
+ */
77
+ execute(options) {
78
+ const model = this.editor.model;
79
+ const document = model.document;
80
+ const selection = document.selection;
81
+ const mentionData = typeof options.mention == 'string' ? { id: options.mention } : options.mention;
82
+ const mentionID = mentionData.id;
83
+ const range = options.range || selection.getFirstRange();
84
+ // Don't execute command if range is in non-editable place.
85
+ if (!model.canEditAt(range)) {
86
+ return;
87
+ }
88
+ const mentionText = options.text || mentionID;
89
+ const mention = _addMentionAttributes({ _text: mentionText, id: mentionID }, mentionData);
90
+ if (options.marker.length != 1) {
91
+ /**
92
+ * The marker must be a single character.
93
+ *
94
+ * Correct markers: `'@'`, `'#'`.
95
+ *
96
+ * Incorrect markers: `'@@'`, `'[@'`.
97
+ *
98
+ * See {@link module:mention/mentionconfig~MentionConfig}.
99
+ *
100
+ * @error mentioncommand-incorrect-marker
101
+ */
102
+ throw new CKEditorError('mentioncommand-incorrect-marker', this);
103
+ }
104
+ if (mentionID.charAt(0) != options.marker) {
105
+ /**
106
+ * The feed item ID must start with the marker character.
107
+ *
108
+ * Correct mention feed setting:
109
+ *
110
+ * ```ts
111
+ * mentions: [
112
+ * {
113
+ * marker: '@',
114
+ * feed: [ '@Ann', '@Barney', ... ]
115
+ * }
116
+ * ]
117
+ * ```
118
+ *
119
+ * Incorrect mention feed setting:
120
+ *
121
+ * ```ts
122
+ * mentions: [
123
+ * {
124
+ * marker: '@',
125
+ * feed: [ 'Ann', 'Barney', ... ]
126
+ * }
127
+ * ]
128
+ * ```
129
+ *
130
+ * See {@link module:mention/mentionconfig~MentionConfig}.
131
+ *
132
+ * @error mentioncommand-incorrect-id
133
+ */
134
+ throw new CKEditorError('mentioncommand-incorrect-id', this);
135
+ }
136
+ model.change(writer => {
137
+ const currentAttributes = toMap(selection.getAttributes());
138
+ const attributesWithMention = new Map(currentAttributes.entries());
139
+ attributesWithMention.set('mention', mention);
140
+ // Replace a range with the text with a mention.
141
+ model.insertContent(writer.createText(mentionText, attributesWithMention), range);
142
+ model.insertContent(writer.createText(' ', currentAttributes), range.start.getShiftedBy(mentionText.length));
143
+ });
144
+ }
145
+ }