1- export namespace Endgine {
2- type Options = {
1+ export type EndgineProps = {
2+ src : string ;
3+ options ?: {
34 /** Language Mode Name */
4- language : string ,
5- }
6-
7- type Result = {
8- markup : string ,
5+ language : string ;
96 } ;
7+ } ;
8+ export type EndgineFunction = ( props : EndgineProps ) => {
9+ markup : string ;
10+ } ;
1011
11- export type callback = ( src : string , options ?: Options ) => Result ;
12- }
13-
14- /** The HTML element for highlighting code fragments. */
1512export default class HTMLCodeBlockElement extends HTMLElement {
13+ static #defaultEndgine: EndgineFunction = ( { src} ) => ( {
14+ markup : src ,
15+ } ) ;
16+
17+ static #endgine = HTMLCodeBlockElement . #defaultEndgine;
18+
1619 /**
1720 * Returns the result of highlighting the received source code string.
1821 * Before running `customElements.define()`,
1922 * you need to assign it directly to `HTMLCodeBlockElement.highlight`.
20- * @param src - Source code string for highlight
21- * @param options - Option for highlight
22- * @return - Object of the highlight result
2323 */
24- static highlight : Endgine . callback = ( src : string , options : any ) => {
25- throw new TypeError ( 'The syntax highlighting engine is not set to `HTMLCodeBlockElement.highlight`.' ) ;
26- return { markup : '' } ;
24+ static get highlight ( ) : EndgineFunction {
25+ const endgine = HTMLCodeBlockElement . #endgine;
26+
27+ if ( endgine === HTMLCodeBlockElement . #defaultEndgine) {
28+ throw new TypeError (
29+ 'The syntax highlighting engine is not set to `HTMLCodeBlockElement.highlight`.'
30+ ) ;
31+ }
32+
33+ return endgine ;
34+ }
35+
36+ static set highlight ( endgine : EndgineFunction ) {
37+ HTMLCodeBlockElement . #endgine = endgine ;
38+ }
39+
40+ static #createSlotElement = ( {
41+ name,
42+ id = '' ,
43+ } : {
44+ name : string ;
45+ id ?: string ;
46+ } ) : HTMLSlotElement => {
47+ const slot = document . createElement ( 'slot' ) ;
48+
49+ slot . name = name ;
50+
51+ if ( id ) {
52+ slot . id = id ;
53+ }
54+
55+ return slot ;
2756 } ;
2857
2958 /** Observer to monitor the editing of the content of this element. */
@@ -38,36 +67,19 @@ export default class HTMLCodeBlockElement extends HTMLElement {
3867 slot . remove ( ) ;
3968 }
4069
41- this . #value = ( this . textContent || this . getAttribute ( 'value' ) || '' ) . replace ( / ^ \n / , '' ) . replace ( / \n $ / , '' ) ;
70+ this . #value = ( this . textContent || this . getAttribute ( 'value' ) || '' )
71+ . replace ( / ^ \n / , '' )
72+ . replace ( / \n $ / , '' ) ;
4273
4374 this . #render( ) ;
4475 } ) ;
4576
4677 /** Slot elements for Shadow DOM content */
47- #slots = ( ( ) => {
48- /**
49- * @param name - The value of name attribute for the slot element
50- * @param id - The value of id attribute for the slot element
51- * @return - The slot element
52- */
53- const mkslot = ( name : string , id : string = '' ) => {
54- const slot = document . createElement ( 'slot' ) ;
55-
56- slot . name = name ;
57-
58- if ( id ) {
59- slot . id = id ;
60- }
61-
62- return slot ;
63- } ;
64-
65- return {
66- name : mkslot ( 'name' , 'name' ) ,
67- copyButton : mkslot ( 'copy-button' ) ,
68- code : mkslot ( 'code' ) ,
69- } ;
70- } ) ( ) ;
78+ #slots = {
79+ name : HTMLCodeBlockElement . #createSlotElement( { name : 'name' , id : 'name' } ) ,
80+ copyButton : HTMLCodeBlockElement . #createSlotElement( { name : 'copy-button' } ) ,
81+ code : HTMLCodeBlockElement . #createSlotElement( { name : 'code' } ) ,
82+ } ;
7183
7284 /**
7385 * True when rendered at least once.
@@ -99,16 +111,16 @@ export default class HTMLCodeBlockElement extends HTMLElement {
99111 #codeWrap: HTMLPreElement ;
100112
101113 /** Actual value of the accessor `value` */
102- #value: string = '' ;
114+ #value = '' ;
103115
104116 /** Actual value of the accessor `label` */
105- #label: string = '' ;
117+ #label = '' ;
106118
107119 /** Actual value of the accessor `language` */
108- #language: string = '' ;
120+ #language = '' ;
109121
110122 /** Actual value of the accessor `controls` */
111- #controls: boolean = false ;
123+ #controls = false ;
112124
113125 /** Click event handler of copy button */
114126 #onClickButton = ( ( ) => {
@@ -151,11 +163,8 @@ export default class HTMLCodeBlockElement extends HTMLElement {
151163 } ;
152164 } ) ( ) ;
153165
154- /**
155- * Outputs the resulting syntax-highlighted markup to the DOM.
156- * @param this - instance
157- */
158- #render = function ( this : HTMLCodeBlockElement ) {
166+ /** Outputs the resulting syntax-highlighted markup to the DOM. */
167+ #render( ) : void {
159168 if ( ! this . parentNode ) {
160169 return ;
161170 }
@@ -171,8 +180,11 @@ export default class HTMLCodeBlockElement extends HTMLElement {
171180 } ) ( ) ;
172181
173182 /** The resulting syntax-highlighted markup */
174- const { markup} = HTMLCodeBlockElement . highlight ( src , {
175- language : this . #language,
183+ const { markup} = HTMLCodeBlockElement . highlight ( {
184+ src,
185+ options : {
186+ language : this . #language,
187+ } ,
176188 } ) ;
177189
178190 // initialize
@@ -191,11 +203,11 @@ export default class HTMLCodeBlockElement extends HTMLElement {
191203 }
192204
193205 /** @return - Syntax Highlighted Source Code */
194- get value ( ) {
206+ get value ( ) : string {
195207 return this . #value;
196208 }
197209
198- set value ( src : any ) {
210+ set value ( src : unknown ) {
199211 this . #value = String ( src ) ;
200212 this . #render( ) ;
201213 }
@@ -204,18 +216,16 @@ export default class HTMLCodeBlockElement extends HTMLElement {
204216 * The accessible name of code block
205217 * @return - The value of the label attribute
206218 */
207- get label ( ) {
219+ get label ( ) : string {
208220 return this . #label;
209221 }
210222
211- set label ( value : any ) {
223+ set label ( value : unknown ) {
212224 if (
213225 this . #label === value ||
214- (
215- this . #label === '' &&
226+ ( this . #label === '' &&
216227 this . getAttribute ( 'label' ) === null &&
217- value === null
218- )
228+ value === null )
219229 ) {
220230 return ;
221231 }
@@ -235,18 +245,16 @@ export default class HTMLCodeBlockElement extends HTMLElement {
235245 * Language name
236246 * @return - The value of the language attribute
237247 */
238- get language ( ) {
248+ get language ( ) : string {
239249 return this . #language;
240250 }
241251
242- set language ( value : any ) {
252+ set language ( value : unknown ) {
243253 if (
244254 this . #language === value ||
245- (
246- this . #language === '' &&
255+ ( this . #language === '' &&
247256 this . getAttribute ( 'language' ) === null &&
248- value === null
249- )
257+ value === null )
250258 ) {
251259 return ;
252260 }
@@ -266,7 +274,7 @@ export default class HTMLCodeBlockElement extends HTMLElement {
266274 * The flag to display the UI
267275 * @return - With or without controls attribute
268276 * */
269- get controls ( ) {
277+ get controls ( ) : boolean {
270278 return this . #controls;
271279 }
272280
@@ -286,19 +294,15 @@ export default class HTMLCodeBlockElement extends HTMLElement {
286294 this . #render( ) ;
287295 }
288296
289- static get observedAttributes ( ) {
290- return [
291- 'label' ,
292- 'language' ,
293- 'controls' ,
294- ] ;
297+ static get observedAttributes ( ) : string [ ] {
298+ return [ 'label' , 'language' , 'controls' ] ;
295299 }
296300
297301 attributeChangedCallback (
298- attrName : string ,
299- oldValue : string ,
300- newValue : string ,
301- ) {
302+ attrName : string ,
303+ oldValue : string ,
304+ newValue : string
305+ ) : void {
302306 if ( oldValue === newValue ) {
303307 return ;
304308 }
@@ -319,11 +323,8 @@ export default class HTMLCodeBlockElement extends HTMLElement {
319323 }
320324 }
321325
322- connectedCallback ( ) {
323- if (
324- this . #rendered === false &&
325- this . #value === ''
326- ) {
326+ connectedCallback ( ) : void {
327+ if ( this . #rendered === false && this . #value === '' ) {
327328 this . #value = this . textContent || '' ;
328329 }
329330
@@ -374,7 +375,6 @@ export default class HTMLCodeBlockElement extends HTMLElement {
374375 return pre ;
375376 } ) ( ) ;
376377
377-
378378 /* -------------------------------------------------------------------------
379379 * Setup Shadow DOM contents
380380 * ---------------------------------------------------------------------- */
@@ -407,11 +407,12 @@ export default class HTMLCodeBlockElement extends HTMLElement {
407407 shadowRoot . append ( a11yNamePrefix ) ;
408408 shadowRoot . append ( container ) ;
409409
410-
411410 /* -------------------------------------------------------------------------
412411 * Hard private props initialize
413412 * ---------------------------------------------------------------------- */
414- this . #value = ( this . textContent || '' ) . replace ( / ^ \n / , '' ) . replace ( / \n $ / , '' ) ;
413+ this . #value = ( this . textContent || '' )
414+ . replace ( / ^ \n / , '' )
415+ . replace ( / \n $ / , '' ) ;
415416 this . #label = a11yName . textContent || '' ;
416417 this . #language = this . getAttribute ( 'language' ) || '' ;
417418 this . #controls = this . getAttribute ( 'controls' ) !== null ;
0 commit comments