-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
stylepanelview.ts
183 lines (148 loc) · 4.82 KB
/
stylepanelview.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* @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 style/ui/stylepanelview
*/
import { FocusCycler, View, ViewCollection } from 'ckeditor5/src/ui.js';
import { FocusTracker, KeystrokeHandler, type Locale } from 'ckeditor5/src/utils.js';
import StyleGroupView from './stylegroupview.js';
import type StyleGridView from './stylegridview.js';
import type { NormalizedStyleDefinitions } from '../styleutils.js';
import '../../theme/stylepanel.css';
/**
* A class representing a panel with available content styles. It renders styles in button grids, grouped
* in categories.
*/
export default class StylePanelView extends View<HTMLDivElement> {
/**
* Tracks information about DOM focus in the panel.
*/
public readonly focusTracker: FocusTracker;
/**
* An instance of the {@link module:utils/keystrokehandler~KeystrokeHandler}.
*/
public readonly keystrokes: KeystrokeHandler;
/**
* A collection of panel children.
*/
public readonly children: ViewCollection<StyleGroupView>;
/**
* A view representing block styles group.
*/
public readonly blockStylesGroupView: StyleGroupView;
/**
* A view representing inline styles group
*/
public readonly inlineStylesGroupView: StyleGroupView;
/**
* Array of active style names. They must correspond to the names of styles from
* definitions passed to the {@link #constructor}.
*
* @observable
*/
declare public readonly activeStyles: Array<string>;
/**
* Array of enabled style names. They must correspond to the names of styles from
* definitions passed to the {@link #constructor}.
*
* @observable
*/
declare public readonly enabledStyles: Array<string>;
/**
* A collection of views that can be focused in the panel.
*/
protected readonly _focusables: ViewCollection<StyleGridView>;
/**
* Helps cycling over {@link #_focusables} in the panel.
*/
protected readonly _focusCycler: FocusCycler;
/**
* Creates an instance of the {@link module:style/ui/stylegroupview~StyleGroupView} class.
*
* @param locale The localization services instance.
* @param styleDefinitions Normalized definitions of the styles.
*/
constructor( locale: Locale, styleDefinitions: NormalizedStyleDefinitions ) {
super( locale );
const t = locale.t;
this.focusTracker = new FocusTracker();
this.keystrokes = new KeystrokeHandler();
this.children = this.createCollection();
this.blockStylesGroupView = new StyleGroupView( locale, t( 'Block styles' ), styleDefinitions.block );
this.inlineStylesGroupView = new StyleGroupView( locale, t( 'Text styles' ), styleDefinitions.inline );
this.set( 'activeStyles', [] );
this.set( 'enabledStyles', [] );
this._focusables = new ViewCollection();
this._focusCycler = new FocusCycler( {
focusables: this._focusables,
focusTracker: this.focusTracker,
keystrokeHandler: this.keystrokes,
actions: {
// Navigate style groups backwards using the <kbd>Shift</kbd> + <kbd>Tab</kbd> keystroke.
focusPrevious: [ 'shift + tab' ],
// Navigate style groups forward using the <kbd>Tab</kbd> key.
focusNext: [ 'tab' ]
}
} );
if ( styleDefinitions.block.length ) {
this.children.add( this.blockStylesGroupView );
}
if ( styleDefinitions.inline.length ) {
this.children.add( this.inlineStylesGroupView );
}
this.blockStylesGroupView.gridView.delegate( 'execute' ).to( this );
this.inlineStylesGroupView.gridView.delegate( 'execute' ).to( this );
this.blockStylesGroupView.gridView
.bind( 'activeStyles', 'enabledStyles' )
.to( this, 'activeStyles', 'enabledStyles' );
this.inlineStylesGroupView.gridView
.bind( 'activeStyles', 'enabledStyles' )
.to( this, 'activeStyles', 'enabledStyles' );
this.setTemplate( {
tag: 'div',
attributes: {
class: [
'ck',
'ck-style-panel'
]
},
children: this.children
} );
}
/**
* @inheritDoc
*/
public override render(): void {
super.render();
// Register the views as focusable.
this._focusables.add( this.blockStylesGroupView.gridView );
this._focusables.add( this.inlineStylesGroupView.gridView );
// Register the views in the focus tracker.
this.focusTracker.add( this.blockStylesGroupView.gridView.element! );
this.focusTracker.add( this.inlineStylesGroupView.gridView.element! );
this.keystrokes.listenTo( this.element! );
}
/**
* Focuses the first focusable element in the panel.
*/
public focus(): void {
this._focusCycler.focusFirst();
}
/**
* Focuses the last focusable element in the panel.
*/
public focusLast(): void {
this._focusCycler.focusLast();
}
}
/**
* Fired when a style was selected (clicked) by the user.
*
* @eventName ~StylePanelView#execute
*/
export type StylePanelViewExecuteEvent = {
name: 'execute';
args: [];
};