data:text/html,
<html contenteditable>
这是html5中用到的代码,只需要在浏览器地址输入,就是出现一个简陋的文本编辑器。
鉴于此,众多网友开始扩展编辑器的功能:
一:使用textarea标签代替html标签,可以添加自己喜欢的样式:
- data:text/html,
- <textarea style="font-size: 1.5em; width: 100%; height: 100%; border: none; outline: none" autofocus />
二:打字的时候编辑器的背景颜色会变化
- data:text/html,
- <html><head>
- <link href='http://fonts.lug.ustc.edu.cn/css?family=Open+Sans' rel='stylesheet' type='text/css'>
- <style type="text/css">
- html { font-family: "Open Sans" }
- * { -webkit-transition: all linear 1s; }
- </style>
- <script>
- window.onload=function(){
- var e=false;var t=0;
- setInterval(function(){
- if(!e){t=Math.round(Math.max(0,t-Math.max(t/3,1)))}var n=(255-t*2).toString(16);
- document.body.style.backgroundColor="#ff"+n+""+n},1e3);
- var n=null;document.onkeydown=function(){
- t=Math.min(128,t+2);e=true;clearTimeout(n);
- n=setTimeout(function(){e=false},1500)
- }
- }
- </script>
- </head>
- <body contenteditable style="font-size:2rem;line-height:1.4;max-width:60rem;margin:0 auto;padding:4rem;">
三:存储功能
- data:text/html,
- <button onClick="SaveTextArea()">Save</button>
- <script language="<a title="javascript" href="http://www.xyhtml5.com/javascript">javascript</a>" type="text/javascript">
- function SaveTextArea() {
- window.location = "data:application/octet-stream," + escape(txtBody.value); }
- </script>
- <textarea id="txtBody" style="font-size: 1.5em; width: 100%; height: 100%; boarder: none; outline: none" autofocus> </textarea>
四:自动保存功能
- data:text/html,
- <html lang="en"><head>
- <style> html,body { height: 100% } #note { width: 100%; height: 100% } </style>
- <script>
- var note, html, timeout;
- window.addEventListener('load', function() {
- note = document.getElementById('note');
- html = document.getElementsByTagName('html')[0];
- html.addEventListener('keyup', function(ev) { if (timeout) clearTimeout(timeout);
- timeout = setTimeout(saveNote, 100); }); restoreNote(); note.focus(); });
- function saveNote() {
- localStorage.note = note.innerText; timeout = null; }
- function restoreNote() { note.innerText = localStorage.note || ''; }
- </script>
- </head><body>
- <h1>Notepad (type below, notes persist)</h1> <p id="note" contenteditable=""></p>
- </body></html>
五:使用第三方API打造了一个在线编辑器,其中将“markdown”换成“python"即可切换高亮语言
- data:text/html,
- <style type="text/css">
- #e {
- position:absolute;
- top:0;
- right:0;
- bottom:0;
- left:0;
- font-size:16px;
- }
- </style>
- <div id="e"></div>
- <script src="http://d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js"></script>
- <script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
- <script>
- var myKey="SecretKeyz";
- $(document).ready(function(){
- var e;
- var url = "http://api.openkeyval.org/"+myKey;
- $.ajax({
- url: url,
- dataType: "jsonp",
- success: function(data){
- e = ace.edit("e");
- e.setTheme("ace/theme/tomorrow_night_eighties");
- e.getSession().setMode("ace/mode/markdown");
- e.setValue(data);
- }
- });
- $("#e").on("keydown", function (b) {
- if (b.ctrlKey && 83 == b.which) {
- b.preventDefault();
- var data = myKey+"="+encodeURIComponent(e.getValue());
- $.ajax({
- data: data,
- url: "http://api.openkeyval.org/store/",
- dataType: "jsonp",
- success: function(data){
- alert("Saved.");
- }
- });
- }
- });
- });
- </script>
六:没有代码高亮功能终归不是一个好的编辑器,有人又定制了Rubh代码高亮功能,不过
- data:text/html,
- <style type="text/css">
- #e{position:absolute;top:0;right:0;bottom:0;left:0;}
- </style>
- <div id="e"></div>
- <script src="http://d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
- <script>var e=ace.edit("e");e.setTheme("ace/theme/monokai");e.getSession().setMode("ace/mode/ruby");</script>
七:你以为到此为止了?有人又提供了一个涂鸦版本
- data:text/html,
- <body><canvas id="dyDraw">你的浏览器不支持HTML5 Canvas</canvas>
- </body>
- <script>
- function $(id){return document.getElementById(id);}
- $('dyDraw').width=document.body.clientWidth;
- $('dyDraw').height=document.body.clientHeight;
- if(window.addEventListener){
- window.addEventListener('load',function(){
- var canvas,canvastext;var hua=false;
- function dyDrawing(){canvas=$('dyDraw');
- canvascanvastext=canvas.getContext('2d');
- canvas.addEventListener('mousedown',canvasMouse,false);
- canvas.addEventListener('mousemove',canvasMouse,false);
- window.addEventListener('mouseup',canvasMouse,false);}
- function canvasMouse(dy){
- var x,y;if(dy.layerX||dy.layerX==0){x=dy.layerX;y=dy.layerY;}else if(dy.offsetX||dy.offsetX==0){x=dy.offsetX;y=dy.offsetY;}
- x-=dyDraw.offsetLeft;y-=dyDraw.offsetTop;
- if(dy.type=='mousedown'){hua=false;canvastext.beginPath();canvastext.moveTo(x,y);hua=true;}else if(dy.type=='mousemove'){if(hua){canvastext.strokeStyle="rgb(255,0,0)";
- canvastext.lineWidth=9;canvastext.lineTo(x,y);canvastext.stroke();}}else if(dy.type=='mouseup'){hua=false;}} dyDrawing();},false);}
- </script>
版权声明:本文为原创文章,版权归 neo 所有。
本文链接:https://idayer.com/html5-build-text-editor/
本作品采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可。
0 条评论