/
standardeditingmodeui.ts
75 lines (61 loc) · 2.03 KB
/
standardeditingmodeui.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-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module restricted-editing/standardeditingmodeui
*/
import { Plugin } from 'ckeditor5/src/core.js';
import { ButtonView, MenuBarMenuListItemButtonView } from 'ckeditor5/src/ui.js';
import unlockIcon from '../theme/icons/contentunlock.svg';
/**
* The standard editing mode UI feature.
*
* It introduces the `'restrictedEditingException'` button that marks text as unrestricted for editing.
*/
export default class StandardEditingModeUI extends Plugin {
/**
* @inheritDoc
*/
public static get pluginName() {
return 'StandardEditingModeUI' as const;
}
/**
* @inheritDoc
*/
public init(): void {
const editor = this.editor;
editor.ui.componentFactory.add( 'restrictedEditingException', () => {
const button = this._createButton( ButtonView );
button.set( {
tooltip: true,
isToggleable: true
} );
return button;
} );
editor.ui.componentFactory.add( 'menuBar:restrictedEditingException', () => {
return this._createButton( MenuBarMenuListItemButtonView );
} );
}
/**
* Creates a button for restricted editing exception command to use either in toolbar or in menu bar.
*/
private _createButton<T extends typeof ButtonView | typeof MenuBarMenuListItemButtonView>( ButtonClass: T ): InstanceType<T> {
const editor = this.editor;
const locale = editor.locale;
const command = this.editor.commands.get( 'restrictedEditingException' )!;
const view = new ButtonClass( locale ) as InstanceType<T>;
const t = locale.t;
view.icon = unlockIcon;
view.bind( 'isOn', 'isEnabled' ).to( command, 'value', 'isEnabled' );
view.bind( 'label' ).to( command, 'value', value => {
return value ? t( 'Disable editing' ) : t( 'Enable editing' );
} );
// Execute the command.
this.listenTo( view, 'execute', () => {
editor.execute( 'restrictedEditingException' );
editor.editing.view.focus();
} );
return view;
}
}