Search in sources :

Example 1 with HtmlRenderer

use of org.commonmark.html.HtmlRenderer in project Slide by ccrama.

the class DoEditorActions method doActions.

public static void doActions(final EditText editText, final View baseView, final FragmentManager fm, final Activity a, final String oldComment, @Nullable final String[] authors) {
    baseView.findViewById(R.id.bold).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (editText.hasSelection()) {
                wrapString("**", // If the user has text selected, wrap that text in the symbols
                editText);
            } else {
                // If the user doesn't have text selected, put the symbols around the cursor's position
                int pos = editText.getSelectionStart();
                editText.getText().insert(pos, "**");
                editText.getText().insert(pos + 1, "**");
                // put the cursor between the symbols
                editText.setSelection(pos + 2);
            }
        }
    });
    if (baseView.findViewById(R.id.author) != null) {
        if (authors != null && authors.length > 0) {
            baseView.findViewById(R.id.author).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (authors.length == 1) {
                        int pos = editText.getSelectionStart();
                        String author = "/u/" + authors[0];
                        editText.setText(editText.getText().toString() + author);
                        // put the cursor between the symbols
                        editText.setSelection(pos + author.length());
                    } else {
                        new AlertDialogWrapper.Builder(a).setTitle(R.string.authors_above).setItems(authors, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                int pos = editText.getSelectionStart();
                                String author = "/u/" + authors[which];
                                editText.setText(editText.getText().toString() + author);
                                // put the cursor between the symbols
                                editText.setSelection(pos + author.length());
                            }
                        }).setNeutralButton(R.string.btn_cancel, null).show();
                    }
                }
            });
        } else {
            baseView.findViewById(R.id.author).setVisibility(View.GONE);
        }
    }
    baseView.findViewById(R.id.italics).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (editText.hasSelection()) {
                wrapString("*", // If the user has text selected, wrap that text in the symbols
                editText);
            } else {
                // If the user doesn't have text selected, put the symbols around the cursor's position
                int pos = editText.getSelectionStart();
                editText.getText().insert(pos, "*");
                editText.getText().insert(pos + 1, "*");
                // put the cursor between the symbols
                editText.setSelection(pos + 1);
            }
        }
    });
    baseView.findViewById(R.id.strike).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (editText.hasSelection()) {
                wrapString("~~", // If the user has text selected, wrap that text in the symbols
                editText);
            } else {
                // If the user doesn't have text selected, put the symbols around the cursor's position
                int pos = editText.getSelectionStart();
                editText.getText().insert(pos, "~~");
                editText.getText().insert(pos + 1, "~~");
                // put the cursor between the symbols
                editText.setSelection(pos + 2);
            }
        }
    });
    baseView.findViewById(R.id.savedraft).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Drafts.addDraft(editText.getText().toString());
            Snackbar s = Snackbar.make(baseView.findViewById(R.id.savedraft), "Draft saved", Snackbar.LENGTH_SHORT);
            View view = s.getView();
            TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
            tv.setTextColor(Color.WHITE);
            s.setAction(R.string.btn_discard, new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    Drafts.deleteDraft(Drafts.getDrafts().size() - 1);
                }
            });
            s.show();
        }
    });
    baseView.findViewById(R.id.draft).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final ArrayList<String> drafts = Drafts.getDrafts();
            Collections.reverse(drafts);
            final String[] draftText = new String[drafts.size()];
            for (int i = 0; i < drafts.size(); i++) {
                draftText[i] = drafts.get(i);
            }
            if (drafts.isEmpty()) {
                new AlertDialogWrapper.Builder(a).setTitle(R.string.dialog_no_drafts).setMessage(R.string.dialog_no_drafts_msg).setPositiveButton(R.string.btn_ok, null).show();
            } else {
                new AlertDialogWrapper.Builder(a).setTitle(R.string.choose_draft).setItems(draftText, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        editText.setText(editText.getText().toString() + draftText[which]);
                    }
                }).setNeutralButton(R.string.btn_cancel, null).setPositiveButton(R.string.manage_drafts, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        final boolean[] selected = new boolean[drafts.size()];
                        new AlertDialogWrapper.Builder(a).setTitle(R.string.choose_draft).setNeutralButton(R.string.btn_cancel, null).alwaysCallMultiChoiceCallback().setNegativeButton(R.string.btn_delete, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                new AlertDialogWrapper.Builder(a).setTitle(R.string.really_delete_drafts).setCancelable(false).setPositiveButton(R.string.btn_yes, new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        ArrayList<String> draf = new ArrayList<>();
                                        for (int i = 0; i < draftText.length; i++) {
                                            if (!selected[i]) {
                                                draf.add(draftText[i]);
                                            }
                                        }
                                        Drafts.save(draf);
                                    }
                                }).setNegativeButton(R.string.btn_no, null).show();
                            }
                        }).setMultiChoiceItems(draftText, selected, new DialogInterface.OnMultiChoiceClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                selected[which] = isChecked;
                            }
                        }).show();
                    }
                }).show();
            }
        }
    });
    /*todo baseView.findViewById(R.id.strikethrough).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wrapString("~~", editText);
            }
        });*/
    baseView.findViewById(R.id.imagerep).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            e = editText.getText();
            sStart = editText.getSelectionStart();
            sEnd = editText.getSelectionEnd();
            TedBottomPicker tedBottomPicker = new TedBottomPicker.Builder(editText.getContext()).setOnImageSelectedListener(new TedBottomPicker.OnImageSelectedListener() {

                @Override
                public void onImageSelected(List<Uri> uri) {
                    handleImageIntent(uri, editText, a);
                }
            }).setLayoutResource(R.layout.image_sheet_dialog).setTitle("Choose a photo").create();
            tedBottomPicker.show(fm);
            InputMethodManager imm = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        }
    });
    baseView.findViewById(R.id.draw).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (SettingValues.tabletUI) {
                doDraw(a, editText, fm);
            } else {
                AlertDialogWrapper.Builder b = new AlertDialogWrapper.Builder(a).setTitle(R.string.general_cropdraw_ispro).setMessage(R.string.pro_upgrade_msg).setPositiveButton(R.string.btn_yes_exclaim, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        try {
                            a.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=me.ccrama.slideforreddittabletuiunlock")));
                        } catch (ActivityNotFoundException e) {
                            a.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=me.ccrama.slideforreddittabletuiunlock")));
                        }
                    }
                }).setNegativeButton(R.string.btn_no_danks, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.dismiss();
                    }
                });
                if (SettingValues.previews > 0) {
                    b.setNeutralButton(a.getString(R.string.pro_previews, SettingValues.previews), new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            SettingValues.prefs.edit().putInt(SettingValues.PREVIEWS_LEFT, SettingValues.previews - 1).apply();
                            SettingValues.previews = SettingValues.prefs.getInt(SettingValues.PREVIEWS_LEFT, 10);
                            doDraw(a, editText, fm);
                        }
                    });
                }
                b.show();
            }
        }
    });
    /*todo baseView.findViewById(R.id.superscript).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insertBefore("^", editText);
            }
        });*/
    baseView.findViewById(R.id.size).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            insertBefore("#", editText);
        }
    });
    baseView.findViewById(R.id.quote).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (oldComment != null) {
                final TextView showText = new TextView(a);
                showText.setText(oldComment);
                showText.setTextIsSelectable(true);
                int sixteen = Reddit.dpToPxVertical(24);
                showText.setPadding(sixteen, 0, sixteen, 0);
                AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(a);
                builder.setView(showText).setTitle(R.string.editor_actions_quote_comment).setCancelable(true).setPositiveButton(a.getString(R.string.btn_select), new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String selected = showText.getText().toString().substring(showText.getSelectionStart(), showText.getSelectionEnd());
                        if (selected.equals("")) {
                            insertBefore("> " + oldComment, editText);
                        } else {
                            insertBefore("> " + selected + "\n\n", editText);
                        }
                    }
                }).setNegativeButton(a.getString(R.string.btn_cancel), null).show();
            } else {
                insertBefore("> ", editText);
            }
        }
    });
    baseView.findViewById(R.id.bulletlist).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            insertBefore("* ", editText);
        }
    });
    baseView.findViewById(R.id.numlist).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            insertBefore("1. ", editText);
        }
    });
    baseView.findViewById(R.id.preview).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            List<Extension> extensions = Arrays.asList(TablesExtension.create(), StrikethroughExtension.create());
            Parser parser = Parser.builder().extensions(extensions).build();
            HtmlRenderer renderer = HtmlRenderer.builder().extensions(extensions).build();
            Node document = parser.parse(editText.getText().toString());
            String html = renderer.render(document);
            LayoutInflater inflater = a.getLayoutInflater();
            final View dialoglayout = inflater.inflate(R.layout.parent_comment_dialog, null);
            final AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(a);
            setViews(html, "NO sub", (SpoilerRobotoTextView) dialoglayout.findViewById(R.id.firstTextView), (CommentOverflow) dialoglayout.findViewById(R.id.commentOverflow));
            builder.setView(dialoglayout);
            builder.show();
        }
    });
    baseView.findViewById(R.id.link).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final LayoutInflater inflater = LayoutInflater.from(a);
            final LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.insert_link, null);
            int[] attrs = { R.attr.fontColor };
            TypedArray ta = baseView.getContext().obtainStyledAttributes(new ColorPreferences(baseView.getContext()).getFontStyle().getBaseId(), attrs);
            ta.recycle();
            String selectedText = "";
            // if the user highlighted text before inputting a URL, use that text for the descriptionBox
            if (editText.hasSelection()) {
                final int startSelection = editText.getSelectionStart();
                final int endSelection = editText.getSelectionEnd();
                selectedText = editText.getText().toString().substring(startSelection, endSelection);
            }
            final boolean selectedTextNotEmpty = !selectedText.isEmpty();
            final MaterialDialog dialog = new MaterialDialog.Builder(editText.getContext()).title(R.string.editor_title_link).customView(layout, false).positiveColorAttr(R.attr.tintColor).positiveText(R.string.editor_action_link).onPositive(new MaterialDialog.SingleButtonCallback() {

                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    final EditText urlBox = (EditText) dialog.findViewById(R.id.url_box);
                    final EditText textBox = (EditText) dialog.findViewById(R.id.text_box);
                    dialog.dismiss();
                    final String s = "[".concat(textBox.getText().toString()).concat("](").concat(urlBox.getText().toString()).concat(")");
                    int start = Math.max(editText.getSelectionStart(), 0);
                    int end = Math.max(editText.getSelectionEnd(), 0);
                    editText.getText().insert(Math.max(start, end), s);
                    // delete the selected text to avoid duplication
                    if (selectedTextNotEmpty) {
                        editText.getText().delete(start, end);
                    }
                }
            }).build();
            // Tint the hint text if the base theme is Sepia
            if (SettingValues.currentTheme == 5) {
                ((EditText) dialog.findViewById(R.id.url_box)).setHintTextColor(ContextCompat.getColor(dialog.getContext(), R.color.md_grey_600));
                ((EditText) dialog.findViewById(R.id.text_box)).setHintTextColor(ContextCompat.getColor(dialog.getContext(), R.color.md_grey_600));
            }
            // use the selected text as the text for the link
            if (!selectedText.isEmpty()) {
                ((EditText) dialog.findViewById(R.id.text_box)).setText(selectedText);
            }
            dialog.show();
        }
    });
    try {
        ((ImageInsertEditText) editText).setImageSelectedCallback(new ImageInsertEditText.ImageSelectedCallback() {

            @Override
            public void onImageSelected(final Uri content, String mimeType) {
                e = editText.getText();
                sStart = editText.getSelectionStart();
                sEnd = editText.getSelectionEnd();
                handleImageIntent(new ArrayList<Uri>() {

                    {
                        add(content);
                    }
                }, editText, a);
            }
        });
    } catch (Exception e) {
    // if thrown, there is likely an issue implementing this on the user's version of Android. There shouldn't be an issue, but just in case
    }
}
Also used : DialogInterface(android.content.DialogInterface) Node(org.commonmark.node.Node) ArrayList(java.util.ArrayList) InputMethodManager(android.view.inputmethod.InputMethodManager) Uri(android.net.Uri) SpoilerRobotoTextView(me.ccrama.redditslide.SpoilerRobotoTextView) AlertDialogWrapper(com.afollestad.materialdialogs.AlertDialogWrapper) TypedArray(android.content.res.TypedArray) SpoilerRobotoTextView(me.ccrama.redditslide.SpoilerRobotoTextView) TextView(android.widget.TextView) List(java.util.List) ArrayList(java.util.ArrayList) EditText(android.widget.EditText) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) ColorPreferences(me.ccrama.redditslide.ColorPreferences) Intent(android.content.Intent) SpoilerRobotoTextView(me.ccrama.redditslide.SpoilerRobotoTextView) View(android.view.View) TextView(android.widget.TextView) FileNotFoundException(java.io.FileNotFoundException) ActivityNotFoundException(android.content.ActivityNotFoundException) IOException(java.io.IOException) Parser(org.commonmark.parser.Parser) SubmissionParser(me.ccrama.redditslide.util.SubmissionParser) ActivityNotFoundException(android.content.ActivityNotFoundException) DialogAction(com.afollestad.materialdialogs.DialogAction) LayoutInflater(android.view.LayoutInflater) HtmlRenderer(org.commonmark.html.HtmlRenderer) TedBottomPicker(gun0912.tedbottompicker.TedBottomPicker) LinearLayout(android.widget.LinearLayout) Snackbar(android.support.design.widget.Snackbar)

Aggregations

ActivityNotFoundException (android.content.ActivityNotFoundException)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 TypedArray (android.content.res.TypedArray)1 Uri (android.net.Uri)1 Snackbar (android.support.design.widget.Snackbar)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 InputMethodManager (android.view.inputmethod.InputMethodManager)1 EditText (android.widget.EditText)1 LinearLayout (android.widget.LinearLayout)1 TextView (android.widget.TextView)1 AlertDialogWrapper (com.afollestad.materialdialogs.AlertDialogWrapper)1 DialogAction (com.afollestad.materialdialogs.DialogAction)1 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)1 TedBottomPicker (gun0912.tedbottompicker.TedBottomPicker)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1