From eb349bd2ffee670c15b17b1713616b707fedbe25 Mon Sep 17 00:00:00 2001 From: shanshuirenjia <978987373@qq.com> Date: Thu, 13 Dec 2018 20:42:51 +0800 Subject: [PATCH] add search module --- frontend/src/app.js | 19 ++++++++++-- frontend/src/components/dir-view/dir-view.js | 14 +++++++-- frontend/src/components/search/search.js | 8 ++--- frontend/src/repo-wiki-mode.js | 27 ++++++++++++----- frontend/src/utils/utils.js | 15 +++++++++- frontend/src/wiki.js | 31 +++++++++++++++----- 6 files changed, 89 insertions(+), 25 deletions(-) diff --git a/frontend/src/app.js b/frontend/src/app.js index 3b91ab1880..8861d4a008 100644 --- a/frontend/src/app.js +++ b/frontend/src/app.js @@ -1,7 +1,8 @@ import React, { Component, Fragment } from 'react'; import ReactDOM from 'react-dom'; -import { Router } from '@reach/router'; +import { Router, navigate } from '@reach/router'; import { siteRoot } from './utils/constants'; +import { Utils } from './utils/utils'; import SidePanel from './components/side-panel'; import MainPanel from './components/main-panel'; import DraftsView from './pages/drafts/drafts-view'; @@ -96,8 +97,20 @@ class App extends Component { }); } - onSearchedClick = () => { - //todos + onSearchedClick = (selectedItem) => { + if (selectedItem.is_dir === true) { + this.setState({currentTab: '', pathPrefix: []}); + let url = siteRoot + 'library/' + selectedItem.repo_id + '/' + selectedItem.repo_name + selectedItem.path; + navigate(url, {repalce: true}); + } else if (Utils.isMarkdownFile(selectedItem.path)) { + let url = siteRoot + 'wiki/lib/' + selectedItem.repo_id + selectedItem.path; + let newWindow = window.open('markdown-editor'); + newWindow.location.href = url; + } else { + let url = siteRoot + 'lib/' + selectedItem.repo_id + '/file' + selectedItem.path; + let newWindow = window.open('about:blank'); + newWindow.location.href = url; + } } tabItemClick = (tabName, groupID) => { diff --git a/frontend/src/components/dir-view/dir-view.js b/frontend/src/components/dir-view/dir-view.js index 68d351798e..ed74122bf7 100644 --- a/frontend/src/components/dir-view/dir-view.js +++ b/frontend/src/components/dir-view/dir-view.js @@ -325,8 +325,18 @@ class DirView extends React.Component { // todo update upload file to direntList } - onSearchedClick = () => { - // todo + onSearchedClick = (selectedItem) => { + if (selectedItem.is_dir === true) { + this.setState({path: selectedItem.path}); + } else if (Utils.isMarkdownFile(selectedItem.path)) { + let url = siteRoot + 'wiki/lib/' + selectedItem.repo_id + selectedItem.path; + let newWindow = window.open('markdown-editor'); + newWindow.location.href = url; + } else { + let url = siteRoot + 'lib/' + selectedItem.repo_id + '/file' + selectedItem.path; + let newWindow = window.open('about:blank'); + newWindow.location.href = url; + } } resetSelected = () => { diff --git a/frontend/src/components/search/search.js b/frontend/src/components/search/search.js index be260c2af2..c8d99e980c 100644 --- a/frontend/src/components/search/search.js +++ b/frontend/src/components/search/search.js @@ -6,8 +6,8 @@ import editorUtilities from '../../utils/editor-utilties'; import More from '../more'; const propTypes = { - placeholder: PropTypes.string, repoID: PropTypes.string, + placeholder: PropTypes.string, onSearchedClick: PropTypes.func.isRequired, }; @@ -65,9 +65,7 @@ class Search extends Component { let queryData = { q: newValue, search_repo: repoID ? repoID : 'all', - search_ftypes: repoID ? 'custom' : 'all', - ftype: repoID ? 'Markdown' : '', - input_fexts: repoID ? 'md' : '' + search_ftypes: 'all', }; if (this.timer) { @@ -146,6 +144,8 @@ class Search extends Component { items[i]['name'] = data[i].name; items[i]['path'] = data[i].fullpath; items[i]['repo_id'] = data[i].repo_id; + items[i]['repo_name'] = data[i].repo_name; + items[i]['is_dir'] = data[i].is_dir; items[i]['link_content'] = decodeURI(data[i].fullpath).substring(1); items[i]['content'] = data[i].content_highlight; } diff --git a/frontend/src/repo-wiki-mode.js b/frontend/src/repo-wiki-mode.js index 1a296b5b66..be88faf8b4 100644 --- a/frontend/src/repo-wiki-mode.js +++ b/frontend/src/repo-wiki-mode.js @@ -283,13 +283,26 @@ class Wiki extends Component { } onSearchedClick = (item) => { - //just for file - let path = item.path; - if (this.state.currentFilePath !== path) { - let tree = this.state.treeData.clone(); - let node = tree.getNodeByPath(path); - tree.expandNode(node); - this.showFile(node.path); + if (item.is_dir) { + let path = item.path.slice(0, item.path.length - 1); + if (this.state.currentFilePath !== path) { + let tree = this.state.treeData.clone(); + let node = tree.getNodeByPath(path); + tree.expandNode(node); + this.showDir(node.path); + } + } else if (Utils.isMarkdownFile(item.path)) { + let path = item.path; + if (this.state.currentFilePath !== path) { + let tree = this.state.treeData.clone(); + let node = tree.getNodeByPath(path); + tree.expandNode(node); + this.showFile(node.path); + } + } else { + let url = siteRoot + 'lib/' + item.repo_id + '/file' + item.path; + let newWindow = window.open('about:blank'); + newWindow.location.href = url; } } diff --git a/frontend/src/utils/utils.js b/frontend/src/utils/utils.js index 917cecfd33..3beb944c52 100644 --- a/frontend/src/utils/utils.js +++ b/frontend/src/utils/utils.js @@ -320,6 +320,19 @@ export const Utils = { } else { return bytes + ' B'; } - } + }, + isMarkdownFile: function(filePath) { + let index = filePath.lastIndexOf('.'); + if (index === -1) { + return false; + } else { + let type = filePath.substring(index).toLowerCase(); + if (type === '.md' || type === '.markdown') { + return true; + } else { + return false; + } + } + } }; diff --git a/frontend/src/wiki.js b/frontend/src/wiki.js index 94ff3adea2..a29c208c82 100644 --- a/frontend/src/wiki.js +++ b/frontend/src/wiki.js @@ -5,6 +5,7 @@ import MainPanel from './pages/wiki/main-panel'; import moment from 'moment'; import { slug, repoID, siteRoot, initialPath } from './utils/constants'; import editorUtilities from './utils/editor-utilties'; +import { Utils } from './utils/utils'; import Node from './components/tree-view/node'; import Tree from './components/tree-view/tree'; import './assets/css/fa-solid.css'; @@ -113,14 +114,28 @@ class Wiki extends Component { } onSearchedClick = (item) => { - let path = item.path; - if (this.state.filePath !== path) { - this.initMainPanelData(path); - - let tree = this.state.tree_data.clone(); - let node = tree.getNodeByPath(path); - tree.expandNode(node); - this.enterViewFileState(tree, node, node.path); + if (item.is_dir) { + let path = item.path.slice(0, item.path.length - 1); + if (this.state.filePath !== path) { + let tree = this.state.tree_data.clone(); + let node = tree.getNodeByPath(path); + tree.expandNode(node); + this.exitViewFileState(tree, node); + } + } else if (Utils.isMarkdownFile(item.path)) { + let path = item.path; + if (this.state.filePath !== path) { + this.initMainPanelData(path); + + let tree = this.state.tree_data.clone(); + let node = tree.getNodeByPath(path); + tree.expandNode(node); + this.enterViewFileState(tree, node, node.path); + } + } else { + let url = siteRoot + 'lib/' + item.repo_id + '/file' + item.path; + let newWindow = window.open('about:blank'); + newWindow.location.href = url; } }