-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
delete.js
108 lines (87 loc) · 3.11 KB
/
delete.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
/**
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module typing/delete
*/
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import DeleteCommand from './deletecommand';
import DeleteObserver from './deleteobserver';
/**
* The delete and backspace feature. Handles keys such as <kbd>Delete</kbd> and <kbd>Backspace</kbd>, other
* keystrokes and user actions that result in deleting content in the editor.
*
* @extends module:core/plugin~Plugin
*/
export default class Delete extends Plugin {
/**
* Whether pressing backspace should trigger undo action
*
* @private
* @member {Boolean} #_undoOnBackspace
*/
/**
* @inheritDoc
*/
static get pluginName() {
return 'Delete';
}
/**
* @inheritDoc
*/
init() {
const editor = this.editor;
const view = editor.editing.view;
const viewDocument = view.document;
const modelDocument = editor.model.document;
view.addObserver( DeleteObserver );
this._undoOnBackspace = false;
const deleteForwardCommand = new DeleteCommand( editor, 'forward' );
// Register `deleteForward` command and add `forwardDelete` command as an alias for backward compatibility.
editor.commands.add( 'deleteForward', deleteForwardCommand );
editor.commands.add( 'forwardDelete', deleteForwardCommand );
editor.commands.add( 'delete', new DeleteCommand( editor, 'backward' ) );
this.listenTo( viewDocument, 'delete', ( evt, data ) => {
// When not in composition, we handle the action, so prevent the default one.
// When in composition, it's the browser who modify the DOM (renderer is disabled).
if ( !viewDocument.isComposing ) {
data.preventDefault();
}
const { direction, sequence, selectionToRemove, unit } = data;
const commandName = direction === 'forward' ? 'deleteForward' : 'delete';
const commandData = { unit, sequence };
if ( unit == 'selection' ) {
const modelRanges = Array.from( selectionToRemove.getRanges() ).map( viewRange => {
return editor.editing.mapper.toModelRange( viewRange );
} );
commandData.selection = editor.model.createSelection( modelRanges );
}
editor.execute( commandName, commandData );
view.scrollToTheSelection();
}, { priority: 'low' } );
if ( this.editor.plugins.has( 'UndoEditing' ) ) {
this.listenTo( viewDocument, 'delete', ( evt, data ) => {
if ( this._undoOnBackspace && data.direction == 'backward' && data.sequence == 1 && data.unit == 'codePoint' ) {
this._undoOnBackspace = false;
editor.execute( 'undo' );
data.preventDefault();
evt.stop();
}
}, { context: '$capture' } );
this.listenTo( modelDocument, 'change', () => {
this._undoOnBackspace = false;
} );
}
}
/**
* If the next user action after calling this method is pressing backspace, it would undo the last change.
*
* Requires {@link module:undo/undoediting~UndoEditing} plugin. If not loaded, does nothing.
*/
requestUndoOnBackspace() {
if ( this.editor.plugins.has( 'UndoEditing' ) ) {
this._undoOnBackspace = true;
}
}
}