EditorsPage.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 = (
  36. data = { type: "", name: "", text: "", index: "", theme: "", font: "" },
  37. onSave,
  38. onChange
  39. ) => {
  40. const [editors, setEditors] = useState(datas);
  41. const [title, setTitle] = useState("");
  42. const [description, setDescription] = useState("");
  43. const [srcDoc, setSrcDoc] = useState("");
  44. useEffect(() => {
  45. const timeout = setTimeout(() => {
  46. let html, css, javascript;
  47. editors.forEach((e) => {
  48. if (e.type === "html") html = e.text;
  49. if (e.type === "css") css = e.text;
  50. if (e.type === "javascript") javascript = e.text;
  51. });
  52. setSrcDoc(`
  53. <html>
  54. <body>${html}</body>
  55. <style>${css}</style>
  56. <script>${javascript}</script>
  57. </html>
  58. `);
  59. }, 250);
  60. return () => clearTimeout(timeout);
  61. }, [editors]);
  62. const changeTheme = (theme) => onChange({ theme });
  63. const changeFont = (font) => onChange({ font });
  64. return (
  65. <>
  66. <div className="styles">
  67. {"Theme: "}
  68. <SelectTheme onChange={() => changeTheme} value={data.theme} />
  69. <br />
  70. {"FontSize: "}
  71. <SelectFontSize onChange={() => changeFont} value={data.font} />
  72. <br />
  73. </div>
  74. {editors.map((data, index) => {
  75. return (
  76. <Editor
  77. data={data}
  78. key={index}
  79. theme='monokai'
  80. font={16}
  81. onChange={(newData) => {
  82. let editor = [...editors];
  83. editor.splice(index, 1, newData);
  84. setEditors(editor);
  85. }}
  86. />
  87. );
  88. })}
  89. <button
  90. className="button_plus"
  91. onClick={(newArray) => {
  92. let editors2 = [...editors];
  93. editors2.push(newArray);
  94. setEditors(editors2);
  95. }}
  96. >
  97. +
  98. </button>
  99. <button
  100. className="button_plus"
  101. onClick={(newArray) => {
  102. let editors2 = [...editors];
  103. editors2.pop(newArray);
  104. setEditors(editors2);
  105. }}
  106. >
  107. -
  108. </button>
  109. <div className="pane">
  110. <iframe
  111. srcDoc={srcDoc}
  112. title="output"
  113. sandbox="allow-scripts"
  114. frameBorder="0"
  115. width="100%"
  116. height="100%"
  117. />
  118. </div>
  119. <p className="text">Name of your project: </p>
  120. <input
  121. className="input_title"
  122. placeholder="Name of your project"
  123. value={title}
  124. onChange={(e) => setTitle(e.target.value)}
  125. />
  126. <p className="text">Description: </p>
  127. <textarea
  128. className="text_desc"
  129. placeholder="Description"
  130. value={description}
  131. onChange={(e) => setDescription(e.target.value)}
  132. />
  133. <button
  134. className="button_submit"
  135. onClick={() => onSave(title, description, editors)}
  136. >
  137. Submit this Editor
  138. </button>
  139. </>
  140. );
  141. };