EditorsPage.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { useState, useEffect } from "react";
  2. import { Editor } from "../components/Editor";
  3. import { SelectTheme } from "../helpers/SelectTheme";
  4. import { SelectFontSize } from "../helpers/SelectFontSize";
  5. import "./EditorsPage.css";
  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
  107. srcDoc={srcDoc}
  108. title="output"
  109. sandbox="allow-scripts"
  110. frameBorder="0"
  111. width="100%"
  112. height="100%"
  113. />
  114. </div>
  115. <p className="text">Name of your project: </p>
  116. <input
  117. className="input_title"
  118. placeholder="Name of your project"
  119. value={title}
  120. onChange={(e) => setTitle(e.target.value)}
  121. />
  122. <p className="text">Description: </p>
  123. <textarea
  124. className="text_desc"
  125. placeholder="Description"
  126. value={description}
  127. onChange={(e) => setDescription(e.target.value)}
  128. />
  129. <button
  130. className="button_submit"
  131. onClick={() => onSave(title, description, editors)}
  132. >
  133. Submit this Editor
  134. </button>
  135. </>
  136. );
  137. };