| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
ve.ui.MWWikitextAction = function VeUiMWWikitextAction() { |
| 17 |
|
| 18 |
ve.ui.MWWikitextAction.super.apply( this, arguments ); |
| 19 |
}; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
OO.inheritClass( ve.ui.MWWikitextAction, ve.ui.Action ); |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
ve.ui.MWWikitextAction.static.name = 'mwWikitext'; |
| 28 |
|
| 29 |
ve.ui.MWWikitextAction.static.methods = [ 'toggleWrapSelection', 'wrapSelection', 'wrapLine' ]; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
ve.ui.MWWikitextAction.prototype.toggleWrapSelection = function ( before, after, placeholder, expandOffsetsCallback, unwrapOffsetsCallback ) { |
| 45 |
const originalFragment = this.surface.getModel().getFragment( null, false, true ); |
| 46 |
|
| 47 |
let fragment = originalFragment; |
| 48 |
let textBefore, textAfter; |
| 49 |
if ( expandOffsetsCallback ) { |
| 50 |
const contextRange = fragment.expandLinearSelection( 'siblings' ).getSelection().getCoveringRange(); |
| 51 |
const data = fragment.getDocument().data; |
| 52 |
const range = fragment.getSelection().getCoveringRange(); |
| 53 |
textBefore = data.getText( true, new ve.Range( contextRange.start, range.start ) ); |
| 54 |
textAfter = data.getText( true, new ve.Range( range.end, contextRange.end ) ); |
| 55 |
const expandOffsets = expandOffsetsCallback( textBefore, textAfter ); |
| 56 |
if ( expandOffsets ) { |
| 57 |
fragment = originalFragment.adjustLinearSelection( expandOffsets[ 0 ], expandOffsets[ 1 ] ); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
if ( unwrapOffsetsCallback ) { |
| 62 |
const unwrapOffsets = unwrapOffsetsCallback( fragment.getText(), textBefore, textAfter ); |
| 63 |
if ( unwrapOffsets ) { |
| 64 |
fragment.unwrapText( unwrapOffsets[ 0 ], unwrapOffsets[ 1 ] ); |
| 65 |
} else { |
| 66 |
fragment.wrapText( before, after, placeholder, true ); |
| 67 |
} |
| 68 |
originalFragment.select(); |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
fragment.wrapText( before, after, placeholder ).select(); |
| 73 |
originalFragment.select(); |
| 74 |
return true; |
| 75 |
}; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
ve.ui.MWWikitextAction.prototype.wrapSelection = function ( before, after, placeholder ) { |
| 86 |
const fragment = this.surface.getModel().getFragment( null, false, true ); |
| 87 |
fragment.wrapText( before, after, placeholder ).select(); |
| 88 |
return true; |
| 89 |
}; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
ve.ui.MWWikitextAction.prototype.wrapLine = function ( before, after, placeholder, unwrapOffsetsCallback ) { |
| 105 |
let originalFragment = this.surface.getModel().getFragment( null, false, true ); |
| 106 |
const selectedNodes = originalFragment.getLeafNodes(); |
| 107 |
|
| 108 |
let unwrapped = false; |
| 109 |
for ( let i = selectedNodes.length - 1; i >= 0; i-- ) { |
| 110 |
if ( selectedNodes.length > 1 && selectedNodes[ i ].nodeRange.isCollapsed() ) { |
| 111 |
continue; |
| 112 |
} |
| 113 |
const fragment = this.surface.getModel().getLinearFragment( selectedNodes[ i ].nodeRange, true ); |
| 114 |
const unwrapOffsets = unwrapOffsetsCallback && unwrapOffsetsCallback( fragment.getText() ); |
| 115 |
|
| 116 |
if ( selectedNodes.length === 1 && originalFragment.getSelection().isCollapsed() ) { |
| 117 |
originalFragment = fragment; |
| 118 |
} |
| 119 |
|
| 120 |
if ( unwrapOffsets ) { |
| 121 |
fragment.unwrapText( unwrapOffsets[ 0 ], unwrapOffsets[ 1 ] ); |
| 122 |
unwrapped = true; |
| 123 |
} |
| 124 |
|
| 125 |
const wrappedFragment = fragment.wrapText( before, after, placeholder ); |
| 126 |
if ( !unwrapped && wrappedFragment !== fragment ) { |
| 127 |
if ( !ve.dm.LinearData.static.isElementData( |
| 128 |
wrappedFragment.collapseToStart().adjustLinearSelection( -1, 0 ).getData()[ 0 ] |
| 129 |
) ) { |
| 130 |
wrappedFragment.collapseToStart().insertContent( [ { type: '/paragraph' }, { type: 'paragraph' } ] ); |
| 131 |
} |
| 132 |
if ( !ve.dm.LinearData.static.isElementData( |
| 133 |
wrappedFragment.collapseToEnd().adjustLinearSelection( 0, 1 ).getData()[ 0 ] |
| 134 |
) ) { |
| 135 |
wrappedFragment.collapseToEnd().insertContent( [ { type: '/paragraph' }, { type: 'paragraph' } ] ); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
originalFragment.select(); |
| 140 |
return true; |
| 141 |
}; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
ve.ui.actionFactory.register( ve.ui.MWWikitextAction ); |