Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mermaid chart sdk integrated #21

Open
wants to merge 2 commits into
base: release-2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"out": true // set this to false to include "out" folder in search results
},
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
"typescript.tsc.autoDetect": "off"
"typescript.tsc.autoDetect": "off",
"cSpell.words": ["mermaidchart"]
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,9 @@
"typescript": "^4.9.5"
},
"dependencies": {
"axios": "^1.4.0",
"uuid": "^9.0.0",
"@mermaidchart/sdk": "^0.2.0",
"yaml": "^2.7.0"
}
},
"packageManager": "[email protected]+sha512.faf344af2d6ca65c4c5c8c2224ea77a81a5e8859cbc4e06b1511ddce2f0151512431dd19e6aff31f2c6a8f5f2aced9bd2273e1fed7dd4de1868984059d2c4247"
}
24 changes: 21 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 64 additions & 22 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from "vscode";
import { MermaidChartProvider, MCTreeItem, getAllTreeViewProjectsCache } from "./mermaidChartProvider";
import { MermaidChartProvider, MCTreeItem, getAllTreeViewProjectsCache, getProjectIdForDocument } from "./mermaidChartProvider";
import { MermaidChartVSCode } from "./mermaidChartVSCode";
import {
applyMermaidChartTokenHighlighting,
Expand Down Expand Up @@ -193,7 +193,7 @@ context.subscriptions.push(
);

context.subscriptions.push(
vscode.commands.registerCommand('mermaid.connectDiagram',async(uri:vscode.Uri, range:vscode.Range)=>{
vscode.commands.registerCommand('mermaid.connectDiagram', async (uri: vscode.Uri, range: vscode.Range) => {
const document = await vscode.workspace.openTextDocument(uri);
const content = document.getText();
const fileExt = path.extname(document.fileName);
Expand All @@ -205,31 +205,57 @@ context.subscriptions.push(
projects.map((p) => ({ label: p.title, description: p.title, projectId: p.uuid })),
{ placeHolder: "Select a project to save the diagram" }
);

if (!selectedProject || !selectedProject?.projectId) {
vscode.window.showInformationMessage("Operation cancelled.");
return;
vscode.window.showInformationMessage("Operation cancelled.");
return;
}

const response = await mcAPI.createDocumentWithDiagram(diagramCode, selectedProject.projectId)
try {
const newDocument = await mcAPI.createDocument(selectedProject.projectId);

if (!newDocument || !newDocument.documentID) {
vscode.window.showErrorMessage("Failed to create a new document.");
return;
}

await mcAPI.setDocument({
documentID: newDocument.documentID,
projectID: selectedProject.projectId,
code: diagramCode,
});

const processedCode = ensureIdField(diagramCode, newDocument.documentID);
mermaidChartProvider.refresh();
const editor = await createMermaidFile(context, processedCode, true);

const processedCode = ensureIdField(diagramCode, response.documentID);
const editor= await await createMermaidFile(context, processedCode, true);
if(editor){
syncAuxFile(editor.document.uri.toString(), uri,range);
}
if (editor) {
syncAuxFile(editor.document.uri.toString(), uri, range);
}
} catch (error) {
vscode.window.showErrorMessage(`Error creating or connecting document: ${error instanceof Error ? error.message : "Unknown error occurred."}`);
}
})
)
vscode.workspace.onWillSaveTextDocument(async (event) => {
if (event.document.languageId.startsWith("mermaid")) {
event.waitUntil(Promise.resolve([]));
const content = event.document.getText();
const diagramId = extractIdFromCode(content);
if (diagramId) {
await mcAPI.saveDocumentCode(content, diagramId);
);

vscode.workspace.onWillSaveTextDocument(async (event) => {
if (event.document.languageId.startsWith("mermaid")) {
event.waitUntil(Promise.resolve([]));
const content = event.document.getText();
const diagramId = extractIdFromCode(content);
if (diagramId) {
const projectId = getProjectIdForDocument(diagramId);

if (projectId) {
await mcAPI.setDocument({
documentID: diagramId,
projectID: projectId,
code: content,
});
}
}
});
}
});

context.subscriptions.push(
vscode.commands.registerCommand('mermaidChart.syncDiagramWithMermaid', async () => {
Expand All @@ -245,13 +271,29 @@ context.subscriptions.push(
try {
const diagramId = extractIdFromCode(content);
if (TempFileCache.hasTempUri(context, document.uri.toString()) && diagramId) {
await mcAPI.saveDocumentCode(content, diagramId);
const projectId = getProjectIdForDocument(diagramId);

if (projectId) {
await mcAPI.setDocument({
documentID: diagramId,
projectID: projectId,
code: content,
});
}
vscode.window.showInformationMessage(`Diagram synced successfully with Mermaid chart. Diagram ID: ${diagramId}`);
} else if (TempFileCache.hasTempUri(context, document.uri.toString())){
vscode.window.showInformationMessage('This is temporary buffer, this can not be saved locally');
} else if (!TempFileCache.hasTempUri(context, document.uri.toString()) && diagramId) {
await vscode.commands.executeCommand('workbench.action.files.save');
await mcAPI.saveDocumentCode(content, diagramId);
const projectId = getProjectIdForDocument(diagramId);

if (projectId) {
await mcAPI.setDocument({
documentID: diagramId,
projectID: projectId,
code: content,
});
}
vscode.window.showInformationMessage(`Diagram synced successfully with Mermaid chart. Diagram ID: ${diagramId}`);
} else {
await vscode.commands.executeCommand('workbench.action.files.save');
Expand Down
Loading