mirror of
https://github.com/haiwen/seahub.git
synced 2025-07-31 06:40:39 +00:00
[search] rewrote 'highlight keyword in file view'
This commit is contained in:
parent
859fc90f66
commit
c97aa55ac4
1
media/js/findAndReplaceDOMText.js
Normal file
1
media/js/findAndReplaceDOMText.js
Normal file
File diff suppressed because one or more lines are too long
@ -181,8 +181,13 @@ var pdf2htmlEX = (function(){
|
||||
_.find_pages();
|
||||
_.schedule_render();
|
||||
_.load_page(idx+1);
|
||||
if (highlight_kw) {
|
||||
highlight_kw($('#file-view'));
|
||||
try {
|
||||
if (highlight_kw) {
|
||||
highlight_kw($('#pf' + parseInt(idx + 1).toString(16))[0]);
|
||||
}
|
||||
} catch (e) {
|
||||
// cases: 'highlight_keyword' in file view is False, history file view
|
||||
// there can be an ReferenceError 'highlight_kw is not defined'
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -216,6 +216,7 @@ OFFICE_PREVIEW_MAX_SIZE = 2 * 1024 * 1024
|
||||
USE_PDFJS = True
|
||||
FILE_ENCODING_LIST = ['auto', 'utf-8', 'gbk', 'ISO-8859-1', 'ISO-8859-5']
|
||||
FILE_ENCODING_TRY_LIST = ['utf-8', 'gbk']
|
||||
HIGHLIGHT_KEYWORD = False # If True, highlight the keywords in the file when the visit is via clicking a link in 'search result' page.
|
||||
|
||||
# Common settings(file extension, storage) for avatar and group avatar.
|
||||
AVATAR_FILE_STORAGE = '' # Replace with 'seahub.base.database_storage.DatabaseStorage' if save avatar files to database
|
||||
|
@ -1,4 +1,3 @@
|
||||
<!-- <script type="text/javascript"> -->
|
||||
{% load i18n %}
|
||||
/**
|
||||
* @param {boolean} html_exists True if the html has already been converted.
|
||||
@ -35,8 +34,13 @@
|
||||
$('#outline .l').removeAttr('data-dest-detail'); // make the anchor links work
|
||||
sidebar.removeClass('hide');
|
||||
|
||||
if (highlight_kw) {
|
||||
highlight_kw(sidebar);
|
||||
try {
|
||||
if (highlight_kw) {
|
||||
highlight_kw(sidebar[0]);
|
||||
}
|
||||
} catch (e) {
|
||||
// cases: 'highlight_keyword' in file view is False, history file view
|
||||
// there can be an ReferenceError 'highlight_kw is not defined'
|
||||
}
|
||||
|
||||
$('#converted-html').append('<div id="pdf2html-toolbar-1"></div>');
|
||||
@ -216,5 +220,3 @@
|
||||
}
|
||||
check_status();
|
||||
{% endif %}
|
||||
|
||||
// </script>
|
||||
|
@ -96,6 +96,9 @@
|
||||
|
||||
{% block extra_script %}
|
||||
<script type="text/javascript" src="{{ MEDIA_URL }}js/select2.min.js?t=1393578720"></script>
|
||||
{% if highlight_keyword %}
|
||||
<script type="text/javascript" src="{{ MEDIA_URL }}js/findAndReplaceDOMText.js"></script>
|
||||
{% endif %}
|
||||
<script type="text/javascript">
|
||||
$(function() {
|
||||
var dld_url = $('#download').data('url');
|
||||
@ -177,41 +180,41 @@ $('#file-op .history').click(function () {
|
||||
location.href = $(this).data('url');
|
||||
});
|
||||
|
||||
// if referrer is 'search result' page, define function highlight_kw
|
||||
{% if highlight_keyword %}
|
||||
var ref_list = document.referrer.split('?');
|
||||
// referrer is 'search result' page, i.e, '{{SITE_ROOT}}search/?q=xx..'
|
||||
if (ref_list.length > 1 && ref_list[0].indexOf('search') > 0) {
|
||||
var search_params = ref_list[1].split('&');
|
||||
var search_keyword = search_params[0].substr(2); // get 'xx' from 'q=xx'
|
||||
|
||||
var highlight_kw = function(hl_area) {
|
||||
if (window.find && window.getSelection) {
|
||||
hl_area.attr('contenteditable', true).attr('spellcheck', false);
|
||||
var sel = window.getSelection();
|
||||
sel.collapse(document.body, 0);
|
||||
|
||||
while (window.find(search_keyword)) {
|
||||
document.execCommand("HiliteColor", false, "yellow");
|
||||
sel.collapseToEnd();
|
||||
var highlight_kw = function(hl_area) { // 'hl_area': a node
|
||||
findAndReplaceDOMText(hl_area, {
|
||||
find: new RegExp(search_keyword, 'gim'),
|
||||
replace: function(portion, match) { // portion is a part of a match
|
||||
var node = document.createElement('span');
|
||||
node.style.background = '#ffe761';
|
||||
node.innerHTML = portion.text; // portion is an object
|
||||
return node;
|
||||
}
|
||||
hl_area.attr('contenteditable', false);
|
||||
} else if (document.body.createTextRange) {
|
||||
var textRange = document.body.createTextRange();
|
||||
while (textRange.findText(search_keyword)) {
|
||||
textRange.execCommand("BackColor", false, "yellow");
|
||||
textRange.collapse(false);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
$(window).load(function() {
|
||||
if (highlight_kw) {
|
||||
// hl kw in file title
|
||||
highlight_kw($('#view-hd'));
|
||||
highlight_kw($('#view-hd')[0]);
|
||||
|
||||
// hl kw in file content
|
||||
setTimeout(function(){ highlight_kw($('#file-view')); }, 100); // delay 100ms for 'text' file
|
||||
if ('{{filetype}}' == 'Markdown') {
|
||||
highlight_kw($('#file-view')[0]);
|
||||
}
|
||||
if ('{{fileext}}' == 'text' || '{{fileext}}' == 'txt') {
|
||||
setTimeout(function(){ highlight_kw($('.CodeMirror')[0]); }, 500);
|
||||
}
|
||||
}
|
||||
});
|
||||
{% endif %}
|
||||
|
||||
//bottom bar
|
||||
{% include "snippets/bottom_bar.html" %}
|
||||
|
@ -464,6 +464,7 @@ def view_file(request, repo_id):
|
||||
'img_prev': img_prev,
|
||||
'img_next': img_next,
|
||||
'search_repo_id': search_repo_id,
|
||||
'highlight_keyword': settings.HIGHLIGHT_KEYWORD,
|
||||
}, context_instance=RequestContext(request))
|
||||
|
||||
def view_history_file_common(request, repo_id, ret_dict):
|
||||
|
Loading…
Reference in New Issue
Block a user