editor.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. function getBlockElements() {
  2. const blockDiv = document.createElement("div");
  3. const descButton = document.createElement('button');
  4. descButton.textContent = 'Add Description';
  5. const imgButton = document.createElement('button');
  6. imgButton.textContent = 'Add Image';
  7. const h = document.createElement("H2");
  8. const input = document.createElement("INPUT");
  9. input.setAttribute("type", "text");
  10. const inputButton = document.createElement('button')
  11. inputButton.textContent = 'Title新增/修改';
  12. contentDiv.append(blockDiv);
  13. blockDiv.appendChild(h);
  14. blockDiv.appendChild(input);
  15. blockDiv.appendChild(inputButton);
  16. blockDiv.appendChild(descButton);
  17. blockDiv.appendChild(imgButton);
  18. return {
  19. blockDiv: blockDiv,
  20. h: h,
  21. titleInput: input,
  22. inputButton: inputButton,
  23. descButton: descButton,
  24. imgButton: imgButton
  25. };
  26. }
  27. function getdescElements(blockDiv){
  28. const descDiv = document.createElement("div");
  29. const p = document.createElement("p");
  30. const descTextArea = document.createElement("TEXTAREA");
  31. descTextArea.setAttribute("type", "text");
  32. const descInputButton = document.createElement('button')
  33. descInputButton.textContent = 'Description新增/修改';
  34. descDiv.appendChild(p);
  35. descDiv.appendChild(descTextArea);
  36. descDiv.appendChild(descInputButton);
  37. blockDiv.appendChild(descDiv);
  38. return {
  39. p: p,
  40. descTextArea: descTextArea,
  41. descInputButton: descInputButton
  42. };
  43. }
  44. function getImgElements(blockDiv) {
  45. const imgDiv = document.createElement("div");
  46. const img = document.createElement("img");
  47. const imgInput = document.createElement("INPUT");
  48. const widthInput = document.createElement("INPUT");
  49. const heightInput = document.createElement("INPUT");
  50. const imgInputButton = document.createElement('button');
  51. imgInputButton.textContent = 'Image新增/修改';
  52. imgDiv.appendChild(img);
  53. imgDiv.appendChild(imgInput);
  54. imgDiv.appendChild(widthInput);
  55. imgDiv.appendChild(heightInput);
  56. imgDiv.appendChild(imgInputButton);
  57. blockDiv.appendChild(imgDiv);
  58. return {
  59. img: img,
  60. imgInput: imgInput,
  61. widthInput: widthInput,
  62. heightInput: heightInput,
  63. imgInputButton: imgInputButton
  64. };
  65. }
  66. function addDataToBlockArray(dataObject, blockArray, index) {
  67. if ('data' in blockArray[index]) {
  68. blockArray[index].data.push(dataObject)
  69. } else {
  70. blockArray[index].data = [dataObject];
  71. }
  72. return blockArray;
  73. }
  74. function parseMd(content) {
  75. const parseTitle = line => {
  76. var title = '';
  77. title = line.replace('## **', '');
  78. title = title.replace('**', '');
  79. return title;
  80. };
  81. const parseAmpImg = line => {
  82. if (line.includes('alt')) {
  83. const altParameter = line.replace(/alt=|"/g, '');
  84. return {alt: altParameter};
  85. } else if (line.includes('src')) {
  86. const srcParameter = line.replace(/src=|"/g, '');
  87. return {src: srcParameter};
  88. } else if (line.includes('height')) {
  89. const heightParameter = line.replace(/height=|"/g, '');
  90. return {height: heightParameter};
  91. } else if (line.includes('width')) {
  92. const widthParameter = line.replace(/width=|"/g, '');
  93. return {width: widthParameter};
  94. } else if (line.includes('layout')) {
  95. const layoutParameter = line.replace(/layout=|"|>/g, '');
  96. }
  97. }
  98. var blockArray = [];
  99. var blockCount = 0;
  100. var parseBlockDiv, index;
  101. var pre_img, pre_imgObject;
  102. var isNotFrontMatterCount = 0;
  103. var isAmpImgRange = false;
  104. for (const line of content.split('\n')) {
  105. if (isNotFrontMatterCount < 2) {
  106. if (line.includes('---')) {
  107. isNotFrontMatterCount += 1;
  108. }
  109. continue;
  110. }
  111. if (line.includes('##')) {
  112. index = blockCount;
  113. blockCount = blockCount + 1;
  114. const {blockDiv, h, titleInput, inputButton, descButton, imgButton} = getBlockElements();
  115. parseBlockDiv = blockDiv;
  116. const title = parseTitle(line);
  117. const text = title;
  118. blockArray[index] = {'title': text};
  119. h.textContent = text;
  120. inputButton.onclick = function() {
  121. const text = titleInput.value;
  122. blockArray[index] = {'title': text};
  123. h.textContent = text;
  124. }
  125. } else if (isAmpImgRange === true) {
  126. if (line.includes('</amp-img>')) {
  127. blockArray = addDataToBlockArray({'image': pre_imgObject}, blockArray, index)
  128. isAmpImgRange = false;
  129. } else {
  130. imgParamObject = parseAmpImg(line);
  131. const key = _.get(_.keys(imgParamObject), 0);
  132. pre_img.setAttribute(key, _.get(imgParamObject, `${key}`));
  133. }
  134. } else if (line.includes('<amp-img')) {
  135. isAmpImgRange = true;
  136. const {img, imgInput, widthInput, heightInput, imgInputButton} = getImgElements(parseBlockDiv);
  137. pre_img = img;
  138. imgInputButton.onclick = function() {
  139. pre_img.setAttribute('src', imgInput.value);
  140. pre_img.setAttribute('width', widthInput.value);
  141. pre_img.setAttribute('height', heightInput.value);
  142. pre_img.setAttribute('alt', 'image field');
  143. const imgObject = {image: {src: imgInput.value,
  144. height: heightInput.value,
  145. width: widthInput.value,
  146. alt: 'image field',
  147. layout: 'responsive'}};
  148. blockArray = addDataToBlockArray(imgObject, blockArray, index)
  149. }
  150. } else {
  151. const {p, descTextArea, descInputButton} = getdescElements(parseBlockDiv);
  152. const text = line;
  153. p.textContent = text;
  154. blockArray = addDataToBlockArray({description: {text: text}}, blockArray, index)
  155. descInputButton.onclick = function () {
  156. const text = descTextArea.value;
  157. p.textContent = text;
  158. addDataToBlockArray({description: {text: text}}, blockArray, index)
  159. }
  160. }
  161. }
  162. return {blockArray: blockArray, blockCount: blockCount}
  163. }
  164. const content = `---
  165. title: "公仔與爵士鼓"
  166. date: 2021-02-23T13:18:22+08:00
  167. draft: false
  168. type: "collection"
  169. url: "/collection/doll_and_drum_set"
  170. image: "https://image-cdn-flare.qdm.cloud/q57568769580e2/image/data/2020/11/09/7f6087c94211080bfe2edfb0180891a7.jpg"
  171. description: "很喜歡現代風格居家設計,利用大理石紋路牆面作為客廳的......"
  172. ---
  173. ## **空間主人|公仔控 \- 陳氏夫妻檔**\n\
  174. \n\
  175. 初次見到陳先生,其實在心裡偷偷覺得,他好像有點嚴肅、不太笑,跟我們打個招呼後,就趕著出門上課了,但實際上多相處後才發現,「外冷內熱」比較貼近真實的他。
  176. <amp-img
  177. alt="小寶優居"
  178. src="https://image-cdn-flare.qdm.cloud/q57568769580e2/image/data/2020/11/10/7200f7c668635a2ef399723c5410ca90.jpg"
  179. height="340"
  180. width="604"
  181. layout="responsive">
  182. </amp-img>
  183. `
  184. const contentDiv = document.getElementsByClassName('ui segment')[0];
  185. const titleButton = document.getElementById('title_button');
  186. const submitButton = document.getElementById('submit_button');
  187. var {blockArray, blockCount} = parseMd(content);
  188. titleButton.onclick = function() {
  189. const index = blockCount;
  190. blockCount = blockCount + 1;
  191. const {blockDiv, h, titleInput, inputButton, descButton, imgButton} = getBlockElements();
  192. inputButton.onclick = function() {
  193. const text = titleInput.value;
  194. blockArray[index] = {'title': text};
  195. h.textContent = text;
  196. }
  197. descButton.onclick = function() {
  198. const {p, descTextArea, descInputButton} = getdescElements(blockDiv);
  199. descInputButton.onclick = function() {
  200. const text = descTextArea.value;
  201. p.textContent = text;
  202. addDataToBlockArray({description: {text: text}}, blockArray, index)
  203. }
  204. }
  205. imgButton.onclick = function() {
  206. const {img, imgInput, widthInput, heightInput, imgInputButton} = getImgElements(blockDiv);
  207. imgInputButton.onclick = function() {
  208. img.setAttribute('src', imgInput.value);
  209. img.setAttribute('width', widthInput.value);
  210. img.setAttribute('height', heightInput.value);
  211. img.setAttribute('alt', 'image field');
  212. const imgObject = {image: {src: imgInput.value,
  213. height: heightInput.value,
  214. width: widthInput.value,
  215. alt: 'image field',
  216. layout: 'responsive'}};
  217. blockArray = addDataToBlockArray(imgObject, blockArray, index)
  218. }
  219. }
  220. }
  221. submitButton.onclick = function() {
  222. var mdContent = '';
  223. for (var idx=0; idx<blockArray.length; idx++) {
  224. mdContent += `## **${_.get(blockArray[idx], 'title', '')}**\n`
  225. for (var data of _.get(blockArray[idx], 'data', [])) {
  226. if (_.get(_.keys(data), 0) === 'description') {
  227. mdContent += _.get(data, 'description.text', '') + '\n';
  228. } else if (_.get(_.keys(data), 0) === 'image') {
  229. ampImgForm = `<amp-img\
  230. \n alt="${_.get(data, 'image.alt', '小寶優居')}"\
  231. \n src="${_.get(data, 'image.src', '')}"\
  232. \n height="${_.get(data, 'image.height', 300)}"\
  233. \n width="${_.get(data, 'image.width', 400)}"\
  234. \n layout="${_.get(data, 'image.layout', 'responsive')}>"\
  235. \n</amp-img>`;
  236. mdContent += ampImgForm;
  237. }
  238. }
  239. }
  240. console.log(mdContent);
  241. console.log(blockArray);
  242. }