Skip to content

Commit

Permalink
Better handling of selection
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Oct 31, 2023
1 parent 63fe2b9 commit 32031b6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ protected final boolean runCommonAction(final @StringRes int action) {
}
case R.string.abid_common_insert_snippet: {
MarkorDialogFactory.showInsertSnippetDialog(_activity, (snip) -> {
TextViewUtils.interpolatePlaceholders(snip, _document.getTitle(), _hlEditor::insertOrReplaceTextOnCursor);
TextViewUtils.interpolateSnippet(snip, _document.getTitle(), TextViewUtils.getSelectedText(_hlEditor), _hlEditor::insertOrReplaceTextOnCursor);
_lastSnip = snip;
});
return true;
Expand Down Expand Up @@ -693,7 +693,7 @@ protected final boolean runCommonLongPressAction(@StringRes int action) {
}
case R.string.abid_common_insert_snippet: {
if (!TextUtils.isEmpty(_lastSnip)) {
TextViewUtils.interpolatePlaceholders(_lastSnip, _document.getTitle(), _hlEditor::insertOrReplaceTextOnCursor);
TextViewUtils.interpolateSnippet(_lastSnip, _document.getTitle(), TextViewUtils.getSelectedText(_hlEditor), _hlEditor::insertOrReplaceTextOnCursor);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class MarkdownActionButtons extends ActionButtonBase {
// Group 1 matches text, group 2 matches path
private static final Pattern MARKDOWN_LINK = Pattern.compile("\\[([^\\]]*)\\]\\(([^)]+)\\)");

private static final Pattern WEB_URL = Pattern.compile("https?://[^\\s/$.?#].[^\\s]*");

private final Set<Integer> _disabledHeadings = new HashSet<>();

public MarkdownActionButtons(@NonNull Context context, Document document) {
Expand Down Expand Up @@ -189,14 +191,18 @@ public boolean onActionLongClick(final @StringRes int action) {

private boolean followLinkUnderCursor() {
final int sel = TextViewUtils.getSelection(_hlEditor)[0];
if (sel < 0) {
return false;
}

final String line = TextViewUtils.getSelectedLines(_hlEditor, sel);
final int cursor = sel - TextViewUtils.getLineStart(_hlEditor.getText(), sel);

final Matcher m = MARKDOWN_LINK.matcher(line);
while (m.find()) {
final String group = m.group(2);
if (m.start() <= cursor && m.end() > cursor && group != null) {
if (Patterns.WEB_URL.matcher(group).matches()) {
if (WEB_URL.matcher(group).matches()) {
GsContextUtils.instance.openWebpageInExternalBrowser(getActivity(), group);
return true;
} else {
Expand Down

0 comments on commit 32031b6

Please sign in to comment.