Skip to content

Commit

Permalink
remove highlith listener before adding new one
Browse files Browse the repository at this point in the history
use innertext in js
  • Loading branch information
Siedlerchr committed May 4, 2019
1 parent 6255111 commit d99edc7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/main/java/org/jabref/gui/PreviewPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public class PreviewPanel extends ScrollPane implements EntryContainer {

private static String JS_HIGHLIGHT_FUNCTION = " <script type=\"text/javascript\">\r\n" +
" function highlight(text) {\r\n" +
" var innerhtml = document.body.innerHTML;\r\n" +
" var response = innerhtml.replace(new RegExp(text, 'gi'), str => `<span style='background-color:red'>${str}</span>`);\r\n" +
" var innertxt = document.body.innerText;\r\n" +
" var response = innertxt.replace(new RegExp(text, 'gi'), str => `<span style='background-color:red'>${str}</span>`);\r\n" +
" document.body.innerHTML = response;\r\n" +
" }\r\n" +
" </script>";
Expand Down Expand Up @@ -161,6 +161,7 @@ public PreviewPanel(BasePanel panel, BibDatabaseContext databaseContext, KeyBind
createKeyBindings();
updateLayout(preferences, true);


}

private void createKeyBindings() {
Expand Down Expand Up @@ -218,6 +219,7 @@ public void setBasePanel(BasePanel basePanel) {

public void updateLayout(PreviewPreferences previewPreferences) {
updateLayout(previewPreferences, false);
Globals.stateManager.fireSearchQueryHighlightEvent();
}

private void updateLayout(PreviewPreferences previewPreferences, boolean init) {
Expand Down Expand Up @@ -331,27 +333,23 @@ private void setPreviewLabel(String text) {

DefaultTaskExecutor.runInJavaFXThread(() -> previewView.getEngine().loadContent(myText));
this.setHvalue(0);

previewView.getEngine().getLoadWorker().stateProperty().addListener((ObservableValue<? extends Worker.State> observable,
Worker.State oldValue,
Worker.State newValue) -> {
if (newValue != Worker.State.SUCCEEDED) {
return;
}

Globals.stateManager.addSearchQueryHighlightListener(highlightPattern -> {
if (highlightPattern.isPresent()) {
String pattern = highlightPattern.get().pattern().replace("\\Q", "").replace("\\E", "");
String pattern = highlightPattern.get().pattern().replace("\\Q", "").replace("\\E", "");

previewView.getEngine().executeScript("highlight('" + pattern + "');");

}
});
});

}



/**
* this fixes the Layout, the user cannot change it anymore. Useful for testing the styles in the settings
*
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/jabref/gui/StateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public void resetSearchQueryHighlightObservable()
}

public void addSearchQueryHighlightListener(SearchQueryHighlightListener listener) {
searchQueryHighlightObservable.removeSearchListener(listener);
searchQueryHighlightObservable.addSearchListener(listener);
}
}

0 comments on commit d99edc7

Please sign in to comment.