Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preview images on editor #1633

Closed
wants to merge 14 commits into from
51 changes: 51 additions & 0 deletions browser/components/CodeEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ export default class CodeEditor extends React.Component {
if (needRefresh) {
this.editor.refresh()
}
if (this.props.previewImage) {
setTimeout(this.previewImage(this.editor, this.props), 500)
}
}

setMode (mode) {
Expand All @@ -232,6 +235,9 @@ export default class CodeEditor extends React.Component {
if (this.props.onChange) {
this.props.onChange(e)
}
if (this.props.previewImage) {
setTimeout(this.previewImage(this.editor, this.props), 500)
}
}

moveCursorTo (row, col) {
Expand Down Expand Up @@ -277,6 +283,9 @@ export default class CodeEditor extends React.Component {

insertImageMd (imageMd) {
this.editor.replaceSelection(imageMd)
if (this.props.previewImage) {
setTimeout(this.previewImage(this.editor, this.props), 500)
}
}

handlePaste (editor, e) {
Expand Down Expand Up @@ -343,6 +352,48 @@ export default class CodeEditor extends React.Component {
})
}

previewImage (editor, props) {
editor.operation(function () {
const storagePath = findStorage(props.storageKey).path
const lastline = editor.lineCount()
for (let i = 0; i < lastline; ++i) {
const lineValue = editor.getLine(i)
var imageMdRe = /\!\[.+\]\(\\:storage\\(.+?)\)/g
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not platform independent. I tried to develope something platform indepnendent in #1852

var imageMd = imageMdRe.exec(lineValue)
if (!imageMd) {
editor.lineInfo(i).handle.widgets = null
continue
}

var div = document.createElement('div')
var img = div.appendChild(document.createElement('img'))
const imageDir = `${path.join(storagePath, 'images', path.basename(imageMd[1]))}`
img.setAttribute('src', imageDir)
img.setAttribute('style', 'max-width:100%')
div.className = 'inline-image'
var widgetNode
try {
widgetNode = editor.lineInfo(i).handle.widgets[0].node
} catch (e) {
widgetNode = undefined
}
if (typeof widgetNode !== 'undefined') {
var widgetSrc = widgetNode.childNodes[0].getAttribute('src')
if (widgetSrc !== imageDir) {
editor.lineInfo(i).handle.widgets = null
editor.addLineWidget(i, div, {coverGutter: false, noHScroll: true})
}
} else {
editor.addLineWidget(i, div, {coverGutter: false, noHScroll: true})
var evt = document.createEvent('UIEvent')
evt.initUIEvent('resize', true, true, window, 1)
document.dispatchEvent(evt)
}
editor.refresh()
}
})
}

decodeResponse (response) {
const headers = response.headers
const _charset = headers.has('content-type')
Expand Down
1 change: 1 addition & 0 deletions browser/components/MarkdownEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ class MarkdownEditor extends React.Component {
scrollPastEnd={config.editor.scrollPastEnd}
storageKey={storageKey}
fetchUrlTitle={config.editor.fetchUrlTitle}
previewImage={config.editor.previewImage}
onChange={(e) => this.handleChange(e)}
onBlur={(e) => this.handleBlur(e)}
/>
Expand Down
1 change: 1 addition & 0 deletions browser/components/MarkdownSplitEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class MarkdownSplitEditor extends React.Component {
indentSize={editorIndentSize}
scrollPastEnd={config.editor.scrollPastEnd}
fetchUrlTitle={config.editor.fetchUrlTitle}
previewImage={config.editor.previewImage}
storageKey={storageKey}
onChange={this.handleOnChange.bind(this)}
onScroll={this.handleScroll.bind(this)}
Expand Down
1 change: 1 addition & 0 deletions browser/main/Detail/SnippetNoteDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ class SnippetNoteDetail extends React.Component {
keyMap={config.editor.keyMap}
scrollPastEnd={config.editor.scrollPastEnd}
fetchUrlTitle={config.editor.fetchUrlTitle}
previewImage={config.editor.previewImage}
onChange={(e) => this.handleCodeChange(index)(e)}
ref={'code-' + index}
/>
Expand Down
3 changes: 2 additions & 1 deletion browser/main/lib/ConfigManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export const DEFAULT_CONFIG = {
switchPreview: 'BLUR', // Available value: RIGHTCLICK, BLUR
scrollPastEnd: false,
type: 'SPLIT',
fetchUrlTitle: true
fetchUrlTitle: true,
previewImage: true
},
preview: {
fontSize: '14',
Expand Down
14 changes: 13 additions & 1 deletion browser/main/modals/PreferencesModal/UiTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ class UiTab extends React.Component {
switchPreview: this.refs.editorSwitchPreview.value,
keyMap: this.refs.editorKeyMap.value,
scrollPastEnd: this.refs.scrollPastEnd.checked,
fetchUrlTitle: this.refs.editorFetchUrlTitle.checked
fetchUrlTitle: this.refs.editorFetchUrlTitle.checked,
previewImage: this.refs.editorPreviewImage.checked
},
preview: {
fontSize: this.refs.previewFontSize.value,
Expand Down Expand Up @@ -341,6 +342,17 @@ class UiTab extends React.Component {
</label>
</div>

<div styleName='group-checkBoxSection'>
<label>
<input onChange={(e) => this.handleUIChange(e)}
checked={this.state.config.editor.previewImage}
ref='editorPreviewImage'
type='checkbox'
/>&nbsp;
Preview images on editor
</label>
</div>

<div styleName='group-header2'>Preview</div>
<div styleName='group-section'>
<div styleName='group-section-label'>
Expand Down