fix scroll behavior of CodeMirror 6

Previously, the CM6 editor always scrolls into view, which is annoying during preview. With this patch, the CM6 editor only scrolls to the selection while the whole webpage does not scroll. In addition, the editor's scroll position will be memorized when previewing.

This patch requires an update of the @codemirror/view package.

Bug: T212899
Bug: T254962
Change-Id: I7f5e4694fa55c380958fa60ff6b3341bea1d2f02
This commit is contained in:
bhsd 2024-02-21 15:29:53 +08:00
parent c9c9ed27d3
commit e4eb2846c9
6 changed files with 22 additions and 13 deletions

8
package-lock.json generated
View file

@ -14,7 +14,7 @@
"@codemirror/language": "6.9.3",
"@codemirror/search": "6.5.4",
"@codemirror/state": "6.2.1",
"@codemirror/view": "6.18.1",
"@codemirror/view": "6.22.2",
"@lezer/highlight": "1.2.0",
"@wdio/cli": "7.30.1",
"@wdio/junit-reporter": "7.29.1",
@ -1404,9 +1404,9 @@
"dev": true
},
"node_modules/@codemirror/view": {
"version": "6.18.1",
"resolved": "https://registry.npmjs.org/@codemirror/view/-/view-6.18.1.tgz",
"integrity": "sha512-xcsXcMkIMd7l3WZEWoc4ljteAiqzxb5gVerRxk5132p5cLix6rTydWTQjsj2oxORepfsrwy1fC4r20iMa9plrg==",
"version": "6.22.2",
"resolved": "https://registry.npmjs.org/@codemirror/view/-/view-6.22.2.tgz",
"integrity": "sha512-cJp64cPXm7QfSBWEXK+76+hsZCGHupUgy8JAbSzMG6Lr0rfK73c1CaWITVW6hZVkOnAFxJTxd0PIuynNbzxYPw==",
"dev": true,
"dependencies": {
"@codemirror/state": "^6.1.4",

View file

@ -27,7 +27,7 @@
"@codemirror/language": "6.9.3",
"@codemirror/search": "6.5.4",
"@codemirror/state": "6.2.1",
"@codemirror/view": "6.18.1",
"@codemirror/view": "6.22.2",
"@lezer/highlight": "1.2.0",
"@wdio/cli": "7.30.1",
"@wdio/junit-reporter": "7.29.1",

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -192,6 +192,10 @@ export default class CodeMirror {
if ( this.$textarea[ 0 ].form ) {
this.$textarea[ 0 ].form.addEventListener( 'submit', () => {
this.$textarea.val( this.view.state.doc.toString() );
const scrollTop = document.getElementById( 'wpScrolltop' );
if ( scrollTop ) {
scrollTop.value = this.view.scrollDOM.scrollTop;
}
} );
}

View file

@ -82,13 +82,18 @@ export default class CodeMirrorWikiEditor extends CodeMirror {
this.initialize( extensions );
// Sync scroll position, selections, and focus state.
this.view.scrollDOM.scrollTop = scrollTop;
this.view.dispatch( {
selection: EditorSelection.create( [
EditorSelection.range( selectionStart, selectionEnd )
] ),
scrollIntoView: true
requestAnimationFrame( () => {
this.view.scrollDOM.scrollTop = scrollTop;
} );
if ( selectionStart !== 0 || selectionEnd !== 0 ) {
const range = EditorSelection.range( selectionStart, selectionEnd ),
scrollEffect = EditorView.scrollIntoView( range );
scrollEffect.value.isSnapshot = true;
this.view.dispatch( {
selection: EditorSelection.create( [ range ] ),
effects: scrollEffect
} );
}
if ( hasFocus ) {
this.view.focus();
}