A pixel art painter geared specifically at NES pixel art. Includes export for .chr binary file as well as palette and namespace data.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
3.7KB

  1. var PRETTY_INDENTATION = " ";
  2. class ENode{
  3. constructor(tag){
  4. this.__tag = tag;
  5. this.__classes = [];
  6. this.__attribs = [];
  7. this.__children = [];
  8. }
  9. appendChild(c){
  10. var val = (c instanceof ENode) ? c : c.toString();
  11. this.__children.push(c);
  12. return this;
  13. }
  14. addClass(cname){
  15. this.__classes.push(cname);
  16. }
  17. addAttribute(aname, aval){
  18. this.__attribs.push([aname, aval]);
  19. }
  20. toPrettyString(depth){
  21. depth || (depth = 0);
  22. var ind = "";
  23. var strind = "";
  24. var eol = "";
  25. if (depth > -1){
  26. if (depth > 0)
  27. ind = PRETTY_INDENTATION + (new Array(depth)).join(PRETTY_INDENTATION);
  28. strind = ind + PRETTY_INDENTATION;
  29. eol = "\n";
  30. }
  31. var r = ind + "<" + this.__tag.toUpperCase();
  32. if (this.__attribs.length > 0){
  33. var attr = this.__attribs.reduce(function(a, at){
  34. a += ((a !== "") ? " " : "") + at[0].trim() + "=\"" + at[1] + "\"";
  35. return a;
  36. }, "");
  37. r += " " + attr;
  38. }
  39. if (this.__children.length > 0){
  40. r += ">" + eol;
  41. var lcn = true // lcn = Last Child was Node;
  42. r += this.__children.reduce(function(a, c){
  43. if (c instanceof ENode){
  44. a += ((lcn === false) ? "\n" : "") + ((depth >= 0) ? c.toPrettyString(depth + 1) : c.toString());
  45. lcn = true;
  46. } else {
  47. a += strind + c;
  48. lcn = false;
  49. }
  50. return a;
  51. }, "");
  52. r += ((lcn === false) ? eol : "") + ind + "</" + this.__tag.toUpperCase() + ">" + eol;
  53. } else {
  54. r += " />" + eol;
  55. }
  56. return r;
  57. }
  58. toString(){
  59. return this.toPrettyString(-1);
  60. }
  61. }
  62. function el(){
  63. if (arguments.length < 1)
  64. throw new Error("Invalid number of arguments.");
  65. var ename = arguments[0];
  66. var args = Array.prototype.slice.call(arguments, 1);
  67. var e = new ENode(ename);
  68. args.forEach(function(item){
  69. if (item === null){return;}
  70. // If given an array, assume it to be an array children.
  71. if (item instanceof Array){
  72. item.forEach(itm => e.appendChild((itm instanceof ENode) ? itm : itm.toString()));
  73. // If given an ENode, then it IS a child
  74. } else if (item instanceof ENode){
  75. e.appendChild(item);
  76. // All objects are assumed to be attributes.
  77. } else if (item instanceof Object){
  78. var keys = Object.keys(item);
  79. keys.forEach(function(key){
  80. if (item.hasOwnProperty(key)){
  81. if (typeof(item[key]) === "string"){
  82. e.addAttribute(key.toString(), item[key]);
  83. }
  84. }
  85. });
  86. // Everything else is assumed to be a child.
  87. } else {
  88. e.appendChild(item.toString());
  89. }
  90. });
  91. return e;
  92. }
  93. module.exports = {
  94. ENode: ENode,
  95. el: el,
  96. html: el.bind(null, "html"),
  97. head: el.bind(null, "head"),
  98. title: el.bind(null, "title"),
  99. script: el.bind(null, "script"),
  100. link: el.bind(null, "link"),
  101. meta: el.bind(null, "meta"),
  102. body: el.bind(null, "body"),
  103. div: el.bind(null, "div"),
  104. span: el.bind(null, "span"),
  105. a: el.bind(null, "a"),
  106. p: el.bind(null, "p"),
  107. b: el.bind(null, "b"),
  108. i: el.bind(null, "i"),
  109. br: el.bind(null, "br"),
  110. ol: el.bind(null, "ol"),
  111. ul: el.bind(null, "ul"),
  112. li: el.bind(null, "li"),
  113. em: el.bind(null, "em"),
  114. img: el.bind(null, "img"),
  115. font: el.bind(null, "font"),
  116. h1: el.bind(null, "h1"),
  117. h2: el.bind(null, "h2"),
  118. h3: el.bind(null, "h3"),
  119. h4: el.bind(null, "h4"),
  120. h5: el.bind(null, "h5"),
  121. h6: el.bind(null, "h6"),
  122. hr: el.bind(null, "hr"),
  123. blockquote: el.bind(null, "blockquote"),
  124. button: el.bind(null, "button"),
  125. nav: el.bind(null, "nav"),
  126. canvas: el.bind(null, "canvas"),
  127. caption: el.bind(null, "caption"),
  128. indentationChar: function(c){
  129. if (type(c) === "string" && c.length > 0){
  130. PRETTY_INDENTATION = c;
  131. }
  132. return PRETTY_INDENTATION;
  133. }
  134. };