simplified code across the app and consolidated duplicated helpers
This commit is contained in:
@@ -123,19 +123,14 @@ class PmImageView extends StatelessWidget {
|
||||
|
||||
double? _resolveWidth(String? width, double available) {
|
||||
if (width == null) return null;
|
||||
final match = RegExp(r'^(\d+(?:\.\d+)?)(px|%|em|rem)$').firstMatch(width);
|
||||
if (match == null) return null;
|
||||
final value = double.parse(match.group(1)!);
|
||||
final double resolved;
|
||||
switch (match.group(2)) {
|
||||
case '%':
|
||||
resolved = available * value / 100;
|
||||
case 'em':
|
||||
case 'rem':
|
||||
resolved = value * 16;
|
||||
default:
|
||||
resolved = value;
|
||||
}
|
||||
final length = parsePmCssLength(width);
|
||||
if (length == null) return null;
|
||||
final value = length.value;
|
||||
final resolved = switch (length.unit) {
|
||||
'%' => available * value / 100,
|
||||
'em' || 'rem' => value * 16,
|
||||
_ => value,
|
||||
};
|
||||
return resolved.clamp(1, available);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -325,3 +325,12 @@ class PmUnknown extends PmNode {
|
||||
|
||||
const PmUnknown({required this.rawType, this.children = const []});
|
||||
}
|
||||
|
||||
final RegExp _cssLength = RegExp(r'^(\d+(?:\.\d+)?)(px|%|em|rem)$');
|
||||
|
||||
/// Splits a CSS length like `12px` / `1.5em` / `50%` into value and unit.
|
||||
({double value, String unit})? parsePmCssLength(String raw) {
|
||||
final match = _cssLength.firstMatch(raw);
|
||||
if (match == null) return null;
|
||||
return (value: double.parse(match.group(1)!), unit: match.group(2)!);
|
||||
}
|
||||
|
||||
@@ -152,22 +152,15 @@ class _PmRichTextState extends State<PmRichText> {
|
||||
|
||||
double? _fontSize(dynamic raw, double base) {
|
||||
if (raw is! String) return null;
|
||||
final match = RegExp(r'^(\d+(?:\.\d+)?)(px|rem|em|%)$').firstMatch(raw);
|
||||
if (match == null) return null;
|
||||
final value = double.parse(match.group(1)!);
|
||||
final double size;
|
||||
switch (match.group(2)) {
|
||||
case 'px':
|
||||
size = value;
|
||||
case 'rem':
|
||||
size = value * 16;
|
||||
case 'em':
|
||||
size = base * value;
|
||||
case '%':
|
||||
size = base * value / 100;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
final length = parsePmCssLength(raw);
|
||||
if (length == null) return null;
|
||||
final value = length.value;
|
||||
final size = switch (length.unit) {
|
||||
'px' => value,
|
||||
'rem' => value * 16,
|
||||
'em' => base * value,
|
||||
_ => base * value / 100,
|
||||
};
|
||||
return size.clamp(8, 72);
|
||||
}
|
||||
|
||||
|
||||
@@ -195,7 +195,7 @@ class _PmTableViewState extends State<PmTableView> {
|
||||
required bool left,
|
||||
required bool visible,
|
||||
}) {
|
||||
final dark = Theme.of(context).brightness == Brightness.dark;
|
||||
final dark = AppTheme.isDarkMode(context);
|
||||
final color = Colors.black.withValues(alpha: dark ? 0.28 : 0.12);
|
||||
return Positioned(
|
||||
top: 0,
|
||||
|
||||
Reference in New Issue
Block a user