From 8d59bd9f71c61aa8be0a87cecd86bb48eb57dbfe Mon Sep 17 00:00:00 2001 From: Nikolay Lopin Date: Sun, 25 Feb 2018 02:42:15 +0300 Subject: [PATCH 01/17] Remove redundant symbol from regexp --- browser/main/lib/dataApi/exportNote.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser/main/lib/dataApi/exportNote.js b/browser/main/lib/dataApi/exportNote.js index 3ed46b92..740baa20 100755 --- a/browser/main/lib/dataApi/exportNote.js +++ b/browser/main/lib/dataApi/exportNote.js @@ -4,7 +4,7 @@ import {findStorage} from 'browser/lib/findStorage' const fs = require('fs') const path = require('path') -const LOCAL_STORED_REGEX = /!\[(.*?)\]\(\s*?\/:storage\/(.*\.\S*?)\)/gi +const LOCAL_STORED_REGEX = /!\[(.*?)]\(\s*?\/:storage\/(.*\.\S*?)\)/gi const IMAGES_FOLDER_NAME = 'images' /** From e5825e898dc0418c04230e6f07526b2507a67b7e Mon Sep 17 00:00:00 2001 From: Nikolay Lopin Date: Sun, 25 Feb 2018 02:45:59 +0300 Subject: [PATCH 02/17] allow to copy images without generating new name --- browser/main/lib/dataApi/copyImage.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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} an image path */ -function copyImage (filePath, storageKey) { +function copyImage (filePath, storageKey, rename = true) { return new Promise((resolve, reject) => { try { const targetStorage = findStorage(storageKey) const inputImage = fs.createReadStream(filePath) const imageExt = path.extname(filePath) - const imageName = Math.random().toString(36).slice(-16) + const imageName = rename ? Math.random().toString(36).slice(-16) : path.basename(filePath, imageExt) const basename = `${imageName}${imageExt}` const imageDir = path.join(targetStorage.path, 'images') if (!fs.existsSync(imageDir)) fs.mkdirSync(imageDir) From 9473a268925938b048fc1595ab0a6e28790b6f75 Mon Sep 17 00:00:00 2001 From: Nikolay Lopin Date: Sun, 25 Feb 2018 02:47:28 +0300 Subject: [PATCH 03/17] Move images between storages together with note 1. Added new step of moving note's images to `moveNote` function 2. Changed behaviour of sidebar navigation after move finished Issue #1578 --- browser/main/SideNav/StorageItem.js | 27 +++++---------------------- browser/main/lib/dataApi/moveNote.js | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 22 deletions(-) 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/lib/dataApi/moveNote.js b/browser/main/lib/dataApi/moveNote.js index 4580062e..b37b6ac5 100644 --- a/browser/main/lib/dataApi/moveNote.js +++ b/browser/main/lib/dataApi/moveNote.js @@ -1,10 +1,12 @@ const resolveStorageData = require('./resolveStorageData') const _ = require('lodash') const path = require('path') +const fs = require('fs') const CSON = require('@rokt33r/season') const keygen = require('browser/lib/keygen') const sander = require('sander') const { findStorage } = require('browser/lib/findStorage') +const copyImage = require('./copyImage') function moveNote (storageKey, noteKey, newStorageKey, newFolderKey) { let oldStorage, newStorage @@ -65,6 +67,27 @@ function moveNote (storageKey, noteKey, newStorageKey, newFolderKey) { return noteData }) + .then(function moveImages (noteData) { + const searchImagesRegex = /!\[.*?]\(\s*?\/:storage\/(.*\.\S*?)\)/gi + let match = searchImagesRegex.exec(noteData.content) + + const moveTasks = [] + while (match != null) { + const [, filename] = match + const oldPath = path.join(oldStorage.path, 'images', filename) + moveTasks.push( + copyImage(oldPath, noteData.storage, false) + .then(() => { + fs.unlinkSync(oldPath) + }) + ) + + // find next occurence + match = searchImagesRegex.exec(noteData.content) + } + + return Promise.all(moveTasks).then(() => noteData) + }) .then(function writeAndReturn (noteData) { CSON.writeFileSync(path.join(newStorage.path, 'notes', noteData.key + '.cson'), _.omit(noteData, ['key', 'storage'])) return noteData From f3f6095d81dec384c5b8b07588b76c1b5eef927b Mon Sep 17 00:00:00 2001 From: lurong Date: Sat, 24 Feb 2018 01:01:54 +0800 Subject: [PATCH 04/17] allow publishing markdown to wordpress --- browser/components/NoteItem.js | 6 +- browser/main/NoteList/index.js | 125 +++++++++++ browser/main/lib/ConfigManager.js | 9 + browser/main/lib/dataApi/updateNote.js | 3 + browser/main/modals/PreferencesModal/Blog.js | 198 ++++++++++++++++++ browser/main/modals/PreferencesModal/index.js | 15 +- 6 files changed, 353 insertions(+), 3 deletions(-) create mode 100644 browser/main/modals/PreferencesModal/Blog.js 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/main/NoteList/index.js b/browser/main/NoteList/index.js index 8c3e8790..ff90404d 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 electron from 'electron' 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) @@ -458,6 +461,9 @@ class NoteList extends React.Component { const deleteLabel = 'Delete Note' const cloneNote = 'Clone Note' const restoreNote = 'Restore Note' + const publishLabel = 'Publish Blog' + const updateLabel = 'Update Blog' + const openBlogLabel = 'Open Blog' const menu = new Menu() if (!location.pathname.match(/\/starred|\/trash/)) { @@ -482,6 +488,24 @@ class NoteList extends React.Component { label: cloneNote, click: this.cloneNote.bind(this) })) + if (note.type === 'MARKDOWN_NOTE') { + if (note.blog) { + 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 +654,107 @@ class NoteList extends React.Component { }) } + 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() + let {address, token, authMethod, username, password} = config.blog + if (authMethod === 'USER') { + token = `Basic ${window.btoa(`${username}:${password}`)}` + } else { + token = `Bearer ${token}` + } + var data = { + title: firstNote.title, + content: markdown.render(firstNote.content), + status: 'publish' + } + + let url = '' + let method = '' + if (firstNote.blog) { + 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': token, + 'Content-Type': 'application/json' + } + }).then(res => res.json()) + .then(response => { + firstNote.blog = { + blogLink: response.link, + blogId: response.id + } + this.save(firstNote) + this.confirmPublish(firstNote) + }) + .catch(() => { + 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 + console.log(note) + shell.openExternal(note.blog.blogLink) + } + importFromFile () { const options = { filters: [ diff --git a/browser/main/lib/ConfigManager.js b/browser/main/lib/ConfigManager.js index 0c8d6ee9..e7e82a9b 100644 --- a/browser/main/lib/ConfigManager.js +++ b/browser/main/lib/ConfigManager.js @@ -49,6 +49,14 @@ export const DEFAULT_CONFIG = { latexBlockOpen: '$$', latexBlockClose: '$$', scrollPastEnd: false + }, + 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: '' } } @@ -151,6 +159,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/updateNote.js b/browser/main/lib/dataApi/updateNote.js index 2fbd52c2..147fbc06 100644 --- a/browser/main/lib/dataApi/updateNote.js +++ b/browser/main/lib/dataApi/updateNote.js @@ -30,6 +30,9 @@ function validateInput (input) { validatedInput.isPinned = !!input.isPinned } + if (!_.isNil(input.blog)) { + validatedInput.blog = input.blog + } validatedInput.type = input.type switch (input.type) { case 'MARKDOWN_NOTE': diff --git a/browser/main/modals/PreferencesModal/Blog.js b/browser/main/modals/PreferencesModal/Blog.js new file mode 100644 index 00000000..675455f7 --- /dev/null +++ b/browser/main/modals/PreferencesModal/Blog.js @@ -0,0 +1,198 @@ +import React from 'react' +import CSSModules from 'browser/lib/CSSModules' +import styles from './ConfigTab.styl' +import ConfigManager from 'browser/main/lib/ConfigManager' +import store from 'browser/main/store' +import PropTypes from 'prop-types' +import _ from 'lodash' + +const electron = require('electron') +const { shell } = electron +const ipc = electron.ipcRenderer +class Blog extends React.Component { + constructor (props) { + super(props) + + this.state = { + config: props.config, + BlogAlert: null + } + } + + handleLinkClick (e) { + shell.openExternal(e.currentTarget.href) + e.preventDefault() + } + + clearMessage () { + _.debounce(() => { + this.setState({ + BlogAlert: null + }) + }, 2000)() + } + + componentDidMount () { + this.handleSettingDone = () => { + this.setState({BlogAlert: { + type: 'success', + message: 'Successfully applied!' + }}) + } + this.handleSettingError = (err) => { + this.setState({BlogAlert: { + type: 'error', + message: err.message != null ? err.message : 'Error occurs!' + }}) + } + this.oldBlog = this.state.config.blog + ipc.addListener('APP_SETTING_DONE', this.handleSettingDone) + ipc.addListener('APP_SETTING_ERROR', this.handleSettingError) + } + + handleBlogChange (e) { + const { config } = this.state + config.blog = { + password: !_.isNil(this.refs.passwordInput) ? this.refs.passwordInput.value : config.blog.password, + username: !_.isNil(this.refs.usernameInput) ? this.refs.usernameInput.value : config.blog.username, + token: !_.isNil(this.refs.tokenInput) ? this.refs.tokenInput.value : config.blog.token, + authMethod: this.refs.authMethodDropdown.value, + address: this.refs.addressInput.value, + type: this.refs.typeDropdown.value + } + this.setState({ + config + }) + if (_.isEqual(this.oldBlog, config.blog)) { + this.props.haveToSave() + } else { + this.props.haveToSave({ + tab: 'Blog', + type: 'warning', + message: 'You have to save!' + }) + } + } + + handleSaveButtonClick (e) { + const newConfig = { + blog: this.state.config.blog + } + + ConfigManager.set(newConfig) + + store.dispatch({ + type: 'SET_UI', + config: newConfig + }) + this.clearMessage() + this.props.haveToSave() + } + + render () { + const {config, BlogAlert} = this.state + const blogAlertElement = BlogAlert != null + ?

+ {BlogAlert.message} +

+ : null + return ( +
+
+
Blog
+
+
+ Blog Type +
+
+ +
+
+
+
Blog Address
+
+ this.handleBlogChange(e)} + ref='addressInput' + value={config.blog.address} + type='text' + /> +
+
+
+ + {blogAlertElement} +
+
+
Auth
+ +
+
+ Authentication Method +
+
+ +
+
+ { config.blog.authMethod === 'JWT' && +
+
Token
+
+ this.handleBlogChange(e)} + ref='tokenInput' + value={config.blog.token} + type='text' /> +
+
+ } + { config.blog.authMethod === 'USER' && +
+
+
UserName
+
+ this.handleBlogChange(e)} + ref='usernameInput' + value={config.blog.username} + type='text' /> +
+
+
+
Password
+
+ this.handleBlogChange(e)} + ref='passwordInput' + value={config.blog.password} + type='password' /> +
+
+
+ } +
+ ) + } +} + +Blog.propTypes = { + dispatch: PropTypes.func, + haveToSave: PropTypes.func +} + +export default CSSModules(Blog, styles) diff --git a/browser/main/modals/PreferencesModal/index.js b/browser/main/modals/PreferencesModal/index.js index 6cd5badc..09ca9a4e 100644 --- a/browser/main/modals/PreferencesModal/index.js +++ b/browser/main/modals/PreferencesModal/index.js @@ -6,6 +6,7 @@ import UiTab from './UiTab' import InfoTab from './InfoTab' import Crowdfunding from './Crowdfunding' import StoragesTab from './StoragesTab' +import Blog from './Blog' import ModalEscButton from 'browser/components/ModalEscButton' import CSSModules from 'browser/lib/CSSModules' import styles from './PreferencesModal.styl' @@ -19,7 +20,8 @@ class Preferences extends React.Component { this.state = { currentTab: 'STORAGES', UIAlert: '', - HotkeyAlert: '' + HotkeyAlert: '', + BlogAlert: '' } } @@ -75,6 +77,14 @@ class Preferences extends React.Component { return ( ) + case 'BLOG': + return ( + this.setState({BlogAlert: alert})} + /> + ) case 'STORAGES': default: return ( @@ -111,7 +121,8 @@ class Preferences extends React.Component { {target: 'HOTKEY', label: 'Hotkeys', Hotkey: this.state.HotkeyAlert}, {target: 'UI', label: 'Interface', UI: this.state.UIAlert}, {target: 'INFO', label: 'About'}, - {target: 'CROWDFUNDING', label: 'Crowdfunding'} + {target: 'CROWDFUNDING', label: 'Crowdfunding'}, + {target: 'BLOG', label: 'Blog', Blog: this.state.BlogAlert} ] const navButtons = tabs.map((tab) => { From 97600e526b0188ef317195df65f7e60109fd320b Mon Sep 17 00:00:00 2001 From: lurong Date: Sat, 24 Feb 2018 02:51:01 +0800 Subject: [PATCH 05/17] remove the first occurrence of title in content when publishing to WP --- browser/main/NoteList/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index ff90404d..56101b63 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -687,9 +687,10 @@ class NoteList extends React.Component { } else { token = `Bearer ${token}` } + let contentToRender = firstNote.content.replace(`# ${firstNote.title}`, '') var data = { title: firstNote.title, - content: markdown.render(firstNote.content), + content: markdown.render(contentToRender), status: 'publish' } From aef603ed8ce45feba9e19ffffdfd412cc7c85bea Mon Sep 17 00:00:00 2001 From: lurong Date: Sun, 25 Feb 2018 22:50:51 +0800 Subject: [PATCH 06/17] rebase and prefer const in node list --- browser/main/NoteList/index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index 56101b63..3db88dd1 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -681,13 +681,14 @@ class NoteList extends React.Component { const selectedNotes = findNotesByKeys(notes, selectedNoteKeys) const firstNote = selectedNotes[0] const config = ConfigManager.get() - let {address, token, authMethod, username, password} = config.blog + const {address, token, authMethod, username, password} = config.blog + let authToken = '' if (authMethod === 'USER') { - token = `Basic ${window.btoa(`${username}:${password}`)}` + authToken = `Basic ${window.btoa(`${username}:${password}`)}` } else { - token = `Bearer ${token}` + authToken = `Bearer ${token}` } - let contentToRender = firstNote.content.replace(`# ${firstNote.title}`, '') + const contentToRender = firstNote.content.replace(`# ${firstNote.title}`, '') var data = { title: firstNote.title, content: markdown.render(contentToRender), @@ -708,7 +709,7 @@ class NoteList extends React.Component { method: method, body: JSON.stringify(data), headers: { - 'Authorization': token, + 'Authorization': authToken, 'Content-Type': 'application/json' } }).then(res => res.json()) From 0d5e57eeabd635f4016e421b1c20cd653014fb10 Mon Sep 17 00:00:00 2001 From: pfftdammitchris Date: Sun, 25 Feb 2018 07:17:01 -0800 Subject: [PATCH 07/17] Adding the ability to copy the note link from the note list --- browser/main/NoteList/index.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index 8c3e8790..8957b432 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -13,6 +13,7 @@ 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' const { remote } = require('electron') @@ -70,6 +71,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 = { @@ -458,6 +460,7 @@ class NoteList extends React.Component { const deleteLabel = 'Delete Note' const cloneNote = 'Clone Note' const restoreNote = 'Restore Note' + const copyNoteLink = 'Copy Note Link' const menu = new Menu() if (!location.pathname.match(/\/starred|\/trash/)) { @@ -482,6 +485,10 @@ class NoteList extends React.Component { label: cloneNote, click: this.cloneNote.bind(this) })) + menu.append(new MenuItem({ + label: copyNoteLink, + click: this.copyNoteLink(note) + })) menu.popup() } @@ -630,6 +637,11 @@ class NoteList extends React.Component { }) } + copyNoteLink (note) { + const noteLink = `[${note.title}](${note.storage}-${note.key})` + return copy(noteLink) + } + importFromFile () { const options = { filters: [ From 6c341d8fa5bc2e741f93be9a1fdcdcf5ea39caae Mon Sep 17 00:00:00 2001 From: pfftdammitchris Date: Sun, 25 Feb 2018 07:41:50 -0800 Subject: [PATCH 08/17] Fixes UI overlapping in the snippet editor (bottom options) --- browser/styles/index.styl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/browser/styles/index.styl b/browser/styles/index.styl index 1bd183bf..6fb208b1 100644 --- a/browser/styles/index.styl +++ b/browser/styles/index.styl @@ -5,7 +5,7 @@ $danger-color = #c9302c $danger-lighten-color = lighten(#c9302c, 5%) // Layouts -$statusBar-height = 0px +$statusBar-height = 22px $sideNav-width = 200px $sideNav--folded-width = 44px $topBar-height = 60px @@ -347,4 +347,4 @@ modalSolarizedDark() width 100% background-color $ui-solarized-dark-backgroundColor overflow hidden - border-radius $modal-border-radius \ No newline at end of file + border-radius $modal-border-radius From d76ecd5423ab2352ad4d26e0475c1897e172d25d Mon Sep 17 00:00:00 2001 From: Sander Steenhuis Date: Mon, 26 Feb 2018 13:20:24 +0100 Subject: [PATCH 09/17] Fixed bad merge; closes #1586; related to #1521 --- browser/components/CodeEditor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 29c3748b..f2b3d8f1 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -32,6 +32,7 @@ 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.blurHandler = (editor, e) => { if (e == null) return null @@ -81,7 +82,6 @@ export default class CodeEditor extends React.Component { } } }) - this.scrollHandler = _.debounce(this.handleScroll.bind(this), 100, {leading: false, trailing: true}) } componentDidMount () { From e5f986a95932ae14a36590bc4dcf22f2acb025f8 Mon Sep 17 00:00:00 2001 From: lijinglue Date: Mon, 26 Feb 2018 21:12:42 +0800 Subject: [PATCH 10/17] checking if return is valid before creating the blog attr in note --- browser/main/NoteList/index.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index 3db88dd1..7075c0ef 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -489,7 +489,7 @@ class NoteList extends React.Component { click: this.cloneNote.bind(this) })) if (note.type === 'MARKDOWN_NOTE') { - if (note.blog) { + if (note.blog && note.blog.blogLink && note.blog.blogId) { menu.append(new MenuItem({ label: updateLabel, click: this.publishMarkdown.bind(this) @@ -697,7 +697,7 @@ class NoteList extends React.Component { let url = '' let method = '' - if (firstNote.blog) { + if (firstNote.blog && firstNote.blog.blogId) { url = `${address}${WP_POST_PATH}/${firstNote.blog.blogId}` method = 'PUT' } else { @@ -714,6 +714,9 @@ class NoteList extends React.Component { } }).then(res => res.json()) .then(response => { + if (_.isNil(response.link) || _.isNil(response.id)) { + return Promise.reject(); + } firstNote.blog = { blogLink: response.link, blogId: response.id @@ -721,7 +724,8 @@ class NoteList extends React.Component { this.save(firstNote) this.confirmPublish(firstNote) }) - .catch(() => { + .catch((error) => { + console.error(error) this.confirmPublishError() }) } @@ -753,7 +757,6 @@ class NoteList extends React.Component { openBlog (note) { const { shell } = electron - console.log(note) shell.openExternal(note.blog.blogLink) } From 7c525ab7a650d565e6a05ec971434e79490be37b Mon Sep 17 00:00:00 2001 From: lurong Date: Mon, 26 Feb 2018 21:27:42 +0800 Subject: [PATCH 11/17] fix lint --- browser/main/NoteList/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index 7075c0ef..13cb4400 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -715,7 +715,7 @@ class NoteList extends React.Component { }).then(res => res.json()) .then(response => { if (_.isNil(response.link) || _.isNil(response.id)) { - return Promise.reject(); + return Promise.reject() } firstNote.blog = { blogLink: response.link, From 650215871684d3c7f7445440708039f0b07b8eca Mon Sep 17 00:00:00 2001 From: Sander Steenhuis Date: Mon, 26 Feb 2018 16:07:27 +0100 Subject: [PATCH 12/17] Add search clear options; closes #1525 --- browser/components/CodeEditor.js | 7 +++++ browser/main/Detail/SnippetNoteDetail.js | 3 ++ browser/main/NoteList/index.js | 15 ++++++++-- browser/main/TopBar/TopBar.styl | 28 ++++++++++++++++- browser/main/TopBar/index.js | 38 +++++++++++++++++------- lib/main-menu.js | 18 +++++++++++ 6 files changed, 96 insertions(+), 13 deletions(-) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 29c3748b..6cbec121 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -8,6 +8,7 @@ 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' +const { ipcRenderer } = require('electron') CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js' @@ -33,7 +34,11 @@ export default class CodeEditor extends React.Component { super(props) 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) { @@ -139,6 +144,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 +168,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) 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..795b4503 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -257,27 +257,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() } 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..0a602a5c 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,11 @@ class TopBar extends React.Component { isIME: false }) + // Clear search on ESC + if (e.keyCode === 27) { + return this.handleSearchClearButton(e) + } + // When the key is an alphabet, del, enter or ctr if (e.keyCode <= 90 || e.keyCode >= 186 && e.keyCode <= 222) { this.setState({ @@ -114,10 +130,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 +168,15 @@ class TopBar extends React.Component { type='text' className='searchInput' /> + {this.state.search !== '' && + + } - {this.state.search > 0 && - - } - {location.pathname === '/trashed' ? '' diff --git a/lib/main-menu.js b/lib/main-menu.js index 0d49ab86..81e5fc22 100644 --- a/lib/main-menu.js +++ b/lib/main-menu.js @@ -1,6 +1,7 @@ const electron = require('electron') const BrowserWindow = electron.BrowserWindow const shell = electron.shell +const ipc = electron.ipcMain const mainWindow = require('./main-window') const macOS = process.platform === 'darwin' @@ -259,6 +260,23 @@ const view = { ] } +let editorFocused + +// Define extra shortcut keys +mainWindow.webContents.on('before-input-event', (event, input) => { + // Synonyms for Search (Find) + if (input.control && input.key === 'f' && input.type === 'keyDown') { + if (!editorFocused) { + mainWindow.webContents.send('top:focus-search') + event.preventDefault() + } + } +}) + +ipc.on('editor:focused', (event, isFocused) => { + editorFocused = isFocused +}) + const window = { label: 'Window', submenu: [ From 5b8519b2b806dfa7f2d31ae665af5cbfd651f94d Mon Sep 17 00:00:00 2001 From: Sander Steenhuis Date: Mon, 26 Feb 2018 16:20:45 +0100 Subject: [PATCH 13/17] Bind up/down to prev/next event; closes #1167 --- browser/main/TopBar/index.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/browser/main/TopBar/index.js b/browser/main/TopBar/index.js index 0a602a5c..246ef614 100644 --- a/browser/main/TopBar/index.js +++ b/browser/main/TopBar/index.js @@ -59,6 +59,18 @@ class TopBar extends React.Component { 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({ From 1f1e54553889f93722dfca608040adf20e769b4a Mon Sep 17 00:00:00 2001 From: Jannick Hemelhof Date: Mon, 26 Feb 2018 12:26:29 +0100 Subject: [PATCH 14/17] Fix for list insertion --- .../boost/boostNewLineIndentContinueMarkdownList.js | 0 lib/main.html | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename {node_modules => extra_scripts}/boost/boostNewLineIndentContinueMarkdownList.js (100%) diff --git a/node_modules/boost/boostNewLineIndentContinueMarkdownList.js b/extra_scripts/boost/boostNewLineIndentContinueMarkdownList.js similarity index 100% rename from node_modules/boost/boostNewLineIndentContinueMarkdownList.js rename to extra_scripts/boost/boostNewLineIndentContinueMarkdownList.js diff --git a/lib/main.html b/lib/main.html index 53520957..8852bd9e 100644 --- a/lib/main.html +++ b/lib/main.html @@ -78,7 +78,7 @@ - + From 012908e32d32ccd9468be9634020922dfc95ae02 Mon Sep 17 00:00:00 2001 From: Jannick Hemelhof Date: Mon, 26 Feb 2018 16:43:13 +0100 Subject: [PATCH 15/17] Linting fixes --- .../boostNewLineIndentContinueMarkdownList.js | 90 ++++++++++--------- 1 file changed, 48 insertions(+), 42 deletions(-) diff --git a/extra_scripts/boost/boostNewLineIndentContinueMarkdownList.js b/extra_scripts/boost/boostNewLineIndentContinueMarkdownList.js index 159d20eb..aa766f6e 100644 --- a/extra_scripts/boost/boostNewLineIndentContinueMarkdownList.js +++ b/extra_scripts/boost/boostNewLineIndentContinueMarkdownList.js @@ -1,46 +1,52 @@ -(function(mod) { - if (typeof exports == "object" && typeof module == "object") // CommonJS - mod(require("../codemirror/lib/codemirror")); - else if (typeof define == "function" && define.amd) // AMD - define(["../codemirror/lib/codemirror"], mod); - else // Plain browser env - mod(CodeMirror); -})(function(CodeMirror) { - "use strict"; +(function (mod) { + if (typeof exports === 'object' && typeof module === 'object') { // Common JS + mod(require('../codemirror/lib/codemirror')) + } else if (typeof define === 'function' && define.amd) { // AMD + define(['../codemirror/lib/codemirror'], mod) + } else { // Plain browser env + mod(CodeMirror) + } +})(function (CodeMirror) { + 'use strict' - var listRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]\s|[*+-]\s|(\d+)([.)]))(\s*)/, - emptyListRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]|[*+-]|(\d+)[.)])(\s*)$/, - unorderedListRE = /[*+-]\s/; + var listRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]\s|[*+-]\s|(\d+)([.)]))(\s*)/ + var emptyListRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]|[*+-]|(\d+)[.)])(\s*)$/ + var unorderedListRE = /[*+-]\s/ - CodeMirror.commands.boostNewLineAndIndentContinueMarkdownList = function(cm) { - if (cm.getOption("disableInput")) return CodeMirror.Pass; - var ranges = cm.listSelections(), replacements = []; - for (var i = 0; i < ranges.length; i++) { - var pos = ranges[i].head; - var eolState = cm.getStateAfter(pos.line); - var inList = eolState.list !== false; - var inQuote = eolState.quote !== 0; - var line = cm.getLine(pos.line), match = listRE.exec(line); - if (!ranges[i].empty() || (!inList && !inQuote) || !match || pos.ch < match[2].length - 1) { - cm.execCommand("newlineAndIndent"); - return; - } - if (emptyListRE.test(line)) { - if (!/>\s*$/.test(line)) cm.replaceRange("", { - line: pos.line, ch: 0 - }, { - line: pos.line, ch: pos.ch + 1 - }); - replacements[i] = "\n"; - } else { - var indent = match[1], after = match[5]; - var bullet = unorderedListRE.test(match[2]) || match[2].indexOf(">") >= 0 - ? match[2].replace("x", " ") - : (parseInt(match[3], 10) + 1) + match[4]; - replacements[i] = "\n" + indent + bullet + after; - } + CodeMirror.commands.boostNewLineAndIndentContinueMarkdownList = function (cm) { + if (cm.getOption('disableInput')) return CodeMirror.Pass + var ranges = cm.listSelections() + var replacements = [] + for (var i = 0; i < ranges.length; i++) { + var pos = ranges[i].head + var eolState = cm.getStateAfter(pos.line) + var inList = eolState.list !== false + var inQuote = eolState.quote !== 0 + var line = cm.getLine(pos.line) + var match = listRE.exec(line) + if (!ranges[i].empty() || (!inList && !inQuote) || !match || pos.ch < match[2].length - 1) { + cm.execCommand('newlineAndIndent') + return + } + if (emptyListRE.test(line)) { + if (!/>\s*$/.test(line)) { + cm.replaceRange('', { + line: pos.line, ch: 0 + }, { + line: pos.line, ch: pos.ch + 1 + }) } + replacements[i] = '\n' + } else { + var indent = match[1] + var after = match[5] + var bullet = unorderedListRE.test(match[2]) || match[2].indexOf('>') >= 0 + ? match[2].replace('x', ' ') + : (parseInt(match[3], 10) + 1) + match[4] + replacements[i] = '\n' + indent + bullet + after + } + } - cm.replaceSelections(replacements); - }; -}); + cm.replaceSelections(replacements) + } +}) From c5c78763d1b1066b5bdc8099919eed44aabdf831 Mon Sep 17 00:00:00 2001 From: Yu-Hung Ou Date: Tue, 27 Feb 2018 23:17:15 +1100 Subject: [PATCH 16/17] fixed missing zoom button --- browser/main/StatusBar/StatusBar.styl | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) 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() From aab192223a14f31d2ce5a83f1d5ae03cf73a5679 Mon Sep 17 00:00:00 2001 From: Romain Le Quellec Date: Wed, 28 Feb 2018 11:39:45 +0100 Subject: [PATCH 17/17] add Toggle FullScreen feat + shortcut --- lib/main-menu.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/main-menu.js b/lib/main-menu.js index 0d49ab86..783b2f0d 100644 --- a/lib/main-menu.js +++ b/lib/main-menu.js @@ -267,6 +267,11 @@ const window = { accelerator: 'Command+M', selector: 'performMiniaturize:' }, + { + label: 'Toggle Full Screen', + accelerator: 'Command+Control+F', + selector: 'toggleFullScreen:' + }, { label: 'Close', accelerator: 'Command+W',