-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathrestrictededitingmodeediting.js
487 lines (410 loc) · 15.3 KB
/
restrictededitingmodeediting.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/**
* @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 restricted-editing/restrictededitingmodeediting
*/
import { Plugin } from 'ckeditor5/src/core';
import RestrictedEditingNavigationCommand from './restrictededitingmodenavigationcommand';
import {
extendMarkerOnTypingPostFixer,
resurrectCollapsedMarkerPostFixer,
setupExceptionHighlighting,
upcastHighlightToMarker
} from './restrictededitingmode/converters';
import { getMarkerAtPosition, isSelectionInMarker } from './restrictededitingmode/utils';
const COMMAND_FORCE_DISABLE_ID = 'RestrictedEditingMode';
/**
* The restricted editing mode editing feature.
*
* * It introduces the exception marker group that renders to `<span>` elements with the `restricted-editing-exception` CSS class.
* * It registers the `'goToPreviousRestrictedEditingException'` and `'goToNextRestrictedEditingException'` commands.
* * It also enables highlighting exception markers that are selected.
*
* @extends module:core/plugin~Plugin
*/
export default class RestrictedEditingModeEditing extends Plugin {
/**
* @inheritDoc
*/
static get pluginName() {
return 'RestrictedEditingModeEditing';
}
/**
* @inheritDoc
*/
constructor( editor ) {
super( editor );
editor.config.define( 'restrictedEditing', {
allowedCommands: [ 'bold', 'italic', 'link', 'unlink' ],
allowedAttributes: [ 'bold', 'italic', 'linkHref' ]
} );
/**
* Command names that are enabled outside the non-restricted regions.
*
* @type {Set.<String>}
* @private
*/
this._alwaysEnabled = new Set( [ 'undo', 'redo', 'goToPreviousRestrictedEditingException', 'goToNextRestrictedEditingException' ] );
/**
* Commands allowed in non-restricted areas.
*
* Commands always enabled combine typing feature commands: `'typing'`, `'delete'` and `'deleteForward'` with commands defined
* in the feature configuration.
*
* @type {Set<string>}
* @private
*/
this._allowedInException = new Set( [ 'input', 'delete', 'deleteForward' ] );
}
/**
* @inheritDoc
*/
init() {
const editor = this.editor;
const editingView = editor.editing.view;
const allowedCommands = editor.config.get( 'restrictedEditing.allowedCommands' );
allowedCommands.forEach( commandName => this._allowedInException.add( commandName ) );
this._setupConversion();
this._setupCommandsToggling();
this._setupRestrictions();
// Commands & keystrokes that allow navigation in the content.
editor.commands.add( 'goToPreviousRestrictedEditingException', new RestrictedEditingNavigationCommand( editor, 'backward' ) );
editor.commands.add( 'goToNextRestrictedEditingException', new RestrictedEditingNavigationCommand( editor, 'forward' ) );
editor.keystrokes.set( 'Tab', getCommandExecuter( editor, 'goToNextRestrictedEditingException' ) );
editor.keystrokes.set( 'Shift+Tab', getCommandExecuter( editor, 'goToPreviousRestrictedEditingException' ) );
editor.keystrokes.set( 'Ctrl+A', getSelectAllHandler( editor ) );
editingView.change( writer => {
for ( const root of editingView.document.roots ) {
writer.addClass( 'ck-restricted-editing_mode_restricted', root );
}
} );
}
/**
* Makes the given command always enabled in the restricted editing mode (regardless
* of selection location).
*
* To enable some commands in non-restricted areas of the content use
* {@link module:restricted-editing/restrictededitingmode~RestrictedEditingModeConfig#allowedCommands} configuration option.
*
* @param {String} commandName Name of the command to enable.
*/
enableCommand( commandName ) {
const command = this.editor.commands.get( commandName );
command.clearForceDisabled( COMMAND_FORCE_DISABLE_ID );
this._alwaysEnabled.add( commandName );
}
/**
* Sets up the restricted mode editing conversion:
*
* * ucpast & downcast converters,
* * marker highlighting in the edting area,
* * marker post-fixers.
*
* @private
*/
_setupConversion() {
const editor = this.editor;
const model = editor.model;
const doc = model.document;
// The restricted editing does not attach additional data to the zones so there's no need for smarter markers managing.
// Also, the markers will only be created when loading the data.
let markerNumber = 0;
editor.conversion.for( 'upcast' ).add( upcastHighlightToMarker( {
view: {
name: 'span',
classes: 'restricted-editing-exception'
},
model: () => {
markerNumber++; // Starting from restrictedEditingException:1 marker.
return `restrictedEditingException:${ markerNumber }`;
}
} ) );
// Currently the marker helpers are tied to other use-cases and do not render a collapsed marker as highlight.
// That's why there are 2 downcast converters for them:
// 1. The default marker-to-highlight will wrap selected text with `<span>`.
editor.conversion.for( 'downcast' ).markerToHighlight( {
model: 'restrictedEditingException',
// Use callback to return new object every time new marker instance is created - otherwise it will be seen as the same marker.
view: () => {
return {
name: 'span',
classes: 'restricted-editing-exception',
priority: -10
};
}
} );
// 2. But for collapsed marker we need to render it as an element.
// Additionally the editing pipeline should always display a collapsed marker.
editor.conversion.for( 'editingDowncast' ).markerToElement( {
model: 'restrictedEditingException',
view: ( markerData, { writer } ) => {
return writer.createUIElement( 'span', {
class: 'restricted-editing-exception restricted-editing-exception_collapsed'
} );
}
} );
editor.conversion.for( 'dataDowncast' ).markerToElement( {
model: 'restrictedEditingException',
view: ( markerData, { writer } ) => {
return writer.createEmptyElement( 'span', {
class: 'restricted-editing-exception'
} );
}
} );
doc.registerPostFixer( extendMarkerOnTypingPostFixer( editor ) );
doc.registerPostFixer( resurrectCollapsedMarkerPostFixer( editor ) );
model.markers.on( 'update:restrictedEditingException', ensureNewMarkerIsFlat( editor ) );
setupExceptionHighlighting( editor );
}
/**
* Setups additional editing restrictions beyond command toggling:
*
* * delete content range trimming
* * disabling input command outside exception marker
* * restricting clipboard holder to text only
* * restricting text attributes in content
*
* @private
*/
_setupRestrictions() {
const editor = this.editor;
const model = editor.model;
const selection = model.document.selection;
const viewDoc = editor.editing.view.document;
const clipboard = editor.plugins.get( 'ClipboardPipeline' );
this.listenTo( model, 'deleteContent', restrictDeleteContent( editor ), { priority: 'high' } );
const inputCommand = editor.commands.get( 'input' );
// The restricted editing might be configured without input support - ie allow only bolding or removing text.
// This check is bit synthetic since only tests are used this way.
if ( inputCommand ) {
this.listenTo( inputCommand, 'execute', disallowInputExecForWrongRange( editor ), { priority: 'high' } );
}
// Block clipboard outside exception marker on paste and drop.
this.listenTo( clipboard, 'contentInsertion', evt => {
if ( !isRangeInsideSingleMarker( editor, selection.getFirstRange() ) ) {
evt.stop();
}
} );
// Block clipboard outside exception marker on cut.
this.listenTo( viewDoc, 'clipboardOutput', ( evt, data ) => {
if ( data.method == 'cut' && !isRangeInsideSingleMarker( editor, selection.getFirstRange() ) ) {
evt.stop();
}
}, { priority: 'high' } );
const allowedAttributes = editor.config.get( 'restrictedEditing.allowedAttributes' );
model.schema.addAttributeCheck( onlyAllowAttributesFromList( allowedAttributes ) );
model.schema.addChildCheck( allowTextOnlyInClipboardHolder );
}
/**
* Sets up the command toggling which enables or disables commands based on the user selection.
*
* @private
*/
_setupCommandsToggling() {
const editor = this.editor;
const model = editor.model;
const doc = model.document;
this._disableCommands( editor );
this.listenTo( doc.selection, 'change', this._checkCommands.bind( this ) );
this.listenTo( doc, 'change:data', this._checkCommands.bind( this ) );
}
/**
* Checks if commands should be enabled or disabled based on the current selection.
*
* @private
*/
_checkCommands() {
const editor = this.editor;
const selection = editor.model.document.selection;
if ( selection.rangeCount > 1 ) {
this._disableCommands( editor );
return;
}
const marker = getMarkerAtPosition( editor, selection.focus );
this._disableCommands();
if ( isSelectionInMarker( selection, marker ) ) {
this._enableCommands( marker );
}
}
/**
* Enables commands in non-restricted regions.
*
* @returns {module:engine/model/markercollection~Marker} marker
* @private
*/
_enableCommands( marker ) {
const editor = this.editor;
const commands = this._getCommandNamesToToggle( editor, this._allowedInException )
.filter( name => this._allowedInException.has( name ) )
.filter( filterDeleteCommandsOnMarkerBoundaries( editor.model.document.selection, marker.getRange() ) )
.map( name => editor.commands.get( name ) );
for ( const command of commands ) {
command.clearForceDisabled( COMMAND_FORCE_DISABLE_ID );
}
}
/**
* Disables commands outside non-restricted regions.
*
* @private
*/
_disableCommands() {
const editor = this.editor;
const commands = this._getCommandNamesToToggle( editor )
.map( name => editor.commands.get( name ) );
for ( const command of commands ) {
command.forceDisabled( COMMAND_FORCE_DISABLE_ID );
}
}
/**
* Returns command names that should be toggleable.
*
* @param {module:core/editor/editor~Editor} editor
* @returns {Array.<String>}
* @private
*/
_getCommandNamesToToggle( editor ) {
return Array.from( editor.commands.names() )
.filter( name => !this._alwaysEnabled.has( name ) );
}
}
// Helper method for executing enabled commands only.
function getCommandExecuter( editor, commandName ) {
return ( data, cancel ) => {
const command = editor.commands.get( commandName );
if ( command.isEnabled ) {
editor.execute( commandName );
cancel();
}
};
}
// Helper for handling Ctrl+A keydown behaviour.
function getSelectAllHandler( editor ) {
return ( data, cancel ) => {
const model = editor.model;
const selection = editor.model.document.selection;
const marker = getMarkerAtPosition( editor, selection.focus );
if ( !marker ) {
return;
}
// If selection range is inside a restricted editing exception, select text only within the exception.
//
// Note: Second Ctrl+A press is also blocked and it won't select the entire text in the editor.
const selectionRange = selection.getFirstRange();
const markerRange = marker.getRange();
if ( markerRange.containsRange( selectionRange, true ) || selection.isCollapsed ) {
cancel();
model.change( writer => {
writer.setSelection( marker.getRange() );
} );
}
};
}
// Additional filtering rule for enabling "delete" and "deleteForward" commands if selection is on range boundaries:
//
// Does not allow to enable command when selection focus is:
// - is on marker start - "delete" - to prevent removing content before marker
// - is on marker end - "deleteForward" - to prevent removing content after marker
function filterDeleteCommandsOnMarkerBoundaries( selection, markerRange ) {
return name => {
if ( name == 'delete' && markerRange.start.isEqual( selection.focus ) ) {
return false;
}
// Only for collapsed selection - non-collapsed selection that extends over a marker is handled elsewhere.
if ( name == 'deleteForward' && selection.isCollapsed && markerRange.end.isEqual( selection.focus ) ) {
return false;
}
return true;
};
}
// Ensures that model.deleteContent() does not delete outside exception markers ranges.
//
// The enforced restrictions are:
// - only execute deleteContent() inside exception markers
// - restrict passed selection to exception marker
function restrictDeleteContent( editor ) {
return ( evt, args ) => {
const [ selection ] = args;
const marker = getMarkerAtPosition( editor, selection.focus ) || getMarkerAtPosition( editor, selection.anchor );
// Stop method execution if marker was not found at selection focus.
if ( !marker ) {
evt.stop();
return;
}
// Collapsed selection inside exception marker does not require fixing.
if ( selection.isCollapsed ) {
return;
}
// Shrink the selection to the range inside exception marker.
const allowedToDelete = marker.getRange().getIntersection( selection.getFirstRange() );
// Some features uses selection passed to model.deleteContent() to set the selection afterwards. For this we need to properly modify
// either the document selection using change block...
if ( selection.is( 'documentSelection' ) ) {
editor.model.change( writer => {
writer.setSelection( allowedToDelete );
} );
}
// ... or by modifying passed selection instance directly.
else {
selection.setTo( allowedToDelete );
}
};
}
// Ensures that input command is executed with a range that is inside exception marker.
//
// This restriction is due to fact that using native spell check changes text outside exception marker.
function disallowInputExecForWrongRange( editor ) {
return ( evt, args ) => {
const [ options ] = args;
const { range } = options;
// Only check "input" command executed with a range value.
// Selection might be set in exception marker but passed range might point elsewhere.
if ( !range ) {
return;
}
if ( !isRangeInsideSingleMarker( editor, range ) ) {
evt.stop();
}
};
}
function isRangeInsideSingleMarker( editor, range ) {
const markerAtStart = getMarkerAtPosition( editor, range.start );
const markerAtEnd = getMarkerAtPosition( editor, range.end );
return markerAtStart && markerAtEnd && markerAtEnd === markerAtStart;
}
// Checks if new marker range is flat. Non-flat ranges might appear during upcast conversion in nested structures, ie tables.
//
// Note: This marker fixer only consider case which is possible to create using StandardEditing mode plugin.
// Markers created by developer in the data might break in many other ways.
//
// See #6003.
function ensureNewMarkerIsFlat( editor ) {
const model = editor.model;
return ( evt, marker, oldRange, newRange ) => {
if ( !oldRange && !newRange.isFlat ) {
model.change( writer => {
const start = newRange.start;
const end = newRange.end;
const startIsHigherInTree = start.path.length > end.path.length;
const fixedStart = startIsHigherInTree ? newRange.start : writer.createPositionAt( end.parent, 0 );
const fixedEnd = startIsHigherInTree ? writer.createPositionAt( start.parent, 'end' ) : newRange.end;
writer.updateMarker( marker, {
range: writer.createRange( fixedStart, fixedEnd )
} );
} );
}
};
}
function onlyAllowAttributesFromList( allowedAttributes ) {
return ( context, attributeName ) => {
if ( context.startsWith( '$clipboardHolder' ) ) {
return allowedAttributes.includes( attributeName );
}
};
}
function allowTextOnlyInClipboardHolder( context, childDefinition ) {
if ( context.startsWith( '$clipboardHolder' ) ) {
return childDefinition.name === '$text';
}
}