Skip to content

Commit 1079fe1

Browse files
authored
Merge pull request #2 from iamspark1e/feat-gallery_preview_insert
fix: add image from gallery now will insert at the cursor point & fix…
2 parents 3bd10a6 + fd04eee commit 1079fe1

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/Control.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ export default class Control extends React.Component {
77
constructor(props) {
88
super(props)
99
this.state = {
10-
mediaControlId: ""
10+
mediaControlId: "",
11+
insertPos: null,
12+
editor: null
1113
}
1214
}
1315

@@ -42,17 +44,30 @@ export default class Control extends React.Component {
4244
let path = nextProps.mediaPaths.get(this.state.mediaControlId)
4345
if (path && nextState.mediaControlId !== "") {
4446
var filename = path.replace(/^.*[\\\/]/, '').split('.')[0]
45-
nextProps.onChange(nextProps.value + `![${filename || ""}](${path})`)
46-
this.setState({
47-
mediaControlId: ""
48-
})
47+
this.insertCustomMark(this.state.insertPos, `![${filename || ""}](${path})`)
4948
}
5049
}
5150

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()
5366
let newId = Date.now()
5467
this.setState({
5568
mediaControlId: newId,
69+
insertPos: pos,
70+
editor: cm
5671
}, () => {
5772
this.props.onOpenMediaLibrary({
5873
controlID: newId,

src/Preview.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import PropTypes from 'prop-types';
22
import React from 'react';
33
import { marked } from 'marked';
44

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})
613
return <div className='markdown-body' dangerouslySetInnerHTML={{__html: marked.parse(value)}}></div>;
714
}
815

0 commit comments

Comments
 (0)