Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(frontend:files): preserve editor focus and track text changes

johaven86fac72a20fb
diff --git a/frontend/src/app/applications/files/components/viewers/files-viewer-editable-base.ts b/frontend/src/app/applications/files/components/viewers/files-viewer-editable-base.ts
index 48c11f1a..9d3ab30b 100644
--- a/frontend/src/app/applications/files/components/viewers/files-viewer-editable-base.ts
+++ b/frontend/src/app/applications/files/components/viewers/files-viewer-editable-base.ts
@@ -71,6 +71,10 @@ export abstract class FilesViewerEditableBase implements OnDestroy {
return
}
+ protected onSaveFinished(): void {
+ return
+ }
+
protected async toggleReadonly() {
if (this.isReadonly()) {
if (await this.lockFile()) {
@@ -88,31 +92,41 @@ export abstract class FilesViewerEditableBase implements OnDestroy {
const content = this.currentFileContent()
this.filesUpload.uploadFileContent(this.file(), content, true).subscribe({
next: () => {
+ if (this.isDestroyed) return
this.onContentSaved(content)
this.isModified.set(false)
this.isSaving.set(false)
this.warnOnUnsavedChanges.set(false)
if (exit) {
this.onClose().catch(console.error)
+ } else {
+ this.onSaveFinished()
}
this.file().updateHTimeAgo()
},
error: (e: HttpErrorResponse) => {
+ if (this.isDestroyed) return
this.isSaving.set(false)
+ this.onSaveFinished()
this.layout.sendNotification('error', 'Unable to save document', e.error.message)
}
})
}
protected canSave(): boolean {
- return this.canEditContent() && this.isModified() && !this.isSaving()
+ return this.canEditContent() && this.isModified()
}
protected canEditContent(): boolean {
- return !this.isReadonly() && this.isWriteable()
+ return !this.isReadonly() && this.isWriteable() && !this.isSaving()
+ }
+
+ protected canRestoreEditorFocus(): boolean {
+ return !document.activeElement?.closest('.files-viewer-search')
}
protected async onClose() {
+ if (this.isSaving()) return
if (!this.isReadonly()) {
await this.unlockFile()
}
diff --git a/frontend/src/app/applications/files/components/viewers/files-viewer-markdown.component.html b/frontend/src/app/applications/files/components/viewers/files-viewer-markdown.component.html
index dd699e40..88f77069 100644
--- a/frontend/src/app/applications/files/components/viewers/files-viewer-markdown.component.html
+++ b/frontend/src/app/applications/files/components/viewers/files-viewer-markdown.component.html
@@ -6,7 +6,7 @@
[adaptivePosition]="true"
class="btn {{ isReadonly() ? 'btn-warning' : 'btn-default' }} me-1"
type="button"
- [disabled]="!isSupported() || !isWriteable()">
+ [disabled]="!isSupported() || !isWriteable() || isSaving()">
<fa-icon [icon]="isReadonly() ? icons.faLock : icons.faLockOpen"></fa-icon>
</button>
<button
@@ -333,6 +333,7 @@
l10nTranslate>Save And Exit
</button>
<button (click)="onClose()"
+ [disabled]="isSaving()"
class="btn btn-sm btn-danger me-2"
data-dismiss="modal" type="button"
l10nTranslate>Close Without Saving
@@ -349,7 +350,7 @@
<div class="files-markdown-visual-pane">
<tiptap-editor
class="files-markdown-visual-editor"
- [class.is-readonly]="isReadonly()"
+ [class.is-readonly]="!canEditContent()"
[editor]="editor">
</tiptap-editor>
</div>
diff --git a/frontend/src/app/applications/files/components/viewers/files-viewer-markdown.component.ts b/frontend/src/app/applications/files/components/viewers/files-viewer-markdown.component.ts
index bb1c4e7f..bb94dcaf 100644
--- a/frontend/src/app/applications/files/components/viewers/files-viewer-markdown.component.ts
+++ b/frontend/src/app/applications/files/components/viewers/files-viewer-markdown.component.ts
@@ -55,6 +55,7 @@ import { FilesViewerSearchComponent } from './components/files-viewer-search.com
type MarkdownHeadingLevel = 1 | 2 | 3 | 4
type MarkdownInlineMark = 'bold' | 'code' | 'italic' | 'strike' | 'underline'
+type MarkdownEditorMode = 'source' | 'visual'
const ExitInlineCodeOnEnter = Extension.create({
name: 'exitInlineCodeOnEnter',
@@ -159,12 +160,13 @@ export class FilesViewerMarkdownComponent extends FilesViewerEditableBase implem
protected readonly headingLevels: MarkdownHeadingLevel[] = [1, 2, 3, 4]
private readonly sourceEditor = viewChild<CodeEditor>('sourceEditor')
private readonly imageFileInput = viewChild<ElementRef<HTMLInputElement>>('imageFileInput')
+ private focusRafId: number | null = null
private savedContent = ''
constructor() {
super()
effect(() => {
- const editable = !this.isReadonly() && this.isWriteable() && this.isSupported()
+ const editable = this.canEditContent() && this.isSupported()
if (!this.editor.isDestroyed) {
this.editor.setEditable(editable, false)
}
@@ -214,6 +216,9 @@ export class FilesViewerMarkdownComponent extends FilesViewerEditableBase implem
override ngOnDestroy() {
super.ngOnDestroy()
+ if (this.focusRafId !== null) {
+ cancelAnimationFrame(this.focusRafId)
+ }
this.editor.destroy()
}
@@ -260,12 +265,7 @@ export class FilesViewerMarkdownComponent extends FilesViewerEditableBase implem
this.setMarkdownContent(this.sourceContent)
}
this.isSourceMode.set(sourceMode)
- setTimeout(() => {
- if (sourceMode) {
- this.sourceEditor()?.view?.requestMeasure?.()
- this.sourceEditor()?.view?.focus()
- }
- })
+ this.scheduleEditorFocus(sourceMode ? 'source' : 'visual')
}
protected sourceContentChange() {
@@ -442,13 +442,17 @@ export class FilesViewerMarkdownComponent extends FilesViewerEditableBase implem
this.sourceContent = content
this.setMarkdownContent(content)
this.isModified.set(false)
+ this.scheduleEditorFocus('visual', 'start')
}
protected override onContentSaved(content: string) {
- this.sourceContent = content
this.savedContent = content
}
+ protected override onSaveFinished() {
+ this.scheduleEditorFocus(this.isSourceMode() ? 'source' : 'visual', undefined, true)
+ }
+
private runEditorCommand(command: () => boolean) {
if (this.canEditVisual() && !this.editor.isDestroyed) {
command()
@@ -535,4 +539,32 @@ export class FilesViewerMarkdownComponent extends FilesViewerEditableBase implem
private setMarkdownContent(content: string) {
this.editor.chain().setMeta('addToHistory', false).setContent(content, { emitUpdate: false, contentType: 'markdown' }).run()
}
+
+ private scheduleEditorFocus(mode: MarkdownEditorMode, position?: 'start', onlyIfUnfocused = false) {
+ if (this.focusRafId !== null) {
+ cancelAnimationFrame(this.focusRafId)
+ }
+ this.focusRafId = requestAnimationFrame(() => {
+ this.focusRafId = null
+ const sourceMode = mode === 'source'
+ if (this.isSourceMode() !== sourceMode) return
+ if (onlyIfUnfocused && !this.canRestoreEditorFocus()) return
+ if (!sourceMode) {
+ if (!this.editor.isDestroyed) {
+ this.editor.commands.focus(position)
+ }
+ return
+ }
+ const view = this.sourceView
+ if (!view) return
+ view.requestMeasure({
+ read: () => undefined,
+ write: () => {
+ if (this.isSourceMode() && (!onlyIfUnfocused || this.canRestoreEditorFocus())) {
+ view.focus()
+ }
+ }
+ })
+ })
+ }
}
diff --git a/frontend/src/app/applications/files/components/viewers/files-viewer-text.component.html b/frontend/src/app/applications/files/components/viewers/files-viewer-text.component.html
index 63dc00d0..91851129 100644
--- a/frontend/src/app/applications/files/components/viewers/files-viewer-text.component.html
+++ b/frontend/src/app/applications/files/components/viewers/files-viewer-text.component.html
@@ -6,7 +6,7 @@
[adaptivePosition]="true"
class="btn {{ isReadonly() ? 'btn-warning' : 'btn-default' }} me-1"
type="button"
- [disabled]="!isSupported() || !isWriteable()">
+ [disabled]="!isSupported() || !isWriteable() || isSaving()">
<fa-icon [icon]="isReadonly() ? icons.faLock : icons.faLockOpen"></fa-icon>
</button>
<button
@@ -64,6 +64,7 @@
l10nTranslate>Save And Exit
</button>
<button (click)="onClose()"
+ [disabled]="isSaving()"
class="btn btn-sm btn-danger me-2"
data-dismiss="modal" type="button"
l10nTranslate>Close Without Saving
@@ -77,14 +78,13 @@
</div>
<code-editor
#editor
- [autoFocus]="true"
[languages]="languages"
[language]="currentLanguage"
[(ngModel)]="content"
- (change)="contentChange()"
+ (change)="contentChange($event)"
[theme]="currentTheme"
- [readonly]="isReadonly()"
- [disabled]="isReadonly()"
+ [readonly]="!canEditContent()"
+ [disabled]="!canEditContent()"
[extensions]="searchAdapter.extensions"
[lineWrapping]="lineWrapping()" />
<app-files-viewer-search [adapter]="searchAdapter"></app-files-viewer-search>
diff --git a/frontend/src/app/applications/files/components/viewers/files-viewer-text.component.ts b/frontend/src/app/applications/files/components/viewers/files-viewer-text.component.ts
index 8a5b3415..a4b9c214 100644
--- a/frontend/src/app/applications/files/components/viewers/files-viewer-text.component.ts
+++ b/frontend/src/app/applications/files/components/viewers/files-viewer-text.component.ts
@@ -1,5 +1,5 @@
import { CodeEditor } from '@acrodata/code-editor'
-import { Component, HostListener, OnInit, signal, viewChild, ViewEncapsulation } from '@angular/core'
+import { Component, HostListener, OnDestroy, OnInit, signal, viewChild, ViewEncapsulation } from '@angular/core'
import { FormsModule } from '@angular/forms'
import { redo, redoDepth, undo, undoDepth } from '@codemirror/commands'
import { LanguageDescription } from '@codemirror/language'
@@ -57,16 +57,17 @@ import { FilesViewerSearchComponent } from './components/files-viewer-search.com
],
templateUrl: 'files-viewer-text.component.html'
})
-export class FilesViewerTextComponent extends FilesViewerEditableBase implements OnInit {
+export class FilesViewerTextComponent extends FilesViewerEditableBase implements OnInit, OnDestroy {
editor = viewChild<CodeEditor>('editor')
protected lineWrapping = signal(false)
- protected content: any = false
+ protected content = ''
protected currentLanguage = undefined
protected readonly languages: LanguageDescription[] = languages
protected readonly icons = { faFloppyDisk, faLock, faLockOpen, faMagnifyingGlass, faSpinner, faArrowsLeftRightToLine, faReply, faShare }
protected readonly searchAdapter = new CodeMirrorFileViewerSearchAdapter(() => this.editor()?.view)
protected readonly isSearchPanelOpen = this.searchAdapter.isOpen
- private isContentReady = false
+ private focusRafId: number | null = null
+ private savedContent = ''
constructor() {
super()
@@ -115,46 +116,77 @@ export class FilesViewerTextComponent extends FilesViewerEditableBase implements
this.loadContent().catch(console.error)
}
+ override ngOnDestroy() {
+ super.ngOnDestroy()
+ if (this.focusRafId !== null) {
+ cancelAnimationFrame(this.focusRafId)
+ }
+ }
+
toggleSearch() {
this.searchAdapter.toggle()
}
- contentChange() {
- // Ignore first call
- if (this.isContentReady) {
- this.isModified.set(true)
- } else {
- this.isContentReady = true
- }
+ contentChange(content: string) {
+ this.isModified.set(content !== this.savedContent)
}
onUndo() {
+ if (!this.canEditContent()) return
undo({ state: this.editor().view.state, dispatch: this.editor().view.dispatch })
}
onRedo() {
+ if (!this.canEditContent()) return
redo({ state: this.editor().view.state, dispatch: this.editor().view.dispatch })
}
canUndo(): boolean {
- if (this.editor()?.view) {
+ if (this.canEditContent() && this.editor()?.view) {
return undoDepth(this.editor().view.state) > 0
}
return false
}
canRedo(): boolean {
- if (this.editor()?.view) {
+ if (this.canEditContent() && this.editor()?.view) {
return redoDepth(this.editor().view.state) > 0
}
return false
}
protected currentFileContent(): string {
- return this.content || ''
+ return this.content
}
protected onContentLoaded(content: string) {
+ this.savedContent = content
this.content = content
+ this.isModified.set(false)
+ this.scheduleEditorFocus(0)
+ }
+
+ protected override onContentSaved(content: string) {
+ this.savedContent = content
+ }
+
+ protected override onSaveFinished() {
+ this.scheduleEditorFocus(undefined, true)
+ }
+
+ private scheduleEditorFocus(position?: number, onlyIfUnfocused = false) {
+ if (this.focusRafId !== null) {
+ cancelAnimationFrame(this.focusRafId)
+ }
+ this.focusRafId = requestAnimationFrame(() => {
+ this.focusRafId = null
+ if (onlyIfUnfocused && !this.canRestoreEditorFocus()) return
+ const view = this.editor()?.view
+ if (!view) return
+ if (position !== undefined) {
+ view.dispatch({ selection: { anchor: position }, scrollIntoView: true })
+ }
+ view.focus()
+ })
}
}