diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 29c3748b..1c9a3799 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -8,6 +8,8 @@ import copyImage from 'browser/main/lib/dataApi/copyImage' import { findStorage } from 'browser/lib/findStorage' import fs from 'fs' import eventEmitter from 'browser/main/lib/eventEmitter' +import iconv from 'iconv-lite' +const { ipcRenderer } = require('electron') CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js' @@ -32,8 +34,13 @@ export default class CodeEditor extends React.Component { constructor (props) { super(props) + this.scrollHandler = _.debounce(this.handleScroll.bind(this), 100, {leading: false, trailing: true}) this.changeHandler = (e) => this.handleChange(e) + this.focusHandler = () => { + ipcRenderer.send('editor:focused', true) + } this.blurHandler = (editor, e) => { + ipcRenderer.send('editor:focused', false) if (e == null) return null let el = e.relatedTarget while (el != null) { @@ -81,7 +88,6 @@ export default class CodeEditor extends React.Component { } } }) - this.scrollHandler = _.debounce(this.handleScroll.bind(this), 100, {leading: false, trailing: true}) } componentDidMount () { @@ -139,6 +145,7 @@ export default class CodeEditor extends React.Component { this.setMode(this.props.mode) + this.editor.on('focus', this.focusHandler) this.editor.on('blur', this.blurHandler) this.editor.on('change', this.changeHandler) this.editor.on('paste', this.pasteHandler) @@ -162,6 +169,7 @@ export default class CodeEditor extends React.Component { } componentWillUnmount () { + this.editor.off('focus', this.focusHandler) this.editor.off('blur', this.blurHandler) this.editor.off('change', this.changeHandler) this.editor.off('paste', this.pasteHandler) @@ -317,7 +325,7 @@ export default class CodeEditor extends React.Component { fetch(pastedTxt, { method: 'get' }).then((response) => { - return (response.text()) + return this.decodeResponse(response) }).then((response) => { const parsedResponse = (new window.DOMParser()).parseFromString(response, 'text/html') const value = editor.getValue() @@ -335,6 +343,31 @@ export default class CodeEditor extends React.Component { }) } + decodeResponse (response) { + const headers = response.headers + const _charset = headers.has('content-type') + ? this.extractContentTypeCharset(headers.get('content-type')) + : undefined + return response.arrayBuffer().then((buff) => { + return new Promise((resolve, reject) => { + try { + const charset = _charset !== undefined && iconv.encodingExists(_charset) ? _charset : 'utf-8' + resolve(iconv.decode(new Buffer(buff), charset).toString()) + } catch (e) { + reject(e) + } + }) + }) + } + + extractContentTypeCharset (contentType) { + return contentType.split(';').filter((str) => { + return str.trim().toLowerCase().startsWith('charset') + }).map((str) => { + return str.replace(/['"]/g, '').split('=')[1] + })[0] + } + render () { const { className, fontSize } = this.props let fontFamily = this.props.fontFamily diff --git a/browser/components/MarkdownPreview.js b/browser/components/MarkdownPreview.js index ddda74bb..6f5c8093 100755 --- a/browser/components/MarkdownPreview.js +++ b/browser/components/MarkdownPreview.js @@ -223,6 +223,7 @@ export default class MarkdownPreview extends React.Component { return `
+ ${styles} diff --git a/browser/components/NoteItem.js b/browser/components/NoteItem.js index 2c93dc18..253faa81 100644 --- a/browser/components/NoteItem.js +++ b/browser/components/NoteItem.js @@ -123,7 +123,11 @@ NoteItem.propTypes = { title: PropTypes.string.isrequired, tags: PropTypes.array, isStarred: PropTypes.bool.isRequired, - isTrashed: PropTypes.bool.isRequired + isTrashed: PropTypes.bool.isRequired, + blog: { + blogLink: PropTypes.string, + blogId: PropTypes.number + } }), handleNoteClick: PropTypes.func.isRequired, handleNoteContextMenu: PropTypes.func.isRequired, diff --git a/browser/lib/markdown-it-sanitize-html.js b/browser/lib/markdown-it-sanitize-html.js new file mode 100644 index 00000000..beec9566 --- /dev/null +++ b/browser/lib/markdown-it-sanitize-html.js @@ -0,0 +1,23 @@ +'use strict' + +import sanitizeHtml from 'sanitize-html' + +module.exports = function sanitizePlugin (md, options) { + options = options || {} + + md.core.ruler.after('linkify', 'sanitize_inline', state => { + for (let tokenIdx = 0; tokenIdx < state.tokens.length; tokenIdx++) { + if (state.tokens[tokenIdx].type === 'html_block') { + state.tokens[tokenIdx].content = sanitizeHtml(state.tokens[tokenIdx].content, options) + } + if (state.tokens[tokenIdx].type === 'inline') { + const inlineTokens = state.tokens[tokenIdx].children + for (let childIdx = 0; childIdx < inlineTokens.length; childIdx++) { + if (inlineTokens[childIdx].type === 'html_inline') { + inlineTokens[childIdx].content = sanitizeHtml(inlineTokens[childIdx].content, options) + } + } + } + } + }) +} diff --git a/browser/lib/markdown.js b/browser/lib/markdown.js index e75e13ee..d1f32640 100644 --- a/browser/lib/markdown.js +++ b/browser/lib/markdown.js @@ -1,4 +1,5 @@ import markdownit from 'markdown-it' +import sanitize from './markdown-it-sanitize-html' import emoji from 'markdown-it-emoji' import math from '@rokt33r/markdown-it-math' import _ from 'lodash' @@ -51,6 +52,18 @@ class Markdown { const updatedOptions = Object.assign(defaultOptions, options) this.md = markdownit(updatedOptions) + + // Sanitize use rinput before other plugins + this.md.use(sanitize, { + allowedTags: ['img', 'iframe'], + allowedAttributes: { + '*': ['alt', 'style'], + 'img': ['src', 'width', 'height'], + 'iframe': ['src', 'width', 'height', 'frameborder', 'allowfullscreen'] + }, + allowedIframeHostnames: ['www.youtube.com'] + }) + this.md.use(emoji, { shortcuts: {} }) diff --git a/browser/main/Detail/SnippetNoteDetail.js b/browser/main/Detail/SnippetNoteDetail.js index dbef37ca..6f8bbb3d 100644 --- a/browser/main/Detail/SnippetNoteDetail.js +++ b/browser/main/Detail/SnippetNoteDetail.js @@ -343,6 +343,7 @@ class SnippetNoteDetail extends React.Component { handleKeyDown (e) { switch (e.keyCode) { + // tab key case 9: if (e.ctrlKey && !e.shiftKey) { e.preventDefault() @@ -355,6 +356,7 @@ class SnippetNoteDetail extends React.Component { this.focusEditor() } break + // L key case 76: { const isSuper = global.process.platform === 'darwin' @@ -366,6 +368,7 @@ class SnippetNoteDetail extends React.Component { } } break + // T key case 84: { const isSuper = global.process.platform === 'darwin' diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index 8c3e8790..b1f2d092 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -13,10 +13,13 @@ import searchFromNotes from 'browser/lib/search' import fs from 'fs' import path from 'path' import { hashHistory } from 'react-router' +import copy from 'copy-to-clipboard' import AwsMobileAnalyticsConfig from 'browser/main/lib/AwsMobileAnalyticsConfig' +import markdown from '../../lib/markdown' const { remote } = require('electron') const { Menu, MenuItem, dialog } = remote +const WP_POST_PATH = '/wp/v2/posts' function sortByCreatedAt (a, b) { return new Date(b.createdAt) - new Date(a.createdAt) @@ -70,6 +73,7 @@ class NoteList extends React.Component { this.getNoteFolder = this.getNoteFolder.bind(this) this.getViewType = this.getViewType.bind(this) this.restoreNote = this.restoreNote.bind(this) + this.copyNoteLink = this.copyNoteLink.bind(this) // TODO: not Selected noteKeys but SelectedNote(for reusing) this.state = { @@ -257,27 +261,38 @@ class NoteList extends React.Component { handleNoteListKeyDown (e) { if (e.metaKey || e.ctrlKey) return true + // A key if (e.keyCode === 65 && !e.shiftKey) { e.preventDefault() ee.emit('top:new-note') } + // D key if (e.keyCode === 68) { e.preventDefault() this.deleteNote() } + // E key if (e.keyCode === 69) { e.preventDefault() ee.emit('detail:focus') } - if (e.keyCode === 38) { + // F or S key + if (e.keyCode === 70 || e.keyCode === 83) { + e.preventDefault() + ee.emit('top:focus-search') + } + + // UP or K key + if (e.keyCode === 38 || e.keyCode === 75) { e.preventDefault() this.selectPriorNote() } - if (e.keyCode === 40) { + // DOWN or J key + if (e.keyCode === 40 || e.keyCode === 74) { e.preventDefault() this.selectNextNote() } @@ -458,6 +473,10 @@ class NoteList extends React.Component { const deleteLabel = 'Delete Note' const cloneNote = 'Clone Note' const restoreNote = 'Restore Note' + const copyNoteLink = 'Copy Note Link' + const publishLabel = 'Publish Blog' + const updateLabel = 'Update Blog' + const openBlogLabel = 'Open Blog' const menu = new Menu() if (!location.pathname.match(/\/starred|\/trash/)) { @@ -482,6 +501,28 @@ class NoteList extends React.Component { label: cloneNote, click: this.cloneNote.bind(this) })) + menu.append(new MenuItem({ + label: copyNoteLink, + click: this.copyNoteLink(note) + })) + if (note.type === 'MARKDOWN_NOTE') { + if (note.blog && note.blog.blogLink && note.blog.blogId) { + menu.append(new MenuItem({ + label: updateLabel, + click: this.publishMarkdown.bind(this) + })) + menu.append(new MenuItem({ + label: openBlogLabel, + click: () => this.openBlog.bind(this)(note) + })) + } else { + menu.append(new MenuItem({ + label: publishLabel, + click: this.publishMarkdown.bind(this) + })) + } + } + menu.popup() } @@ -630,6 +671,117 @@ class NoteList extends React.Component { }) } + copyNoteLink (note) { + const noteLink = `[${note.title}](${note.storage}-${note.key})` + return copy(noteLink) + } + + save (note) { + const { dispatch } = this.props + dataApi + .updateNote(note.storage, note.key, note) + .then((note) => { + dispatch({ + type: 'UPDATE_NOTE', + note: note + }) + }) + } + + publishMarkdown () { + if (this.pendingPublish) { + clearTimeout(this.pendingPublish) + } + this.pendingPublish = setTimeout(() => { + this.publishMarkdownNow() + }, 1000) + } + + publishMarkdownNow () { + const {selectedNoteKeys} = this.state + const notes = this.notes.map((note) => Object.assign({}, note)) + const selectedNotes = findNotesByKeys(notes, selectedNoteKeys) + const firstNote = selectedNotes[0] + const config = ConfigManager.get() + const {address, token, authMethod, username, password} = config.blog + let authToken = '' + if (authMethod === 'USER') { + authToken = `Basic ${window.btoa(`${username}:${password}`)}` + } else { + authToken = `Bearer ${token}` + } + const contentToRender = firstNote.content.replace(`# ${firstNote.title}`, '') + var data = { + title: firstNote.title, + content: markdown.render(contentToRender), + status: 'publish' + } + + let url = '' + let method = '' + if (firstNote.blog && firstNote.blog.blogId) { + url = `${address}${WP_POST_PATH}/${firstNote.blog.blogId}` + method = 'PUT' + } else { + url = `${address}${WP_POST_PATH}` + method = 'POST' + } + // eslint-disable-next-line no-undef + fetch(url, { + method: method, + body: JSON.stringify(data), + headers: { + 'Authorization': authToken, + 'Content-Type': 'application/json' + } + }).then(res => res.json()) + .then(response => { + if (_.isNil(response.link) || _.isNil(response.id)) { + return Promise.reject() + } + firstNote.blog = { + blogLink: response.link, + blogId: response.id + } + this.save(firstNote) + this.confirmPublish(firstNote) + }) + .catch((error) => { + console.error(error) + this.confirmPublishError() + }) + } + + confirmPublishError () { + const { remote } = electron + const { dialog } = remote + const alertError = { + type: 'warning', + message: 'Publish Failed', + detail: 'Check and update your blog setting and try again.', + buttons: ['Confirm'] + } + dialog.showMessageBox(remote.getCurrentWindow(), alertError) + } + + confirmPublish (note) { + const buttonIndex = dialog.showMessageBox(remote.getCurrentWindow(), { + type: 'warning', + message: 'Publish Succeeded', + detail: `${note.title} is published at ${note.blog.blogLink}`, + buttons: ['Confirm', 'Open Blog'] + }) + + if (buttonIndex === 1) { + this.openBlog(note) + } + } + + openBlog (note) { + const { shell } = electron + shell.openExternal(note.blog.blogLink) + } + importFromFile () { const options = { filters: [ diff --git a/browser/main/SideNav/StorageItem.js b/browser/main/SideNav/StorageItem.js index 5d7e6005..0df7a98e 100644 --- a/browser/main/SideNav/StorageItem.js +++ b/browser/main/SideNav/StorageItem.js @@ -191,33 +191,16 @@ class StorageItem extends React.Component { dropNote (storage, folder, dispatch, location, noteData) { noteData = noteData.filter((note) => folder.key !== note.folder) if (noteData.length === 0) return - const newNoteData = noteData.map((note) => Object.assign({}, note, {storage: storage, folder: folder.key})) Promise.all( - newNoteData.map((note) => dataApi.createNote(storage.key, note)) + noteData.map((note) => dataApi.moveNote(note.storage, note.key, storage.key, folder.key)) ) .then((createdNoteData) => { - createdNoteData.forEach((note) => { + createdNoteData.forEach((newNote) => { dispatch({ - type: 'UPDATE_NOTE', - note: note - }) - }) - }) - .catch((err) => { - console.error(`error on create notes: ${err}`) - }) - .then(() => { - return Promise.all( - noteData.map((note) => dataApi.deleteNote(note.storage, note.key)) - ) - }) - .then((deletedNoteData) => { - deletedNoteData.forEach((note) => { - dispatch({ - type: 'DELETE_NOTE', - storageKey: note.storageKey, - noteKey: note.noteKey + type: 'MOVE_NOTE', + originNote: noteData.find((note) => note.content === newNote.content), + note: newNote }) }) }) diff --git a/browser/main/StatusBar/StatusBar.styl b/browser/main/StatusBar/StatusBar.styl index e055dc0d..9f189fec 100644 --- a/browser/main/StatusBar/StatusBar.styl +++ b/browser/main/StatusBar/StatusBar.styl @@ -21,20 +21,19 @@ color white .zoom - display none - // navButtonColor() - // color rgba(0,0,0,.54) - // height 20px - // display flex - // padding 0 - // align-items center - // background-color transparent - // &:hover - // color $ui-active-color - // &:active - // color $ui-active-color - // span - // margin-left 5px + navButtonColor() + color rgba(0,0,0,.54) + height 20px + display flex + padding 0 + align-items center + background-color transparent + &:hover + color $ui-active-color + &:active + color $ui-active-color + span + margin-left 5px .update navButtonColor() diff --git a/browser/main/TopBar/TopBar.styl b/browser/main/TopBar/TopBar.styl index eb0fc12f..0956571f 100644 --- a/browser/main/TopBar/TopBar.styl +++ b/browser/main/TopBar/TopBar.styl @@ -40,6 +40,32 @@ $control-height = 34px padding-bottom 2px background-color $ui-noteList-backgroundColor +.control-search-input-clear + height 16px + width 16px + position absolute + right 40px + top 10px + z-index 300 + border none + background-color transparent + color #999 + &:hover .control-search-input-clear-tooltip + opacity 1 + +.control-search-input-clear-tooltip + tooltip() + position fixed + pointer-events none + top 50px + left 433px + z-index 200 + padding 5px + line-height normal + border-radius 2px + opacity 0 + transition 0.1s + .control-search-optionList position fixed z-index 200 @@ -207,4 +233,4 @@ body[data-theme="solarized-dark"] background-color $ui-solarized-dark-noteList-backgroundColor input background-color $ui-solarized-dark-noteList-backgroundColor - color $ui-solarized-dark-text-color \ No newline at end of file + color $ui-solarized-dark-text-color diff --git a/browser/main/TopBar/index.js b/browser/main/TopBar/index.js index 6b4a118e..246ef614 100644 --- a/browser/main/TopBar/index.js +++ b/browser/main/TopBar/index.js @@ -36,6 +36,17 @@ class TopBar extends React.Component { ee.off('code:init', this.codeInitHandler) } + handleSearchClearButton (e) { + const { router } = this.context + this.setState({ + search: '', + isSearching: false + }) + this.refs.search.childNodes[0].blur + router.push('/searched') + e.preventDefault() + } + handleKeyDown (e) { // reset states this.setState({ @@ -43,6 +54,23 @@ class TopBar extends React.Component { isIME: false }) + // Clear search on ESC + if (e.keyCode === 27) { + return this.handleSearchClearButton(e) + } + + // Next note on DOWN key + if (e.keyCode === 40) { + ee.emit('list:next') + e.preventDefault() + } + + // Prev note on UP key + if (e.keyCode === 38) { + ee.emit('list:prior') + e.preventDefault() + } + // When the key is an alphabet, del, enter or ctr if (e.keyCode <= 90 || e.keyCode >= 186 && e.keyCode <= 222) { this.setState({ @@ -114,10 +142,12 @@ class TopBar extends React.Component { } handleOnSearchFocus () { + const el = this.refs.search.childNodes[0] if (this.state.isSearching) { - this.refs.search.childNodes[0].blur() + el.blur() } else { - this.refs.search.childNodes[0].focus() + el.focus() + el.setSelectionRange(0, el.value.length) } } @@ -150,15 +180,15 @@ class TopBar extends React.Component { type='text' className='searchInput' /> + {this.state.search !== '' && + + } - {this.state.search > 0 && - - } - {location.pathname === '/trashed' ? '' diff --git a/browser/main/lib/ConfigManager.js b/browser/main/lib/ConfigManager.js index 7080105c..1c4d955b 100644 --- a/browser/main/lib/ConfigManager.js +++ b/browser/main/lib/ConfigManager.js @@ -50,6 +50,14 @@ export const DEFAULT_CONFIG = { latexBlockClose: '$$', scrollPastEnd: false, smartQuotes: true + }, + blog: { + type: 'wordpress', // Available value: wordpress, add more types in the future plz + address: 'http://wordpress.com/wp-json', + authMethod: 'JWT', // Available value: JWT, USER + token: '', + username: '', + password: '' } } @@ -152,6 +160,7 @@ function set (updates) { function assignConfigValues (originalConfig, rcConfig) { const config = Object.assign({}, DEFAULT_CONFIG, originalConfig, rcConfig) config.hotkey = Object.assign({}, DEFAULT_CONFIG.hotkey, originalConfig.hotkey, rcConfig.hotkey) + config.blog = Object.assign({}, DEFAULT_CONFIG.blog, originalConfig.blog, rcConfig.blog) config.ui = Object.assign({}, DEFAULT_CONFIG.ui, originalConfig.ui, rcConfig.ui) config.editor = Object.assign({}, DEFAULT_CONFIG.editor, originalConfig.editor, rcConfig.editor) config.preview = Object.assign({}, DEFAULT_CONFIG.preview, originalConfig.preview, rcConfig.preview) diff --git a/browser/main/lib/dataApi/copyImage.js b/browser/main/lib/dataApi/copyImage.js index ae79f8fb..6a79b8b7 100644 --- a/browser/main/lib/dataApi/copyImage.js +++ b/browser/main/lib/dataApi/copyImage.js @@ -3,19 +3,20 @@ const path = require('path') const { findStorage } = require('browser/lib/findStorage') /** - * @description To copy an image and return the path. + * @description Copy an image and return the path. * @param {String} filePath * @param {String} storageKey - * @return {String} an image path + * @param {Boolean} rename create new filename or leave the old one + * @return {Promise+ {BlogAlert.message} +
+ : null + return ( +