2018-11-21 16:07:57 +00:00
import QtQml 2.0
import QOwnNotesTypes 1.0
import com . qownnotes . noteapi 1.0
QtObject {
2018-11-21 17:47:21 +00:00
property string idMarker ;
2018-11-28 16:49:24 +00:00
property bool wikiLinks ;
2018-11-21 17:47:21 +00:00
property variant settingsVariables: [
{
"identifier" : "idMarker" ,
"name" : "Zettelkasten ID Marker" ,
"description" : "The in-text marker denoting that a Zettel ID is following. Make sure to use something which is not ambiguous, and does not appear in any other way than to actually denote the Zettel ID. Multiple characters are fine (as in the default '§§')" ,
"type" : "string" ,
"default" : "§§" ,
2018-11-28 16:49:24 +00:00
} ,
{
"identifier" : "wikiLinks" ,
"name" : "Default to [[WikiLinks]]" ,
"description" : "By default, links will take the form of standard markdown links. This will make automatically created links between Zettel take the form of wiki-links instead. The go to link function understands both types, regardless of this choice." ,
"type" : "boolean" ,
"default" : "false" ,
} ,
2018-11-21 17:47:21 +00:00
]
2018-11-21 16:07:57 +00:00
function init ( ) {
script . registerCustomAction ( "zettelCreate" , "Create Zettelkasten Zettel" , "Zettel" , "address-book-new" ) ;
script . registerCustomAction ( "manualLink" , "Insert Zettelkasten Link" , "InsertLink" , "insert-link" ) ;
script . registerCustomAction ( "jumpToId" , "Follow Zettelkasten Link" , "Link" , "go-next" ) ;
}
function customActionInvoked ( identifier ) {
switch ( identifier ) {
case "zettelCreate" :
onZettelCreateClicked ( ) ;
break ;
case "jumpToId" :
onJumpToZettelClicked ( ) ;
break ;
case "manualLink" :
onInsertLinkClicked ( ) ;
break ;
}
}
// ----------------------------
// ----- BUTTON FUNCTIONS -----
// ----------------------------
function onZettelCreateClicked ( ) {
let uid = exactDate ( ) ;
let selected = script . noteTextEditSelectedText ( ) ;
// Link the from the old Zettel to the new one if anything was selected
if ( selected !== "" ) {
createLinkInPlace ( uid , selected ) ;
}
// Create a Zettel
createZettel ( uid , selected ) ;
}
function onInsertLinkClicked ( ) {
let selected = script . noteTextEditSelectedText ( ) ;
let clipboard = script . clipboard ( ) ;
let uid = null
if ( verifyZettelId ( clipboard ) ) {
uid = verifyZettelId ( clipboard ) ;
} else {
let otherNote = zettelSelectorDialog ( true ) ;
uid = extractZettelIdFromString ( otherNote . noteText ) ;
}
if ( uid == null ) {
return ;
}
createLinkInPlace ( uid , selected )
}
function onJumpToZettelClicked ( ) {
2018-11-28 16:49:24 +00:00
let selected = verifyZettelId ( script . noteTextEditSelectedText ( ) , true ) ? verifyZettelId ( script . noteTextEditSelectedText ( ) , true ) : verifyZettelId ( script . noteTextEditCurrentWord ( true ) , true ) ;
2018-11-21 16:07:57 +00:00
2018-11-28 16:49:24 +00:00
if ( selected ) {
setNoteToZettelIdOrElse ( selected , function ( uid ) { script . informationMessageBox ( "Zettel " + uid + " does not exist." ) } ) ;
2018-11-21 16:07:57 +00:00
} else {
2018-11-28 16:49:24 +00:00
script . setCurrentNote ( zettelSelectorDialog ( true ) ) ;
2018-11-21 16:07:57 +00:00
}
}
// ----------------------------
// --- Autocomplete Linking ---
// ----------------------------
function autocompletionHook ( ) {
2018-11-28 16:49:24 +00:00
let uid = verifyZettelId ( script . noteTextEditCurrentWord ( true ) , true ) ;
2018-11-21 16:07:57 +00:00
if ( uid == false ) {
return [ ] ;
}
2018-11-28 16:49:24 +00:00
setNoteToZettelIdOrElse ( uid ) ;
2018-11-21 16:07:57 +00:00
return [ ] ;
}
// ----------------------------
// ---- Internal Functions ----
// ----------------------------
function zettelSelectorDialog ( editableTextBox , zettelArray ) {
if ( zettelArray == null ) {
zettelArray = [ ] ;
2018-11-21 17:47:21 +00:00
script . fetchNoteIdsByNoteTextPart ( idMarker ) . forEach ( function ( noteId ) {
2018-11-21 16:07:57 +00:00
let note = script . fetchNoteById ( noteId ) ;
zettelArray . push ( note . name + " --id:" + note . id ) ;
} ) ;
}
let selected = script . inputDialogGetItem ( "Zettel" , "Select a Zettel" , zettelArray , 0 , editableTextBox ) ;
2018-11-28 16:49:24 +00:00
// FIXME dont display id in names - rather, go getNotebyName(name) -> (get its id) -> set note to it
2018-11-21 16:07:57 +00:00
return script . fetchNoteById ( selected . substring ( selected . search ( /--id:/ ) + 5 ) ) ;
}
function extractZettelIdFromString ( text ) {
2018-11-21 17:47:21 +00:00
let regex = new RegExp ( idMarker + '\\d{14}\\s' )
let markerpos = text . search ( regex ) + 2 ;
2018-11-21 16:07:57 +00:00
if ( markerpos == - 1 ) {
return ;
}
return text . substring ( markerpos , markerpos + 14 ) ;
}
2018-11-28 16:49:24 +00:00
function stripZettelId ( link ) {
return link . replace ( /\D*/g , "" ) ;
}
function verifyZettelId ( id , shouldStrip ) {
if ( shouldStrip ) id = stripZettelId ( id ) ;
if ( id . match ( /^\d{14}$/ ) != null ) {
2018-11-21 16:07:57 +00:00
return id ;
2018-11-28 16:49:24 +00:00
} else if ( id . match ( new RegExp ( idMarker + '^\\d{14}$' ) ) != null ) {
return id . substring ( 2 ) ;
2018-11-21 16:07:57 +00:00
} else {
return false ;
}
}
function getNoteByZettelId ( uid ) {
2018-11-21 17:47:21 +00:00
let noteId = script . fetchNoteIdsByNoteTextPart ( idMarker + uid ) ;
2018-11-21 16:07:57 +00:00
if ( noteId . length == 0 ) {
return ;
}
return script . fetchNoteById ( noteId [ 0 ] ) ;
}
2018-11-28 16:49:24 +00:00
function setNoteToZettelIdOrElse ( uid , orElse ) {
let zettel = getNoteByZettelId ( uid ) ;
if ( zettel == null ) {
if ( orElse != null ) orElse ( uid ) ;
return ;
}
script . setCurrentNote ( zettel ) ;
}
2018-11-21 16:07:57 +00:00
function createLinkInPlace ( uid , selected ) {
2018-11-28 16:49:24 +00:00
wikiLinks ? script . noteTextEditWrite ( "[[" + uid + "]] " + selected ) : script . noteTextEditWrite ( "[" + selected + "](" + uid + ")" ) ;
2018-11-21 16:07:57 +00:00
}
function createZettel ( uid , headline ) {
// Create Headline - empty if nothing selected
if ( headline == "" ) {
headline = "Zettel"
}
let text = headline . toString ( ) + "\n" ;
// Create Headline Separator
text += "===============\n" ;
// Create uid tag
2018-11-21 17:47:21 +00:00
text += idMarker + uid . toString ( ) + "\n\n" ;
2018-11-21 16:07:57 +00:00
// Create content unit (i.e. space)
text += "\n\n\n\n\n" ;
// Create Related unit - FIXME add option to automatically link back to the card this was created from
text += "## Related\n\n\n\n" ;
// Create Source unit
text += "## Reference\n\n" ;
script . createNote ( text ) ;
return script . currentNote ( ) ;
}
function exactDate ( ) {
let date = new Date ( ) ;
return ( ""
+ date . getUTCFullYear ( ) . toString ( )
+ padToLen2 ( ( date . getUTCMonth ( ) + 1 ) . toString ( ) )
+ padToLen2 ( date . getUTCDate ( ) . toString ( ) )
+ padToLen2 ( date . getUTCHours ( ) . toString ( ) )
+ padToLen2 ( date . getUTCMinutes ( ) . toString ( ) )
+ padToLen2 ( date . getUTCSeconds ( ) . toString ( ) )
) ;
}
function padToLen2 ( text ) {
return ( "00" + text ) . slice ( - 2 )
}
2018-11-28 16:49:24 +00:00
}