Skip to content

Commit a956bbe

Browse files
committed
Add migration
1 parent db3a561 commit a956bbe

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

api/lib/src/converter/note.dart

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,65 @@ NoteData _migrate(NoteData noteData, FileMetadata metadata) {
121121
kInfoArchiveFile, utf8.encode(json.encode(infoData)));
122122
}
123123
}
124+
if (version < 12) {
125+
Map? resolvePackAssetLocation(Map location, String kind) {
126+
final pack = location['pack'] as String?;
127+
final name = location['name'] as String?;
128+
if ((pack?.isEmpty ?? true) || (name?.isEmpty ?? true)) {
129+
return null;
130+
}
131+
final packData = noteData.getAsset('$kPacksArchiveDirectory/$pack.bfly');
132+
if (packData == null) return null;
133+
final data = NoteData.fromData(packData);
134+
final item = data.getAsset('$kind/$name');
135+
if (item == null) return null;
136+
return {
137+
'name': name,
138+
'item': item,
139+
};
140+
}
141+
142+
void updatePenProperty(Map property) {
143+
if (property['fill'] != true) return;
144+
property['fill'] = property['color'];
145+
property['color'] = null;
146+
}
147+
148+
for (final page in noteData.getAssets('$kPagesArchiveDirectory/')) {
149+
final data = noteData.getAsset('$kPagesArchiveDirectory/$page');
150+
if (data == null) continue;
151+
final pageData = json.decode(utf8.decode(data)) as Map<String, dynamic>;
152+
final layers = pageData['layers'] as List?;
153+
for (final layer in layers ?? []) {
154+
final content = layer['content'] as List?;
155+
for (final item in content ?? []) {
156+
if ((const ['text', 'markdown'].contains(item['type']))) {
157+
item['stylesheet'] = resolvePackAssetLocation(
158+
item['stylesheet'], kStylesArchiveDirectory);
159+
} else if (item['type'] == 'pen') {
160+
updatePenProperty(item['property']);
161+
}
162+
}
163+
}
164+
}
165+
final info = noteData.getAsset(kInfoArchiveFile);
166+
if (info != null) {
167+
final infoData = json.decode(utf8.decode(info)) as Map<String, dynamic>;
168+
final tools = infoData['tools'] as List?;
169+
for (final item in tools ?? []) {
170+
if ((const ['text', 'markdown'].contains(item['type']))) {
171+
item['stylesheet'] = resolvePackAssetLocation(
172+
item['stylesheet'], kStylesArchiveDirectory);
173+
} else if (item['type'] == 'stamp') {
174+
item['component'] = resolvePackAssetLocation(
175+
item['component'], kComponentsArchiveDirectory);
176+
} else if (item['type'] == 'pen') {
177+
updatePenProperty(item['property']);
178+
}
179+
}
180+
noteData = noteData.setAsset(
181+
kInfoArchiveFile, utf8.encode(json.encode(infoData)));
182+
}
183+
}
124184
return noteData;
125185
}

api/lib/src/models/meta.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import '../converter/core.dart';
55
part 'meta.freezed.dart';
66
part 'meta.g.dart';
77

8-
const kFileVersion = 11;
8+
const kFileVersion = 12;
99
const kBreakingChangesVersion = 7;
1010

1111
@freezed

0 commit comments

Comments
 (0)