如何将文本区域设为 ACE 编辑器?
我希望能够将页面上的特定文本区域转换为 ACE 编辑器。
请问有人有指点吗?
编辑:
我有 editor.html 文件使用一个文本区域,但是一旦我添加第二个,第二个就不会转换为编辑器。
编辑2:
我决定放弃拥有多个的想法,而是在新窗口中打开一个。我的新困境是,当我 hide() 和 show() 文本区域时,显示会出错。有什么想法吗?
I'd like to be able to convert specific textareas on a page to be ACE editors.
Does anyone have any pointers please?
EDIT:
I have the the editor.html file working with one textarea, but as soon as I add a second, the second isn't converted to an editor.
EDIT 2:
I decided to scrap the idea of having several, and instead open one up in a new window. My new predicament is that when I hide() and show() the textarea, the display goes awry. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
据我了解 Ace 的想法,您不应该将 textarea 本身设置为 Ace 编辑器。您应该创建一个额外的 div 并使用 .getSession() 函数更新文本区域。
html
js
或仅
在您使用给定文本区域提交表单时调用。我不确定这是否是使用 Ace 的正确方法,但这是它在 GitHub 上的使用方式。
As far as I understood the idea of Ace, you shouldn't make a textarea an Ace editor itself. You should create an additional div and update textarea using .getSession() function instead.
html
js
or just call
only when you submit the form with the given textarea. I'm not sure whether this is the right way to use Ace, but it's the way it is used on GitHub.
Duncansmart 在他的 github 页面上有一个非常棒的解决方案,progressive-ace它演示了一种将 ACE 编辑器连接到页面的简单方法。
基本上,我们获取所有具有
data-editor
属性的Duncansmart has a pretty awesome solution on his github page, progressive-ace which demonstrates one simple way to hook up an ACE editor to your page.
Basically we get all
<textarea>
elements with thedata-editor
attribute and convert each to an ACE editor. The example also sets some properties which you should customize to your liking, and demonstrates how you can usedata
attributes to set properties per element like showing and hiding the gutter withdata-gutter
.您可以拥有多个 Ace 编辑器。只需给每个文本区域一个 ID 并为两个 IDS 创建一个 Ace 编辑器,如下所示:
You can have multiple Ace Editors. Just give each textarea an ID and create an Ace Editor for both IDS like so:
要创建编辑器,只需执行以下操作:
HTML:
JS:
CSS:
它们必须明确定位和调整大小。通过 show() 和 hide() 我相信您指的是 jQuery 函数。我不确定他们到底是如何做到的,但它不能修改它在 DOM 中占用的空间。我隐藏和显示使用:
如果您使用 css 属性“display”,它将不起作用。
请查看此处的 wiki,了解如何添加主题、模式等... https: //github.com/ajaxorg/ace/wiki/Embedding---API
注意:它们不必是文本区域,它们可以是您想要的任何元素。
To create an editor just do:
HTML:
JS:
CSS:
They must be explicitly positioned and sized. By show() and hide() I believe you are referring to the jQuery functions. I'm not sure exactly how they do it, but it cannot modify the space it takes up in the DOM. I hide and show using:
If you use the css property 'display' it will not work.
Check out the wiki here for how to add themes, modes, etc... https://github.com/ajaxorg/ace/wiki/Embedding---API
Note: they do not have to be textareas, they can be whatever element you want.
对于那些只想要一个使用 CDN 中的 Ace 的最小可行示例的人:
For anyone that just wants a minimal, working example of using Ace from the CDN: