Skip to content

Commit

Permalink
feat(sort): 增加排序功能 (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
czfadmin committed May 7, 2024
1 parent c630e8a commit 97e3259
Show file tree
Hide file tree
Showing 19 changed files with 378 additions and 166 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,11 @@
"type": "boolean",
"default": false,
"description": "%bookmark-manager.autoSwitchSingleToMultiWithLineWrap%"
},
"bookmark-manager.ignoreFolders": {
"type": "array",
"default": [],
"description": "%bookmark-manager.ignoreFolders%"
}
}
},
Expand Down
3 changes: 2 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@
"bookmark-manager.changeBookmarkGroup": "Change group",
"bookmark-manager.listBookmarksInSelectedGroup": "List the bookmarks in the selected group",
"bookmark-manager.showWalkthroughs": "Show walkthroughs",
"bookmark-manager.changeSetting": "Change setting"
"bookmark-manager.changeSetting": "Change setting",
"bookmark-manager.ignoreFolders": "When this is set, the creation of the storage file `bookmark-manager.json` in the `.vscode` directory under the folder directory in this array will be automatically ignored."
}
3 changes: 2 additions & 1 deletion package.nls.zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@
"bookmark-manager.changeBookmarkGroup": "改变书签分组",
"bookmark-manager.listBookmarksInSelectedGroup": "列出已选择分组中的书签列表",
"bookmark-manager.showWalkthroughs": "显示交互指南",
"bookmark-manager.changeSetting": "更改设置"
"bookmark-manager.changeSetting": "更改设置",
"bookmark-manager.ignoreFolders": "当设置此项时, 将自动忽略在此数组中的文件夹目录下的`.vscode`目录中创建存储文件`bookmark-manager.json`"
}
14 changes: 9 additions & 5 deletions src/commands/bookmark/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
import {resolveBookmarkController} from '../../bootstrap';
import resolveServiceManager from '../../services/ServiceManager';
import {BookmarksGroupedByColorType, IBookmark} from '../../stores';
import {BookmarksGroupedByFileType, LineBookmarkContext} from '../../types';
import {
BookmarksGroupedByFileType,
BookmarkTypeEnum,
LineBookmarkContext,
} from '../../types';
import {
checkIfBookmarksIsInCurrentEditor,
chooseBookmarkColor,
Expand All @@ -30,7 +34,7 @@ import {
* 开启行书签, 使用默认颜色且无标签等相关信息
*/
export function toggleLineBookmark(args: LineBookmarkContext) {
toggleBookmark(args, {type: 'line'});
toggleBookmark(args, {type: BookmarkTypeEnum.LINE});
}

/**
Expand All @@ -50,7 +54,7 @@ export async function toggleLineBookmarkWithLabel(
}
toggleBookmark(context, {
label,
type: 'line',
type: BookmarkTypeEnum.LINE,
});
}

Expand All @@ -60,7 +64,7 @@ export async function toggleLineBookmarkWithLabel(
export function toggleLineBookmarkWithColor(context: LineBookmarkContext) {
toggleBookmark(context, {
withColor: true,
type: 'line',
type: BookmarkTypeEnum.LINE,
});
}

Expand Down Expand Up @@ -111,7 +115,7 @@ export async function editLabel(context: LineBookmarkContext) {
if (!bookmark) {
toggleBookmark(context as LineBookmarkContext | undefined, {
label,
type: 'line',
type: BookmarkTypeEnum.LINE,
});
} else {
bookmark.updateLabel(label);
Expand Down
13 changes: 6 additions & 7 deletions src/commands/bookmark/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {resolveBookmarkController} from '../../bootstrap';
import {DEFAULT_BOOKMARK_COLOR} from '../../constants';
import {DEFAULT_BOOKMARK_GROUP_ID} from '../../constants/bookmark';
import {IBookmarkGroup} from '../../stores';
import {BookmarksGroupedByCustomType} from '../../types';
import {BookmarksGroupedByCustomType, TreeViewGroupEnum} from '../../types';
import {
showGroupQuickPick,
getBookmarkFromCtx,
Expand Down Expand Up @@ -106,7 +106,6 @@ export const BookmarkGroupCommands: IBookmarkCommand[] = [
docs: ` * 通过命令创建分组 * - 可支持自定义分组名称(非按照颜色分组,同时之前未分组的归为Default组) * - 分组拖拽移动 * - 分组拖拽排序`,
callback: async (ctx: IBookmarkCommandContext, args: any) => {
let selectedWorkspaceFolder;
console.log(args);
// 此时从命令面板或者视图顶部的菜单中选择调用此命令,以及当存在多个工作区间, 需要用户选择工作区进行操作.
if (!args || (args && args.contextValue !== 'workspace')) {
selectedWorkspaceFolder = await showWorkspaceFolderQuickPick();
Expand Down Expand Up @@ -139,31 +138,31 @@ export const BookmarkGroupCommands: IBookmarkCommand[] = [
docs: '按颜色分组',
callback: async (ctx: IBookmarkCommandContext, args: any) => {
const controller = resolveBookmarkController();
controller.changeGroupView('color');
controller.changeGroupView(TreeViewGroupEnum.COLOR);
},
},
{
name: 'groupedByDefault',
docs: `默认排序分组`,
callback: async (ctx: IBookmarkCommandContext, args: any) => {
const controller = resolveBookmarkController();
controller.changeGroupView('default');
controller.changeGroupView(TreeViewGroupEnum.DEFAULT);
},
},
{
name: 'groupedByWorkspace',
docs: '按照工作区间分组',
callback: async (ctx: IBookmarkCommandContext, args: any) => {
const controller = resolveBookmarkController();
controller.changeGroupView('workspace');
controller.changeGroupView(TreeViewGroupEnum.WORKSPACE);
},
},
{
name: 'groupedByCustom',
docs: '按照用户自定义分组方式分组, 默认未分组放到 `Default` 组中',
callback: async (ctx: IBookmarkCommandContext, args: any) => {
const controller = resolveBookmarkController();
controller.changeGroupView('custom');
controller.changeGroupView(TreeViewGroupEnum.CUSTOM);
},
},
];
Expand Down Expand Up @@ -213,7 +212,7 @@ export async function listBookmarksInSelectedGroup(args: any) {
const bookmarks =
(
controller.store
.getBookmarksGroupedByCustom as BookmarksGroupedByCustomType[]
.bookmarksGroupedByCustom as BookmarksGroupedByCustomType[]
).find(it => it.id === group.id)?.bookmarks || [];

const selectedBookmark = await showBookmarksQuickPick(bookmarks);
Expand Down
5 changes: 3 additions & 2 deletions src/commands/bookmark/view.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import {resolveBookmarkController} from '../../bootstrap';
import {TreeViewStyleEnum} from '../../types';

/**
* 按照树格式展示
*/
export function viewAsTree(args: any) {
const controller = resolveBookmarkController();
controller.changeViewType('tree');
controller.changeViewType(TreeViewStyleEnum.TREE);
}

/**
* 按照列表方式显示
*/
export function viewAsList(args: any) {
const controller = resolveBookmarkController();
controller.changeViewType('list');
controller.changeViewType(TreeViewStyleEnum.LIST);
}
53 changes: 31 additions & 22 deletions src/controllers/BookmarksController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {fileURLToPath} from 'node:url';
import {IDisposable} from '../utils';
import {
DEFAULT_BOOKMARK_COLOR,
DEFAULT_BOOKMARK_GROUP_ID,
EXTENSION_ID,
EXTENSION_STORE_FILE_NAME,
EXTENSION_STORE_PATH,
Expand All @@ -38,11 +39,11 @@ import {BookmarksStore} from '../stores/bookmark-store';

import {IBookmarkManagerConfigure} from '../stores/configure';
import {
TreeViewType,
TreeViewGroupType,
TreeViewSortedType,
TreeViewStyleEnum,
TreeViewSortedTypeEnum,
IBookmarkStoreInfo,
BookmarksGroupedByCustomType,
TreeViewGroupEnum,
} from '../types';
import IController from './IController';

Expand All @@ -53,16 +54,16 @@ export default class BookmarksController implements IController {

private _store!: IBookmarksStore;

public get viewType(): TreeViewType {
return this._store.viewType as TreeViewType;
public get viewType(): TreeViewStyleEnum {
return this._store.viewType as TreeViewStyleEnum;
}

public get groupView(): TreeViewGroupType {
return this._store.groupView as TreeViewGroupType;
public get groupView(): TreeViewGroupEnum {
return this._store.groupView as TreeViewGroupEnum;
}

public get sortedType(): TreeViewSortedType {
return this._store.sortedType as TreeViewSortedType;
public get sortedType(): TreeViewSortedTypeEnum {
return this._store.sortedType as TreeViewSortedTypeEnum;
}

public get groupedBookmarks():
Expand All @@ -71,15 +72,15 @@ export default class BookmarksController implements IController {
| BookmarksGroupedByColorType[]
| BookmarksGroupedByWorkspaceType[] {
switch (this._store.groupView) {
case 'file':
case 'default':
case TreeViewGroupEnum.FILE:
case TreeViewGroupEnum.DEFAULT:
return this._store.bookmarksGroupedByFile;
case 'color':
case TreeViewGroupEnum.COLOR:
return this._store.bookmarksGroupedByColor;
case 'workspace':
case TreeViewGroupEnum.WORKSPACE:
return this._store.bookmarksGroupedByWorkspace;
case 'custom':
return this._store.getBookmarksGroupedByCustom;
case TreeViewGroupEnum.CUSTOM:
return this._store.bookmarksGroupedByCustom;
}
return [];
}
Expand Down Expand Up @@ -174,9 +175,9 @@ export default class BookmarksController implements IController {

if (
(!workspace.workspaceFolders || workspace.workspaceFolders!.length < 2) &&
this.groupView !== 'default'
this.groupView !== TreeViewGroupEnum.DEFAULT
) {
this._store.updateGroupView('default');
this._store.updateGroupView(TreeViewGroupEnum.DEFAULT);
}

this._resolveDatastoreFromStoreFile();
Expand Down Expand Up @@ -216,7 +217,9 @@ export default class BookmarksController implements IController {
'**/.vscode/bookmark-manager.json',
);
this._watcher.onDidDelete(uri => {
if (!uri) {return;}
if (!uri) {
return;
}
const wsFolder = workspace.getWorkspaceFolder(uri);
/**
* 清除指定的工作区间的书签
Expand Down Expand Up @@ -370,17 +373,17 @@ export default class BookmarksController implements IController {
this._fire();
}

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

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

changeGroupView(groupType: TreeViewGroupType) {
changeGroupView(groupType: TreeViewGroupEnum) {
this._store.updateGroupView(groupType);
this.refresh();
}
Expand Down Expand Up @@ -461,8 +464,14 @@ export default class BookmarksController implements IController {
const saveBookmarks = this._store.bookmarks.filter(
it => it.wsFolder?.uri.fsPath === workspace.uri.fsPath,
);
// 可能使用了在其他公共区间船创建的分组
// 可能使用了在其他工作区间船创建的分组
const _usedGroupdIds = saveBookmarks.map(it => it.groupId);

// 默认都要带上默认的分组, 避免首次切换到自定义分组的时候失败
if (!_usedGroupdIds.includes(DEFAULT_BOOKMARK_GROUP_ID)) {
_usedGroupdIds.push(DEFAULT_BOOKMARK_GROUP_ID);
}

const groups = this._store.groups.filter(
it =>
!it.workspace ||
Expand Down
14 changes: 7 additions & 7 deletions src/controllers/IController.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Event, Disposable} from 'vscode';
import {
BaseMeta,
TreeViewGroupType,
TreeViewSortedType,
TreeViewType,
TreeViewGroupEnum,
TreeViewSortedTypeEnum,
TreeViewStyleEnum,
} from '../types';
import {UniversalStoreType} from './UniversalBookmarkController';
import {IBookmarksStore} from '../stores';
Expand All @@ -15,9 +15,9 @@ export default interface IController extends Disposable {

get labeledCount(): number;

get viewType(): TreeViewType;
get viewType(): TreeViewStyleEnum;

get groupView(): TreeViewGroupType;
get groupView(): TreeViewGroupEnum;

onDidChangeEvent: Event<void>;

Expand All @@ -31,7 +31,7 @@ export default interface IController extends Disposable {
clearAll(): void;

refresh(): void;
changeViewType(viewType: TreeViewType): void;
changeViewType(viewType: TreeViewStyleEnum): void;

changeSortType(sortType: TreeViewSortedType): void;
changeSortType(sortType: TreeViewSortedTypeEnum): void;
}
23 changes: 13 additions & 10 deletions src/controllers/UniversalBookmarkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {Event, EventEmitter, ExtensionContext, Uri} from 'vscode';
import {
BaseMeta,
BookmarkColor,
TreeViewGroupType,
TreeViewSortedType,
TreeViewType,
TreeViewGroupEnum,
TreeViewSortedTypeEnum,
TreeViewStyleEnum,
} from '../types';
import {generateUUID} from '../utils';
import IController from './IController';
Expand Down Expand Up @@ -63,7 +63,10 @@ export default class UniversalBookmarkController implements IController {
private _onDidChangeEvent: EventEmitter<void> = new EventEmitter<void>();

private _serviceManager: ServiceManager;
public sortedType: Omit<TreeViewSortedType, 'linenumber'> = 'linenumber';
public sortedType: Omit<
TreeViewSortedTypeEnum,
TreeViewSortedTypeEnum.LINENUMBER
> = TreeViewSortedTypeEnum.CREATED_TIME;
public onDidChangeEvent: Event<void> = this._onDidChangeEvent.event;

get globalState() {
Expand Down Expand Up @@ -96,11 +99,11 @@ export default class UniversalBookmarkController implements IController {
}
this._initial();
}
get viewType(): TreeViewType {
return 'list';
get viewType(): TreeViewStyleEnum {
return TreeViewStyleEnum.LIST;
}
get groupView(): TreeViewGroupType {
return 'default';
get groupView(): TreeViewGroupEnum {
return TreeViewGroupEnum.DEFAULT;
}

dispose() {}
Expand Down Expand Up @@ -174,9 +177,9 @@ export default class UniversalBookmarkController implements IController {
refresh() {
this._onDidChangeEvent.fire();
}
changeViewType(viewType: TreeViewType): void {}
changeViewType(viewType: TreeViewStyleEnum): void {}

changeSortType(sortType: TreeViewSortedType): void {
changeSortType(sortType: TreeViewSortedTypeEnum): void {
this.sortedType = sortType;
}
}
Loading

0 comments on commit 97e3259

Please sign in to comment.