add fit to screen to exported svg
This commit is contained in:
parent
e5e76372d6
commit
643aa13de9
4 changed files with 44 additions and 15 deletions
|
|
@ -7,3 +7,4 @@
|
||||||
#### Bugfixes ⛑️
|
#### Bugfixes ⛑️
|
||||||
|
|
||||||
- Appendix seperator line no longer added to PNG export when appendix doesn't exist. [#582](https://github.com/terrastruct/d2/pull/582)
|
- Appendix seperator line no longer added to PNG export when appendix doesn't exist. [#582](https://github.com/terrastruct/d2/pull/582)
|
||||||
|
- Watch mode only fits to screen on initial load. [#582](https://github.com/terrastruct/d2/pull/582)
|
||||||
|
|
|
||||||
|
|
@ -1096,6 +1096,9 @@ func embedFonts(buf *bytes.Buffer, fontFamily *d2fonts.FontFamily) {
|
||||||
buf.WriteString(`]]></style>`)
|
buf.WriteString(`]]></style>`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//go:embed fitToScreen.js
|
||||||
|
var fitToScreenScript string
|
||||||
|
|
||||||
// TODO minify output at end
|
// TODO minify output at end
|
||||||
func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) {
|
func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) {
|
||||||
var sketchRunner *d2sketch.Runner
|
var sketchRunner *d2sketch.Runner
|
||||||
|
|
@ -1124,6 +1127,8 @@ func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) {
|
||||||
]]>
|
]]>
|
||||||
</style>`, styleCSS, styleCSS2))
|
</style>`, styleCSS, styleCSS2))
|
||||||
|
|
||||||
|
buf.WriteString(fmt.Sprintf(`<script type="application/javascript"><![CDATA[%s]]></script>`, fitToScreenScript))
|
||||||
|
|
||||||
hasMarkdown := false
|
hasMarkdown := false
|
||||||
for _, s := range diagram.Shapes {
|
for _, s := range diagram.Shapes {
|
||||||
if s.Label != "" && s.Type == d2target.ShapeText {
|
if s.Label != "" && s.Type == d2target.ShapeText {
|
||||||
|
|
|
||||||
19
d2renderers/d2svg/fitToScreen.js
Normal file
19
d2renderers/d2svg/fitToScreen.js
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
window.addEventListener("DOMContentLoaded", () => {
|
||||||
|
const svgEl = document.querySelector("svg");
|
||||||
|
let width = parseInt(svgEl.getAttribute("width"), 10);
|
||||||
|
let height = parseInt(svgEl.getAttribute("height"), 10);
|
||||||
|
let ratio;
|
||||||
|
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
|
||||||
|
if (ratio) {
|
||||||
|
// body padding is 8px
|
||||||
|
svgEl.setAttribute("width", width * ratio - 16);
|
||||||
|
svgEl.setAttribute("height", height * ratio - 16);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
@ -11,6 +11,7 @@ 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;
|
||||||
ws.onopen = () => {
|
ws.onopen = () => {
|
||||||
reconnectDelay = 1000;
|
reconnectDelay = 1000;
|
||||||
console.info("watch websocket opened");
|
console.info("watch websocket opened");
|
||||||
|
|
@ -32,22 +33,25 @@ function init(reconnectDelay) {
|
||||||
// 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("svg");
|
if (isInit) {
|
||||||
let width = parseInt(svgEl.getAttribute("width"), 10);
|
const svgEl = d2SVG.querySelector("svg");
|
||||||
let height = parseInt(svgEl.getAttribute("height"), 10);
|
let width = parseInt(svgEl.getAttribute("width"), 10);
|
||||||
let ratio;
|
let height = parseInt(svgEl.getAttribute("height"), 10);
|
||||||
if (width > height) {
|
let ratio;
|
||||||
if (width > window.innerWidth) {
|
if (width > height) {
|
||||||
ratio = window.innerWidth / width;
|
if (width > window.innerWidth) {
|
||||||
|
ratio = window.innerWidth / width;
|
||||||
|
}
|
||||||
|
} else if (height > window.innerHeight) {
|
||||||
|
ratio = window.innerHeight / height;
|
||||||
}
|
}
|
||||||
} else if (height > window.innerHeight) {
|
// Scale svg fit to zoom
|
||||||
ratio = window.innerHeight / height;
|
if (ratio) {
|
||||||
}
|
// body padding is 8px
|
||||||
// Scale svg fit to zoom
|
svgEl.setAttribute("width", width * ratio - 16);
|
||||||
if (ratio) {
|
svgEl.setAttribute("height", height * ratio - 16);
|
||||||
// body padding is 8px
|
}
|
||||||
svgEl.setAttribute("width", width * ratio - 16);
|
isInit = false;
|
||||||
svgEl.setAttribute("height", height * ratio - 16);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
d2ErrDiv.style.display = "none";
|
d2ErrDiv.style.display = "none";
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue