-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
domwrapperview.ts
82 lines (68 loc) · 1.87 KB
/
domwrapperview.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
/**
* @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 mention/ui/domwrapperview
*/
import { View } from 'ckeditor5/src/ui.js';
import type { Locale } from 'ckeditor5/src/utils.js';
/**
* This class wraps DOM element as a CKEditor5 UI View.
*
* It allows to render any DOM element and use it in mentions list.
*/
export default class DomWrapperView extends View {
/**
* The DOM element for which wrapper was created.
*/
public domElement: HTMLElement;
/**
* Controls whether the dom wrapper view is "on". This is in line with {@link module:ui/button/button~Button#isOn} property.
*
* @observable
* @default true
*/
declare public isOn: boolean;
/**
* Creates an instance of {@link module:mention/ui/domwrapperview~DomWrapperView} class.
*
* Also see {@link #render}.
*/
constructor( locale: Locale, domElement: HTMLElement ) {
super( locale );
// Disable template rendering on this view.
this.template = undefined;
this.domElement = domElement;
// Render dom wrapper as a button.
this.domElement.classList.add( 'ck-button' );
this.set( 'isOn', false );
// Handle isOn state as in buttons.
this.on( 'change:isOn', ( evt, name, isOn ) => {
if ( isOn ) {
this.domElement.classList.add( 'ck-on' );
this.domElement.classList.remove( 'ck-off' );
} else {
this.domElement.classList.add( 'ck-off' );
this.domElement.classList.remove( 'ck-on' );
}
} );
// Pass click event as execute event.
this.listenTo( this.domElement, 'click', () => {
this.fire( 'execute' );
} );
}
/**
* @inheritDoc
*/
public override render(): void {
super.render();
this.element = this.domElement;
}
/**
* Focuses the DOM element.
*/
public focus(): void {
this.domElement.focus();
}
}