미디어위키:Gadget-newtoolbar.js: 두 판 사이의 차이
편집 요약 없음 태그: 되돌려진 기여 |
편집 요약 없음 태그: 수동 되돌리기 |
||
| 1번째 줄: | 1번째 줄: | ||
/* MediaWiki:Gadget-NewToolbar.js */ | /* MediaWiki:Gadget-NewToolbar.js */ | ||
( function ( mw, $ ) { | ( function ( mw, $ ) { | ||
'use strict'; | 'use strict'; | ||
| 485번째 줄: | 203번째 줄: | ||
icon: 'ntb-icon-search', | icon: 'ntb-icon-search', | ||
label: '찾기/바꾸기', | label: '찾기/바꾸기', | ||
title: ' | title: 'Ctrl+F를 대신 안내하거나, 별도 UI로 확장 가능', | ||
onClick: function () { | onClick: function () { | ||
// 기본 브라우저 찾기를 안내하거나, 커스텀 UI를 나중에 붙일 수 있음 | |||
// | alert( '브라우저의 Ctrl+F(찾기)를 사용하세요.\n(원하면 여기 커스텀 찾기 UI를 만들 수 있습니다.)' ); | ||
} | } | ||
} ) | } ) | ||
); | |||
$toolbar | $toolbar | ||
| 523번째 줄: | 220번째 줄: | ||
$oldToolbar.hide().before( $toolbar ); | $oldToolbar.hide().before( $toolbar ); | ||
} | } | ||
// wikiEditor 준비되면 호출 | // wikiEditor 준비되면 호출 | ||
2025년 11월 13일 (목) 22:36 판
/* MediaWiki:Gadget-NewToolbar.js */
( function ( mw, $ ) {
'use strict';
/**
* 선택 영역을 앞뒤 텍스트로 감싸기 (간단 버전)
*/
function surroundSelection( textarea, before, after ) {
var $ta = $( textarea );
var start = textarea.selectionStart;
var end = textarea.selectionEnd;
if ( start == null || end == null ) {
// 구형 브라우저 fallback (거의 안 씀)
$ta.text( $ta.text() + before + after );
return;
}
var text = $ta.val();
var selected = text.slice( start, end );
var newText = text.slice( 0, start ) + before + selected + after + text.slice( end );
$ta.val( newText );
// 커서/선택 다시 잡기
var newStart = start + before.length;
var newEnd = newStart + selected.length;
textarea.selectionStart = newStart;
textarea.selectionEnd = newEnd;
$ta.trigger( 'change' );
}
/**
* 현재 커서 위치에 텍스트 삽입
*/
function insertAtCursor( textarea, snippet ) {
var $ta = $( textarea );
var start = textarea.selectionStart;
var end = textarea.selectionEnd;
var text = $ta.val();
if ( start == null || end == null ) {
$ta.val( text + snippet );
return;
}
var newText = text.slice( 0, start ) + snippet + text.slice( end );
$ta.val( newText );
var pos = start + snippet.length;
textarea.selectionStart = textarea.selectionEnd = pos;
$ta.trigger( 'change' );
}
/**
* 버튼 생성 헬퍼
*/
function createButton( opts ) {
var $btn = $( '<button>' )
.attr( 'type', 'button' )
.addClass( 'ntb-button' )
.append(
$( '<span>' )
.addClass( 'ntb-icon ' + ( opts.icon || '' ) )
)
.append(
$( '<span>' )
.addClass( 'ntb-label' )
.text( opts.label )
)
.on( 'click', function () {
var textarea = document.getElementById( 'wpTextbox1' );
if ( textarea && typeof opts.onClick === 'function' ) {
opts.onClick( textarea );
textarea.focus();
}
} );
if ( opts.title ) {
$btn.attr( 'title', opts.title );
}
return $btn;
}
/**
* 새 툴바 초기화
*/
function initNewToolbar( $textarea ) {
var $oldToolbar = $( '#wikiEditor-ui-toolbar' );
// wikiEditor 없는 페이지 or 이미 생성된 경우
if ( !$oldToolbar.length || $( '#ntb-toolbar' ).length ) {
return;
}
var $toolbar = $( '<div>' )
.attr( 'id', 'ntb-toolbar' )
.addClass( 'ntb-toolbar' );
// --- 그룹 1: 텍스트 스타일 ---
var $groupText = $( '<div>' )
.addClass( 'ntb-group ntb-group-text' )
.append(
$( '<span>' ).addClass( 'ntb-group-label' ).text( '텍스트' )
);
$groupText.append(
createButton( {
icon: 'ntb-icon-bold',
label: '굵게',
title: "'''굵게'''",
onClick: function ( ta ) {
surroundSelection( ta, "'''", "'''" );
}
} ),
createButton( {
icon: 'ntb-icon-italic',
label: '기울임',
title: "''기울임''",
onClick: function ( ta ) {
surroundSelection( ta, "''", "''" );
}
} )
);
// --- 그룹 2: 문단/목록 ---
var $groupBlock = $( '<div>' )
.addClass( 'ntb-group ntb-group-block' )
.append(
$( '<span>' ).addClass( 'ntb-group-label' ).text( '문단' )
);
$groupBlock.append(
createButton( {
icon: 'ntb-icon-heading',
label: '제목',
title: '== 문단 제목 ==',
onClick: function ( ta ) {
insertAtCursor( ta, '\n== 새 문단 제목 ==\n' );
}
} ),
createButton( {
icon: 'ntb-icon-ul',
label: '글머리 목록',
title: '* 글머리',
onClick: function ( ta ) {
insertAtCursor( ta, '\n* 항목\n' );
}
} ),
createButton( {
icon: 'ntb-icon-ol',
label: '번호 목록',
title: '# 번호 항목',
onClick: function ( ta ) {
insertAtCursor( ta, '\n# 항목\n' );
}
} )
);
// --- 그룹 3: 삽입 ---
var $groupInsert = $( '<div>' )
.addClass( 'ntb-group ntb-group-insert' )
.append(
$( '<span>' ).addClass( 'ntb-group-label' ).text( '삽입' )
);
$groupInsert.append(
createButton( {
icon: 'ntb-icon-link',
label: '링크',
title: '[[문서 링크]] 또는 [https://example.com 외부 링크]',
onClick: function ( ta ) {
insertAtCursor( ta, '[[링크 대상|표시 텍스트]]' );
}
} ),
createButton( {
icon: 'ntb-icon-file',
label: '파일',
title: '[[파일:Example.png|thumb|캡션]]',
onClick: function ( ta ) {
insertAtCursor( ta, '[[파일:Example.png|thumb|캡션]]' );
}
} ),
createButton( {
icon: 'ntb-icon-ref',
label: '각주',
title: '<ref>내용</ref>',
onClick: function ( ta ) {
surroundSelection( ta, '<ref>', '</ref>' );
}
} )
);
// --- 그룹 4: 기타 도구 ---
var $groupTools = $( '<div>' )
.addClass( 'ntb-group ntb-group-tools' )
.append(
$( '<span>' ).addClass( 'ntb-group-label' ).text( '도구' )
);
$groupTools.append(
createButton( {
icon: 'ntb-icon-search',
label: '찾기/바꾸기',
title: 'Ctrl+F를 대신 안내하거나, 별도 UI로 확장 가능',
onClick: function () {
// 기본 브라우저 찾기를 안내하거나, 커스텀 UI를 나중에 붙일 수 있음
alert( '브라우저의 Ctrl+F(찾기)를 사용하세요.\n(원하면 여기 커스텀 찾기 UI를 만들 수 있습니다.)' );
}
} )
);
$toolbar
.append( $groupText )
.append( $groupBlock )
.append( $groupInsert )
.append( $groupTools );
// 기존 툴바 숨기고, 새 툴바 삽입
$oldToolbar.hide().before( $toolbar );
}
// wikiEditor 준비되면 호출
mw.hook( 'wikiEditor.toolbarReady' ).add( initNewToolbar );
}( mediaWiki, jQuery ) );