Move updateListener out of sourceOnly wrapper (#19573)

GitOrigin-RevId: 3633b7c9763eac05793c2099124bd78369f90a73
This commit is contained in:
Alf Eaton
2024-08-07 08:05:50 +00:00
committed by Copybot
parent 049f8af680
commit 6ec26060e4
4 changed files with 32 additions and 26 deletions
@@ -7,7 +7,7 @@ import {
Text,
TransactionSpec,
} from '@codemirror/state'
import { toggleVisualEffect } from './visual/visual'
import { sourceOnly, toggleVisualEffect } from './visual/visual'
import { debugConsole } from '@/utils/debugging'
const buildStorageKey = (docId: string) => `doc.position.${docId}`
@@ -23,11 +23,14 @@ type LineInfo = {
* or the window is closed, or when switching between Source and Rich Text, and
* b) dispatches the scroll position (middle visible line) when it changes, for use in the outline.
*/
export const scrollPosition = ({
currentDoc: { doc_id: docId },
}: {
currentDoc: { doc_id: string }
}) => {
export const scrollPosition = (
{
currentDoc: { doc_id: docId },
}: {
currentDoc: { doc_id: string }
},
{ visual }: { visual: boolean }
) => {
// store lineInfo for use on unload, when the DOM has already been unmounted
let lineInfo: LineInfo
@@ -90,6 +93,26 @@ export const scrollPosition = ({
},
}
),
// restore the scroll position when switching to source mode
sourceOnly(
visual,
EditorView.updateListener.of(update => {
for (const tr of update.transactions) {
for (const effect of tr.effects) {
if (effect.is(toggleVisualEffect)) {
if (!effect.value) {
// switching to the source editor
window.setTimeout(() => {
update.view.dispatch(restoreScrollPosition())
update.view.focus()
})
}
}
}
}
})
),
]
}