| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
ve.ui.BlockquoteAction = function VeUiBlockquoteAction() { |
| 17 |
|
| 18 |
ve.ui.BlockquoteAction.super.apply( this, arguments ); |
| 19 |
}; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
OO.inheritClass( ve.ui.BlockquoteAction, ve.ui.Action ); |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
ve.ui.BlockquoteAction.static.name = 'blockquote'; |
| 28 |
|
| 29 |
ve.ui.BlockquoteAction.static.methods = [ 'wrap', 'unwrap', 'toggle' ]; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
ve.ui.BlockquoteAction.prototype.isWrapped = function () { |
| 39 |
const fragment = this.surface.getModel().getFragment(); |
| 40 |
return fragment.hasMatchingAncestor( 'blockquote' ); |
| 41 |
}; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
ve.ui.BlockquoteAction.prototype.toggle = function () { |
| 49 |
return this[ this.isWrapped() ? 'unwrap' : 'wrap' ](); |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
ve.ui.BlockquoteAction.prototype.wrap = function () { |
| 58 |
const surfaceModel = this.surface.getModel(), |
| 59 |
selection = surfaceModel.getSelection(); |
| 60 |
|
| 61 |
if ( !( selection instanceof ve.dm.LinearSelection ) ) { |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
let fragment = surfaceModel.getFragment( null, true ); |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
const leaves = fragment.getSelectedLeafNodes(); |
| 72 |
const leavesRange = new ve.Range( |
| 73 |
leaves[ 0 ].getRange().start, |
| 74 |
leaves[ leaves.length - 1 ].getRange().end |
| 75 |
); |
| 76 |
fragment = surfaceModel.getLinearFragment( leavesRange, true ); |
| 77 |
|
| 78 |
|
| 79 |
fragment = fragment.expandLinearSelection( 'siblings' ); |
| 80 |
|
| 81 |
|
| 82 |
while ( |
| 83 |
fragment.getCoveredNodes().some( ( nodeInfo ) => !nodeInfo.node.isAllowedParentNodeType( 'blockquote' ) || nodeInfo.node.isContent() ) |
| 84 |
) { |
| 85 |
fragment = fragment.expandLinearSelection( 'parent' ); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
fragment.wrapAllNodes( { type: 'blockquote' } ); |
| 90 |
|
| 91 |
return true; |
| 92 |
}; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
ve.ui.BlockquoteAction.prototype.unwrap = function () { |
| 100 |
const surfaceModel = this.surface.getModel(), |
| 101 |
selection = surfaceModel.getSelection(); |
| 102 |
|
| 103 |
if ( !( selection instanceof ve.dm.LinearSelection ) ) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
if ( !this.isWrapped() ) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
let fragment = surfaceModel.getFragment( null, true ); |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
const leaves = fragment.getSelectedLeafNodes(); |
| 118 |
const leavesRange = new ve.Range( |
| 119 |
leaves[ 0 ].getRange().start, |
| 120 |
leaves[ leaves.length - 1 ].getRange().end |
| 121 |
); |
| 122 |
fragment = surfaceModel.getLinearFragment( leavesRange, true ); |
| 123 |
|
| 124 |
fragment |
| 125 |
|
| 126 |
.expandLinearSelection( 'closest', ve.dm.BlockquoteNode ) |
| 127 |
|
| 128 |
.unwrapNodes( 0, 1 ); |
| 129 |
|
| 130 |
return true; |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
ve.ui.actionFactory.register( ve.ui.BlockquoteAction ); |