@@ -14,6 +14,14 @@ export class EmptyEntryLabelRemoveEvent extends Event {
1414 }
1515}
1616
17+ export class EntryLabelChangeEvent extends Event {
18+ static readonly eventName = 'entrylabelchangeevent' ;
19+
20+ constructor ( public newLabel : string ) {
21+ super ( EntryLabelChangeEvent . eventName ) ;
22+ }
23+ }
24+
1725export class EntryLabelOverlay extends HTMLElement {
1826 // The label is angled on the left from the centre of the entry it belongs to.
1927 // `LABEL_AND_CONNECTOR_SHIFT_LENGTH` specifies how many pixels to the left it is shifted.
@@ -25,7 +33,7 @@ export class EntryLabelOverlay extends HTMLElement {
2533 static readonly LABEL_AND_CONNECTOR_HEIGHT =
2634 EntryLabelOverlay . LABEL_HEIGHT + EntryLabelOverlay . LABEL_PADDING * 2 + EntryLabelOverlay . LABEL_CONNECTOR_HEIGHT ;
2735 // Set the max label length to avoid labels that could signicantly increase the file size.
28- static readonly MAX_LABEL_LENGTH = 100 ;
36+ static readonly MAX_LABEL_LENGTH = 1 ;
2937
3038 static readonly litTagName = LitHtml . literal `devtools-entry-label-overlay` ;
3139 readonly #shadow = this . attachShadow ( { mode : 'open' } ) ;
@@ -37,6 +45,7 @@ export class EntryLabelOverlay extends HTMLElement {
3745
3846 #labelPartsWrapper: HTMLElement | null = null ;
3947 #labelBox: HTMLElement | null = null ;
48+ #label: string ;
4049
4150 /*
4251The entry label overlay consists of 3 parts - the label part with the label string inside,
@@ -62,22 +71,29 @@ Otherwise, the entry label overlay object only gets repositioned.
6271 super ( ) ;
6372 this . #render( ) ;
6473 this . #labelPartsWrapper = this . #shadow. querySelector < HTMLElement > ( '.label-parts-wrapper' ) ;
74+ this . #label = label ;
6575 this . #drawLabel( label ) ;
6676 this . #drawConnector( ) ;
6777 }
6878
6979 connectedCallback ( ) : void {
7080 this . #shadow. adoptedStyleSheets = [ styles ] ;
71- this . #labelBox?. addEventListener ( 'keydown' , this . #handleLabelInputKeyDown) ;
72- this . #labelBox?. addEventListener ( 'paste' , this . #handleLabelInputPaste) ;
7381 }
7482
75- disconnectedCallback ( ) : void {
76- this . #labelBox?. removeEventListener ( 'keydown' , this . #handleLabelInputKeyDown) ;
77- this . #labelBox?. removeEventListener ( 'paste' , this . #handleLabelInputPaste) ;
83+ #handleLabelInputKeyUp( ) : void {
84+ // If the label changed on key up, dispatch label changed event
85+ const labelBoxTextContent = this . #labelBox?. textContent ?? '' ;
86+ if ( labelBoxTextContent !== this . #label) {
87+ this . #label = labelBoxTextContent ;
88+ this . dispatchEvent ( new EntryLabelChangeEvent ( this . #label) ) ;
89+ }
7890 }
7991
8092 #handleLabelInputKeyDown( event : KeyboardEvent ) : boolean {
93+ if ( ! this . #labelBox) {
94+ return false ;
95+ }
96+
8197 const allowedKeysAfterReachingLenLimit = [
8298 'Backspace' ,
8399 'Delete' ,
@@ -89,12 +105,13 @@ Otherwise, the entry label overlay object only gets repositioned.
89105 // Therefore, if the new key is `Enter` key, treat it
90106 // as the end of the label input and blur the input field.
91107 if ( event . key === 'Enter' || event . key === 'Escape' ) {
92- this . dispatchEvent ( new FocusEvent ( 'blur' , { bubbles : true } ) ) ;
108+ this . #labelBox . dispatchEvent ( new FocusEvent ( 'blur' , { bubbles : true } ) ) ;
93109 return false ;
94110 }
95111
96112 // If the max limit is not reached, return true
97- if ( ! this . textContent || this . textContent . length <= EntryLabelOverlay . MAX_LABEL_LENGTH ) {
113+ if ( this . #labelBox. textContent !== null &&
114+ this . #labelBox. textContent . length <= EntryLabelOverlay . MAX_LABEL_LENGTH ) {
98115 return true ;
99116 }
100117
@@ -114,21 +131,21 @@ Otherwise, the entry label overlay object only gets repositioned.
114131 event . preventDefault ( ) ;
115132
116133 const clipboardData = event . clipboardData ;
117- if ( ! clipboardData ) {
134+ if ( ! clipboardData || ! this . #labelBox ) {
118135 return ;
119136 }
120137
121138 const pastedText = clipboardData . getData ( 'text' ) ;
122139
123- const newText = this . textContent + pastedText ;
140+ const newText = this . #labelBox . textContent + pastedText ;
124141 const trimmedText = newText . slice ( 0 , EntryLabelOverlay . MAX_LABEL_LENGTH + 1 ) ;
125142
126- this . textContent = trimmedText ;
143+ this . #labelBox . textContent = trimmedText ;
127144
128145 // Reset the selection to the end
129146 const selection = window . getSelection ( ) ;
130147 const range = document . createRange ( ) ;
131- range . selectNodeContents ( this ) ;
148+ range . selectNodeContents ( this . #labelBox ) ;
132149 range . collapse ( false ) ;
133150 selection ?. removeAllRanges ( ) ;
134151 selection ?. addRange ( range ) ;
@@ -262,6 +279,9 @@ Otherwise, the entry label overlay object only gets repositioned.
262279 class ="label-box "
263280 @dblclick =${ ( ) => this . #setLabelEditabilityAndRemoveEmptyLabel( true ) }
264281 @blur =${ ( ) => this . #setLabelEditabilityAndRemoveEmptyLabel( false ) }
282+ @keydown=${ this . #handleLabelInputKeyDown}
283+ @paste=${ this . #handleLabelInputPaste}
284+ @keyup=${ this . #handleLabelInputKeyUp}
265285 contenteditable=${ this . #isLabelEditable} >
266286 </ span >
267287 < svg class ="connectorContainer ">
0 commit comments