-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
indentui.ts
93 lines (77 loc) · 2.47 KB
/
indentui.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* @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 indent/indentui
*/
import { ButtonView, MenuBarMenuListItemButtonView } from 'ckeditor5/src/ui.js';
import { icons, Plugin } from 'ckeditor5/src/core.js';
/**
* The indent UI feature.
*
* This plugin registers the `'indent'` and `'outdent'` buttons.
*
* **Note**: In order for the commands to work, at least one of the compatible features is required. Read more in
* the {@link module:indent/indent~Indent indent feature} API documentation.
*/
export default class IndentUI extends Plugin {
/**
* @inheritDoc
*/
public static get pluginName() {
return 'IndentUI' as const;
}
/**
* @inheritDoc
*/
public init(): void {
const editor = this.editor;
const locale = editor.locale;
const t = editor.t;
const localizedIndentIcon = locale.uiLanguageDirection == 'ltr' ? icons.indent : icons.outdent;
const localizedOutdentIcon = locale.uiLanguageDirection == 'ltr' ? icons.outdent : icons.indent;
this._defineButton( 'indent', t( 'Increase indent' ), localizedIndentIcon );
this._defineButton( 'outdent', t( 'Decrease indent' ), localizedOutdentIcon );
}
/**
* Defines UI buttons for both toolbar and menu bar.
*/
private _defineButton( commandName: 'indent' | 'outdent', label: string, icon: string ): void {
const editor = this.editor;
editor.ui.componentFactory.add( commandName, () => {
const buttonView = this._createButton( ButtonView, commandName, label, icon );
buttonView.set( {
tooltip: true
} );
return buttonView;
} );
editor.ui.componentFactory.add( 'menuBar:' + commandName, () => {
return this._createButton( MenuBarMenuListItemButtonView, commandName, label, icon );
} );
}
/**
* Creates a button to use either in toolbar or in menu bar.
*/
private _createButton<T extends typeof ButtonView | typeof MenuBarMenuListItemButtonView>(
ButtonClass: T,
commandName: string,
label: string,
icon: string
): InstanceType<T> {
const editor = this.editor;
const command = editor.commands.get( commandName )!;
const view = new ButtonClass( editor.locale ) as InstanceType<T>;
view.set( {
label,
icon
} );
view.bind( 'isEnabled' ).to( command, 'isEnabled' );
// Execute the command.
this.listenTo( view, 'execute', () => {
editor.execute( commandName );
editor.editing.view.focus();
} );
return view;
}
}