MediaWiki:Common.js
Aus RI Wiki
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 () {
// 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.');
});
});