跳转到内容

本站任何人都可以阅读;注册登录后即可编辑,保存即发布。不需要懂任何语法,全部可视化操作——新手请看 新手编辑指南 与 贡献中心。

MediaWiki:Common.js

来自土域百科
Admin​(留言 | 贡献)2026年10月1日 (四) 18:01的版本 (改为选中图片时才在图片旁浮出「换行」按钮,移出工具栏)

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
  • Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
  • Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
/* 土域百科:可视化编辑器「图片换行」浮动按钮
 * 背景:插入图片后图片处于选中状态,此时按回车不会换行(VE 原生行为是打开图片设置)。
 * 本脚本:仅当点选图片时,在图片左下角浮出一个「换行」小按钮,
 * 点击即在图片下方插入新段落;图片失焦后按钮自动消失。不占用工具栏空间。
 */
( function () {
	'use strict';

	var $btn = null;
	var hookedSurface = null;

	function getSurface() {
		try {
			var target = window.ve && ve.init && ve.init.target;
			return target && target.getSurface ? target.getSurface() : null;
		} catch ( err ) {
			return null;
		}
	}

	function ensureButton() {
		if ( $btn ) {
			return $btn;
		}
		$btn = $( '<button type="button" class="ty-ve-br" title="在图片下方插入新的一行">↵ 换行</button>' );
		// mousedown 阻止默认行为:保持编辑器内的图片选中状态不被点击破坏
		$btn.on( 'mousedown', function ( e ) {
			e.preventDefault();
			e.stopPropagation();
		} );
		$btn.on( 'click', function ( e ) {
			e.preventDefault();
			e.stopPropagation();
			insertAfterFocused();
		} );
		$( document.body ).append( $btn );
		return $btn;
	}

	function hide() {
		if ( $btn ) {
			$btn.hide();
		}
	}

	function insertAfterFocused() {
		var surface = getSurface();
		if ( !surface ) {
			return;
		}
		try {
			var view = surface.getView();
			var model = surface.getModel();
			var focused = view.getFocusedNode && view.getFocusedNode();
			if ( !focused || !focused.getModel || !focused.getModel().getOuterRange ) {
				hide();
				return;
			}
			var offset = focused.getModel().getOuterRange().to;
			var tx = ve.dm.TransactionBuilder.static.newFromInsertion(
				model.getDocument(), offset,
				[ { type: 'paragraph' }, { type: '/paragraph' } ]
			);
			model.change( tx, new ve.dm.LinearSelection( new ve.Range( offset + 1 ) ) );
			view.focus();
		} catch ( err ) {
			mw.notify( '换行没有生效:可以把光标移到图片后,按一下键盘 ↓ 方向键。', { type: 'warn' } );
		}
		hide();
	}

	function isImageNode( node ) {
		try {
			return /image/i.test( node.getModel().getType() );
		} catch ( e ) {
			return false;
		}
	}

	function update() {
		var surface = getSurface();
		if ( !surface ) {
			hide();
			return;
		}
		var focused = surface.getView().getFocusedNode && surface.getView().getFocusedNode();
		if ( !focused || !isImageNode( focused ) ) {
			hide();
			return;
		}
		var el = focused.$element && focused.$element[0];
		if ( !el ) {
			hide();
			return;
		}
		var r = el.getBoundingClientRect();
		if ( !r.width || r.bottom < 0 || r.top > window.innerHeight ) {
			hide();
			return;
		}
		ensureButton().css( {
			left: Math.max( 8, r.left + 4 ) + 'px',
			top: Math.min( window.innerHeight - 34, r.bottom + 6 ) + 'px'
		} ).show();
	}

	function hookSurface() {
		var surface = getSurface();
		if ( !surface || hookedSurface === surface.getModel() ) {
			return;
		}
		hookedSurface = surface.getModel();
		hookedSurface.on( 'select', update );
	}

	// 编辑器激活后挂载;时序不确定时重试
	mw.hook( 've.activationComplete' ).add( hookSurface );
	mw.hook( 've.activate' ).add( function () {
		var n = 0;
		var timer = setInterval( function () {
			n++;
			hookSurface();
			if ( hookedSurface || n > 10 ) {
				clearInterval( timer );
			}
		}, 800 );
	} );
	mw.hook( 've.deactivate' ).add( function () {
		hookedSurface = null;
		hide();
	} );

	// 滚动/缩放时让按钮跟着图片走
	$( window ).on( 'scroll.tyvebr resize.tyvebr', function () {
		if ( $btn && $btn.is( ':visible' ) ) {
			update();
		}
	} );
}() );