Skip to content

Commit

Permalink
Cleaner taskId getter
Browse files Browse the repository at this point in the history
  • Loading branch information
baflo committed Sep 24, 2024
1 parent 7eb14bf commit 41af88d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 41 deletions.
12 changes: 7 additions & 5 deletions ext-src/KanbnBoardPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const sortByFields: Record<string, string> = {
export default class KanbnBoardPanel {
private static readonly viewType = 'KanbnBoardPanel'
// Maps a kanbn task ID to the KanbnTaskPanel instance
private readonly openedTaskPanels = new Map<string, KanbnTaskPanel>()
private readonly openedTaskPanels = new Map<string | symbol, KanbnTaskPanel>()
private readonly _extensionPath: string
private readonly _workspacePath: string
private readonly column: vscode.ViewColumn
Expand All @@ -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
Expand All @@ -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
}
Expand Down
48 changes: 41 additions & 7 deletions ext-src/KanbnTaskPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = []

Expand All @@ -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<string, KanbnTaskPanel>
taskCache: Map<string | symbol, KanbnTaskPanel>
) {
const column = vscode.window.activeTextEditor?.viewColumn ?? vscode.ViewColumn.One
this._extensionPath = extensionPath
Expand Down Expand Up @@ -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
})
Expand Down Expand Up @@ -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<boolean>('showTaskNotifications') ?? true) {
Expand Down Expand Up @@ -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<boolean>('showTaskNotifications') ?? true) {
void vscode.window.showInformationMessage(`Deleted task '${taskName}'.`)
Expand All @@ -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()
Expand All @@ -236,6 +242,34 @@ export default class KanbnTaskPanel {
}
}

public async showTaskFilePanel (): Promise<void> {
if (typeof this._taskId === 'string') {
const folderPath = path.join(this._kanbnFolderName, '.kanbn')
const taskFilename = this._taskId.replace(/(?<!\.md)$/, '.md')
const taskPath = path.join(folderPath, 'tasks', taskFilename)

const taskfileEditor = vscode.workspace.getConfiguration('kanbn').get<boolean>('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<any> {
let index: any
try {
Expand Down
31 changes: 2 additions & 29 deletions ext-src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,7 @@ export async function activate (context: vscode.ExtensionContext): Promise<void>
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()
})
)

Expand Down Expand Up @@ -296,21 +287,7 @@ export async function activate (context: vscode.ExtensionContext): Promise<void>
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(/(?<!\.md)$/, '.md')
const taskPath = path.join(folderPath, taskFilename)

const taskfileEditor = vscode.workspace.getConfiguration('kanbn').get<boolean>('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()
})
)

Expand Down Expand Up @@ -436,7 +413,3 @@ export async function activate (context: vscode.ExtensionContext): Promise<void>
}
})
}

function formatAsTaskId (label: string | undefined): string | undefined {
return label?.toLowerCase().replace(/\s+/g, '-')
}

0 comments on commit 41af88d

Please sign in to comment.