@@ -45,13 +45,15 @@ class CodeBlock extends HTMLElement {
45
45
46
46
attributeChangedCallback ( _name : string , oldValue : string , newValue : string ) {
47
47
const updated = ! Object . is ( oldValue , newValue ) ;
48
+
48
49
this . #connected && updated && this . scheduleUpdate ( ) ;
49
50
}
50
51
51
52
scheduleUpdate ( ) {
52
53
if ( this . #updateTask) {
53
54
return ;
54
55
}
56
+
55
57
this . #updateTask = Promise . resolve ( ) . then ( ( ) => {
56
58
this . #updateTask = undefined ;
57
59
this . update ( ) ;
@@ -75,6 +77,7 @@ class CodeBlock extends HTMLElement {
75
77
result instanceof DocumentFragment ? result : parseDocumentFragmentFromString ( result ) ;
76
78
77
79
const body = highlightedCodeFragment . querySelector ( 'pre' ) ;
80
+
78
81
body ?. classList . add ( 'webchat__code-block__body' ) ;
79
82
options ?. theme && body ?. classList . add ( `webchat__code-block__theme--${ options . theme } ` ) ;
80
83
@@ -116,9 +119,9 @@ const useCodeBlockProps = (copyButtonTagName: string) => {
116
119
const [ { codeBlockTheme } ] = useStyleOptions ( ) ;
117
120
const copyButtonAltCopied = localize ( 'COPY_BUTTON_COPIED_TEXT' ) ;
118
121
const copyButtonAltCopy = localize ( 'COPY_BUTTON_TEXT' ) ;
119
-
120
122
const propsChangedEventTarget = useMemo < EventTarget > ( ( ) => new EventTarget ( ) , [ ] ) ;
121
123
const propsRef = useRef < CodeBlockReactProps > ( ) ;
124
+
122
125
useMemo ( ( ) => {
123
126
propsRef . current = Object . freeze ( {
124
127
codeBlockClass,
@@ -129,6 +132,7 @@ const useCodeBlockProps = (copyButtonTagName: string) => {
129
132
copyButtonTagName,
130
133
highlightCode
131
134
} ) ;
135
+
132
136
propsChangedEventTarget . dispatchEvent ( new CustomEvent ( 'change' ) ) ;
133
137
} , [
134
138
codeBlockClass ,
@@ -156,6 +160,7 @@ export default function useReactCodeBlockClass(copyButtonTagName: string) {
156
160
static observedAttributes = CodeBlock . observedAttributes ;
157
161
158
162
#prevProps: CodeBlockReactProps | undefined ;
163
+
159
164
get #props( ) {
160
165
return codeBlockPropsRef . current ;
161
166
}
@@ -179,6 +184,7 @@ export default function useReactCodeBlockClass(copyButtonTagName: string) {
179
184
180
185
if ( prevProps ?. copyButtonTagName !== props . copyButtonTagName || ! this . copyButtonElement ) {
181
186
const { ownerDocument : document } = this ;
187
+
182
188
this . copyButtonElement = document . createElement ( props . copyButtonTagName ) ;
183
189
this . scheduleUpdate ( ) ;
184
190
}
@@ -192,8 +198,8 @@ export default function useReactCodeBlockClass(copyButtonTagName: string) {
192
198
this . classList . add ( 'webchat__code-block' ) ;
193
199
194
200
codeBlockTarget . addEventListener ( 'change' , this . #handlePropsChange) ;
195
- this . #handlePropsChange( ) ;
196
201
202
+ this . #handlePropsChange( ) ;
197
203
super . connectedCallback ( ) ;
198
204
}
199
205
@@ -205,9 +211,11 @@ export default function useReactCodeBlockClass(copyButtonTagName: string) {
205
211
206
212
highlightCode ( ...args : Parameters < HighlightCodeFn > ) {
207
213
const [ , language , options ] = args ;
214
+
208
215
if ( ! language || ! options ) {
209
216
return defaultHighlightCode ( ...args ) ;
210
217
}
218
+
211
219
return this . #props. highlightCode ( ...args ) ;
212
220
}
213
221
} ,
0 commit comments