사용자:하늘/addCopyButtonToCode.js: 두 판 사이의 차이

편집 요약 없음
편집 요약 없음
 
(같은 사용자의 중간 판 5개는 보이지 않습니다)
1번째 줄: 1번째 줄:
function addCopyLinkToCodeElements() {
/**
mw.util.addCSS('.copy-button { margin-top: 0.7em; margin-right: 0.7em; position: absolute; z-index: 100; right: 0; }\
* 코드 상자에 코드를 복사하는 버튼을 추가합니다.
*/
mw.loader.using(['oojs-ui-core', 'oojs-ui.styles.icons-editing-advanced'], () => {
 
mw.util.addCSS('.copyToClipBoard-button { margin-top: 0.7em; margin-right: 0.7em; position: absolute; z-index: 100; right: 0; }\
.mw-highlight { position: relative; }');
.mw-highlight { position: relative; }');
const elements = document.querySelectorAll(".mw-parser-output .mw-highlight");
elements.forEach(function (element) {
    const highlightElements = document.querySelectorAll(".mw-parser-output .mw-highlight");
var copybutton = new OO.ui.ButtonWidget({
highlightElements.forEach(element => {
var copyToClipBoardbutton = new OO.ui.ButtonWidget({
label: 'copy',
label: 'copy',
/*icon: 'code',*/
icon: 'code',
title: 'copy',
title: 'copy this code to clipboard',
classes: ['copy-button']
classes: ['copyToClipBoard-button']
});
});
const code = element.firstChild.innerText;
copybutton.on('click', function () { copy(code); });
const codeText = element.firstChild.innerText;
copyToClipBoardbutton.on('click', () => copyToClipBoard(codeText));
const endBlock = document.createElement('div');
const endBlock = document.createElement('div');
endBlock.style = "clear:both";
endBlock.style = "clear:both";
$(element).prepend(copybutton.$element);
$(element).prepend(copyToClipBoardbutton.$element);
element.appendChild(endBlock);
element.appendChild(endBlock);
});
});
}
});


function copy(string) {
/**
navigator.clipboard.writeText(string);
* content를 클립보드로 복사합니다.
* @param {string} content
*/
function copyToClipBoard(content) {
navigator.clipboard.writeText(content);
}
}
mw.loader.using(['oojs-ui-core'], function () {
addCopyLinkToCodeElements();
});

2023년 12월 31일 (일) 03:21 기준 최신판

/**
 * 코드 상자에 코드를 복사하는 버튼을 추가합니다.
 */
mw.loader.using(['oojs-ui-core', 'oojs-ui.styles.icons-editing-advanced'], () => {

	mw.util.addCSS('.copyToClipBoard-button { 	margin-top: 0.7em; margin-right: 0.7em; position: absolute; z-index: 100; right: 0; }\
	.mw-highlight { position: relative; }');
	
    const highlightElements = document.querySelectorAll(".mw-parser-output .mw-highlight");
	
	highlightElements.forEach(element => {
		var copyToClipBoardbutton = new OO.ui.ButtonWidget({
			label: 'copy',
			icon: 'code',
			title: 'copy this code to clipboard',
			classes: ['copyToClipBoard-button']
		});
		
		const codeText = element.firstChild.innerText;
		copyToClipBoardbutton.on('click', () => copyToClipBoard(codeText));
		
		const endBlock = document.createElement('div');
		endBlock.style = "clear:both";
		$(element).prepend(copyToClipBoardbutton.$element);
		element.appendChild(endBlock);
	});
});

/**
 * content를 클립보드로 복사합니다.
 * @param {string} content 
 */
function copyToClipBoard(content) {
	navigator.clipboard.writeText(content);
}