@@ -25,7 +25,7 @@ function init() {
2525
2626function handleExportClick ( ) {
2727 if ( Sketch . getASCII ( ) ) {
28- copyASCII ( ) ;
28+ toggleASCIIMenu ( ) ;
2929 } else {
3030 toggleMenu ( ) ;
3131 }
@@ -40,10 +40,60 @@ function updateExportButton() {
4040 }
4141
4242 if ( Sketch . getASCII ( ) ) {
43- exportBtn . textContent = " Copy" ;
44- exportMenu . style . display = "none" ;
43+ exportBtn . innerHTML = ' Copy<img src="assets/icons/down.svg" class="btn-icon" />' ;
44+ setupASCIIMenu ( ) ;
4545 } else {
4646 exportBtn . innerHTML = 'Export<img src="assets/icons/down.svg" class="btn-icon" />' ;
47+ setupExportMenu ( ) ;
48+ }
49+ }
50+
51+ function setupASCIIMenu ( ) {
52+ const exportMenu = document . getElementById ( "export-menu" ) ;
53+ exportMenu . innerHTML = `
54+ <div id="export-menu-ascii-extended" class="btn-menu-item">Extended ASCII</div>
55+ <div id="export-menu-ascii-standard" class="btn-menu-item">Standard ASCII</div>
56+ ` ;
57+
58+ document
59+ . getElementById ( "export-menu-ascii-extended" )
60+ . addEventListener ( "click" , copyASCIIExtended ) ;
61+ document
62+ . getElementById ( "export-menu-ascii-standard" )
63+ . addEventListener ( "click" , copyASCIIStandard ) ;
64+ }
65+
66+ function setupExportMenu ( ) {
67+ const exportMenu = document . getElementById ( "export-menu" ) ;
68+ exportMenu . innerHTML = `
69+ <div id="export-menu-png" class="btn-menu-item">PNG</div>
70+ <div id="export-menu-svg" class="btn-menu-item">SVG</div>
71+ <div id="export-menu-png-clipboard" class="btn-menu-item">PNG (copy)</div>
72+ <div id="export-menu-svg-clipboard" class="btn-menu-item">SVG (copy)</div>
73+ ` ;
74+
75+ document . getElementById ( "export-menu-png" ) . addEventListener ( "click" , exportPNG ) ;
76+ document . getElementById ( "export-menu-svg" ) . addEventListener ( "click" , exportSVG ) ;
77+
78+ if ( ! Mobile . is ( ) ) {
79+ document
80+ . getElementById ( "export-menu-png-clipboard" )
81+ . addEventListener ( "click" , exportPNGClipboard ) ;
82+ document
83+ . getElementById ( "export-menu-svg-clipboard" )
84+ . addEventListener ( "click" , exportSVGClipboard ) ;
85+ } else {
86+ document . getElementById ( "export-menu-png-clipboard" ) . style . display = "none" ;
87+ document . getElementById ( "export-menu-svg-clipboard" ) . style . display = "none" ;
88+ }
89+ }
90+
91+ function toggleASCIIMenu ( ) {
92+ const menu = document . getElementById ( "export-menu" ) ;
93+ if ( menu . style . display == "none" ) {
94+ menu . style . display = "block" ;
95+ } else {
96+ menu . style . display = "none" ;
4797 }
4898}
4999
@@ -226,21 +276,69 @@ async function exportPNGClipboard() {
226276 } , "image/png" ) ;
227277}
228278
229- async function copyASCII ( ) {
230- const ascii = Editor . getDiagramSVG ( ) ;
231- if ( ascii == "" ) {
232- Alert . show ( `Compile a diagram to copy` , 4000 ) ;
233- return ;
279+ async function copyASCIIExtended ( ) {
280+ hideMenu ( ) ;
281+ const ascii = await renderASCII ( "extended" ) ;
282+ if ( ! ascii ) return ;
283+
284+ try {
285+ await navigator . clipboard . writeText ( ascii ) ;
286+ Alert . show ( `Extended ASCII copied to clipboard` , 2000 , "success" ) ;
287+ } catch ( e ) {
288+ Alert . show ( `Failed to copy to clipboard: ${ e } ` , 4000 ) ;
234289 }
290+ }
291+
292+ async function copyASCIIStandard ( ) {
293+ hideMenu ( ) ;
294+ const ascii = await renderASCII ( "standard" ) ;
295+ if ( ! ascii ) return ;
235296
236297 try {
237298 await navigator . clipboard . writeText ( ascii ) ;
238- Alert . show ( `Copied to clipboard` , 2000 , "success" ) ;
299+ Alert . show ( `Standard ASCII copied to clipboard` , 2000 , "success" ) ;
239300 } catch ( e ) {
240301 Alert . show ( `Failed to copy to clipboard: ${ e } ` , 4000 ) ;
241302 }
242303}
243304
305+ async function renderASCII ( mode ) {
306+ const script = Editor . getScript ( ) ;
307+ if ( ! script ) {
308+ Alert . show ( `Compile a diagram to copy` , 4000 ) ;
309+ return null ;
310+ }
311+
312+ try {
313+ const compileRequest = {
314+ fs : { index : script } ,
315+ options : {
316+ layout : "elk" ,
317+ sketch : false ,
318+ ascii : true ,
319+ asciiMode : mode ,
320+ forceAppendix : false ,
321+ target : "" ,
322+ animateInterval : 0 ,
323+ salt : "" ,
324+ noXMLTag : false ,
325+ } ,
326+ } ;
327+
328+ const compiled = await window . d2 . compile ( compileRequest ) ;
329+ const renderOptions = {
330+ ascii : true ,
331+ asciiMode : mode ,
332+ center : true ,
333+ } ;
334+
335+ return await window . d2 . render ( compiled . diagram , renderOptions ) ;
336+ } catch ( err ) {
337+ Alert . show ( `Failed to render ASCII: ${ err . message } ` , 4000 ) ;
338+ return null ;
339+ }
340+ }
341+
244342export default {
245343 init,
246344 updateExportButton,
0 commit comments