many, MANY improvements to admin UI
This commit is contained in:
1 parent
eac0938ccf
commit
3b6ac1dcab
14 files changed
+2421
-99
No files matched your search
@@ -0,0 +1,193 @@
|
||||
.markdown-editor {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
width: 100%;
|
||||
box-sizing: border-box;
|
||||
background: #26262c;
|
||||
border-radius: 24px;
|
||||
}
|
||||
|
||||
.toolbar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 10px;
|
||||
background: #26262c;
|
||||
border-radius: 24px 24px 0 0;
|
||||
box-sizing: border-box;
|
||||
padding: 0 20px;
|
||||
}
|
||||
|
||||
.toolbar-left {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 0;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.toolbar-right {
|
||||
display: flex;
|
||||
flex-shrink: 0;
|
||||
margin-left: auto;
|
||||
}
|
||||
|
||||
.toolbar.mobile {
|
||||
flex-direction: column;
|
||||
align-items: stretch;
|
||||
}
|
||||
|
||||
.toolbar.mobile .toolbar-left {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.toolbar.mobile .toolbar-right {
|
||||
width: 100%;
|
||||
margin-left: 0;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.toolbar button {
|
||||
width: auto;
|
||||
white-space: nowrap;
|
||||
margin: 5px;
|
||||
background: transparent;
|
||||
border: none;
|
||||
color: white;
|
||||
font-size: 0.9rem;
|
||||
font-family: "Roboto", sans-serif;
|
||||
padding: 8px 12px;
|
||||
cursor: pointer;
|
||||
border-radius: 12px;
|
||||
}
|
||||
|
||||
.toolbar button:hover {
|
||||
background: #2c2c33;
|
||||
}
|
||||
|
||||
.preview-toggle button {
|
||||
margin-left: auto;
|
||||
}
|
||||
|
||||
.toolbar-right button {
|
||||
background-color: #202025;
|
||||
}
|
||||
|
||||
.separator {
|
||||
width: 5px;
|
||||
border-radius: 2px;
|
||||
margin: 5px 0;
|
||||
background: #202025;
|
||||
}
|
||||
|
||||
.editor-textarea,
|
||||
.preview {
|
||||
order: 2;
|
||||
}
|
||||
|
||||
textarea {
|
||||
width: calc(100% - 10px);
|
||||
margin: 0 5px;
|
||||
resize: none;
|
||||
overflow-x: hidden;
|
||||
overflow-y: auto;
|
||||
border: none;
|
||||
box-sizing: border-box;
|
||||
border-radius: 20px;
|
||||
background: #202025;
|
||||
color: white;
|
||||
padding: 15px 20px;
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
textarea:focus {
|
||||
outline: none;
|
||||
background: #2b2b32;
|
||||
}
|
||||
|
||||
.preview {
|
||||
width: calc(100% - 10px);
|
||||
height: calc(100% - 49px);
|
||||
margin-left: 5px;
|
||||
text-align: left;
|
||||
padding: 0 20px;
|
||||
border-radius: 20px;
|
||||
background: #2b2b32;
|
||||
box-sizing: border-box;
|
||||
font-family: Roboto, sans-serif;
|
||||
overflow-x: hidden;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
textarea:disabled, .toolbar button:disabled {
|
||||
background-color: rgba(28, 21, 21, 0.4);
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
::ng-deep h1, ::ng-deep h2, ::ng-deep h3, ::ng-deep h4, ::ng-deep h6 {
|
||||
color: #9993b3;
|
||||
}
|
||||
|
||||
::ng-deep a {
|
||||
text-decoration: underline;
|
||||
color: #C985DFFF;
|
||||
}
|
||||
|
||||
::ng-deep a:visited {
|
||||
color: #8E44A39E;
|
||||
}
|
||||
|
||||
::ng-deep a:hover {
|
||||
color: #df59ff;
|
||||
}
|
||||
|
||||
::ng-deep a:active {
|
||||
color: #eea9ff;
|
||||
}
|
||||
|
||||
::ng-deep .centered-text {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
::ng-deep img {
|
||||
max-width: 90%;
|
||||
padding: 15px;
|
||||
border-radius: 35px;
|
||||
height: auto;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
::ng-deep .gallery {
|
||||
display: grid;
|
||||
max-width: 90vw;
|
||||
grid-template-columns: repeat(3, 1fr);
|
||||
gap: 15px;
|
||||
margin: auto;
|
||||
padding: 15px;
|
||||
}
|
||||
|
||||
::ng-deep .gallery img {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
border-radius: 20px;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
::ng-deep .gallery img {
|
||||
flex: 1 1 calc(50% - 15px);
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
::ng-deep .gallery img {
|
||||
gap: 0;
|
||||
padding: 0;
|
||||
flex: 1 1 100%;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
::ng-deep img {
|
||||
max-width: 98%;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
<div
|
||||
class="markdown-editor"
|
||||
[class.disabled]="disabled"
|
||||
[ngStyle]="{'height': isNarrow ? 'calc(100% - 280px)' : 'calc(100% - 75px)'}"
|
||||
>
|
||||
@if (!previewMode) {
|
||||
<textarea
|
||||
#editorRef
|
||||
class="editor-textarea"
|
||||
[value]="value"
|
||||
[placeholder]="placeholder"
|
||||
[disabled]="disabled"
|
||||
(input)="onInput(editorRef.value)"
|
||||
(blur)="markTouched()"
|
||||
[ngStyle]="{'height': isNarrow ? 'calc(100% - 192px)' : 'calc(100% - 49px)'}"
|
||||
></textarea>
|
||||
|
||||
<div class="toolbar" [class.mobile]="isNarrow">
|
||||
<div class="toolbar-left">
|
||||
<button type="button" (click)="insertAtCursor(editorRef, '**', '**', 'bold')" [disabled]="disabled">
|
||||
<strong>B</strong>
|
||||
</button>
|
||||
|
||||
@if (!isNarrow) {
|
||||
<div class="separator"></div>
|
||||
}
|
||||
|
||||
<button type="button" (click)="insertAtCursor(editorRef, '*', '*', 'italic')" [disabled]="disabled">
|
||||
<em>I</em>
|
||||
</button>
|
||||
|
||||
@if (!isNarrow) {
|
||||
<div class="separator"></div>
|
||||
}
|
||||
|
||||
<button type="button" (click)="insertLinePrefix(editorRef, '# ')" [disabled]="disabled">
|
||||
H1
|
||||
</button>
|
||||
|
||||
@if (!isNarrow) {
|
||||
<div class="separator"></div>
|
||||
}
|
||||
|
||||
<button type="button" (click)="insertLinePrefix(editorRef, '## ')" [disabled]="disabled">
|
||||
H2
|
||||
</button>
|
||||
|
||||
@if (!isNarrow) {
|
||||
<div class="separator"></div>
|
||||
}
|
||||
|
||||
<button type="button" (click)="insertLinePrefix(editorRef, '- ')" [disabled]="disabled">
|
||||
• List
|
||||
</button>
|
||||
|
||||
@if (!isNarrow) {
|
||||
<div class="separator"></div>
|
||||
}
|
||||
|
||||
<button type="button" (click)="insertLinePrefix(editorRef, '1. ')" [disabled]="disabled">
|
||||
1. List
|
||||
</button>
|
||||
|
||||
@if (!isNarrow) {
|
||||
<div class="separator"></div>
|
||||
}
|
||||
|
||||
<button type="button" (click)="insertLink(editorRef)" [disabled]="disabled">
|
||||
Link
|
||||
</button>
|
||||
|
||||
@if (!isNarrow) {
|
||||
<div class="separator"></div>
|
||||
}
|
||||
|
||||
<button type="button" (click)="insertImage(editorRef)" [disabled]="disabled">
|
||||
Image
|
||||
</button>
|
||||
|
||||
@if (!isNarrow) {
|
||||
<div class="separator"></div>
|
||||
}
|
||||
|
||||
<button type="button" (click)="insertCenter(editorRef)" [disabled]="disabled">
|
||||
Center
|
||||
</button>
|
||||
|
||||
@if (!isNarrow) {
|
||||
<div class="separator"></div>
|
||||
}
|
||||
|
||||
<button type="button" (click)="insertGallery(editorRef)" [disabled]="disabled">
|
||||
Gallery
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="toolbar-right">
|
||||
<button type="button" class="preview-toggle" (click)="togglePreview()">
|
||||
Preview
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
@if (previewMode) {
|
||||
<div class="toolbar">
|
||||
<div class="toolbar-right">
|
||||
<button type="button" class="preview-toggle" (click)="togglePreview()">
|
||||
Raw Markdown
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="preview">
|
||||
<markdown [data]="previewContent"></markdown>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
@@ -0,0 +1,221 @@
|
||||
import {Component, forwardRef, OnInit} from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { ControlValueAccessor, FormsModule, NG_VALUE_ACCESSOR } from '@angular/forms';
|
||||
import {MarkdownModule} from 'ngx-markdown';
|
||||
import { ViewportService} from '../services/viewport.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-markdown-editor',
|
||||
imports: [CommonModule, FormsModule, MarkdownModule],
|
||||
templateUrl: './markdown-editor.component.html',
|
||||
styleUrl: './markdown-editor.component.css',
|
||||
providers: [
|
||||
{
|
||||
provide: NG_VALUE_ACCESSOR,
|
||||
useExisting: forwardRef(() => MarkdownEditorComponent),
|
||||
multi: true,
|
||||
},
|
||||
],
|
||||
})
|
||||
export class MarkdownEditorComponent implements ControlValueAccessor, OnInit {
|
||||
value = '';
|
||||
placeholder = 'Write your article content here...';
|
||||
previewMode = false;
|
||||
disabled = false;
|
||||
|
||||
isNarrow: boolean = false;
|
||||
|
||||
constructor(private viewportService: ViewportService) { }
|
||||
|
||||
private onChange: (value: string) => void = () => {};
|
||||
private onTouched: () => void = () => {};
|
||||
|
||||
ngOnInit() {
|
||||
this.isNarrow = this.viewportService.isNarrow;
|
||||
}
|
||||
|
||||
writeValue(value: string | null): void {
|
||||
this.value = value ?? '';
|
||||
}
|
||||
|
||||
registerOnChange(fn: (value: string) => void): void {
|
||||
this.onChange = fn;
|
||||
}
|
||||
|
||||
registerOnTouched(fn: () => void): void {
|
||||
this.onTouched = fn;
|
||||
}
|
||||
|
||||
setDisabledState(isDisabled: boolean): void {
|
||||
this.disabled = isDisabled;
|
||||
}
|
||||
|
||||
onInput(value: string): void {
|
||||
this.value = value;
|
||||
this.onChange(this.value);
|
||||
}
|
||||
|
||||
markTouched(): void {
|
||||
this.onTouched();
|
||||
}
|
||||
|
||||
togglePreview(): void {
|
||||
this.previewMode = !this.previewMode;
|
||||
}
|
||||
|
||||
insertAtCursor(
|
||||
textarea: HTMLTextAreaElement,
|
||||
before: string,
|
||||
after = '',
|
||||
placeholder = 'text'
|
||||
): void {
|
||||
if (this.disabled) return;
|
||||
|
||||
const start = textarea.selectionStart;
|
||||
const end = textarea.selectionEnd;
|
||||
const selectedText = this.value.slice(start, end);
|
||||
const replacement = `${before}${selectedText || placeholder}${after}`;
|
||||
|
||||
this.value =
|
||||
this.value.slice(0, start) +
|
||||
replacement +
|
||||
this.value.slice(end);
|
||||
|
||||
this.onChange(this.value);
|
||||
this.onTouched();
|
||||
|
||||
queueMicrotask(() => {
|
||||
textarea.focus();
|
||||
|
||||
const cursorStart = start + before.length;
|
||||
const cursorEnd = selectedText ? start + replacement.length : cursorStart + placeholder.length;
|
||||
|
||||
textarea.setSelectionRange(cursorStart, cursorEnd);
|
||||
});
|
||||
}
|
||||
|
||||
insertLinePrefix(textarea: HTMLTextAreaElement, prefix: string): void {
|
||||
if (this.disabled) return;
|
||||
|
||||
const start = textarea.selectionStart;
|
||||
const end = textarea.selectionEnd;
|
||||
|
||||
const beforeSelection = this.value.slice(0, start);
|
||||
const afterSelection = this.value.slice(end);
|
||||
|
||||
const selectionStartLine = beforeSelection.lastIndexOf('\n') + 1;
|
||||
const fullSelectedBlock = this.value.slice(selectionStartLine, end);
|
||||
|
||||
const updatedBlock = fullSelectedBlock
|
||||
.split('\n')
|
||||
.map(line => (line.trim().length ? `${prefix}${line}` : line))
|
||||
.join('\n');
|
||||
|
||||
this.value =
|
||||
this.value.slice(0, selectionStartLine) +
|
||||
updatedBlock +
|
||||
afterSelection;
|
||||
|
||||
this.onChange(this.value);
|
||||
this.onTouched();
|
||||
|
||||
queueMicrotask(() => {
|
||||
textarea.focus();
|
||||
textarea.setSelectionRange(selectionStartLine, selectionStartLine + updatedBlock.length);
|
||||
});
|
||||
}
|
||||
|
||||
insertLink(textarea: HTMLTextAreaElement): void {
|
||||
if (this.disabled) return;
|
||||
|
||||
const url = window.prompt('Enter URL:', 'https://');
|
||||
if (!url) return;
|
||||
|
||||
this.insertAtCursor(textarea, '[', `](${url})`, 'link text');
|
||||
}
|
||||
|
||||
insertImage(textarea: HTMLTextAreaElement): void {
|
||||
if (this.disabled) return;
|
||||
|
||||
const url = window.prompt('Enter image URL:', 'https://');
|
||||
if (!url) return;
|
||||
|
||||
const alt = window.prompt('Enter alt text:', 'image') || 'image';
|
||||
this.insertAtCursor(textarea, `', url);
|
||||
}
|
||||
|
||||
insertCenter(textarea: HTMLTextAreaElement): void {
|
||||
this.insertAtCursor(textarea, '[center]\n', '\n[/center]', 'centered text');
|
||||
}
|
||||
|
||||
insertGallery(textarea: HTMLTextAreaElement): void {
|
||||
if (this.disabled) return;
|
||||
|
||||
const template = [
|
||||
'[gallery]',
|
||||
'https://example.com/image1.jpg',
|
||||
'https://example.com/image2.jpg',
|
||||
'https://example.com/image3.jpg',
|
||||
'[/gallery]'
|
||||
].join('\n');
|
||||
|
||||
const start = textarea.selectionStart;
|
||||
const end = textarea.selectionEnd;
|
||||
const selectedText = this.value.slice(start, end).trim();
|
||||
|
||||
const replacement = selectedText
|
||||
? `[gallery]\n${selectedText}\n[/gallery]`
|
||||
: template;
|
||||
|
||||
this.value =
|
||||
this.value.slice(0, start) +
|
||||
replacement +
|
||||
this.value.slice(end);
|
||||
|
||||
this.onChange(this.value);
|
||||
this.onTouched();
|
||||
|
||||
queueMicrotask(() => {
|
||||
textarea.focus();
|
||||
|
||||
if (selectedText) {
|
||||
textarea.setSelectionRange(start, start + replacement.length);
|
||||
} else {
|
||||
const firstUrlStart = start + '[gallery]\n'.length;
|
||||
const firstUrlEnd = firstUrlStart + 'https://example.com/image1.jpg'.length;
|
||||
textarea.setSelectionRange(firstUrlStart, firstUrlEnd);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
get previewContent(): string {
|
||||
return this.transformCustomBlocks(this.value);
|
||||
}
|
||||
|
||||
transformCustomBlocks(content: string): string {
|
||||
let result = content;
|
||||
|
||||
result = result.replace(
|
||||
/\[gallery\]\s*([\s\S]*?)\s*\[\/gallery\]/g,
|
||||
(_, inner) => {
|
||||
const urls = inner
|
||||
.split('\n')
|
||||
.map((line: string) => line.trim())
|
||||
.filter((line: string) => line.length > 0);
|
||||
|
||||
const images = urls
|
||||
.map((url: string) => `<img src="${url}" alt="Gallery image">`)
|
||||
.join('');
|
||||
|
||||
return `<div class="gallery">${images}</div>`;
|
||||
}
|
||||
);
|
||||
|
||||
result = result.replace(
|
||||
/\[center\]\s*([\s\S]*?)\s*\[\/center\]/g,
|
||||
(_, inner) => `<div class="centered-text">${inner}</div>`
|
||||
);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -28,14 +28,20 @@ export class ArticleService {
|
||||
return this.http.get<Article[]>(`${this.apiUrl}?timestamp=${new Date().getTime()}`);
|
||||
}
|
||||
|
||||
getArticleBySlug(slug: string): Observable<Article |undefined> {
|
||||
return this.http.get<Article>(`${this.apiUrl}/slug/${slug}`).pipe(
|
||||
getArticleBySlug(slug: string, token?: string | null): Observable<Article |undefined> {
|
||||
const options = token
|
||||
? { headers: new HttpHeaders().set('Authorization', `Bearer ${token}`) }
|
||||
: {};
|
||||
return this.http.get<Article>(`${this.apiUrl}/slug/${slug}`, options).pipe(
|
||||
map(article => article)
|
||||
);
|
||||
}
|
||||
|
||||
getArticleById(id: number): Observable<Article |undefined> {
|
||||
return this.http.get<Article>(`${this.apiUrl}/id/${id}`).pipe(
|
||||
getArticleById(id: number, token?: string | null): Observable<Article |undefined> {
|
||||
const options = token
|
||||
? { headers: new HttpHeaders().set('Authorization', `Bearer ${token}`) }
|
||||
: {};
|
||||
return this.http.get<Article>(`${this.apiUrl}/id/${id}`, options).pipe(
|
||||
map(article => article)
|
||||
);
|
||||
}
|
||||
@@ -61,10 +67,12 @@ export class ArticleService {
|
||||
);
|
||||
}
|
||||
|
||||
editArticle(id: number, token: string | null, title: string, content: string): Observable<any> {
|
||||
editArticle(
|
||||
id: number,
|
||||
token: string | null,
|
||||
updates: { title?: string; content?: string }
|
||||
): Observable<any> {
|
||||
const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`);
|
||||
const body = { title, content };
|
||||
|
||||
return this.http.put<any>(`${this.apiUrl}/${id}`, body, { headers });
|
||||
return this.http.put<any>(`${this.apiUrl}/${id}`, updates, { headers });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user