News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Improved Quote Selected Text on mobile devices (SMF 2.1)

Started by Daretary, June 13, 2023, 06:30:31 AM

Previous topic - Next topic

Daretary

Here we discussed this issue. But we couldn't solve the problem properly.

I worked long and hard on this. And now Quote Selected Text works great on both computers and mobile devices.

You need to rename the Themes/default/scripts/quotedText.js file to, for example, quotedText-old.js

Instead, place a file quotedText.js with the following code in that folder:

function getClosest(el, divID) {
    if (typeof divID == 'undefined' || divID == false)
        return null;
    while (el) {
        if (el.id === divID)
            return el;

        el = el.parentNode;
    }
    return null;
}


function getSelectedText() {
    var text = '';
    if (typeof window.getSelection != 'undefined') {
        text = window.getSelection().toString();
    } else if (typeof document.selection != 'undefined' && document.selection.type == 'Text') {
        text = document.selection.createRange().text;
    }
    return text;
}


function quotedTextClick(oOptions) {
    text = '';
    $('#quoteSelected_' + oOptions.msgID).hide();
    $.ajax({
        url: smf_prepareScriptUrl(smf_scripturl) + 'action=quotefast;quote=' + oOptions.msgID + ';xml;pb=' + oEditorID + ';mode=' + (oEditorObject.bRichTextEnabled ? 1 : 0),
        type: 'GET',
        headers: {
            "X-SMF-AJAX": 1
        },
        xhrFields: {
            withCredentials: typeof allow_xhjr_credentials !== "undefined" ? allow_xhjr_credentials : false
        },
        dataType: 'xml',
        beforeSend: function () {
            ajax_indicator(true);
        },
        complete: function (jqXHR, textStatus) {
            ajax_indicator(false);
        },
        success: function (data, textStatus, xhr) {
            oOptions.text = oOptions.text.replaceAll(/<img src=".*?" alt="(.*?)" title=".*?" class="smiley">/, '$1');
            text = $(data).find('quote').text();
            text = text.match(/^\[quote(.*)]/ig) + oOptions.text + '[/quote]' + '\n\n';
            var e = $('#' + oEditorID).get(0);
            var oEditor = sceditor.instance(e);
            text = oEditor.toBBCode(text);
            oEditor.insert(text);
            if (typeof oJumpAnchor != 'undefined') {
                if (navigator.appName == 'Microsoft Internet Explorer')
                    window.location.hash = oJumpAnchor;
                else
                    window.location.hash = '#' + oJumpAnchor;
            }
        },
        error: function (xhr, textStatus, errorThrown) {
        }
    });
}

$(function () {
    var oSelected = {
        divID: null,
        msgID: null,
        text: null,
    };

    $(document).on('mousedown touchstart', '.inner, .list_posts', function () {
        oSelected.divID = $(this).attr('id');
        oSelected.msgID = $(this).data('msgid');
        $('#quoteSelected_' + oSelected.msgID).show();
        $(document).off('mouseup touchend', '#quoteSelected_' + oSelected.msgID + ' a');
    });

    $(document).on('mouseup touchend', function (e) {
        oSelected.text = getSelectedText(oSelected.divID);
        if (typeof oSelected.text == 'undefined' || oSelected.text == false || oSelected.text == '')
            return true;
        $(document).one('mouseup touchend', '#quoteSelected_' + oSelected.msgID + ' a', function (e) {
            e.preventDefault();
            quotedTextClick(oSelected);
        });

        $(document).on('mouseup.ondeselecttext touchend.ondeselecttext' + oSelected.msgID, function () {
            setTimeout(function () {
                selectedText = getSelectedText(oSelected.divID);

                if (typeof selectedText != 'undefined' && selectedText != false)
                    return;

                $(document).off('mouseup touchend', '#quoteSelected_' + oSelected.msgID + ' a');

                $('#quoteSelected_' + oSelected.msgID).hide();

                $(document).off('mouseup.ondeselecttext touchend.ondeselecttext' + oSelected.msgID);
            }, 1);
        });

        return true;
    });
});


After that, in the admin panel, delete the minified_*.

Julius_2000


CRM 114

Quote from: Daretary on June 13, 2023, 06:30:31 AMI worked long and hard on this. And now Quote Selected Text works great on both computers and mobile devices.

Works great on my Smart Phone with FOSS Browser. Thanks a lot!
German Wet Shaving Forum: www.gut-rasiert.de/forum

live627

Thanks for sharing! Do you mind if I add this to the repository so that it can be in a future version of SMF?

Daretary

live627, of course. I will be glad if it helps to improve SMF. :)

Julius_2000

May I report a minor "bug", if you will? The label "quote selected" appears immediately when you click/ touch the post's area even without having selected any content. Is there a way to only do if there's anything actually highlighted or is it a compromise of the workaround for this feature?

Daretary

Yes, I am aware of this feature. I found this helpful for those new to the community. This way they will understand that there is a «QUOTE SELECTED TEXT» button and quickly get used to it. I have noticed that it is the new members who often only use the «QUOTE» button. Therefore, overquoting occurs.
And this way I was able to solve the problem of the button disappearing in Chromium-based mobile browsers.

The more important problem with this script is that it is possible to accidentally highlight the next (or previous) message. Thus, the quote will contain the text of more than one message. Moreover, service information will be included in the quote. But this is a rare case - for this you need to specially try. :)

HendriXXX

Thanks! This was good tip!
I had to shorten "quote selected text" to "Q. selected", so i could fit all in same line with NameX theme.

Antechinus


Daretary

In fact, there are several bugs to overcome.
For example, in some browsers sometimes the quote is duplicated.


Helgi

Regarding this fix.
Sometimes some users have duplicate quotes.
To prevent this from happening, in the Subs-Post.php file before the
// Fix up some use of tables without [tr]s, etc. (it has to be done more than once to catch it all.)add
// Removing duplicate quotes
$pattern = '/^(\[quote[^\]]+\].*?\[\/quote\])(?:\s*^\1$)+/ms';
$message = preg_replace($pattern, '$1', $message);

Helgi

Sometimes the first quote is a continuation of the previous line, so the correction in the previous post does not work. There are different ways to fix it, but here is the simplest:

// Removing duplicate quotes
$message = preg_replace('/(?<!\n)\[quote/', "\n[quote", $message);
$message = preg_replace('/^(\[quote[^\]]+\].*?\[\/quote\])(?:\s*^\1$)+/ms', '$1', $message);

Advertisement: