@@ -2,7 +2,7 @@ import './editor.scss';
2
2
3
3
import React , { useRef , useEffect , useCallback , useState , useContext } from 'react' ;
4
4
import { useMount } from 'ahooks' ;
5
- import useCodemirror from '../../components/useCodemirror' ;
5
+ import useCodemirror , { CodemirrorObj } from '../../components/useCodemirror' ;
6
6
import { FileContext } from './store/sidbar' ;
7
7
import { EditorContext } from 'views/src/pages/editor/store/editor' ;
8
8
@@ -12,12 +12,16 @@ import MdView from '../../components/md-view';
12
12
import ToolBar from './components/tool-bar' ;
13
13
import Footer from './components/footer' ;
14
14
15
- import { success , error } from '../.. /utils/message' ;
15
+ import { success , error } from 'views/src /utils/message' ;
16
16
import { isFilePath } from 'views/src/utils/tools' ;
17
17
18
- import { fileEvent , FS_SAVE } from '../.. /utils/event' ;
18
+ import { fileEvent , FS_SAVE } from 'views/src /utils/event' ;
19
19
import { pandora } from 'views/src/services/pandora' ;
20
20
21
+ import { blobToBase64 , getFileName } from 'shared/utils/img' ;
22
+ import { uploader } from 'shared/utils/chunk' ;
23
+ import { PROTOCOL_IMG } from 'shared/common/constant' ;
24
+
21
25
function Editor ( ) {
22
26
const [ storeState ] = useContext ( FileContext ) ;
23
27
const {
@@ -30,14 +34,56 @@ function Editor() {
30
34
} = useCodemirror ( ) ;
31
35
32
36
/* -------------------------------------------------------------------------- */
33
- /* store 全局状态 */
37
+ /* handle pic */
34
38
/* -------------------------------------------------------------------------- */
35
39
const [ , dispatch ] = useContext ( EditorContext ) ;
36
40
useEffect ( ( ) => {
37
41
dispatch ( {
38
42
type : 'storeeditor' ,
39
43
payload : editor
40
44
} ) ;
45
+ if ( editor ) {
46
+ // @ts -ignore
47
+ editor . on ( 'paste' , ( instance : CodemirrorObj [ 'editor' ] , e : ClipboardEvent ) => {
48
+ if ( e . clipboardData && e . clipboardData . items ) {
49
+ const items = e . clipboardData . items ;
50
+ for ( let i = 0 , len = items . length ; i < len ; i ++ ) {
51
+ let item = items [ i ] ;
52
+ if ( item . kind !== 'file' ) {
53
+ return ;
54
+ }
55
+ let pasteFile = item . getAsFile ( ) ;
56
+ if ( ! pasteFile ) {
57
+ return ;
58
+ }
59
+ if ( pasteFile . size > 0 && pasteFile . type . match ( '^image/' ) ) {
60
+ blobToBase64 ( pasteFile ) . then ( data => {
61
+ const imageName = getFileName ( ) ;
62
+ // 拒绝压缩
63
+ uploader ( {
64
+ send ( data : any ) {
65
+ pandora . ipcRenderer . invoke ( 'pandora:storeImage' , {
66
+ name : imageName ,
67
+ base64 : data
68
+ } ) . then ( ( res : { success : boolean , data : string } ) => {
69
+ const doc = editor . getDoc ( ) ;
70
+ const curs = doc . getCursor ( ) ;
71
+ doc . replaceRange (
72
+ `<img src="${ PROTOCOL_IMG } :\/\/${ res . data } " width="100%" height="100%" />` ,
73
+ { line : curs . line , ch : curs . ch }
74
+ ) ;
75
+ } ) . catch ( err => {
76
+ console . warn ( err ) ;
77
+ } ) ;
78
+ }
79
+ } , data as string ) ;
80
+ } ) ;
81
+ }
82
+ return ;
83
+ }
84
+ }
85
+ } ) ;
86
+ }
41
87
} , [ editor ] ) ;
42
88
43
89
/* -------------------------------------------------------------------------- */
0 commit comments