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,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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user