1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 01:36:22 +00:00

use uuid in keygen, remove storage.key from note hash

This commit is contained in:
mirsch
2018-03-04 22:21:14 +01:00
parent 6bcb6398f8
commit 0280a5f09e
13 changed files with 50 additions and 40 deletions

View File

@@ -504,9 +504,20 @@ export default class MarkdownPreview extends React.Component {
handlelinkClick (e) { handlelinkClick (e) {
const noteHash = e.target.href.split('/').pop() const noteHash = e.target.href.split('/').pop()
const regexIsNoteLink = /^(.{20})-(.{20})$/ // this will match the new uuid v4 hash and the old hash
// e.g.
// :note:1c211eb7dcb463de6490 and
// :note:7dd23275-f2b4-49cb-9e93-3454daf1af9c
const regexIsNoteLink = /^:note:([a-zA-Z0-9-]{20,36})$/
if (regexIsNoteLink.test(noteHash)) { if (regexIsNoteLink.test(noteHash)) {
eventEmitter.emit('list:jump', noteHash) eventEmitter.emit('list:jump', noteHash.replace(':note:', ''))
}
// this will match the old link format storage.key-note.key
// e.g.
// 877f99c3268608328037-1c211eb7dcb463de6490
const regexIsLegacyNoteLink = /^(.{20})-(.{20})$/
if (regexIsLegacyNoteLink.test(noteHash)) {
eventEmitter.emit('list:jump', noteHash.split('-')[1])
} }
} }

View File

@@ -62,9 +62,9 @@ const NoteItem = ({
? 'item--active' ? 'item--active'
: 'item' : 'item'
} }
key={`${note.storage}-${note.key}`} key={note.key}
onClick={e => handleNoteClick(e, `${note.storage}-${note.key}`)} onClick={e => handleNoteClick(e, note.key)}
onContextMenu={e => handleNoteContextMenu(e, `${note.storage}-${note.key}`)} onContextMenu={e => handleNoteContextMenu(e, note.key)}
onDragStart={e => handleDragStart(e, note)} onDragStart={e => handleDragStart(e, note)}
draggable='true' draggable='true'
> >

View File

@@ -28,9 +28,9 @@ const NoteItemSimple = ({
? 'item-simple--active' ? 'item-simple--active'
: 'item-simple' : 'item-simple'
} }
key={`${note.storage}-${note.key}`} key={note.key}
onClick={e => handleNoteClick(e, `${note.storage}-${note.key}`)} onClick={e => handleNoteClick(e, note.key)}
onContextMenu={e => handleNoteContextMenu(e, `${note.storage}-${note.key}`)} onContextMenu={e => handleNoteContextMenu(e, note.key)}
onDragStart={e => handleDragStart(e, note)} onDragStart={e => handleDragStart(e, note)}
draggable='true' draggable='true'
> >

View File

@@ -1,7 +1,5 @@
const crypto = require('crypto') const uuidv4 = require('uuid/v4')
const _ = require('lodash')
module.exports = function (length) { module.exports = function () {
if (!_.isFinite(length)) length = 10 return uuidv4()
return crypto.randomBytes(length).toString('hex')
} }

View File

@@ -139,7 +139,7 @@ class MarkdownNoteDetail extends React.Component {
hashHistory.replace({ hashHistory.replace({
pathname: location.pathname, pathname: location.pathname,
query: { query: {
key: newNote.storage + '-' + newNote.key key: newNote.key
} }
}) })
this.setState({ this.setState({
@@ -393,7 +393,7 @@ class MarkdownNoteDetail extends React.Component {
<InfoPanel <InfoPanel
storageName={currentOption.storage.name} storageName={currentOption.storage.name}
folderName={currentOption.folder.name} folderName={currentOption.folder.name}
noteLink={`[${note.title}](${location.query.key})`} noteLink={`[${note.title}](:note:${location.query.key})`}
updatedAt={formatDate(note.updatedAt)} updatedAt={formatDate(note.updatedAt)}
createdAt={formatDate(note.createdAt)} createdAt={formatDate(note.createdAt)}
exportAsMd={this.exportAsMd} exportAsMd={this.exportAsMd}

View File

@@ -147,7 +147,7 @@ class SnippetNoteDetail extends React.Component {
hashHistory.replace({ hashHistory.replace({
pathname: location.pathname, pathname: location.pathname,
query: { query: {
key: newNote.storage + '-' + newNote.key key: newNote.key
} }
}) })
this.setState({ this.setState({
@@ -647,7 +647,7 @@ class SnippetNoteDetail extends React.Component {
<InfoPanel <InfoPanel
storageName={currentOption.storage.name} storageName={currentOption.storage.name}
folderName={currentOption.folder.name} folderName={currentOption.folder.name}
noteLink={`[${note.title}](${location.query.key})`} noteLink={`[${note.title}](:note:${location.query.key})`}
updatedAt={formatDate(note.updatedAt)} updatedAt={formatDate(note.updatedAt)}
createdAt={formatDate(note.createdAt)} createdAt={formatDate(note.createdAt)}
exportAsMd={this.showWarning} exportAsMd={this.showWarning}

View File

@@ -56,11 +56,8 @@ class Detail extends React.Component {
const { location, data, config } = this.props const { location, data, config } = this.props
let note = null let note = null
if (location.query.key != null) { if (location.query.key != null) {
const splitted = location.query.key.split('-') const noteKey = location.query.key
const storageKey = splitted.shift() note = data.noteMap.get(noteKey)
const noteKey = splitted.shift()
note = data.noteMap.get(storageKey + '-' + noteKey)
} }
if (note == null) { if (note == null) {

View File

@@ -31,7 +31,7 @@ function sortByUpdatedAt (a, b) {
} }
function findNoteByKey (notes, noteKey) { function findNoteByKey (notes, noteKey) {
return notes.find((note) => `${note.storage}-${note.key}` === noteKey) return notes.find((note) => note.key === noteKey)
} }
function findNotesByKeys (notes, noteKeys) { function findNotesByKeys (notes, noteKeys) {
@@ -39,7 +39,7 @@ function findNotesByKeys (notes, noteKeys) {
} }
function getNoteKey (note) { function getNoteKey (note) {
return `${note.storage}-${note.key}` return note.key
} }
class NoteList extends React.Component { class NoteList extends React.Component {
@@ -114,10 +114,10 @@ class NoteList extends React.Component {
componentDidUpdate (prevProps) { componentDidUpdate (prevProps) {
const { location } = this.props const { location } = this.props
const { selectedNoteKeys } = this.state const { selectedNoteKeys } = this.state
const visibleNoteKeys = this.notes.map(note => `${note.storage}-${note.key}`) const visibleNoteKeys = this.notes.map(note => note.key)
const note = this.notes[0] const note = this.notes[0]
const prevKey = prevProps.location.query.key const prevKey = prevProps.location.query.key
const noteKey = visibleNoteKeys.includes(prevKey) ? prevKey : note && `${note.storage}-${note.key}` const noteKey = visibleNoteKeys.includes(prevKey) ? prevKey : note && note.key
if (note && location.query.key == null) { if (note && location.query.key == null) {
const { router } = this.context const { router } = this.context
@@ -613,19 +613,18 @@ class NoteList extends React.Component {
content: firstNote.content content: firstNote.content
}) })
.then((note) => { .then((note) => {
const uniqueKey = note.storage + '-' + note.key
dispatch({ dispatch({
type: 'UPDATE_NOTE', type: 'UPDATE_NOTE',
note: note note: note
}) })
this.setState({ this.setState({
selectedNoteKeys: [uniqueKey] selectedNoteKeys: [note.key]
}) })
hashHistory.push({ hashHistory.push({
pathname: location.pathname, pathname: location.pathname,
query: {key: uniqueKey} query: {key: note.key}
}) })
}) })
} }

View File

@@ -35,7 +35,7 @@ class NewNoteModal extends React.Component {
content: '' content: ''
}) })
.then((note) => { .then((note) => {
const noteHash = `${note.storage}-${note.key}` const noteHash = note.key
dispatch({ dispatch({
type: 'UPDATE_NOTE', type: 'UPDATE_NOTE',
note: note note: note
@@ -75,7 +75,7 @@ class NewNoteModal extends React.Component {
}] }]
}) })
.then((note) => { .then((note) => {
const noteHash = `${note.storage}-${note.key}` const noteHash = note.key
dispatch({ dispatch({
type: 'UPDATE_NOTE', type: 'UPDATE_NOTE',
note: note note: note

View File

@@ -27,7 +27,7 @@ function data (state = defaultDataMap(), action) {
action.notes.some((note) => { action.notes.some((note) => {
if (note === undefined) return true if (note === undefined) return true
const uniqueKey = note.storage + '-' + note.key const uniqueKey = note.key
const folderKey = note.storage + '-' + note.folder const folderKey = note.storage + '-' + note.folder
state.noteMap.set(uniqueKey, note) state.noteMap.set(uniqueKey, note)
@@ -66,7 +66,7 @@ function data (state = defaultDataMap(), action) {
case 'UPDATE_NOTE': case 'UPDATE_NOTE':
{ {
const note = action.note const note = action.note
const uniqueKey = note.storage + '-' + note.key const uniqueKey = note.key
const folderKey = note.storage + '-' + note.folder const folderKey = note.storage + '-' + note.folder
const oldNote = state.noteMap.get(uniqueKey) const oldNote = state.noteMap.get(uniqueKey)
@@ -162,9 +162,9 @@ function data (state = defaultDataMap(), action) {
case 'MOVE_NOTE': case 'MOVE_NOTE':
{ {
const originNote = action.originNote const originNote = action.originNote
const originKey = originNote.storage + '-' + originNote.key const originKey = originNote.key
const note = action.note const note = action.note
const uniqueKey = note.storage + '-' + note.key const uniqueKey = note.key
const folderKey = note.storage + '-' + note.folder const folderKey = note.storage + '-' + note.folder
const oldNote = state.noteMap.get(uniqueKey) const oldNote = state.noteMap.get(uniqueKey)
@@ -423,7 +423,7 @@ function data (state = defaultDataMap(), action) {
state.folderNoteMap = new Map(state.folderNoteMap) state.folderNoteMap = new Map(state.folderNoteMap)
state.tagNoteMap = new Map(state.tagNoteMap) state.tagNoteMap = new Map(state.tagNoteMap)
action.notes.forEach((note) => { action.notes.forEach((note) => {
const uniqueKey = note.storage + '-' + note.key const uniqueKey = note.key
const folderKey = note.storage + '-' + note.folder const folderKey = note.storage + '-' + note.folder
state.noteMap.set(uniqueKey, note) state.noteMap.set(uniqueKey, note)
@@ -483,7 +483,7 @@ function data (state = defaultDataMap(), action) {
state.tagNoteMap = new Map(state.tagNoteMap) state.tagNoteMap = new Map(state.tagNoteMap)
state.starredSet = new Set(state.starredSet) state.starredSet = new Set(state.starredSet)
notes.forEach((note) => { notes.forEach((note) => {
const noteKey = storage.key + '-' + note.key const noteKey = note.key
state.noteMap.delete(noteKey) state.noteMap.delete(noteKey)
state.starredSet.delete(noteKey) state.starredSet.delete(noteKey)
note.tags.forEach((tag) => { note.tags.forEach((tag) => {

View File

@@ -86,7 +86,8 @@
"sander": "^0.5.1", "sander": "^0.5.1",
"striptags": "^2.2.1", "striptags": "^2.2.1",
"superagent": "^1.2.0", "superagent": "^1.2.0",
"superagent-promise": "^1.0.3" "superagent-promise": "^1.0.3",
"uuid": "^3.2.1"
}, },
"devDependencies": { "devDependencies": {
"ava": "^0.16.0", "ava": "^0.16.0",

View File

@@ -149,9 +149,9 @@ function dummyLegacyStorage (storagePath, override = {}) {
var folderNotes = [] var folderNotes = []
var noteCount = Math.floor((Math.random() * 5)) + 1 var noteCount = Math.floor((Math.random() * 5)) + 1
for (var i = 0; i < noteCount; i++) { for (var i = 0; i < noteCount; i++) {
var key = keygen(6) var key = keygen()
while (folderNotes.some((note) => note.key === key)) { while (folderNotes.some((note) => note.key === key)) {
key = keygen(6) key = keygen()
} }
var noteData = dummyNote({ var noteData = dummyNote({

View File

@@ -6672,6 +6672,10 @@ uuid@^2.0.1, uuid@^2.0.2:
version "2.0.3" version "2.0.3"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-2.0.3.tgz#67e2e863797215530dff318e5bf9dcebfd47b21a" resolved "https://registry.yarnpkg.com/uuid/-/uuid-2.0.3.tgz#67e2e863797215530dff318e5bf9dcebfd47b21a"
uuid@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.2.1.tgz#12c528bb9d58d0b9265d9a2f6f0fe8be17ff1f14"
validate-npm-package-license@^3.0.1: validate-npm-package-license@^3.0.1:
version "3.0.1" version "3.0.1"
resolved "https://registry.yarnpkg.com/validate-npm-package-license/-/validate-npm-package-license-3.0.1.tgz#2804babe712ad3379459acfbe24746ab2c303fbc" resolved "https://registry.yarnpkg.com/validate-npm-package-license/-/validate-npm-package-license-3.0.1.tgz#2804babe712ad3379459acfbe24746ab2c303fbc"