Skip to content

Commit 07a604f

Browse files
committed
add copy/save
1 parent b730863 commit 07a604f

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/components/App.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,14 @@ hr {
751751
justify-content: flex-end;
752752
padding-top: 1em;
753753
}
754+
.copy-text {
755+
position: relative;
756+
}
757+
.copy-text .copy-buttons {
758+
position: absolute;
759+
right: 0;
760+
top: 0;
761+
}
754762

755763
@media (max-width: 740px) {
756764
.head {

src/components/Export.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ import React from 'react';
22

33
import {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+
7388
function 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 &lt;script&gt; in the HTML.</p>
207+
<p>S.O. does not support es6 modules yet so picking "As Module" puts the code in a &lt;script&gt; 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

Comments
 (0)