-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
enter.ts
66 lines (55 loc) · 1.76 KB
/
enter.ts
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
/**
* @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
*/
/**
* @module enter/enter
*/
import { Plugin } from '@ckeditor/ckeditor5-core';
import EnterCommand from './entercommand.js';
import EnterObserver, { type ViewDocumentEnterEvent } from './enterobserver.js';
/**
* This plugin handles the <kbd>Enter</kbd> keystroke (hard line break) in the editor.
*
* See also the {@link module:enter/shiftenter~ShiftEnter} plugin.
*
* For more information about this feature see the {@glink api/enter package page}.
*/
export default class Enter extends Plugin {
/**
* @inheritDoc
*/
public static get pluginName() {
return 'Enter' as const;
}
public init(): void {
const editor = this.editor;
const view = editor.editing.view;
const viewDocument = view.document;
const t = this.editor.t;
view.addObserver( EnterObserver );
editor.commands.add( 'enter', new EnterCommand( editor ) );
this.listenTo<ViewDocumentEnterEvent>( viewDocument, 'enter', ( 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();
}
// The soft enter key is handled by the ShiftEnter plugin.
if ( data.isSoft ) {
return;
}
editor.execute( 'enter' );
view.scrollToTheSelection();
}, { priority: 'low' } );
// Add the information about the keystroke to the accessibility database.
editor.accessibility.addKeystrokeInfos( {
keystrokes: [
{
label: t( 'Insert a hard break (a new paragraph)' ),
keystroke: 'Enter'
}
]
} );
}
}