editor.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.appendChild(blockDiv);
  13. blockDiv.append(h, input, inputButton, descButton, imgButton);
  14. return {
  15. blockDiv: blockDiv,
  16. h: h,
  17. titleInput: input,
  18. inputButton: inputButton,
  19. descButton: descButton,
  20. imgButton: imgButton
  21. };
  22. }
  23. function getdescElements(blockDiv) {
  24. const descDiv = document.createElement("div");
  25. const p = document.createElement("p");
  26. const descTextArea = document.createElement("TEXTAREA");
  27. descTextArea.setAttribute("type", "text");
  28. const descInputButton = document.createElement('button')
  29. descInputButton.textContent = 'Description新增/修改';
  30. blockDiv.appendChild(descDiv);
  31. descDiv.append(p, descTextArea, descInputButton);
  32. return {
  33. p: p,
  34. descTextArea: descTextArea,
  35. descInputButton: descInputButton
  36. };
  37. }
  38. function getImgElements(blockDiv) {
  39. const imgDiv = document.createElement("div");
  40. const img = document.createElement("img");
  41. const imgInput = document.createElement("INPUT");
  42. const widthInput = document.createElement("INPUT");
  43. const heightInput = document.createElement("INPUT");
  44. const imgInputButton = document.createElement('button');
  45. imgInputButton.textContent = 'Image新增/修改';
  46. blockDiv.appendChild(imgDiv);
  47. imgDiv.append(img, imgInput, widthInput, heightInput, imgInputButton);
  48. return {
  49. img: img,
  50. imgInput: imgInput,
  51. widthInput: widthInput,
  52. heightInput: heightInput,
  53. imgInputButton: imgInputButton
  54. };
  55. }
  56. function addDataToBlockArray(dataObject, blockArray, blockIndex, dataIndex) {
  57. if ('data' in blockArray[blockIndex]) {
  58. blockArray[blockIndex].data[dataIndex] = dataObject
  59. } else {
  60. blockArray[blockIndex].data = [dataObject];
  61. }
  62. return blockArray;
  63. }
  64. function parseMd(content) {
  65. const parseTitle = line => {
  66. var title = '';
  67. title = line.replace('## **', '');
  68. title = title.replace('**', '');
  69. return title;
  70. };
  71. const parseAmpImg = line => {
  72. if (line.includes('alt')) {
  73. const altParameter = line.replace(/alt=|"/g, '');
  74. return {alt: altParameter};
  75. } else if (line.includes('src')) {
  76. const srcParameter = line.replace(/src=|"/g, '');
  77. return {src: srcParameter};
  78. } else if (line.includes('height')) {
  79. const heightParameter = line.replace(/height=|"/g, '');
  80. return {height: heightParameter};
  81. } else if (line.includes('width')) {
  82. const widthParameter = line.replace(/width=|"/g, '');
  83. return {width: widthParameter};
  84. } else if (line.includes('layout')) {
  85. const layoutParameter = line.replace(/layout=|"|>/g, '');
  86. }
  87. }
  88. var blockArray = [{title: ''}];
  89. var blockCount;
  90. var preDataIndex, imgDataIndex;
  91. var parseBlockDiv;
  92. var preImg, preImgObject;
  93. var preImgObject = {'image': {}}
  94. var isNotFrontMatterCount = 0;
  95. var isAmpImgRange = false;
  96. for (const line of content.split('\n')) {
  97. if (isNotFrontMatterCount < 2) {
  98. if (line.includes('---')) {
  99. isNotFrontMatterCount += 1;
  100. }
  101. continue;
  102. }
  103. if (isAmpImgRange === true && !(line.includes('</amp-img>'))) {
  104. imgParamObject = parseAmpImg(line);
  105. preImgObject.image = {...preImgObject.image, ...imgParamObject};
  106. const key = _.get(_.keys(imgParamObject), 0);
  107. preImg.setAttribute(key, _.get(imgParamObject, `${key}`));
  108. continue;
  109. }
  110. if (line.includes('##')) {
  111. var preDataIndex = 0;
  112. blockCount = blockCount + 1 | 0;
  113. const preBlockindex = blockCount;
  114. const {blockDiv, h, titleInput, inputButton, descButton, imgButton} = getBlockElements();
  115. parseBlockDiv = blockDiv;
  116. const title = parseTitle(line);
  117. const text = title;
  118. blockArray[preBlockindex] = {title: text};
  119. h.textContent = text;
  120. inputButton.onclick = function() {
  121. const text = titleInput.value;
  122. blockArray[preBlockindex].title = text;
  123. h.textContent = text;
  124. }
  125. } else if (line.includes('amp-img')) {
  126. const preBlockindex = blockCount;
  127. if (line.includes('</amp-img>')) {
  128. blockArray = addDataToBlockArray(preImgObject, blockArray, preBlockindex, imgDataIndex)
  129. isAmpImgRange = false;
  130. continue;
  131. }
  132. imgDataIndex = preDataIndex;
  133. preDataIndex += 1;
  134. isAmpImgRange = true;
  135. const {img, imgInput, widthInput, heightInput, imgInputButton} = getImgElements(parseBlockDiv);
  136. preImg = img;
  137. imgInputButton.onclick = function() {
  138. preImg.setAttribute('src', imgInput.value);
  139. preImg.setAttribute('width', widthInput.value);
  140. preImg.setAttribute('height', heightInput.value);
  141. preImg.setAttribute('alt', 'image field');
  142. const imgObject = {image: {src: imgInput.value,
  143. height: heightInput.value,
  144. width: widthInput.value,
  145. alt: 'image field',
  146. layout: 'responsive'}};
  147. blockArray = addDataToBlockArray(imgObject, blockArray, preBlockindex, imgDataIndex)
  148. }
  149. } else {
  150. const preBlockindex = blockCount;
  151. const ownDataIndex = preDataIndex;
  152. preDataIndex += 1;
  153. const {p, descTextArea, descInputButton} = getdescElements(parseBlockDiv);
  154. const text = line;
  155. p.textContent = text;
  156. blockArray = addDataToBlockArray({description: {text: text}}, blockArray, preBlockindex, ownDataIndex)
  157. descInputButton.onclick = function () {
  158. const text = descTextArea.value;
  159. p.textContent = text;
  160. addDataToBlockArray({description: {text: text}}, blockArray, preBlockindex, ownDataIndex)
  161. }
  162. }
  163. }
  164. return {blockArray: blockArray, blockCount: blockCount}
  165. }
  166. const contentDiv = document.getElementsByClassName('ui segment')[0];
  167. const titleButton = document.getElementById('title_button');
  168. const submitButton = document.getElementById('submit_button');
  169. axios.get('/公仔與爵士鼓.md').then(function(response) {
  170. const content = response.data;
  171. var {blockArray, blockCount} = parseMd(content);
  172. var dataIndex;
  173. titleButton.onclick = function() {
  174. dataIndex = 0;
  175. const blockIndex = blockCount;
  176. blockCount = blockCount + 1;
  177. const {blockDiv, h, titleInput, inputButton, descButton, imgButton} = getBlockElements();
  178. inputButton.onclick = function() {
  179. const text = titleInput.value;
  180. if (blockIndex < blockArray.length) {
  181. blockArray[blockIndex].title = text;
  182. h.textContent = text;
  183. } else {
  184. blockArray[blockIndex] = {title: text};
  185. h.textContent = text;
  186. }
  187. }
  188. descButton.onclick = function() {
  189. const ownDataIndex = dataIndex;
  190. dataIndex += 1;
  191. const {p, descTextArea, descInputButton} = getdescElements(blockDiv);
  192. descInputButton.onclick = function() {
  193. const text = descTextArea.value;
  194. p.textContent = text;
  195. addDataToBlockArray({description: {text: text}}, blockArray, blockIndex, ownDataIndex)
  196. }
  197. }
  198. imgButton.onclick = function() {
  199. const ownDataIndex = dataIndex;
  200. dataIndex += 1;
  201. const {img, imgInput, widthInput, heightInput, imgInputButton} = getImgElements(blockDiv);
  202. imgInputButton.onclick = function() {
  203. img.setAttribute('src', imgInput.value);
  204. img.setAttribute('width', widthInput.value);
  205. img.setAttribute('height', heightInput.value);
  206. img.setAttribute('alt', 'image field');
  207. const imgObject = {image: {src: imgInput.value,
  208. height: heightInput.value,
  209. width: widthInput.value,
  210. alt: 'image field',
  211. layout: 'responsive'}};
  212. blockArray = addDataToBlockArray(imgObject, blockArray, blockIndex, ownDataIndex)
  213. }
  214. }
  215. }
  216. submitButton.onclick = function() {
  217. var mdContent = '';
  218. for (var idx=0; idx<blockArray.length; idx++) {
  219. mdContent += `## **${_.get(blockArray[idx], 'title', '')}**\n`
  220. for (var data of _.get(blockArray[idx], 'data', [])) {
  221. if (_.get(_.keys(data), 0) === 'description') {
  222. mdContent += _.get(data, 'description.text', '') + '\n';
  223. } else if (_.get(_.keys(data), 0) === 'image') {
  224. ampImgForm = `<amp-img\
  225. \n alt="${_.get(data, 'image.alt', '小寶優居')}"\
  226. \n src="${_.get(data, 'image.src', '')}"\
  227. \n height="${_.get(data, 'image.height', 300)}"\
  228. \n width="${_.get(data, 'image.width', 400)}"\
  229. \n layout="${_.get(data, 'image.layout', 'responsive')}>"\
  230. \n</amp-img>`;
  231. mdContent += ampImgForm;
  232. }
  233. }
  234. }
  235. console.log(mdContent);
  236. console.log(blockArray);
  237. }
  238. });