@@ -2,6 +2,17 @@ import React from 'react';
22
33import { escapeTextForHTMLContent , getOrFind } from '../libs/utils' ;
44
5+ const saveData = ( function ( ) {
6+ const a = document . createElement ( "a" ) ;
7+ a . style . display = "none" ;
8+ document . body . appendChild ( a ) ;
9+ return function saveData ( blob , fileName ) {
10+ const url = window . URL . createObjectURL ( blob ) ;
11+ a . href = url ;
12+ a . download = fileName ;
13+ a . click ( )
14+ } ;
15+ } ) ( ) ;
516/*
617
718<!-- begin snippet: js hide: false console: true babel: false -->
@@ -70,6 +81,10 @@ ${indent4(mainHTML.content)}
7081
7182}
7283
84+ function selectAndCopy ( e ) {
85+ navigator . clipboard . writeText ( e . target . textContent ) ;
86+ }
87+
7388function openInCodepen ( data ) {
7489 const files = data . files ;
7590 const mainHTML = getOrFind ( files , 'index.html' , 'html' ) ;
@@ -170,6 +185,14 @@ export default class Export extends React.Component {
170185 exportToJSFiddle = ( ) => {
171186 openInJSFiddle ( this . props . data ) ;
172187 }
188+ saveToFile = ( ) => {
189+ const { data} = this . props ;
190+ const { asModule} = this . state ;
191+ const html = makeHTML ( data , asModule ) ;
192+ const blob = new Blob ( [ html ] , { type : 'text/html' } ) ;
193+ const filename = `jsgist-${ data . name } .html` ;
194+ saveData ( blob , filename ) ;
195+ }
173196 render ( ) {
174197 const { data} = this . props ;
175198 const { asModule} = this . state ;
@@ -181,15 +204,26 @@ export default class Export extends React.Component {
181204 < div >
182205 < div > < input type = "radio" id = "export-as-module" checked = { asModule } onChange = { _ => this . onChange ( true ) } /> < label htmlFor = "export-as-module" > As es6 module</ label > </ div >
183206 < div > < input type = "radio" id = "export-as-script" checked = { ! asModule } onChange = { _ => this . onChange ( false ) } /> < label htmlFor = "export-as-script" > As script</ label > </ div >
184- < p > S.O. does not support es6 modules yet so picking "As Module" puts the code in a <script> in the HTML.</ p >
207+ < p > S.O. does not support es6 modules yet so picking "As Module" puts the code in a <script> in the HTML area.</ p >
208+ </ div >
209+ < div className = "copy-text" >
210+ < pre className = "layout-scrollbar" onClick = { selectAndCopy } style = { { userSelect : 'all' , overflow : 'auto' , height : '5em' } } > { makeSnippet ( data , asModule ) } </ pre >
211+ < div className = "copy-buttons" >
212+ < button type = "button" onClick = { ( ) => navigator . clipboard . writeText ( makeSnippet ( data , asModule ) ) } > copy</ button >
213+ </ div >
185214 </ div >
186- < pre className = "layout-scrollbar" style = { { userSelect : 'all' , overflow : 'auto' , height : '5em' } } > { makeSnippet ( data , asModule ) } </ pre >
187215 < p > HTML (copy the code below paste into a file)</ p >
188216 < div >
189217 < div > < input type = "radio" id = "export-as-module" checked = { asModule } onChange = { _ => this . onChange ( true ) } /> < label htmlFor = "export-as-module" > As es6 module</ label > </ div >
190218 < div > < input type = "radio" id = "export-as-script" checked = { ! asModule } onChange = { _ => this . onChange ( false ) } /> < label htmlFor = "export-as-script" > As script</ label > </ div >
191219 </ div >
192- < pre className = "layout-scrollbar" style = { { userSelect : 'all' , overflow : 'auto' , height : '5em' } } > { makeHTML ( data , asModule ) } </ pre >
220+ < div className = "copy-text" >
221+ < pre className = "layout-scrollbar" onClick = { selectAndCopy } style = { { userSelect : 'all' , overflow : 'auto' , height : '5em' } } > { makeHTML ( data , asModule ) } </ pre >
222+ < div className = "copy-buttons" >
223+ < button type = "button" onClick = { this . saveToFile } > save</ button >
224+ < button type = "button" onClick = { ( ) => navigator . clipboard . writeText ( makeHTML ( data , asModule ) ) } > copy</ button >
225+ </ div >
226+ </ div >
193227 </ div >
194228 ) ;
195229 }
0 commit comments