Skip to content

Commit

Permalink
Progresses #28 - pub to OWM & generate clone urls
Browse files Browse the repository at this point in the history
  • Loading branch information
damonsk committed Jan 11, 2024
1 parent dd4491e commit a6e7d4e
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 2 deletions.
1 change: 0 additions & 1 deletion extension/src/MapViewLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ class MapViewLoader {
scriptText += `<script nonce="${nonce}" src="${element}"></script>\n`;
}
}
//<meta http-equiv="Content-Security-Policy" content="default-src 'self' vscode-resource:; connect-src 'self' vscode-resource: https:; img-src 'self' data: vscode-resource: https:; script-src 'nonce-${nonce}'; style-src vscode-resource: 'unsafe-inline' http: https: data:;">
return `<!DOCTYPE html>
<html lang="en">
<head>
Expand Down
112 changes: 111 additions & 1 deletion extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ interface MapView {
loader: MapViewLoader;
}

interface OwmApiResponse {
id: string;
mapText: string;
// Add other attributes as needed
}

function activate(context: vscode.ExtensionContext) {
const mapViewExists = (name: string) => {
return getMapView(name) !== undefined;
Expand Down Expand Up @@ -213,7 +219,111 @@ function activate(context: vscode.ExtensionContext) {
);
}
}
)
),
vscode.commands.registerCommand(
'vscode-wardley-maps.export-to-owm',
async function () {
const editor = vscode.window.activeTextEditor;

if (editor) {
const { fileName } = editor.document;
console.log(
'[extension.ts] vscode-wardley-maps.export-to-owm -- ' +
editor.document.fileName
);
const mapText =
editor.document.getText() +
'\n\n//Exported from vscode-wardley-maps';

try {
const response = await fetch(
wmlandscape.Defaults.ApiEndpoint + 'save',
{
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: JSON.stringify({
id: '',
text: mapText,
}),
}
);

const data = await response.json() as OwmApiResponse;

// Show a VSCode info alert with the response data
vscode.window.showInformationMessage(
`Map exported successfully. URL: https://onlinewardleymaps.com/#${data.id}`
);

vscode.env.openExternal(vscode.Uri.parse(`https://onlinewardleymaps.com/#${data.id}`));

} catch (error) {
console.error('Error exporting to OWM:', error);
vscode.window.showErrorMessage(
'An error occurred while exporting to OWM.'
);
}
} else {
vscode.window.showErrorMessage(
'Please make sure Map Text document has focus.'
);
}
}
),
vscode.commands.registerCommand(
'vscode-wardley-maps.generate-clone-url',
async function () {
const editor = vscode.window.activeTextEditor;

if (editor) {
const { fileName } = editor.document;
console.log(
'[extension.ts] vscode-wardley-maps.generate-clone-url -- ' +
editor.document.fileName
);
const mapText =
editor.document.getText() +
'\n\n//Exported from vscode-wardley-maps';

try {
const response = await fetch(
wmlandscape.Defaults.ApiEndpoint + 'save',
{
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: JSON.stringify({
id: '',
text: mapText,
}),
}
);

const data = await response.json() as OwmApiResponse;

// Show a VSCode info alert with the response data
vscode.window.showInformationMessage(
`Map exported successfully. URL: https://onlinewardleymaps.com/#clone:${data.id}`
);

vscode.env.openExternal(vscode.Uri.parse(`https://onlinewardleymaps.com/#clone:${data.id}`));

} catch (error) {
console.error('Error exporting to OWM:', error);
vscode.window.showErrorMessage(
'An error occurred while exporting to OWM.'
);
}
} else {
vscode.window.showErrorMessage(
'Please make sure Map Text document has focus.'
);
}
}
),
);

context.subscriptions.push(
Expand Down
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@
"command": "vscode-wardley-maps.export-map-png",
"title": "Export Map as PNG",
"category": "Wardley Maps"
},
{
"command": "vscode-wardley-maps.export-to-owm",
"title": "Publish to OnlineWardleyMaps.com",
"category": "Wardley Maps"
}
,
{
"command": "vscode-wardley-maps.generate-clone-url",
"title": "Publish & Generate Clone URL",
"category": "Wardley Maps"
}
],
"languages": [
Expand Down

0 comments on commit a6e7d4e

Please sign in to comment.