User:BZPN/PNHelper.js

From Test Wiki
Revision as of 16:08, 20 November 2024 by BZPN (talk | contribs)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// <nowiki>
mw.loader.using(['oojs-ui-core', 'oojs-ui-windows', 'oojs-ui-widgets'], function () {
    mw.hook('wikipage.content').add(function ($content) {
        // Tworzenie przycisku
        var $button = $('<li id="t-custom-task"><a href="#"></a></li>');
        $button.find('a').text('Dodaj zadanie');

        // Dodanie przycisku do paska narzędzi
        $('#p-tb ul').append($button);

        // Funkcja tworząca okno dialogowe
        function createDialog() {
            var dialog = new OO.ui.ProcessDialog({
                size: 'large',
                title: 'Dodaj nowe zadanie'
            });

            // Tworzenie pól formularza
            var titleInput = new OO.ui.TextInputWidget({
                placeholder: 'Wpisz tytuł zadania'
            });

            var descriptionInput = new OO.ui.MultilineTextInputWidget({
                placeholder: 'Wpisz opis zadania',
                rows: 10,
                autosize: true
            });

            // Przyciski edycji tekstu
            var boldButton = new OO.ui.ButtonWidget({
                icon: 'bold',
                title: 'Pogrubienie'
            });
            var italicButton = new OO.ui.ButtonWidget({
                icon: 'italic',
                title: 'Kursywa'
            });
            var underlineButton = new OO.ui.ButtonWidget({
                icon: 'underline',
                title: 'Podkreślenie'
            });
            var strikethroughButton = new OO.ui.ButtonWidget({
                icon: 'strikethrough',
                title: 'Przekreślenie'
            });
            var subscriptButton = new OO.ui.ButtonWidget({
                icon: 'subscript',
                title: 'Indeks dolny'
            });
            var superscriptButton = new OO.ui.ButtonWidget({
                icon: 'superscript',
                title: 'Indeks górny'
            });
            var linkButton = new OO.ui.ButtonWidget({
                icon: 'link',
                title: 'Wstaw link'
            });

            var editButtons = new OO.ui.ButtonGroupWidget({
                items: [boldButton, italicButton, underlineButton, strikethroughButton, subscriptButton, superscriptButton, linkButton]
            });

            // Funkcje edycji tekstu
            function wrapSelection(before, after) {
                var textarea = descriptionInput.$input[0];
                var start = textarea.selectionStart;
                var end = textarea.selectionEnd;
                var selectedText = textarea.value.substring(start, end);
                var replacement = before + selectedText + after;
                textarea.value = textarea.value.substring(0, start) + replacement + textarea.value.substring(end);
                descriptionInput.setValue(textarea.value);
            }

            boldButton.on('click', function() { wrapSelection("'''", "'''"); });
            italicButton.on('click', function() { wrapSelection("''", "''"); });
            underlineButton.on('click', function() { wrapSelection("<u>", "</u>"); });
            strikethroughButton.on('click', function() { wrapSelection("<s>", "</s>"); });
            subscriptButton.on('click', function() { wrapSelection("<sub>", "</sub>"); });
            superscriptButton.on('click', function() { wrapSelection("<sup>", "</sup>"); });
            linkButton.on('click', function() {
                var selectedText = descriptionInput.$input[0].value.substring(
                    descriptionInput.$input[0].selectionStart,
                    descriptionInput.$input[0].selectionEnd
                );
                if (selectedText) {
                    wrapSelection("[[", "]]");
                } else {
                    alert('Zaznacz tekst, aby dodać link.');
                }
            });

            // Konfiguracja okna dialogowego
            dialog.$body.append(
                new OO.ui.FieldsetLayout({
                    items: [
                        new OO.ui.FieldLayout(titleInput, {
                            label: 'Tytuł zadania',
                            align: 'top'
                        }),
                        new OO.ui.FieldLayout(editButtons, {
                            label: 'Edycja tekstu',
                            align: 'top'
                        }),
                        new OO.ui.FieldLayout(descriptionInput, {
                            label: 'Opis zadania',
                            align: 'top'
                        })
                    ]
                }).$element
            );

            // Przyciski akcji
            dialog.actions = [
                { action: 'cancel', label: 'Anuluj', flags: ['safe', 'close'] },
                { action: 'submit', label: 'Zapisz', flags: ['primary', 'progressive'] }
            ];

            // Obsługa akcji
            dialog.getActionProcess = function (action) {
                if (action === 'submit') {
                    return new OO.ui.Process(function () {
                        console.log('Zapisano zadanie:', titleInput.getValue(), descriptionInput.getValue());
                        dialog.close();
                    });
                }
                return dialog.parent.getActionProcess.call(this, action);
            };

            // Otwieranie okna dialogowego
            var windowManager = new OO.ui.WindowManager();
            $('body').append(windowManager.$element);
            windowManager.addWindows([dialog]);
            windowManager.openWindow(dialog);
        }

        // Dodanie obsługi kliknięcia przycisku
        $button.on('click', function (e) {
            e.preventDefault();
            createDialog();
        });
    });
});
// </nowiki>