Skip to content

Commit 1df8abf

Browse files
fmiccolisandi34
authored andcommitted
generator: fix font selection
Change-Id: I95b7ac0668190184bb1a57b6ca8dab8620cc808d
1 parent d3e07df commit 1df8abf

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

admin/generator/index.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@
236236
justify-content: center;
237237
}
238238
</style>
239+
<style id="fontselectedStyle"></style>
239240
<div class="w-full flex items-center justify-center flex-col">
240241
<div class="w-full max-w-[1500px] rounded-lg p-4 md:p-8 bg-white flex flex-col shadow-xl place-items-center relative">
241242
<div class="w-full text-center flex flex-col items-center justify-center text-2xl font-bold text-brand-1 mb-2">
@@ -722,7 +723,7 @@
722723
<div id="collage_frame" class="absolute h-full w-full">
723724
<img class="h-full w-full hidden" src="" alt="Choose the frame">
724725
</div>
725-
<div id="collage_text" class="absolute h-full">
726+
<div id="collage_text" class="absolute h-full font-selected">
726727
<div class='relative'>
727728
<div class='absolute whitespace-nowrap origin-top-left text-line-1 leading-none'></div>
728729
<div class='absolute whitespace-nowrap origin-top-left text-line-2 leading-none'></div>

assets/js/admin/fontSelect.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// eslint-disable-next-line no-unused-vars
2-
function adminFontSelect(element, path) {
2+
function adminFontSelect(element, path, fontclass) {
33
const parent = element.closest('.adminFontSelection');
44
const origin = element.dataset.origin;
55
const src = element.src;
@@ -10,6 +10,7 @@ function adminFontSelect(element, path) {
1010
previewElement.src = src;
1111
textElement.textContent = origin;
1212
inputElement.value = origin;
13+
inputElement.setAttribute('data-fontclass', fontclass);
1314

1415
const event = new Event('change');
1516
inputElement.dispatchEvent(event);

assets/js/admin/generator.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ function loadCurrentConfig() {
103103
//text
104104
$('input[name=\'text_enabled\'][data-trigger=\'general\']').prop('checked', text_enabled);
105105
$('input[name=\'text_font_family\']').val(font_family);
106-
let fClass = $('input[name=\'text_font_family\']').data('fontclass');
106+
$('input[name=\'text_font_family\']')[0].setAttribute('data-fontclass', 'font-selected');
107107
$('#fontselectedStyle').html(
108-
`@font-face{font-family:"fontselected";src:url(${font_family}) format("truetype");} .${fClass}{font-family:"fontselected",Verdena,Tahoma;}`
108+
`@font-face{font-family:"fontselected";src:url(${font_family}) format("truetype");} .font-selected{font-family:"fontselected",Verdena,Tahoma;}`
109109
);
110110
$('input[name=\'text_font_color\']').attr('value', font_color);
111111
$('input[name=\'text_font_size\']').attr('value', font_size);
@@ -157,7 +157,7 @@ function changeGeneralSetting() {
157157
const c_show_background = $('input[name=\'show-background\'][data-trigger=\'general\']').is(':checked');
158158

159159
const c_text_enabled = $('input[name=\'text_enabled\'][data-trigger=\'general\']').is(':checked');
160-
let c_text_font = $('input[name=\'text_font_family\']').data('fontclass');
160+
let c_text_font = $('input[name=\'text_font_family\']')[0].getAttribute('data-fontclass');
161161
let c_text_font_unique_id = $('input[name=\'text_font_family\']').data('unique-id');
162162
let c_text_font_value = $('input[name=\'text_font_family\']').val();
163163
const c_font_color = $('input[name=\'text_font_color\']').val();
@@ -217,15 +217,15 @@ function changeGeneralSetting() {
217217
left: adjusted_tl + 'px'
218218
});
219219
if (c_text_font_value.startsWith('http')) {
220-
c_text_font = `customfont-${c_text_font_unique_id}`;
220+
c_text_font = `font-${c_text_font_unique_id}`;
221221
$('#fontselectedStyle').html(
222222
`@font-face{font-family:"fontselected";src:url(${c_text_font_value}) format("truetype");} .${c_text_font}{font-family:"fontselected",Verdena,Tahoma;}`
223223
);
224224
}
225225
collageTextDOM.removeClass((index, classes) =>
226226
classes
227227
.split(' ')
228-
.filter((cName) => cName.startsWith('customfont'))
228+
.filter((cName) => cName.startsWith('font-'))
229229
.join(' ')
230230
);
231231
collageTextDOM.addClass(c_text_font);

src/Utility/AdminInput.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ public static function renderFontSelect(array $setting, string $label): string
347347
$languageService = LanguageService::getInstance();
348348
$fonts = '';
349349

350+
$attributes = '';
351+
if (isset($setting['attributes'])) {
352+
foreach ($setting['attributes'] as $key => $prop) {
353+
$attributes .= $key . '="' . $prop . '" ';
354+
}
355+
}
356+
350357
if (isset($setting['paths']) && is_array($setting['paths'])) {
351358
$pathIndex = 0;
352359
foreach ($setting['paths'] as $path) {
@@ -355,6 +362,7 @@ public static function renderFontSelect(array $setting, string $label): string
355362
<h2 class="font-bold">' . PathUtility::getPublicPath($path) . '</h2>
356363
</div>
357364
';
365+
$fontClassName = 'font-' . $pathIndex;
358366
try {
359367
$files = FontUtility::getFontsFromPath($path, false);
360368
$files = array_map(fn ($file): string => PathUtility::getPublicPath($file), $files);
@@ -365,14 +373,18 @@ public static function renderFontSelect(array $setting, string $label): string
365373
</div>
366374
';
367375
}
368-
foreach ($files as $file) {
376+
$fontIndex = 0;
377+
foreach ($files as $name => $path) {
378+
$fontClassName .= '-' . $fontIndex;
369379
$imageAttributes = [
370-
'onClick' => 'adminFontSelect(this, "' . $setting['name'] . '");',
371-
'data-origin' => $file,
372-
'title' => $file,
373-
'class' => 'w-full h-full left-0 top-0 absolute object-contain cursor-pointer'
380+
'onClick' => 'adminFontSelect(this, "' . $setting['name'] . '", "' . $fontClassName . '");',
381+
'data-origin' => $path,
382+
'title' => $name,
383+
'class' => 'w-full h-full left-0 top-0 absolute object-contain cursor-pointer',
374384
];
375-
$fonts .= '<div class="w-full relative h-0 pb-2/3">' . FontUtility::getFontPreviewImage(fontPath: $file, attributes: $imageAttributes) . '</div>';
385+
$fonts .= '<style>.' . $fontClassName . ' {font-family:"' . $name . '"}</style>';
386+
$fonts .= '<div class="w-full relative h-0 pb-2/3">' . FontUtility::getFontPreviewImage(fontPath: $path, attributes: $imageAttributes) . '</div>';
387+
$fontIndex++;
376388
}
377389
} catch (\Exception $e) {
378390
$fonts .= '
@@ -428,6 +440,7 @@ public static function renderFontSelect(array $setting, string $label): string
428440
class="w-full h-10 border-2 border-solid border-gray-300 focus:border-brand-1 rounded-md px-3 mt-auto"
429441
name="' . $setting['name'] . '"
430442
value="' . $setting['value'] . '"
443+
' . $attributes . '
431444
/>
432445
</div>
433446
';

0 commit comments

Comments
 (0)