Skip to content

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

feat(frontend:files): WIP markdown viewer editor

johavenc2bf44f1083c
diff --git a/frontend/package.json b/frontend/package.json
index 5082c5bc..b8db852f 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -41,6 +41,7 @@
"@fortawesome/free-solid-svg-icons": "^7.0.0",
"@perfectmemory/ngx-contextmenu": "^20.0.0",
"@tiptap/core": "^3.23.4",
+ "@tiptap/extension-image": "^3.23.4",
"@tiptap/extension-list": "^3.23.4",
"@tiptap/extension-table": "^3.23.4",
"@tiptap/extension-task-list": "^3.23.4",
diff --git a/frontend/src/app/applications/files/components/dialogs/files-viewer-dialog.component.ts b/frontend/src/app/applications/files/components/dialogs/files-viewer-dialog.component.ts
index 8b195a2e..0978339f 100644
--- a/frontend/src/app/applications/files/components/dialogs/files-viewer-dialog.component.ts
+++ b/frontend/src/app/applications/files/components/dialogs/files-viewer-dialog.component.ts
@@ -1,4 +1,4 @@
-import { Component, computed, inject, Input, model, OnDestroy, OnInit, signal } from '@angular/core'
+import { Component, computed, HostListener, inject, Input, model, OnDestroy, OnInit, signal } from '@angular/core'
import { FaIconComponent } from '@fortawesome/angular-fontawesome'
import { faEye, faPen } from '@fortawesome/free-solid-svg-icons'
import { FILE_MODE } from '@sync-in-server/backend/src/applications/files/constants/operations'
@@ -85,6 +85,12 @@ export class FilesViewerDialogComponent implements OnInit, OnDestroy {
this.layout.minimizeDialog(this.openedFile.id, { name: this.openedFile.name, mimeUrl: this.openedFile.mimeUrl })
}
+ @HostListener('window:beforeunload', ['$event'])
+ onBeforeUnload(event: BeforeUnloadEvent) {
+ event.preventDefault()
+ event.returnValue = ''
+ }
+
protected toggleViewer(): void {
if (this.activeViewer() === SHORT_MIME.PDF) {
this.activeViewer.set(SHORT_MIME.DOCUMENT)
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
new file mode 100644
index 00000000..d042aa0e
--- /dev/null
+++ b/frontend/src/app/applications/files/components/viewers/files-viewer-editable-base.ts
@@ -0,0 +1,160 @@
+import { HttpClient, HttpErrorResponse } from '@angular/common/http'
+import { Directive, effect, inject, input, model, OnDestroy, signal, untracked } from '@angular/core'
+import type { FileLockProps } from '@sync-in-server/backend/src/applications/files/interfaces/file-props.interface'
+import { L10N_LOCALE, L10nLocale } from 'angular-l10n'
+import { firstValueFrom } from 'rxjs'
+import { type AppWindow, themeDark } from '../../../../layout/layout.interfaces'
+import { LayoutService } from '../../../../layout/layout.service'
+import { FileModel } from '../../models/file.model'
+import { FilesService } from '../../services/files.service'
+import { FilesUploadService } from '../../services/files-upload.service'
+import { fileLockPropsToString } from '../utils/file-lock.utils'
+
+@Directive()
+export abstract class FilesViewerEditableBase implements OnDestroy {
+ currentHeight = input.required<number>()
+ file = model.required<FileModel>()
+ isWriteable = input.required<boolean>()
+ isReadonly = model.required<boolean>()
+ modalClosing = input.required<boolean>()
+ protected isSupported = signal(false)
+ protected isModified = signal(false)
+ protected isSaving = signal(false)
+ protected warnOnUnsavedChanges = signal(false)
+ protected currentTheme: 'dark' | 'light' = 'light'
+ protected readonly layout = inject(LayoutService)
+ protected readonly locale = inject<L10nLocale>(L10N_LOCALE)
+ private readonly http = inject(HttpClient)
+ private readonly filesServices = inject(FilesService)
+ private readonly filesUpload = inject(FilesUploadService)
+ private readonly subscription = this.layout.switchTheme.subscribe((layout: string) => (this.currentTheme = layout === themeDark ? 'dark' : 'light'))
+
+ protected constructor() {
+ effect(() => {
+ if (!this.modalClosing()) return
+ const fileId = untracked(() => this.file().id)
+ const modified = untracked(() => this.isModified())
+ if (modified) {
+ this.warnOnUnsavedChanges.set(true)
+ if (this.layout.windows.getValue().find((w: AppWindow) => w.id === fileId)) {
+ this.layout.restoreDialog(fileId)
+ }
+ } else {
+ this.onClose().catch(console.error)
+ }
+ })
+ }
+
+ ngOnDestroy() {
+ this.subscription.unsubscribe()
+ }
+
+ protected abstract currentFileContent(): string
+
+ protected abstract onContentLoaded(content: string): void
+
+ protected onContentSaved(_content: string): void {
+ return
+ }
+
+ protected async toggleReadonly() {
+ if (this.isReadonly()) {
+ if (await this.lockFile()) {
+ this.isReadonly.set(false)
+ }
+ } else {
+ await this.unlockFile()
+ this.isReadonly.set(true)
+ }
+ }
+
+ protected save(exit = false) {
+ if (!this.canSave()) return
+ this.isSaving.set(true)
+ const content = this.currentFileContent()
+ this.filesUpload.uploadOneFile(this.file(), content, true).subscribe({
+ next: () => {
+ this.onContentSaved(content)
+ this.isModified.set(false)
+ this.isSaving.set(false)
+ this.warnOnUnsavedChanges.set(false)
+ if (exit) {
+ this.onClose().catch(console.error)
+ }
+ this.file().updateHTimeAgo()
+ },
+ error: (e: HttpErrorResponse) => {
+ this.isSaving.set(false)
+ this.layout.sendNotification('error', 'Unable to save document', e.error.message)
+ }
+ })
+ }
+
+ protected canSave(): boolean {
+ return this.canEditContent() && this.isModified() && !this.isSaving()
+ }
+
+ protected canEditContent(): boolean {
+ return !this.isReadonly() && this.isWriteable()
+ }
+
+ protected async onClose() {
+ if (!this.isReadonly()) {
+ await this.unlockFile()
+ }
+ this.layout.closeDialog(null, this.file().id)
+ }
+
+ protected async loadContent() {
+ if (!this.isReadonly()) {
+ await this.lockFile()
+ }
+ this.http.get(this.file().dataUrl, { responseType: 'text' }).subscribe({
+ next: (data: string) => this.onContentLoaded(data),
+ error: (e: HttpErrorResponse) => this.layout.sendNotification('error', 'Unable to open document', this.file().name, e)
+ })
+ }
+
+ protected async lockFile(): Promise<boolean> {
+ if (!this.isSupported() || !this.isWriteable()) return false
+ try {
+ const lock: FileLockProps = await firstValueFrom(this.filesServices.lock(this.file()))
+ this.file.update((f) => {
+ f.lock = lock
+ return f
+ })
+ return true
+ } catch (e) {
+ this.lockError(e as HttpErrorResponse)
+ return false
+ }
+ }
+
+ protected async unlockFile() {
+ if (!this.isSupported() || !this.isWriteable()) return
+ try {
+ await firstValueFrom(this.filesServices.unlock(this.file()))
+ this.file.update((f) => {
+ delete f.lock
+ return f
+ })
+ } catch (e) {
+ this.lockError(e as HttpErrorResponse)
+ }
+ }
+
+ private lockError(e: HttpErrorResponse) {
+ this.isReadonly.set(true)
+ this.isSupported.set(false)
+ if (e.error?.owner) {
+ const lock: FileLockProps = e.error
+ this.file.update((f) => {
+ f.lock = lock
+ return f
+ })
+ this.layout.sendNotification('info', 'The file is locked', fileLockPropsToString(lock))
+ } else {
+ this.layout.sendNotification('warning', this.file().name, e.error.message)
+ }
+ }
+}
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 e56bca16..d6c1cf4d 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
@@ -4,7 +4,7 @@
(click)="toggleReadonly()"
[tooltip]="(isReadonly() || !isWriteable() ? 'Read-only' : 'Read-write') | translate:locale.language"
[adaptivePosition]="true"
- class="btn btn-default me-1"
+ class="btn {{ isReadonly() ? 'btn-warning' : 'btn-default' }} me-1"
type="button"
[disabled]="!isSupported() || !isWriteable()">
<fa-icon [icon]="isReadonly() ? icons.faLock : icons.faLockOpen"></fa-icon>
@@ -44,9 +44,9 @@
<button
(click)="toggleViewMode()"
[tooltip]="(isSourceMode() ? 'Visual Mode' : 'Code Mode') | translate:locale.language"
- class="btn btn-custom"
+ class="btn btn-custom mx-0"
type="button">
- <fa-icon [icon]="isSourceMode() ? icons.faKeyboard : icons.faCode"></fa-icon>
+ <fa-icon [icon]="isSourceMode() ? icons.faKeyboard : icons.faEye"></fa-icon>
</button>
<div class="vr files-markdown-mode-separator files-markdown-mode-separator-end"></div>
</div>
@@ -64,21 +64,10 @@
<fa-icon [icon]="icons.faHeading"></fa-icon>
</button>
<ul *dropdownMenu class="dropdown-menu files-markdown-heading-menu" role="menu">
- <li role="menuitem">
- <a (click)="setParagraph()" [class.active]="editor.isActive('paragraph')" class="dropdown-item">
- <span class="files-markdown-heading-item">
- <span class="files-markdown-heading-level"><fa-icon [icon]="icons.faParagraph"></fa-icon></span>
- <span class="files-markdown-heading-name" l10nTranslate>Paragraph</span>
- </span>
- </a>
- </li>
@for (level of headingLevels; track level) {
<li role="menuitem">
<a (click)="toggleHeading(level)" [class.active]="editor.isActive('heading', { level })" class="dropdown-item">
- <span class="files-markdown-heading-item">
- <span class="files-markdown-heading-level">H{{ level }}</span>
- <span class="files-markdown-heading-name">{{ 'Heading' | translate:locale.language }} {{ level }}</span>
- </span>
+ H{{ level }}
</a>
</li>
}
@@ -126,6 +115,7 @@
<div class="btn-group" dropdown>
<button
[disabled]="!canEditVisual()"
+ [class.active]="editor.isActive('bulletList') || editor.isActive('orderedList') || editor.isActive('taskList')"
[tooltip]="'List' | translate:locale.language"
class="btn btn-custom dropdown-toggle"
dropdownToggle
@@ -134,7 +124,7 @@
</button>
<ul *dropdownMenu class="dropdown-menu files-markdown-list-menu" role="menu">
<li role="menuitem">
- <a (click)="toggleBulletList()" [class.active]="isSelectionActive('bulletList')" class="dropdown-item">
+ <a (click)="toggleBulletList()" [class.active]="editor.isActive('bulletList')" class="dropdown-item">
<span class="files-markdown-list-item">
<fa-icon [icon]="icons.faListUl"></fa-icon>
<span l10nTranslate>Bullet List</span>
@@ -142,7 +132,7 @@
</a>
</li>
<li role="menuitem">
- <a (click)="toggleOrderedList()" [class.active]="isSelectionActive('orderedList')" class="dropdown-item">
+ <a (click)="toggleOrderedList()" [class.active]="editor.isActive('orderedList')" class="dropdown-item">
<span class="files-markdown-list-item">
<fa-icon [icon]="icons.faListOl"></fa-icon>
<span l10nTranslate>Ordered List</span>
@@ -150,7 +140,7 @@
</a>
</li>
<li role="menuitem">
- <a (click)="toggleTaskList()" [class.active]="isSelectionActive('taskList')" class="dropdown-item">
+ <a (click)="toggleTaskList()" [class.active]="editor.isActive('taskList')" class="dropdown-item">
<span class="files-markdown-list-item">
<fa-icon [icon]="icons.faSquareCheck"></fa-icon>
<span l10nTranslate>Task List</span>
@@ -163,6 +153,7 @@
<div class="btn-group" dropdown>
<button
[disabled]="!canEditVisual()"
+ [class.active]="editor.isActive('code') || editor.isActive('codeBlock')"
[tooltip]="'Code' | translate:locale.language"
class="btn btn-custom dropdown-toggle"
dropdownToggle
@@ -171,7 +162,7 @@
</button>
<ul *dropdownMenu class="dropdown-menu files-markdown-code-menu" role="menu">
<li role="menuitem">
- <a (click)="toggleCode()" [class.active]="isSelectionActive('code')" class="dropdown-item">
+ <a (click)="toggleCode()" [class.active]="editor.isActive('code')" class="dropdown-item">
<span class="files-markdown-code-item">
<fa-icon [icon]="icons.faCode"></fa-icon>
<span l10nTranslate>Inline Code</span>
@@ -179,7 +170,7 @@
</a>
</li>
<li role="menuitem">
- <a (click)="toggleCodeBlock()" [class.active]="isSelectionActive('codeBlock')" class="dropdown-item">
+ <a (click)="toggleCodeBlock()" [class.active]="editor.isActive('codeBlock')" class="dropdown-item">
<span class="files-markdown-code-item">
<fa-icon [icon]="icons.faCode"></fa-icon>
<span l10nTranslate>Code Block</span>
@@ -203,19 +194,10 @@
</div>
<div class="files-markdown-toolbar-group me-1">
- <button
- (click)="setLink()"
- [disabled]="!canEditVisual()"
- [class.active]="isSelectionActive('link')"
- [tooltip]="'Link' | translate:locale.language"
- class="btn btn-custom"
- type="button">
- <fa-icon [icon]="icons.faLink"></fa-icon>
- </button>
-
<div class="d-none d-lg-block btn-group" dropdown>
<button
[disabled]="!canEditVisual()"
+ [class.active]="editor.isActive('table')"
[tooltip]="'Table' | translate:locale.language"
class="btn btn-custom dropdown-toggle"
dropdownToggle
@@ -225,44 +207,44 @@
<ul *dropdownMenu class="dropdown-menu files-markdown-table-menu" role="menu">
<li role="menuitem">
<a (click)="insertTable()" class="dropdown-item">
- <span><fa-icon [icon]="icons.faTable"></fa-icon><span class="ms-2" l10nTranslate>Create a table</span></span>
+ <span><fa-icon [icon]="icons.faTable"></fa-icon><span l10nTranslate>Create a table</span></span>
+ </a>
+ </li>
+ <li role="menuitem">
+ <a (click)="deleteTable()" [class.disabled]="!canEditTable()" class="dropdown-item">
+ <span><fa-icon [icon]="icons.faTrashCan"></fa-icon><span l10nTranslate>Delete table</span></span>
</a>
</li>
<li class="dropdown-divider"></li>
<li role="menuitem">
<a (click)="addTableRow('above')" [class.disabled]="!canEditTable()" class="dropdown-item">
- <span><fa-icon [icon]="icons.faPlus"></fa-icon><span class="ms-2" l10nTranslate>Add row above</span></span>
+ <span><fa-icon [icon]="icons.faSquarePlus"></fa-icon><span l10nTranslate>Add row above</span></span>
</a>
</li>
<li role="menuitem">
<a (click)="addTableRow('below')" [class.disabled]="!canEditTable()" class="dropdown-item">
- <span><fa-icon [icon]="icons.faPlus"></fa-icon><span class="ms-2" l10nTranslate>Add row below</span></span>
+ <span><fa-icon [icon]="icons.faSquarePlus"></fa-icon><span l10nTranslate>Add row below</span></span>
</a>
</li>
<li role="menuitem">
<a (click)="deleteTableRow()" [class.disabled]="!canEditTable()" class="dropdown-item">
- <span><fa-icon [icon]="icons.faMinus"></fa-icon><span class="ms-2" l10nTranslate>Delete row</span></span>
+ <span><fa-icon [icon]="icons.faSquareMinus"></fa-icon><span l10nTranslate>Delete row</span></span>
</a>
</li>
<li class="dropdown-divider"></li>
<li role="menuitem">
<a (click)="addTableColumn('left')" [class.disabled]="!canEditTable()" class="dropdown-item">
- <span><fa-icon [icon]="icons.faPlus"></fa-icon><span class="ms-2" l10nTranslate>Add column left</span></span>
+ <span><fa-icon [icon]="icons.faSquarePlus"></fa-icon><span l10nTranslate>Add column left</span></span>
</a>
</li>
<li role="menuitem">
<a (click)="addTableColumn('right')" [class.disabled]="!canEditTable()" class="dropdown-item">
- <span><fa-icon [icon]="icons.faPlus"></fa-icon><span class="ms-2" l10nTranslate>Add column right</span></span>
+ <span><fa-icon [icon]="icons.faSquarePlus"></fa-icon><span l10nTranslate>Add column right</span></span>
</a>
</li>
<li role="menuitem">
<a (click)="deleteTableColumn()" [class.disabled]="!canEditTable()" class="dropdown-item">
- <span><fa-icon [icon]="icons.faTrashCan"></fa-icon><span class="ms-2" l10nTranslate>Delete column</span></span>
- </a>
- </li>
- <li role="menuitem">
- <a (click)="deleteTable()" [class.disabled]="!canEditTable()" class="dropdown-item">
- <span><fa-icon [icon]="icons.faTrashCan"></fa-icon><span class="ms-2" l10nTranslate>Delete table</span></span>
+ <span><fa-icon [icon]="icons.faSquareMinus"></fa-icon><span l10nTranslate>Delete column</span></span>
</a>
</li>
</ul>
@@ -276,7 +258,49 @@
type="button">
<fa-icon [icon]="icons.faMinus"></fa-icon>
</button>
+
+ <button
+ (click)="setLink()"
+ [disabled]="!canEditVisual()"
+ [class.active]="isSelectionActive('link')"
+ [tooltip]="'Link' | translate:locale.language"
+ class="btn btn-custom"
+ type="button">
+ <fa-icon [icon]="icons.faLink"></fa-icon>
+ </button>
+
+ <div class="btn-group" dropdown>
+ <button
+ [disabled]="!canEditVisual()"
+ [class.active]="isSelectionActive('image')"
+ [tooltip]="'Image' | translate:locale.language"
+ class="btn btn-custom dropdown-toggle"
+ dropdownToggle
+ type="button">
+ <fa-icon [icon]="icons.faImage"></fa-icon>
+ </button>
+ <ul *dropdownMenu class="dropdown-menu files-markdown-image-menu" role="menu">
+ <li role="menuitem">
+ <a (click)="openImageFilePicker()" class="dropdown-item">
+ <span class="files-markdown-image-item">
+ <fa-icon [icon]="icons.faImage"></fa-icon>
+ <span l10nTranslate>Image from file</span>
+ </span>
+ </a>
+ </li>
+ <li role="menuitem">
+ <a (click)="insertImageFromUrl()" class="dropdown-item">
+ <span class="files-markdown-image-item">
+ <fa-icon [icon]="icons.faLink"></fa-icon>
+ <span l10nTranslate>Image from URL</span>
+ </span>
+ </a>
+ </li>
+ </ul>
+ </div>
</div>
+
+ <input #imageFileInput (change)="insertImageFromFile($event)" accept="image/*" class="d-none" type="file" />
}
@if (isSourceMode()) {
diff --git a/frontend/src/app/applications/files/components/viewers/files-viewer-markdown.component.scss b/frontend/src/app/applications/files/components/viewers/files-viewer-markdown.component.scss
index 48dfe78f..823e0866 100644
--- a/frontend/src/app/applications/files/components/viewers/files-viewer-markdown.component.scss
+++ b/frontend/src/app/applications/files/components/viewers/files-viewer-markdown.component.scss
@@ -48,47 +48,23 @@
.files-markdown-heading-menu,
.files-markdown-list-menu,
.files-markdown-code-menu,
+ .files-markdown-image-menu,
.files-markdown-table-menu {
z-index: 4;
}
.files-markdown-heading-menu .dropdown-item,
.files-markdown-list-menu .dropdown-item,
- .files-markdown-code-menu .dropdown-item {
+ .files-markdown-code-menu .dropdown-item,
+ .files-markdown-image-menu .dropdown-item {
padding: 0.5rem 0.65rem;
}
- .files-markdown-heading-menu .dropdown-item.active {
+ .files-markdown-heading-menu {
background: var(--files-markdown-heading-active-bg, rgba(127, 127, 127, 0.18));
color: inherit;
}
- .files-markdown-heading-item {
- display: grid;
- grid-template-columns: 1.55rem max-content;
- column-gap: 0.45rem;
- align-items: center;
- }
-
- .files-markdown-heading-level,
- .files-markdown-heading-name {
- display: block;
- }
-
- .files-markdown-list-item,
- .files-markdown-code-item {
- display: grid;
- grid-template-columns: 1.35rem max-content;
- column-gap: 0.55rem;
- align-items: center;
- }
-
- .files-markdown-list-menu .dropdown-item fa-icon,
- .files-markdown-code-menu .dropdown-item fa-icon,
- .files-markdown-table-menu .dropdown-item fa-icon {
- width: 1.25rem;
- }
-
.files-markdown-content {
position: relative;
z-index: 1;
@@ -119,18 +95,26 @@
overflow: auto;
background: #fff;
- &.is-readonly .ProseMirror {
- cursor: default;
- }
}
.ProseMirror {
+ width: 100%;
+ max-width: 1000px;
min-height: 100%;
- padding: 1rem 1.25rem 2rem;
+ margin: 0 auto;
+ padding: 1rem;
outline: none;
font-size: 0.92rem;
line-height: 1.58;
+ > *:first-child {
+ margin-top: 0 !important;
+ }
+
+ > *:last-child {
+ margin-bottom: 0 !important;
+ }
+
h1,
h2,
h3,
@@ -139,38 +123,64 @@
.h2,
.h3,
.h4 {
- margin: 0.75rem 0 0.55rem;
+ margin: 1.5rem 0 1rem;
font-weight: 600;
line-height: 1.25;
}
h1,
.h1 {
- font-size: 1.75rem;
+ font-size: 2em;
}
h2,
.h2 {
- font-size: 1.45rem;
+ font-size: 1.5em;
}
h3,
.h3 {
- font-size: 1.2rem;
+ font-size: 1.25em;
}
h4,
.h4 {
- font-size: 1.05rem;
+ font-size: 1em;
}
p,
ul,
ol,
blockquote,
+ img,
pre,
.tableWrapper {
- margin: 0 0 0.8rem;
+ margin: 0 0 1rem;
+ }
+
+ ul,
+ ol {
+ padding-left: 2em;
+ }
+
+ ul ul,
+ ul ol,
+ ol ol,
+ ol ul {
+ margin-top: 0;
+ margin-bottom: 0;
+ }
+
+ li + li {
+ margin-top: 0.25em;
+ }
+
+ li > p {
+ margin: 0;
+ }
+
+ li > p + p {
+ margin-top: 1rem;
}
ul[data-type='taskList'] {
@@ -191,26 +201,57 @@
> div {
flex: 1 1 auto;
min-width: 0;
+
+ > p {
+ margin: 0;
+ }
}
}
+
+ li + li {
+ margin-top: 0.15rem;
+ }
}
blockquote {
padding-left: 0.85rem;
border-left: 3px solid #8aa4c8;
color: #53657d;
+
+ > :first-child {
+ margin-top: 0;
+ }
+
+ > :last-child {
+ margin-bottom: 0;
+ }
+ }
+
+ img {
+ display: block;
+ max-width: 100%;
+ height: auto;
+ border-radius: 4px;
+
+ &.ProseMirror-selectednode {
+ outline: 2px solid #5f84bd;
+ }
}
pre {
padding: 0.85rem 1rem;
overflow: auto;
border-radius: 6px;
- background: #172033;
- color: #eef3fb;
+ background: rgba(115, 132, 156, 0.12);
+ color: inherit;
+ font-size: 0.85em;
+ line-height: 1.45;
code {
padding: 0;
+ border-radius: 0;
background: transparent;
+ color: inherit;
}
}
@@ -218,10 +259,11 @@
padding: 0.08rem 0.28rem;
border-radius: 4px;
background: rgba(115, 132, 156, 0.16);
+ color: #2563eb;
}
hr {
- margin: 1rem 0;
+ margin: 1.5rem 0;
border: 0;
border-top: 1px solid rgba(128, 128, 128, 0.35);
}
@@ -243,6 +285,10 @@
border: 1px solid rgba(128, 128, 128, 0.35);
vertical-align: top;
+ > *:first-child {
+ margin-top: 0;
+ }
+
> *:last-child {
margin-bottom: 0;
}
@@ -299,19 +345,8 @@
}
}
-body.theme-light {
- .files-viewer-markdown {
- .files-markdown-heading-menu .dropdown-item.active {
- --files-markdown-heading-active-bg: #f3f6f9;
- }
- }
-}
-
body.theme-dark {
.files-viewer-markdown {
- .files-markdown-heading-menu .dropdown-item.active {
- --files-markdown-heading-active-bg: rgba(92, 141, 202, 0.32);
- }
.files-markdown-visual-editor {
background: #161b22;
@@ -324,8 +359,18 @@ body.theme-dark {
color: #aeb9ca;
}
+ pre {
+ background: rgba(156, 171, 194, 0.12);
+
+ code {
+ background: transparent;
+ color: inherit;
+ }
+ }
+
code {
background: rgba(156, 171, 194, 0.18);
+ color: #60a5fa;
}
table {
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 bf7ebcd9..0b0232de 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
@@ -1,31 +1,20 @@
import { CodeEditor } from '@acrodata/code-editor'
-import { HttpClient, HttpErrorResponse } from '@angular/common/http'
-import {
- Component,
- effect,
- HostListener,
- inject,
- input,
- model,
- OnDestroy,
- OnInit,
- signal,
- untracked,
- viewChild,
- ViewEncapsulation
-} from '@angular/core'
+import { Component, effect, ElementRef, 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'
import { languages } from '@codemirror/language-data'
import { closeSearchPanel, openSearchPanel } from '@codemirror/search'
import { FaIconComponent } from '@fortawesome/angular-fontawesome'
+import { faSquareMinus, faSquarePlus } from '@fortawesome/free-regular-svg-icons'
import {
faArrowsLeftRightToLine,
faBold,
faCode,
+ faEye,
faFloppyDisk,
faHeading,
+ faImage,
faItalic,
faKeyboard,
faLink,
@@ -35,7 +24,6 @@ import {
faLockOpen,
faMagnifyingGlass,
faMinus,
- faParagraph,
faPlus,
faQuoteLeft,
faReply,
@@ -47,27 +35,35 @@ import {
faTrashCan,
faUnderline
} from '@fortawesome/free-solid-svg-icons'
-import type { FileLockProps } from '@sync-in-server/backend/src/applications/files/interfaces/file-props.interface'
-import { Editor } from '@tiptap/core'
+import { Editor, Extension, type Range } from '@tiptap/core'
+import Image, { type SetImageOptions } from '@tiptap/extension-image'
import { TaskItem } from '@tiptap/extension-list'
import { TableKit } from '@tiptap/extension-table'
import { TaskList } from '@tiptap/extension-task-list'
import { Markdown } from '@tiptap/markdown'
import StarterKit from '@tiptap/starter-kit'
-import { L10N_LOCALE, L10nLocale, L10nTranslateDirective, L10nTranslatePipe } from 'angular-l10n'
+import { L10nTranslateDirective, L10nTranslatePipe } from 'angular-l10n'
import { ButtonCheckboxDirective } from 'ngx-bootstrap/buttons'
import { BsDropdownModule } from 'ngx-bootstrap/dropdown'
import { TooltipModule } from 'ngx-bootstrap/tooltip'
import { TiptapEditorDirective } from 'ngx-tiptap'
-import { firstValueFrom } from 'rxjs'
-import { type AppWindow, themeDark } from '../../../../layout/layout.interfaces'
-import { LayoutService } from '../../../../layout/layout.service'
-import { FileModel } from '../../models/file.model'
-import { FilesService } from '../../services/files.service'
-import { FilesUploadService } from '../../services/files-upload.service'
-import { fileLockPropsToString } from '../utils/file-lock.utils'
+import { FilesViewerEditableBase } from './files-viewer-editable-base'
type MarkdownHeadingLevel = 1 | 2 | 3 | 4
+type MarkdownInlineMark = 'bold' | 'code' | 'italic' | 'strike' | 'underline'
+
+const ExitInlineCodeOnEnter = Extension.create({
+ name: 'exitInlineCodeOnEnter',
+
+ addKeyboardShortcuts() {
+ return {
+ Enter: () => {
+ if (!this.editor.isActive('code') || this.editor.isActive('codeBlock')) return false
+ return this.editor.chain().splitBlock().unsetMark('code').run()
+ }
+ }
+ }
+})
@Component({
selector: 'app-files-viewer-markdown',
@@ -86,23 +82,14 @@ type MarkdownHeadingLevel = 1 | 2 | 3 | 4
styleUrl: 'files-viewer-markdown.component.scss',
templateUrl: 'files-viewer-markdown.component.html'
})
-export class FilesViewerMarkdownComponent implements OnInit, OnDestroy {
+export class FilesViewerMarkdownComponent extends FilesViewerEditableBase implements OnInit, OnDestroy {
private readonly sourceEditor = viewChild<CodeEditor>('sourceEditor')
- currentHeight = input.required<number>()
- file = model.required<FileModel>()
- isWriteable = input.required<boolean>()
- isReadonly = model.required<boolean>()
- modalClosing = input.required<boolean>()
- protected isSupported = signal(false)
- protected isModified = signal(false)
- protected isSaving = signal(false)
+ private readonly imageFileInput = viewChild<ElementRef<HTMLInputElement>>('imageFileInput')
protected isSourceMode = signal(false)
- protected lineWrapping = signal(true)
+ protected lineWrapping = signal(false)
protected isSearchPanelOpen = signal(false)
- protected warnOnUnsavedChanges = signal(false)
protected sourceContent = ''
protected currentLanguage: string | undefined = undefined
- protected currentTheme: 'dark' | 'light' = 'light'
protected readonly languages: LanguageDescription[] = languages
protected readonly editor = new Editor({
extensions: [
@@ -111,6 +98,7 @@ export class FilesViewerMarkdownComponent implements OnInit, OnDestroy {
openOnClick: false
}
}),
+ ExitInlineCodeOnEnter,
TaskList,
TaskItem.configure({
nested: true
@@ -120,6 +108,9 @@ export class FilesViewerMarkdownComponent implements OnInit, OnDestroy {
resizable: true
}
}),
+ Image.configure({
+ allowBase64: true
+ }),
Markdown
],
editable: false,
@@ -131,8 +122,10 @@ export class FilesViewerMarkdownComponent implements OnInit, OnDestroy {
faArrowsLeftRightToLine,
faBold,
faCode,
+ faEye,
faFloppyDisk,
faHeading,
+ faImage,
faItalic,
faKeyboard,
faLink,
@@ -142,7 +135,6 @@ export class FilesViewerMarkdownComponent implements OnInit, OnDestroy {
faLockOpen,
faMagnifyingGlass,
faMinus,
- faParagraph,
faPlus,
faQuoteLeft,
faReply,
@@ -152,31 +144,15 @@ export class FilesViewerMarkdownComponent implements OnInit, OnDestroy {
faStrikethrough,
faTable,
faTrashCan,
- faUnderline
+ faUnderline,
+ faSquareMinus,
+ faSquarePlus
}
protected readonly headingLevels: MarkdownHeadingLevel[] = [1, 2, 3, 4]
- protected readonly locale = inject<L10nLocale>(L10N_LOCALE)
- protected readonly layout = inject(LayoutService)
- private readonly http = inject(HttpClient)
- private readonly filesServices = inject(FilesService)
- private readonly filesUpload = inject(FilesUploadService)
- private readonly subscription = this.layout.switchTheme.subscribe((layout: string) => (this.currentTheme = layout === themeDark ? 'dark' : 'light'))
private savedContent = ''
constructor() {
- effect(() => {
- if (!this.modalClosing()) return
- const fileId = untracked(() => this.file().id)
- const modified = untracked(() => this.isModified())
- if (modified) {
- this.warnOnUnsavedChanges.set(true)
- if (this.layout.windows.getValue().find((w: AppWindow) => w.id === fileId)) {
- this.layout.restoreDialog(fileId)
- }
- } else {
- this.onClose().catch(console.error)
- }
- })
+ super()
effect(() => {
const editable = !this.isReadonly() && this.isWriteable() && this.isSupported()
if (!this.editor.isDestroyed) {
@@ -222,49 +198,11 @@ export class FilesViewerMarkdownComponent implements OnInit, OnDestroy {
this.loadContent().catch(console.error)
}
- ngOnDestroy() {
- this.subscription.unsubscribe()
+ override ngOnDestroy() {
+ super.ngOnDestroy()
this.editor.destroy()
}
- protected async toggleReadonly() {
- if (this.isReadonly()) {
- if (await this.lockFile()) {
- this.isReadonly.set(false)
- }
- } else {
- await this.unlockFile()
- this.isReadonly.set(true)
- }
- }
-
- protected save(exit = false) {
- if (!this.canSave()) return
- this.isSaving.set(true)
- const content = this.currentMarkdown()
- this.filesUpload.uploadOneFile(this.file(), content, true).subscribe({
- next: () => {
- this.sourceContent = content
- this.savedContent = content
- this.isModified.set(false)
- this.isSaving.set(false)
- this.warnOnUnsavedChanges.set(false)
- if (exit) {
- this.onClose().catch(console.error)
- }
- this.file().updateHTimeAgo()
- },
- error: (e: HttpErrorResponse) => {
- this.isSaving.set(false)
- this.layout.sendNotification('error', 'Unable to save document', e.error.message)
- }
- })
- }
-
- protected canSave(): boolean {
- return this.canEditContent() && this.isModified() && !this.isSaving()
- }
-
protected onUndo() {
if (this.isSourceMode()) {
this.runSourceHistory(undo)
@@ -308,7 +246,7 @@ export class FilesViewerMarkdownComponent implements OnInit, OnDestroy {
if (this.isSearchPanelOpen()) {
this.toggleSearch()
}
- this.editor.commands.setContent(this.sourceContent, { emitUpdate: false, contentType: 'markdown' })
+ this.setMarkdownContent(this.sourceContent)
}
this.isSourceMode.set(sourceMode)
setTimeout(() => {
@@ -336,32 +274,28 @@ export class FilesViewerMarkdownComponent implements OnInit, OnDestroy {
}
}
- protected setParagraph() {
- this.runEditorCommand(() => this.editor.chain().focus().setParagraph().run())
- }
-
protected toggleHeading(level: MarkdownHeadingLevel) {
this.runEditorCommand(() => this.editor.chain().focus().toggleHeading({ level }).run())
}
protected toggleBold() {
- this.runEditorCommand(() => this.editor.chain().focus().toggleBold().run())
+ this.toggleInlineMark('bold')
}
protected toggleItalic() {
- this.runEditorCommand(() => this.editor.chain().focus().toggleItalic().run())
+ this.toggleInlineMark('italic')
}
protected toggleUnderline() {
- this.runEditorCommand(() => this.editor.chain().focus().toggleUnderline().run())
+ this.toggleInlineMark('underline')
}
protected toggleStrike() {
- this.runEditorCommand(() => this.editor.chain().focus().toggleStrike().run())
+ this.toggleInlineMark('strike')
}
protected toggleCode() {
- this.runEditorCommand(() => this.editor.chain().focus().toggleCode().run())
+ this.toggleInlineMark('code')
}
protected toggleBulletList() {
@@ -400,6 +334,51 @@ export class FilesViewerMarkdownComponent implements OnInit, OnDestroy {
})
}
+ protected openImageFilePicker() {
+ this.runEditorCommand(() => {
+ const inputElement = this.imageFileInput()?.nativeElement
+ if (!inputElement) return false
+ inputElement.value = ''
+ inputElement.click()
+ return true
+ })
+ }
+
+ protected insertImageFromFile(event: Event) {
+ const inputElement = event.target as HTMLInputElement
+ const imageFile = inputElement.files?.[0]
+ inputElement.value = ''
+ if (!imageFile || !this.canEditVisual()) return
+ if (!imageFile.type.startsWith('image/')) {
+ this.layout.sendNotification('error', 'Unable to insert image', 'Unsupported image file')
+ return
+ }
+
+ const reader = new FileReader()
+ reader.onload = () => {
+ const src = typeof reader.result === 'string' ? reader.result : ''
+ if (!src) {
+ this.layout.sendNotification('error', 'Unable to insert image', imageFile.name)
+ return
+ }
+ this.insertImage({ src, alt: imageFile.name })
+ }
+ reader.onerror = () => this.layout.sendNotification('error', 'Unable to insert image', imageFile.name)
+ reader.readAsDataURL(imageFile)
+ }
+
+ protected insertImageFromUrl() {
+ this.runEditorCommand(() => {
+ const previousSrc = this.editor.getAttributes('image').src as string | undefined
+ const src = window.prompt('Image URL', previousSrc || 'https://')
+ if (src === null || !src.trim()) return false
+ const previousAlt = this.editor.getAttributes('image').alt as string | undefined
+ const alt = window.prompt('Alternative text', previousAlt || '')
+ if (alt === null) return false
+ return this.insertImage({ src: src.trim(), alt: alt.trim() || undefined })
+ })
+ }
+
protected insertTable() {
this.runEditorCommand(() => this.editor.chain().focus().insertTable({ rows: 3, cols: 3, withHeaderRow: true }).run())
}
@@ -438,25 +417,66 @@ export class FilesViewerMarkdownComponent implements OnInit, OnDestroy {
return this.editor.isFocused && this.editor.isActive(name, attributes)
}
- protected async onClose() {
- if (!this.isReadonly()) {
- await this.unlockFile()
- }
- this.layout.closeDialog(null, this.file().id)
- }
-
private runEditorCommand(command: () => boolean) {
if (this.canEditVisual() && !this.editor.isDestroyed) {
command()
}
}
- protected canEditVisual(): boolean {
- return !this.isSourceMode() && this.canEditContent()
+ private toggleInlineMark(markName: MarkdownInlineMark) {
+ this.runEditorCommand(() => {
+ const wordRange = this.currentWordRange()
+ if (!wordRange) {
+ return this.editor.chain().focus().toggleMark(markName, {}, { extendEmptyMarkRange: true }).run()
+ }
+
+ const cursorPosition = this.editor.state.selection.from
+ const command = this.editor.chain().focus().setTextSelection(wordRange)
+ return (this.editor.isActive(markName) ? command.unsetMark(markName) : command.setMark(markName)).setTextSelection(cursorPosition).run()
+ })
}
- protected canEditContent(): boolean {
- return !this.isReadonly() && this.isWriteable()
+ private insertImage(options: SetImageOptions): boolean {
+ return this.canEditVisual() && !this.editor.isDestroyed && this.editor.chain().focus().setImage(options).run()
+ }
+
+ private currentWordRange(): Range | null {
+ const { selection } = this.editor.state
+ if (!selection.empty) return null
+
+ const { $from } = selection
+ const text = $from.parent.textBetween(0, $from.parent.content.size, '\n', '\ufffc')
+ const offset = $from.parentOffset
+
+ if (!this.isWordCharacter(text[offset - 1]) && !this.isWordCharacter(text[offset])) {
+ return null
+ }
+
+ let start = offset
+ let end = offset
+
+ while (start > 0 && this.isWordCharacter(text[start - 1])) {
+ start--
+ }
+
+ while (end < text.length && this.isWordCharacter(text[end])) {
+ end++
+ }
+
+ if (start === end) return null
+
+ return {
+ from: $from.start() + start,
+ to: $from.start() + end
+ }
+ }
+
+ private isWordCharacter(character: string | undefined): boolean {
+ return !!character && /[\p{L}\p{N}_]/u.test(character)
+ }
+
+ protected canEditVisual(): boolean {
+ return !this.isSourceMode() && this.canEditContent()
}
private get sourceView() {
@@ -483,66 +503,28 @@ export class FilesViewerMarkdownComponent implements OnInit, OnDestroy {
return this.sourceContent
}
- private markModifiedIfNeeded() {
- const content = this.isSourceMode() ? this.sourceContent : this.editor.getMarkdown()
- this.isModified.set(content !== this.savedContent)
+ protected currentFileContent(): string {
+ return this.currentMarkdown()
}
- private async loadContent() {
- if (!this.isReadonly()) {
- await this.lockFile()
- }
- this.http.get(this.file().dataUrl, { responseType: 'text' }).subscribe({
- next: (data: string) => {
- this.savedContent = data
- this.sourceContent = data
- this.editor.commands.setContent(data, { emitUpdate: false, contentType: 'markdown' })
- this.isModified.set(false)
- },
- error: (e: HttpErrorResponse) => this.layout.sendNotification('error', 'Unable to open document', this.file().name, e)
- })
+ protected onContentLoaded(content: string) {
+ this.savedContent = content
+ this.sourceContent = content
+ this.setMarkdownContent(content)
+ this.isModified.set(false)
}
- private async lockFile(): Promise<boolean> {
- if (!this.isSupported() || !this.isWriteable()) return false
- try {
- const lock: FileLockProps = await firstValueFrom(this.filesServices.lock(this.file()))
- this.file.update((f) => {
- f.lock = lock
- return f
- })
- return true
- } catch (e) {
- this.lockError(e as HttpErrorResponse)
- return false
- }
+ protected override onContentSaved(content: string) {
+ this.sourceContent = content
+ this.savedContent = content
}
- private async unlockFile() {
- if (!this.isSupported() || !this.isWriteable()) return
- try {
- await firstValueFrom(this.filesServices.unlock(this.file()))
- this.file.update((f) => {
- delete f.lock
- return f
- })
- } catch (e) {
- this.lockError(e as HttpErrorResponse)
- }
+ private markModifiedIfNeeded() {
+ const content = this.isSourceMode() ? this.sourceContent : this.editor.getMarkdown()
+ this.isModified.set(content !== this.savedContent)
}
- private lockError(e: HttpErrorResponse) {
- this.isReadonly.set(true)
- this.isSupported.set(false)
- if (e.error?.owner) {
- const lock: FileLockProps = e.error
- this.file.update((f) => {
- f.lock = lock
- return f
- })
- this.layout.sendNotification('info', 'The file is locked', fileLockPropsToString(lock))
- } else {
- this.layout.sendNotification('warning', this.file().name, e.error.message)
- }
+ private setMarkdownContent(content: string) {
+ this.editor.chain().setMeta('addToHistory', false).setContent(content, { emitUpdate: false, contentType: 'markdown' }).run()
}
}
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 1c2d485f..78dbba9b 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
@@ -4,18 +4,18 @@
(click)="toggleReadonly()"
[tooltip]="(isReadonly() || !isWriteable() ? 'Read-only' : 'Read-write') | translate:locale.language"
[adaptivePosition]="true"
- class="btn btn-default me-1"
+ class="btn {{ isReadonly() ? 'btn-warning' : 'btn-default' }} me-1"
type="button"
[disabled]="!isSupported() || !isWriteable()">
<fa-icon [icon]="isReadonly() ? icons.faLock : icons.faLockOpen"></fa-icon>
</button>
<button
(click)="save()"
- [tooltip]="(isReadonly() || !isWriteable() || !isModified() ? undefined : 'Save') | translate:locale.language"
- [isDisabled]="isReadonly() || !isWriteable() || !isModified()"
+ [tooltip]="(canSave() ? 'Save' : undefined) | translate:locale.language"
+ [isDisabled]="!canSave()"
class="btn btn-default me-1"
type="button"
- [disabled]="isReadonly() || !isWriteable() || !isModified()">
+ [disabled]="!canSave()">
@if (isSaving()) {
<fa-icon [icon]="icons.faSpinner" animation="spin"></fa-icon>
} @else {
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 b22d42e5..fd360a3f 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,19 +1,5 @@
import { CodeEditor } from '@acrodata/code-editor'
-import { HttpClient, HttpErrorResponse } from '@angular/common/http'
-import {
- Component,
- effect,
- HostListener,
- inject,
- input,
- model,
- OnDestroy,
- OnInit,
- signal,
- untracked,
- viewChild,
- ViewEncapsulation
-} from '@angular/core'
+import { Component, effect, HostListener, OnInit, signal, untracked, viewChild, ViewEncapsulation } from '@angular/core'
import { FormsModule } from '@angular/forms'
import { redo, redoDepth, undo, undoDepth } from '@codemirror/commands'
import { LanguageDescription } from '@codemirror/language'
@@ -30,17 +16,10 @@ import {
faShare,
faSpinner
} from '@fortawesome/free-solid-svg-icons'
-import type { FileLockProps } from '@sync-in-server/backend/src/applications/files/interfaces/file-props.interface'
-import { L10N_LOCALE, L10nLocale, L10nTranslateDirective, L10nTranslatePipe } from 'angular-l10n'
+import { L10nTranslateDirective, L10nTranslatePipe } from 'angular-l10n'
import { ButtonCheckboxDirective } from 'ngx-bootstrap/buttons'
import { TooltipModule } from 'ngx-bootstrap/tooltip'
-import { firstValueFrom } from 'rxjs'
-import { AppWindow, themeDark } from '../../../../layout/layout.interfaces'
-import { LayoutService } from '../../../../layout/layout.service'
-import { FileModel } from '../../models/file.model'
-import { FilesUploadService } from '../../services/files-upload.service'
-import { FilesService } from '../../services/files.service'
-import { fileLockPropsToString } from '../utils/file-lock.utils'
+import { FilesViewerEditableBase } from './files-viewer-editable-base'
@Component({
selector: 'app-files-viewer-text',
@@ -74,48 +53,18 @@ import { fileLockPropsToString } from '../utils/file-lock.utils'
],
templateUrl: 'files-viewer-text.component.html'
})
-export class FilesViewerTextComponent implements OnInit, OnDestroy {
+export class FilesViewerTextComponent extends FilesViewerEditableBase implements OnInit {
editor = viewChild<CodeEditor>('editor')
- currentHeight = input.required<number>()
- file = model.required<FileModel>()
- isWriteable = input.required<boolean>()
- isReadonly = model.required<boolean>()
- modalClosing = input.required<boolean>()
- protected isSupported = signal(false)
- protected isModified = signal(false)
- protected isSaving = signal(false)
protected lineWrapping = signal(false)
- protected warnOnUnsavedChanges = signal(false)
protected content: any = false
protected currentLanguage = undefined
protected readonly languages: LanguageDescription[] = languages
- protected currentTheme: any = 'light'
protected readonly icons = { faFloppyDisk, faLock, faLockOpen, faMagnifyingGlass, faSpinner, faArrowsLeftRightToLine, faReply, faShare }
protected isSearchPanelOpen = signal(false)
- protected readonly layout = inject(LayoutService)
- protected readonly locale = inject<L10nLocale>(L10N_LOCALE)
private isContentReady = false
- private readonly http = inject(HttpClient)
- private readonly filesServices = inject(FilesService)
- private readonly filesUpload = inject(FilesUploadService)
- private subscription = this.layout.switchTheme.subscribe((layout: string) => (this.currentTheme = layout === themeDark ? 'dark' : 'light'))
constructor() {
- effect(() => {
- // Only track modalClosing
- if (!this.modalClosing()) return
- const fileId = untracked(() => this.file().id)
- const modified = untracked(() => this.isModified()) // ignore dependency on isModified
- if (modified) {
- this.warnOnUnsavedChanges.set(true)
- // restore dialog if minimized
- if (this.layout.windows.getValue().find((w: AppWindow) => w.id === fileId)) {
- this.layout.restoreDialog(fileId)
- }
- } else {
- this.onClose().catch(console.error)
- }
- })
+ super()
effect(() => {
this.isReadonly()
// Reset search state when open to enable/disable the replace function
@@ -172,34 +121,6 @@ export class FilesViewerTextComponent implements OnInit, OnDestroy {
this.loadContent().catch(console.error)
}
- async toggleReadonly() {
- if (this.isReadonly()) {
- await this.lockFile()
- } else {
- await this.unlockFile()
- }
- this.isReadonly.update((state) => !state)
- }
-
- save(exit = false) {
- this.isSaving.set(true)
- this.filesUpload.uploadOneFile(this.file(), this.content, true).subscribe({
- next: () => {
- this.isModified.set(false)
- this.isSaving.set(false)
- this.warnOnUnsavedChanges.set(false)
- if (exit) {
- this.onClose().catch(console.error)
- }
- this.file().updateHTimeAgo()
- },
- error: (e: HttpErrorResponse) => {
- this.isSaving.set(false)
- this.layout.sendNotification('error', 'Unable to save document', e.error.message)
- }
- })
- }
-
toggleSearch() {
this.isSearchPanelOpen.update((value) => !value)
if (this.isSearchPanelOpen()) {
@@ -240,65 +161,11 @@ export class FilesViewerTextComponent implements OnInit, OnDestroy {
return false
}
- async onClose() {
- if (!this.isReadonly()) {
- await this.unlockFile()
- }
- this.layout.closeDialog(null, this.file().id)
- }
-
- ngOnDestroy() {
- this.subscription.unsubscribe()
- }
-
- private async loadContent() {
- if (!this.isReadonly()) {
- await this.lockFile()
- }
- this.http.get(this.file().dataUrl, { responseType: 'text' }).subscribe({
- next: (data: string) => (this.content = data),
- error: (e: HttpErrorResponse) => this.layout.sendNotification('error', 'Unable to open document', this.file().name, e)
- })
- }
-
- private async lockFile() {
- if (!this.isSupported || !this.isWriteable()) return
- try {
- const lock: FileLockProps = await firstValueFrom(this.filesServices.lock(this.file()))
- this.file.update((f) => {
- f.lock = lock
- return f
- })
- } catch (e) {
- this.lockError(e as HttpErrorResponse)
- }
- }
-
- private async unlockFile() {
- if (!this.isSupported || !this.isWriteable()) return
- try {
- await firstValueFrom(this.filesServices.unlock(this.file()))
- this.file.update((f) => {
- delete f.lock
- return f
- })
- } catch (e) {
- this.lockError(e as HttpErrorResponse)
- }
+ protected currentFileContent(): string {
+ return this.content || ''
}
- private lockError(e: HttpErrorResponse) {
- this.isReadonly.set(true)
- this.isSupported.set(false)
- if (e.error?.owner) {
- const lock: FileLockProps = e.error
- this.file.update((f) => {
- f.lock = lock
- return f
- })
- this.layout.sendNotification('info', 'The file is locked', fileLockPropsToString(lock))
- } else {
- this.layout.sendNotification('warning', this.file().name, e.error.message)
- }
+ protected onContentLoaded(content: string) {
+ this.content = content
}
}
diff --git a/frontend/src/styles/components/_sidebar_right.scss b/frontend/src/styles/components/_sidebar_right.scss
index a375dd01..0e87be49 100755
--- a/frontend/src/styles/components/_sidebar_right.scss
+++ b/frontend/src/styles/components/_sidebar_right.scss
@@ -46,7 +46,7 @@
display: flex;
align-items: center;
width: 100%;
- height: 35px;
+ height: 40px;
padding: .2rem .4rem .2rem .4rem;
border-bottom: .5px solid transparent;
}
diff --git a/package-lock.json b/package-lock.json
index 46214fe7..a37a4128 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -169,6 +169,7 @@
"@fortawesome/free-solid-svg-icons": "^7.0.0",
"@perfectmemory/ngx-contextmenu": "^20.0.0",
"@tiptap/core": "^3.23.4",
+ "@tiptap/extension-image": "^3.23.4",
"@tiptap/extension-list": "^3.23.4",
"@tiptap/extension-table": "^3.23.4",
"@tiptap/extension-task-list": "^3.23.4",
@@ -10734,6 +10735,19 @@
"@tiptap/pm": "3.23.4"
}
},
+ "node_modules/@tiptap/extension-image": {
+ "version": "3.23.4",
+ "resolved": "https://registry.npmjs.org/@tiptap/extension-image/-/extension-image-3.23.4.tgz",
+ "integrity": "sha512-qandp5HLRl+n8D61+LCT67qtb1uSKffyEGD0fVTkg/RfbyFsJvCDFbjVEoiIG8JOx8O5DehgrDCvS35QOWgr2A==",
+ "license": "MIT",
+ "funding": {
+ "type": "github",
+ "url": "https://github.com/sponsors/ueberdosis"
+ },
+ "peerDependencies": {
+ "@tiptap/core": "3.23.4"
+ }
+ },
"node_modules/@tiptap/extension-italic": {
"version": "3.23.4",
"resolved": "https://registry.npmjs.org/@tiptap/extension-italic/-/extension-italic-3.23.4.tgz",