1. 程式人生 > >Slog73_ArthurSlog_Markdown編輯器!(專案上線之ArthurSlog個人網站上線5)

Slog73_ArthurSlog_Markdown編輯器!(專案上線之ArthurSlog個人網站上線5)

  • ArthurSlog
  • SLog-73
  • Year·1

  • Guangzhou·China

  • September 15th 2018

關注微信公眾號“ArthurSlog”

一尺之棰 日取其半 萬世不竭

開發環境MacOS(High Sierra 10.13.6 (17G65))

需要的資訊和資訊源:

開始編碼

  • 這個功能就是 Markdown編輯器模組

  • 簡單點說,Markdwon檔案 使用易讀易寫的純文字格式編寫文件,然後轉換成有效的XHTML(或者HTML)文件

  • 這個換裝的過程就需要一個轉換器,這裡我們使用 Markdown-it,具體介紹 在此

  • 在這裡選擇下載markdown-it.js檔案,在 html檔案裡直接引用

  • 這裡把html、css、js都一起寫在一個文件裡,簡單直接一點

ClientFile: ArthurSlog.com/Markdown_Editor.html

<!DOCTYPE html>
<html lang="en">

<head>
    <title>ArthurSlog_MarkdownEditor</title>
    <meta charset="UTF-8">
    <script src="./markdown-it.js"></script>
</head>
<body> <div id="container"> <!-- ArthurSlog_Manual_Area --> <div id="editorManual"> <div id="editorManual_Title">ArthurSlog 創作中心</div> <div id="editorManual_Tool">工具欄(建設中...)</div> </div> <div
id="editorContent">
<!-- ArthurSlog_MarkdownEditor_Area --> <div id="sourceCode_Markdown" contenteditable="true"></div> <div id="middleLine"></div> <!-- ArthurSlog_MarkdownToHTML_AREA --> <div id="resultCode_HTML"></div> </div> </div> <script> // target.addEventListener(type, listener, options); // target.addEventListener(type, listener ,{capture: Boolean, passive: Boolean, once: Boolean}); // target.addEventListener(type, listener, useCapture); // target.addEventListener(type, listener[, useCapture, wantsUntrusted ]); // --- // 1. target = document.getElementById("basic") // 2. type = 'input' // 3. listener = document.getElementById("resultHTML").innerHTML = window.markdownit().render(document.getElementById("basic").value) // 4. options = false // => // 5. target.addEventListener(type, listener, options) document.getElementById("sourceCode_Markdown").addEventListener('input', function () { document.getElementById("resultCode_HTML").innerHTML = window.markdownit().render(document.getElementById("sourceCode_Markdown").innerText) }, false); //document.getElementById("sourceCode_Markdown").addEventListener('paste', function () { document.getElementById("sourceCode_Markdown").innerText = document.getElementById("sourceCode_Markdown").innerText.toString()}, false); document.getElementById("sourceCode_Markdown").style.boxShadow = "none"; document.getElementById("sourceCode_Markdown").style.textShadow = "none"; document.getElementById("sourceCode_Markdown").style.outlineColor = "none"; document.getElementById("sourceCode_Markdown").style.outlineStyle = "none"; document.getElementById("sourceCode_Markdown").style.outline = "none"; document.getElementById("sourceCode_Markdown").style.borderColor = "none"; </script> <style> html, body { height: 100%; display: block; overflow: hidden; } body { margin: 0; padding: 0; } #container { display: flex; flex-direction: column; position: absolute; margin: 0; padding: 0; } #EditorManual { display: flex; flex-direction: column; position: relative; margin-left: 0%; margin-right: 0%; height: 15vh; } #editorManual_Title { margin-left: 0%; margin-right: 0%; height: 10vh; background-color: ghostwhite; } #editorManual_Tool { margin-left: 0%; margin-right: 0%; height: 5vh; background-color: rgb(211, 250, 255); } #editorContent { display: flex; flex-direction: row; position: relative; margin: 0; padding: 0; } #sourceCode_Markdown { width: 47.5vw; height: 85vh; margin-left: 0%; margin-right: 0%; overflow: scroll; background-color: aliceblue; text-decoration:none; } #middleLine { width: 5vw; height: 85vh; overflow: hidden; background-color: ghostwhite; } #resultCode_HTML { width: 47.5vw; height: 85vh; margin-left: 0%; margin-right: 0%; overflow: scroll; background-color: ghostwhite; } </style> </body> </html>
  • 這裡有幾個需要關鍵的地方:

    1. 當div標籤出現 contenteditable屬性,且這個屬性為“true”的時候,這個div 會變成可編輯狀態,點選這裡詳細瞭解contenteditable 屬性

    2. 根據Markdown-it的官方說明,使用 window.markdownit().render(document.getElementById(“sourceCode_Markdown”).innerText)把輸入的文字轉換為Html

    3. 關閉輸入div標籤區域的邊框效果:

ClientFile: ArthurSlog.com/Markdown_Editor.html(script)

document.getElementById("sourceCode_Markdown").style.boxShadow = "none";
document.getElementById("sourceCode_Markdown").style.textShadow = "none";
document.getElementById("sourceCode_Markdown").style.outlineColor = "none";
document.getElementById("sourceCode_Markdown").style.outlineStyle = "none";
document.getElementById("sourceCode_Markdown").style.outline = "none";
document.getElementById("sourceCode_Markdown").style.borderColor = "none";
  1. 使用flex對頁面進行佈局

  2. 調整html和body的樣式,以達到覆蓋整個頁面的效果:

ClientFile: ArthurSlog.com/Markdown_Editor.html(style)

html,
body {
    height: 100%;
    display: block;
    overflow: hidden;
}

body {
    margin: 0;
    padding: 0;
}
  • 完成了編輯,現在給首頁加個連結先,直接跳轉過去

ClientFile: ArthurSlog.com/index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>ArthurSlog</title>
        <link rel="stylesheet" type="text/css" href="style.css" >
    </head>
    <body>
        <p>個人技術分享</p>
        <div>
            <a href="./Markdown_Editor.html">ArthurSlog_MarkdownEditor</a>
        </div>
        <footer>
            <a href="http://www.miitbeian.gov.cn">粵ICP備18088522號-1</a>
        </footer>
    </body>
</html>
  • 其中更新的部分是:

ClientFile: ArthurSlog.com/index.html

<div>
    <a href="./Markdown_Editor.html">ArthurSlog_MarkdownEditor</a>
</div>
  • 編輯器還有一些問題,後續再繼續更新優化

  • 至此,ArthurSlog的網站又新增新模組—-ArthurSlog_Markdown編輯器!。

歡迎關注我的微信公眾號 ArthurSlog

關注微信公眾號“ArthurSlog”

如果你喜歡我的文章 歡迎點贊 留言

謝謝