Skip to content

Commit 3a7623c

Browse files
committed
fix(migrate): 修复版本升级后数据旧版本保存数据丢失问题
1 parent fde3ba9 commit 3a7623c

File tree

6 files changed

+35
-32
lines changed

6 files changed

+35
-32
lines changed

src/commands/bookmark/base.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
QuickPickItem,
66
workspace,
77
Uri,
8-
TextEditorRevealType,
98
commands,
109
Position,
1110
} from 'vscode';
@@ -34,7 +33,6 @@ import {
3433
getBookmarkColorFromCtx,
3534
toggleBookmarksWithSelections,
3635
highlightSelection,
37-
openDocumentAndGotoLocation,
3836
} from '../../utils';
3937
import {range} from 'lodash';
4038

src/commands/bookmark/gutter.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/controllers/BookmarksController.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,6 @@ export default class BookmarksController implements IController {
163163
private async _initial() {
164164
this._needWarning = this.configService.getGlobalValue('_needWarning', true);
165165

166-
// this.configService.onExtensionConfigChange(configuration => {
167-
// this._configuration = configuration;
168-
// });
169-
170166
this.configService.onDidChangeConfiguration(ev => {
171167
if (!ev.affectsConfiguration(`${EXTENSION_ID}.createJsonFile`)) {
172168
return;
@@ -203,7 +199,7 @@ export default class BookmarksController implements IController {
203199
store = this._store;
204200
}
205201

206-
applySnapshot(this._store, isProxy(store) ? getSnapshot(store) : store);
202+
applySnapshot(this._store, isProxy(store) ? getSnapshot(store) : this._sm.migrateService.migrate(store));
207203

208204
if (!this._store.groups.length) {
209205
this._store.addGroups([]);
@@ -611,7 +607,9 @@ export default class BookmarksController implements IController {
611607
});
612608
}
613609

614-
const storeContent = JSON.parse(content) as IBookmarkStoreInfo;
610+
let storeContent = JSON.parse(content) as IBookmarkStoreInfo;
611+
612+
storeContent = this._sm.migrateService.migrate(storeContent);
615613

616614
applySnapshot(this._store, {
617615
...storeContent,

src/services/MigrateService.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {IBookmarkStoreInfo} from '../types';
12
import {BaseService} from './BaseService';
23
import {ServiceManager} from './ServiceManager';
34

@@ -8,6 +9,29 @@ export class MigrateService extends BaseService {
89
constructor(sm: ServiceManager) {
910
super(MigrateService.name, sm);
1011
}
12+
13+
/**
14+
* @zh 对之前版本的书签保存的内容进行迁移工作
15+
* @param data
16+
* @returns
17+
*/
18+
migrate(data: IBookmarkStoreInfo) {
19+
if (!data.version || data.version <= '0.0.36') {
20+
data.bookmarks = data.bookmarks.map(it => {
21+
return {
22+
...it,
23+
sortedInfo: {
24+
...it.sortedInfo,
25+
icon: 0,
26+
},
27+
};
28+
});
29+
if (!data.version) {
30+
data.version = process.env.version!
31+
}
32+
}
33+
return data;
34+
}
1135
initial(): void {}
1236
dispose(): void {}
1337
}

src/services/ServiceManager.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {createGlobalStore, GlobalStore} from '../stores';
1111
import {IconsService} from './IconsService';
1212
import ColorsService from './ColorsService';
1313
import {Instance} from 'mobx-state-tree';
14+
import { MigrateService } from './MigrateService';
1415

1516
export interface IServiceManager {
1617
readonly configService: ConfigService;
@@ -22,6 +23,8 @@ export interface IServiceManager {
2223
readonly iconsService: IconsService;
2324
readonly colorsService: ColorsService;
2425
readonly fileService: FileService;
26+
27+
readonly migrateService: MigrateService
2528
}
2629

2730
export class ServiceManager implements IServiceManager, IDisposable {
@@ -34,6 +37,8 @@ export class ServiceManager implements IServiceManager, IDisposable {
3437
readonly iconsService: IconsService;
3538

3639
readonly colorsService: ColorsService;
40+
41+
readonly migrateService: MigrateService
3742
private _statusbarService: StatusbarService | undefined;
3843

3944
public readonly fileService: FileService;
@@ -82,6 +87,7 @@ export class ServiceManager implements IServiceManager, IDisposable {
8287
this.configService = new ConfigService(this);
8388
this.colorsService = new ColorsService(this);
8489
this.gutterService = new GutterService(this);
90+
this.migrateService = new MigrateService(this)
8591
this.decorationService = new DecorationService(this);
8692
this.workspaceService = new WorkspaceService(this);
8793
this.gitService = new GitService(this);

src/stores/bookmark-store.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export const BookmarksStore = types
8585
groups: types.array(BookmarkGroup),
8686

8787
groupInfo: types.array(BookmarkGroupInfo),
88+
version: types.optional(types.string, process.env.version || ""),
8889
})
8990
.views(self => {
9091
return {

0 commit comments

Comments
 (0)