MediaWiki:Common.js: Unterschied zwischen den Versionen

Aus RI Wiki
Zur Navigation springenZur Suche springen
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
Markierung: Manuelle Zurücksetzung
 
(2 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
mw.hook('ve.loadModules').add(function (addPlugin) {
mw.hook('ve.loadModules').add(function (addPlugin) {
     addPlugin(function () {
     addPlugin(function () {
         // Define and register the command
         // 1. Define a custom annotation for highlighting
         ve.ui.HighlightCommand = function VeUiHighlightCommand() {
         ve.dm.HighlightAnnotation = function VeDmHighlightAnnotation() {
             ve.ui.HighlightCommand.super.call(this, 'insertHighlight');
             ve.dm.HighlightAnnotation.super.apply(this, arguments);
         };
         };
         OO.inheritClass(ve.ui.HighlightCommand, ve.ui.Command);
         OO.inheritClass(ve.dm.HighlightAnnotation, ve.dm.TextStyleAnnotation);
        ve.dm.HighlightAnnotation.static.name = 'textStyle/highlight';
        ve.dm.HighlightAnnotation.static.matchTagNames = ['mark'];
        ve.dm.annotationFactory.register(ve.dm.HighlightAnnotation);


         ve.ui.HighlightCommand.prototype.execute = function (surface) {
        // 2. Define the contentEditable view for the annotation
             var fragment = surface.getModel().getFragment();
         ve.ce.HighlightAnnotation = function VeCeHighlightAnnotation() {
            var selectedText = fragment.getText();
             ve.ce.HighlightAnnotation.super.apply(this, arguments);
           
        };
            if (!selectedText) {
        OO.inheritClass(ve.ce.HighlightAnnotation, ve.ce.TextStyleAnnotation);
                surface.getView().flash();
        ve.ce.HighlightAnnotation.static.name = 'textStyle/highlight';
                return false;
        ve.ce.HighlightAnnotation.static.tagName = 'mark';
            }
        ve.ce.annotationFactory.register(ve.ce.HighlightAnnotation);


            // Get the original content structure instead of just text
        // 3. Register the command to toggle the highlight
            var selectedNodes = fragment.getSelectedNodes();
        ve.ui.commandRegistry.register(
           
            new ve.ui.Command(
            // Create an array to hold our new content
                'insertHighlight',  
            var newContent = [];
                'annotation',  
           
                'toggle',
            // Process each selected node to preserve structure
                {  
            for (var i = 0; i < selectedNodes.length; i++) {
                    args: ['textStyle/highlight'],  
                var node = selectedNodes[i];
                     supportedSelections: ['linear', 'table']  
               
                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
         // 4. Register the tool (now inherits from AnnotationTool)
         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.AnnotationTool);
         ve.ui.HighlightTool.static.name = 'highlight';
         ve.ui.HighlightTool.static.name = 'highlight';
         ve.ui.HighlightTool.static.group = 'insert';
         ve.ui.HighlightTool.static.group = 'style'; // Can now go in style group!
         ve.ui.HighlightTool.static.icon = 'highlight';
         ve.ui.HighlightTool.static.icon = 'highlight';
         ve.ui.HighlightTool.static.title = 'Highlight text';
         ve.ui.HighlightTool.static.title = 'Highlight text';
        ve.ui.HighlightTool.static.annotation = { name: 'textStyle/highlight' };
         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 with structure preservation.');
         console.log('Highlight tool registered with custom annotation.');
     });
     });
});
});

Aktuelle Version vom 5. September 2025, 13:03 Uhr

mw.hook('ve.loadModules').add(function (addPlugin) {
    addPlugin(function () {
        // 1. Define a custom annotation for highlighting
        ve.dm.HighlightAnnotation = function VeDmHighlightAnnotation() {
            ve.dm.HighlightAnnotation.super.apply(this, arguments);
        };
        OO.inheritClass(ve.dm.HighlightAnnotation, ve.dm.TextStyleAnnotation);
        ve.dm.HighlightAnnotation.static.name = 'textStyle/highlight';
        ve.dm.HighlightAnnotation.static.matchTagNames = ['mark'];
        ve.dm.annotationFactory.register(ve.dm.HighlightAnnotation);

        // 2. Define the contentEditable view for the annotation
        ve.ce.HighlightAnnotation = function VeCeHighlightAnnotation() {
            ve.ce.HighlightAnnotation.super.apply(this, arguments);
        };
        OO.inheritClass(ve.ce.HighlightAnnotation, ve.ce.TextStyleAnnotation);
        ve.ce.HighlightAnnotation.static.name = 'textStyle/highlight';
        ve.ce.HighlightAnnotation.static.tagName = 'mark';
        ve.ce.annotationFactory.register(ve.ce.HighlightAnnotation);

        // 3. Register the command to toggle the highlight
        ve.ui.commandRegistry.register(
            new ve.ui.Command(
                'insertHighlight', 
                'annotation', 
                'toggle',
                { 
                    args: ['textStyle/highlight'], 
                    supportedSelections: ['linear', 'table'] 
                }
            )
        );

        // 4. Register the tool (now inherits from AnnotationTool)
        ve.ui.HighlightTool = function VeUiHighlightTool(toolGroup, config) {
            ve.ui.HighlightTool.super.call(this, toolGroup, config);
        };
        OO.inheritClass(ve.ui.HighlightTool, ve.ui.AnnotationTool);
        ve.ui.HighlightTool.static.name = 'highlight';
        ve.ui.HighlightTool.static.group = 'style'; // Can now go in style group!
        ve.ui.HighlightTool.static.icon = 'highlight';
        ve.ui.HighlightTool.static.title = 'Highlight text';
        ve.ui.HighlightTool.static.annotation = { name: 'textStyle/highlight' };
        ve.ui.HighlightTool.static.commandName = 'insertHighlight';
        ve.ui.toolFactory.register(ve.ui.HighlightTool);

        console.log('Highlight tool registered with custom annotation.');
    });
});