AppFlowy/frontend/appflowy_flutter/lib/util/string_extension.dart
Lucas.Xu aa4fe2ba50
fix: color selection compatible with older versions (#5276)
* fix: color selection compatible with older versions

* chore: add a cover color function
2024-05-07 12:13:52 +08:00

49 lines
1.4 KiB
Dart

import 'dart:io';
import 'package:appflowy/shared/patterns/common_patterns.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flowy_infra/theme_extension.dart';
import 'package:flutter/material.dart';
extension StringExtension on String {
static const _specialCharacters = r'\/:*?"<>| ';
/// Encode a string to a file name.
///
/// Normalizes the string to remove special characters and replaces the "\/:*?"<>|" with underscores.
String toFileName() {
final buffer = StringBuffer();
for (final character in characters) {
if (_specialCharacters.contains(character)) {
buffer.write('_');
} else {
buffer.write(character);
}
}
return buffer.toString();
}
/// Returns the file size of the file at the given path.
///
/// Returns null if the file does not exist.
int? get fileSize {
final file = File(this);
if (file.existsSync()) {
return file.lengthSync();
}
return null;
}
/// Returns true if the string is a appflowy cloud url.
bool get isAppFlowyCloudUrl => appflowyCloudUrlRegex.hasMatch(this);
/// Returns the color of the string.
///
/// ONLY used for the cover.
Color? coverColor(BuildContext context) {
// try to parse the color from the tint id,
// if it fails, try to parse the color as a hex string
return FlowyTint.fromId(this)?.color(context) ?? tryToColor();
}
}