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 4d469eb commit 1117426
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 10 deletions.
1 change: 1 addition & 0 deletions TODO-ZN.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [x] 删除分组
- [x] 新建分组,编辑分组
- [x] 清除分组的内的书签
- [x] 新增激活默认组状态
- [x] 支持自定义分组拖拽
- [x] 分组拖拽排序
- [x] 分组内书签拖拽调整书签分组
Expand Down
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Use the extended function of VSCODE to develop and manage users' bookmarks, allo
- [x] Delete group
- [x] Create new group, edit group
- [x] Clear bookmarks within a group
- [x] Activate default group status
- [x] Support custom group drag and drop
- [x] Group drag and drop sorting
- [x] Drag and drop bookmarks within a group to adjust bookmark groups
Expand Down
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@
"title": "%bookmark-manager.clearAllBookmarksInColor%",
"icon": "$(clear-all)",
"category": "Bookmark Manager (BM)"
},
{
"command": "bookmark-manager.setAsDefaultActivedGroup",
"title": "%bookmark-manager.setAsDefaultActivedGroup%",
"icon": "$(clear-all)",
"category": "Bookmark Manager (BM)"
}
],
"views": {
Expand Down Expand Up @@ -603,9 +609,14 @@
"group": "itemContextGroup@2"
},
{
"command": "bookmark-manager.deleteBookmarkGroup",
"command": "bookmark-manager.setAsDefaultActivedGroup",
"when": "view == bookmark-manager && viewItem === custom",
"group": "itemContextGroup@1"
},
{
"command": "bookmark-manager.deleteBookmarkGroup",
"when": "view == bookmark-manager && viewItem === custom",
"group": "itemContextGroup@2"
}
],
"commandPalette": [
Expand Down
6 changes: 4 additions & 2 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@
"bookmark-manager.autoSwitchSingleToMultiWithLineWrap": "Automatically switches a single-line bookmark to a multi-line bookmark when wrapping",
"bookmark-manager.addBookmark": "Add new bookmark",
"bookmark-manager.changeBookmarkGroupLabel": "Change group label",
"bookmark-manager.clearAllBookmarksInGroup": "clear bookmarks in this group",
"bookmark-manager.clearAllBookmarksInGroup": "Clear bookmarks in this group",
"bookmark-manager.changeBookmarkGroupColor": "Change group color",
"bookmark-manager.deleteBookmarkGroup": "Delete group",
"bookmark-manager.addBookmarkGroup": "Add a bookmark group",
"bookmark-manager.groupedByCustom": "Grouped by custom"
"bookmark-manager.groupedByCustom": "Grouped by custom",
"bookmark-manager.clearAllBookmarksInColor": "Clear bookmarks in this color",
"bookmark-manager.setAsDefaultActivedGroup": "Set as default activation group"
}
4 changes: 3 additions & 1 deletion package.nls.zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,7 @@
"bookmark-manager.changeBookmarkGroupColor": "改变分组颜色",
"bookmark-manager.deleteBookmarkGroup": "删除分组",
"bookmark-manager.addBookmarkGroup": "新增书签分组",
"bookmark-manager.groupedByCustom": "自定义分组"
"bookmark-manager.groupedByCustom": "自定义分组",
"bookmark-manager.clearAllBookmarksInColor": "清除此颜色下的所有书签",
"bookmark-manager.setAsDefaultActivedGroup": "设置为默认激活组"
}
18 changes: 18 additions & 0 deletions src/commands/bookmark/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,21 @@ export const BookmarkGroupCommands: IBookmarkCommand[] = [
},
},
];

/**
* 设置默认激活组, 此时可以将默认创建的书签放到此分组中
* @param args
*/
export async function setAsDefaultActivedGroup(args: any) {
let meta: IBookmarkGroup | undefined;
if (!args || !args.meta) {
meta = await showGroupPickItems(false);
if (!meta) {
return;
}
} else {
meta = (args.meta as BookmarksGroupedByCustomType).group!;
}
const controller = resolveBookmarkController();
controller.setAsDefaultActivedGroup(meta);
}
4 changes: 4 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ function log(name: string) {

/**
* 注册所需要的代码相关命令
* 注意写法, 默认从对应的模块中导入, 然后注册自定义的命令以及回调,
* 不要额外的进行使用`register`去注册,同意使用此方法注册命令
* @param context
*
*/
function registerCodeCommands(context: ExtensionContext) {
import('./bookmark')
Expand Down
28 changes: 22 additions & 6 deletions src/controllers/BookmarksController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,13 @@ export default class BookmarksController implements IController {
}

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

const newBookmark = this._store.createBookmark({
...bookmark,
groupId: activedGroup?.id,
});

this._store.add(newBookmark);
}

Expand Down Expand Up @@ -458,6 +464,20 @@ export default class BookmarksController implements IController {
this._store.clearAllBookmarksInGroup(groupId);
}

/**
* 设置分组的激活状态
* @param meta 待激活的分组
*/
setAsDefaultActivedGroup(meta: IBookmarkGroup) {
const prevActivedGroup = this._store.activedGroup;
if (prevActivedGroup && prevActivedGroup.id === meta.id) {
return;
}

prevActivedGroup?.setActiveStatus(false);

meta.setActiveStatus(true);
}
/**
* 将数据写入到`.vscode/bookmark.json`中
* @returns
Expand Down Expand Up @@ -504,11 +524,7 @@ export default class BookmarksController implements IController {
bookmarks: this._store.bookmarks.filter(
it => it.wsFolder?.uri.fsPath === workspace.uri.fsPath,
),
groups: this._store.groups.map(group => ({
id: group.id,
label: group.label,
sortedIndex: group.sortedIndex,
})),
groups: this._store.groups,
};
return JSON.stringify(storeInfo);
}
Expand Down
4 changes: 4 additions & 0 deletions src/providers/BookmarksTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ export default class BookmarkTreeItem extends BaseTreeItem {
this.description = workspace.asRelativePath(_meta.fileId);
} else if (this.contextValue === 'custom') {
this.label = label;
const meta = this.meta as BookmarksGroupedByCustomType;
this.iconPath = ThemeIcon.Folder;
if (meta && meta.group.activeStatus) {
this.iconPath = new ThemeIcon('folder-active');
}
} else {
this.command = {
title: l10n.t('Jump to bookmark position'),
Expand Down
21 changes: 21 additions & 0 deletions src/stores/bookmark-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,28 @@ import {DEFAULT_BOOKMARK_GROUP_COLOR} from '../constants';

export const BookmarkGroup = types
.model('BookmarkGroup', {
/**
* 书签ID
*/
id: types.string,
/**
* 书签标签
*/
label: types.string,
/**
* 书签在视图中排序索引
*/
sortedIndex: types.optional(types.number, 0),
/**
* 书签颜色
*/
color: types.optional(types.string, DEFAULT_BOOKMARK_GROUP_COLOR),
/**
* 当前组是否为默认激活的分组
* - 默认情况下,默认分组为激活状态,其他未非激活状态
* - 设置为true 的话,默认创建的书签将会放置到激活的分组中
*/
activeStatus: types.optional(types.boolean, false),
})
.actions(self => {
return {
Expand All @@ -19,6 +37,9 @@ export const BookmarkGroup = types
changeColor(color: string) {
self.color = color;
},
setActiveStatus(isActived: boolean) {
self.activeStatus = isActived;
},
};
});

Expand Down
8 changes: 8 additions & 0 deletions src/stores/bookmark-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ export const BookmarksStore = types
get colors() {
return self.bookmarks.map(it => it.color || it.customColor.name);
},
get activedGroup() {
return (
self.groups.find(it => it.activeStatus === true) ||
self.groups.find(it => it.id === DEFAULT_BOOKMARK_GROUP_ID)
);
},
};
})
.actions(self => {
Expand Down Expand Up @@ -305,6 +311,7 @@ export const BookmarksStore = types
label,
sortedIndex: self.groups.length,
color,
activeStatus: false,
}),
);
},
Expand All @@ -325,6 +332,7 @@ export const BookmarksStore = types
id: DEFAULT_BOOKMARK_GROUP_ID,
label: l10n.t('Default Group'),
sortedIndex: 0,
activeStatus: true,
}),
);
}
Expand Down

0 comments on commit 1117426

Please sign in to comment.