MediaWiki:Common.js

Aus RI Wiki
Zur Navigation springenZur Suche springen

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
mw.hook('ve.loadModules').add(function (addPlugin) {
    addPlugin(function () {
        // Custom command for highlighting
        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());

        // Register 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 = 'insert';
        ve.ui.HighlightTool.static.icon = 'marker';
        ve.ui.HighlightTool.static.title = 'Highlight text';
        ve.ui.HighlightTool.static.commandName = 'insertHighlight';
        ve.ui.toolFactory.register(ve.ui.HighlightTool);

        console.log('Highlight tool and command registered.');
    });
});

// Run this in the browser console after opening VisualEditor
setTimeout(function() {
    if (ve.init && ve.init.target && ve.init.target.toolbar) {
        // Find the highlight tool in any group
        var allTools = [];
        ve.init.target.toolbar.tools.forEach(function(toolGroup) {
            if (toolGroup && toolGroup.tools) {
                toolGroup.tools.forEach(function(tool) {
                    if (tool && tool.constructor && tool.constructor.static) {
                        allTools.push({
                            name: tool.constructor.static.name,
                            group: tool.constructor.static.group,
                            icon: tool.constructor.static.icon
                        });
                    }
                });
            }
        });
        console.log('All registered tools:', allTools);
        
        // Check if our tool is there
        var highlightTool = allTools.find(function(t) {
            return t.name === 'highlight';
        });
        console.log('Highlight tool found:', highlightTool);
    }
}, 3000);