Add download button

This commit is contained in:
Nathan McRae
2025-08-27 21:15:20 -07:00
parent 5e795ac9f3
commit 6a4a0f5719
4 changed files with 92 additions and 66 deletions

View File

@@ -17,6 +17,8 @@
Ternary Graph Generator Ternary Graph Generator
</div> </div>
<hr/> <hr/>
<div id="main-content">
<div id="controls">
<div class="group"> <div class="group">
<h3>Axis 1</h3> <h3>Axis 1</h3>
<div class="input"> <div class="input">
@@ -81,8 +83,12 @@
<button id="tick-size-down"></button> <button id="tick-size-down"></button>
<button id="tick-size-up"></button> <button id="tick-size-up"></button>
</div> </div>
<button onclick="downloadSVG">Download SVG</button> <a id="download-button">
<button>Download SVG</button>
</a>
</div>
<div id="svg-container" class="my-class"></div> <div id="svg-container" class="my-class"></div>
</div>
</body> </body>
<script src="index.js"></script> <script src="index.js"></script>
</html> </html>

View File

@@ -19,6 +19,7 @@ to generate this file without the comments in this block.
, "exceptions" , "exceptions"
, "foldable-traversable" , "foldable-traversable"
, "integers" , "integers"
, "js-uri"
, "lists" , "lists"
, "maybe" , "maybe"
, "numbers" , "numbers"

View File

@@ -15,6 +15,7 @@ import Effect (Effect)
import Effect.Console (log) import Effect.Console (log)
import Effect.Class (liftEffect) import Effect.Class (liftEffect)
import Effect.Exception (throw) import Effect.Exception (throw)
import JSURI (encodeURIComponent)
import TernaryGraph (Dimension, svgTextID, ternaryGraph, ternaryGraphSvg, TextStyle, tickLabelStrings) import TernaryGraph (Dimension, svgTextID, ternaryGraph, ternaryGraphSvg, TextStyle, tickLabelStrings)
import TernaryGraph as TernaryGraph import TernaryGraph as TernaryGraph
import Web.DOM.Document (contentType import Web.DOM.Document (contentType
@@ -228,6 +229,15 @@ update = do
newNode <- importNode svgNode true document newNode <- importNode svgNode true document
appendChild newNode svgContainer appendChild newNode svgContainer
-- Set SVG content to download button
downloadButtonEl <- myGetElementById document "download-button"
encodedSVG <- case encodeURIComponent mySVG of
Nothing -> throw "Failed to encode SVG"
Just encoded -> pure encoded
let downloadText = "data:xml/svg;charset=utf-8," <> encodedSVG
Element.setAttribute "href" downloadText downloadButtonEl
Element.setAttribute "download" "ternary-graph.svg" downloadButtonEl
addUpdateListener :: Document -> String -> Effect Unit addUpdateListener :: Document -> String -> Effect Unit
addUpdateListener doc id = do addUpdateListener doc id = do
listener <- eventListener updateEvent listener <- eventListener updateEvent
@@ -251,8 +261,6 @@ registerInputIncButton doc inputID buttonID inc = do
element <- myGetElementById doc buttonID element <- myGetElementById doc buttonID
addEventListener (EventType "click") listener true (Element.toEventTarget element) addEventListener (EventType "click") listener true (Element.toEventTarget element)
main :: Effect Unit main :: Effect Unit
main = do main = do
w <- window w <- window

View File

@@ -6,6 +6,11 @@ button {
border-top-color: #E3E3E3; border-top-color: #E3E3E3;
} }
#controls {
display: flex;
flex-direction: column;
}
.group { .group {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
@@ -28,10 +33,16 @@ input[type="number"] {
} }
.input { .input {
display: flex;
flex-direction: row;
height: 1.5em; height: 1.5em;
margin-bottom: 0.4em; margin-bottom: 0.4em;
} }
#main-content {
flex-direction: row;
}
p { p {
margin: auto; margin: auto;
margin-right: 0.5em; margin-right: 0.5em;