-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathfullscreencommand.ts
More file actions
113 lines (98 loc) · 3.36 KB
/
fullscreencommand.ts
File metadata and controls
113 lines (98 loc) · 3.36 KB
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
/**
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/
/**
* @module fullscreen/fullscreencommand
*/
import { Command, type Editor } from 'ckeditor5/src/core.js';
import type { ClassicEditor } from '@ckeditor/ckeditor5-editor-classic';
import type { DecoupledEditor } from '@ckeditor/ckeditor5-editor-decoupled';
import { FullscreenAbstractEditorHandler } from './handlers/abstracteditorhandler.js';
import { FullscreenClassicEditorHandler } from './handlers/classiceditorhandler.js';
import { FullscreenDecoupledEditorHandler } from './handlers/decouplededitorhandler.js';
/**
* A command toggling the fullscreen mode.
*/
export class FullscreenCommand extends Command {
/**
* Indicates whether the fullscreen mode is enabled.
*
* @observable
* @readonly
*/
declare public value: boolean;
/**
* Specialized class handling the fullscreen mode toggling for a specific editor type.
*
* If you want to add support for a new editor type (for now, only Classic and Decoupled editors are handled),
* create a custom handler that extends `FullscreenAbstractEditorHandler` and replace `fullscreenHandler` with it after
* editor initialization:
*
* ```ts
* // See the details of how to implement a custom handler in the `FullscreenAbstractEditorHandler` class API docs.
* class CustomEditorHandler extends FullscreenAbstractEditorHandler {}
*
* CustomEditorClass.create( document.querySelector( '#editor' ), {} )
* .then( ( editor ) => {
* editor.commands.get( 'toggleFullscreen' ).fullscreenHandler = new CustomEditorHandler( editor );
* } );
* ```
*/
public fullscreenHandler: FullscreenAbstractEditorHandler;
/**
* @inheritDoc
*/
public constructor( editor: Editor ) {
super( editor );
this.affectsData = false;
this.isEnabled = true;
this.value = false;
// Choose the appropriate handler based on the editor type.
// Currently only `ClassicEditor` and `DecoupledEditor` are supported. For other editor types, you should create a custom handler
// that extends `FullscreenAbstractEditorHandler` and replace `fullscreenHandler` with it.
if ( isClassicEditor( editor ) ) {
this.fullscreenHandler = new FullscreenClassicEditorHandler( editor );
} else if ( isDecoupledEditor( editor ) ) {
this.fullscreenHandler = new FullscreenDecoupledEditorHandler( editor );
} else {
this.fullscreenHandler = new FullscreenAbstractEditorHandler( editor );
}
}
/**
* Toggles the fullscreen mode.
*/
public override execute(): void {
if ( this.value ) {
this._disableFullscreenMode();
} else {
this._enableFullscreenMode();
}
}
/**
* Enables the fullscreen mode.
*/
private _enableFullscreenMode(): void {
this.fullscreenHandler.enable();
this.value = true;
}
/**
* Disables the fullscreen mode.
*/
private _disableFullscreenMode(): void {
this.fullscreenHandler.disable();
this.value = false;
}
}
/**
* Classic editor typeguard.
*/
function isClassicEditor( editor: Editor ): editor is ClassicEditor {
return ( editor.constructor as typeof Editor ).editorName === 'ClassicEditor';
}
/**
* Decoupled editor typeguard.
*/
function isDecoupledEditor( editor: Editor ): editor is DecoupledEditor {
return ( editor.constructor as typeof Editor ).editorName === 'DecoupledEditor';
}