EditorsPage.js 3.1 KB

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