Squire is an extremely lightweight HTML5 rich text editor, best suited for rich input forms and simple document creation for your applications. It provides cross-browser support but deliberately avoids the complexity of supporting older browsers. It works best on Opera 10, Firefox 3.5, Safari 4, Chrome 9 and IE8.
Squire is not suitable for creating and editing WYSIWYG website pages. However, for many rich text input and web applications, Squire may be just what you need. It provides power without bloat. It is also licensed from MIT for flexible reuse.
In this tutorial, I'll show you how to download Squire and use it to build a sample input form. If you want to see Squire in action, visit the demo.
The FastMail team built Squire to simplify their webmail editor requirements. FastMail is an excellent cloud-based email alternative to Gmail — I'm a daily FastMail user. Since it's based in Australia and operates under different laws than the United States, FastMail users have slightly improved privacy protections. You can read more about it here: FastMail says it's not subject to NSA surveillance.
As the FastMail team writes in their blog, they have used CKeditor before:
While not a bad choice, like most other editors it is designed for creating websites rather than writing emails. So by default, simply inserting an image brings up a dialog box with three tabs and more options than you might imagine... It also comes with its own UI toolkit and framework that we have to It took a lot of customization to fit the rest of the new UI we were building; it was a pain to maintain.
Since we care about speed and performance, we also care about code size. The version of CKEditor we used for the previous (classic) UI only included the plugins we needed and was a 159 KB download (gzipped; 441 KB uncompressed). This is code only, styles and images are not included.
They decided to build Squire from scratch. With a compressed and gzipped JavaScript size of just 11.5 KB (34.7 KB uncompressed) and no dependencies, Squire is extremely lightweight.
The results are impressive. The combined code weight required to load the entire compose screen, base libraries, mail and contact model code, and all UI code to render the entire screen is now just 149.4 KB (459.7 KB uncompressed), which is smaller than CKEditor alone.
Squire has no dependencies. There are no XHR wrappers, widget libraries or lightbox overlays. The toolbar has no user interface, which eliminates the bloat that comes with loading two UI toolkits. This is just a simple <textarea>
component that can be manipulated via JavaScript.
Squire uses the selection and range APIs to manipulate the DOM. This eliminates common cross-browser incompatibilities. Again from the FastMail Blog:
Making a rich text editor is notoriously difficult because different browsers are extremely inconsistent in this regard. These APIs were all introduced by Microsoft during the heyday of IE and then copied by other vendors in various incompatible ways... Most rich text editors execute the command and then try to clean up the mess the browser created. With Squire, we neatly bypass this problem.
The general idea of Squire is to allow the browser to do as much as possible (not a lot unfortunately), but control anything that deviates from what is required, or where there are significant cross-browser differences.
Install SquireSecond, copy the contents of the
build/
directory into your application. p>Third, edit the
<style>
block indocument.html
to add the default style you want the editor to use (or link to an external style sheet).Use Squire
Let’s take a look at the demo application that comes with Squire. When using Squire, you can use
<iframe src="path/to/document.html">
instead of the<textarea>
element.In demo:
<iframe src="build/document.html" onload="top.editor=this.contentWindow.editor" width="500" height="500"></iframe>Copy after login
Document.html
is a blank canvas with default styles that loads Squire:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title></title> <style type="text/css"> ... a { text-decoration: underline; } h1 { font-size: 138.5%; } ... blockquote { border-left: 2px solid blue; margin: 0; padding: 0 10px; } </style> </head> <body> <script type="text/javascript" src="squire.js"></script> </body> </html>Copy after loginWhen using Squire, you can attach an event listener to the iframe's load event. When this event fires, you can get a reference to the editor object via iframe.contentWindow.editor. For example, a demo iframe includes:
onload="top.editor=this.contentWindow.editor"
该演示有两种类型的链接,用于在 Squire 中操作内容。可以对任何选定文本进行操作的简单命令如下所示:
<span id="bold">Bold</span>
需要额外用户输入的复杂命令如下所示;他们添加了提示符 c:
<span id="setFontSize" class="prompt">字体大小</span>
演示应用程序如下所示:
演示页面顶部的 JavaScript 侦听对这些 span 命令的任何点击。如果提示类存在,它会从用户收集更多信息:
<script type="text/javascript" charset="utf-8"> var editor; document.addEventListener( 'click', function ( e ) { var id = e.target.id, value; if ( id && editor && editor[ id ] ) { if ( e.target.className === 'prompt' ) { value = prompt( 'Value:' ); } editor[ id ]( value ); } }, false ); </script>Copy after login然后,它使用命令和任何用户提供的值调用 Squire 编辑器。然后,Squire 将命令应用于当前选定的文本:
编辑器[ id ]( value );
您可以了解有关 Squire 的更多信息,并在自述文件中查看其完整的 API 文档。
扩展演示
作为示例,让我们添加两个读取 Squire 编辑器状态的命令。我们将在演示命令标题的底部添加两个链接:
<p> <a href="#" onclick="alert(editor['getSelectedText']());">get selection</a> | <a href="#" onclick="alert(editor['getDocument']());">get doc</a> </p> </header>Copy after login当您选择某些文本并单击它时,所选内容将在警报中弹出,如下所示。
让我们来看看更美观的演示及其工具栏:
此页面的头块集成了 Bootstrap 的样式表和名为
Squire-UI
的样式表。它还为此Squire-UI
提供 JavaScript。<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta charset="UTF-8"> <title>Squire</title> <!--[if IE 8]> <script type="text/javascript" src="build/ie8.js"></script> <![endif]--> <link href='//fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'> <link href="build/Squire-UI.css" rel="stylesheet" type="text/css" /> <link href="build/bootstrap/bootstrap.min.css" rel="stylesheet" type="text/css" /> <script src="build/jQuery/jQuery.js" type="text/javascript"></script> <script src="build/squire-raw.js" type="text/javascript"></script> <script src="build/Squire-UI.js" type="text/javascript"></script> It also offers static html for a textarea in the body <div class="container"> <div class="row"> <div class="col-centered"> <textarea id="foo"></textarea> </div> </div>Copy after login但在加载时,其 JQuery
$(document).ready
函数将静态#foo
文本区域替换为其SquireUI
。<script> $(document).ready(function () { UI = new SquireUI({replace: 'textarea#foo', height: 300}); }); </script>Copy after login工具栏配置是通过相当复杂的 JQuery、AJAX、HTML5 和 CSS 配置来实现的。它正在加载此 HTML 页面以显示大部分工具栏:http://neilj.github.io/Squire/build/Squire-UI.html。
$(div).load(options.buildPath + 'Squire-UI.html', function() { this.linkDrop = new Drop({ target: $('#makeLink').first()[0], content: $('#drop-link').html(), position: 'bottom center', openOn: 'click' });Copy after login以下是 Squire-UI.html 源代码的子集,以便您可以查看正在加载的内容:
<div class="menu" contenteditable="false"> <div class="group"> <div data-action="bold" class="item"><i class="fa fa-bold"></i></div> <div data-action="italic" class="item"><i class="fa fa-italic"></i></div> <div data-action="underline" class="item"><i class="fa fa-underline"></i></div> <div id="selectFont" data-action="selectFont" class="item"> <i class="fa fa-font"></i> </div> </div> <div class="group"> <div id="makeLink" data-action="makeLink" class="item"><i class="fa fa-link"></i></div> <div data-action="makeOrderedList" class="item"><i class="fa fa-list"></i></div> <div id="insertImage" data-action="insertImage" class="item"> <i class="fa fa-picture-o"></i> </div> <div data-action="increaseQuoteLevel" class="item"><i class="fa fa-quote-right"></i></div> </div> ...Copy after login如果他们在分发代码中提供简化的 Bootstrap 工具栏作为附加组件,那就太好了,但您当然可以从他们在上面自己的演示中所做的事情中学习。
我希望您发现 Squire 对您自己的应用程序很有用。请随时在下面发表更正、问题或评论。您还可以通过 Twitter @reifman 联系我或直接向我发送电子邮件。
相关链接
- Squire:FastMail 的富文本编辑器
- Squire演示页面
- Github 上的 Squire 代码库
The above is the detailed content of Revisiting Squire: an efficient HTML5 rich text editor. For more information, please follow other related articles on the PHP Chinese website!