MediaWiki
Gadget-ve-sandbox.js
From Dogcraft Wiki
(Undo revision 24949 by Domino) Tag: Undo |
m (tentatively testing transforming the thing to targeted task) |
||
Line 1: | Line 1: | ||
mw.loader.using( [ 'ext.visualEditor.core', 'ext.visualEditor.mwtransclusion' ] ).then(function () { | mw.loader.using( [ 'ext.visualEditor.core', 'ext.visualEditor.mwtransclusion' ] ).then(function () { | ||
// --------- (start of ve.ui. | // --------- (start of ve.ui.ColumnAction definition) ----------------------------------------------- | ||
// This is based on [lib/ve/src/ui/actions/ve.ui.BlockquoteAction.js] from Extension:VisualEditor. | // This is based on [lib/ve/src/ui/actions/ve.ui.BlockquoteAction.js] from Extension:VisualEditor. | ||
ve.ui. | ve.ui.ColumnAction = function VeUiColumnAction() { | ||
ve.ui. | ve.ui.ColumnAction.super.apply( this, arguments ); | ||
}; | }; | ||
OO.inheritClass( ve.ui. | OO.inheritClass( ve.ui.ColumnAction, ve.ui.Action ); | ||
ve.ui. | ve.ui.ColumnAction.static.name = 'column'; | ||
ve.ui. | ve.ui.ColumnAction.static.methods = [ 'wrap', 'unwrap', 'toggle' ]; | ||
ve.ui. | ve.ui.ColumnAction.prototype.isWrapped = function () { | ||
var fragment = this.surface.getModel().getFragment(); | var fragment = this.surface.getModel().getFragment(); | ||
return fragment.hasMatchingAncestor( ' | return fragment.hasMatchingAncestor( 'column' ); | ||
}; | }; | ||
ve.ui. | ve.ui.ColumnAction.prototype.toggle = function () { | ||
return this[ this.isWrapped() ? 'unwrap' : 'wrap' ](); | return this[ this.isWrapped() ? 'unwrap' : 'wrap' ](); | ||
}; | }; | ||
ve.ui. | ve.ui.ColumnAction.prototype.wrap = function () { | ||
var | var | ||
surfaceModel = this.surface.getModel(), | surfaceModel = this.surface.getModel(), | ||
Line 41: | Line 41: | ||
while ( | while ( | ||
fragment.getCoveredNodes().some( function ( nodeInfo ) { | fragment.getCoveredNodes().some( function ( nodeInfo ) { | ||
return !nodeInfo.node.isAllowedParentNodeType( ' | return !nodeInfo.node.isAllowedParentNodeType( 'column' ) || nodeInfo.node.isContent(); | ||
} ) | } ) | ||
) { | ) { | ||
Line 47: | Line 47: | ||
} | } | ||
// Wrap everything in a < | // Wrap everything in a <div style="column-count:2"> tag | ||
fragment.wrapAllNodes( { type: ' | fragment.wrapAllNodes( { type: 'div' } ); | ||
return true; | return true; | ||
}; | }; | ||
ve.ui. | ve.ui.ColumnAction.prototype.unwrap = function () { | ||
var | var | ||
surfaceModel = this.surface.getModel(), | surfaceModel = this.surface.getModel(), | ||
Line 75: | Line 75: | ||
fragment | fragment | ||
// Expand to cover entire < | // Expand to cover entire <div> tag | ||
.expandLinearSelection( 'closest', ve.dm. | .expandLinearSelection( 'closest', ve.dm.DivNode ) | ||
// Unwrap it | // Unwrap it | ||
.unwrapNodes( 0, 1 ); | .unwrapNodes( 0, 1 ); | ||
Line 82: | Line 82: | ||
return true; | return true; | ||
}; | }; | ||
ve.ui.actionFactory.register( ve.ui. | ve.ui.actionFactory.register( ve.ui.ColumnAction ); | ||
// --------- (end of ve.ui. | // --------- (end of ve.ui.ColumnAction definition) ------------------------------------------------- | ||
ve.ui. | ve.ui.ColumnFormatTool = function VeUiColumnFormatTool() { | ||
ve.ui. | ve.ui.ColumnFormatTool.super.apply( this, arguments ); | ||
}; | }; | ||
OO.inheritClass( ve.ui. | OO.inheritClass( ve.ui.ColumnFormatTool, ve.ui.FormatTool ); | ||
ve.ui. | ve.ui.ColumnFormatTool.static.name = 'column'; | ||
ve.ui. | ve.ui.ColumnFormatTool.static.group = 'format'; | ||
ve.ui. | ve.ui.ColumnFormatTool.static.title = 'Column'; | ||
ve.ui. | ve.ui.ColumnFormatTool.static.format = { type: 'column' }; | ||
ve.ui. | ve.ui.ColumnFormatTool.static.commandName = 'column'; | ||
ve.ui.toolFactory.register( ve.ui. | ve.ui.toolFactory.register( ve.ui.ColumnFormatTool ); | ||
ve.ui.commandRegistry.register( | ve.ui.commandRegistry.register( | ||
new ve.ui.Command( | new ve.ui.Command( | ||
' | 'column', 'column', 'toggle', | ||
{ supportedSelections: [ 'linear' ] } | { supportedSelections: [ 'linear' ] } | ||
) | ) | ||
); | ); | ||
} ); | } ); |
Revision as of 22:25, 10 May 2022
mw.loader.using( [ 'ext.visualEditor.core', 'ext.visualEditor.mwtransclusion' ] ).then(function () {
// --------- (start of ve.ui.ColumnAction definition) -----------------------------------------------
// This is based on [lib/ve/src/ui/actions/ve.ui.BlockquoteAction.js] from Extension:VisualEditor.
ve.ui.ColumnAction = function VeUiColumnAction() {
ve.ui.ColumnAction.super.apply( this, arguments );
};
OO.inheritClass( ve.ui.ColumnAction, ve.ui.Action );
ve.ui.ColumnAction.static.name = 'column';
ve.ui.ColumnAction.static.methods = [ 'wrap', 'unwrap', 'toggle' ];
ve.ui.ColumnAction.prototype.isWrapped = function () {
var fragment = this.surface.getModel().getFragment();
return fragment.hasMatchingAncestor( 'column' );
};
ve.ui.ColumnAction.prototype.toggle = function () {
return this[ this.isWrapped() ? 'unwrap' : 'wrap' ]();
};
ve.ui.ColumnAction.prototype.wrap = function () {
var
surfaceModel = this.surface.getModel(),
selection = surfaceModel.getSelection(),
fragment = surfaceModel.getFragment( null, true ),
leaves, leavesRange;
if ( !( selection instanceof ve.dm.LinearSelection ) ) {
return false;
}
leaves = fragment.getSelectedLeafNodes();
leavesRange = new ve.Range(
leaves[ 0 ].getRange().start,
leaves[ leaves.length - 1 ].getRange().end
);
fragment = surfaceModel.getLinearFragment( leavesRange, true );
fragment = fragment.expandLinearSelection( 'siblings' );
while (
fragment.getCoveredNodes().some( function ( nodeInfo ) {
return !nodeInfo.node.isAllowedParentNodeType( 'column' ) || nodeInfo.node.isContent();
} )
) {
fragment = fragment.expandLinearSelection( 'parent' );
}
// Wrap everything in a <div style="column-count:2"> tag
fragment.wrapAllNodes( { type: 'div' } );
return true;
};
ve.ui.ColumnAction.prototype.unwrap = function () {
var
surfaceModel = this.surface.getModel(),
selection = surfaceModel.getSelection(),
fragment = surfaceModel.getFragment( null, true ),
leaves, leavesRange;
if ( !( selection instanceof ve.dm.LinearSelection ) ) {
return false;
}
if ( !this.isWrapped() ) {
return false;
}
leaves = fragment.getSelectedLeafNodes();
leavesRange = new ve.Range(
leaves[ 0 ].getRange().start,
leaves[ leaves.length - 1 ].getRange().end
);
fragment = surfaceModel.getLinearFragment( leavesRange, true );
fragment
// Expand to cover entire <div> tag
.expandLinearSelection( 'closest', ve.dm.DivNode )
// Unwrap it
.unwrapNodes( 0, 1 );
return true;
};
ve.ui.actionFactory.register( ve.ui.ColumnAction );
// --------- (end of ve.ui.ColumnAction definition) -------------------------------------------------
ve.ui.ColumnFormatTool = function VeUiColumnFormatTool() {
ve.ui.ColumnFormatTool.super.apply( this, arguments );
};
OO.inheritClass( ve.ui.ColumnFormatTool, ve.ui.FormatTool );
ve.ui.ColumnFormatTool.static.name = 'column';
ve.ui.ColumnFormatTool.static.group = 'format';
ve.ui.ColumnFormatTool.static.title = 'Column';
ve.ui.ColumnFormatTool.static.format = { type: 'column' };
ve.ui.ColumnFormatTool.static.commandName = 'column';
ve.ui.toolFactory.register( ve.ui.ColumnFormatTool );
ve.ui.commandRegistry.register(
new ve.ui.Command(
'column', 'column', 'toggle',
{ supportedSelections: [ 'linear' ] }
)
);
} );
This page was last modified on 10 May 2022, at 22:25. (2 years ago)