import { DAILY_NOTE_PAGE_TITLE_REGEX , parseRoamDate , toRoamDateUid , } from "./date" ; import { getActiveUids , getUidsFromId } from "./dom" ; import { InputTextNode } from "./types" ; export const updateActiveBlock = ( text ) => window . roamAlphaAPI . updateBlock ( { block : { uid : getActiveUids ( ) . blockUid , string : text , } , } ) ; export const clearBlockById = ( id ) => window . roamAlphaAPI . updateBlock ( { block : { uid : getUidsFromId ( id ) . blockUid , string : "" , } , } ) ; export const clearBlockByUid = ( uid ) => window . roamAlphaAPI . updateBlock ( { block : { uid , string : "" , } , } ) ; export const createBlock = ( { node : { text , children = [ ] , uid = window . roamAlphaAPI . util . generateUID ( ) , heading , viewType , textAlign , open = true , } , parentUid , order = 0 , } ) => { window . roamAlphaAPI . createBlock ( { location : { "parent-uid" : parentUid , order } , block : { uid , string : text , heading , "text-align" : textAlign , "children-view-type" : viewType , open , } , } ) ; children . forEach ( ( n , o ) => createBlock ( { node : n , parentUid : uid , order : o } ) ) ; if ( ! open ) window . roamAlphaAPI . updateBlock ( { block : { uid , open : false } } ) ; return uid ; } ; export const createPage = ( { title , tree = [ ] , } ) : string => { const uid = DAILY_NOTE_PAGE_TITLE_REGEX . test ( title ) ? toRoamDateUid ( parseRoamDate ( title ) ) : window . roamAlphaAPI . util . generateUID ( ) ; window . roamAlphaAPI . createPage ( { page : { title , uid } } ) ; tree . forEach ( ( node , order ) => createBlock ( { node , parentUid : uid , order } ) ) ; return uid ; } ; export const updateBlock = ( { text , uid , heading , textAlign , viewType , open , } ) => { window . roamAlphaAPI . updateBlock ( { block : { string : text , uid , heading , "text-align" : textAlign , "children-view-type" : viewType , open , } , } ) ; return uid ; } ; export const deleteBlock = ( uid ) => { window . roamAlphaAPI . deleteBlock ( { block : { uid } } ) ; return uid ; } ; export const openBlockInSidebar = ( blockUid ) : boolean | void => window . roamAlphaAPI . ui . rightSidebar . getWindows ( ) . some ( ( w ) => w . type === "block" && w [ "block-uid" ] === blockUid ) ? window . roamAlphaAPI . ui . rightSidebar . open ( ) : window . roamAlphaAPI . ui . rightSidebar . addWindow ( { window : { type : "block" , "block-uid" : blockUid , } , } ) ;