imported>=海豚= |
imported>Irukaza |
(未显示2个用户的132个中间版本) |
第1行: |
第1行: |
| "use strict";
| | |
| /* jshint bitwise: true, curly: true, nonew: true, singleGroups: true, unused: true, varstmt: true */
| |
| /*global mw, $, OO, CodeMirror*/
| |
| $.when( $.ready, mw.loader.using( ['oojs-ui-windows', 'mediawiki.util'] ) ).then(() => {
| |
| const contentModel = mw.config.get( 'wgPageContentModel' ),
| |
| modes = {css: ['css', '//cdn.jsdelivr.net/npm/[email protected]/mode/css/css.min.js'], | |
| 'sanitized-css': ['css', '//cdn.jsdelivr.net/npm/[email protected]/mode/css/css.min.js'], | |
| javascript: ['javascript', '//cdn.jsdelivr.net/npm/[email protected]/mode/javascript/javascript.min.js'], | |
| json: ['javascript', '//cdn.jsdelivr.net/npm/[email protected]/mode/javascript/javascript.min.js'], | |
| | |
| | |
| wikitext: ['mediawiki', '//cdn.jsdelivr.net/gh/bhsd-harry/LLWiki@latest/otherwiki/mediawiki.min.js']
| |
| };
| |
| if (!mw.config.get( 'wgIsArticle' ) || !(window.Wikiplus ||
| |
| ['ready', 'loaded', 'loading', 'executing'].includes( mw.loader.getState( 'ext.gadget.Wikiplus' ) ))) {
| |
| return;
| |
| }
| |
| let promise, cm, mode;
| |
| const $search = $('<input>', {id: 'Wikiplus-Quickedit-Search', placeholder: '查找'})
| |
| .on('input', () => { $search.css('background-color', ''); }),
| |
| $searchClose = $('<span>', {text: '×', id:'Wikiplus-Quickedit-Search-Close'}),
| |
| findNext = () => {
| |
| const ptn = $search.val();
| |
| if (!ptn) { return; }
| |
| const regexp = new RegExp( (mw.RegExp ? mw.RegExp.escape : mw.util.escapeRegExp)( ptn ), 'i' ),
| |
| lastLine = cm.lastLine(),
| |
| to = {line: lastLine, ch: cm.getLine( lastLine ).length};
| |
| let from = cm.getCursor( 'head' ),
| |
| text = cm.getRange(from, to),
| |
| search = text.match( regexp );
| |
| if (!search) {
| |
| from = {line: 0, ch: 0};
| |
| text = cm.getValue();
| |
| search = text.match( regexp );
| |
| }
| |
| if (!search) {
| |
| $search.css('background-color', 'pink');
| |
| return;
| |
| }
| |
| const index = cm.indexFromPos( from ) + search.index,
| |
| anchor = cm.posFromIndex( index ),
| |
| head = cm.posFromIndex( index + ptn.length );
| |
| cm.setSelection(anchor, head);
| |
| cm.scrollIntoView({from: anchor, to: head});
| |
| },
| |
| highlight = ($textarea) => {
| |
| const height = $textarea.height(),
| |
| update = () => { $textarea.val( cm.getValue() ); },
| |
| $summary = $textarea.nextAll( '#Wikiplus-Quickedit-Summary-Input' ),
| |
| $submit = $textarea.nextAll( 'button' ).mousedown( update ).first(),
| |
| $minor = $submit.prev().children( '#Wikiplus-Quickedit-MinorEdit' ),
| |
| $searchContainer = $('<div>', {id: 'Wikiplus-Quickedit-Search-Div', html: [$search, $searchClose]})
| |
| .hide().insertBefore( $textarea ),
| |
| findNew = () => {
| |
| $searchContainer.show();
| |
| $search.select().focus()[0].scrollIntoView( {behavior: 'smooth'} );
| |
| };
| |
| $searchClose.click(() => { $searchContainer.hide(); });
| |
| cm = CodeMirror.fromTextArea($textarea[0], $.extend({
| |
| mode: mode[0], mwConfig: window.mwConfig, lineWrapping: true, lineNumbers: true
| |
| }, contentModel == 'wikitext' ? {} : {indentUnit: 4, indentWithTabs: true},
| |
| contentModel == 'json' ? {json: true} : {}));
| |
| mw.hook( 'wiki-codemirror' ).fire( cm );
| |
| cm.setSize(null, height);
| |
| cm.addKeyMap({'Ctrl-S': () => {
| |
| update();
| |
| $submit.triggerHandler( 'click' );
| |
| }, 'Shift-Ctrl-S': () => {
| |
| update();
| |
| $minor.click();
| |
| $submit.triggerHandler( 'click' );
| |
| }, 'Ctrl-F': findNew, 'Cmd-F': findNew, 'Ctrl-G': findNext, 'Cmd-G': findNext});
| |
| $textarea.closest( '.Wikiplus-InterBox' ).keydown(e => {
| |
| if (!e.metaKey && !e.ctrlKey || !['f', 'g'].includes( e.key )) { return; }
| |
| e.preventDefault();
| |
| if (e.key == 'f') { findNew(); }
| |
| else { findNext(); }
| |
| });
| |
| $summary.add([ $minor[0], $search[0] ]).off( 'keydown' ).keydown(e => {
| |
| if (!e.metaKey && !e.ctrlKey || e.keyCode != 83) { return; }
| |
| e.stopImmediatePropagation();
| |
| update();
| |
| if (e.shiftKey) { $minor.click(); }
| |
| $submit.triggerHandler( 'click' );
| |
| });
| |
| },
| |
| init = ($node) => {
| |
| const $textarea = $node.find( '#Wikiplus-Quickedit' );
| |
| $search.keyup(e => { if (e.key == 'Enter') { findNext(); } });
| |
| if (!promise) {
| |
| mw.loader.load('//cdn.jsdelivr.net/npm/[email protected]/lib/codemirror.min.css', 'text/css'); | |
| mw.loader.addStyleTag( `#Wikiplus-Quickedit + .CodeMirror { border: 1px solid #c8ccd1; line-height: 1.3; clear: both; }
| |
| #Wikiplus-Quickedit-Search-Div { margin: 7px 0 5px; }
| |
| #Wikiplus-Quickedit-Search-Close { font-size: 20px; margin: 7px; vertical-align: middle; cursor: pointer; }
| |
| #Wikiplus-Quickedit-Search { width: 50%; }` );
| |
| if (['wikitext', 'Scribunto'].includes( contentModel )) {
| |
| mw.loader.load('//cdn.jsdelivr.net/gh/bhsd-harry/[email protected]/otherwiki/mediawiki.min.css', 'text/css'); | |
| }
| |
| }
| |
| (contentModel == 'Scribunto' ?
| |
| OO.ui.prompt( '无法检测待编辑内容的内容模型,请手动输入"lua"或"wikitext"或其首字母:' ) :
| |
| Promise.resolve( contentModel )).then(model => {
| |
| model = model || contentModel;
| |
| if (model.length == 1) { model = ['lua', 'wikitext'].find(ele => ele.startsWith( model.toLowerCase() )); }
| |
| mode = modes[ model ] || modes[ contentModel ];
| |
| const getExt = (window.CodeMirror ? Promise.resolve() : $.get({ dataType: 'script', cache: true,
| |
| | |
| })).then(() => (window.CodeMirror || {modes: {}}).modes[ mode[0] ] ? Promise.resolve() :
| |
| $.get({dataType: 'script', cache: true, url: mode[1]})
| |
| ),
| |
| getJSON = window.mwConfig || model != 'wikitext' ? Promise.resolve() :$.get({ dataType: 'json',
| |
| cache: true, url: '//cdn.jsdelivr.net/gh/bhsd-harry/LLWiki@latest/otherwiki/gadget-CodeMirror.json'
| |
| }).then(config => { window.mwConfig = window.mwConfig || config; });
| |
| promise = Promise.all([getJSON, getExt]);
| |
| promise.then(() => { highlight( $textarea ) });
| |
| });
| |
| },
| |
| callback = (records) => {
| |
| const $node = $( records[0].addedNodes[0] );
| |
| if (!$node.hasClass( 'Wikiplus-InterBox' )) { return; }
| |
| init( $node );
| |
| observer2.disconnect();
| |
| observer2.observe( $node.find( '#Wikiplus-Quickedit-Preview-Output' )[0], {childList: true} );
| |
| },
| |
| observer = new MutationObserver( callback ),
| |
| callback2 = (records) => {
| |
| const $node = $( records[0].addedNodes[1] );
| |
| if ($node.hasClass( 'mw-body-content' )) { mw.hook( 'wikipage.content' ).fire( $node ); }
| |
| },
| |
| observer2 = new MutationObserver( callback2 );
| |
| observer.observe(document.body, {childList: true});
| |
|
| |
| // 以下内容引自[[MW:Extension:CodeMirror]]
| |
| $.valHooks.textarea = {
| |
| get: (ele) => ele.id == 'Wikiplus-Quickedit' && cm ? cm.getValue() : ele.value,
| |
| set: (ele, value) => ele.id == 'Wikiplus-Quickedit' && cm ? cm.setValue( value ) : ele.value = value
| |
| };
| |
| });
| |