javascript - Google Docs是如何实现富文本编辑的?

标签 javascript dom

<分区>

我刚刚读完了 Nicholas Zakas 撰写的 Professional JavaScript for Web Developers 中有关在浏览器中进行富文本编辑的部分。它说有两种方法可以实现富文本编辑:

  1. designMode 属性设置为 on 的 iframe>
  2. 或具有contenteditable属性的元素

我从这些信息中得出的结论是,这两种技术都存在很多跨浏览器的不一致性,以至于没有一种是真正可靠的解决方案。

检查 Google Doc 的标记,我看到的只是一堆嵌套的 div

那么,我很好奇,Google 确定了哪种技术最适合实现富文本编辑?

最佳答案

Google 文档既不使用 designMode 也不使用 contentEditable。它有自己的用 Javascript 编写的渲染引擎。来自他们的 blog post on it :

To get around these problems, the new Google document editor doesn’t use the browser to handle editable text. We wrote a brand new editing surface and layout engine, entirely in JavaScript.

By far the most difficult thing the editor does is figure out where to draw text. For this, we built a new layout engine. Here’s an example of how the new engine works: say you type the letter ‘a’. We notice you pressed the ‘a’ key and respond by drawing a single ‘a’ off-screen. We then measure the width and height of that ‘a’, combine those measurements with the x and y position of your cursor, and place the ‘a’ at the correct spot on the screen. If you’re in the middle of a word, we push the characters after your cursor over. If you’re at the end of a line, the editor moves your word to the next line and pushes any overflow to the lines after it.

关于javascript - Google Docs是如何实现富文本编辑的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19619155/

相关文章:

javascript - 在 laravel 5 中显示多个评级星 jquery bar 评级

javascript - 如何在不触发 JSHint 的困惑语义错误的情况下在 JavaScript 的循环中添加 eventListeners?

javascript - 使用不同的 ng-controller 和 ng-include

javascript - 使用 THREEJS,我试图在场景中切换雾

javascript - 我的 p5.js Canvas 窗口随着每次代码执行而不断变化。我怎样才能阻止这个?

javascript - 如何使用javascript动态附加卡?

javascript - insertRow 与 appendChild

javascript - 在 jQuery 中在窗口滚动上运行一次函数

javascript - 调用两个同名函数

php - 加载带有 HTML 特殊字符的 DOMDocument (php)