This commit is contained in:
Developer
2026-04-18 05:20:42 +08:00
parent 20fb06cac9
commit e347b3ca73
74 changed files with 11893 additions and 663 deletions

View File

@@ -166,4 +166,57 @@ class CookingNoteController extends GetxController {
List<CookingNoteModel> getNotesByTag(String tag) {
return _notes.where((note) => note.tags.contains(tag)).toList();
}
String exportToJson() {
final data = _notes.map((e) => e.toJson()).toList();
return const JsonEncoder.withIndent(' ').convert(data);
}
String exportToCsv() {
final buffer = StringBuffer();
buffer.writeln('ID,菜谱ID,标题,内容,标签,创建时间');
for (final note in _notes) {
buffer.writeln(
[
note.id,
note.recipeId,
'"${(note.title ?? '').replaceAll('"', '""')}"',
'"${note.content.replaceAll('"', '""').replaceAll('\n', ' ')}"',
'"${note.tags.join('; ')}"',
note.createdAt,
].join(','),
);
}
return buffer.toString();
}
String exportToMarkdown() {
final buffer = StringBuffer();
buffer.writeln('# 📝 烹饪笔记');
buffer.writeln();
for (final note in _notes) {
buffer.writeln('## ${note.displayTitle}');
if (note.hasTags) {
buffer.writeln('标签: ${note.tags.map((t) => '`$t`').join(' ')}');
}
buffer.writeln();
buffer.writeln(note.content);
buffer.writeln();
buffer.writeln('*${note.displayDate}*');
buffer.writeln('---');
buffer.writeln();
}
return buffer.toString();
}
void importFromJson(Map<String, dynamic> json) {
try {
final note = CookingNoteModel.fromJson(json);
if (!_notes.any((n) => n.id == note.id)) {
addNote(note);
}
} catch (e) {
debugPrint('CookingNoteController: importFromJson failed: $e');
}
}
}