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

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


function copyCode(codeText) {
/**
  navigator.clipboard.writeText(codeText);
* 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);
}