MediaWiki:Common.js:修订间差异
外观
「手动换行」按钮改放到保存更改左侧 |
改为选中图片时才在图片旁浮出「换行」按钮,移出工具栏 |
||
| 第1行: | 第1行: | ||
/* | /* 土域百科:可视化编辑器「图片换行」浮动按钮 | ||
* 背景:插入图片后图片处于选中状态,此时按回车不会换行(VE 原生行为是打开图片设置)。 | * 背景:插入图片后图片处于选中状态,此时按回车不会换行(VE 原生行为是打开图片设置)。 | ||
* | * 本脚本:仅当点选图片时,在图片左下角浮出一个「换行」小按钮, | ||
* | * 点击即在图片下方插入新段落;图片失焦后按钮自动消失。不占用工具栏空间。 | ||
*/ | */ | ||
( function () { | ( function () { | ||
'use strict'; | 'use strict'; | ||
var $btn = null; | |||
var hookedSurface = null; | |||
function getSurface() { | function getSurface() { | ||
| 第17行: | 第19行: | ||
} | } | ||
function | 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 | if ( !focused || !focused.getModel || !focused.getModel().getOuterRange ) { | ||
hide(); | |||
return; | return; | ||
} | } | ||
var offset = focused.getModel().getOuterRange().to; | |||
ve. | 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(); | view.focus(); | ||
} catch ( err ) { | } catch ( err ) { | ||
mw.notify( ' | mw.notify( '换行没有生效:可以把光标移到图片后,按一下键盘 ↓ 方向键。', { type: 'warn' } ); | ||
} | } | ||
hide(); | |||
} | } | ||
function | 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 | var focused = surface.getView().getFocusedNode && surface.getView().getFocusedNode(); | ||
if ( !focused || !isImageNode( focused ) ) { | |||
hide(); | |||
return; | return; | ||
} | } | ||
if ( ! | var el = focused.$element && focused.$element[0]; | ||
if ( !el ) { | |||
hide(); | |||
return; | return; | ||
} | } | ||
if ( | var r = el.getBoundingClientRect(); | ||
return; | 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 | var surface = getSurface(); | ||
if ( | if ( !surface || hookedSurface === surface.getModel() ) { | ||
return; | return; | ||
} | } | ||
hookedSurface = surface.getModel(); | |||
hookedSurface.on( 'select', update ); | |||
} | } | ||
mw.hook( 've.activationComplete' ).add( function () { | // 编辑器激活后挂载;时序不确定时重试 | ||
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 ( | hookSurface(); | ||
if ( hookedSurface || n > 10 ) { | |||
clearInterval( timer ); | clearInterval( timer ); | ||
} | } | ||
}, 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();
}
} );
}() );