MediaWiki:Gadget-HizliDegistir.js

Not: Sayfayı kaydettikten sonra değişiklikleri görebilmek için tarayıcınızın önbelleğinizi temizlemeniz gerekir. Google Chrome, Firefox, Microsoft Edge ve Safari: ⇧ Shift tuşuna basılı tutun ve Yeniden Yükle araç çubuğu düğmesine tıklayın. Ayrıntılar ve diğer tarayıcılara yönelik yönergeler için Vikipedi:Önbelleğinizi atlayın sayfasını inceleyin.

// Edit sections of a page without leaving the article
// [[User:BrandonXLF/QuickEdit]]
// By [[User:BrandonXLF]]

$.when(mw.loader.using('oojs-ui-core'),$.ready).done(function(){
	var mobile = mw.config.get('skin') === 'minerva';
	var US = '-quickedit-';
	if (mobile) {
		mw.util.addCSS('.mw-editsection{white-space:nowrap}.content .collapsible-heading .' + US + 'section{visibility:hidden}.content .collapsible-heading.open-block .' + US + 'section{visibility:visible}');
	}
	$(document.body).on('click',function(e){
		var el = $(e.target);
		if (el.hasClass(US + 'editlink') && !el.hasClass(US + 'loading') ) {
			e.preventDefault();
			e.stopImmediatePropagation();
			var dots = 0;
			var interval;
			if (!mobile) {
				interval = setInterval(function(){
					dots = (dots+1)%3;
					el.attr('data-dots',dots);
				}, 200);
				el.addClass(US + 'loading');
				mw.util.addCSS('.' + US + 'hide{display:none!important}.' + US + 'loading,.' + US + 'heading{color:rgba(0,0,0,0.4)}[data-dots]:after{white-space:pre}[data-dots="0"]:after{content:".  "}[data-dots="1"]:after{content:".. "}[data-dots="2"]:after{content:"..."}');
			}
			$('.' + US + 'hide').removeClass(US + 'hide');
			$('.' + US + 'heading').removeClass(US + 'heading');
			$('#' + US + 'editor').remove(); 
			var targ = el.siblings('.' + US + 'target').last();
			var title = decodeURIComponent((/[\?&]title=([^&#]*)/.exec(targ.attr('href')) || (new RegExp('.*' + mw.config.get('wgArticlePath').replace(/\$1/,'([^?]+)'))).exec(targ.attr('href')))[1]);
			var secid = /[\?&]v?e?section=T?-?(\d*)/.exec(targ.attr('href'))[1];
			var level = 0;
			var sname = '';
			$.get(mw.config.get('wgScript'), {
				title: title,
				section: secid,
				action: 'raw'
			}).done(function(full){
				$.post(mw.config.get('wgScriptPath') + '/api.php',{
					action: 'query',
					prop: 'revisions',
					rvprop: 'timestamp',
					titles: title,
					curtimestamp: 'true',
					format: 'json'
				}).done(function(base){
					var start = base.curtimestamp;
					for (var id in base.query.pages) {
						base = base.query.pages[id].revisions[0].timestamp;
					}
					full = full.replace(/\s+$/,'');
					var i = 0;
					var saving = false;
					var expanded = false;
					var heading = el.closest('h1,h2,h3,h4,h5,h6');
					var part = full.split(/(?:^|\n)(=+.+=+)/,3).join('');
					var post = full.replace(part,'');
					full.replace(/^(=+)(.+)(=+)\n?/,function(m,a,b,c){
						level = Math.min(a.length,b.length);
						sname = b;
						return m;
					});
					if (!$.contains(document.getElementsByClassName('mw-parser-output')[0],heading[0])) {
						heading = $('.mw-parser-output').children().first();
					}
					var partsect = heading.nextUntil('#toc,h2:has(*),h3,h4,h5,h6').addClass(US + 'hide');
					var fullsect = heading.nextUntil(level == 6 ? 'h1,h2:has(*),h3,h4,h5,h6' : level == 5 ? 'h1,h2:has(*),h3,h4,h5' : level == 4 ? 'h1,h2:has(*),h3,h4' : level == 3 ? 'h1,h2:has(*),h3' : level == 2 ? 'h1,h2:has(*),#toc' : 'h1');
					if (heading.is('h1,h2,h3,h4,h5,h6')) {
						heading.addClass(US + 'heading');
					} else {
						heading.addClass(US + 'hide');
					}
					var textarea = new OO.ui.MultilineTextInputWidget({
						rows: 1,
						maxRows: 20,
						autosize: true,
						value: part,
					});
					var summary = new OO.ui.TextInputWidget({
						value: heading.is('h1,h2,h3,h4,h5,h6') ? '/* ' + heading.find('.mw-headline').text() + ' */ ' : ''
					});
					var minor = new OO.ui.CheckboxInputWidget();
					var save = new OO.ui.ButtonInputWidget({
						label: 'Değişiklikleri yayımla',
						title: 'Değişikliği kaydet',
						flags: ['primary','progressive']
					});
					var preview = new OO.ui.ButtonInputWidget({
						label: 'Önizlemeyi göster',
						title: 'Özizlemeyi göster'
					});
					var compare = new OO.ui.ButtonInputWidget({
						label: 'Değişiklikleri göster',
						title: 'Önceki ve sonraki sürüm arasındaki farkı gösterir'
					});
					var cancel = new OO.ui.ButtonInputWidget({
						useInputTag: true,
						label: 'İptal',
						title: 'Değişikliği kaydetmeden çık',
						flags: ['secondary','destructive']
					});
					var more = new OO.ui.ButtonInputWidget({
						label: '+',
						title: 'Tüm bölümü düzenleyin (alt bölümler dahil))'
					});
					textarea.$element.find('textarea').css('border-radius','0');
					save.on('click',function(){
						var savetxt = (expanded ? textarea.getValue() : textarea.getValue() + post).replace(/\s+$/,'') + '\n\n';
						if (saving) return;
						saving = true;
						save.setLabel('Kaydediliyor...');
						compare.setDisabled(true);
						preview.setDisabled(true);
						cancel.setDisabled(true);
						more.setDisabled(true);
						$.post(mw.config.get('wgScriptPath') + '/api.php', {
							action: 'edit',
							format: 'json',
							title: title,
							section: secid,
							summary: summary.getValue(),
							text: savetxt,
							minor: minor.isSelected() ? true : undefined,
							notminor: minor.isSelected() ? undefined : true,
							starttimestamp: start,
							basetimestamp: base,
							token: mw.user.tokens.get('csrfToken'),
						}).done(function(r){
							if (r.error && r.error.code == 'editconflict') {
								mw.notify('An edit conflict has occured. Please merge your changes with the conflicting changes.',{type:'error',tag:US});
								$.get(mw.config.get('wgScript'), {
									title: title,
									section: secid,
									action: 'raw'
								}).done(function(newtxt){
									$.post(mw.config.get('wgScriptPath') + '/api.php',{
										action: 'query',
										prop: 'revisions',
										rvprop: 'timestamp',
										titles: title,
										curtimestamp: 'true',
										format: 'json'
									}).done(function(newbase){
										start = newbase.curtimestamp;
										for (var id in newbase.query.pages) {
											base = newbase.query.pages[id].revisions[0].timestamp;
										}
									});
									function syncSize () {
										var t1 = textarea.adjustSize().$element.find('textarea').first();
										var t2 = textarea2.adjustSize().$element.find('textarea').first();
										var rows = Math.max(t1.attr('rows'),t2.attr('rows'));
										var height = Math.max(t1.height(),t2.height());
										t1.attr('rows',rows).height(height);
										t2.attr('rows',rows).height(height);
									}
									compare.setDisabled(false);
									preview.setDisabled(false);
									cancel.setDisabled(false);
									more.$element.remove();
									saving = false;
									expanded = true;
									oldtxt = (expanded ? textarea.getValue() : textarea.getValue() + post).replace(/\s+$/,'');
									newtxt = newtxt.replace(/\s+$/,'');
									var textarea2 = new OO.ui.MultilineTextInputWidget({
										rows: 1,
										maxRows: 20,
										autosize: true,
										value: oldtxt,
									});
									textarea.setValue(newtxt);
									textarea.on('change',syncSize);
									textarea2.on('change',syncSize);
									save.setLabel('Save');
									textarea.$element.closest('td').empty().append($('<table style="width:100%;border:1px solid #a2a9b1;border-bottom:none;">')
										.append('<tr><th style="width:50%;">Their version (to be saved)</th><th style="width:50%;">Your version</th></tr>')
										.append($('<tr>')
											.append($('<td style="width:50%;">')
												.append(textarea.$element.css({width:'100%',maxWidth:'100%',fontFamily:'monospace, monospace'}))
											)
											.append($('<td style="width:50%;">')
												.append(textarea2.$element.css({width:'100%',maxWidth:'100%',fontFamily:'monospace, monospace'}))
											)
										)
									);
									syncSize();
									$('#' + US + 'editor .previewtable').css({margin:'0.5em 0',border:'1px solid #a2a9b1'});
									$('#' + US + 'editor .preview').html('Loading...');
									mw.loader.load('mediawiki.diff.styles');
									$.post(mw.config.get('wgScriptPath') + '/api.php',{
										action: 'compare',
										format: 'json',
										fromslots: 'main',
										'fromtext-main': newtxt.replace(/\s+$/,'') + '\n\n',
										fromtitle: title,
										frompst: 'true',
										toslots: 'main',
										'totext-main': oldtxt.replace(/\s+$/,'') + '\n\n',
										totitle: title,
										topst: 'true'
									}).done(function(r){
										$('#' + US + 'editor .preview').html(r.compare['*'] ? '<table class="diff"><tr><td style="width:1em;"></td><td style="width:50%;"></td><td style="width:1em;"></td><td style="width:50%;"></td></tr>' + r.compare['*']  + '</table>' : '<tr><td>Değişiklik yok.</td></tr>');
									});
								}).fail(function(){
									mw.notify('Yeni revizyon alınırken bir hata oluştu, muhtemelen bölüm silindi. Sayfayı yeniden yüklemeyi deneyin.',{type:'error',tag:US});
								});
								return;
							}
							if (r.error) {
								compare.setDisabled(false);
								preview.setDisabled(false);
								cancel.setDisabled(false);
								more.setDisabled(expanded);
								mw.notify(r.error.info,{type:'error',tag:US});
								saving = false;
								save.setLabel('Save');
								return;
							}
							$.get(mw.config.get('wgScriptPath') + '/api.php', {
								action: 'parse',
								page: mw.config.get('wgPageName'),
								prop: 'text|categorieshtml',
								format: 'json'
							}).done(function(r){
								$('#' + US + 'editor, .' + US + 'section').remove();
								$('.mw-parser-output').replaceWith(r.parse.text['*']);
								mw.hook('wikipage.content').fire($('#mw-content-text'));
								$('.catlinks').replaceWith(r.parse.categorieshtml['*']);
								mw.hook('wikipage.categories').fire($('.catlinks'));
								saving = false;
							});
						});
					});
					preview.on('click',function(){
						$('#' + US + 'editor .previewtable').css({margin:'0.5em 0',border:'1px solid #a2a9b1'});
						$('#' + US + 'editor .preview').html('Loading...');
						$.post(mw.config.get('wgScriptPath') + '/api.php',{
							action: 'parse',
							format: 'json',
							title: title,
							prop: 'text',
							pst: 'true',
							disablelimitreport: 'true',
							disableeditsection: 'true',
							sectionpreview: 'true',
							disabletoc: 'true',
							text: textarea.getValue().replace(/\s+$/,'') + '\n\n'
						}).done(function(r){
							$('#' + US + 'editor .preview').html(r.parse.text['*'] + '<div style="clear:both;"></div>');
						});
					});
					compare.on('click',function(){
						$('#' + US + 'editor .previewtable').css({margin:'0.5em 0',border:'1px solid #a2a9b1'});
						$('#' + US + 'editor .preview').html('Loading...');
						mw.loader.load('mediawiki.diff.styles');
						$.post(mw.config.get('wgScriptPath') + '/api.php',{
							action: 'compare',
							format: 'json',
							fromslots: 'main',
							'fromtext-main': part + (expanded ? post : ''),
							fromtitle: title,
							frompst: 'true',
							toslots: 'main',
							'totext-main': textarea.getValue().replace(/\s+$/,'') + '\n\n',
							totitle: title,
							topst: 'true'
						}).done(function(r){
							$('#' + US + 'editor .preview').html(r.compare['*'] ? '<table class="diff"><tr><td style="width:1em;"></td><td style="width:50%;"></td><td style="width:1em;"></td><td style="width:50%;"></td></tr>' + r.compare['*']  + '</table>' : '<tr><td>Değişiklik yok.</td></tr>');
						});
					});
					cancel.on('click',function(){
						$('#' + US + 'editor').remove();
						fullsect.removeClass(US + 'hide');
						$('.' + US + 'heading').removeClass(US + 'heading');
					});
					more.on('click',function(){
						expanded = true;
						textarea.setValue(textarea.getValue() + post);
						fullsect.addClass(US + 'hide');
						more.setDisabled(true);
					});
					if (!mobile) {
						el.removeClass(US + 'loading').removeAttr('data-dots');
						clearInterval(interval);
						el.html('hızlı değiştir');
					}
					heading.after($('<div id="' + US + 'editor">')
						.append($('<table style="background-color:#eaecf0;border-spacing:0;">')
							.append($('<tr>')
								.append($('<td colspan="3" style="padding:0;">')
									.append(textarea.$element.css({width:'100%',maxWidth:'100%',fontFamily:'monospace, monospace'}))
								)
							)
							.append($('<tr>')
								.append('<td style="padding:10px 5px 10px 10px;border-left:1px solid #a2a9b1;">Özet</td>')
								.append($('<td style="padding:10px 0 10px 0;width:100%;">').append(summary.$element.css({width:'100%',maxWidth:'100%'})))
								.append($('<td style="padding:10px 10px 1px 5px;border-right:1px solid #a2a9b1;">')
									.append((new OO.ui.FieldsetLayout()).addItems([
							    		new OO.ui.FieldLayout(minor,{label:'Küçük değişiklik',align:'inline'})
									]).$element)
								)
							)
							.append($('<tr>')
								.append($('<td style="border:1px solid #a2a9b1;border-top:none;padding:10 10px 10px 10px;" style="width:100%;padding:5px;" colspan="3">')
									.append(save.$element)
									.append(preview.$element)
									.append(compare.$element)
									.append(part != full ? more.$element : '')
									.append(cancel.$element)
									.append(title != mw.config.get('wgPageName') ? '<span style="margin-left:2px;">Editing page: <a href="' + mw.config.get('wgArticlePath').replace('$1',title) + '"><b>' + title.replace(/_/g,' ') + '</b></a></span>' : '')
								)
							)
						)
						.append('<table style="border-spacing:0;padding:5px;" class="previewtable"><tr><td></td><td style="width:100%;padding:0;"><div class="preview"></div></td></tr></table>')
					);
				});
			}).fail(function(){
				if (!mobile) {
					el.removeClass(US + 'loading').removeAttr('data-dots');
					clearInterval(interval);
					el.html('hızlı değiştir');
				}
				mw.notify('Yeni revizyon alınırken bir hata oluştu, muhtemelen bölüm silindi. Sayfayı yeniden yüklemeyi deneyin',{type:'error',tag:US});
			});
		}
	});
	mw.hook('wikipage.content').add(function(){
		$('.mw-editsection').each(function(){
			$('[href*="title="][href*="section="]', this).last().after((mobile ? '' : '<span class="' + US + 'section"> | </span>') + '<a class="' + US + 'section ' + US + 'editlink">' + (mobile ? '&nbsp;Q' : 'hızlı değiştir') + '</a>').addClass(US + 'target');
		});
	});
});