Skip to content

Commit

Permalink
perf: value editor maintains scroll offset after refresh #162
Browse files Browse the repository at this point in the history
  • Loading branch information
tiny-craft committed Feb 26, 2024
1 parent c4d41b1 commit ad2c4c4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
66 changes: 37 additions & 29 deletions frontend/src/components/content_value/ContentEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { computed, nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
import * as monaco from 'monaco-editor'
import usePreferencesStore from 'stores/preferences.js'
import { useThemeVars } from 'naive-ui'
import { isEmpty } from 'lodash'
const props = defineProps({
content: {
Expand All @@ -22,25 +23,35 @@ const props = defineProps({
type: Boolean,
default: false,
},
keyPath: {
offsetKey: {
type: String,
default: "",
default: '',
},
rememberScroll: {
keepOffset: {
type: Boolean,
default: true,
}
default: false,
},
})
const emit = defineEmits(['reset', 'input', 'save'])
const scrollTop = ref(0)
const themeVars = useThemeVars()
/** @type {HTMLElement|null} */
const editorRef = ref(null)
/** @type monaco.editor.IStandaloneCodeEditor */
let editorNode = null
const scrollOffset = { top: 0, left: 0 }
const updateScroll = () => {
if (editorNode != null) {
if (props.keepOffset && !isEmpty(props.offsetKey)) {
editorNode.setScrollPosition({ scrollTop: scrollOffset.top, scrollLeft: scrollOffset.left })
} else {
// reset offset if not needed
editorNode.setScrollPosition({ scrollTop: 0, scrollLeft: 0 })
}
}
}
const destroyEditor = () => {
if (editorNode != null && editorNode.dispose != null) {
Expand Down Expand Up @@ -98,14 +109,17 @@ onMounted(async () => {
emit('save')
})
if (props.rememberScroll) {
editorNode.onDidScrollChange((event) => {
// Update scrolltop when scroll height changes, ie. content changes
if (!event.scrollHeightChanged) {
scrollTop.value = event.scrollTop
}
})
}
editorNode.onDidScrollChange((event) => {
// save scroll offset when changes, ie. content changes
if (props.keepOffset && !event.scrollHeightChanged) {
scrollOffset.top = event.scrollTop
scrollOffset.left = event.scrollLeft
}
})
editorNode.onDidLayoutChange((event) => {
updateScroll()
})
// editorNode.onDidChangeModelLanguageConfiguration(() => {
// editorNode?.getAction('editor.action.formatDocument')?.run()
Expand All @@ -121,31 +135,25 @@ onMounted(async () => {
watch(
() => props.content,
async (content, oldContent, onCleanup) => {
async (content) => {
if (editorNode != null) {
editorNode.setValue(content)
const disposable = editorNode.onDidLayoutChange(() => {
if (props.rememberScroll && scrollTop.value > 0) {
editorNode.setScrollTop(scrollTop.value)
}
});
onCleanup(() => disposable.dispose())
await nextTick(() => emit('reset', content))
updateScroll()
}
},
)
watch(
() => props.keyPath,
() => props.offsetKey,
() => {
// reset scroll offset when key changed
if (editorNode != null) {
scrollTop.value = 0
editorNode.setScrollTop(0)
scrollOffset.top = 0
scrollOffset.left = 0
editorNode.setScrollPosition({ scrollTop: 0, scrollLeft: 0 })
}
}
},
)
watch(
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/content_value/ContentValueJson.vue
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ defineExpose({
v-show="!props.loading"
:content="displayValue"
:loading="props.loading"
:key-path="props.keyPath"
:offset-key="props.keyPath"
keep-offset
class="flex-item-expand"
language="json"
style="height: 100%"
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/content_value/ContentValueString.vue
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ defineExpose({
:content="displayValue"
:language="viewLanguage"
:loading="props.loading"
:key-path="props.keyPath"
:offset-key="props.keyPath"
keep-offset
class="flex-item-expand"
style="height: 100%"
@input="onInput"
Expand Down

0 comments on commit ad2c4c4

Please sign in to comment.