1+
12<!DOCTYPE html>
23< meta lang ="en ">
34< meta charset ="UTF-8 ">
2526}
2627textarea , iframe {
2728 margin : 0px ;
29+ display : block;
30+ width : 100% ;
2831}
2932iframe {
3033 resize : vertical;
3134}
3235</ style >
3336< header >
3437< h1 > DWeb Scratchpad</ h1 >
35- < button
36- title ="Save your scratchpad to the dweb "
37- onclick ="saveDialog.showModal() "
38- > 💾</ button >
39- < button
40- title ="Load and edit a site from a link "
41- onclick ="loadDialog.showModal() "
42- > 📂</ button >
43- < button
44- title ="Look at your saved sites "
45- onclick ="window.showAllSaved() "
46- > 📃</ button >
47- < button
48- title ="Help "
49- onclick ="helpDialog.showModal() "
50- > ❔</ button >
38+ < button title ="Save your scratchpad to the dweb " onclick ="saveDialog.showModal() "> 💾</ button >
39+ < button title ="Load and edit a site from a link " onclick ="loadDialog.showModal() "> 📂</ button >
40+ < button title ="Look at your saved sites " onclick ="window.showAllSaved() "> 📃</ button >
41+ < button title ="Help " onclick ="helpDialog.showModal() "> ❔</ button >
5142</ header >
5243< main id ="mainContents ">
5344
5445< iframe id ="previewFrame "> </ iframe >
55- < textarea autofocus id ="htmlContent ">
56- < marquee > 🧑💻HTML content here📃</ marquee >
46+ < textarea autofocus ="" id ="htmlContent "> <marquee>🧑💻HTML content here📃</marquee>
5747</ textarea >
58- < textarea id ="cssContent ">
59- /*CSS here*/
48+ < textarea id ="cssContent "> /*CSS here*/
6049marquee {
6150 color: var(--ag-theme-primary);
6251 font-weight: bold;
6352 font-size: 3em;
6453}
6554</ textarea >
66- < textarea id ="jsContent ">
67- // JavaScript here
55+ < textarea id ="jsContent "> // JavaScript here
6856console.log("Hello World!")
6957</ textarea >
7058
7159</ main >
7260< dialog id ="saveDialog ">
7361< h2 > Save to the dweb</ h2 >
74- < form id ="saveForm ">
62+ < form id ="saveForm ">
7563 < label >
7664 Title
7765 < input id ="appTitle " name ="title " value ="Scratchpad App ">
@@ -89,6 +77,7 @@ <h2>Save to the dweb</h2>
8977 < select name ="saveType ">
9078 < option value ="hyper "> hyper://</ option >
9179 < option value ="ipfs "> ipfs://</ option >
80+ < option value ="ipns "> ipns://</ option >
9281 < option value ="download "> Download</ option >
9382 </ select >
9483 </ label >
@@ -185,17 +174,29 @@ <h2>How does this work?</h2>
185174 return await response . text ( )
186175}
187176
177+ async function getIPNSURL ( ) {
178+ const name = "dweb_scratchpad"
179+ const response = await fetch ( `ipns://localhost/?key=${ name } ` , { method : 'POST' } ) ;
180+ if ( ! response . ok ) {
181+ throw new Error ( `Failed to generate IPNS key: ${ response . statusText } ` ) ;
182+ }
183+ await response . text ( )
184+ return response . headers . get ( 'Location' )
185+ }
186+
188187async function saveAndPublish ( filename , saveType ) {
189188 const content = genPage ( )
190189
191- if ( saveType === 'download' ) {
192- const url = previewFrame . src
193- downloadDataUrl ( url , filename )
194- return url
195- } else if ( saveType === 'ipfs' ) {
196- return saveIPFS ( filename , content ) ;
197- } else {
198- return saveHyper ( filename , content ) ;
190+ if ( saveType === 'download' ) {
191+ const url = previewFrame . src
192+ downloadDataUrl ( url , filename )
193+ return url
194+ } else if ( saveType === 'ipfs' ) {
195+ return saveIPFS ( filename , content ) ;
196+ } else if ( saveType === 'ipns' ) {
197+ return saveIPNS ( filename , content ) ;
198+ } else {
199+ return saveHyper ( filename , content ) ;
199200 }
200201}
201202
@@ -210,7 +211,6 @@ <h2>How does this work?</h2>
210211 }
211212 await response . text ( )
212213 return response . headers . get ( 'Location' )
213- return url
214214}
215215
216216async function saveHyper ( filename , content ) {
@@ -227,6 +227,21 @@ <h2>How does this work?</h2>
227227 return url
228228}
229229
230+ async function saveIPNS ( filename , content ) {
231+ const rootURL = await getIPNSURL ( )
232+ const url = new URL ( filename , rootURL ) . href
233+ const response = await fetch ( url , {
234+ method : 'PUT' ,
235+ body : content
236+ } )
237+ if ( ! response . ok ) {
238+ throw new Error ( `Failed to upload ${ await response . text ( ) } ` )
239+ }
240+ await response . text ( )
241+ return url
242+ }
243+
244+
230245function updatePreview ( ) {
231246 if ( previewTimeout ) clearTimeout ( previewTimeout )
232247 setTimeout ( ( ) => {
0 commit comments