| 1 |
'use strict'; |
| 2 |
|
| 3 |
let initialOffset, indentWidth, firstMarker; |
| 4 |
const updaters = []; |
| 5 |
|
| 6 |
const isRtl = $( 'html' ).attr( 'dir' ) === 'rtl'; |
| 7 |
|
| 8 |
function markTimestamp( parser, node, match ) { |
| 9 |
const dfParsers = parser.getLocalTimestampParsers(); |
| 10 |
|
| 11 |
const newNode = node.splitText( match.matchData.index ); |
| 12 |
newNode.splitText( match.matchData[ 0 ].length ); |
| 13 |
|
| 14 |
const wrapper = document.createElement( 'span' ); |
| 15 |
wrapper.className = 'ext-discussiontools-debughighlighter-timestamp'; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
const date = dfParsers[ match.parserIndex ]( match.matchData ).date; |
| 20 |
wrapper.title = date.format() + ' / ' + date.fromNow(); |
| 21 |
wrapper.appendChild( newNode ); |
| 22 |
node.parentNode.insertBefore( wrapper, node.nextSibling ); |
| 23 |
} |
| 24 |
|
| 25 |
function markSignature( sigNodes ) { |
| 26 |
const |
| 27 |
where = sigNodes[ 0 ], |
| 28 |
wrapper = document.createElement( 'span' ); |
| 29 |
wrapper.className = 'ext-discussiontools-debughighlighter-signature'; |
| 30 |
where.parentNode.insertBefore( wrapper, where ); |
| 31 |
while ( sigNodes.length ) { |
| 32 |
wrapper.appendChild( sigNodes.pop() ); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
function fixFakeFirstHeadingRect( rect, comment ) { |
| 37 |
|
| 38 |
|
| 39 |
if ( rect.x === 0 && rect.y === 0 && comment.type === 'heading' ) { |
| 40 |
const node = document.getElementsByClassName( 'firstHeading' )[ 0 ]; |
| 41 |
return node.getBoundingClientRect(); |
| 42 |
} |
| 43 |
return rect; |
| 44 |
} |
| 45 |
|
| 46 |
function calculateSizes() { |
| 47 |
|
| 48 |
const $content = $( '#mw-content-text' ); |
| 49 |
const $test = $( '<dd>' ).appendTo( $( '<dl>' ).appendTo( $content ) ); |
| 50 |
const rect = $content[ 0 ].getBoundingClientRect(); |
| 51 |
|
| 52 |
initialOffset = isRtl ? document.body.scrollWidth - rect.left - rect.width : rect.left; |
| 53 |
indentWidth = parseFloat( $test.css( isRtl ? 'margin-right' : 'margin-left' ) ) + |
| 54 |
parseFloat( $test.parent().css( isRtl ? 'margin-right' : 'margin-left' ) ); |
| 55 |
|
| 56 |
$test.parent().remove(); |
| 57 |
} |
| 58 |
|
| 59 |
function markComment( comment ) { |
| 60 |
const marker = document.createElement( 'div' ); |
| 61 |
marker.className = 'ext-discussiontools-debughighlighter-comment'; |
| 62 |
|
| 63 |
if ( !firstMarker ) { |
| 64 |
firstMarker = marker; |
| 65 |
} |
| 66 |
|
| 67 |
let marker2 = null; |
| 68 |
if ( comment.parent ) { |
| 69 |
marker2 = document.createElement( 'div' ); |
| 70 |
marker2.className = 'ext-discussiontools-debughighlighter-comment-ruler'; |
| 71 |
} |
| 72 |
|
| 73 |
let markerWarnings = null; |
| 74 |
if ( comment.warnings && comment.warnings.length ) { |
| 75 |
markerWarnings = document.createElement( 'div' ); |
| 76 |
markerWarnings.className = 'ext-discussiontools-debughighlighter-comment-warnings'; |
| 77 |
markerWarnings.innerText = comment.warnings.join( '\n' ); |
| 78 |
} |
| 79 |
|
| 80 |
const update = function () { |
| 81 |
const rect = fixFakeFirstHeadingRect( |
| 82 |
comment.getRange().getBoundingClientRect(), |
| 83 |
comment |
| 84 |
); |
| 85 |
const scrollTop = document.documentElement.scrollTop || document.body.scrollTop; |
| 86 |
const scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft; |
| 87 |
|
| 88 |
marker.style.top = ( rect.top + scrollTop ) + 'px'; |
| 89 |
marker.style.height = ( rect.height ) + 'px'; |
| 90 |
marker.style.left = ( rect.left + scrollLeft ) + 'px'; |
| 91 |
marker.style.width = ( rect.width ) + 'px'; |
| 92 |
|
| 93 |
if ( marker2 ) { |
| 94 |
let parentRect = comment.parent.getRange().getBoundingClientRect(); |
| 95 |
parentRect = fixFakeFirstHeadingRect( parentRect, comment.parent ); |
| 96 |
if ( comment.parent.level === 0 ) { |
| 97 |
|
| 98 |
parentRect = Object.assign( {}, parentRect ); |
| 99 |
parentRect.height -= 10; |
| 100 |
} |
| 101 |
|
| 102 |
marker2.style.top = ( parentRect.top + parentRect.height + scrollTop ) + 'px'; |
| 103 |
marker2.style.height = ( rect.top - ( parentRect.top + parentRect.height ) + 10 ) + 'px'; |
| 104 |
if ( isRtl ) { |
| 105 |
marker2.style.right = ( initialOffset - indentWidth / 2 + comment.parent.level * indentWidth ) + 'px'; |
| 106 |
marker2.style.width = ( ( comment.level - comment.parent.level ) * indentWidth - indentWidth / 2 ) - 2 + 'px'; |
| 107 |
} else { |
| 108 |
marker2.style.left = ( initialOffset - indentWidth / 2 + comment.parent.level * indentWidth ) + 'px'; |
| 109 |
marker2.style.width = ( ( comment.level - comment.parent.level ) * indentWidth - indentWidth / 2 ) - 2 + 'px'; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if ( markerWarnings ) { |
| 114 |
markerWarnings.style.cssText = marker.style.cssText; |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
updaters.push( update ); |
| 119 |
update(); |
| 120 |
|
| 121 |
document.body.appendChild( marker ); |
| 122 |
if ( marker2 ) { |
| 123 |
document.body.appendChild( marker2 ); |
| 124 |
} |
| 125 |
if ( markerWarnings ) { |
| 126 |
|
| 127 |
|
| 128 |
document.body.insertBefore( markerWarnings, firstMarker ); |
| 129 |
} |
| 130 |
|
| 131 |
comment.replies.forEach( markComment ); |
| 132 |
} |
| 133 |
|
| 134 |
function markThreads( threads ) { |
| 135 |
calculateSizes(); |
| 136 |
threads.forEach( markComment ); |
| 137 |
|
| 138 |
|
| 139 |
$( 'body' ).append( $( '.ext-discussiontools-debughighlighter-comment-ruler' ).get().reverse() ); |
| 140 |
} |
| 141 |
|
| 142 |
function updateAll() { |
| 143 |
updaters.forEach( ( update ) => { |
| 144 |
calculateSizes(); |
| 145 |
update(); |
| 146 |
} ); |
| 147 |
} |
| 148 |
|
| 149 |
window.addEventListener( 'resize', OO.ui.debounce( updateAll, 500 ) ); |
| 150 |
|
| 151 |
module.exports = { |
| 152 |
markThreads: markThreads, |
| 153 |
markTimestamp: markTimestamp, |
| 154 |
markSignature: markSignature |
| 155 |
}; |