mirror of
https://github.com/haiwen/seahub.git
synced 2025-08-10 11:22:09 +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();
|
_.find_pages();
|
||||||
_.schedule_render();
|
_.schedule_render();
|
||||||
_.load_page(idx+1);
|
_.load_page(idx+1);
|
||||||
if (highlight_kw) {
|
try {
|
||||||
highlight_kw($('#file-view'));
|
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
|
USE_PDFJS = True
|
||||||
FILE_ENCODING_LIST = ['auto', 'utf-8', 'gbk', 'ISO-8859-1', 'ISO-8859-5']
|
FILE_ENCODING_LIST = ['auto', 'utf-8', 'gbk', 'ISO-8859-1', 'ISO-8859-5']
|
||||||
FILE_ENCODING_TRY_LIST = ['utf-8', 'gbk']
|
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.
|
# 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
|
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 %}
|
{% load i18n %}
|
||||||
/**
|
/**
|
||||||
* @param {boolean} html_exists True if the html has already been converted.
|
* @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
|
$('#outline .l').removeAttr('data-dest-detail'); // make the anchor links work
|
||||||
sidebar.removeClass('hide');
|
sidebar.removeClass('hide');
|
||||||
|
|
||||||
if (highlight_kw) {
|
try {
|
||||||
highlight_kw(sidebar);
|
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>');
|
$('#converted-html').append('<div id="pdf2html-toolbar-1"></div>');
|
||||||
@ -216,5 +220,3 @@
|
|||||||
}
|
}
|
||||||
check_status();
|
check_status();
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
// </script>
|
|
||||||
|
@ -96,6 +96,9 @@
|
|||||||
|
|
||||||
{% block extra_script %}
|
{% block extra_script %}
|
||||||
<script type="text/javascript" src="{{ MEDIA_URL }}js/select2.min.js?t=1393578720"></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">
|
<script type="text/javascript">
|
||||||
$(function() {
|
$(function() {
|
||||||
var dld_url = $('#download').data('url');
|
var dld_url = $('#download').data('url');
|
||||||
@ -177,41 +180,41 @@ $('#file-op .history').click(function () {
|
|||||||
location.href = $(this).data('url');
|
location.href = $(this).data('url');
|
||||||
});
|
});
|
||||||
|
|
||||||
// if referrer is 'search result' page, define function highlight_kw
|
{% if highlight_keyword %}
|
||||||
var ref_list = document.referrer.split('?');
|
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) {
|
if (ref_list.length > 1 && ref_list[0].indexOf('search') > 0) {
|
||||||
var search_params = ref_list[1].split('&');
|
var search_params = ref_list[1].split('&');
|
||||||
var search_keyword = search_params[0].substr(2); // get 'xx' from 'q=xx'
|
var search_keyword = search_params[0].substr(2); // get 'xx' from 'q=xx'
|
||||||
|
|
||||||
var highlight_kw = function(hl_area) {
|
var highlight_kw = function(hl_area) { // 'hl_area': a node
|
||||||
if (window.find && window.getSelection) {
|
findAndReplaceDOMText(hl_area, {
|
||||||
hl_area.attr('contenteditable', true).attr('spellcheck', false);
|
find: new RegExp(search_keyword, 'gim'),
|
||||||
var sel = window.getSelection();
|
replace: function(portion, match) { // portion is a part of a match
|
||||||
sel.collapse(document.body, 0);
|
var node = document.createElement('span');
|
||||||
|
node.style.background = '#ffe761';
|
||||||
while (window.find(search_keyword)) {
|
node.innerHTML = portion.text; // portion is an object
|
||||||
document.execCommand("HiliteColor", false, "yellow");
|
return node;
|
||||||
sel.collapseToEnd();
|
|
||||||
}
|
}
|
||||||
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() {
|
$(window).load(function() {
|
||||||
if (highlight_kw) {
|
if (highlight_kw) {
|
||||||
// hl kw in file title
|
// hl kw in file title
|
||||||
highlight_kw($('#view-hd'));
|
highlight_kw($('#view-hd')[0]);
|
||||||
|
|
||||||
// hl kw in file content
|
// 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
|
//bottom bar
|
||||||
{% include "snippets/bottom_bar.html" %}
|
{% include "snippets/bottom_bar.html" %}
|
||||||
|
@ -464,6 +464,7 @@ def view_file(request, repo_id):
|
|||||||
'img_prev': img_prev,
|
'img_prev': img_prev,
|
||||||
'img_next': img_next,
|
'img_next': img_next,
|
||||||
'search_repo_id': search_repo_id,
|
'search_repo_id': search_repo_id,
|
||||||
|
'highlight_keyword': settings.HIGHLIGHT_KEYWORD,
|
||||||
}, context_instance=RequestContext(request))
|
}, context_instance=RequestContext(request))
|
||||||
|
|
||||||
def view_history_file_common(request, repo_id, ret_dict):
|
def view_history_file_common(request, repo_id, ret_dict):
|
||||||
|
Loading…
Reference in New Issue
Block a user