/ *
THIS IS A GENERATED / BUNDLED FILE BY ESBUILD
if you want to view the source , please visit the github repository of this plugin
* /
var _ _create = Object . create ;
var _ _defProp = Object . defineProperty ;
var _ _getOwnPropDesc = Object . getOwnPropertyDescriptor ;
var _ _getOwnPropNames = Object . getOwnPropertyNames ;
var _ _getProtoOf = Object . getPrototypeOf ;
var _ _hasOwnProp = Object . prototype . hasOwnProperty ;
var _ _markAsModule = ( target ) => _ _defProp ( target , "__esModule" , { value : true } ) ;
var _ _export = ( target , all ) => {
_ _markAsModule ( target ) ;
for ( var name in all )
_ _defProp ( target , name , { get : all [ name ] , enumerable : true } ) ;
} ;
var _ _reExport = ( target , module2 , desc ) => {
if ( module2 && typeof module2 === "object" || typeof module2 === "function" ) {
for ( let key of _ _getOwnPropNames ( module2 ) )
if ( ! _ _hasOwnProp . call ( target , key ) && key !== "default" )
_ _defProp ( target , key , { get : ( ) => module2 [ key ] , enumerable : ! ( desc = _ _getOwnPropDesc ( module2 , key ) ) || desc . enumerable } ) ;
}
return target ;
} ;
var _ _toModule = ( module2 ) => {
return _ _reExport ( _ _markAsModule ( _ _defProp ( module2 != null ? _ _create ( _ _getProtoOf ( module2 ) ) : { } , "default" , module2 && module2 . _ _esModule && "default" in module2 ? { get : ( ) => module2 . default , enumerable : true } : { value : module2 , enumerable : true } ) ) , module2 ) ;
} ;
var _ _async = ( _ _this , _ _arguments , generator ) => {
return new Promise ( ( resolve , reject ) => {
var fulfilled = ( value ) => {
try {
step ( generator . next ( value ) ) ;
} catch ( e ) {
reject ( e ) ;
}
} ;
var rejected = ( value ) => {
try {
step ( generator . throw ( value ) ) ;
} catch ( e ) {
reject ( e ) ;
}
} ;
var step = ( x ) => x . done ? resolve ( x . value ) : Promise . resolve ( x . value ) . then ( fulfilled , rejected ) ;
step ( ( generator = generator . apply ( _ _this , _ _arguments ) ) . next ( ) ) ;
} ) ;
} ;
// src/main.ts
_ _export ( exports , {
default : ( ) => ShortcutLauncherPlugin
} ) ;
var import _obsidian3 = _ _toModule ( require ( "obsidian" ) ) ;
// src/SettingsTab.ts
var import _obsidian2 = _ _toModule ( require ( "obsidian" ) ) ;
// src/LauncherModal.ts
var import _obsidian = _ _toModule ( require ( "obsidian" ) ) ;
var LauncherModal = class extends import _obsidian . Modal {
constructor ( app , isEditing , commandName , shortcutName , inputTypes , separator , onSave ) {
super ( app ) ;
this . isEditing = isEditing ;
this . commandName = commandName ;
this . shortcutName = shortcutName ;
this . inputTypes = inputTypes ;
this . separator = separator ;
this . onSave = onSave ;
}
onOpen ( ) {
const { contentEl } = this ;
contentEl . empty ( ) ;
contentEl . createEl ( "h2" , {
text : this . isEditing ? "Edit Launcher" : "New Launcher"
} ) ;
new import _obsidian . Setting ( contentEl ) . setName ( "Command Name" ) . setDesc ( "The Obsidian command name." ) . addText ( ( text ) => text . setPlaceholder ( "Command Name" ) . setValue ( this . commandName ) . onChange ( ( value ) => this . commandName = value ) ) ;
new import _obsidian . Setting ( contentEl ) . setName ( "Shortcut Name" ) . setDesc ( "The name of the shortcut to launch." ) . addText ( ( text ) => text . setPlaceholder ( "Shortcut Name" ) . setValue ( this . shortcutName ) . onChange ( ( value ) => this . shortcutName = value ) ) ;
new import _obsidian . Setting ( contentEl ) . setName ( "Input Type" ) . setDesc ( "The initial input into the shortcut." ) . addDropdown ( ( dropdown ) => dropdown . addOptions ( {
"Selected Text" : "Selected Text" ,
"Selected Link/Embed Contents" : "Selected Link/Embed Contents" ,
"Current Paragraph" : "Current Paragraph" ,
"Entire Document" : "Entire Document" ,
"Link to Document" : "Link to Document" ,
"Document Name" : "Document Name" ,
"Document Path" : "Document Path" ,
Multiple : "Multiple"
} ) . setValue ( this . inputTypes [ 0 ] ) . onChange ( ( value ) => {
if ( value == "Multiple" ) {
this . inputTypes = [
"Multiple" ,
"Document Name" ,
"Selected Text"
] ;
} else {
this . inputTypes = [ value ] ;
}
this . onOpen ( ) ;
} ) ) ;
if ( this . inputTypes . length > 1 ) {
this . inputTypes . filter ( ( _ , index ) => index > 0 ) . forEach ( ( inputType , index ) => {
let setting = new import _obsidian . Setting ( contentEl ) . setName ( ` Input Type # ${ index + 1 } ` ) . addDropdown ( ( dropdown ) => dropdown . addOptions ( {
"Selected Text" : "Selected Text" ,
"Selected Link/Embed Contents" : "Selected Link/Embed Contents" ,
"Current Paragraph" : "Current Paragraph" ,
"Entire Document" : "Entire Document" ,
"Link to Document" : "Link to Document" ,
"Document Name" : "Document Name" ,
"Document Path" : "Document Path"
} ) . setValue ( inputType ) . onChange ( ( value ) => {
this . inputTypes [ index + 1 ] = value ;
} ) ) ;
if ( index > 1 ) {
setting . addButton ( ( button ) => button . setIcon ( "trash" ) . setWarning ( ) . onClick ( ( ) => {
this . inputTypes . splice ( index + 1 , 1 ) ;
this . onOpen ( ) ;
} ) ) ;
}
} ) ;
new import _obsidian . Setting ( contentEl ) . addButton ( ( button ) => button . setButtonText ( "Add Input" ) . onClick ( ( ) => {
this . inputTypes . push ( "Selected Text" ) ;
this . onOpen ( ) ;
} ) ) ;
new import _obsidian . Setting ( contentEl ) . setName ( "Separator" ) . setDesc ( "The separator to insert between input types." ) . addText ( ( text ) => text . setValue ( this . separator ) . onChange ( ( value ) => this . separator = value ) ) ;
}
new import _obsidian . Setting ( contentEl ) . addButton ( ( button ) => button . setButtonText ( "Save" ) . setCta ( ) . onClick ( ( ) => {
if ( ! this . commandName || this . commandName . length == 0 ) {
return new import _obsidian . Notice ( "Specify a command name." ) ;
}
if ( ! this . shortcutName || this . shortcutName . length == 0 ) {
return new import _obsidian . Notice ( "Specify a shortcut name." ) ;
}
this . onSave ( this . commandName , this . shortcutName , this . inputTypes , this . separator ) ;
this . close ( ) ;
} ) ) ;
}
onClose ( ) {
const { contentEl } = this ;
contentEl . empty ( ) ;
}
} ;
// src/SettingsTab.ts
var SettingsTab = class extends import _obsidian2 . PluginSettingTab {
constructor ( app , plugin ) {
super ( app , plugin ) ;
this . plugin = plugin ;
}
display ( ) {
const { containerEl } = this ;
containerEl . empty ( ) ;
containerEl . createEl ( "h2" , { text : "Shortcut Launchers" } ) ;
new import _obsidian2 . Setting ( containerEl ) . addButton ( ( button ) => button . setButtonText ( "New" ) . setCta ( ) . onClick ( ( ) => {
new LauncherModal ( this . app , false , "" , "" , [ "Selected Text" ] , "," , ( commandName , shortcutName , inputTypes , separator ) => {
this . plugin . settings . launchers . splice ( 0 , 0 , {
commandName ,
shortcutName ,
inputTypes ,
separator
} ) ;
this . plugin . saveSettings ( ) ;
this . display ( ) ;
} ) . open ( ) ;
} ) ) ;
this . plugin . settings . launchers . forEach ( ( launcher , index ) => {
new import _obsidian2 . Setting ( containerEl ) . setName ( launcher . commandName ) . setDesc ( ` ${ launcher . shortcutName } < ${ launcher . inputTypes [ 0 ] } ` ) . addButton ( ( button ) => button . setIcon ( "pencil" ) . onClick ( ( event ) => {
new LauncherModal ( this . app , true , launcher . commandName , launcher . shortcutName , launcher . inputTypes , launcher . separator , ( commandName , shortcutName , inputTypes , separator ) => {
this . plugin . settings . launchers [ index ] . commandName = commandName ;
this . plugin . settings . launchers [ index ] . shortcutName = shortcutName ;
this . plugin . settings . launchers [ index ] . inputTypes = inputTypes ;
this . plugin . settings . launchers [ index ] . separator = separator ;
this . plugin . saveSettings ( ) ;
this . display ( ) ;
} ) . open ( ) ;
} ) ) . addButton ( ( button ) => button . setIcon ( "trash" ) . setWarning ( ) . onClick ( ( ) => {
this . plugin . settings . launchers . splice ( index , 1 ) ;
this . plugin . saveSettings ( ) ;
this . display ( ) ;
} ) ) ;
} ) ;
}
} ;
// src/main.ts
var DEFAULT _SETTINGS = {
launchers : [ ]
} ;
var ShortcutLauncherPlugin = class extends import _obsidian3 . Plugin {
constructor ( ) {
super ( ... arguments ) ;
this . registeredCommands = [ ] ;
}
onload ( ) {
return _ _async ( this , null , function * ( ) {
yield this . loadSettings ( ) ;
this . addSettingTab ( new SettingsTab ( this . app , this ) ) ;
yield this . createCommands ( ) ;
} ) ;
}
createCommands ( ) {
return _ _async ( this , null , function * ( ) {
this . registeredCommands = [ ] ;
this . settings . launchers . forEach ( ( launcher ) => {
this . registeredCommands . push ( this . addCommand ( {
id : launcher . commandName . replace ( /\s+/g , "-" ) . toLowerCase ( ) ,
name : launcher . commandName ,
checkCallback : ( checking ) => {
if ( checking ) {
return this . check ( launcher ) ;
}
var inputs = [ ] ;
launcher . inputTypes . filter ( ( inputType ) => inputType != "Multiple" ) . reduce ( ( promise , inputType ) => _ _async ( this , null , function * ( ) {
var _a , _b ;
yield promise ;
var text = "" ;
if ( inputType == "Selected Text" ) {
let view = this . app . workspace . activeLeaf . view ;
if ( view . getSelection ) {
text = view . getSelection ( ) ;
}
} else if ( inputType == "Selected Link/Embed Contents" ) {
let metadataCache = this . app . metadataCache . getFileCache ( this . app . workspace . getActiveFile ( ) ) ;
let linksAndEmbeds = ( ( _a = metadataCache . links ) != null ? _a : [ ] ) . concat ( ( _b = metadataCache . embeds ) != null ? _b : [ ] ) ;
let mdView = this . app . workspace . getActiveViewOfType ( import _obsidian3 . MarkdownView ) ;
let cursorOffset = mdView . editor . posToOffset ( mdView . editor . getCursor ( ) ) ;
let matchingLinkOrEmbed = linksAndEmbeds . filter ( ( cached ) => cached . position . start . offset <= cursorOffset && cached . position . end . offset >= cursorOffset ) ;
if ( matchingLinkOrEmbed . length > 0 ) {
let linkpath = ( 0 , import _obsidian3 . getLinkpath ) ( matchingLinkOrEmbed [ 0 ] . link ) ;
let linkedFile = this . app . metadataCache . getFirstLinkpathDest ( linkpath , this . app . workspace . getActiveFile ( ) . path ) ;
if ( ! matchingLinkOrEmbed [ 0 ] . link . contains ( "." ) || linkpath . endsWith ( ".md" ) || linkpath . endsWith ( "txt" ) ) {
text = yield this . app . vault . read ( linkedFile ) ;
} else {
let binary = yield this . app . vault . readBinary ( linkedFile ) ;
text = arrayBufferToBase64 ( binary ) ;
}
} else {
new import _obsidian3 . Notice ( "Could not find current link or embed" ) ;
}
} else if ( inputType == "Current Paragraph" ) {
let metadataCache = this . app . metadataCache . getFileCache ( this . app . workspace . getActiveFile ( ) ) ;
if ( ! metadataCache . sections ) {
new import _obsidian3 . Notice ( "Could not find current paragraph" ) ;
}
let mdView = this . app . workspace . getActiveViewOfType ( import _obsidian3 . MarkdownView ) ;
let cursorOffset = mdView . editor . posToOffset ( mdView . editor . getCursor ( ) ) ;
let matchingSection = metadataCache . sections . filter ( ( section ) => section . position . start . offset <= cursorOffset && section . position . end . offset >= cursorOffset ) ;
if ( matchingSection . length > 0 ) {
let documentContents = yield this . app . vault . read ( this . app . workspace . getActiveFile ( ) ) ;
text = documentContents . substring ( matchingSection [ 0 ] . position . start . offset , matchingSection [ 0 ] . position . end . offset ) ;
} else {
new import _obsidian3 . Notice ( "Could not find current paragraph" ) ;
}
} else if ( inputType == "Entire Document" ) {
text = yield this . app . vault . read ( this . app . workspace . getActiveFile ( ) ) ;
} else if ( inputType == "Link to Document" ) {
text = ` obsidian://open?vault= ${ encodeURIComponent ( this . app . vault . getName ( ) ) } &file= ${ encodeURIComponent ( this . app . workspace . getActiveFile ( ) . path ) } ` ;
} else if ( inputType == "Document Name" ) {
text = this . app . workspace . getActiveFile ( ) . basename ;
} else if ( inputType == "Document Path" ) {
text = this . app . workspace . getActiveFile ( ) . path ;
}
inputs . push ( text ) ;
} ) , Promise . resolve ( ) ) . then ( ( ) => {
if ( import _obsidian3 . Platform . isMobileApp ) {
window . open ( ` shortcuts://run-shortcut?name= ${ encodeURIComponent ( launcher . shortcutName ) } &input=text&text= ${ encodeURIComponent ( inputs . join ( launcher . separator ) ) } ` ) ;
} else {
let tempFilePath = require ( "path" ) . join ( require ( "os" ) . tmpdir ( ) , "obsidian-shortcut-launcher-temp-input" ) ;
let escapedShortcutName = launcher . shortcutName . replace ( /["\\]/g , "\\$&" ) ;
let fs = require ( "fs" ) ;
fs . writeFile ( tempFilePath , inputs . join ( launcher . separator ) , ( ) => {
require ( "child_process" ) . exec ( ` shortcuts run " ${ escapedShortcutName } " -i ${ tempFilePath } ` , ( ) => _ _async ( this , null , function * ( ) {
fs . unlink ( tempFilePath , ( ) => {
} ) ;
} ) ) ;
} ) ;
}
} ) ;
return true ;
}
} ) ) ;
} ) ;
} ) ;
}
check ( launcher ) {
var _a , _b ;
if ( launcher . inputTypes . contains ( "Selected Text" ) ) {
let view = this . app . workspace . activeLeaf . view ;
if ( ! view . getSelection ) {
return false ;
}
}
if ( launcher . inputTypes . contains ( "Selected Link/Embed Contents" ) ) {
let mdView = this . app . workspace . getActiveViewOfType ( import _obsidian3 . MarkdownView ) ;
if ( ! mdView || mdView . getMode ( ) !== "source" ) {
return false ;
}
let metadataCache = this . app . metadataCache . getFileCache ( this . app . workspace . getActiveFile ( ) ) ;
let linksAndEmbeds = ( ( _a = metadataCache . links ) != null ? _a : [ ] ) . concat ( ( _b = metadataCache . embeds ) != null ? _b : [ ] ) ;
if ( typeof mdView . editor == "undefined" ) {
return false ;
}
let cursorOffset = mdView . editor . posToOffset ( mdView . editor . getCursor ( ) ) ;
let matchingLinkOrEmbed = linksAndEmbeds . filter ( ( cached ) => cached . position . start . offset <= cursorOffset && cached . position . end . offset >= cursorOffset ) ;
if ( matchingLinkOrEmbed . length == 0 ) {
return false ;
}
}
if ( launcher . inputTypes . contains ( "Current Paragraph" ) ) {
let mdView = this . app . workspace . getActiveViewOfType ( import _obsidian3 . MarkdownView ) ;
if ( ! mdView || mdView . getMode ( ) !== "source" ) {
return false ;
}
}
if ( launcher . inputTypes . contains ( "Entire Document" ) || launcher . inputTypes . contains ( "Link to Document" ) || launcher . inputTypes . contains ( "Document Name" ) || launcher . inputTypes . contains ( "Document Path" ) ) {
if ( ! this . app . workspace . getActiveFile ( ) ) {
return false ;
}
}
return true ;
}
loadSettings ( ) {
return _ _async ( this , null , function * ( ) {
this . settings = Object . assign ( { } , DEFAULT _SETTINGS , yield this . loadData ( ) ) ;
} ) ;
}
saveSettings ( ) {
return _ _async ( this , null , function * ( ) {
yield this . saveData ( this . settings ) ;
this . registeredCommands . forEach ( ( command ) => {
this . app . commands . removeCommand ( command . id ) ;
} ) ;
this . registeredCommands = [ ] ;
yield this . createCommands ( ) ;
} ) ;
}
} ;
function arrayBufferToBase64 ( buffer ) {
var binary = "" ;
var bytes = new Uint8Array ( buffer ) ;
var len = bytes . byteLength ;
for ( var i = 0 ; i < len ; i ++ ) {
binary += String . fromCharCode ( bytes [ i ] ) ;
}
return window . btoa ( binary ) ;
}