ve.ui.CodeMirror.v6: use DOMRect width when updating gutter width

This fixes some Chromium-specific issues where offsetWidth was rounded
up and could cause misalignment issues. It does not fix all alignment
issues, but should hopefully be an improvement in most cases.

Other changes:
* Add Core VE padding to match CM5 variant (doesn't affect WMF cluster)
* Remove redundant margin rules from VE surface
* Minor code cleanup

Bug: T357482
Change-Id: Ic4246c0b8c39914021bbc2a84d52b332dafec20a
This commit is contained in:
MusikAnimal 2024-11-05 20:15:22 -05:00
parent 0ebd773243
commit f4cf12b383
2 changed files with 10 additions and 18 deletions

View file

@ -11,6 +11,9 @@
top: 0; top: 0;
width: 100%; width: 100%;
// Core VE default padding
padding: 1.5em;
// Skin specific paddings // Skin specific paddings
.skin-vector & { .skin-vector & {
padding: 0 1rem; padding: 0 1rem;
@ -112,16 +115,6 @@
font-weight: inherit; font-weight: inherit;
} }
.ext-codemirror-wrapper .mw-content-ltr .ve-ce-paragraphNode {
/* @noflip */
margin-left: 6px !important; /* stylelint-disable-line declaration-no-important */
}
.ext-codemirror-wrapper .mw-content-rtl .ve-ce-paragraphNode {
/* @noflip */
margin-right: 6px !important; /* stylelint-disable-line declaration-no-important */
}
.cm-activeLineGutter { .cm-activeLineGutter {
background-color: transparent; background-color: transparent;
border-right: 0; border-right: 0;

View file

@ -158,13 +158,13 @@ ve.ui.CodeMirrorAction.prototype.toggle = function ( enable ) {
* @param {string} dir Document direction * @param {string} dir Document direction
*/ */
ve.ui.CodeMirrorAction.prototype.updateGutterWidth = function ( dir ) { ve.ui.CodeMirrorAction.prototype.updateGutterWidth = function ( dir ) {
const guttersWidth = this.surface.mirror.view.dom.querySelector( '.cm-gutters' ).offsetWidth; const guttersWidth = this.surface.mirror.view.dom.querySelector( '.cm-gutters' ).getBoundingClientRect().width;
this.surface.getView().$documentNode.css( { this.surface.getView().$documentNode.css( {
'margin-left': dir === 'rtl' ? 0 : guttersWidth - 6, 'margin-left': dir === 'rtl' ? 0 : guttersWidth,
'margin-right': dir === 'rtl' ? guttersWidth - 6 : 0 'margin-right': dir === 'rtl' ? guttersWidth : 0
} ); } );
// Also update width of .cm-content due to apparent Chromium bug. // Also update width of .cm-content due to apparent Chromium bug.
this.surface.mirror.view.contentDOM.style.width = 'calc(100% - ' + ( guttersWidth + 1 ) + 'px)'; this.surface.mirror.view.contentDOM.style.width = 'calc(100% - ' + guttersWidth + 'px)';
}; };
/** /**
@ -218,7 +218,6 @@ ve.ui.CodeMirrorAction.prototype.onSelect = function ( selection ) {
*/ */
ve.ui.CodeMirrorAction.prototype.onDocumentPrecommit = function ( tx ) { ve.ui.CodeMirrorAction.prototype.onDocumentPrecommit = function ( tx ) {
const replacements = [], const replacements = [],
action = this,
store = this.surface.getModel().getDocument().getStore(); store = this.surface.getModel().getDocument().getStore();
let offset = 0; let offset = 0;
@ -227,8 +226,8 @@ ve.ui.CodeMirrorAction.prototype.onDocumentPrecommit = function ( tx ) {
offset += op.length; offset += op.length;
} else if ( op.type === 'replace' ) { } else if ( op.type === 'replace' ) {
replacements.push( { replacements.push( {
from: action.surface.getModel().getSourceOffsetFromOffset( offset ), from: this.surface.getModel().getSourceOffsetFromOffset( offset ),
to: action.surface.getModel().getSourceOffsetFromOffset( offset + op.remove.length ), to: this.surface.getModel().getSourceOffsetFromOffset( offset + op.remove.length ),
insert: new ve.dm.ElementLinearData( store, op.insert ).getSourceText() insert: new ve.dm.ElementLinearData( store, op.insert ).getSourceText()
} ); } );
offset += op.remove.length; offset += op.remove.length;
@ -240,7 +239,7 @@ ve.ui.CodeMirrorAction.prototype.onDocumentPrecommit = function ( tx ) {
this.surface.mirror.view.dispatch( { changes: replacements[ i ] } ); this.surface.mirror.view.dispatch( { changes: replacements[ i ] } );
} }
action.updateGutterWidth( this.surface.getView().getDocument().getDir() ); this.updateGutterWidth( this.surface.getView().getDocument().getDir() );
}; };
/* Registration */ /* Registration */