import { BlockLocation , CraftBlock , CraftBlockUpdate , CraftTextBlock , CraftTextRun , IndexLocation , } from "@craftdocs/craft-extension-api" ; function initColorSchemeHandler ( ) { craft . env . setListener ( ( env ) => { if ( env . colorScheme === "dark" ) { document . documentElement . classList . add ( "dark" ) ; } else { document . documentElement . classList . remove ( "dark" ) ; } } ) ; } function renderStories ( stories ) { const articlesDiv = document . getElementById ( "articles" ) ; articlesDiv . innerHTML = "" ; stories . forEach ( ( element ) => { const htmlToInsert = createHTMLForStory ( element ) ; articlesDiv . appendChild ( htmlToInsert ) ; } ) ; } function createHTMLForStory ( story ) { let numberOfCommens = 0 ; if ( story [ "kids" ] != undefined ) { numberOfCommens = story [ "kids" ] . length ; } const storyUrl = story [ "url" ] ; const id = story [ "id" ] ; const discusssionUrl = `https://news.ycombinator.com/item?id=${ id }` ; const htmlString = `
  <div class="gridItem">
    <h3 class="text-sm font-medium select-none">
      ${ story [ "title" ] }
    </h3>
    ${ story [ "url" ] != null ? `<div>
      <a href= "" onclick = "window.openURL('${ storyUrl }'); return false;" class="underline text-sm text-secondaryText dark:text-secondaryText-dark" >
        ${ new URL ( story [ "url" ] ) . hostname }
      </a>
    </div>` : "" }
    <div>
      <a href="" onclick="window.openURL('${ discusssionUrl }'); return false;" class="text-xs text-secondaryText dark:text-secondaryText-dark">
        ${ story [ "by" ] } | ${ story [ "score" ] } points | ${ numberOfCommens } comments
      </a>
    </div>
  </div>` ; return new DOMParser ( ) . parseFromString ( htmlString , "text/html" ) . body . childNodes [ 0 ] ; } let loadedStoryDetails = [ ] ; function initInsertButtonHandler ( ) { document . getElementById ( "insert_button" ) . onclick = async ( ) => { console . log ( "Insert, " + loadStoryDetails . length ) ; const items = loadedStoryDetails . map ( ( element ) => { let numberOfComments = 0 ; if ( element [ "kids" ] != undefined ) { numberOfComments = element [ "kids" ] . length ; } const discusssionUrl = `https://news.ycombinator.com/item?id=${ element [ "id" ] }` ; const articleUrl = element [ "url" ] ; const content = articleUrl != null ? [ { text : element [ "title" ] , isBold : true } , { text : "\n" } , { text : new URL ( element [ "url" ] ) . hostname , isCode : true , link : { url : element [ "url" ] , type : "url" } , } , { text : " " } , { text : `↑ ${ element [ "score" ] }` , isCode : true , link : { url : discusssionUrl , type : "url" } , } , ] : [ { text : element [ "title" ] , isBold : true } , { text : "\n" } , { text : " " } , { text : `↑ ${ element [ "score" ] }` , isCode : true , link : { url : discusssionUrl , type : "url" } , } , ] ; const branchBlock = craft . blockFactory . textBlock ( { content , listStyle : { type : "numbered" } , } ) ; return branchBlock ; } ) ; await craft . dataApi . addBlocks ( items ) ; } ; } async function loadTopStoryIds ( ) { let ids = [ ] ; try { const data = await fetch ( "https://hacker-news.firebaseio.com/v0/topstories.json" ) . then ( ( res ) => res . json ( ) ) ; ids = data ; } catch ( err ) { console . error ( err ) ; } let top10ids = ids . slice ( 0 , 20 ) ; return top10ids ; } async function loadStoryDetails ( id ) { let retData = [ ] ; try { const data = await fetch ( `https://hacker-news.firebaseio.com/v0/item/${ id }.json` ) . then ( ( res ) => res . json ( ) ) ; retData = data ; } catch ( err ) { console . error ( err ) ; } return retData ; } async function loadTop10StoryDetails ( ) { let ids = await loadTopStoryIds ( ) ; const promises = ids . map ( ( storyId ) => loadStoryDetails ( storyId ) ) ; const storyDetails = await Promise . all ( promises ) ; let storyTypeStories = storyDetails . filter ( ( story ) => story [ "type" ] == "story" ) ; return storyTypeStories . slice ( 0 , 10 ) ; } async function loadAndRenderStories ( ) { loadedStoryDetails = await loadTop10StoryDetails ( ) ; renderStories ( loadedStoryDetails ) ; document . getElementById ( "spinner" ) . style . visibility = "hidden" ; document . getElementById ( "spinner" ) . style . display = "none" ; document . getElementById ( "footer" ) . style . display = "block" ; } function addCustomWindowOpen ( ) { window [ "openURL" ] = function ( url ) { console . log ( "openURL" ) ; craft . editorApi . openURL ( url ) ; } ; } export function initApp ( ) { initColorSchemeHandler ( ) ; initInsertButtonHandler ( ) ; addCustomWindowOpen ( ) ; loadAndRenderStories ( ) ; }