File tree Expand file tree Collapse file tree 2 files changed +29
-7
lines changed Expand file tree Collapse file tree 2 files changed +29
-7
lines changed Original file line number Diff line number Diff line change @@ -7,7 +7,9 @@ export default class Control extends React.Component {
7
7
constructor ( props ) {
8
8
super ( props )
9
9
this . state = {
10
- mediaControlId : ""
10
+ mediaControlId : "" ,
11
+ insertPos : null ,
12
+ editor : null
11
13
}
12
14
}
13
15
@@ -42,17 +44,30 @@ export default class Control extends React.Component {
42
44
let path = nextProps . mediaPaths . get ( this . state . mediaControlId )
43
45
if ( path && nextState . mediaControlId !== "" ) {
44
46
var filename = path . replace ( / ^ .* [ \\ \/ ] / , '' ) . split ( '.' ) [ 0 ]
45
- nextProps . onChange ( nextProps . value + `` )
46
- this . setState ( {
47
- mediaControlId : ""
48
- } )
47
+ this . insertCustomMark ( this . state . insertPos , `` )
49
48
}
50
49
}
51
50
52
- handleAppendImage = ( ) => {
51
+ insertCustomMark = ( pos , mark ) => {
52
+ if ( ! this . state . editor || ! pos ) return ;
53
+ this . state . editor . setSelection ( pos , pos )
54
+ this . state . editor . replaceSelection ( mark )
55
+
56
+ this . setState ( {
57
+ editor : this . state . editor ,
58
+ mediaControlId : "" ,
59
+ insertPos : null
60
+ } )
61
+ }
62
+
63
+ handleAppendImage = ( editor ) => {
64
+ let cm = editor . codemirror
65
+ let pos = cm . getCursor ( )
53
66
let newId = Date . now ( )
54
67
this . setState ( {
55
68
mediaControlId : newId ,
69
+ insertPos : pos ,
70
+ editor : cm
56
71
} , ( ) => {
57
72
this . props . onOpenMediaLibrary ( {
58
73
controlID : newId ,
Original file line number Diff line number Diff line change @@ -2,7 +2,14 @@ import PropTypes from 'prop-types';
2
2
import React from 'react' ;
3
3
import { marked } from 'marked' ;
4
4
5
- export default function Preview ( { value } ) {
5
+ export default function Preview ( { value, getAsset } ) {
6
+ const previewRenderer = {
7
+ image ( href , title , text ) {
8
+ return `<img src="${ getAsset ( href ) } " title="${ title } " alt="${ title || text || '' } " style="max-width:100%;display:block;" />`
9
+ }
10
+ }
11
+
12
+ marked . use ( { renderer : previewRenderer } )
6
13
return < div className = 'markdown-body' dangerouslySetInnerHTML = { { __html : marked . parse ( value ) } } > </ div > ;
7
14
}
8
15
You can’t perform that action at this time.
0 commit comments