/
mentioncommand.js
231 lines (178 loc) · 7.17 KB
/
mentioncommand.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
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor.js';
import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model.js';
import MentionCommand from '../src/mentioncommand.js';
import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils.js';
describe( 'MentionCommand', () => {
let editor, command, model, doc, selection;
beforeEach( () => {
return ModelTestEditor
.create()
.then( newEditor => {
editor = newEditor;
model = editor.model;
doc = model.document;
selection = doc.selection;
model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
model.schema.register( 'x', { inheritAllFrom: '$block' } );
model.schema.extend( '$text', { allowAttributes: [ 'mention' ] } );
command = new MentionCommand( editor );
} );
} );
afterEach( () => {
command.destroy();
return editor.destroy();
} );
describe( 'isEnabled', () => {
it( 'should return true if characters with the attribute can be placed at caret position', () => {
setData( model, '<paragraph>f[]oo</paragraph>' );
expect( command.isEnabled ).to.be.true;
} );
it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
model.schema.addAttributeCheck( ( ctx, attributeName ) => {
// Allow 'bold' on p>$text.
if ( ctx.endsWith( 'x $text' ) && attributeName == 'mention' ) {
return false;
}
} );
setData( model, '<x>fo[]o</x>' );
expect( command.isEnabled ).to.be.false;
} );
} );
describe( 'execute()', () => {
it( 'inserts mention object if mention was passed as string', () => {
setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
command.execute( {
marker: '@',
mention: '@John',
range: model.createRange( selection.focus.getShiftedBy( -3 ), selection.focus )
} );
assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '@John' );
} );
it( 'should not execute if selectable is not editable', () => {
setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
model.document.isReadOnly = true;
command.execute( {
marker: '@',
mention: '@John',
range: model.createRange( selection.focus.getShiftedBy( -3 ), selection.focus )
} );
expect( doc.getRoot().getChild( 0 ).getChild( 1 ) ).to.be.null;
} );
it( 'inserts mention object with data if mention was passed as object', () => {
setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
command.execute( {
marker: '@',
mention: { id: '@John', userId: '123456' },
range: model.createRange( selection.focus.getShiftedBy( -3 ), selection.focus )
} );
const mentionNode = doc.getRoot().getChild( 0 ).getChild( 1 );
assertMention( mentionNode, '@John' );
expect( mentionNode.getAttribute( 'mention' ) ).to.have.property( 'userId', '123456' );
} );
it( 'inserts options.text as mention text', () => {
setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
command.execute( {
marker: '@',
mention: '@John',
text: '@John Doe',
range: model.createRange( selection.focus.getShiftedBy( -3 ), selection.focus )
} );
assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '@John' );
} );
it( 'inserts mention attribute with passed marker for given range', () => {
setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
const end = model.createPositionAt( selection.focus );
const start = end.getShiftedBy( -3 );
command.execute( {
marker: '#',
mention: '#John',
range: model.createRange( start, end )
} );
assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '#John' );
} );
it( 'inserts mention attribute at current selection if no range was passed', () => {
setData( model, '<paragraph>foo []bar</paragraph>' );
command.execute( {
marker: '@',
mention: '@John'
} );
assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '@John' );
} );
it( 'should set also other styles in inserted text', () => {
model.schema.extend( '$text', { allowAttributes: [ 'bold' ] } );
setData( model, '<paragraph><$text bold="true">foo@John[]bar</$text></paragraph>' );
command.execute( {
marker: '@',
mention: '@John',
range: model.createRange( selection.focus.getShiftedBy( -5 ), selection.focus )
} );
const textNode = doc.getRoot().getChild( 0 ).getChild( 1 );
assertMention( textNode, '@John' );
expect( textNode.hasAttribute( 'bold' ) ).to.be.true;
} );
it( 'should throw if marker is not one character', () => {
setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
const testCases = [
{ marker: '##', mention: '##foo' },
{ marker: '', mention: '@foo' }
];
for ( const options of testCases ) {
expectToThrowCKEditorError( () => command.execute( options ), /mentioncommand-incorrect-marker/, editor );
}
} );
it( 'should throw if marker does not match mention id', () => {
setData( model, '<paragraph>foo @Jo[]bar</paragraph>' );
const testCases = [
{ marker: '@', mention: 'foo' },
{ marker: '@', mention: { id: 'foo' } },
{ marker: '@', mention: { id: '#foo' } }
];
for ( const options of testCases ) {
expectToThrowCKEditorError( () => command.execute( options ), /mentioncommand-incorrect-id/, editor );
}
} );
it( 'should not insert a white space if the mention was already followed by one', () => {
setData( model, '<paragraph>foo [] bar</paragraph>' );
command.execute( {
marker: '@',
mention: '@John'
} );
assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '@John' );
expect( getData( model ) ).to.match(
/<paragraph>foo <\$text mention="{"uid":"[^"]+","_text":"@John","id":"@John"}">@John\[\]<\/\$text> bar<\/paragraph>/
);
} );
[ [ '(', ')' ], [ '[', ']' ], [ '{', '}' ] ].forEach( ( [ openingBracket, closingBracket ] ) => {
it( `should not insert a white space if the mention injected in "${ openingBracket }${ closingBracket }" brackets`, () => {
model.change( writer => {
const paragraph = writer.createElement( 'paragraph' );
const text = writer.createText( `${ openingBracket }${ closingBracket }` );
writer.append( text, paragraph );
writer.append( paragraph, model.document.getRoot() );
writer.setSelection( paragraph, 1 );
} );
command.execute( {
marker: '@',
mention: '@John'
} );
assertMention( doc.getRoot().getChild( 0 ).getChild( 1 ), '@John' );
expect( getData( model ) ).to.match(
new RegExp( '<paragraph>\\' + openingBracket +
'<\\$text mention="{"uid":"[^"]+","_text":"@John","id":"@John"}">@John\\[\\]</\\$text>\\' +
closingBracket + '</paragraph>'
)
);
} );
} );
} );
function assertMention( textNode, id ) {
expect( textNode.hasAttribute( 'mention' ) ).to.be.true;
expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'uid' );
expect( textNode.getAttribute( 'mention' ) ).to.have.property( '_text', textNode.data );
expect( textNode.getAttribute( 'mention' ) ).to.have.property( 'id', id );
}
} );