Skip to content

Commit

Permalink
Merge pull request #13 from Ho-Wan/feature/show-card-members
Browse files Browse the repository at this point in the history
Feature/show card members
  • Loading branch information
ho-wan authored Jan 20, 2019
2 parents 9e19284 + cc974a4 commit ca45409
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 17 deletions.
68 changes: 51 additions & 17 deletions src/trello/TrelloUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
TrelloChecklist,
TrelloActionComment,
CheckItem,
TrelloMember,
} from "./trelloComponents";
import {
VSCODE_VIEW_COLUMN,
Expand All @@ -27,13 +28,25 @@ export class TrelloUtils {
private API_KEY: string | undefined;
private API_TOKEN: string | undefined;
private FAVORITE_LIST_ID: string | undefined;
private tempTrelloFile: string;

constructor(context?: vscode.ExtensionContext) {
this.globalState = context ? context.globalState : {};
axios.defaults.baseURL = TRELLO_API_BASE_URL;
this.tempTrelloFile = new UserDataFolder().getPathCodeSettings() + TEMP_TRELLO_FILE_NAME || "";

this.getCredentials();
this.getFavoriteList();
this.setMarkdownPreviewBreaks();
}

setMarkdownPreviewBreaks(): void {
try {
const config = vscode.workspace.getConfiguration();
config.update("markdown.preview.breaks", true, true);
} catch (error) {
console.error(error);
}
}

isCredentialsProvided(): boolean {
Expand Down Expand Up @@ -197,6 +210,7 @@ export class TrelloUtils {
attachments: "cover",
actions: "commentCard",
actions_limit: 20,
members: true,
},
credentialsRequired
);
Expand Down Expand Up @@ -259,9 +273,16 @@ export class TrelloUtils {
vscode.commands.executeCommand("trelloViewer.refreshFavoriteList");
}

showChecklistsAsMarkdown(checklists: TrelloChecklist[]): string | undefined {
showCardMembersAsString(members: TrelloMember[]): string {
if (!members || members.length == 0) {
return "";
}
return members.map(member => member.initials).join(', ');
};

showChecklistsAsMarkdown(checklists: TrelloChecklist[]): string {
if (!checklists || checklists.length == 0) {
return;
return "";
}

let checklistMarkdown: string = "";
Expand All @@ -277,9 +298,9 @@ export class TrelloUtils {
return checklistMarkdown;
}

showCommentsAsMarkdown(comments: any): string | undefined {
showCommentsAsMarkdown(comments: TrelloActionComment[]): string {
if (!comments || comments.length == 0) {
return;
return "";
}

let commentsMarkdown: string = "";
Expand All @@ -299,32 +320,45 @@ export class TrelloUtils {
return commentsMarkdown;
}

showMarkdownDecorated(header: string, content: string | undefined): string {
if (!content) {
return "";
}
return `## **\`${header}\`** \n${content}\n\n--- \n`;
};

async showCard(card: TrelloCard): Promise<void> {
if (!card) {
vscode.window.showErrorMessage("No card selected or invalid card.");
return;
}

let checklistItems: string | undefined = this.showChecklistsAsMarkdown(card.trelloChecklists);
let commentItems: string | undefined = this.showCommentsAsMarkdown(card.actions);
const cardMembers: string = this.showCardMembersAsString(card.members);
const checklistItems: string = this.showChecklistsAsMarkdown(card.trelloChecklists);
const commentItems: string = this.showCommentsAsMarkdown(card.actions);
const cardCoverImageUrl = (!!card.attachments && card.attachments.length > 0) ? card.attachments[0].url : "";

const cardCoverImageUrl = card.attachments.length > 0 ? card.attachments[0].url : "";
const cardContentAndHeaders = [
{ header: "URL", content: card.url },
{ header: "Title", content: card.name },
{ header: "Members", content: cardMembers },
{ header: "Description", content: card.desc },
{ header: "Checklists", content: checklistItems },
{ header: "Comments", content: commentItems },
];

let cardContent: string = "";
cardContent += card.url ? `${card.url}\n\n---\n` : "";
cardContent += card.name ? `## ===TITLE===\n${card.name}\n\n---\n` : "";
cardContent += card.desc ? `## ===DESCRIPTION===\n${card.desc}\n\n---\n` : "";
cardContent += checklistItems ? `## ===CHECKLISTS===\n${checklistItems}\n\n---\n` : "";
cardContent += commentItems ? `## ===COMMENTS===\n${commentItems}\n\n---\n` : "";
cardContentAndHeaders.map(({header, content}) => {
cardContent += this.showMarkdownDecorated(header, content);
});
cardContent += cardCoverImageUrl ? `<img src="${cardCoverImageUrl}" alt="Image not found" />` : "";

// Get location of user's vs code folder to save temp markdown file
const tempTrelloFile = new UserDataFolder().getPathCodeSettings() + TEMP_TRELLO_FILE_NAME;
writeFile(tempTrelloFile, cardContent, err => {
// Write temp markdown file at user's vs code default settings directory
writeFile(this.tempTrelloFile, cardContent, err => {
if (err) {
vscode.window.showErrorMessage(`Error writing to temp file: ${err}`);
}
console.info(`✍ Writing to file: ${tempTrelloFile}`);
console.info(`✍ Writing to file: ${this.tempTrelloFile}`);
});

// open markdown file and preview view
Expand All @@ -336,7 +370,7 @@ export class TrelloUtils {
viewColumn = SETTING_CONFIG.DEFAULT_VIEW_COLUMN;
}
vscode.workspace
.openTextDocument(tempTrelloFile)
.openTextDocument(this.tempTrelloFile)
.then(doc => vscode.window.showTextDocument(doc, viewColumn, false))
.then(() => vscode.commands.executeCommand("markdown.showPreview"))
.then(() => vscode.commands.executeCommand("markdown.preview.toggleLock"));
Expand Down
6 changes: 6 additions & 0 deletions src/trello/trelloComponents.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ export interface TrelloCard {
idChecklists: string[];
trelloChecklists: TrelloChecklist[];
actions: TrelloActionComment[];
members: TrelloMember[];
[key: string]: any;
}

export interface TrelloMember {
id: string;
initials: string;
}

export interface TrelloActionComment {
id: string;
date: string;
Expand Down

0 comments on commit ca45409

Please sign in to comment.