@@ -217,9 +217,11 @@ async function init() {
217217 copy ()
218218 })
219219
220- watch (selectedSnippetContent , (v ) => {
220+ watch (selectedSnippetContent , (v , oldV ) => {
221221 nextTick (() => {
222- setValue (v ?.value || ' ' )
222+ const isNewValue = v ?.id !== oldV ?.id
223+ // Не сохраняем вьюпорт при смене фрагмента/сниппета
224+ setValue (v ?.value || ' ' , true , ! isNewValue )
223225 nextTick (() => {
224226 if (searchQuery .value ) {
225227 updateSearchOverlay ()
@@ -261,21 +263,32 @@ async function init() {
261263 })
262264}
263265
264- function setValue(value : string , programmatic = true ) {
266+ function setValue(value : string , programmatic = true , preserveViewport = true ) {
265267 if (! editor )
266268 return
267269
268- const cursor = editor .getCursor ()
270+ const current = editor .getValue ()
271+ if (current === value )
272+ return
269273
270- isProgrammaticChange .value = programmatic
271- editor ?.setValue (value )
274+ const cursor = preserveViewport ? editor .getCursor () : null
275+ const { left, top } = preserveViewport
276+ ? editor .getScrollInfo ()
277+ : { left: 0 , top: 0 }
272278
273- nextTick (() => {
274- isProgrammaticChange . value = false
275- })
279+ isProgrammaticChange . value = programmatic
280+ editor . setValue ( value )
281+ isProgrammaticChange . value = false
276282
277- if (cursor )
278- editor .setCursor (cursor )
283+ if (preserveViewport ) {
284+ if (cursor )
285+ editor .setCursor (cursor )
286+ editor .scrollTo (left , top )
287+ }
288+ else {
289+ editor .setCursor ({ line: 0 , ch: 0 })
290+ editor .scrollTo (0 , 0 )
291+ }
279292}
280293
281294function setLanguage(language : Language ) {
0 commit comments