diff --git a/ext-src/KanbnBoardPanel.ts b/ext-src/KanbnBoardPanel.ts index 1423024..090c724 100644 --- a/ext-src/KanbnBoardPanel.ts +++ b/ext-src/KanbnBoardPanel.ts @@ -23,7 +23,7 @@ const sortByFields: Record = { export default class KanbnBoardPanel { private static readonly viewType = 'KanbnBoardPanel' // Maps a kanbn task ID to the KanbnTaskPanel instance - private readonly openedTaskPanels = new Map() + private readonly openedTaskPanels = new Map() private readonly _extensionPath: string private readonly _workspacePath: string private readonly column: vscode.ViewColumn @@ -39,6 +39,10 @@ export default class KanbnBoardPanel { this._panel?.reveal(this.column) } + public getActiveTaskPanel (): KanbnTaskPanel | undefined { + return Array.from(this.openedTaskPanels.values()).find(v => v.isActive()) + } + public getTaskPanel (taskId: string | null): KanbnTaskPanel | undefined { if (taskId != null) { return this.openedTaskPanels.get(taskId) as KanbnTaskPanel @@ -48,10 +52,8 @@ export default class KanbnBoardPanel { public showTaskPanel (taskId: string | null, column: string | null = null): void { let panel: KanbnTaskPanel if (taskId == null || !this.openedTaskPanels.has(taskId)) { - panel = new KanbnTaskPanel(this._extensionPath, this._workspacePath, this._kanbn, this._kanbnFolderName, taskId, column, this.openedTaskPanels) - if (taskId != null) { - this.openedTaskPanels.set(taskId, panel) - } + panel = new KanbnTaskPanel(this._extensionPath, this._workspacePath, this._kanbn, this._kanbnFolderName, taskId ?? Symbol('prelim-task-id'), column, this.openedTaskPanels) + this.openedTaskPanels.set(panel.getTaskId(), panel) } else { panel = this.openedTaskPanels.get(taskId) as KanbnTaskPanel } diff --git a/ext-src/KanbnTaskPanel.ts b/ext-src/KanbnTaskPanel.ts index a0cc17e..2c17952 100644 --- a/ext-src/KanbnTaskPanel.ts +++ b/ext-src/KanbnTaskPanel.ts @@ -77,7 +77,7 @@ export default class KanbnTaskPanel { private readonly _workspacePath: string private readonly _kanbn: Kanbn private readonly _kanbnFolderName: string - private _taskId: string | null + private _taskId: string | symbol private readonly _defaultColumn: string | null private readonly _disposables: vscode.Disposable[] = [] @@ -95,9 +95,9 @@ export default class KanbnTaskPanel { workspacePath: string, kanbn: Kanbn, kanbnFolderName: string, - taskId: string | null, + taskId: string | symbol, defaultColumn: string | null, - taskCache: Map + taskCache: Map ) { const column = vscode.window.activeTextEditor?.viewColumn ?? vscode.ViewColumn.One this._extensionPath = extensionPath @@ -130,7 +130,7 @@ export default class KanbnTaskPanel { } // Set the webview's title to the kanbn task name - if (this._taskId !== null) { + if (typeof this._taskId === 'string') { void this._kanbn.getTask(this._taskId).then((task) => { this._panel.title = task.name }) @@ -159,12 +159,13 @@ export default class KanbnTaskPanel { // Create a task case 'kanbn.updateOrCreate': - if (this._taskId === null) { + if (typeof this._taskId === 'symbol') { await this._kanbn.createTask( transformTaskData(message.taskData, message.customFields), message.taskData.column ) this._panel.onDidDispose((e) => { if (this._taskId !== null) taskCache.delete(this._taskId) }) + taskCache.delete(this._taskId) taskCache.set(message.taskData.id, this) void this.update() if (vscode.workspace.getConfiguration('kanbn').get('showTaskNotifications') ?? true) { @@ -194,12 +195,15 @@ export default class KanbnTaskPanel { // Delete a task and close the webview panel case 'kanbn.delete': { - const taskName: string = (await this._kanbn.getTask(this._taskId ?? '')).name + const deleteTaskId = this._taskId + if (typeof deleteTaskId !== 'string') return + + const taskName: string = (await this._kanbn.getTask(deleteTaskId)).name void vscode.window .showInformationMessage(`Delete task '${taskName}'?`, 'Yes', 'No') .then(async (value) => { if (value === 'Yes') { - if (this._taskId !== null) { await this._kanbn.deleteTask(this._taskId, true) } + await this._kanbn.deleteTask(deleteTaskId, true) this.dispose() if (vscode.workspace.getConfiguration('kanbn').get('showTaskNotifications') ?? true) { void vscode.window.showInformationMessage(`Deleted task '${taskName}'.`) @@ -211,6 +215,8 @@ export default class KanbnTaskPanel { // Archive a task and close the webview panel case 'kanbn.archive': { + if (typeof this._taskId !== 'string') return + const taskName: string = (await this._kanbn.getTask(this._taskId ?? '')).name if (this._taskId !== null) await this._kanbn.archiveTask(this._taskId) this.dispose() @@ -236,6 +242,34 @@ export default class KanbnTaskPanel { } } + public async showTaskFilePanel (): Promise { + if (typeof this._taskId === 'string') { + const folderPath = path.join(this._kanbnFolderName, '.kanbn') + const taskFilename = this._taskId.replace(/(?('taskfileEditor') + try { + await vscode.commands.executeCommand('vscode.openWith', vscode.Uri.file(taskPath), taskfileEditor) + } catch { + const textDoc = await vscode.workspace.openTextDocument(taskPath) + await vscode.window.showTextDocument(textDoc) + } + } + } + + public getTaskId (): string | symbol { + return this._taskId + } + + public isActive (): boolean { + try { + return this._panel.active + } catch { + return false + } + } + private async _getTaskData (): Promise { let index: any try { diff --git a/ext-src/extension.ts b/ext-src/extension.ts index e88094e..eb3f314 100644 --- a/ext-src/extension.ts +++ b/ext-src/extension.ts @@ -217,16 +217,7 @@ export async function activate (context: vscode.ExtensionContext): Promise const kanbnTuple = boardCache.get(board) if (kanbnTuple === undefined) { return } - // Use label of tab as taskId - const taskId = formatAsTaskId(vscode.window.tabGroups.activeTabGroup?.activeTab?.label) - - if (taskId !== undefined) { - const panel = kanbnTuple.kanbnBoardPanel.getTaskPanel(taskId) - - if (panel !== undefined) { - panel.sendSaveRequest() - } - } + kanbnTuple.kanbnBoardPanel.getActiveTaskPanel()?.sendSaveRequest() }) ) @@ -296,21 +287,7 @@ export async function activate (context: vscode.ExtensionContext): Promise if (kanbnTuple === undefined) { return } // Use label of tab as taskId - const taskId = formatAsTaskId(vscode.window.tabGroups.activeTabGroup?.activeTab?.label) - - if (taskId !== undefined) { - const folderPath = await kanbnTuple.kanbn.getTaskFolderPath() - const taskFilename = taskId.replace(/(?('taskfileEditor') - try { - await vscode.commands.executeCommand('vscode.openWith', vscode.Uri.file(taskPath), taskfileEditor) - } catch { - const textDoc = await vscode.workspace.openTextDocument(taskPath) - await vscode.window.showTextDocument(textDoc) - } - } + await kanbnTuple.kanbnBoardPanel.getActiveTaskPanel()?.showTaskFilePanel() }) ) @@ -436,7 +413,3 @@ export async function activate (context: vscode.ExtensionContext): Promise } }) } - -function formatAsTaskId (label: string | undefined): string | undefined { - return label?.toLowerCase().replace(/\s+/g, '-') -}