-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
195 lines (169 loc) · 5.99 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Import required modules
const vscode = require('vscode');
const fs = require('fs');
const path = require('path');
const os = require('os');
const { execSync } = require('child_process');
const { v4: uuidv4 } = require('uuid');
const DataAccessLayer = require('./dataAccessLayer');
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
// Create file watcher
const fileWatcher = vscode.workspace.createFileSystemWatcher(filePath);
fileWatcher.onDidChange(getWebviewContent);
context.subscriptions.push(fileWatcher);
// Log activation message
console.log('Congratulations, your extension "bingo-kanban" is now active!');
// Register commands
registerBoardCommand(context);
registerFindTodosAndWipsCommand(context);
}
// This method is called when your extension is deactivated
function deactivate() { }
// Define file path
const filePath = path.join(vscode.workspace.rootPath, 'kanban.json');
// Initialize data access layer
const dal = new DataAccessLayer(filePath);
// Function to get webview content
const getWebviewContent = () => {
dal.ensureKanbanData();
const kanbanData = dal.getKanbanData();
if (!kanbanData) return 'Error: Kanban data not found';
let kanbanHtml = '';
for (let column in kanbanData) {
let columnHtml = `<div class="column"><h2>${column}</h2>`;
for (let card of kanbanData[column]) {
columnHtml += `<div class="card">
<h4>${card.task}</h4>
<time>${new Date(card.modifiedDate).toLocaleDateString()}</time>
<span>${card.username}</span>
<button onclick="deleteCard('${card.id}')">Delete</button>
</div>`;
}
columnHtml += '</div>';
kanbanHtml += columnHtml;
}
// Return the full HTML
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kanban Board</title>
<style>
.kanban-board {
display: flex;
justify-content: space-between;
gap: 20px;
}
.column {
flex: 1;
border: 1px solid #ccc;
padding: 10px;
border-radius: 5px;
}
.card {
background-color: #eee;
margin-bottom: 10px;
padding: 5px;
border-radius: 3px;
}
</style>
<script>
const vscode = acquireVsCodeApi();
function deleteCard(id) {
vscode.postMessage({ command: 'delete', id: id });
}
function updateCard(id) {
//todo: Add your update logic here
vscode.postMessage({ command: 'update', id: id });
}
</script>
</head>
<body>
<h1>Kanban Board</h1>
<div class="kanban-board">
${kanbanHtml}
</div>
</body>
</html>`;
}
// Register board command
function registerBoardCommand(context) {
let disposable = vscode.commands.registerCommand('bingo-kanban.view', () => {
const panel = vscode.window.createWebviewPanel(
'kanban',
'Kanban Board',
vscode.ViewColumn.One,
{ enableScripts: true }
);
dal.ensureKanbanData();
const kanbanData = dal.getKanbanData();
panel.webview.html = getWebviewContent(kanbanData);
panel.webview.onDidReceiveMessage(
message => {
vscode.window.showInformationMessage(`Processing: ${message.command}`);
switch (message.command) {
case 'delete':
dal.deleteTask(message.id);
panel.webview.html = getWebviewContent(dal.getKanbanData());
return;
case 'update':
// Add your update logic here
return;
}
},
undefined,
context.subscriptions
);
});
context.subscriptions.push(disposable);
}
// Register find todos and wips command
function registerFindTodosAndWipsCommand(context) {
let disposable = vscode.commands.registerCommand('bingo-kanban.add', () => {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showErrorMessage('No active editor found');
return;
}
const text = editor.document.getText();
const lines = text.split('\n');
let todos = [];
let wips = [];
lines.forEach(line => {
const todoIndex = line.indexOf('todo:');
if (todoIndex !== -1) {
const todo = line.slice(todoIndex + 'todo:'.length).trim();
todos.push(todo);
}
const wipIndex = line.indexOf('wip:');
if (wipIndex !== -1) {
const wip = line.slice(wipIndex + 'wip:'.length).trim();
wips.push(wip);
}
});
dal.ensureKanbanData();
let kanbanData = dal.getKanbanData();
const fileStats = fs.statSync(editor.document.fileName);
const modifiedDate = fileStats.mtime;
let username;
try {
username = execSync('git config user.name', { encoding: 'utf8' }).trim();
} catch (error) {
username = os.userInfo().username;
}
todos = todos.map(todo => ({ id: uuidv4(), task: todo, modifiedDate: modifiedDate, username: username }));
wips = wips.map(wip => ({ id: uuidv4(), task: wip, modifiedDate: modifiedDate, username: username }));
kanbanData['To Do'] = [...new Set([...kanbanData['To Do'] || [], ...todos])];
kanbanData['In Progress'] = [...new Set([...kanbanData['In Progress'] || [], ...wips])];
dal.saveKanbanData(kanbanData);
});
context.subscriptions.push(disposable);
}
module.exports = {
activate,
deactivate
}