/
shiftenter.ts
75 lines (63 loc) · 2 KB
/
shiftenter.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
67
68
69
70
71
72
73
74
75
/**
* @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 enter/shiftenter
*/
import ShiftEnterCommand from './shiftentercommand';
import EnterObserver, { type ViewDocumentEnterEvent } from './enterobserver';
import { Plugin } from '@ckeditor/ckeditor5-core';
/**
* This plugin handles the <kbd>Shift</kbd>+<kbd>Enter</kbd> keystroke (soft line break) in the editor.
*
* See also the {@link module:enter/enter~Enter} plugin.
*
* For more information about this feature see the {@glink api/enter package page}.
*/
export default class ShiftEnter extends Plugin {
/**
* @inheritDoc
*/
public static get pluginName(): 'ShiftEnter' {
return 'ShiftEnter';
}
public init(): void {
const editor = this.editor;
const schema = editor.model.schema;
const conversion = editor.conversion;
const view = editor.editing.view;
const viewDocument = view.document;
// Configure the schema.
schema.register( 'softBreak', {
allowWhere: '$text',
isInline: true
} );
// Configure converters.
conversion.for( 'upcast' )
.elementToElement( {
model: 'softBreak',
view: 'br'
} );
conversion.for( 'downcast' )
.elementToElement( {
model: 'softBreak',
view: ( modelElement, { writer } ) => writer.createEmptyElement( 'br' )
} );
view.addObserver( EnterObserver );
editor.commands.add( 'shiftEnter', new ShiftEnterCommand( 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 hard enter key is handled by the Enter plugin.
if ( !data.isSoft ) {
return;
}
editor.execute( 'shiftEnter' );
view.scrollToTheSelection();
}, { priority: 'low' } );
}
}