MediaWiki:Common.js: Unterschied zwischen den Versionen

Aus RI Wiki
Zur Navigation springenZur Suche springen
Keine Bearbeitungszusammenfassung
Markierung: Zurückgesetzt
Keine Bearbeitungszusammenfassung
Markierung: Zurückgesetzt
Zeile 1: Zeile 1:
mw.hook('ve.loadModules').add(function (addPlugin) {
mw.hook('ve.loadModules').add(function (addPlugin) {
     addPlugin(function () {
     addPlugin(function () {
         // 1. Define the custom command to handle the highlight action
         // Define and register the command
         ve.ui.HighlightCommand = function VeUiHighlightCommand() {
         ve.ui.HighlightCommand = function VeUiHighlightCommand() {
             ve.ui.HighlightCommand.super.call(this, 'insertHighlight');
             ve.ui.HighlightCommand.super.call(this, 'insertHighlight');
         };
         };
         OO.inheritClass(ve.ui.HighlightCommand, ve.ui.Command);
         OO.inheritClass(ve.ui.HighlightCommand, ve.ui.Command);
         ve.ui.HighlightCommand.prototype.execute = function (surface) {
         ve.ui.HighlightCommand.prototype.execute = function (surface) {
             var fragment = surface.getModel().getFragment();
             var fragment = surface.getModel().getFragment();
             var selectedText = fragment.getText();
             var selectedText = fragment.getText();
           
             if (!selectedText) {
             if (!selectedText) {
                // Optional: Provide user feedback if no text is selected
                 surface.getView().flash();
                 surface.getView().flash();
                 return false; // Indicate the command did not execute
                 return false;
             }
             }
            // 2. Construct the template data structure for {{Highlight|selectedText|yellow}}
             var templateData = [
             var templateData = [
                 {
                 {
Zeile 30: Zeile 25:
                                     },
                                     },
                                     params: {
                                     params: {
                                         1: { wt: selectedText }, // The selected text
                                         1: { wt: selectedText },
                                         2: { wt: 'yellow' }     // Default highlight color
                                         2: { wt: 'yellow' }
                                     }
                                     }
                                 }
                                 }
Zeile 40: Zeile 35:
                 { type: '/mwTransclusionInline' }
                 { type: '/mwTransclusionInline' }
             ];
             ];
            // 3. Replace the selected text with the template
             fragment.insertContent(templateData);
             fragment.insertContent(templateData);
             return true; // Indicate successful execution
             return true;
         };
         };
         ve.ui.commandRegistry.register(new ve.ui.HighlightCommand());
         ve.ui.commandRegistry.register(new ve.ui.HighlightCommand());


         // 4. Define and register the tool itself
         // Define and register the tool
         ve.ui.HighlightTool = function VeUiHighlightTool(toolGroup, config) {
         ve.ui.HighlightTool = function VeUiHighlightTool(toolGroup, config) {
             ve.ui.HighlightTool.super.call(this, toolGroup, config);
             ve.ui.HighlightTool.super.call(this, toolGroup, config);
         };
         };
         OO.inheritClass(ve.ui.HighlightTool, ve.ui.Tool);
         OO.inheritClass(ve.ui.HighlightTool, ve.ui.Tool);
        // *** KEY CHANGE: Set group to 'style' instead of 'insert' ***
         ve.ui.HighlightTool.static.name = 'highlight';
         ve.ui.HighlightTool.static.name = 'highlight';
         ve.ui.HighlightTool.static.group = 'style'; // Now belongs to the Style group
         ve.ui.HighlightTool.static.group = 'style'; // Correct group for formatting
         ve.ui.HighlightTool.static.icon = 'highlight';
         ve.ui.HighlightTool.static.icon = 'marker'; // Use a standard icon
         ve.ui.HighlightTool.static.title = 'Highlight text';
         ve.ui.HighlightTool.static.title = 'Highlight text';
         ve.ui.HighlightTool.static.commandName = 'insertHighlight';
         ve.ui.HighlightTool.static.commandName = 'insertHighlight';
         ve.ui.toolFactory.register(ve.ui.HighlightTool);
         ve.ui.toolFactory.register(ve.ui.HighlightTool);


         console.log('Highlight tool registered in the "style" group.');
         console.log('Highlight tool registered in the "style" group.');
        // Debug: Log after a delay to check registration
        setTimeout(function() {
            if (ve.init.target && ve.init.target.toolbar) {
                var styleGroup = ve.init.target.toolbar.groups.find(g => g.name === 'style');
                if (styleGroup) {
                    console.log('Tools in style group:', styleGroup.items.map(item => item.constructor.static.name));
                }
            }
        }, 5000);
     });
     });
});
});

Version vom 3. September 2025, 22:03 Uhr

mw.hook('ve.loadModules').add(function (addPlugin) {
    addPlugin(function () {
        // Define and register the command
        ve.ui.HighlightCommand = function VeUiHighlightCommand() {
            ve.ui.HighlightCommand.super.call(this, 'insertHighlight');
        };
        OO.inheritClass(ve.ui.HighlightCommand, ve.ui.Command);
        ve.ui.HighlightCommand.prototype.execute = function (surface) {
            var fragment = surface.getModel().getFragment();
            var selectedText = fragment.getText();
            if (!selectedText) {
                surface.getView().flash();
                return false;
            }
            var templateData = [
                {
                    type: 'mwTransclusionInline',
                    attributes: {
                        mw: {
                            parts: [{
                                template: {
                                    target: {
                                        href: 'Template:Highlight',
                                        wt: 'Highlight'
                                    },
                                    params: {
                                        1: { wt: selectedText },
                                        2: { wt: 'yellow' }
                                    }
                                }
                            }]
                        }
                    }
                },
                { type: '/mwTransclusionInline' }
            ];
            fragment.insertContent(templateData);
            return true;
        };
        ve.ui.commandRegistry.register(new ve.ui.HighlightCommand());

        // Define and register the tool
        ve.ui.HighlightTool = function VeUiHighlightTool(toolGroup, config) {
            ve.ui.HighlightTool.super.call(this, toolGroup, config);
        };
        OO.inheritClass(ve.ui.HighlightTool, ve.ui.Tool);
        ve.ui.HighlightTool.static.name = 'highlight';
        ve.ui.HighlightTool.static.group = 'style'; // Correct group for formatting
        ve.ui.HighlightTool.static.icon = 'marker'; // Use a standard icon
        ve.ui.HighlightTool.static.title = 'Highlight text';
        ve.ui.HighlightTool.static.commandName = 'insertHighlight';
        ve.ui.toolFactory.register(ve.ui.HighlightTool);

        console.log('Highlight tool registered in the "style" group.');

        // Debug: Log after a delay to check registration
        setTimeout(function() {
            if (ve.init.target && ve.init.target.toolbar) {
                var styleGroup = ve.init.target.toolbar.groups.find(g => g.name === 'style');
                if (styleGroup) {
                    console.log('Tools in style group:', styleGroup.items.map(item => item.constructor.static.name));
                }
            }
        }, 5000);
    });
});