Files

51 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
final class ReadMoreExtension extends Minz_Extension {
public const DEFAULT_HEIGHT = 250;
public const MIN_HEIGHT = 100;
public const MAX_HEIGHT = 2000;
#[\Override]
public function init(): void {
$this->registerTranslates();
if (!FreshRSS_Context::hasUserConf()) {
return;
}
Minz_View::appendStyle($this->getFileUrl('read-more.css'));
Minz_View::appendScript($this->getFileUrl('read-more.js'));
$this->registerHook(Minz_HookType::JsVars, $this->jsVars(...));
}
public static function clampHeight(int $height): int {
return max(self::MIN_HEIGHT, min(self::MAX_HEIGHT, $height));
}
public function getCollapsedHeight(): int {
return self::clampHeight($this->getUserConfigurationInt('collapsed_height') ?? self::DEFAULT_HEIGHT);
}
/**
* Pass parameters to read-more.js via `window.context.extensions`.
* @param array<mixed> $vars
* @return array<mixed>
*/
public function jsVars(array $vars): array {
$vars['read_more'] = [
'collapsed_height' => $this->getCollapsedHeight(),
'label' => _t('ext.read_more.button'),
];
return $vars;
}
#[\Override]
public function handleConfigureAction(): void {
$this->registerTranslates();
if (Minz_Request::isPost()) {
$height = Minz_Request::paramInt('collapsed_height') ?: self::DEFAULT_HEIGHT;
$this->setUserConfigurationValue('collapsed_height', self::clampHeight($height));
}
}
}