diff --git a/src/components/App.js b/src/components/App.js index 2b723d9..ae5f19e 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -29,7 +29,7 @@ export default class App extends Component { return (
- {viewStore.showBookmark = false}} cancelText="取消" style={{padding: 10}}> + {viewStore.bookmarkHistory = null}} style={{padding: 10}} footer={null}> { viewStore.breadcrumbs.map((breadcrumb, index) => ( this.onBreadcrumbClick(breadcrumb, index)} diff --git a/src/components/BookmarkItem.js b/src/components/BookmarkItem.js index 4ad4b75..b9a4d10 100644 --- a/src/components/BookmarkItem.js +++ b/src/components/BookmarkItem.js @@ -1,5 +1,5 @@ import React, {Component} from 'react'; -import {Row, Col, Icon} from 'antd'; +import {Row, Col, Icon, message} from 'antd'; import PropTypes from 'prop-types'; import {observer, inject} from "mobx-react"; @@ -14,16 +14,15 @@ export default class BookmarkItem extends Component { } onBookmarkSelect(node) { - const {viewStore} = this.props; - viewStore.selectedBookmark = node; + const {bookmarkStore, viewStore} = this.props; + bookmarkStore.add(viewStore.bookmarkHistory,node); + viewStore.bookmarkHistory = null; + message.success(`已添加书签到 ${node.title} 中`, 3); } render() { const {viewStore, bookmark} = this.props; const style = {wordWrap: 'break-word'}; - if (viewStore.selectedBookmark && viewStore.selectedBookmark.id === bookmark.id) { - style.color = '#3143fa'; - } let icon = ''; @@ -31,8 +30,7 @@ export default class BookmarkItem extends Component { icon = this.onBookmarkClick(bookmark)} type="right"/> } - return ( this.onBookmarkSelect(bookmark)}> + return ( this.onBookmarkSelect(bookmark)}> {bookmark.title} {icon} ) diff --git a/src/components/HistoryItem.js b/src/components/HistoryItem.js index 8a8deb4..baad56b 100644 --- a/src/components/HistoryItem.js +++ b/src/components/HistoryItem.js @@ -2,14 +2,15 @@ import React, {Component} from 'react'; import {Row, Col, Checkbox, Icon, Tooltip} from 'antd'; import PropTypes from 'prop-types'; import {inject, observer} from 'mobx-react'; -import viewStore from "../stores/ViewStore"; @inject('historyStore', 'viewStore', 'bookmarkStore') @observer export default class HistoryItem extends Component { render() { - const {historyStore, bookmarkStore, history} = this.props; + const {historyStore, viewStore, history} = this.props; + + const star = history.bookmark ? 'star' : 'star-o'; return ( @@ -23,9 +24,9 @@ export default class HistoryItem extends Component { {history.title} - - { - viewStore.showBookmark = true; + + { + viewStore.bookmarkHistory = history; }}/> diff --git a/src/service/BookmarkService.js b/src/service/BookmarkService.js index efd70ca..e2cc3f2 100644 --- a/src/service/BookmarkService.js +++ b/src/service/BookmarkService.js @@ -9,6 +9,27 @@ class BookmarkService { }); }); } + + async create(bookmark) { + return new Promise((resolve) => { + chrome.bookmarks.create(bookmark, result => { + resolve(result); + }) + }); + } + + async search(url) { + return new Promise((resolve) => { + chrome.bookmarks.search({url}, result => { + let bookmark = result.length ? result[0] : null + resolve(bookmark); + }) + }); + } + + move(id, parentId) { + chrome.bookmarks.move(id, {parentId}); + } } let service = new BookmarkService(); diff --git a/src/stores/BookmarkStore.js b/src/stores/BookmarkStore.js index 57e48ba..ae09526 100644 --- a/src/stores/BookmarkStore.js +++ b/src/stores/BookmarkStore.js @@ -25,7 +25,25 @@ class BookmarkStore { bookmark.children = await bookmarkService.getFolder(bookmark.id); return bookmark; })); - console.log(this.nodes); + } + + @action + async add(history, bookmark) { + if (history.bookmark) { + bookmarkService.move(history.bookmark.id, bookmark.id); + } else { + const data = { + title: history.title, + url: history.url, + parentId: bookmark.id + } + history.bookmark = await bookmarkService.create(data); + } + } + + @action + async move(bookmark, parent) { + bookmarkService.move(bookmark.id, parent.id); } } diff --git a/src/stores/HistoryStore.js b/src/stores/HistoryStore.js index c3eaf22..a64dbb3 100644 --- a/src/stores/HistoryStore.js +++ b/src/stores/HistoryStore.js @@ -1,5 +1,6 @@ import {observable, action, computed} from 'mobx'; import historyService from '../service/HistoryService'; +import bookmarkService from '../service/BookmarkService'; import moment from 'moment'; class History { @@ -8,8 +9,7 @@ class History { url = ''; rangeKey = ''; lastVisitTime = ''; - visitCount = 0; - typedCount = 0; + @observable bookmark = null; @observable checked = false; constructor(historyObject) { @@ -113,9 +113,10 @@ class HistoryStore { this.ranges = []; } - add(historyItems) { + async add(historyItems) { for (let historyItem of historyItems) { let history = new History(historyItem); + history.bookmark = await bookmarkService.search(history.url); let range = this.getRange(history.rangeKey); range.add(history); } diff --git a/src/stores/ViewStore.js b/src/stores/ViewStore.js index 55cc35a..1fce9ef 100644 --- a/src/stores/ViewStore.js +++ b/src/stores/ViewStore.js @@ -4,12 +4,10 @@ import historyStore from './HistoryStore'; class ViewStore { @observable checkedHistories = []; - @observable showBookmark = false; + @observable bookmarkHistory = null; @observable breadcrumbs = []; - @observable selectedBookmark = null; - @computed get hasChecked() { return this.checkedHistories.length > 0; @@ -40,15 +38,11 @@ class ViewStore { @action addBreadcrumb(node) { - this.selectedBookmark = null; this.breadcrumbs.push(node); } @action changeBreadcrumb(index) { - if (index !== this.breadcrumbs.length - 1) { - this.selectedBookmark = null; - } this.breadcrumbs.splice(index + 1); } }