@@ -2,14 +2,14 @@ import React from 'react'
2
2
import styled from '../../../lib/styled'
3
3
import copy from 'copy-to-clipboard'
4
4
import Icon from '../Icon'
5
- import { mdiContentCopy } from '@mdi/js'
5
+ import { mdiContentCopy , mdiContentSave } from '@mdi/js'
6
6
import { flexCenter } from '../../../lib/styled/styleFunctions'
7
-
7
+ import { downloadBlob } from '../../../lib/download' ;
8
8
const CodeFenceContainer = styled . div `
9
9
position: relative;
10
10
`
11
11
12
- const CodeFenceCopyButton = styled . button `
12
+ const CodeFenceButton = styled . button `
13
13
position: absolute;
14
14
top: 0;
15
15
right: 0;
@@ -36,7 +36,6 @@ const CodeFenceCopyButton = styled.button`
36
36
color: ${ ( { theme } ) => theme . navButtonActiveColor } ;
37
37
}
38
38
`
39
-
40
39
const CodeFence = ( props : React . HTMLProps < HTMLPreElement > = { } ) => {
41
40
if ( props . className != null && props . className ! . includes ( 'CodeMirror' ) ) {
42
41
const otherProps = { ...props }
@@ -45,14 +44,27 @@ const CodeFence = (props: React.HTMLProps<HTMLPreElement> = {}) => {
45
44
return (
46
45
< CodeFenceContainer >
47
46
< pre { ...otherProps } />
48
- < CodeFenceCopyButton
47
+ < CodeFenceButton
49
48
onClick = { ( ) => {
50
49
copy ( rawContent )
51
50
} }
52
51
title = 'Copy to Clipboard'
53
52
>
54
53
< Icon path = { mdiContentCopy } />
55
- </ CodeFenceCopyButton >
54
+ </ CodeFenceButton >
55
+ { rawContent && < CodeFenceButton
56
+ onClick = { ( ) => {
57
+ var blob = new Blob ( [ rawContent ] , { type : "text/plain;charset=utf-8" } ) ;
58
+ var ext = "" ;
59
+ if ( props . className ! . includes ( 'language-' ) )
60
+ ext = "." + / l a n g u a g e - ( .+ ?) $ / . exec ( props . className ! ) ! [ 1 ]
61
+ downloadBlob ( blob , "snippet" + ext ) ;
62
+ } }
63
+ title = 'Save to File'
64
+ style = { { right : '30px' } }
65
+ >
66
+ < Icon path = { mdiContentSave } />
67
+ </ CodeFenceButton > }
56
68
</ CodeFenceContainer >
57
69
)
58
70
}
0 commit comments