Utente:Nastoshka/SmartEdittools2.js

Da Wikivoyage.

Nota: dopo aver salvato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.

  • Firefox / Safari: tenere premuto il tasto delle maiuscole e fare clic su Ricarica, oppure premere Ctrl-F5 o Ctrl-R (⌘-R su Mac)
  • Google Chrome: premere Ctrl-Shift-R (⌘-Shift-R su un Mac)
  • Internet Explorer: tenere premuto il tasto Ctrl mentre si fa clic su Refresh, oppure premere Ctrl-F5
  • Opera: svuotare completamente la cache dal menu Strumenti → Preferenze
var specialChars = [
    { displayText: 'Smile', value: '😀' },
    { displayText: 'Firma', value: '<nowiki>--~~~~</nowiki>' },
    { displayText: 'Template', value: '{{}}' }
];

function initReplyCharacterInsert() {
    // Check if the new reply tool is enabled
    if (mw.config.get('wgDiscussionToolsFeaturesEnabled')) {
        waitForElement(".ext-discussiontools-ui-replyWidget-bodyWrapper", 10, 500, function(bodyWrapper) {
            if (bodyWrapper && !document.getElementById('customCharInsertButtons')) {
                const placeholder = bodyWrapper.querySelector('.ve-ui-surface-placeholder');
                if (placeholder) {
                    placeholder.style.display = 'none'; // Hide placeholder
                    placeholder.classList.add('oo-ui-element-hidden');
                }
                insertCharacterButtons(bodyWrapper);
            }
        });
    }
}

function insertCharacterButtons(bodyWrapper) {
    const buttonContainer = document.createElement('div');
    buttonContainer.id = 'customCharInsertButtons';

    specialChars.forEach(charObj => {
        const button = document.createElement('button');
        button.textContent = charObj.displayText;
        button.style.margin = '5px';
        button.onclick = () => {
            const contentEditableDiv = bodyWrapper.querySelector('.ve-ce-documentNode[contenteditable="true"]');
            insertHtmlAtCursor(contentEditableDiv, charObj.value);
        };
        buttonContainer.appendChild(button);
    });

    bodyWrapper.parentNode.insertBefore(buttonContainer, bodyWrapper.nextSibling);
}

function insertHtmlAtCursor(contentEditableDiv, html) {
    contentEditableDiv.focus();
    document.execCommand('insertHTML', false, html);
}

function waitForElement(selector, attemptsLeft, interval, callback) {
    const element = document.querySelector(selector);
    if (element) {
        callback(element);
    } else if (attemptsLeft > 0) {
        setTimeout(() => {
            waitForElement(selector, attemptsLeft - 1, interval, callback);
        }, interval);
    } else {
        callback(null);
    }
}

$(document).ready(function() {
    initReplyCharacterInsert(); // Initialize the character insert functionality
});