实时 html 编辑器可供下载

问题描述 投票:0回答:2

我喜欢这篇文章中的解决方案Real-Time Html editor with Javascriptthis site上的编辑器。只是简单的解决方案,没有额外的功能。

如何编辑代码以添加SAVE/LOAD选项?有没有像“可下载的小 CMS”之类的东西可以玩 HTML/CSS? 我想将其上传到我的主机,可以从家里/电话/工作轻松访问,我不想使用像codepen或Liveweave这样的在线服务。

谢谢!

编辑:由于评论,我将澄清我的问题。我有一些托管,mySite.com。有一个带有这个神奇编辑器的文件夹,mySite.com/xxx,其中有一些示例图像和一些基本的 css 等。我正在学习 html/css,所以我开发了一些基本的 html temapltes。我链接的 HTML 编辑器就很好。只需添加 3 个按钮,新建/打开/保存,它们可以创建新的 html 文件/可以在实时编辑器中打开它/并保存它。我希望在我自己的托管上拥有这个简单的解决方案。

php html download editor real-time
2个回答
1
投票
<?php
$fileName = "page.html";
$fileContent = fopen($fileName, "r") or die("Unable to open file!");
if (isset($_POST['text'])) {
    file_put_contents($fileName, $_POST["text"]);
}
?>

<!DOCTYPE html><html lang="cs"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<style> 
.error {background-color: red; color: white;}
</style>

</head><body>

<form method="POST">
<textarea name="text" class="form-control" rows="20" id="pure">
<?php echo fread($fileContent,filesize($fileName)); ?> 
</textarea><br>
<input type="submit" value="submit" />
</form>
<hr>
<div id="compiled"></div>
</body>
</html>

 <?php
fclose($fileContent);
?> 

<script type="text/javascript">
var h = document.getElementById("pure");
var compiled = document.getElementById("compiled");
h.onkeyup = function() {
  compiled.innerHTML = h.value;
  pure.classList.toggle("error",
    compiled.innerHTML !== h.value); 
};
h.onkeyup();
</script>

这是我的实际工作,来自我问题中链接的实时 html 编辑器。文件“page.html”必须存在。

新增功能。页面加载时将“page.html”文件中的内容加载到文本区域..

添加按钮以在完成后将文本区域内容保存到“page.html”..

可能不是完美的代码,对于多个项目必须复制到不同的文件夹并一一加载:(没有弹出窗口或表单来轻松选择不同的文件名..但现在它符合我的要求。我可以学习/尝试/处理 html来自家庭/工作/手机的模板在我自己的托管上,无需登录第三方服务。


0
投票

好的,我完成了。这是我的最终解决方案。

  • 实时 html 编辑器
  • 加载/保存按钮
  • 自动将 /projects/ 文件夹中的文件加载到下拉列表中
  • 您可以上传文件来制作新项目
  • 无需修改任何东西,直接使用即可。

索引.php

<?php
$fileName = $_POST['project']?? 'index.html';
$fileContent = fopen("./projects/" . $fileName, "r") or die("Unable to open file!");
if (isset($_POST['text'])) {
    file_put_contents("./projects/" . $fileName, $_POST["text"]);
}
?>

<!DOCTYPE html><html lang="cs"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<style> 
.error {background-color:red; color:white;}
.dib {display:inline-block;}
</style>
</head><body>

<?php 
echo "<form class='dib' method='POST'><select name='project'>";
$path    = './projects/';
$files = scandir($path);
$files = array_diff(scandir($path), array('.', '..'));
foreach($files as $file){
  echo "<option" . (($fileName == $file)?' selected':'') . ">" . $file . "</option>";
} 
echo "</select> <input type='submit' value='Load!'></form>";
?>

<input type='submit' form='content' value='Save!'> 

    <form class="dib" style="float:right;" action="fileUploadScript.php" method="post" enctype="multipart/form-data">
        Upload a File:
        <input type="file" name="the_file" id="fileToUpload">
        <input type="submit" name="submit" value="Start Upload">
    </form>

<p style='margin:auto; text-align:center; width:20%; text-transform:uppercase; font-weight: bold;'><?php echo $fileName?></p>

<br>
<form id="content" method="POST">
<textarea name="text" rows="40" id="pure" style="width:100%;margin-top:8px;" wrap="off">
<?php echo fread($fileContent,filesize("./projects/" . $fileName)); ?> 
</textarea><br>
<input type="hidden" name="project" value="<?php echo $fileName; ?>">
</form>
<hr>
<div id="compiled"></div>
</body>
</html>

 <?php
fclose($fileContent);
?> 

<script type="text/javascript">
var h = document.getElementById("pure");
var compiled = document.getElementById("compiled");
h.onkeyup = function() {
  compiled.innerHTML = h.value;
  pure.classList.toggle("error",
    compiled.innerHTML !== h.value); 
};
h.onkeyup();
</script>

文件上传脚本.php

<?php
    $currentDirectory = getcwd();
    $uploadDirectory = "/projects/";

    $errors = []; // Store errors here

    $fileExtensionsAllowed = ['jpeg','jpg','txt','bmp','html','htm','rar','zip','7z','doc','docx','xls','xlsx','ppt','pptx','pdf','pptm','png','gif']; // These will be the only file extensions allowed 

    $fileNamee = $_FILES['the_file']['name'];
    $fileSize = $_FILES['the_file']['size'];
    $fileTmpName  = $_FILES['the_file']['tmp_name'];
    $fileType = $_FILES['the_file']['type'];
    $tmp = explode('.',$fileNamee);
    $fileExtension = strtolower(end($tmp));

    $uploadPath = $currentDirectory . $uploadDirectory . basename($fileNamee); 

    if (isset($_POST['submit'])) {

      if (! in_array($fileExtension,$fileExtensionsAllowed)) {
        $errors[] = "This file extension is not allowed. Please upload a JPEG or PNG file";
      }

      if ($fileSize > 4000000) {
        $errors[] = "File exceeds maximum size (4MB)";
      }

      if (empty($errors)) {
        $didUpload = move_uploaded_file($fileTmpName, $uploadPath);

        if ($didUpload) {
          echo "The file " . basename($fileNamee) . " has been uploaded";
        } else {
          echo "An error occurred. Please contact the administrator.";
        }
      } else {
        foreach ($errors as $error) {
          echo $error . "These are the errors" . "\n";
        }
      }

    }
?>

保存这两个文件后,不要忘记在此文件夹中创建名为“projects”的新文件夹和名为“index.html”的默认文件..

© www.soinside.com 2019 - 2024. All rights reserved.