editor.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. function addDataToBlockArray(dataObject, blockArray, blockIndex, dataIndex) {
  2. if ('data' in blockArray[blockIndex]) {
  3. blockArray[blockIndex].data[dataIndex] = dataObject
  4. } else {
  5. blockArray[blockIndex].data = [dataObject];
  6. }
  7. return blockArray;
  8. }
  9. function isIndexExistInBlockArray(blockIndex, length) {
  10. return blockIndex < length;
  11. }
  12. const contentDiv = document.getElementsByClassName('ui segment')[0];
  13. const titleButton = document.getElementById('title_button');
  14. const submitButton = document.getElementById('submit_button');
  15. axios.get('/公仔與爵士鼓.md').then(function(response) {
  16. const content = response.data;
  17. var {blockArray, blockCount} = parseMd(content);
  18. var dataIndex;
  19. titleButton.onclick = function() {
  20. dataIndex = 0;
  21. const blockIndex = blockCount;
  22. blockCount = blockCount + 1;
  23. const {blockDiv, h, titleInput, inputButton, descButton, imgButton} = getBlockElements(contentDiv);
  24. inputButton.onclick = function() {
  25. const text = titleInput.value;
  26. if (isIndexExistInBlockArray(blockIndex, blockArray.length)) {
  27. blockArray[blockIndex].title = text;
  28. h.textContent = text;
  29. } else {
  30. blockArray[blockIndex] = {title: text};
  31. h.textContent = text;
  32. }
  33. }
  34. descButton.onclick = function() {
  35. const ownDataIndex = dataIndex;
  36. dataIndex += 1;
  37. const {p, descTextArea, descInputButton} = getdescElements(blockDiv);
  38. descInputButton.onclick = function() {
  39. const text = descTextArea.value;
  40. p.textContent = text;
  41. addDataToBlockArray({description: {text: text}}, blockArray, blockIndex, ownDataIndex)
  42. }
  43. }
  44. imgButton.onclick = function() {
  45. const ownDataIndex = dataIndex;
  46. dataIndex += 1;
  47. const {img, imgInput, widthInput, heightInput, imgInputButton} = getImgElements(blockDiv);
  48. imgInputButton.onclick = function() {
  49. img.setAttribute('src', imgInput.value);
  50. img.setAttribute('width', widthInput.value);
  51. img.setAttribute('height', heightInput.value);
  52. img.setAttribute('alt', 'image field');
  53. const imgObject = {image: {src: imgInput.value,
  54. height: heightInput.value,
  55. width: widthInput.value,
  56. alt: 'image field',
  57. layout: 'responsive'}};
  58. blockArray = addDataToBlockArray(imgObject, blockArray, blockIndex, ownDataIndex)
  59. }
  60. }
  61. }
  62. submitButton.onclick = function() {
  63. var mdContent = '';
  64. for (var idx=0; idx<blockArray.length; idx++) {
  65. mdContent += `## **${_.get(blockArray[idx], 'title', '')}**\n`
  66. for (var data of _.get(blockArray[idx], 'data', [])) {
  67. if (_.get(_.keys(data), 0) === 'description') {
  68. mdContent += _.get(data, 'description.text', '') + '\n';
  69. } else if (_.get(_.keys(data), 0) === 'image') {
  70. ampImgForm = `<amp-img\
  71. \n alt="${_.get(data, 'image.alt', '小寶優居')}"\
  72. \n src="${_.get(data, 'image.src', '')}"\
  73. \n height="${_.get(data, 'image.height', 300)}"\
  74. \n width="${_.get(data, 'image.width', 400)}"\
  75. \n layout="${_.get(data, 'image.layout', 'responsive')}>"\
  76. \n</amp-img>`;
  77. mdContent += ampImgForm;
  78. }
  79. }
  80. }
  81. console.log(mdContent);
  82. console.log(blockArray);
  83. }
  84. });