Merge pull request #944 from gavin-ts/fix-watch-mode-zoom
fix zoom in watch mode
This commit is contained in:
commit
7655c1b272
3 changed files with 26 additions and 10 deletions
|
|
@ -7,3 +7,4 @@
|
||||||
#### Bugfixes ⛑️
|
#### Bugfixes ⛑️
|
||||||
|
|
||||||
- Fixes a regression where PNG backgrounds could be cut off in the appendix. [#941](https://github.com/terrastruct/d2/pull/941)
|
- Fixes a regression where PNG backgrounds could be cut off in the appendix. [#941](https://github.com/terrastruct/d2/pull/941)
|
||||||
|
- Fixes zooming not working in watch mode. [#944](https://github.com/terrastruct/d2/pull/944)
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,3 @@
|
||||||
body {
|
|
||||||
margin: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#d2-svg-container > svg {
|
|
||||||
position: absolute;
|
|
||||||
width: 100%;
|
|
||||||
height: 100%;
|
|
||||||
}
|
|
||||||
|
|
||||||
#d2-err {
|
#d2-err {
|
||||||
/* This style was copied from Chrome's svg parser error style. */
|
/* This style was copied from Chrome's svg parser error style. */
|
||||||
white-space: pre-wrap;
|
white-space: pre-wrap;
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,8 @@ function init(reconnectDelay) {
|
||||||
const ws = new WebSocket(
|
const ws = new WebSocket(
|
||||||
`ws://${window.location.host}${window.location.pathname}watch`
|
`ws://${window.location.host}${window.location.pathname}watch`
|
||||||
);
|
);
|
||||||
|
let isInit = true;
|
||||||
|
let ratio;
|
||||||
ws.onopen = () => {
|
ws.onopen = () => {
|
||||||
reconnectDelay = 1000;
|
reconnectDelay = 1000;
|
||||||
console.info("watch websocket opened");
|
console.info("watch websocket opened");
|
||||||
|
|
@ -31,6 +33,29 @@ function init(reconnectDelay) {
|
||||||
// setting innerHTML to only the actual svg innards. However then you also need to parse
|
// setting innerHTML to only the actual svg innards. However then you also need to parse
|
||||||
// out the width, height and viewbox out of the top level SVG tag and update those manually.
|
// out the width, height and viewbox out of the top level SVG tag and update those manually.
|
||||||
d2SVG.innerHTML = msg.svg;
|
d2SVG.innerHTML = msg.svg;
|
||||||
|
|
||||||
|
const svgEl = d2SVG.querySelector("#d2-svg");
|
||||||
|
// just use inner SVG in watch mode
|
||||||
|
svgEl.parentElement.replaceWith(svgEl);
|
||||||
|
let width = parseInt(svgEl.getAttribute("width"), 10);
|
||||||
|
let height = parseInt(svgEl.getAttribute("height"), 10);
|
||||||
|
if (isInit) {
|
||||||
|
if (width > height) {
|
||||||
|
if (width > window.innerWidth) {
|
||||||
|
ratio = window.innerWidth / width;
|
||||||
|
}
|
||||||
|
} else if (height > window.innerHeight) {
|
||||||
|
ratio = window.innerHeight / height;
|
||||||
|
}
|
||||||
|
// Scale svg fit to zoom
|
||||||
|
isInit = false;
|
||||||
|
}
|
||||||
|
if (ratio) {
|
||||||
|
// body padding is 8px
|
||||||
|
svgEl.setAttribute("width", width * ratio - 16);
|
||||||
|
svgEl.setAttribute("height", height * ratio - 16);
|
||||||
|
}
|
||||||
|
|
||||||
d2ErrDiv.style.display = "none";
|
d2ErrDiv.style.display = "none";
|
||||||
}
|
}
|
||||||
if (msg.err) {
|
if (msg.err) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue