11"use strict" ;
22Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
3+ /** The HTML element for highlighting code fragments. */
34class 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 ;
115 /**
126 * 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`.
139 * @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
1612 */
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 */
3018 #slots = ( ( ) => {
3119 /**
3220 * @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
3423 */
3524 const mkslot = ( name , id ) => {
3625 const slot = document . createElement ( 'slot' ) ;
@@ -46,9 +35,13 @@ class HTMLCodeBlockElement extends HTMLElement {
4635 code : mkslot ( 'code' ) ,
4736 } ;
4837 } ) ( ) ;
38+ /** Pure DOM content */
4939 #a11yName;
40+ /** Pure DOM content */
5041 #copyButton;
42+ /** Pure DOM content */
5143 #codeBlock;
44+ /** Pure DOM content */
5245 #codeWrap;
5346 /** Actual value of the accessor `value` */
5447 #value = '' ;
@@ -61,27 +54,53 @@ class HTMLCodeBlockElement extends HTMLElement {
6154 /** Click event handler of copy button */
6255 #onClickButton = ( ( ) => {
6356 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 $ / , '' ) ;
6678 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' ) ;
7281 this . #copyButton. textContent = 'Copied!' ;
7382 key = window . setTimeout ( ( ) => {
83+ this . #copyButton. classList . remove ( '--copied' ) ;
7484 this . #copyButton. textContent = 'Copy' ;
7585 } , 1500 ) ;
7686 } ;
7787 } ) ( ) ;
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+ */
7992 #render = function ( ) {
8093 if ( ! this . parentNode ) {
8194 return ;
8295 }
96+ const src = ( ( ) => {
97+ if ( / [ ^ \n ] \n $ / . test ( this . #value) ) {
98+ return `${ this . #value} \n` ;
99+ }
100+ return this . #value;
101+ } ) ( ) ;
83102 /** The resulting syntax-highlighted markup */
84- const { value : markup } = HTMLCodeBlockElement . highlight ( this . #value , {
103+ const { markup } = HTMLCodeBlockElement . highlight ( src , {
85104 language : this . #language,
86105 } ) ;
87106 // initialize
@@ -95,22 +114,17 @@ class HTMLCodeBlockElement extends HTMLElement {
95114 this . append ( this . #copyButton) ;
96115 this . append ( this . #codeWrap) ;
97116 } ;
98- /** @returns - Syntax Highlighted Source Code */
117+ /** @return - Syntax Highlighted Source Code */
99118 get value ( ) {
100119 return this . #value;
101120 }
102121 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 ) ;
109123 this . #render( ) ;
110124 }
111125 /**
112126 * The name of code block
113- * @returns - The value of the label attribute
127+ * @return - The value of the label attribute
114128 */
115129 get label ( ) {
116130 return this . #label;
@@ -128,7 +142,7 @@ class HTMLCodeBlockElement extends HTMLElement {
128142 }
129143 /**
130144 * Language Mode
131- * @returns - The value of the language attribute
145+ * @return - The value of the language attribute
132146 */
133147 get language ( ) {
134148 return this . #language;
@@ -146,7 +160,7 @@ class HTMLCodeBlockElement extends HTMLElement {
146160 }
147161 /**
148162 * Flag to display the UI
149- * @returns - With or without controls attribute
163+ * @return - With or without controls attribute
150164 * */
151165 get controls ( ) {
152166 return this . #controls;
@@ -250,7 +264,7 @@ class HTMLCodeBlockElement extends HTMLElement {
250264 /* -------------------------------------------------------------------------
251265 * Hard private props initialize
252266 * ---------------------------------------------------------------------- */
253- this . #value = ( this . textContent || '' ) . replace ( / ^ \n / , '' ) ;
267+ this . #value = ( this . textContent || '' ) . replace ( / ^ \n / , '' ) . replace ( / \n $ / , '' ) ;
254268 this . #label = a11yName . textContent || '' ;
255269 this . #language = this . getAttribute ( 'language' ) || '' ;
256270 this . #controls = this . getAttribute ( 'controls' ) !== null ;
0 commit comments