1
1
import type { Printable } from 'tree-dump' ;
2
- import type { UiLifeCycles } from '../../dom/types' ;
3
- import type { UndoRedo } from './types' ;
2
+ import type { UiLifeCycles } from './types' ;
4
3
5
- export class DomUndoRedo < State > implements UndoRedo < State > , UiLifeCycles , Printable {
4
+ export class UndoRedoController implements UiLifeCycles , Printable {
6
5
private _duringUpdate : boolean = false ;
7
- private _stack : State [ ] = [ ] ;
8
- private el : HTMLElement ;
6
+ private _stack : unknown [ ] = [ ] ;
7
+ private el ! : HTMLElement ;
9
8
10
9
constructor (
11
- public onundo ?: ( state : State ) => void ,
12
- public onredo ?: ( state : State ) => void ,
13
- ) {
14
- // nb. Previous versions of this used `input` for browsers other than Firefox (as Firefox
15
- // _only_ supports execCommand on contentEditable)
16
- const el = this . el = document . createElement ( 'div' ) ;
17
- el . tabIndex = - 1 ;
18
- el . contentEditable = 'true' ;
19
- el . setAttribute ( 'aria-hidden' , 'true' ) ;
20
- const style = el . style ;
21
- // style.opacity = '0';
22
- style . position = 'fixed' ;
23
- // style.top = '-1000px';
24
- style . top = '10px' ;
25
- style . left = '10px' ;
26
- style . pointerEvents = 'none' ;
27
- style . fontSize = '2px' ;
28
- // style.visibility = 'hidden';
29
-
30
- document . body . appendChild ( el ) ;
31
-
32
- el . addEventListener ( 'focus' , ( ) => {
33
- // Timeout, as immediate blur doesn't work in some browsers.
34
- window . setTimeout ( ( ) => el . blur ( ) , 0 ) ;
35
- } ) ;
36
- el . addEventListener ( 'input' , ( ev ) => {
37
- if ( ! this . _duringUpdate ) {
38
- // callback(this.data);
39
- }
40
-
41
- // clear selection, otherwise user copy gesture will copy value
42
- // nb. this _probably_ won't work inside Shadow DOM
43
- // nb. this is mitigated by the fact that we set visibility: 'hidden'
44
- // const s = window.getSelection();
45
- // if (s.containsNode(this._ctrl, true)) {
46
- // s.removeAllRanges();
47
- // }
48
- } ) ;
49
- }
10
+ public onundo ?: ( state : unknown ) => void ,
11
+ public onredo ?: ( state : unknown ) => void ,
12
+ ) { }
50
13
51
14
/** ------------------------------------------------------ {@link UndoRedo} */
52
15
53
- public do ( state : State ) : void {
16
+ public do ( state : unknown ) : void {
54
17
const activeElement = document . activeElement ;
55
18
const el = this . el ;
56
19
const style = el . style ;
@@ -70,12 +33,48 @@ export class DomUndoRedo<State> implements UndoRedo<State>, UiLifeCycles, Printa
70
33
/** -------------------------------------------------- {@link UiLifeCycles} */
71
34
72
35
public start ( ) : void {
73
- throw new Error ( 'Method not implemented.' ) ;
36
+ const el = this . el = document . createElement ( 'div' ) ;
37
+ el . tabIndex = - 1 ;
38
+ el . contentEditable = 'true' ;
39
+ el . setAttribute ( 'aria-hidden' , 'true' ) ;
40
+ const style = el . style ;
41
+ // style.opacity = '0';
42
+ style . position = 'fixed' ;
43
+ // style.top = '-1000px';
44
+ style . top = '10px' ;
45
+ style . left = '10px' ;
46
+ style . pointerEvents = 'none' ;
47
+ style . fontSize = '2px' ;
48
+ // style.visibility = 'hidden';
49
+ document . body . appendChild ( el ) ;
50
+ el . addEventListener ( 'focus' , this . onFocus ) ;
51
+ el . addEventListener ( 'input' , this . onInput ) ;
74
52
}
75
53
76
54
public stop ( ) : void {
77
- throw new Error ( 'Method not implemented.' ) ;
55
+ const el = this . el ;
56
+ document . body . removeChild ( el ) ;
57
+ el . removeEventListener ( 'focus' , this . onFocus ) ;
58
+ el . removeEventListener ( 'input' , this . onInput ) ;
78
59
}
60
+
61
+ public readonly onFocus = ( ) => {
62
+ window . setTimeout ( ( ) => this . el . blur ( ) , 0 ) ;
63
+ } ;
64
+
65
+ public readonly onInput = ( ) => {
66
+ if ( ! this . _duringUpdate ) {
67
+ // callback(this.data);
68
+ }
69
+ // clear selection, otherwise user copy gesture will copy value
70
+ // nb. this _probably_ won't work inside Shadow DOM
71
+ // nb. this is mitigated by the fact that we set visibility: 'hidden'
72
+ // const s = window.getSelection();
73
+ // if (s.containsNode(this._ctrl, true)) {
74
+ // s.removeAllRanges();
75
+ // }
76
+ } ;
77
+
79
78
/** ----------------------------------------------------- {@link Printable} */
80
79
81
80
public toString ( tab ?: string ) : string {
0 commit comments