From b6eddf0821cef2ca9ff4f90003bf3162f0dfdcdc Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Fri, 10 Mar 2017 20:31:54 -0800 Subject: [PATCH 1/7] Add dataApi.copyImage for copying image to boostnote storage on an image to boostnote storage on an image doropped into CodeEditor --- browser/components/CodeEditor.js | 11 ++++++--- browser/components/MarkdownEditor.js | 3 ++- browser/main/Detail/MarkdownNoteDetail.js | 1 + browser/main/lib/dataApi/copyImage.js | 27 +++++++++++++++++++++++ 4 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 browser/main/lib/dataApi/copyImage.js diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index d17987ce..e430d097 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -2,6 +2,7 @@ import React, { PropTypes } from 'react' import _ from 'lodash' import CodeMirror from 'codemirror' import path from 'path' +import copyImage from 'browser/main/lib/dataApi/copyImage' CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js' @@ -166,9 +167,13 @@ export default class CodeEditor extends React.Component { handleDropImage (e) { e.preventDefault() - const imagePath = e.dataTransfer.files[0].path - const filename = path.basename(imagePath) - const imageMd = `![${encodeURI(filename)}](${encodeURI(imagePath)})` + let imagePath = e.dataTransfer.files[0].path + let filename = path.basename(imagePath) + const cachedStorageList = JSON.parse(localStorage.getItem('storages')) + const storagePath = _.find(cachedStorageList, {key: this.props.storageKey}) + + copyImage(imagePath, this.props.storageKey) + const imageMd = `![${encodeURI(filename)}](${storagePath.path}/images/${encodeURI(filename)})` this.insertImage(imageMd) } diff --git a/browser/components/MarkdownEditor.js b/browser/components/MarkdownEditor.js index a00a87bf..99c04e4f 100644 --- a/browser/components/MarkdownEditor.js +++ b/browser/components/MarkdownEditor.js @@ -180,7 +180,7 @@ class MarkdownEditor extends React.Component { } render () { - let { className, value, config } = this.props + let { className, value, config, storageKey } = this.props let editorFontSize = parseInt(config.editor.fontSize, 10) if (!(editorFontSize > 0 && editorFontSize < 101)) editorFontSize = 14 @@ -210,6 +210,7 @@ class MarkdownEditor extends React.Component { fontSize={editorFontSize} indentType={config.editor.indentType} indentSize={editorIndentSize} + storageKey={storageKey} onChange={(e) => this.handleChange(e)} onBlur={(e) => this.handleBlur(e)} /> diff --git a/browser/main/Detail/MarkdownNoteDetail.js b/browser/main/Detail/MarkdownNoteDetail.js index 8b5a15ed..f1aff024 100644 --- a/browser/main/Detail/MarkdownNoteDetail.js +++ b/browser/main/Detail/MarkdownNoteDetail.js @@ -292,6 +292,7 @@ class MarkdownNoteDetail extends React.Component { styleName='body-noteEditor' config={config} value={this.state.note.content} + storageKey={this.state.note.storage} onChange={(e) => this.handleChange(e)} ignorePreviewPointerEvents={this.props.ignorePreviewPointerEvents} /> diff --git a/browser/main/lib/dataApi/copyImage.js b/browser/main/lib/dataApi/copyImage.js new file mode 100644 index 00000000..2a3bc65c --- /dev/null +++ b/browser/main/lib/dataApi/copyImage.js @@ -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 From aae2bddd327acfe086a4dbe31cb124f9e7ac1dce Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Fri, 10 Mar 2017 20:33:11 -0800 Subject: [PATCH 2/7] Improve the Promise of copyImage --- browser/components/CodeEditor.js | 15 +++++++-------- browser/main/lib/dataApi/copyImage.js | 20 +++++++++++++------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index e430d097..4b439b91 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -169,17 +169,16 @@ export default class CodeEditor extends React.Component { e.preventDefault() let imagePath = e.dataTransfer.files[0].path let filename = path.basename(imagePath) - const cachedStorageList = JSON.parse(localStorage.getItem('storages')) - const storagePath = _.find(cachedStorageList, {key: this.props.storageKey}) - copyImage(imagePath, this.props.storageKey) - const imageMd = `![${encodeURI(filename)}](${storagePath.path}/images/${encodeURI(filename)})` - this.insertImage(imageMd) + copyImage(imagePath, this.props.storageKey).then((imagePathInTheStorage) => { + let imageMd = `![${encodeURI(filename)}](${imagePathInTheStorage})` + this.insertImageMd(imageMd) + }) } - insertImage (imageMd) { - const textarea = this.editor.getInputField() - textarea.value = textarea.value.substr(0, textarea.selectionStart) + imageMd + textarea.value.substr(textarea.selectionEnd) + insertImageMd (imageMd) { + const cm = this.editor + cm.setValue(cm.getValue() + imageMd) } render () { diff --git a/browser/main/lib/dataApi/copyImage.js b/browser/main/lib/dataApi/copyImage.js index 2a3bc65c..072b91eb 100644 --- a/browser/main/lib/dataApi/copyImage.js +++ b/browser/main/lib/dataApi/copyImage.js @@ -15,13 +15,19 @@ function copyImage (filePath, storageKey) { 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) + return new Promise((resolve, reject) => { + try { + 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) + resolve(`${encodeURI(targetStorage.path)}/images/${encodeURI(imageName)}`) + } + catch(e) { + return reject(e) + } + }) } module.exports = copyImage From b2187b72abbb0ce095b609804f6e3094f6f002bf Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Tue, 21 Feb 2017 23:24:02 +0900 Subject: [PATCH 3/7] Fix let to const --- browser/main/lib/dataApi/copyImage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser/main/lib/dataApi/copyImage.js b/browser/main/lib/dataApi/copyImage.js index 072b91eb..0532c333 100644 --- a/browser/main/lib/dataApi/copyImage.js +++ b/browser/main/lib/dataApi/copyImage.js @@ -6,7 +6,7 @@ const sander = require('sander') function copyImage (filePath, storageKey) { let targetStorage try { - let cachedStorageList = JSON.parse(localStorage.getItem('storages')) + const cachedStorageList = JSON.parse(localStorage.getItem('storages')) if (!_.isArray(cachedStorageList)) throw new Error('Target storage doesn\'t exist.') targetStorage = _.find(cachedStorageList, {key: storageKey}) From 928df018dc2f27bbc8d3205efe37ead4edc85cca Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Tue, 21 Feb 2017 23:41:52 +0900 Subject: [PATCH 4/7] Fix the assginment of targetStorage --- browser/main/lib/dataApi/copyImage.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/browser/main/lib/dataApi/copyImage.js b/browser/main/lib/dataApi/copyImage.js index 0532c333..e814c41b 100644 --- a/browser/main/lib/dataApi/copyImage.js +++ b/browser/main/lib/dataApi/copyImage.js @@ -4,16 +4,18 @@ const _ = require('lodash') const sander = require('sander') function copyImage (filePath, storageKey) { - let targetStorage - try { - const cachedStorageList = JSON.parse(localStorage.getItem('storages')) - if (!_.isArray(cachedStorageList)) throw new Error('Target storage doesn\'t exist.') + const targetStorage = (() => { + try { + const 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) - } + const storage = _.find(cachedStorageList, {key: storageKey}) + if (storage == null) throw new Error('Target storage doesn\'t exist.') + return storage + } catch (e) { + return Promise.reject(e) + } + })() return new Promise((resolve, reject) => { try { From 65573bd4db892fdac709acb6a207cf49237268a5 Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Tue, 21 Feb 2017 23:48:23 +0900 Subject: [PATCH 5/7] Add comments --- browser/main/lib/dataApi/copyImage.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/browser/main/lib/dataApi/copyImage.js b/browser/main/lib/dataApi/copyImage.js index e814c41b..75e2f4a4 100644 --- a/browser/main/lib/dataApi/copyImage.js +++ b/browser/main/lib/dataApi/copyImage.js @@ -3,6 +3,12 @@ const path = require('path') const _ = require('lodash') const sander = require('sander') +/** + * @description To copy an image and return the path. + * @param {String} filePath + * @param {String} storageKey + * @return {String} an image path + */ function copyImage (filePath, storageKey) { const targetStorage = (() => { try { From 0f3230110c1f55222a3c6e073179a2c2babd8870 Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Fri, 10 Mar 2017 20:40:11 -0800 Subject: [PATCH 6/7] Change to use const instead of let --- browser/components/CodeEditor.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 4b439b91..7156f67f 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -167,11 +167,11 @@ export default class CodeEditor extends React.Component { handleDropImage (e) { e.preventDefault() - let imagePath = e.dataTransfer.files[0].path - let filename = path.basename(imagePath) + const imagePath = e.dataTransfer.files[0].path + const filename = path.basename(imagePath) copyImage(imagePath, this.props.storageKey).then((imagePathInTheStorage) => { - let imageMd = `![${encodeURI(filename)}](${imagePathInTheStorage})` + const imageMd = `![${encodeURI(filename)}](${imagePathInTheStorage})` this.insertImageMd(imageMd) }) } From 893a92c87b61c0795d340d03786da75190c82035 Mon Sep 17 00:00:00 2001 From: asmsuechan Date: Sun, 19 Mar 2017 12:34:31 -0700 Subject: [PATCH 7/7] Fix from reviews --- browser/main/lib/dataApi/copyImage.js | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/browser/main/lib/dataApi/copyImage.js b/browser/main/lib/dataApi/copyImage.js index 75e2f4a4..8c6f1787 100644 --- a/browser/main/lib/dataApi/copyImage.js +++ b/browser/main/lib/dataApi/copyImage.js @@ -10,29 +10,20 @@ const sander = require('sander') * @return {String} an image path */ function copyImage (filePath, storageKey) { - const targetStorage = (() => { + return new Promise((resolve, reject) => { try { const cachedStorageList = JSON.parse(localStorage.getItem('storages')) if (!_.isArray(cachedStorageList)) throw new Error('Target storage doesn\'t exist.') - const storage = _.find(cachedStorageList, {key: storageKey}) - if (storage == null) throw new Error('Target storage doesn\'t exist.') - return storage - } catch (e) { - return Promise.reject(e) - } - })() + if (storage === undefined) throw new Error('Target storage doesn\'t exist.') + const targetStorage = storage - return new Promise((resolve, reject) => { - try { 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) resolve(`${encodeURI(targetStorage.path)}/images/${encodeURI(imageName)}`) - } - catch(e) { + } catch (e) { return reject(e) } })