2
0

render.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. "use strict";
  2. // https://css-tricks.com/how-to-animate-the-details-element/
  3. class Accordion {
  4. constructor(el) {
  5. this.el = el;
  6. this.summary = el.querySelector('summary');
  7. this.content = el.querySelector('.opennamu_folding');
  8. this.animation = null;
  9. this.isClosing = false;
  10. this.isExpanding = false;
  11. this.summary.addEventListener('click', (e) => this.onClick(e));
  12. }
  13. onClick(e) {
  14. e.preventDefault();
  15. this.el.style.overflow = 'hidden';
  16. if(this.isClosing || !this.el.open) {
  17. this.open();
  18. } else if(this.isExpanding || this.el.open) {
  19. this.shrink();
  20. }
  21. }
  22. shrink() {
  23. this.isClosing = true;
  24. const startHeight = `${this.el.offsetHeight}px`;
  25. const endHeight = `${this.summary.offsetHeight}px`;
  26. if(this.animation) {
  27. this.animation.cancel();
  28. }
  29. this.animation = this.el.animate({
  30. height: [startHeight, endHeight]
  31. }, {
  32. duration: 200,
  33. easing: 'ease-out'
  34. });
  35. this.animation.onfinish = () => this.onAnimationFinish(false);
  36. this.animation.oncancel = () => this.isClosing = false;
  37. }
  38. open() {
  39. this.el.style.height = `${this.el.offsetHeight}px`;
  40. this.el.open = true;
  41. window.requestAnimationFrame(() => this.expand());
  42. }
  43. expand() {
  44. this.isExpanding = true;
  45. const startHeight = `${this.el.offsetHeight}px`;
  46. const endHeight = `${this.summary.offsetHeight + this.content.offsetHeight}px`;
  47. if(this.animation) {
  48. this.animation.cancel();
  49. }
  50. this.animation = this.el.animate({
  51. height: [startHeight, endHeight]
  52. }, {
  53. duration: 200,
  54. easing: 'ease-out'
  55. });
  56. this.animation.onfinish = () => this.onAnimationFinish(true);
  57. this.animation.oncancel = () => this.isExpanding = false;
  58. }
  59. onAnimationFinish(open) {
  60. this.el.open = open;
  61. this.animation = null;
  62. this.isClosing = false;
  63. this.isExpanding = false;
  64. this.el.style.height = this.el.style.overflow = '';
  65. }
  66. }
  67. function opennamu_heading_folding(data, element = '') {
  68. let fol = document.getElementById(data);
  69. if(fol.style.display === '' || fol.style.display === 'inline-block' || fol.style.display === 'block') {
  70. document.getElementById(data).style.display = 'none';
  71. document.getElementById(data + '_sub').style.opacity = '0.5';
  72. } else {
  73. document.getElementById(data).style.display = 'block';
  74. document.getElementById(data + '_sub').style.opacity = '1';
  75. }
  76. if(element !== '') {
  77. if(element.innerHTML !== '⊖') {
  78. element.innerHTML = '⊖';
  79. } else {
  80. element.innerHTML = '⊕';
  81. }
  82. }
  83. }
  84. function opennamu_do_render_html(name = '') {
  85. if(document.getElementById(name)) {
  86. let data = document.getElementById(name).innerHTML;
  87. let src_list = ['www.youtube.com', 'www.google.com', 'play-tv.kakao.com'];
  88. let t_data = [
  89. 'b', 'i', 's', 'del', 'strong', 'bold', 'em', 'sub', 'sup',
  90. 'div', 'span',
  91. 'a',
  92. 'iframe'
  93. ];
  94. for(let key in t_data) {
  95. let patt = new RegExp(
  96. '<' + t_data[key] + '( (?:(?:(?!>).)+))?>((?:(?!<\/' + t_data[key] + '>).)*)<\/' + t_data[key] + '>',
  97. 'ig'
  98. );
  99. data = data.replace(patt, function(full, in_data, in_data_2) {
  100. if(['b', 'i', 's', 'del', 'strong', 'bold', 'em', 'sub', 'sup'].includes(t_data[key])) {
  101. return '<' + t_data[key] + '>' + in_data_2 + '</' + t_data[key] + '>'
  102. } else if(t_data[key] === 'div' || t_data[key] === 'span') {
  103. let style_data = in_data.match(/ style=['"]([^'"]*)['"]/);
  104. if(style_data) {
  105. style_data = style_data[1].replace(/position/ig, '');
  106. } else {
  107. style_data = '';
  108. }
  109. return '<' + t_data[key] + ' style="' + style_data + '">' + in_data_2 + '</' + t_data[key] + '>';
  110. } else if(t_data[key] === 'a') {
  111. let link_data = in_data.match(/ href=['"]([^'"]*)['"]/);
  112. if(link_data) {
  113. link_data = link_data[1].replace(/^javascript:/ig, '');
  114. } else {
  115. link_data = '';
  116. }
  117. return '<' + t_data[key] + ' class="opennamu_link_out" href="' + link_data + '">' + in_data_2 + '</' + t_data[key] + '>';
  118. } else if(t_data[key] === 'iframe') {
  119. let src_data = in_data.match(/ src=['"]([^'"]*)['"]/);
  120. if(src_data) {
  121. src_data = src_data[1];
  122. let src_check = src_data.match(/^http(?:s)?:\/\/([^/]+)/);
  123. if(src_check) {
  124. if(!src_list.includes(src_check[1])) {
  125. src_data = '';
  126. }
  127. } else {
  128. src_data = '';
  129. }
  130. } else {
  131. src_data = '';
  132. }
  133. let width_data = in_data.match(/ width=['"]([^'"]*)['"]/);
  134. if(width_data) {
  135. width_data = width_data[1];
  136. } else {
  137. width_data = '';
  138. }
  139. let height_data = in_data.match(/ height=['"]([^'"]*)['"]/);
  140. if(height_data) {
  141. height_data = height_data[1];
  142. } else {
  143. height_data = '';
  144. }
  145. return '<' + t_data[key] + ' src="' + src_data + '" width="' + width_data + '" height="' + height_data + '" allowfullscreen frameborder="0">' + in_data_2 + '</' + t_data[key] + '>';
  146. } else {
  147. let src_data = in_data.match(/ src=['"]([^'"]*)['"]/);
  148. if(src_data) {
  149. src_data = src_data[1];
  150. } else {
  151. src_data = '';
  152. }
  153. let width_data = in_data.match(/ width=['"]([^'"]*)['"]/);
  154. if(width_data) {
  155. width_data = width_data[1];
  156. } else {
  157. width_data = '';
  158. }
  159. let height_data = in_data.match(/ height=['"]([^'"]*)['"]/);
  160. if(height_data) {
  161. height_data = height_data[1];
  162. } else {
  163. height_data = '';
  164. }
  165. return '<' + t_data[key] + ' controls src="' + src_data + '" width="' + width_data + '" height="' + height_data + '">' + in_data_2 + '</' + t_data[key] + '>';
  166. }
  167. });
  168. }
  169. document.getElementById(name).innerHTML = data;
  170. }
  171. }
  172. function opennamu_do_footnote_spread(set_name, load_name) {
  173. if(document.getElementById(set_name + '_load').style.display === 'none') {
  174. document.getElementById(set_name).title = '';
  175. document.getElementById(set_name + '_load').innerHTML = '<a href="#' + load_name + '">(Go)</a> ' + document.getElementById(load_name + '_title').innerHTML;
  176. document.getElementById(set_name + '_load').style.display = "inline-block";
  177. } else {
  178. document.getElementById(set_name + '_load').style.display = "none";
  179. }
  180. }
  181. function opennamu_do_footnote_popover(set_name, load_name, sub_obj = undefined) {
  182. if(document.getElementById(set_name + '_load').style.display === 'none') {
  183. if(sub_obj !== undefined) {
  184. document.getElementById(set_name + '_load').innerHTML = document.getElementById(sub_obj).innerHTML;
  185. } else {
  186. document.getElementById(set_name).title = '';
  187. document.getElementById(set_name + '_load').innerHTML = '<a href="#' + load_name + '">(Go)</a> ' + document.getElementById(load_name + '_title').innerHTML;
  188. }
  189. document.getElementById(set_name + '_load').style.display = "inline-block";
  190. let width = document.getElementById(set_name + '_load').clientWidth;
  191. let screen_width = window.innerWidth;
  192. let left = document.getElementById(set_name).getBoundingClientRect().left;
  193. let left_org = document.getElementById(set_name + '_load').getBoundingClientRect().left;
  194. let top = window.pageYOffset + document.getElementById(set_name).getBoundingClientRect().top;
  195. document.getElementById(set_name + '_load').style.top = String(top) + "px";
  196. if(screen_width - (left + width) < 50) {
  197. if(left > 350) {
  198. document.getElementById(set_name + '_load').style.left = String(left - 300) + "px";
  199. } else {
  200. document.getElementById(set_name + '_load').style.left = "0px";
  201. }
  202. left = document.getElementById(set_name + '_load').getBoundingClientRect().left;
  203. width = document.getElementById(set_name + '_load').clientWidth;
  204. if(300 > width) {
  205. document.getElementById(set_name + '_load').style.left = String(left + (300 - width)) + "px";
  206. } else {
  207. document.getElementById(set_name + '_load').style.marginTop = "20px";
  208. }
  209. }
  210. } else {
  211. document.getElementById(set_name + '_load').style.display = "none";
  212. }
  213. }
  214. function opennamu_do_category_spread() {
  215. if(document.getElementsByClassName('opennamu_render_complete')) {
  216. document.getElementsByClassName('opennamu_render_complete')[0].innerHTML = '' +
  217. '<style>.opennamu_category_button { display: none; } .opennamu_category { white-space: pre-wrap; overflow-x: unset; text-overflow: unset; }</style>' +
  218. '' + document.getElementsByClassName('opennamu_render_complete')[0].innerHTML;
  219. }
  220. }
  221. function opennamu_do_include(name, render_name, to_obj, option_obj) {
  222. let option = {};
  223. if(option_obj !== '') {
  224. if(document.getElementById(option_obj)) {
  225. option = document.getElementById(option_obj).innerHTML;
  226. option = decodeURIComponent(option);
  227. }
  228. }
  229. fetch("/api/raw/" + opennamu_do_url_encode(name)).then(function(res) {
  230. return res.json();
  231. }).then(function(data) {
  232. if(data["data"]) {
  233. opennamu_do_render(to_obj, data["data"], render_name, 'include', option);
  234. document.getElementById(option_obj).style.display = "inline";
  235. }
  236. });
  237. }
  238. function opennamu_do_toc() {
  239. let data = document.getElementById('opennamu_render_complete');
  240. let h_tag = data.querySelectorAll("h1, h2, h3, h4, h5, h6");
  241. let toc_count = [0, 0, 0, 0, 0, 0];
  242. let toc_html = '';
  243. for(let for_a = 0; for_a < h_tag.length; for_a++) {
  244. let tag = h_tag[for_a].tagName.toLowerCase();
  245. tag = tag.replace('h', '');
  246. tag = Number(tag) - 1;
  247. for(let for_b = tag + 1; for_b < 6; for_b++) {
  248. toc_count[for_b] = 0;
  249. }
  250. toc_count[tag] += 1;
  251. let toc_string = '';
  252. let add_on = false;
  253. for(let for_b = 5; for_b >= 0; for_b--) {
  254. if(add_on == false && toc_count[for_b] != 0) {
  255. add_on = true;
  256. }
  257. if(add_on == true) {
  258. toc_string = String(toc_count[for_b]) + '.' + toc_string;
  259. }
  260. }
  261. toc_string = toc_string.replace(/^(0\.)+/, '');
  262. let toc_string_sub = toc_string.replace(/\.$/, '');
  263. let toc_margin = '<span style="margin-left: 10px;"></span>'.repeat(toc_string_sub.split('.').length - 1);
  264. toc_html += toc_margin + '<a href="#s-' + toc_string_sub + '">' + toc_string + '</a> ' + h_tag[for_a].innerHTML + '<br>';
  265. h_tag[for_a].innerHTML = '<a id="s-' + toc_string_sub + '" href="#toc">' + toc_string + '</a> ' + h_tag[for_a].innerHTML;
  266. }
  267. data.innerHTML = data.innerHTML.replace(/(<h[1-6]>)/, '<div class="opennamu_toc"></div>$1');
  268. data.innerHTML = data.innerHTML.replace(/<div class="opennamu_toc"><\/div>/g, function(match) {
  269. return '<div class="opennamu_TOC" id="toc"><div class="opennamu_TOC_title">TOC</div><br>' + toc_html + '</div>';
  270. });
  271. }