mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-28 09:47:35 +08:00
Note: Allow tags to be read and serialized
This is just exposing the tags on the Note class. Not making it available through any interface.
This commit is contained in:
@ -36,6 +36,7 @@ class Note with NotesNotifier {
|
||||
DateTime _modified;
|
||||
String _body = "";
|
||||
NoteType _type = NoteType.Unknown;
|
||||
List<String> _tags = [];
|
||||
|
||||
MdYamlDoc _data = MdYamlDoc();
|
||||
NoteSerializer noteSerializer = NoteSerializer();
|
||||
@ -124,6 +125,17 @@ class Note with NotesNotifier {
|
||||
_notifyModified();
|
||||
}
|
||||
|
||||
List<String> get tags {
|
||||
return _tags;
|
||||
}
|
||||
|
||||
set tags(List<String> tags) {
|
||||
if (!canHaveMetadata) return;
|
||||
|
||||
_tags = tags;
|
||||
_notifyModified();
|
||||
}
|
||||
|
||||
bool get canHaveMetadata {
|
||||
return Settings.instance.yamlHeaderEnabled;
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
import 'package:gitjournal/utils/datetime.dart';
|
||||
import 'package:gitjournal/settings.dart';
|
||||
import 'package:flutter_emoji/flutter_emoji.dart';
|
||||
import 'package:gitjournal/utils/logger.dart';
|
||||
import 'package:yaml/yaml.dart';
|
||||
|
||||
import 'md_yaml_doc.dart';
|
||||
import 'note.dart';
|
||||
@ -17,6 +19,7 @@ class NoteSerializationSettings {
|
||||
String createdKey = "created";
|
||||
String titleKey = "title";
|
||||
String typeKey = "type";
|
||||
String tagsKey = "tags";
|
||||
}
|
||||
|
||||
class NoteSerializer implements NoteSerializerInterface {
|
||||
@ -54,6 +57,12 @@ class NoteSerializer implements NoteSerializerInterface {
|
||||
data.props.remove(settings.typeKey);
|
||||
}
|
||||
|
||||
if (note.tags.isEmpty) {
|
||||
data.props.remove(settings.tagsKey);
|
||||
} else {
|
||||
data.props[settings.tagsKey] = note.tags;
|
||||
}
|
||||
|
||||
data.body = emojiParser.unemojify(note.body);
|
||||
}
|
||||
|
||||
@ -95,5 +104,14 @@ class NoteSerializer implements NoteSerializerInterface {
|
||||
note.type = NoteType.Unknown;
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
var tags = data.props[settings.tagsKey] as YamlList;
|
||||
if (tags != null) {
|
||||
note.tags = tags.map((t) => t.toString()).toList();
|
||||
}
|
||||
} catch (e) {
|
||||
Log.e("Note Decoding Failed: $e");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -71,6 +71,44 @@ title: Foo
|
||||
mod: 2019-12-02T04:00:00+00:00
|
||||
---
|
||||
|
||||
Hello""";
|
||||
|
||||
var actualContent = File(notePath).readAsStringSync();
|
||||
expect(actualContent, equals(expectedContent));
|
||||
});
|
||||
|
||||
test('Should read and write tags', () async {
|
||||
var content = """---
|
||||
title: Foo
|
||||
modified: 2017-02-15T22:41:19+01:00
|
||||
tags: [A, B]
|
||||
---
|
||||
|
||||
Hello""";
|
||||
|
||||
var notePath = p.join(tempDir.path, "note5.md");
|
||||
File(notePath).writeAsString(content);
|
||||
|
||||
var parentFolder = NotesFolderFS(null, tempDir.path);
|
||||
var note = Note(parentFolder, notePath);
|
||||
await note.load();
|
||||
|
||||
expect(note.tags[0], 'A');
|
||||
expect(note.tags[1], 'B');
|
||||
expect(note.tags.length, 2);
|
||||
|
||||
note.tags = [...note.tags]..add('C');
|
||||
note.tags.add('D');
|
||||
note.tags.remove('B');
|
||||
|
||||
await note.save();
|
||||
|
||||
var expectedContent = """---
|
||||
title: Foo
|
||||
modified: 2017-02-15T22:41:19+01:00
|
||||
tags: [A, C, D]
|
||||
---
|
||||
|
||||
Hello""";
|
||||
|
||||
var actualContent = File(notePath).readAsStringSync();
|
||||
|
Reference in New Issue
Block a user