-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
todocheckboxchangeobserver.ts
59 lines (53 loc) · 1.92 KB
/
todocheckboxchangeobserver.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
/**
* @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 list/todolist/todocheckboxchangeobserver
*/
import { DomEventObserver, type DomEventData } from 'ckeditor5/src/engine.js';
/**
* Observes all to-do list checkboxes state changes.
*
* Note that this observer is not available by default. To make it available it needs to be added to
* {@link module:engine/view/view~View} by {@link module:engine/view/view~View#addObserver} method.
*/
export default class TodoCheckboxChangeObserver extends DomEventObserver<'change'> {
/**
* @inheritDoc
*/
public readonly domEventType = [ 'change' ] as const;
/**
* @inheritDoc
*/
public onDomEvent( domEvent: Event ): void {
if ( domEvent.target ) {
const viewTarget = this.view.domConverter.mapDomToView( domEvent.target as HTMLElement );
if (
viewTarget &&
viewTarget.is( 'element', 'input' ) &&
viewTarget.getAttribute( 'type' ) == 'checkbox' &&
viewTarget.findAncestor( { classes: 'todo-list__label' } )
) {
this.fire( 'todoCheckboxChange', domEvent );
}
}
}
}
/**
* Fired when the to-do list checkbox is toggled.
*
* Introduced by {@link module:list/todolist/todocheckboxchangeobserver~TodoCheckboxChangeObserver}.
*
* Note that this event is not available by default. To make it available,
* {@link module:list/todolist/todocheckboxchangeobserver~TodoCheckboxChangeObserver}
* needs to be added to {@link module:engine/view/view~View} by the {@link module:engine/view/view~View#addObserver} method.
*
* @see module:list/todolist/todocheckboxchangeobserver~TodoCheckboxChangeObserver
* @eventName module:engine/view/document~Document#todoCheckboxChange
* @param data The event data.
*/
export type ViewDocumentTodoCheckboxChangeEvent = {
name: 'todoCheckboxChange';
args: [ data: DomEventData<Event> ];
};