使用画布元素作为文本区域

发布于 2024-11-15 09:29:16 字数 159 浏览 2 评论 0原文

我正在寻找有关如何使用类似于文本区域的画布元素的直接描述。

我见过诸如 Ace 之类的项目。只是想知道如何像文本区域一样写入该区域。只是纯文本,没有什么花哨的。

提前致谢。

I'm looking for a straight forward description of how to use a canvas element sort of like a text area.

I have seen projects such as Ace. Just wondering how to go about writing to the area as if it where a textarea. Just plain text, nothing fancy.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

上课铃就是安魂曲 2024-11-22 09:29:16

Ace 曾经是 Mozilla Skywriter,又曾经是 Mozilla Bespin。

如果您愿意深入研究 Bespin 的代码并根据它创建自己的代码,那么 Bespin 的代码实际上很容易理解,但这有点像傻瓜的任务。 Canvas 规范实际上特别建议不要这样做:

作者应避免使用文本
使用画布编辑控件
元素。这样做有很大的数量
缺点:

鼠标放置插入符号必须是
重新实现。

插入符号的键盘移动必须
被重新实现(可能跨越
lines,用于多行文本输入)。

文本字段的滚动必须是
实施(水平长期
行,垂直为多行
输入)。

复制和粘贴等本机功能
必须重新实现。

拼写检查等本机功能
必须重新实现。

拖放等本机功能
必须重新实现。

本机功能,例如页面宽度的文本
必须重新实现搜索。

特定于用户的本机功能,
例如自定义文本服务,有
予以重新实施。这接近于
不可能,因为每个用户可能都有
安装了不同的服务,以及
存在一组无限的可能
此类服务。

双向文本编辑必须是
重新实现。

对于多行文本编辑,line
必须对所有人实施包装
相关语言。

文本选择必须是
重新实现。

双向文本拖动
必须重新实施选择。

平台本机键盘快捷键
必须重新实现。

平台原生输入法编辑器
(IME)必须重新实现。

撤消和重做功能必须
重新实现。

辅助功能,例如
插入符号后的放大倍数或
必须重新实施选择。
这是一项巨大的工作量,并且
作者受到最强烈的鼓励
以避免做任何事情
使用输入元素,文本区域
元素,或内容可编辑
属性。

Ace used to be Mozilla Skywriter, which used to be Mozilla Bespin.

The code for Bespin is actually pretty simple to understand if you are willing to dig through it and make your own based on it, but it is sort of a fool's errand. The Canvas spec actually advises specifically against this:

Authors should avoid implementing text
editing controls using the canvas
element. Doing so has a large number
of disadvantages:

Mouse placement of the caret has to be
reimplemented.

Keyboard movement of the caret has to
be reimplemented (possibly across
lines, for multiline text input).

Scrolling of the text field has to be
implemented (horizontally for long
lines, vertically for multiline
input).

Native features such as copy-and-paste
have to be reimplemented.

Native features such as spell-checking
have to be reimplemented.

Native features such as drag-and-drop
have to be reimplemented.

Native features such as page-wide text
search have to be reimplemented.

Native features specific to the user,
for example custom text services, have
to be reimplemented. This is close to
impossible since each user might have
different services installed, and
there is an unbounded set of possible
such services.

Bidirectional text editing has to be
reimplemented.

For multiline text editing, line
wrapping has to be implemented for all
relevant languages.

Text selection has to be
reimplemented.

Dragging of bidirectional text
selections has to be reimplemented.

Platform-native keyboard shortcuts
have to be reimplemented.

Platform-native input method editors
(IMEs) have to be reimplemented.

Undo and redo functionality has to be
reimplemented.

Accessibility features such as
magnification following the caret or
selection have to be reimplemented.
This is a huge amount of work, and
authors are most strongly encouraged
to avoid doing any of it by instead
using the input element, the textarea
element, or the contenteditable
attribute.

青巷忧颜 2024-11-22 09:29:16

使用 chrome 检查现场演示表明他们使用 div 和 span 来实现这一点。闪烁的光标是一个似乎定期在隐藏和可见之间切换的 div。我认为他们只是检查事件中按下的键并将其写入该行的相应范围。

Inspecting the live demo with chrome shows they use divs and spans to achieve this. The blinking cursor is a div that seems to switch between hidden and visible on a regular basis. I would think that they just check the pressed key from the event and write it to the corresponding span for the line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文