-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
(function () { | ||
'use strict'; | ||
/* globals version, Macro, jQuery */ | ||
|
||
if (!version || !version.title || 'SugarCube' !== version.title || !version.major || version.major < 2) { | ||
throw new Error('<<dlg>> macro requires SugarCube 2.0 or greater, aborting load'); | ||
} | ||
|
||
function getOrCreate(id) { | ||
let element = jQuery('#' + id); | ||
if (element.length === 0) { | ||
element = jQuery(`<div id="${id}"></div>`); | ||
} | ||
|
||
return element; | ||
} | ||
|
||
function isNumber(arg) { | ||
return typeof arg === 'number'; | ||
} | ||
|
||
Macro.add('line', { | ||
tags: null, | ||
handler() { | ||
let level = 0; | ||
const parentLevel = this.contextSelect((context) => context.name === 'level'); | ||
if (parentLevel) { | ||
level = parseInt(parentLevel.args[0], 10); | ||
} | ||
|
||
let targetLevel = level + 1; | ||
if (isNumber(this.args[1])) { | ||
targetLevel = parseInt(this.args[1], 10); | ||
} else if (isNumber(this.args[2])) { | ||
targetLevel = parseInt(this.args[2], 10); | ||
} | ||
|
||
let dlgId = 'dlg'; | ||
const parentDlg = this.contextSelect((context) => context.name === 'dlg'); | ||
if (parentDlg && parentDlg.args[0]) { | ||
dlgId = parentDlg.args[0]; | ||
} | ||
|
||
let bullet = ''; | ||
if (this.args.length === 2 && !isNumber(this.args[1])) { | ||
bullet = this.args[1]; | ||
} else if (parentDlg && parentDlg.args.length === 3) { | ||
bullet = parentDlg.args[2]; | ||
} | ||
|
||
let line; | ||
if (this.args.length === 0) { | ||
line = this.payload[0].content; | ||
} else { | ||
line = this.args[0]; | ||
} | ||
|
||
const link = jQuery(`<a class="dlg-line">${bullet ? bullet + ' ' : ''}${line}</a>`); | ||
link.ariaClick(() => { | ||
jQuery('#' + dlgId).wiki(this.payload[0].contents); | ||
|
||
const dlgStage = jQuery('#' + dlgId + '-stage'); | ||
dlgStage.find('.dlg-level-' + level).attr('hidden', 'hidden'); | ||
dlgStage.find('.dlg-level-' + targetLevel).removeAttr('hidden'); | ||
}); | ||
|
||
jQuery(this.output).append(link); | ||
}, | ||
}); | ||
|
||
Macro.add('level', { | ||
tags: [], | ||
handler() { | ||
const level = parseInt(this.args[0], 10); | ||
if (isNaN(level)) { | ||
this.error('missing <<dlg>> <<level>> number'); | ||
} else { | ||
const wrapper = jQuery(`<div class="dlg-level-${level}" hidden></div>`); | ||
for (const payload of this.payload) { | ||
wrapper.wiki(payload.contents); | ||
} | ||
|
||
jQuery(this.output).append(wrapper); | ||
} | ||
}, | ||
}); | ||
|
||
Macro.add('dlg', { | ||
tags: [], | ||
handler() { | ||
const dlgId = this.args[0] || 'dlg'; | ||
|
||
const currentLevel = this.args.length === 2 ? parseInt(this.args[1], 10) : 0; | ||
|
||
let dlgWrapper = getOrCreate(dlgId); | ||
let dlgStage = getOrCreate(dlgId + '-stage'); | ||
|
||
for (const payload of this.payload) { | ||
dlgStage.wiki(payload.contents); | ||
} | ||
|
||
dlgStage.find('.dlg-level-' + currentLevel).removeAttr('hidden', 'hidden'); | ||
|
||
jQuery(this.output) | ||
.append(dlgWrapper) | ||
.append(dlgStage); | ||
}, | ||
}); | ||
}()); |