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 | ||
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 12: | Zeile 12: | ||
if (!selectedText) { | if (!selectedText) { | ||
// Optional: Provide user feedback if no text is selected | |||
surface.getView().flash(); | surface.getView().flash(); | ||
return false; | return false; // Indicate the command did not execute | ||
} | } | ||
// 2. Construct the template data structure for {{Highlight|selectedText|yellow}} | |||
var templateData = [ | var templateData = [ | ||
{ | { | ||
Zeile 28: | Zeile 30: | ||
}, | }, | ||
params: { | params: { | ||
1: { wt: selectedText }, | 1: { wt: selectedText }, // The selected text | ||
2: { wt: 'yellow' } | 2: { wt: 'yellow' } // Default highlight color | ||
} | } | ||
} | } | ||
Zeile 39: | Zeile 41: | ||
]; | ]; | ||
// 3. Replace the selected text with the template | |||
fragment.insertContent(templateData); | fragment.insertContent(templateData); | ||
return true; | return true; // Indicate successful execution | ||
}; | }; | ||
ve.ui.commandRegistry.register(new ve.ui.HighlightCommand()); | ve.ui.commandRegistry.register(new ve.ui.HighlightCommand()); | ||
// | // 4. Define and register the tool itself | ||
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'; | ve.ui.HighlightTool.static.group = 'style'; // Now belongs to the 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.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 | console.log('Highlight tool registered in the "style" group.'); | ||
}); | }); | ||
}); | }); |
Version vom 3. September 2025, 22:01 Uhr
mw.hook('ve.loadModules').add(function (addPlugin) {
addPlugin(function () {
// 1. Define the custom command to handle the highlight action
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) {
// Optional: Provide user feedback if no text is selected
surface.getView().flash();
return false; // Indicate the command did not execute
}
// 2. Construct the template data structure for {{Highlight|selectedText|yellow}}
var templateData = [
{
type: 'mwTransclusionInline',
attributes: {
mw: {
parts: [{
template: {
target: {
href: 'Template:Highlight',
wt: 'Highlight'
},
params: {
1: { wt: selectedText }, // The selected text
2: { wt: 'yellow' } // Default highlight color
}
}
}]
}
}
},
{ type: '/mwTransclusionInline' }
];
// 3. Replace the selected text with the template
fragment.insertContent(templateData);
return true; // Indicate successful execution
};
ve.ui.commandRegistry.register(new ve.ui.HighlightCommand());
// 4. Define and register the tool itself
ve.ui.HighlightTool = function VeUiHighlightTool(toolGroup, config) {
ve.ui.HighlightTool.super.call(this, toolGroup, config);
};
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.group = 'style'; // Now belongs to the Style group
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 in the "style" group.');
});
});