鉴于 ace 官网国内访问较慢,搬运了一份guide到博客,仅供学习,请勿用于商业用途,侵权请联系我删除
1 | <!DOCTYPE html> |
In all of these examples Ace has been invoked as shown in the embedding guide.
Configuring the editor
there are several ways to pass configuration to Ace
1 | // pass options to ace.edit |
See Configuring-Ace wiki page for a more detailed list of options.
Changing the size of the editor
Ace only checks for changes of the size of it’s container when window is resized. If you resize the editor div in another manner, and need Ace to resize, use the following:
1 | editor.resize() |
if you want editor to change it’s size based on contents, use maxLines option as shown in https://ace.c9.io/demo/autoresize.html
Setting Themes
Themes are loaded on demand; all you have to do is pass the string name:
1 | editor.setTheme("ace/theme/twilight"); |
Setting the Programming Language Mode
By default, the editor supports plain text mode. All other language modes are available as separate modules, loaded on demand like this:
1 | editor.session.setMode("ace/mode/javascript"); |
One Editor, Multiple Sessions
Ace keeps everything about the state of the editor (selection, scroll position, etc.) in ``
1 | editor.session |
. This means you can grab the session, store it in a var, and set the editor to another session (e.g. a tabbed editor).
You might accomplish this like so:
1 | var EditSession = require("ace/edit_session").EditSession; |
Common Operations
Set and get content:
1 | editor.setValue("the new text here"); |
Get selected text:
1 | editor.getSelectedText(); // or for a specific range |
Insert at cursor, emulating user input:
1 | editor.insert("Something cool"); |
Replace text in range:
1 | editor.session.replace(new ace.Range(0, 0, 1, 1), "new text"); |
Get the current cursor line and column:
1 | editor.selection.getCursor(); |
Go to a line:
1 | editor.gotoLine(lineNumber); |
Get total number of lines:
1 | editor.session.getLength(); |
Set the default tab size:
1 | editor.session.setTabSize(4); |
Use soft tabs:
1 | editor.session.setUseSoftTabs(true); |
Set the font size:
1 | document.getElementById('editor').style.fontSize='12px'; |
Toggle word wrapping:
1 | editor.session.setUseWrapMode(true); |
Set line highlighting:
1 | editor.setHighlightActiveLine(false); |
Set the print margin visibility:
1 | editor.setShowPrintMargin(false); |
Set the editor to read-only:
1 | editor.setReadOnly(true); // false to make it editable |
Using undo manager
To group undo delta of the next edit with the previous one set mergeUndoDeltas
to true
1 | editor.session.mergeUndoDeltas = true; |
To start new undo group use markUndoGroup
method
1 | editor.insertSnippet("a$0b"); |
To disable undo of a an edit in a collaborative editor
1 | var rev = session.$undoManager.startNewGroup(); // start new undo group |
To implement undo/redo buttons see https://ace.c9.io/demo/toolbar.html
Searching
1 | editor.find('needle',{ |
The following options are available to you for your search parameters:
needle
: The string or regular expression you’re looking forbackwards
: Whether to search backwards from where cursor currently is. Defaults tofalse
.wrap
: Whether to wrap the search back to the beginning when it hits the end. Defaults tofalse
.caseSensitive
: Whether the search ought to be case-sensitive. Defaults tofalse
.wholeWord
: Whether the search matches only on whole words. Defaults tofalse
.range
: The Range to search within. Set this tonull
for the whole documentregExp
: Whether the search is a regular expression or not. Defaults tofalse
.start
: The starting Range or cursor position to begin the searchskipCurrent
: Whether or not to include the current line in the search. Default tofalse
.preventScroll
: Whether or not to move the cursor to the next match. Default tofalse
.
Here’s how you can perform a replace:
1 | editor.find('foo'); |
And here’s a replace all:
1 | editor.replaceAll('bar'); |
(editor.replaceAll
uses the needle set earlier by editor.find('needle', ...
)
Listening to Events
To listen for an onchange
:
1 | editor.session.on('change', function(delta) { |
To listen for an selection
change:
1 | editor.session.selection.on('changeSelection', function(e) { |
To listen for a cursor
change:
1 | editor.session.selection.on('changeCursor', function(e) { |
Adding New Commands and Keybindings
To assign key bindings to a custom function:
1 | editor.commands.addCommand({ |
Configure dynamic loading of modes and themes
By default ace detcts the url for dynamic loading by finding the script node for ace.js. This doesn’t work if ace.js is not loaded with a separate script tag, and in this case it is required to set url explicitely
1 | ace.config.set("basePath", "https://url.to.a/folder/that/contains-ace-modes"); |
Path for one module alone can be configured with:
1 | ace.config.setModuleUrl("ace/theme/textmate", "url for textmate.js"); |
When using ace with webpack, it is possible to configure paths for all submodules using
1 | require("ace-builds/webpack-resolver"); |
which depends on file-loader
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 jaytp@qq.com