-
Notifications
You must be signed in to change notification settings - Fork 9
/
linkonce.js
104 lines (84 loc) · 3.33 KB
/
linkonce.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
(function linkonceMacro() {
'use strict';
/* globals version, Macro, visited, Story, Config, Wikifier, Engine, State */
const macroName = 'linkonce';
if (!version || !version.title || 'SugarCube' !== version.title || !version.major || version.major < 2) {
throw new Error(`<<${macroName}>> macro requires SugarCube 2.0 or greater, aborting load`);
}
version.extensions[macroName] = { major: 1, minor: 0, revision: 0 };
function has(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
function parseLinkArg(arg) {
let passage;
let $content;
if (arg.isImage) {
// Argument was in wiki image syntax.
$content = jQuery(document.createElement('img'))
.attr('src', arg.source);
if (has(arg, 'passage')) {
$content.attr('data-passage', arg.passage);
}
if (has(arg,'title')) {
$content.attr('title', arg.title);
}
if (has(arg, 'align')) {
$content.attr('align', arg.align);
}
passage = arg.link;
} else {
// Argument was in wiki link syntax.
$content = jQuery(document.createTextNode(arg.text));
passage = arg.link;
}
return {
passage,
$content,
};
}
Macro.add(macroName, {
tags: null,
handler() {
if (this.args.length === 0) {
return this.error(`no <<${macroName}>> text specified`);
}
if (this.args.length > 1) {
return this.error('<<${macroName}>> only accepts wiki-syntax: [[link text|Passage name]]');
}
const {passage, $content} = parseLinkArg(this.args[0]);
const hasVisited = visited(passage) > 0;
const $link = jQuery(document.createElement( hasVisited ? 'span' : 'a'));
$content.appendTo($link);
$link.append($content);
$link.addClass(`macro-${this.name}`);
if (!hasVisited) {
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');
}
$link
.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,
));
}
$link.appendTo(this.output);
},
});
}());