MediaWiki:Common.js: Unterschied zwischen den Versionen

Aus RI Wiki
Zur Navigation springenZur Suche springen
Keine Bearbeitungszusammenfassung
Markierung: Manuelle Zurücksetzung
Keine Bearbeitungszusammenfassung
Zeile 1: Zeile 1:
mw.hook('ve.loadModules').add(function (addPlugin) {
mw.hook('ve.loadModules').add(function (addPlugin) {
     addPlugin(function () {
     addPlugin(function () {
         // Custom command for highlighting
         // 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');
Zeile 16: Zeile 16:
             }
             }


             var templateData = [
             // Get the original content structure instead of just text
                 {
            var selectedNodes = fragment.getSelectedNodes();
                     type: 'mwTransclusionInline',
           
                    attributes: {
            // Create an array to hold our new content
                        mw: {
            var newContent = [];
                            parts: [{
           
                                template: {
            // Process each selected node to preserve structure
                                    target: {
            for (var i = 0; i < selectedNodes.length; i++) {
                                        href: 'Template:Highlight',
                var node = selectedNodes[i];
                                        wt: 'Highlight'
                  
                if (node.type === 'paragraph') {
                     // For paragraphs, wrap the paragraph content in the template
                    var paragraphText = node.getText();
                    if (paragraphText.trim()) {
                        newContent.push(
                            {
                                type: 'paragraph',
                                children: [
                                    {
                                        type: 'mwTransclusionInline',
                                        attributes: {
                                            mw: {
                                                parts: [{
                                                    template: {
                                                        target: {
                                                            href: 'Template:Highlight',
                                                            wt: 'Highlight'
                                                        },
                                                        params: {
                                                            1: { wt: paragraphText },
                                                            2: { wt: 'yellow' }
                                                        }
                                                    }
                                                }]
                                            }
                                        }
                                     },
                                     },
                                     params: {
                                     { type: '/mwTransclusionInline' }
                                        1: { wt: selectedText },
                                ]
                                        2: { wt: 'yellow' }
                            }
                        );
                    } else {
                        // Preserve empty paragraphs
                        newContent.push({ type: 'paragraph' });
                    }
                } else if (node.isContent()) {
                    // For inline content, wrap directly in template
                    var nodeText = node.getText();
                    if (nodeText.trim()) {
                        newContent.push(
                            {
                                type: 'mwTransclusionInline',
                                attributes: {
                                    mw: {
                                        parts: [{
                                            template: {
                                                target: {
                                                    href: 'Template:Highlight',
                                                    wt: 'Highlight'
                                                },
                                                params: {
                                                    1: { wt: nodeText },
                                                    2: { wt: 'yellow' }
                                                }
                                            }
                                        }]
                                     }
                                     }
                                 }
                                 }
                             }]
                             },
                            { type: '/mwTransclusionInline' }
                        );
                    }
                }
            }
 
            // If we couldn't process structured content, fall back to simple text wrapping
            if (newContent.length === 0 && selectedText) {
                newContent = [
                    {
                        type: 'mwTransclusionInline',
                        attributes: {
                            mw: {
                                parts: [{
                                    template: {
                                        target: {
                                            href: 'Template:Highlight',
                                            wt: 'Highlight'
                                        },
                                        params: {
                                            1: { wt: selectedText },
                                            2: { wt: 'yellow' }
                                        }
                                    }
                                }]
                            }
                         }
                         }
                     }
                     },
                },
                    { type: '/mwTransclusionInline' }
                { type: '/mwTransclusionInline' }
                ];
            ];
            }


             fragment.insertContent(templateData);
             fragment.insertContent(newContent);
             return true;
             return true;
         };
         };
         ve.ui.commandRegistry.register(new ve.ui.HighlightCommand());
         ve.ui.commandRegistry.register(new ve.ui.HighlightCommand());


         // Register tool
         // 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);
Zeile 56: Zeile 134:
         ve.ui.toolFactory.register(ve.ui.HighlightTool);
         ve.ui.toolFactory.register(ve.ui.HighlightTool);


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

Version vom 3. September 2025, 22:22 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;
            }

            // Get the original content structure instead of just text
            var selectedNodes = fragment.getSelectedNodes();
            
            // Create an array to hold our new content
            var newContent = [];
            
            // Process each selected node to preserve structure
            for (var i = 0; i < selectedNodes.length; i++) {
                var node = selectedNodes[i];
                
                if (node.type === 'paragraph') {
                    // For paragraphs, wrap the paragraph content in the template
                    var paragraphText = node.getText();
                    if (paragraphText.trim()) {
                        newContent.push(
                            {
                                type: 'paragraph',
                                children: [
                                    {
                                        type: 'mwTransclusionInline',
                                        attributes: {
                                            mw: {
                                                parts: [{
                                                    template: {
                                                        target: {
                                                            href: 'Template:Highlight',
                                                            wt: 'Highlight'
                                                        },
                                                        params: {
                                                            1: { wt: paragraphText },
                                                            2: { wt: 'yellow' }
                                                        }
                                                    }
                                                }]
                                            }
                                        }
                                    },
                                    { type: '/mwTransclusionInline' }
                                ]
                            }
                        );
                    } else {
                        // Preserve empty paragraphs
                        newContent.push({ type: 'paragraph' });
                    }
                } else if (node.isContent()) {
                    // For inline content, wrap directly in template
                    var nodeText = node.getText();
                    if (nodeText.trim()) {
                        newContent.push(
                            {
                                type: 'mwTransclusionInline',
                                attributes: {
                                    mw: {
                                        parts: [{
                                            template: {
                                                target: {
                                                    href: 'Template:Highlight',
                                                    wt: 'Highlight'
                                                },
                                                params: {
                                                    1: { wt: nodeText },
                                                    2: { wt: 'yellow' }
                                                }
                                            }
                                        }]
                                    }
                                }
                            },
                            { type: '/mwTransclusionInline' }
                        );
                    }
                }
            }

            // If we couldn't process structured content, fall back to simple text wrapping
            if (newContent.length === 0 && selectedText) {
                newContent = [
                    {
                        type: 'mwTransclusionInline',
                        attributes: {
                            mw: {
                                parts: [{
                                    template: {
                                        target: {
                                            href: 'Template:Highlight',
                                            wt: 'Highlight'
                                        },
                                        params: {
                                            1: { wt: selectedText },
                                            2: { wt: 'yellow' }
                                        }
                                    }
                                }]
                            }
                        }
                    },
                    { type: '/mwTransclusionInline' }
                ];
            }

            fragment.insertContent(newContent);
            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 = 'insert';
        ve.ui.HighlightTool.static.icon = 'highlight';
        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 with structure preservation.');
    });
});