1
1
"use strict" ;
2
2
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
3
+ /** The HTML element for highlighting code fragments. */
3
4
class HTMLCodeBlockElement extends HTMLElement {
4
- /**
5
- * A library for performing syntax highlighting.
6
- * Before running `customElements.define()`,
7
- * you need to assign it directly to `HTMLCodeBlockElement.endgine`.
8
- * Currently, only highlight.js is assumed.
9
- */
10
- static endgine ;
11
5
/**
12
6
* Returns the result of highlighting the received source code string.
7
+ * Before running `customElements.define()`,
8
+ * you need to assign it directly to `HTMLCodeBlockElement.highlight`.
13
9
* @param src - Source code string for highlight
14
- * @param options - Option for library of highlight
15
- * @returns - Object of the highlight result
10
+ * @param options - Option for highlight
11
+ * @return - Object of the highlight result
16
12
*/
17
- static highlight ( src , options ) {
18
- const { endgine } = HTMLCodeBlockElement ;
19
- if ( ! endgine ) {
20
- throw new Error ( 'The syntax highlighting engine is not set to `HTMLCodeBlockElement.endgine`.' ) ;
21
- }
22
- if (
23
- // Verifying the existence of a language
24
- options ?. language &&
25
- endgine . getLanguage ( options . language ) ) {
26
- return endgine . highlight ( src , options ) ;
27
- }
28
- return endgine . highlightAuto ( src ) ;
29
- }
13
+ static highlight = ( src , options ) => {
14
+ throw new TypeError ( 'The syntax highlighting engine is not set to `HTMLCodeBlockElement.highlight`.' ) ;
15
+ return { markup : '' } ;
16
+ } ;
17
+ /** Slot elements for Shadow DOM content */
30
18
#slots = ( ( ) => {
31
19
/**
32
20
* @param name - The value of name attribute for the slot element
33
- * @returns - The slot element
21
+ * @param id - The value of id attribute for the slot element
22
+ * @return - The slot element
34
23
*/
35
24
const mkslot = ( name , id ) => {
36
25
const slot = document . createElement ( 'slot' ) ;
@@ -46,9 +35,13 @@ class HTMLCodeBlockElement extends HTMLElement {
46
35
code : mkslot ( 'code' ) ,
47
36
} ;
48
37
} ) ( ) ;
38
+ /** Pure DOM content */
49
39
#a11yName;
40
+ /** Pure DOM content */
50
41
#copyButton;
42
+ /** Pure DOM content */
51
43
#codeBlock;
44
+ /** Pure DOM content */
52
45
#codeWrap;
53
46
/** Actual value of the accessor `value` */
54
47
#value = '' ;
@@ -61,27 +54,53 @@ class HTMLCodeBlockElement extends HTMLElement {
61
54
/** Click event handler of copy button */
62
55
#onClickButton = ( ( ) => {
63
56
let key = - 1 ;
64
- const textarea = document . createElement ( 'textarea' ) ;
65
- return ( ) => {
57
+ /**
58
+ * Write to the clipboard.
59
+ * @param value - String to be saved to the clipboard
60
+ * @return - A promise
61
+ */
62
+ const copy = ( value ) => {
63
+ if ( navigator . clipboard ) {
64
+ return navigator . clipboard . writeText ( value ) ;
65
+ }
66
+ return new Promise ( ( r ) => {
67
+ const textarea = document . createElement ( 'textarea' ) ;
68
+ textarea . value = value ;
69
+ document . body . append ( textarea ) ;
70
+ textarea . select ( ) ;
71
+ document . execCommand ( 'copy' ) ;
72
+ textarea . remove ( ) ;
73
+ r ( ) ;
74
+ } ) ;
75
+ } ;
76
+ return async ( ) => {
77
+ const value = this . #value. replace ( / \n $ / , '' ) ;
66
78
clearTimeout ( key ) ;
67
- textarea . value = this . #value. replace ( / \n $ / , '' ) ;
68
- document . body . append ( textarea ) ;
69
- textarea . select ( ) ;
70
- document . execCommand ( 'copy' ) ;
71
- textarea . remove ( ) ;
79
+ await copy ( value ) ;
80
+ this . #copyButton. classList . add ( '--copied' ) ;
72
81
this . #copyButton. textContent = 'Copied!' ;
73
82
key = window . setTimeout ( ( ) => {
83
+ this . #copyButton. classList . remove ( '--copied' ) ;
74
84
this . #copyButton. textContent = 'Copy' ;
75
85
} , 1500 ) ;
76
86
} ;
77
87
} ) ( ) ;
78
- /** Outputs the resulting syntax-highlighted markup to the DOM. */
88
+ /**
89
+ * Outputs the resulting syntax-highlighted markup to the DOM.
90
+ * @param this - instance
91
+ */
79
92
#render = function ( ) {
80
93
if ( ! this . parentNode ) {
81
94
return ;
82
95
}
96
+ const src = ( ( ) => {
97
+ if ( / [ ^ \n ] \n $ / . test ( this . #value) ) {
98
+ return `${ this . #value} \n` ;
99
+ }
100
+ return this . #value;
101
+ } ) ( ) ;
83
102
/** The resulting syntax-highlighted markup */
84
- const { value : markup } = HTMLCodeBlockElement . highlight ( this . #value , {
103
+ const { markup } = HTMLCodeBlockElement . highlight ( src , {
85
104
language : this . #language,
86
105
} ) ;
87
106
// initialize
@@ -95,22 +114,17 @@ class HTMLCodeBlockElement extends HTMLElement {
95
114
this . append ( this . #copyButton) ;
96
115
this . append ( this . #codeWrap) ;
97
116
} ;
98
- /** @returns - Syntax Highlighted Source Code */
117
+ /** @return - Syntax Highlighted Source Code */
99
118
get value ( ) {
100
119
return this . #value;
101
120
}
102
121
set value ( src ) {
103
- if ( / \n $ / . test ( src ) ) {
104
- this . #value = `${ src } \n` ;
105
- }
106
- else {
107
- this . #value = src ;
108
- }
122
+ this . #value = String ( src ) ;
109
123
this . #render( ) ;
110
124
}
111
125
/**
112
126
* The name of code block
113
- * @returns - The value of the label attribute
127
+ * @return - The value of the label attribute
114
128
*/
115
129
get label ( ) {
116
130
return this . #label;
@@ -128,7 +142,7 @@ class HTMLCodeBlockElement extends HTMLElement {
128
142
}
129
143
/**
130
144
* Language Mode
131
- * @returns - The value of the language attribute
145
+ * @return - The value of the language attribute
132
146
*/
133
147
get language ( ) {
134
148
return this . #language;
@@ -146,7 +160,7 @@ class HTMLCodeBlockElement extends HTMLElement {
146
160
}
147
161
/**
148
162
* Flag to display the UI
149
- * @returns - With or without controls attribute
163
+ * @return - With or without controls attribute
150
164
* */
151
165
get controls ( ) {
152
166
return this . #controls;
@@ -250,7 +264,7 @@ class HTMLCodeBlockElement extends HTMLElement {
250
264
/* -------------------------------------------------------------------------
251
265
* Hard private props initialize
252
266
* ---------------------------------------------------------------------- */
253
- this . #value = ( this . textContent || '' ) . replace ( / ^ \n / , '' ) ;
267
+ this . #value = ( this . textContent || '' ) . replace ( / ^ \n / , '' ) . replace ( / \n $ / , '' ) ;
254
268
this . #label = a11yName . textContent || '' ;
255
269
this . #language = this . getAttribute ( 'language' ) || '' ;
256
270
this . #controls = this . getAttribute ( 'controls' ) !== null ;
0 commit comments