editor.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. blockCount = blockCount + 1 | 0;
  22. const blockIndex = blockCount;
  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. blockArray = handleDescInputClick(p, descTextArea.value, blockArray, blockindex, ownDataIndex);
  40. }
  41. }
  42. imgButton.onclick = function() {
  43. const imgDataIndex = dataIndex;
  44. dataIndex += 1;
  45. const {img, imgInput, widthInput, heightInput, imgInputButton} = getImgElements(blockDiv);
  46. imgInputButton.onclick = function() {
  47. const imgObject = {image: {src: imgInput.value,
  48. height: heightInput.value,
  49. width: widthInput.value,
  50. alt: 'image field',
  51. layout: 'responsive'}};
  52. blockArray = handleImgInputClick(img, imgObject, blockArray, blockIndex, imgDataIndex);
  53. }
  54. }
  55. }
  56. submitButton.onclick = function() {
  57. var mdContent = '';
  58. for (var idx=0; idx<blockArray.length; idx++) {
  59. mdContent += `## **${_.get(blockArray[idx], 'title', '')}**\n`
  60. for (var data of _.get(blockArray[idx], 'data', [])) {
  61. if (_.get(_.keys(data), 0) === 'description') {
  62. mdContent += _.get(data, 'description.text', '') + '\n';
  63. } else if (_.get(_.keys(data), 0) === 'image') {
  64. ampImgForm = `<amp-img\
  65. \n alt="${_.get(data, 'image.alt', '小寶優居')}"\
  66. \n src="${_.get(data, 'image.src', '')}"\
  67. \n height="${_.get(data, 'image.height', 300)}"\
  68. \n width="${_.get(data, 'image.width', 400)}"\
  69. \n layout="${_.get(data, 'image.layout', 'responsive')}>"\
  70. \n</amp-img>`;
  71. mdContent += ampImgForm;
  72. }
  73. }
  74. }
  75. console.log(mdContent);
  76. console.log(blockArray);
  77. }
  78. });