Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] owcorpusviewer: Avoid 'synchronous' evalJS, use 'runJavaScript' instead #341

Merged
merged 2 commits into from
May 4, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions orangecontrib/text/widgets/owcorpusviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import sre_constants
from itertools import chain

from AnyQt.QtCore import Qt, QUrl, QItemSelection, QItemSelectionModel, QItemSelectionRange
from AnyQt.QtCore import (
Qt, QUrl, QItemSelection, QItemSelectionModel, QItemSelectionRange,
pyqtSlot as Slot
)

from AnyQt.QtGui import QStandardItemModel, QStandardItem
from AnyQt.QtWidgets import (QListView, QSizePolicy, QTableView,
QAbstractItemView, QHeaderView, QSplitter,
Expand Down Expand Up @@ -115,6 +119,7 @@ def __init__(self):

# Document contents
self.doc_webview = gui.WebviewWidget(self.splitter, debug=False)
self.doc_webview.loadFinished.connect(self.highlight_docs)

self.mainArea.layout().addWidget(self.splitter)

Expand Down Expand Up @@ -222,6 +227,12 @@ def show_docs(self):
<!doctype html>
<html>
<head>
<script type="text/javascript" src="resources/jquery-3.1.1.min.js">
</script>
<script type="text/javascript" src="resources/jquery.mark.min.js">
</script>
<script type="text/javascript" src="resources/highlighter.js">
</script>
<meta charset='utf-8'>
<style>

Expand Down Expand Up @@ -306,17 +317,8 @@ def show_docs(self):
token) for token in tokens[row_ind]))

html += '</table>'

# QUrl is a workaround to allow local resources
# https://bugreports.qt.io/browse/QTBUG-55902?focusedCommentId=335945
self.doc_webview.setHtml(HTML.format(html), QUrl("file://"))
self.load_js()
self.highlight_docs()

def load_js(self):
resources = os.path.join(os.path.dirname(__file__), 'resources')
for script in ('jquery-3.1.1.min.js', 'jquery.mark.min.js', 'highlighter.js', ):
self.doc_webview.evalJS(open(os.path.join(resources, script), encoding='utf-8').read())
base = QUrl.fromLocalFile(__file__)
self.doc_webview.setHtml(HTML.format(html), base)

def search_features_changed(self):
self.regenerate_docs()
Expand All @@ -338,11 +340,21 @@ def refresh_search(self):
self.update_info()
self.commit()

@Slot()
def highlight_docs(self):
search_keyword = self.regexp_filter.\
strip('|').replace('\\', '\\\\') # escape one \ to two for mark.js

if search_keyword:
self.doc_webview.evalJS('mark("{}");'.format(search_keyword))
# mark is undefined when clearing the view (`setHtml('')`). Maybe
# set and template html with all the scripts, ... but no contents?
self.doc_webview.runJavaScript(
'''
if (typeof mark !== "undefined") {{
mark("{}");
}}
'''.format(search_keyword)
)

def update_info(self):
if self.corpus is not None:
Expand Down