跳转到内容

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

MediaWiki:Common.js:修订间差异

来自土域百科
Admin​(留言 | 贡献)
「手动换行」按钮改放到保存更改左侧
Admin​(留言 | 贡献)
改为选中图片时才在图片旁浮出「换行」按钮,移出工具栏
第1行: 第1行:
/* 土域百科:可视化编辑器「手动换行」按钮
/* 土域百科:可视化编辑器「图片换行」浮动按钮
  * 背景:插入图片后图片处于选中状态,此时按回车不会换行(VE 原生行为是打开图片设置)。
  * 背景:插入图片后图片处于选中状态,此时按回车不会换行(VE 原生行为是打开图片设置)。
  * 本脚本在编辑器工具栏「保存更改」左侧加一个「手动换行」按钮:
  * 本脚本:仅当点选图片时,在图片左下角浮出一个「换行」小按钮,
  *   - 图片等块级节点被选中时 → 在它下方插入一个新段落;
  * 点击即在图片下方插入新段落;图片失焦后按钮自动消失。不占用工具栏空间。
*  - 普通文字光标 → 执行与回车完全相同的原生换行逻辑。
  */
  */
( function () {
( function () {
'use strict';
'use strict';
var $btn = null;
var hookedSurface = null;


function getSurface() {
function getSurface() {
第17行: 第19行:
}
}


function manualBreak() {
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();
var surface = getSurface();
if ( !surface ) {
if ( !surface ) {
第26行: 第53行:
var model = surface.getModel();
var model = surface.getModel();
var focused = view.getFocusedNode && view.getFocusedNode();
var focused = view.getFocusedNode && view.getFocusedNode();
if ( focused && focused.getModel && focused.getModel().getOuterRange ) {
if ( !focused || !focused.getModel || !focused.getModel().getOuterRange ) {
// 图片/表格等块级节点被选中:在其后插入空段落
hide();
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();
mw.notify( '已插入新的一行', { autoHide: true } );
return;
return;
}
}
// 普通光标:复用编辑器原生回车逻辑
var offset = focused.getModel().getOuterRange().to;
ve.ce.LinearEnterKeyDownHandler.static.execute( view, {
var tx = ve.dm.TransactionBuilder.static.newFromInsertion(
preventDefault: function () {},
model.getDocument(), offset,
ctrlKey: false,
[ { type: 'paragraph' }, { type: '/paragraph' } ]
metaKey: false,
);
shiftKey: false
model.change( tx, new ve.dm.LinearSelection( new ve.Range( offset + 1 ) ) );
} );
view.focus();
view.focus();
} catch ( err ) {
} catch ( err ) {
mw.notify( '换行没有生效:可以把光标移到目标位置后,按一下键盘 ↓ 方向键。', { type: 'warn' } );
mw.notify( '换行没有生效:可以把光标移到图片后,按一下键盘 ↓ 方向键。', { type: 'warn' } );
}
}
hide();
}
}


function addButton() {
function isImageNode( node ) {
try {
return /image/i.test( node.getModel().getType() );
} catch ( e ) {
return false;
}
}
 
function update() {
var surface = getSurface();
var surface = getSurface();
if ( !surface ) {
if ( !surface ) {
hide();
return;
return;
}
}
var toolbar;
var focused = surface.getView().getFocusedNode && surface.getView().getFocusedNode();
try {
if ( !focused || !isImageNode( focused ) ) {
toolbar = ve.init.target.getToolbar && ve.init.target.getToolbar();
hide();
} catch ( err ) {
return;
return;
}
}
if ( !toolbar || !toolbar.$element ) {
var el = focused.$element && focused.$element[0];
if ( !el ) {
hide();
return;
return;
}
}
if ( toolbar.$element.find( '.ty-newline-btn' ).length ) {
var r = el.getBoundingClientRect();
return; // 已添加
if ( !r.width || r.bottom < 0 || r.top > window.innerHeight ) {
hide();
return;
}
}
var btn = new OO.ui.ButtonWidget( {
ensureButton().css( {
label: '↵ 手动换行',
left: Math.max( 8, r.left + 4 ) + 'px',
title: '在图片下方(或光标处)插入新的一行。插图后按回车没反应时点这里。',
top: Math.min( window.innerHeight - 34, r.bottom + 6 ) + 'px'
framed: false
} ).show();
} );
}
btn.$element.addClass( 'ty-newline-btn' );
btn.on( 'click', manualBreak );


// 放进「保存更改」所在的工具组,排在保存按钮左侧
function hookSurface() {
var $save = toolbar.$element.find( '.ve-ui-toolbar-group-save' );
var surface = getSurface();
if ( $save.length ) {
if ( !surface || hookedSurface === surface.getModel() ) {
$save.prepend( btn.$element );
return;
return;
}
}
var $actions = toolbar.$element.find( '.oo-ui-toolbar-actions' );
hookedSurface = surface.getModel();
if ( $actions.length ) {
hookedSurface.on( 'select', update );
$actions.prepend( btn.$element );
} else {
toolbar.$element.find( '.oo-ui-toolbar-bar' ).first()
.append( $( '<div>' ).addClass( 'oo-ui-toolbar-actions' ).append( btn.$element ) );
}
}
}


mw.hook( 've.activationComplete' ).add( function () {
// 编辑器激活后挂载;时序不确定时重试
addButton();
mw.hook( 've.activationComplete' ).add( hookSurface );
// 兜底:加载时序不确定时重试几次
mw.hook( 've.activate' ).add( function () {
var n = 0;
var n = 0;
var timer = setInterval( function () {
var timer = setInterval( function () {
n++;
n++;
if ( document.querySelector( '.ve-ui-toolbar .ty-newline-btn' ) || n > 10 ) {
hookSurface();
if ( hookedSurface || n > 10 ) {
clearInterval( timer );
clearInterval( timer );
return;
}
}
addButton();
}, 800 );
}, 800 );
} );
mw.hook( 've.deactivate' ).add( function () {
hookedSurface = null;
hide();
} );
// 滚动/缩放时让按钮跟着图片走
$( window ).on( 'scroll.tyvebr resize.tyvebr', function () {
if ( $btn && $btn.is( ':visible' ) ) {
update();
}
} );
} );
}() );
}() );

2026年10月1日 (四) 18:01的版本

/* 土域百科:可视化编辑器「图片换行」浮动按钮
 * 背景:插入图片后图片处于选中状态,此时按回车不会换行(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();
		}
	} );
}() );