refactor(frontend): rename booklore-ui to frontend
- Frontend path: - rename the tracked Angular app tree from `booklore-ui/` to `frontend/` - preserve file contents during the move so this commit stays focused on the path change itself - Frontend docs: - update the moved frontend README and contribution guide where they refer to the renamed folder - keep those edits limited to path references introduced by the directory rename
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import {CustomFont} from '../model/custom-font.model';
|
||||
|
||||
/**
|
||||
* Font dropdown item for epub-reader component (PrimeNG Select format)
|
||||
*/
|
||||
export interface FontDropdownItem {
|
||||
label: string;
|
||||
value: string | null;
|
||||
disabled?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Font dropdown item for epub-reader-preferences component
|
||||
*/
|
||||
export interface FontPreferenceItem {
|
||||
name: string;
|
||||
displayName: string;
|
||||
key: string | null;
|
||||
}
|
||||
|
||||
const MAX_FONT_DISPLAY_LENGTH = 12;
|
||||
|
||||
/**
|
||||
* Adds custom fonts to a dropdown array with a separator.
|
||||
* Removes any existing separator before adding to prevent duplicates.
|
||||
*/
|
||||
export function addCustomFontsToDropdown<T extends FontDropdownItem | FontPreferenceItem>(
|
||||
fonts: CustomFont[],
|
||||
targetArray: T[],
|
||||
format: 'select' | 'preference'
|
||||
): void {
|
||||
if (fonts.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (format === 'select') {
|
||||
const separatorIndex = (targetArray as FontDropdownItem[]).findIndex(item => item.value === 'separator');
|
||||
if (separatorIndex !== -1) {
|
||||
targetArray.splice(separatorIndex, 1);
|
||||
}
|
||||
|
||||
fonts.forEach(font => {
|
||||
(targetArray as FontDropdownItem[]).push({
|
||||
label: font.fontName,
|
||||
value: `custom:${font.id}`
|
||||
});
|
||||
});
|
||||
} else {
|
||||
const separatorIndex = (targetArray as FontPreferenceItem[]).findIndex(item => item.key === 'separator');
|
||||
if (separatorIndex !== -1) {
|
||||
targetArray.splice(separatorIndex, 1);
|
||||
}
|
||||
|
||||
fonts.forEach(font => {
|
||||
(targetArray as FontPreferenceItem[]).push({
|
||||
name: font.fontName,
|
||||
displayName: font.fontName.substring(0, MAX_FONT_DISPLAY_LENGTH),
|
||||
key: `custom:${font.id}`
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
const MODIFIER_PLACEHOLDER_REGEX = /\{([^}:]+)(?::([^}]+))?}/g;
|
||||
|
||||
export function applyModifier(value: string, modifier: string, fieldName: string): string {
|
||||
if (!value) return value;
|
||||
|
||||
switch (modifier) {
|
||||
case 'first':
|
||||
return value.split(', ')[0].trim();
|
||||
case 'sort': {
|
||||
const first = value.split(', ')[0].trim();
|
||||
const lastSpace = first.lastIndexOf(' ');
|
||||
return lastSpace > 0 ? `${first.substring(lastSpace + 1)}, ${first.substring(0, lastSpace)}` : first;
|
||||
}
|
||||
case 'initial': {
|
||||
let target = value;
|
||||
if (fieldName === 'authors') {
|
||||
const firstAuthor = value.split(', ')[0].trim();
|
||||
const lastSpace = firstAuthor.lastIndexOf(' ');
|
||||
target = lastSpace > 0 ? firstAuthor.substring(lastSpace + 1) : firstAuthor;
|
||||
}
|
||||
return target.charAt(0).toUpperCase();
|
||||
}
|
||||
case 'upper':
|
||||
return value.toUpperCase();
|
||||
case 'lower':
|
||||
return value.toLowerCase();
|
||||
default:
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
function resolveModifierPlaceholders(block: string, values: Record<string, string>): string {
|
||||
return block.replace(MODIFIER_PLACEHOLDER_REGEX, (_, fieldName: string, modifier?: string) => {
|
||||
const val = values[fieldName] ?? '';
|
||||
return modifier ? applyModifier(val, modifier, fieldName) : val;
|
||||
});
|
||||
}
|
||||
|
||||
function checkAllPlaceholdersPresent(block: string, values: Record<string, string>): boolean {
|
||||
const matches = [...block.matchAll(MODIFIER_PLACEHOLDER_REGEX)];
|
||||
return matches.every(m => values[m[1]]?.trim());
|
||||
}
|
||||
|
||||
export function replacePlaceholders(pattern: string, values: Record<string, string>): string {
|
||||
// Handle optional blocks with else clause: <primary|fallback>
|
||||
pattern = pattern.replace(/<([^<>]+)>/g, (_, blockContent: string) => {
|
||||
const pipeIndex = blockContent.indexOf('|');
|
||||
const primary = pipeIndex >= 0 ? blockContent.substring(0, pipeIndex) : blockContent;
|
||||
const fallback = pipeIndex >= 0 ? blockContent.substring(pipeIndex + 1) : null;
|
||||
|
||||
if (checkAllPlaceholdersPresent(primary, values)) {
|
||||
return resolveModifierPlaceholders(primary, values);
|
||||
}
|
||||
return fallback != null ? resolveModifierPlaceholders(fallback, values) : '';
|
||||
});
|
||||
|
||||
// Replace remaining top-level placeholders (with optional modifiers)
|
||||
return resolveModifierPlaceholders(pattern, values).trim();
|
||||
}
|
||||
Reference in New Issue
Block a user