Skip to content

Instantly share code, notes, and snippets.

@Akjosch
Created May 6, 2020 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Akjosch/19c6e32ffd6330bded4c65d1413e78a2 to your computer and use it in GitHub Desktop.
Save Akjosch/19c6e32ffd6330bded4c65d1413e78a2 to your computer and use it in GitHub Desktop.
Slightly enhanced link and button macros for SugarCube 2
/* Added options to add a pop-up, a custom class a custom ID for styling */
Macro.add(['ibutton', 'ilink'], {
isAsync : true,
tags : ["comment"],
handler() {
if (this.args.length === 0) {
return this.error(`no ${this.name === 'ibutton' ? 'button' : 'link'} text specified`);
}
const $link = jQuery(document.createElement(this.name === 'ibutton' ? 'button' : 'a'));
/** @type {string|undefined} */
let passage = undefined;
/** @type {string[]} */
let extraClasses = [];
/** @type {string|undefined} */
let extraId;
if (typeof this.args[0] === 'object') {
if (this.args[0].isImage) {
// Argument was in wiki image syntax.
const $image = jQuery(document.createElement('img'))
.attr('src', this.args[0].source)
.appendTo($link);
if (this.args[0].hasOwnProperty('passage')) {
$image.attr('data-passage', this.args[0].passage);
}
if (this.args[0].hasOwnProperty('title')) {
$image.attr('title', this.args[0].title);
}
if (this.args[0].hasOwnProperty('align')) {
$image.attr('align', this.args[0].align);
}
passage = this.args[0].link;
} else {
// Argument was in wiki link syntax.
$link.append(document.createTextNode(this.args[0].text));
passage = this.args[0].link;
}
} else {
// Argument was simply the link text.
$link.wikiWithOptions({ profile : 'core' }, this.args[0]);
let passageIdx = 1;
while(this.args.length > passageIdx) {
let val = String(this.args[passageIdx]);
if(val.startsWith(".")) {
extraClasses.push(val.substring(1));
} else if(val.startsWith("#")) {
extraId = val.substring(1);
} else {
passage = val;
}
++ passageIdx;
}
}
if (passage != null) { // lazy equality for null
$link.attr('data-passage', passage);
if (Story.has(passage)) {
$link.addClass('link-internal');
if (Config.addVisitedLinkClass && State.hasPlayed(passage)) {
$link.addClass('link-visited');
}
}
else {
$link.addClass('link-broken');
}
}
else {
$link.addClass('link-internal');
}
extraClasses.filter((c) => !!c).forEach((c) => $link.addClass(c));
extraId && $link.attr("id", extraId);
$link
.addClass(`macro-${this.name}`)
.ariaClick({
namespace : '.macros',
one : passage != null // lazy equality for null
}, this.createShadowWrapper(
this.payload[0].contents !== ''
? () => Wikifier.wikifyEval(this.payload[0].contents.trim())
: null,
passage != null // lazy equality for null
? () => Engine.play(passage)
: null
))
.appendTo(this.output);
}
});
@Tranberry
Copy link

are you still around the twine community Akjosch?

@Akjosch
Copy link
Author

Akjosch commented Mar 17, 2021

are you still around the twine community Akjosch?

Sure I am. I'm not overly active, but if you have comments on my gists or projects here, I read them all and react if needed. And you can @ me in Twine discord too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment