2
0

render.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. console.log(element.innerHTML);
  78. if(element.innerHTML !== '⊖') {
  79. element.innerHTML = '⊖';
  80. } else {
  81. element.innerHTML = '⊕';
  82. }
  83. }
  84. }
  85. function opennamu_do_render_html(name = '') {
  86. if(document.getElementById(name)) {
  87. let data = document.getElementById(name).innerHTML;
  88. let src_list = ['www.youtube.com', 'www.google.com', 'play-tv.kakao.com'];
  89. let t_data = [
  90. 'b', 'i', 's', 'del', 'strong', 'bold', 'em', 'sub', 'sup',
  91. 'div', 'span',
  92. 'a',
  93. 'iframe'
  94. ];
  95. for(let key in t_data) {
  96. let patt = new RegExp(
  97. '<' + t_data[key] + '( (?:(?:(?!>).)+))?>((?:(?!<\/' + t_data[key] + '>).)*)<\/' + t_data[key] + '>',
  98. 'ig'
  99. );
  100. data = data.replace(patt, function(full, in_data, in_data_2) {
  101. if(['b', 'i', 's', 'del', 'strong', 'bold', 'em', 'sub', 'sup'].includes(t_data[key])) {
  102. return '<' + t_data[key] + '>' + in_data_2 + '</' + t_data[key] + '>'
  103. } else if(t_data[key] === 'div' || t_data[key] === 'span') {
  104. let style_data = in_data.match(/ style=['"]([^'"]*)['"]/);
  105. if(style_data) {
  106. style_data = style_data[1].replace(/position/ig, '');
  107. } else {
  108. style_data = '';
  109. }
  110. return '<' + t_data[key] + ' style="' + style_data + '">' + in_data_2 + '</' + t_data[key] + '>';
  111. } else if(t_data[key] === 'a') {
  112. let link_data = in_data.match(/ href=['"]([^'"]*)['"]/);
  113. if(link_data) {
  114. link_data = link_data[1].replace(/^javascript:/ig, '');
  115. } else {
  116. link_data = '';
  117. }
  118. return '<' + t_data[key] + ' class="opennamu_link_out" href="' + link_data + '">' + in_data_2 + '</' + t_data[key] + '>';
  119. } else if(t_data[key] === 'iframe') {
  120. let src_data = in_data.match(/ src=['"]([^'"]*)['"]/);
  121. if(src_data) {
  122. src_data = src_data[1];
  123. let src_check = src_data.match(/^http(?:s)?:\/\/([^/]+)/);
  124. if(src_check) {
  125. if(!src_list.includes(src_check[1])) {
  126. src_data = '';
  127. }
  128. } else {
  129. src_data = '';
  130. }
  131. } else {
  132. src_data = '';
  133. }
  134. let width_data = in_data.match(/ width=['"]([^'"]*)['"]/);
  135. if(width_data) {
  136. width_data = width_data[1];
  137. } else {
  138. width_data = '';
  139. }
  140. let height_data = in_data.match(/ height=['"]([^'"]*)['"]/);
  141. if(height_data) {
  142. height_data = height_data[1];
  143. } else {
  144. height_data = '';
  145. }
  146. return '<' + t_data[key] + ' src="' + src_data + '" width="' + width_data + '" height="' + height_data + '" allowfullscreen frameborder="0">' + in_data_2 + '</' + t_data[key] + '>';
  147. } else {
  148. let src_data = in_data.match(/ src=['"]([^'"]*)['"]/);
  149. if(src_data) {
  150. src_data = src_data[1];
  151. } else {
  152. src_data = '';
  153. }
  154. let width_data = in_data.match(/ width=['"]([^'"]*)['"]/);
  155. if(width_data) {
  156. width_data = width_data[1];
  157. } else {
  158. width_data = '';
  159. }
  160. let height_data = in_data.match(/ height=['"]([^'"]*)['"]/);
  161. if(height_data) {
  162. height_data = height_data[1];
  163. } else {
  164. height_data = '';
  165. }
  166. return '<' + t_data[key] + ' controls src="' + src_data + '" width="' + width_data + '" height="' + height_data + '">' + in_data_2 + '</' + t_data[key] + '>';
  167. }
  168. });
  169. }
  170. document.getElementById(name).innerHTML = data;
  171. }
  172. }
  173. function opennamu_do_footnote_spread(set_name, load_name) {
  174. if(document.getElementById(set_name + '_load').style.display === 'none') {
  175. document.getElementById(set_name).title = '';
  176. document.getElementById(set_name + '_load').innerHTML = '<a href="#' + load_name + '">(Go)</a> ' + document.getElementById(load_name + '_title').innerHTML;
  177. document.getElementById(set_name + '_load').style.display = "inline-block";
  178. } else {
  179. document.getElementById(set_name + '_load').style.display = "none";
  180. }
  181. }
  182. function opennamu_do_footnote_popover(set_name, load_name, sub_obj = undefined) {
  183. if(document.getElementById(set_name + '_load').style.display === 'none') {
  184. if(sub_obj !== undefined) {
  185. document.getElementById(set_name + '_load').innerHTML = document.getElementById(sub_obj).innerHTML;
  186. } else {
  187. document.getElementById(set_name).title = '';
  188. document.getElementById(set_name + '_load').innerHTML = '<a href="#' + load_name + '">(Go)</a> ' + document.getElementById(load_name + '_title').innerHTML;
  189. }
  190. document.getElementById(set_name + '_load').style.display = "inline-block";
  191. let width = document.getElementById(set_name + '_load').clientWidth;
  192. let screen_width = window.innerWidth;
  193. let left = document.getElementById(set_name).getBoundingClientRect().left;
  194. let left_org = document.getElementById(set_name + '_load').getBoundingClientRect().left;
  195. let top = window.pageYOffset + document.getElementById(set_name).getBoundingClientRect().top;
  196. document.getElementById(set_name + '_load').style.top = String(top) + "px";
  197. if(screen_width - (left + width) < 50) {
  198. if(left > 350) {
  199. document.getElementById(set_name + '_load').style.left = String(left - 300) + "px";
  200. } else {
  201. document.getElementById(set_name + '_load').style.left = "0px";
  202. }
  203. left = document.getElementById(set_name + '_load').getBoundingClientRect().left;
  204. width = document.getElementById(set_name + '_load').clientWidth;
  205. if(300 > width) {
  206. document.getElementById(set_name + '_load').style.left = String(left + (300 - width)) + "px";
  207. } else {
  208. document.getElementById(set_name + '_load').style.marginTop = "20px";
  209. }
  210. }
  211. } else {
  212. document.getElementById(set_name + '_load').style.display = "none";
  213. }
  214. }
  215. function opennamu_do_category_spread() {
  216. if(document.getElementsByClassName('opennamu_render_complete')) {
  217. document.getElementsByClassName('opennamu_render_complete')[0].innerHTML = '' +
  218. '<style>.opennamu_category_button { display: none; } .opennamu_category { white-space: pre-wrap; overflow-x: unset; text-overflow: unset; }</style>' +
  219. '' + document.getElementsByClassName('opennamu_render_complete')[0].innerHTML;
  220. }
  221. }
  222. function opennamu_do_include(name, render_name, to_obj, option_obj) {
  223. let option = {};
  224. if(option_obj !== '') {
  225. if(document.getElementById(option_obj)) {
  226. option = document.getElementById(option_obj).innerHTML;
  227. option = decodeURIComponent(option);
  228. }
  229. }
  230. fetch("/api/raw/" + opennamu_do_url_encode(name)).then(function(res) {
  231. return res.json();
  232. }).then(function(data) {
  233. if(data["data"]) {
  234. opennamu_do_render(to_obj, data["data"], render_name, 'include', option);
  235. document.getElementById(option_obj).style.display = "inline";
  236. }
  237. });
  238. }
  239. function opennamu_do_toc() {
  240. let data = document.getElementById('opennamu_render_complete');
  241. let h_tag = data.querySelectorAll("h1, h2, h3, h4, h5, h6");
  242. let toc_count = [0, 0, 0, 0, 0, 0];
  243. let toc_html = '';
  244. for(let for_a = 0; for_a < h_tag.length; for_a++) {
  245. let tag = h_tag[for_a].tagName.toLowerCase();
  246. tag = tag.replace('h', '');
  247. tag = Number(tag) - 1;
  248. for(let for_b = tag + 1; for_b < 6; for_b++) {
  249. toc_count[for_b] = 0;
  250. }
  251. toc_count[tag] += 1;
  252. let toc_string = '';
  253. let add_on = false;
  254. for(let for_b = 5; for_b >= 0; for_b--) {
  255. if(add_on == false && toc_count[for_b] != 0) {
  256. add_on = true;
  257. }
  258. if(add_on == true) {
  259. toc_string = String(toc_count[for_b]) + '.' + toc_string;
  260. }
  261. }
  262. toc_string = toc_string.replace(/^(0\.)+/, '');
  263. let toc_string_sub = toc_string.replace(/\.$/, '');
  264. let toc_margin = '<span style="margin-left: 10px;"></span>'.repeat(toc_string_sub.split('.').length - 1);
  265. toc_html += toc_margin + '<a href="#s-' + toc_string_sub + '">' + toc_string + '</a> ' + h_tag[for_a].innerHTML + '<br>';
  266. h_tag[for_a].innerHTML = '<a id="s-' + toc_string_sub + '" href="#toc">' + toc_string + '</a> ' + h_tag[for_a].innerHTML;
  267. }
  268. data.innerHTML = data.innerHTML.replace(/(<h[1-6]>)/, '<div class="opennamu_toc"></div>$1');
  269. data.innerHTML = data.innerHTML.replace(/<div class="opennamu_toc"><\/div>/g, function(match) {
  270. return '<div class="opennamu_TOC" id="toc"><div class="opennamu_TOC_title">TOC</div><br>' + toc_html + '</div>';
  271. });
  272. }