Skip to content

Commit

Permalink
feat(bookmark-group): 新增支持上下文改变书签分组命令, 同时优化视图
Browse files Browse the repository at this point in the history
  • Loading branch information
czfadmin committed Apr 5, 2024
1 parent 1117426 commit 493efe5
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 183 deletions.
3 changes: 2 additions & 1 deletion l10n/bundle.l10n.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"Please select bookmark color": "Please select bookmark color",
"Select a bookmark to jump to the corresponding location.": "Select a bookmark to jump to the corresponding location.",
"Please select the bookmark you want to open": "Please select the bookmark you want to open",
"Group name already exists": "Group name already exists",
"Change bookmark grouping": "Change bookmark grouping",
"Default Group": "Default Group",
"Group name already exists": "Group name already exists",
"'showGutterIcon', 'showGutterInOverviewRuler', 'showTextDecoration' not available at the same time this is only 'false'": "'showGutterIcon', 'showGutterInOverviewRuler', 'showTextDecoration' not available at the same time this is only 'false'",
"Jump to bookmark position": "Jump to bookmark position",
"`bookmark-manager.json` will be tracked by the version tool. Please try to avoid submitting this file to the source code repository. You can manually add it to `.gitignore` or automatically complete this step by turning on the `alwaysIgnore` option .": "`bookmark-manager.json` will be tracked by the version tool. Please try to avoid submitting this file to the source code repository. You can manually add it to `.gitignore` or automatically complete this step by turning on the `alwaysIgnore` option .",
Expand Down
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@
"title": "%bookmark-manager.setAsDefaultActivedGroup%",
"icon": "$(clear-all)",
"category": "Bookmark Manager (BM)"
},
{
"command": "bookmark-manager.changeBookmarkGroup",
"title": "%bookmark-manager.changeBookmarkGroup%",
"icon": "$(bookmark)",
"category": "Bookmark Manager (BM)"
}
],
"views": {
Expand Down Expand Up @@ -448,6 +454,10 @@
"command": "bookmark-manager.toggleBookmarkWithSelection",
"group": "bookmarksActionsGroup@3"
},
{
"command": "bookmark-manager.changeBookmarkGroup",
"group": "bookmarksAGroupActionGroup@1"
},
{
"command": "bookmark-manager.editLabel",
"group": "bookmarksEditActionGroup@1"
Expand Down Expand Up @@ -548,6 +558,11 @@
"when": "view==bookmark-manager && viewItem === bookmark",
"group": "inline@3"
},
{
"command": "bookmark-manager.changeBookmarkGroup",
"when": "view==bookmark-manager && viewItem === bookmark",
"group": "bookmarksGroupActionGroup@1"
},
{
"command": "bookmark-manager.clearAllBookmarksInCurrentFile",
"when": "view == bookmark-manager && (viewItem === file || viewItem === workspace)",
Expand Down
3 changes: 2 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@
"bookmark-manager.addBookmarkGroup": "Add a bookmark group",
"bookmark-manager.groupedByCustom": "Grouped by custom",
"bookmark-manager.clearAllBookmarksInColor": "Clear bookmarks in this color",
"bookmark-manager.setAsDefaultActivedGroup": "Set as default activation group"
"bookmark-manager.setAsDefaultActivedGroup": "Set as default activation group",
"bookmark-manager.changeBookmarkGroup": "Change group"
}
3 changes: 2 additions & 1 deletion package.nls.zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@
"bookmark-manager.addBookmarkGroup": "新增书签分组",
"bookmark-manager.groupedByCustom": "自定义分组",
"bookmark-manager.clearAllBookmarksInColor": "清除此颜色下的所有书签",
"bookmark-manager.setAsDefaultActivedGroup": "设置为默认激活组"
"bookmark-manager.setAsDefaultActivedGroup": "设置为默认激活组",
"bookmark-manager.changeBookmarkGroup": "改变书签分组"
}
17 changes: 17 additions & 0 deletions src/commands/bookmark/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
getLineInfoStrFromBookmark,
gotoSourceLocation as gotoSourceLocationUtil,
quicklyJumpToBookmark,
showGroupPickItems,
toggleBookmark,
toggleBookmarksWithSelections,
} from '../../utils';
Expand Down Expand Up @@ -398,3 +399,19 @@ export function clearAllBookmarksInColor(args: any) {
const meta = args.meta as BookmarksGroupedByColorType;
controller.clearAllBookmarksInColor(meta.color);
}

/**
* 更改书签分组
* @param args
*/
export async function changeBookmarkGroup(args: any) {
const bookmark = getBookmarkFromCtx(args);
if (!bookmark) {
return;
}
const newGroup = await showGroupPickItems(true, bookmark.groupId);
if (!newGroup) {
return;
}
bookmark.changeGroupId(newGroup.id);
}
151 changes: 46 additions & 105 deletions src/controllers/BookmarksController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,42 @@ export default class BookmarksController implements IController {

private _store!: IBookmarksStore;

private _groupedByFile: BookmarksGroupedByFileWithSortType[] = [];
// private _groupedByFile: BookmarksGroupedByFileWithSortType[] = [];

private _groupedByColor: BookmarksGroupedByColorType[] = [];
// private _groupedByColor: BookmarksGroupedByColorType[] = [];

private _groupedByWorkspaceFolders: BookmarksGroupedByWorkspaceType[] = [];
// private _groupedByWorkspaceFolders: BookmarksGroupedByWorkspaceType[] = [];

public viewType: TreeViewType = 'tree';
public get viewType(): TreeViewType {
return this._store.viewType as TreeViewType;
}

public groupView: TreeViewGroupType = 'default';
public get groupView(): TreeViewGroupType {
return this._store.groupView as TreeViewGroupType;
}

public sortedType: TreeViewSortedType = 'linenumber';
public get sortedType(): TreeViewSortedType {
return this._store.sortedType as TreeViewSortedType;
}

public get groupedBookmarks():
| BookmarksGroupedByFileWithSortType[]
| BookmarksGroupedByCustomType[]
| BookmarksGroupedByColorType[]
| BookmarksGroupedByWorkspaceType[] {
switch (this._store.groupView) {
case 'file':
case 'default':
return this._store.bookmarksGroupedByFile;
case 'color':
return this._store.bookmarksGroupedByColor;
case 'workspace':
return this._store.bookmarksGroupedByWorkspace;
case 'custom':
return this._store.getBookmarksGroupedByCustom;
}
return [];
}
private _disposables: IDisposable[] = [];

private _watcher: FileSystemWatcher | undefined;
Expand All @@ -89,25 +113,6 @@ export default class BookmarksController implements IController {

public onDidChangeEvent: Event<void> = this._onDidChangeEvent.event;

public get groupedByFileBookmarks(): BookmarksGroupedByFileWithSortType[] {
return this._groupedByFile;
}

public get groupedByColorBookmarks(): BookmarksGroupedByColorType[] {
return this._groupedByColor;
}

public get groupedByWorkspaceFolders(): BookmarksGroupedByWorkspaceType[] {
return this._groupedByWorkspaceFolders;
}

public get groupedByCustomBookmarks(): BookmarksGroupedByCustomType[] {
if (!this.store) {
return [];
}
return this.store.getBookmarksGroupedByCustom;
}

public get workspaceState(): Memento {
return this._context.workspaceState;
}
Expand Down Expand Up @@ -176,10 +181,6 @@ export default class BookmarksController implements IController {
private async _initStore() {
this._store = BookmarksStore.create();

this.viewType = this._store.viewType as TreeViewType;
this.groupView = this._store.groupView as TreeViewGroupType;
this.sortedType = this._store.sortedType as TreeViewSortedType;

if (
(!workspace.workspaceFolders || workspace.workspaceFolders!.length < 2) &&
this.groupView !== 'default'
Expand All @@ -203,14 +204,10 @@ export default class BookmarksController implements IController {
}
}

// 监听mst的store的快照, 当快照发生变化时, 将数据保存到存储文件中
this._storeDisposer = onSnapshot(this._store, snapshot => {
// 更新对应的配置数据
this.viewType = this._store.viewType as TreeViewType;
this.groupView = this._store.groupView as TreeViewGroupType;
this.sortedType = this._store.sortedType as TreeViewSortedType;
this.save();
});
this._changeView();
}

/**
Expand All @@ -232,14 +229,10 @@ export default class BookmarksController implements IController {
this._watcher.onDidDelete(uri => {
this._store.clearAll();
});
this._watcher.onDidChange(uri => {
// if (this._needWatchFiles) {
// this._resolveDatastoreFromStoreFile();
// this._changeView();
// this.refresh();
// }
// this._needWatchFiles = true;
});
// this._watcher.onDidChange(uri => {
// this._resolveDatastoreFromStoreFile();
// this.refresh();
// });
this._disposables.push(this._watcher);
}
}
Expand All @@ -264,63 +257,19 @@ export default class BookmarksController implements IController {
content: [],
bookmarks: [],
groups: [],
viewType: 'tree',
groupView: 'default',
sortedType: 'linenumber',
});
}
const storeContent = JSON.parse(content) as IBookmarkStoreInfo;

const _bookmarks = storeContent.content || storeContent.bookmarks || [];

this._store.addGroups(storeContent.groups || []);

if (_bookmarks && _bookmarks.length) {
this._store.addBookmarks(_bookmarks);
}
this._store.updateStore(storeContent);
}
}
this._needWarning = true;
}

/**
* 当viewType改变的时候, 转换成对应的格式
*/
private _changeView() {
if (this.viewType === 'tree') {
if (this.groupView === 'color') {
this._groupedByColor = this._getBookmarksGroupedByColor();
}
if (this.groupView === 'default') {
this._groupedByFile = this._getBookmarksGroupedByFile();
}
if (this.groupView === 'workspace') {
this._groupedByWorkspaceFolders =
this._getBookmarksGroupedByWorkspace();
}
}
}

/**
* 获取按照书签颜色设置的书签列表
* @returns
*/
private _getBookmarksGroupedByColor() {
if (!this._store) {
return [];
}
return this._store.bookmarksGroupedByColor;
}

/**
* 获取按照工作区间分组的书签列表
* @returns
*/
private _getBookmarksGroupedByWorkspace() {
if (!this._store) {
return [];
}

return this._store.bookmarksGroupedByWorkspace;
}

add(bookmark: Partial<Omit<IBookmark, 'id'>>) {
const activedGroup = this._store.activedGroup;

Expand Down Expand Up @@ -373,9 +322,6 @@ export default class BookmarksController implements IController {
}

getBookmarkStoreByFileUri(fileUri: Uri): IBookmark[] {
if (!this._store) {
return [];
}
return this._store.getBookmarksByFileUri(fileUri);
}

Expand All @@ -400,9 +346,6 @@ export default class BookmarksController implements IController {
* @returns
*/
clearAllBookmarkInFile(fileUri: Uri) {
if (!this._store.bookmarks.length) {
return;
}
this._store.clearBookmarksByFile(fileUri);
}

Expand All @@ -412,9 +355,6 @@ export default class BookmarksController implements IController {
* @returns
*/
clearAllBookmarksInColor(color: string) {
if (!this._store.bookmarks.length) {
return;
}
this._store.clearBookmarksByColor(color);
}

Expand All @@ -424,8 +364,7 @@ export default class BookmarksController implements IController {
} else {
this.workspaceState.update(EXTENSION_ID, this._store);
}
this._changeView();
this._fire();
this.refresh();
}

refresh() {
Expand All @@ -434,18 +373,17 @@ export default class BookmarksController implements IController {

changeSortType(sortType: TreeViewSortedType): void {
this._store.updateSortedType(sortType);
this.refresh();
}

changeViewType(viewType: TreeViewType) {
this._store.udpateViewType(viewType);
this._changeView();
this._fire();
this.refresh();
}

changeGroupView(groupType: TreeViewGroupType) {
this._store.updateGroupView(groupType);
this._changeView();
this._fire();
this.refresh();
}

addBookmarkGroup(groupName: string, color: string = DEFAULT_BOOKMARK_COLOR) {
Expand Down Expand Up @@ -521,6 +459,9 @@ export default class BookmarksController implements IController {
version: process.env.version!,
workspace: workspace.name,
updatedDate: new Date().toLocaleDateString(),
viewType: this._store.viewType,
groupView: this._store.groupView,
sortedType: this._store.sortedType,
bookmarks: this._store.bookmarks.filter(
it => it.wsFolder?.uri.fsPath === workspace.uri.fsPath,
),
Expand Down
Loading

0 comments on commit 493efe5

Please sign in to comment.