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

First PR creating the viewer for d2 files in VsCode. #19

Merged
merged 1 commit into from
Jan 12, 2023
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.DS_Store
node_modules
*.vsix
dist
142 changes: 120 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"name": "d2",
"version": "0.3.0",
"displayName": "D2",
"publisher": "Terrastruct",
"description": "Support for .d2 files.",
"version": "0.4.0",
"license": "BSD-3-Clause",
"contributors": [
"Barry Nolte <barry@barrynolte.com>"
],
"engines": {
"vscode": "^1.0.0"
"vscode": "^1.73.0"
},
"license": "BSD-3-Clause",
"displayName": "d2",
"description": "Support for .d2 files.",
"categories": [
"Programming Languages",
"Themes"
"Visualization"
],
"keywords": [
"diagram",
Expand All @@ -21,6 +24,10 @@
"uml",
"mindmap"
],
"activationEvents": [
"onStartupFinished"
],
"main": "./dist/extension.js",
"contributes": {
"languages": [
{
Expand Down Expand Up @@ -50,32 +57,123 @@
"path": "./syntaxes/markdown.tmLanguage.json"
}
],
"themes": [
"commands": [
{
"label": "D2 Light",
"uiTheme": "vs",
"path": "./themes/light-color-theme.json"
},
"command": "D2.ShowPreviewWindow",
"title": "Show d2 Preview Window",
"category": "d2"
}
],
"keybindings": [
{
"label": "D2 Dark",
"uiTheme": "vs-dark",
"path": "./themes/dark-color-theme.json"
"command": "D2.ShowPreviewWindow",
"key": "ctrl+shift+d",
"mac": "shift+cmd+d"
}
]
],
"configuration": {
"type": "object",
"title": "d2 Viewer Configuration",
"properties": {
"D2.autoUpdate": {
"type": "boolean",
"default": true,
"description": "Automatically update the preview as you edit the d2 file."
},
"D2.updateTimer": {
"type": "number",
"default": 1500,
"description": "Specifies the amount of time (in ms) to wait between keystrokes before updating the preview."
},
"D2.updateOnSave": {
"type": "boolean",
"default": false,
"description": "Automatically update on saving the d2 file."
}
}
},
"menus": {
"commandPalette": [
{
"command": "D2.ShowPreviewWindow",
"when": "editorLangId == d2",
"group": "d2"
}
],
"editor/context": [
{
"command": "D2.ShowPreviewWindow",
"when": "resourceLangId == d2",
"group": "d2"
}
],
"editor/title": [
{
"command": "D2.ShowPreviewWindow",
"when": "resourceLangId == d2",
"group": "d2"
}
],
"editor/title/context": [
{
"command": "D2.ShowPreviewWindow",
"when": "resourceLangId == d2",
"group": "d2"
}
],
"explorer/context": [
{
"command": "D2.ShowPreviewWindow",
"when": "resourceLangId == d2",
"group": "d2"
}
]
}
},
"scripts": {
"vscode:prepublish": "yarn run package",
"compile": "webpack",
"watch": "webpack --watch",
"package": "webpack --mode production --devtool hidden-source-map",
"lint": "eslint src --ext ts",
"dev": "code --uninstall-extension terrastruct.d2; yarn gen && yarn pkg && code --install-extension d2.vsix",
"gen": "yq --output-format json syntaxes/d2.tmLanguage.yaml > syntaxes/d2.tmLanguage.json",
"pkg": "vsce package --out d2.vsix"
},
"devDependencies": {
"@types/glob": "^8.0.0",
"@types/mocha": "^10.0.0",
"@types/node": "16.x",
"@types/temp": "^0.9.1",
"@types/vscode": "^1.73.0",
"@typescript-eslint/eslint-plugin": "^5.42.0",
"@typescript-eslint/parser": "^5.42.0",
"@vscode/test-electron": "^2.2.0",
"eslint": "^8.26.0",
"eslint-config-next": "^13.0.7",
"eslint-config-prettier": "^8.5.0",
"glob": "^8.0.3",
"mocha": "^10.1.0",
"ts-loader": "^9.4.2",
"typescript": "^4.8.4",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0"
},
"dependencies": {
"child_process": "^1.0.2",
"eslint-plugin-simple-import-sort": "^8.0.0",
"eslint-plugin-unused-imports": "^2.0.0",
"next": "^13.0.7",
"temp": "^0.9.4"
},
"homepage": "https://d2lang.com",
"icon": "d2-icon.png",
"bugs": {
"url": "https://github.com/terrastruct/d2-vscode/issues",
"email": "info@terrastruct.com"
},
"repository": {
"type": "git",
"url": "https://github.com/terrastruct/d2-vscode"
},
"homepage": "https://d2lang.com",
"icon": "d2-icon.png",
"scripts": {
"dev": "code --uninstall-extension terrastruct.d2; yarn gen && yarn pkg && code --install-extension d2.vsix",
"gen": "yq --output-format json syntaxes/d2.tmLanguage.yaml > syntaxes/d2.tmLanguage.json",
"pkg": "vsce package --out d2.vsix"
}
}
173 changes: 173 additions & 0 deletions pages/previewPage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<!DOCTYPE html>
<html lang="en" title="d2 Diagram">
<title>Diagram</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">

<head>
<style>
#toolbar {
z-index: 100;
position: fixed;
left: 0px;
top: 0px;
background-color: #909090;
padding-left: 15px;
padding-right: 200px;
padding-bottom: 5px;
width: 100%;
color: #0D32B2;
}

#zoomSlider {
appearance: none;
border-radius: 3px;
background: #0D32B2;
height: 5px;
}

#zoomSlider::-webkit-slider-thumb {
appearance: none;
box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
border: 1px solid darkblue;
height: 20px;
width: 8px;
margin-bottom: 5px;
border-radius: 3px;
background: lightblue;
cursor: pointer;
}

#percent {
display: inline-block;
font-family: monospace;
font-size: 18px;
font-weight: 600;
width: 55px;
}

button {
margin-top: 3px;
color: #0D32B2;
border-color: #0D32B2;
background-color: #F7F8FE;
border-radius: 6px;
border: 3 3 3 3;
box-shadow: 1px 1px 1px black, 0px 0px 1px 0d0d0d;
padding: 5px;
min-width: 50px;
font-size: 14px;
font-weight: 600;
}

svg {
position: absolute;
left: 0px;
top: 0px;
transform-origin: 0px 0px;
}

body {
background-color: var(--vscode-editor-background);
color: var(--vscode-editor-foreground);
fill: var(--vscode-editor-foreground);
width: 100%;
height: 100%;
}
</style>
<script>
var zoomLevel = 100;
var percent;
var zoomSlider;
var fitMode = true;

function getSvgElement() {
var svgs = document.getElementsByTagName('svg');
return svgs[0];
}
function init() {
percent = document.getElementById('percent');
zoomSlider = document.getElementById('zoomSlider');
zoomFit();
}
function zoomPlus() {
zoomLevel = Math.min(150, zoomLevel + 5);
setZoomLevel(zoomLevel);
}
function zoomMinus() {
zoomLevel = Math.max(10, zoomLevel - 5);
setZoomLevel(zoomLevel);
}
function zoomFit() {
setZoomLevel(100);
var svg = getSvgElement();
svg.style.height = "100%";
svg.style.width = "100%";
setZoomText(false)

fitMode = true;
}
function setZoomLevel(zl) {
var svg = getSvgElement();
svg.style.height = "";
svg.style.width = "";
svg.style.zoom = zl / 100.0;
setZoomText(true);

zoomSlider.value = zl;

fitMode = false;
}
function sliderOnChange() { setZoomLevel(zoomSlider.value); }
function setZoomText(text) {
var svg = getSvgElement();

if (text === true) {
percent.innerText = (svg.style.zoom * 100.0).toFixed() + "%";
}
else {
percent.innerText = "Fit";
}
}

window.addEventListener("DOMContentLoaded", () => {
init();
});

window.addEventListener("message", (event) => {
const message = event.data; // The JSON data our extension sent

switch (message.command) {
case "render":
var previewWrapper = document.getElementById('previewWrapper');
previewWrapper.innerHTML = message.data;

if (fitMode === true) {
zoomFit();
} else {
setZoomLevel(zoomLevel);
}
break;
}
});
</script>

</head>

<body>
<div id="toolbar">
<button onclick="zoomMinus();" title="Zoom Out">Out</button>
<input id="zoomSlider" title="Zoom" onchange="sliderOnChange()" type="range" min="10" max="150" value="100"
step="5">
<button onclick="zoomPlus();" title="Zoom In">In</button>
<span id="percent"></span>
<button id="zoomFit" onclick="zoomFit()" title="Fit to window">Fit to window</button>
</div>
<div id="previewWrapper">
<svg>
<text font-size="x-large" x="20" y="50">Loading...</text>
</svg>
</div>

<body>
<html>
Loading