Skip to content

Commit

Permalink
SMM2 Links 1.2.0
Browse files Browse the repository at this point in the history
* Added: Option to include a link to Wizulus's SMM2 Viewer in addition to, or replacing, the standard links to OpenCourseWorld or MakerCentral.
  • Loading branch information
falsidge authored Mar 22, 2024
1 parent a379cf7 commit 7047269
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 154 deletions.
339 changes: 187 additions & 152 deletions src/smm2-links/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,158 +7,193 @@ const SMM2_CODE = /\b(?:[0-9BCDFGHJKLMNPQRSTVWXY]{3}-?){2}[0-9BCDFGHJKLMNPQRSTVW

class SMM2Links extends Addon {

constructor(...args) {
super(...args);

this.inject('chat');
this.inject('settings');

this.settings.add('addon.smm2-links.enabled', {
default: null,
requires: ['context.categoryID'],

process(ctx, val) {
if ( val == null )
return ctx.get('context.categoryID') == 511399;
return val;
},

ui: {
sort: -1,
path: 'Add-Ons > SMM2 Links >> General',
title: 'Enable clickable Mario Maker 2 IDs.',
description: 'By default, this is only enabled if the current category is Super Mario Maker 2.',
component: 'setting-check-box'
}
});

this.settings.add('addon.smm2-links.allow-embed', {
default: false,
ui: {
path: 'Add-Ons > SMM2 Links >> General',
title: 'Allow rich embeds for Mario Maker 2 IDs.',
description: '**Note:** This only works when the IDs are clickable.',
component: 'setting-check-box',
extra: {
component: 'chat-rich-example',
getChat: () => this.chat,
url: `https://smm2.wizul.us/smm2/course/SYG-7FR-QLG`
}
}
});

const t = this;

this.tokenizer = {
type: 'smm_link',
priority: 0,

render(token, createElement) {
return (<strong>{token.meta}: {token.code}</strong>)
},

process(tokens) {
if ( ! tokens || ! tokens.length || ! this.context.get('addon.smm2-links.enabled') )
return;

const out = [],
allow_rich = this.context.get('addon.smm2-links.allow-embed');

for(const token of tokens) {
if ( token.type !== 'text' ) {
out.push(token);
continue;
}

SMM2_CODE.lastIndex = 0;
const text = token.text;
let idx = 0, match;

while((match = SMM2_CODE.exec(text))) {
let code = match[0];
let parsed, meta;
try {
parsed = new Code(code);
meta = parsed.getMeta();
} catch(err) {
t.log.debug('Unable to parse code:', code, err);
continue;
}

let dashes = true,
base;

if ( meta === OCW_COURSE_META )
base = 'https://opencourse.world/courses/';
else if ( meta === OCW_MAKER_META )
base = 'https://opencourse.world/makers/';
else if ( meta === NSO_COURSE_META ) {
base = 'https://makercentral.io/levels/view/';
dashes = false;
} else if ( meta === NSO_MAKER_META ) {
base = 'https://makercentral.io/users/';
dashes = false;
} else {
continue;
}

const nix = match.index;
if ( idx !== nix )
out.push({type: 'text', text: text.slice(idx, nix)});

let url = parsed.toString();
if ( ! dashes )
url = url.replace(/-/g, '');

url = base + url;

out.push({
type: 'link',
allow_rich,
url,
is_mail: false,
text: code
});

idx = nix + code.length;
}

if ( idx === 0 )
out.push(token);
else if ( idx < text.length )
out.push({type: 'text', text: text.slice(idx)});
}

return out;
}

}

}

onEnable() {
this.chat.addTokenizer(this.tokenizer);

for(const setting of [
'addon.smm2-links.enabled',
'addon.smm2-links.allow-embed'
])
this.chat.context.on(`changed:${setting}`, this.onChanged, this);
}

onDisable() {
this.chat.removeTokenizer(this.tokenizer);

for(const setting of [
'addon.smm2-links.enabled',
'addon.smm2-links.allow-embed'
])
this.chat.context.off(`changed:${setting}`, this.onChanged, this);
}

onChanged() {
this.emit('chat:update-line-tokens');
}
constructor(...args) {
super(...args);

this.inject('chat');
this.inject('settings');

this.settings.add('addon.smm2-links.enabled', {
default: null,
requires: ['context.categoryID'],

process(ctx, val) {
if ( val == null )
return ctx.get('context.categoryID') == 511399;
return val;
},

ui: {
sort: -1,
path: 'Add-Ons > SMM2 Links >> General',
title: 'Enable clickable Mario Maker 2 IDs.',
description: 'By default, this is only enabled if the current category is Super Mario Maker 2.',
component: 'setting-check-box'
}
});

this.settings.add('addon.smm2-links.allow-embed', {
default: false,
ui: {
path: 'Add-Ons > SMM2 Links >> General',
title: 'Allow rich embeds for Mario Maker 2 IDs.',
description: '**Note:** This only works when the IDs are clickable. Make sure to enable rich embeds in [Chat >> Appearance > Rich Content](~chat.appearance.rich_content).',
component: 'setting-check-box',
extra: {
component: 'chat-rich-example',
getChat: () => this.chat,
url: `https://smm2.wizul.us/smm2/course/SYG-7FR-QLG`
}
}
});

this.settings.add('addon.smm2-links.viewer_link', {
default: 0,
ui: {
path: 'Add-Ons > SMM2 Links >> General',
title: 'Wizulus\'s Viewer Link',
description: 'You can optionally include a link to view makers and courses using Wizulus\'s SMM2 Viewer.',
component: 'setting-select-box',

data: [
{ value: 0, title: 'Disabled' },
{ value: 1, title: 'Replace ID link' },
{ value: 2, title: 'Add [Viewer] next to ID link' },
]
}
});

const t = this;

this.tokenizer = {
type: 'smm_link',
priority: 0,

render(token, createElement) {
return (<strong>{token.meta}: {token.code}</strong>)
},

process(tokens) {
if ( ! tokens || ! tokens.length || ! this.context.get('addon.smm2-links.enabled') )
return;

const out = [],
allow_rich = this.context.get('addon.smm2-links.allow-embed'),
viewer_link = this.context.get('addon.smm2-links.viewer_link');


for(const token of tokens) {
if ( token.type !== 'text' ) {
out.push(token);
continue;
}

SMM2_CODE.lastIndex = 0;
const text = token.text;
let idx = 0, match;

while((match = SMM2_CODE.exec(text))) {
const code = match[0];

let parsed, meta;
try {
parsed = new Code(code);
meta = parsed.getMeta();
} catch(err) {
t.log.debug('Unable to parse code:', code, err);
continue;
}


let dashes = true,
base;
if ( viewer_link === 1 )
base = 'https://smm2.wizul.us/smm2/course/';
else if ( meta === OCW_COURSE_META )
base = 'https://opencourse.world/courses/';
else if ( meta === OCW_MAKER_META )
base = 'https://opencourse.world/makers/';
else if ( meta === NSO_COURSE_META ) {
base = 'https://makercentral.io/levels/view/';
dashes = false;
} else if ( meta === NSO_MAKER_META ) {
base = 'https://makercentral.io/users/';
dashes = false;
} else {
continue;
}

const nix = match.index;
if ( idx !== nix )
out.push({type: 'text', text: text.slice(idx, nix)});

let url = parsed.toString();
if ( ! dashes )
url = url.replace(/-/g, '');

url = base + url;

out.push({
type: 'link',
allow_rich,
url,
is_mail: false,
text: code
});

if ( viewer_link === 2 ) {
out.push({type: 'text', text: ' ['});
out.push({
type: 'link',
allow_rich : false,
url : `https://smm2.wizul.us/smm2/course/${parsed.toString()}`,
is_mail: false,
text: 'Viewer'
});
out.push({type: 'text', text: ']'});
}

idx = nix + code.length;
}

if ( idx === 0 )
out.push(token);
else if ( idx < text.length )
out.push({type: 'text', text: text.slice(idx)});
}

return out;
}

}

}

onEnable() {
this.chat.addTokenizer(this.tokenizer);

for(const setting of [
'addon.smm2-links.enabled',
'addon.smm2-links.allow-embed',
'addon.smm2-links.viewer_link'
])
this.chat.context.on(`changed:${setting}`, this.onChanged, this);
}

onDisable() {
this.chat.removeTokenizer(this.tokenizer);

for(const setting of [
'addon.smm2-links.enabled',
'addon.smm2-links.allow-embed',
'addon.smm2-links.viewer_link'
])
this.chat.context.off(`changed:${setting}`, this.onChanged, this);
}

onChanged() {
this.emit('chat:update-line-tokens');
}

}

Expand Down
4 changes: 2 additions & 2 deletions src/smm2-links/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"enabled": true,
"requires": [],
"version": "1.1.0",
"version": "1.2.0",
"load_events": [
"chat-data"
],
Expand All @@ -11,5 +11,5 @@
"description": "This add-on turns Super Mario Maker 2 course and maker IDs in chat into clickable links.",
"settings": "add_ons.smm2_links",
"created": "2023-04-19T21:50:22.107Z",
"updated": "2023-11-05T19:56:16.231Z"
"updated": "2024-03-22T09:39:47.001Z"
}

0 comments on commit 7047269

Please sign in to comment.