this commit implements scorllToLineNumber to close #376

This commit is contained in:
Kosh 2017-04-29 02:12:20 +08:00
parent 51c16db77f
commit ae5e261da6
8 changed files with 89 additions and 14 deletions

View File

@ -13,6 +13,28 @@ ol.linenums {
color: #727272;
}
ol {
margin-left: 0;
padding-left: 0.2em;
counter-reset: ol-item
}
ol li:nth-child(-n+9) {
padding-left: 4px;
}
ol li {
margin-left: 0;
padding-left: 0;
list-style-type: none;
counter-increment: ol-item
}
ol li:before {
content: counter(ol-item) " "
}
li.L0,
li.L1,
li.L2,
@ -22,10 +44,7 @@ li.L5,
li.L6,
li.L7,
li.L8,
li.L9 {
padding-left: 0.5em;
list-style-type: decimal;
}
li.L9 {}
@media screen {

View File

@ -14,6 +14,27 @@ ol.linenums {
color: #656d78;
}
ol {
margin-left: 0;
padding-left: 0.2em;
counter-reset: ol-item
}
ol li:nth-child(-n+9) {
padding-left: 4px;
}
ol li {
margin-left: 0;
padding-left: 0;
list-style-type: none;
counter-increment: ol-item
}
ol li:before {
content: counter(ol-item) " "
}
li.L0,
li.L1,
li.L2,
@ -23,10 +44,7 @@ li.L5,
li.L6,
li.L7,
li.L8,
li.L9 {
padding-left: 0.5em;
list-style-type: decimal;
}
li.L9 {}
@media screen {

View File

@ -67,7 +67,7 @@ public class ViewerFragment extends BaseFragment<ViewerMvp.View, ViewerPresenter
@Override public void onSetCode(@NonNull String text) {
stateLayout.hideProgress();
webView.setVisibility(View.VISIBLE);
webView.setSource(text, isWrap);
webView.setSource(text, isWrap, getPresenter().url());
getActivity().supportInvalidateOptionsMenu();
}

View File

@ -46,5 +46,7 @@ interface ViewerMvp {
boolean isRepo();
boolean isImage();
@NonNull String url();
}
}

View File

@ -145,4 +145,8 @@ class ViewerPresenter extends BasePresenter<ViewerMvp.View> implements ViewerMvp
@Override public boolean isImage() {
return isImage;
}
@NonNull @Override public String url() {
return url;
}
}

View File

@ -108,7 +108,7 @@ public class PrettifyWebView extends NestedWebView {
this.onContentChangedListener = onContentChangedListener;
}
public void setSource(@NonNull String source, boolean wrap) {
public void setSource(@NonNull String source, boolean wrap, @Nullable String url) {
WebSettings settings = getSettings();
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
@ -116,8 +116,28 @@ public class PrettifyWebView extends NestedWebView {
settings.setBuiltInZoomControls(!wrap);
if (!wrap) settings.setDisplayZoomControls(false);
if (!InputHelper.isEmpty(source)) {
String page = PrettifyHelper.generateContent(source, AppHelper.isNightMode(getResources()), wrap);
String page = PrettifyHelper.generateContent(source, AppHelper.isNightMode(getResources()), wrap, url);
post(() -> loadDataWithBaseURL("file:///android_asset/highlight/", page, "text/html", "utf-8", null));
setOnContentChangedListener(progress -> {
Logger.e(progress);
if (progress == 100) {
int lineNo = 0;
if (url != null) {
try {
Uri uri = Uri.parse(url);
String lineNumber = uri.getEncodedFragment();
Logger.e(lineNumber);
if (lineNumber != null) {
lineNumber = lineNumber.replace("L", "");
lineNo = Integer.valueOf(lineNumber);
}
} catch (Exception ignored) {}
}
if (lineNo != 0) {
if (isAttachedToWindow()) loadUrl("javascript:scrollToLineNumber('" + lineNo + "')");
}
}
});
}
}

View File

@ -6,7 +6,6 @@ import android.webkit.MimeTypeMap;
import com.fastaccess.data.dao.NameParser;
import com.fastaccess.helper.InputHelper;
import com.fastaccess.helper.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -37,7 +36,6 @@ public class GithubHelper {
if (src.startsWith("http://") || src.startsWith("https://")) {
continue;
}
Logger.e(src);
String finalSrc = "https://raw.githubusercontent.com/" + owner + "/" + repoName + "/master/" + src;
source = source.replace("src=\"" + src + "\"", "src=\"" + finalSrc + "\"");
}

View File

@ -1,6 +1,7 @@
package com.prettifier.pretty.helper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
/**
* Created by Kosh on 25 Dec 2016, 9:12 PM
@ -19,6 +20,19 @@ public class PrettifyHelper {
"<body onload=\"prettyPrint()\">\n" +
"<pre class=\"prettyprint linenums\">%s</pre>\n" +
"<script src=\"./js/prettify.js\"></script>\n" +
"<script>\n" +
" function scrollToLineNumber(lineNo) {\n" +
" var lists = document.getElementsByTagName(\"li\");\n" +
" for (var i = 1; i < lists.length + 1; i++) {\n" +
" console.log(lineNo + \" : \" + i);\n" +
" if (lineNo === i) {\n" +
" lists[i - 1].scrollIntoView();\n" +
" break;\n" +
" }\n" +
" }\n" +
" }\n" +
"\n" +
"</script>" +
"</body>\n" +
"</html>";
@ -53,7 +67,7 @@ public class PrettifyHelper {
"</style>";
@NonNull public static String generateContent(@NonNull String source, boolean isDark, boolean wrap) {
@NonNull public static String generateContent(@NonNull String source, boolean isDark, boolean wrap, @Nullable String url) {
return String.format(HTML_CONTENT, getStyle(isDark), wrap ? WRAPPED_STYLE : "", getFormattedSource(source));
}