EditorsPage.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { useState, useEffect } from "react";
  2. import { SelectTheme } from "helpers/SelectTheme";
  3. import { SelectFontSize } from "helpers/SelectFontSize";
  4. import "./EditorsPage.css";
  5. import { Editor } from "./Editor";
  6. const datas = [
  7. {
  8. type: "html",
  9. name: "",
  10. text: `<!DOCTYPE html>
  11. <h1 id="codepen">Welcome to the Codepen</h1>`,
  12. },
  13. {
  14. type: "css",
  15. name: "",
  16. text: `#codepen {
  17. color: blue;
  18. font-size: 25px;
  19. font-family: Times New Roman;
  20. }`,
  21. },
  22. {
  23. type: "javascript",
  24. name: "",
  25. text: `let btn = document.createElement('button');
  26. btn.innerHTML = 'Change color';
  27. btn.style.color = 'red';
  28. btn.style.fontSize = '20px';
  29. document.body.append(btn);
  30. btn.style.cursor = 'pointer';
  31. btn.onclick = () => {
  32. document.getElementById('codepen').style.color = 'red'};`,
  33. },
  34. ];
  35. export const EditorsPage = ({ onSave }) => {
  36. const [editors, setEditors] = useState(datas);
  37. const [title, setTitle] = useState("");
  38. const [description, setDescription] = useState("");
  39. const [srcDoc, setSrcDoc] = useState("");
  40. const [theme, setTheme] = useState();
  41. const [font, setFont] = useState();
  42. useEffect(() => {
  43. const timeout = setTimeout(() => {
  44. let html, css, javascript;
  45. editors.forEach(e => {
  46. if (e.type === "html") html = e.text;
  47. if (e.type === "css") css = e.text;
  48. if (e.type === "javascript") javascript = e.text;
  49. });
  50. setSrcDoc(`
  51. <html>
  52. <body>${html}</body>
  53. <style>${css}</style>
  54. <script>${javascript}</script>
  55. </html>
  56. `);
  57. }, 250);
  58. return () => clearTimeout(timeout);
  59. }, [editors]);
  60. return (
  61. <>
  62. <div className='styles'>
  63. {"Theme: "}
  64. <SelectTheme onChange={setTheme} value={theme} />
  65. <br />
  66. {"FontSize: "}
  67. <SelectFontSize onChange={setFont} value={font} />
  68. <br />
  69. </div>
  70. {editors.map((data, index) => {
  71. return (
  72. <Editor
  73. data={data}
  74. key={index}
  75. theme={theme}
  76. font={font}
  77. onChange={newData => {
  78. let editor = [...editors];
  79. editor.splice(index, 1, newData);
  80. setEditors(editor);
  81. }}
  82. />
  83. );
  84. })}
  85. <button
  86. className='button_plus'
  87. onClick={newArray => {
  88. let editors2 = [...editors];
  89. editors2.push(newArray);
  90. setEditors(editors2);
  91. }}
  92. >
  93. +
  94. </button>
  95. <button
  96. className='button_plus'
  97. onClick={newArray => {
  98. let editors2 = [...editors];
  99. editors2.pop(newArray);
  100. setEditors(editors2);
  101. }}
  102. >
  103. -
  104. </button>
  105. <div className='pane'>
  106. <iframe srcDoc={srcDoc} title='output' sandbox='allow-scripts' frameBorder='0' width='100%' height='100%' />
  107. </div>
  108. <p className='text'>Name of your project: </p>
  109. <input
  110. className='input_title'
  111. placeholder='Name of your project'
  112. value={title}
  113. onChange={e => setTitle(e.target.value)}
  114. />
  115. <p className='text'>Description: </p>
  116. <textarea
  117. className='text_desc'
  118. placeholder='Description'
  119. value={description}
  120. onChange={e => setDescription(e.target.value)}
  121. />
  122. <button className='button_submit' onClick={() => onSave(title, description, editors)}>
  123. Submit this Editor
  124. </button>
  125. </>
  126. );
  127. };