mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 17:56:25 +00:00
Add dataApi.copyImage for copying image to boostnote storage on an image to boostnote storage on an image doropped into CodeEditor
This commit is contained in:
@@ -2,6 +2,7 @@ import React, { PropTypes } from 'react'
|
|||||||
import _ from 'lodash'
|
import _ from 'lodash'
|
||||||
import CodeMirror from 'codemirror'
|
import CodeMirror from 'codemirror'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
|
import copyImage from 'browser/main/lib/dataApi/copyImage'
|
||||||
|
|
||||||
CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js'
|
CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js'
|
||||||
|
|
||||||
@@ -166,9 +167,13 @@ export default class CodeEditor extends React.Component {
|
|||||||
|
|
||||||
handleDropImage (e) {
|
handleDropImage (e) {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
const imagePath = e.dataTransfer.files[0].path
|
let imagePath = e.dataTransfer.files[0].path
|
||||||
const filename = path.basename(imagePath)
|
let filename = path.basename(imagePath)
|
||||||
const imageMd = `})`
|
const cachedStorageList = JSON.parse(localStorage.getItem('storages'))
|
||||||
|
const storagePath = _.find(cachedStorageList, {key: this.props.storageKey})
|
||||||
|
|
||||||
|
copyImage(imagePath, this.props.storageKey)
|
||||||
|
const imageMd = `})`
|
||||||
this.insertImage(imageMd)
|
this.insertImage(imageMd)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -180,7 +180,7 @@ class MarkdownEditor extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render () {
|
render () {
|
||||||
let { className, value, config } = this.props
|
let { className, value, config, storageKey } = this.props
|
||||||
|
|
||||||
let editorFontSize = parseInt(config.editor.fontSize, 10)
|
let editorFontSize = parseInt(config.editor.fontSize, 10)
|
||||||
if (!(editorFontSize > 0 && editorFontSize < 101)) editorFontSize = 14
|
if (!(editorFontSize > 0 && editorFontSize < 101)) editorFontSize = 14
|
||||||
@@ -210,6 +210,7 @@ class MarkdownEditor extends React.Component {
|
|||||||
fontSize={editorFontSize}
|
fontSize={editorFontSize}
|
||||||
indentType={config.editor.indentType}
|
indentType={config.editor.indentType}
|
||||||
indentSize={editorIndentSize}
|
indentSize={editorIndentSize}
|
||||||
|
storageKey={storageKey}
|
||||||
onChange={(e) => this.handleChange(e)}
|
onChange={(e) => this.handleChange(e)}
|
||||||
onBlur={(e) => this.handleBlur(e)}
|
onBlur={(e) => this.handleBlur(e)}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -292,6 +292,7 @@ class MarkdownNoteDetail extends React.Component {
|
|||||||
styleName='body-noteEditor'
|
styleName='body-noteEditor'
|
||||||
config={config}
|
config={config}
|
||||||
value={this.state.note.content}
|
value={this.state.note.content}
|
||||||
|
storageKey={this.state.note.storage}
|
||||||
onChange={(e) => this.handleChange(e)}
|
onChange={(e) => this.handleChange(e)}
|
||||||
ignorePreviewPointerEvents={this.props.ignorePreviewPointerEvents}
|
ignorePreviewPointerEvents={this.props.ignorePreviewPointerEvents}
|
||||||
/>
|
/>
|
||||||
|
|||||||
27
browser/main/lib/dataApi/copyImage.js
Normal file
27
browser/main/lib/dataApi/copyImage.js
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
const fs = require('fs')
|
||||||
|
const path = require('path')
|
||||||
|
const _ = require('lodash')
|
||||||
|
const sander = require('sander')
|
||||||
|
|
||||||
|
function copyImage (filePath, storageKey) {
|
||||||
|
let targetStorage
|
||||||
|
try {
|
||||||
|
let cachedStorageList = JSON.parse(localStorage.getItem('storages'))
|
||||||
|
if (!_.isArray(cachedStorageList)) throw new Error('Target storage doesn\'t exist.')
|
||||||
|
|
||||||
|
targetStorage = _.find(cachedStorageList, {key: storageKey})
|
||||||
|
if (targetStorage == null) throw new Error('Target storage doesn\'t exist.')
|
||||||
|
} catch (e) {
|
||||||
|
return Promise.reject(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
//return resolveStorageData(targetStorage)
|
||||||
|
|
||||||
|
const inputImage = fs.createReadStream(filePath)
|
||||||
|
const imageName = path.basename(filePath)
|
||||||
|
sander.mkdirSync(`${targetStorage.path}/images`)
|
||||||
|
const outputImage = fs.createWriteStream(path.join(targetStorage.path, 'images', imageName))
|
||||||
|
inputImage.pipe(outputImage)
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = copyImage
|
||||||
Reference in New Issue
Block a user