-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add dataApi.copyImage for copying image to boostnote storage on an image dropped into CodeEditor #293
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
Merged
sota1235
merged 7 commits into
BoostIO:master
from
asmsuechan:copy-image-on-dropped-into-CodeEditor
Jun 4, 2017
Merged
Add dataApi.copyImage for copying image to boostnote storage on an image dropped into CodeEditor #293
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b6eddf0
Add dataApi.copyImage for copying image to boostnote storage on an im…
asmsuechan aae2bdd
Improve the Promise of copyImage
asmsuechan b2187b7
Fix let to const
asmsuechan 928df01
Fix the assginment of targetStorage
asmsuechan 65573bd
Add comments
asmsuechan 0f32301
Change to use const instead of let
asmsuechan 893a92c
Fix from reviews
asmsuechan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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' | ||
|
|
||
|
|
@@ -168,13 +169,16 @@ export default class CodeEditor extends React.Component { | |
| e.preventDefault() | ||
| const imagePath = e.dataTransfer.files[0].path | ||
| const filename = path.basename(imagePath) | ||
| const imageMd = `})` | ||
| this.insertImage(imageMd) | ||
|
|
||
| copyImage(imagePath, this.props.storageKey).then((imagePathInTheStorage) => { | ||
| const imageMd = `` | ||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What the mean of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's CodeMirror and used this name overall of Boostnote. |
||
| cm.setValue(cm.getValue() + imageMd) | ||
| } | ||
|
|
||
| render () { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| const fs = require('fs') | ||
| 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) { | ||
| 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 === undefined) throw new Error('Target storage doesn\'t exist.') | ||
| const targetStorage = storage | ||
|
|
||
| const inputImage = fs.createReadStream(filePath) | ||
| const imageName = path.basename(filePath) | ||
| 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good Rename!