Skip to content

Commit

Permalink
updates to <<done>> and <<first>>
Browse files Browse the repository at this point in the history
minor improvements to both macros, nothing game changing.

`<<done>>` should now have better performance and will be less likely to create any sort of isssues with multiples (which are still not recommended; you're likely doing *something* wrong if you have multiples in one passage)

closes #16
  • Loading branch information
ChapelR committed Jul 22, 2018
1 parent dd6ac55 commit 366bfbb
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ Temporary Items

/old
/node_modules
build.bat
2 changes: 1 addition & 1 deletion docs/done-macro.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Done + Replace: @@#my-other-element;@@

**Task object warning**:

This macro registers a `postdisplay` task object called `:chapel-done-macro`, so you'll want to avoid using that name for your own task objects to prevent potential bugs. There is no configuration option to change this, but it's fairly easy to see and change in the unminified source code, where it should appear three times in total.
This macro registers one or more `postdisplay` task object called `:chapel-done-macro-#`, where the `#` is a number. So you'll want to avoid using these names for your own task objects to prevent potential bugs. There is no configuration option to change this, but it's fairly easy to see and change in the unminified source code, where it should appear only once.

-----

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"doc": "docs"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"build": "node build"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Chapel's Custom Macro Collection

### Contents
### Documentation
* [The Dialog API Macro Set](./docs/dialog-api-macro-set.md)
* [The Done Macro](./docs/done-macro.md)
* [The Dropdown Macro](./docs/dropdown.md)
Expand Down
37 changes: 13 additions & 24 deletions scripts/done.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
// the <<done>> macro, v1.0.0; for SugarCube 2, by chapel
// the <<done>> macro, v1.0.1; for SugarCube 2, by chapel

(function () {

var counter = 1;
// the core function
function done (wikiCode) {
if (!wikiCode || typeof wikiCode !== 'string') {
return; // bail out
}
// check the task name
var cached;
if (postdisplay[':chapel-done-macro'] && typeof postdisplay[':chapel-done-macro'] === 'function') {
// two possibilities; 1) there's a <<done>> on the page already; 2) there's a another task the user named after me (flatterer)
cached = clone(postdisplay[':chapel-done-macro']);
}
// register the task
postdisplay[':chapel-done-macro'] = function (task) {
delete postdisplay[task]; // single use

if (cached && typeof cached === 'function') {
cached(task); // run the cached function, which may also unregister... kind of a mess
}

$.wiki(wikiCode); // wikify the source code
};
}

Macro.add('done', {
skipArgs : true,
tags : null,
handler : function () {

var wiki = this.payload[0].contents;
var wiki = this.payload[0].contents.trim();

if (wiki === '') {
return; // bail
}

done(wiki);
postdisplay[':chapel-done-macro-' + counter] = function (task) {
delete postdisplay[task]; // single use
$.wiki(wiki); // wikify the source code
};

counter++;
}
});

Expand Down
7 changes: 4 additions & 3 deletions scripts/first-macro.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// first macro, by chapel; for sugarcube 2
// version 1.1.0
// version 1.1.1
// see the documentation: https://github.com/ChapelR/custom-macros-for-sugarcube-2#first-macro

// <<first>> macro
Macro.add('first', {
tags : ['then', 'finally'],
handler : function () {
skipArgs : true,
tags : ['then', 'finally'],
handler : function () {

var $wrapper = $(document.createElement('span')),
last = this.payload[this.payload.length - 1],
Expand Down
2 changes: 1 addition & 1 deletion scripts/minified/done.min.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// done.min.js, for SugarCube 2, by Chapel
;Macro.add("done",{tags:null,handler:function(){var a,n,o=this.payload[0].contents;(a=o)&&"string"==typeof a&&(postdisplay[":chapel-done-macro"]&&"function"==typeof postdisplay[":chapel-done-macro"]&&(n=clone(postdisplay[":chapel-done-macro"])),postdisplay[":chapel-done-macro"]=function(o){delete postdisplay[o],n&&"function"==typeof n&&n(o),$.wiki(a)})}});
;!function(){var a=1;Macro.add("done",{skipArgs:!0,tags:null,handler:function(){var n=this.payload[0].contents.trim();""!==n&&(postdisplay[":chapel-done-macro-"+a]=function(a){delete postdisplay[a],$.wiki(n)},a++)}})}();
// end done.min.js
2 changes: 1 addition & 1 deletion scripts/minified/first-macro.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 366bfbb

Please sign in to comment.