资源说明:EpicEditor is an embeddable JavaScript Markdown editor with split fullscreen editing, live previewing, automatic draft saving, offline support, and more. For developers, it offers a robust API, can be easily themed, and allows you to swap out the bundled Markdown parser with anything you throw at it.
# ⚠️ DEPRECATION WARNING
This repository is no longer actively maintained.
# ![EpicEditor](http://epiceditor.com/docs/images/epiceditor-logo.png)
## An Embeddable JavaScript Markdown Editor
EpicEditor is an embeddable JavaScript [Markdown](http://daringfireball.net/projects/markdown/) editor with split fullscreen editing, live previewing, automatic draft saving, offline support, and more. For developers, it offers a robust API, can be easily themed, and allows you to swap out the bundled Markdown parser with anything you throw at it.
## Why
Because, WYSIWYGs suck. Markdown is quickly becoming the replacement. [GitHub](http://github.com), [Stackoverflow](http://stackoverflow.com), and even blogging apps like [Posterous](http://posterous.com) are now supporting Markdown. EpicEditor allows you to create a Markdown editor with a single line of JavaScript:
```javascript
var editor = new EpicEditor().load();
```
## Quick Start
EpicEditor is easy to implement. Add the script and assets to your page, provide a target container and call `load()`.
### Step 1: Download
[Download the latest release](http://epiceditor.com) or clone the repo:
```bash
$ git clone git@github.com:OscarGodson/EpicEditor
```
### Step 2: Install
Copy `EpicEditor/epiceditor/` onto your webserver, for example to `/static/lib/epiceditor`.
```bash
$ scp -r EpicEditor/epiceditor you@webserver:public_html/static/lib/
```
You can of course customize this step for your directory layout.
### Step 3: Create your container element
```html
```
Alternately, wrap an existing textarea to load the contents into the EpicEditor instance.
```html
```
### Step 4: Add the `epiceditor.js` file
```html
```
### Step 5: Init EpicEditor
EpicEditor needs to know where to find its themes, so it needs to be told its install directory at init.
```javascript
var editor = new EpicEditor({basePath: '/static/lib/epiceditor'}).load();
```
## API
### EpicEditor([_options_])
The `EpicEditor` constructor creates a new editor instance. Customize the instance by passing the `options` parameter. The example below uses all options and their defaults:
```javascript
var opts = {
container: 'epiceditor',
textarea: null,
basePath: 'epiceditor',
clientSideStorage: true,
localStorageName: 'epiceditor',
useNativeFullscreen: true,
parser: marked,
file: {
name: 'epiceditor',
defaultContent: '',
autoSave: 100
},
theme: {
base: '/themes/base/epiceditor.css',
preview: '/themes/preview/preview-dark.css',
editor: '/themes/editor/epic-dark.css'
},
button: {
preview: true,
fullscreen: true,
bar: "auto"
},
focusOnLoad: false,
shortcut: {
modifier: 18,
fullscreen: 70,
preview: 80
},
string: {
togglePreview: 'Toggle Preview Mode',
toggleEdit: 'Toggle Edit Mode',
toggleFullscreen: 'Enter Fullscreen'
},
autogrow: false
}
var editor = new EpicEditor(opts);
```
### Options
Option |
Description |
Default |
container |
The ID (string) or element (object) of the target container in which you want the editor to appear. |
epiceditor |
textarea |
The ID (string) or element (object) of a textarea you would like to sync the editor's content with. On page load if there is content in the textarea, the editor will use that as its content. |
|
basePath |
The base path of the directory containing the /themes . |
epiceditor |
clientSideStorage |
Setting this to false will disable localStorage. |
true |
localStorageName |
The name to use for the localStorage object. |
epiceditor |
useNativeFullscreen |
Set to false to always use faux fullscreen (the same as what is used for unsupported browsers). |
true |
parser |
[Marked](https://github.com/chjj/marked) is the only parser built into EpicEditor, but you can customize or toggle this by passing a parsing function to this option. For example:
parser: MyCustomParser.parse |
marked |
focusOnLoad |
If true , editor will focus on load. |
false |
file.name |
If no file exists with this name a new one will be made, otherwise the existing will be opened. |
container ID |
file.defaultContent |
The content to show if no content exists for a file. NOTE: if the textarea option is used, the textarea's value will take precedence over defaultContent . |
|
file.autoSave |
How often to auto save the file in milliseconds. Set to false to turn it off. |
100 |
theme.base |
The base styles such as the utility bar with the buttons. |
themes/base/epiceditor.css |
theme.editor |
The theme for the editor which is the area you type into. |
themes/editor/epic-dark.css |
theme.preview |
The theme for the previewer. |
themes/preview/github.css |
button |
If set to false will remove all buttons. |
All buttons set to true . |
button.preview |
If set to false will remove the preview button. |
true |
button.fullscreen |
If set to false will remove the fullscreen button. |
true |
button.bar |
If true or "show" , any defined buttons will always be visible. If false or "hide" , any defined buttons will never be visible. If "auto" , buttons will usually be hidden, but shown if whenever the mouse is moved. |
"auto" |
shortcut.modifier |
The key to hold while holding the other shortcut keys to trigger a key combo. |
18 (alt key) |
shortcut.fullscreen |
The shortcut to open fullscreen. |
70 (f key) |
shortcut.preview |
The shortcut to toggle the previewer. |
80 (p key) |
string.togglePreview |
The tooltip text that appears when hovering the preview icon. |
Toggle Preview Mode |
string.toggleEdit |
The tooltip text that appears when hovering the edit icon. |
Toggle Edit Mode |
string.toggleFullscreen |
The tooltip text that appears when hovering the fullscreen icon. |
Enter Fullscreen |
autogrow |
Whether to autogrow EpicEditor to fit its contents. If autogrow is desired one can either specify true , meaning to use default autogrow settings, or an object to define custom settings |
false |
autogrow.minHeight |
The minimum height (in pixels) that the editor should ever shrink to. This may also take a function that returns the desired minHeight if this is not a constant, or a falsey value if no minimum is desired |
80 |
autogrow.maxHeight |
The maximum height (in pixels) that the editor should ever grow to. This may also take a function that returns the desired maxHeight if this is not a constant, or a falsey value if no maximum is desired |
false |
autogrow.scroll |
Whether the page should scroll to keep the caret in the same vertical place while autogrowing (recommended for mobile in particular) |
true |
### load([_callback_])
Loads the editor by inserting it into the DOM by creating an `iframe`. Will trigger the `load` event, or you can provide a callback.
```javascript
editor.load(function () {
console.log("Editor loaded.")
});
```
### unload([_callback_])
Unloads the editor by removing the `iframe`. Keeps any options and file contents so you can easily call `.load()` again. Will trigger the `unload` event, or you can provide a callback.
```javascript
editor.unload(function () {
console.log("Editor unloaded.")
});
```
### getElement(_element_)
Grabs an editor element for easy DOM manipulation. See the Themes section below for more on the layout of EpicEditor elements.
* `container`: The element given at setup in the options.
* `wrapper`: The wrapping `
` containing the 2 editor and previewer iframes.
* `wrapperIframe`: The iframe containing the `wrapper` element.
* `editor`: The #document of the editor iframe (i.e. you could do `editor.getElement('editor').body`).
* `editorIframe`: The iframe containing the `editor` element.
* `previewer`: The #document of the previewer iframe (i.e. you could do `editor.getElement('previewer').body`).
* `previewerIframe`: The iframe containing the `previewer` element.
```javascript
someBtn.onclick = function () {
console.log(editor.getElement('editor').body.innerHTML); // Returns the editor's content
}
```
### is(_state_)
Returns a boolean for the requested state. Useful when you need to know if the editor is loaded yet for example. Below is a list of supported states:
* `loaded`
* `unloaded`
* `edit`
* `preview`
* `fullscreen`
```javascript
fullscreenBtn.onclick = function () {
if (!editor.is('loaded')) { return; }
editor.enterFullscreen();
}
```
### open(_filename_)
Opens a client side storage file into the editor.
**Note:** This does _not_ open files on your server or machine (yet). This simply looks in localStorage where EpicEditor stores drafts.
```javascript
openFileBtn.onclick = function () {
editor.open('some-file'); // Opens a file when the user clicks this button
}
```
### importFile([_filename_],[_content_])
Imports a string of content into a client side storage file. If the file already exists, it will be overwritten. Useful if you want to inject a bunch of content via AJAX. Will also run `.open()` after import automatically.
**Note:** This does _not_ import files on your server or machine (yet). This simply looks in localStorage where EpicEditor stores drafts.
```javascript
importFileBtn.onclick = function () {
editor.importFile('some-file',"#Imported markdown\nFancy, huh?"); //Imports a file when the user clicks this button
}
```
### exportFile([_filename_],[_type_])
Returns the plain text of the client side storage file, or if given a `type`, will return the content in the specified type. If you leave both parameters `null` it will return the current document's content in plain text. The supported export file types are:
**Note:** This does _not_ export files to your server or machine (yet). This simply looks in localStorage where EpicEditor stores drafts.
* text (default)
* html
* json (includes metadata)
* raw (warning: this is browser specific!)
```javascript
syncWithServerBtn.onclick = function () {
var theContent = editor.exportFile();
saveToServerAjaxCall('/save', {data:theContent}, function () {
console.log('Data was saved to the database.');
});
}
```
### rename(_oldName_, _newName_)
Renames a client side storage file.
**Note:** This does _not_ rename files on your server or machine (yet). This simply looks in localStorage where EpicEditor stores drafts.
```javascript
renameFileBtn.onclick = function () {
var newName = prompt('What do you want to rename this file to?');
editor.rename('old-filename.md', newName); //Prompts a user and renames a file on button click
}
```
### save()
Manually saves a file to client side storage (localStorage by default). EpicEditor will save continuously every 100ms by default, but if you set `autoSave` in the options to `false` or to longer intervals it's useful to manually save.
**Note:** This does _not_ save files to your server or machine (yet). This simply looks in localStorage where EpicEditor stores drafts.
```javascript
saveFileBtn.onclick = function () {
editor.save();
}
```
### remove(_name_)
Deletes a client side storage file.
**Note:** This does _not_ remove files from your server or machine (yet). This simply looks in localStorage where EpicEditor stores drafts.
```javascript
removeFileBtn.onclick = function () {
editor.remove('example.md');
}
```
### getFiles([_name_], [_excludeContent_])
If no `name` is given it returns an object containing the names and metadata of all client side storage file objects. If a `name` is specified it will return just the metadata of that single file object. If `excludeContent` is true, it will remove the content from the returned object. This is useful when you just want a list of files or get some meta data. If `excludeContent` is false (default), it'll return a `content` property per file in plain text format.
**Note:** This does _not_ get files from your server or machine (yet). This simply looks in localStorage where EpicEditor stores drafts.
```javascript
var files = editor.getFiles();
for (x in files) {
console.log('File: ' + x); //Returns the name of each file
};
```
### on(_event_, _handler_)
Sets up an event handler (callback) for a specified event. For all event types, see the Events section below.
```javascript
editor.on('unload', function () {
console.log('Editor was removed');
});
```
### emit(_event_)
Fires an event programatically. Similar to jQuery's `.trigger()`
```javascript
editor.emit('unload'); // Triggers the handler provided in the "on" method above
```
### removeListener(_event_, [_handler_])
Allows you to remove all listeners for an event, or just the specified one.
```javascript
editor.removeListener('unload'); //The handler above would no longer fire
```
### preview()
Puts the editor into preview mode.
```javascript
previewBtn.onclick = function () {
editor.preview();
}
```
### edit()
Puts the editor into edit mode.
```javascript
editBtn.onclick = function () {
editor.edit();
}
```
### focus()
Puts focus on the editor or previewer (whichever is visible). Works just like
doing plain old JavaScript and input focus like `someInput.focus()`. The
benefit of using this method however, is that it handles cross browser issues
and also will focus on the visible view (edit or preview).
```
showEditorBtn.onclick = function () {
editorWrapper.style.display = 'block'; // switch from being hidden from the user
editor.focus(); // Focus and allow user to start editing right away
}
```
### enterFullscreen([callback])
Puts the editor into fullscreen mode. A callback will be fired after the entering fullscreen animation completes. Some browsers
will be nearly instant while others, mainly Chrome, take 750ms before this event is fired. If already in fullscreen, the
callback will fire immediately.
**Note:** due to browser security restrictions, calling `enterFullscreen` programmatically
like this will not trigger native fullscreen. Native fullscreen can only be triggered by a user interaction like mousedown or keyup.
```javascript
enterFullscreenBtn.onclick = function () {
editor.enterFullscreen(function () {
console.log('Welcome to fullscreen mode!');
});
}
```
### exitFullscreen([callback])
Closes fullscreen mode. A callback will be fired after the exiting fullscreen animation completes. If already not in fullscreen, the
callback will fire immediately.
```javascript
exitFullscreenBtn.onclick = function () {
editor.exitFullscreen(function () {
console.log('Finished closing fullscreen!');
});
}
```
### reflow([type], [callback])
`reflow()` allows you to "reflow" the editor in it's container. For example, let's say you increased
the height of your wrapping element and want the editor to resize too. You could call `reflow`
and the editor will resize to fit. You can pass it one of two strings as the first parameter to
constrain the reflow to either `width` or `height`.
It also provides you with a callback parameter if you'd like to do something after the resize is finished.
The callback will return the new width and/or height in an object. Additionally, you can also listen for
the `reflow` event. This will also give you back the new size.
**Note:** If you call `reflow()` or `reflow('width')` and you have a fluid width container
EpicEditor will no longer be fluid because doing a reflow on the width sets an inline style on the editor.
```javascript
// For an editor that takes up the whole browser window:
window.onresize = function () {
editor.reflow();
}
// Constrain the reflow to just height:
someDiv.resizeHeightHandle = function () {
editor.reflow('height');
}
// Same as the first example, but this has a callback
window.onresize = function () {
editor.reflow(function (data) {
console.log('width: ', data.width, ' ', 'height: ', data.height);
});
}
```
## Events
You can hook into specific events in EpicEditor with
on()
such as when a file is
created, removed, or updated. Below is a complete list of currently supported events and their description.
Event Name |
Description |
create |
Fires whenever a new file is created. |
read |
Fires whenever a file is read. |
update |
Fires whenever a file is updated. |
remove |
Fires whenever a file is deleted. |
load |
Fires when the editor loads via load() . |
unload |
Fires whenever the editor is unloaded via unload() |
preview |
Fires whenever the previewer is opened (excluding fullscreen) via preview() or the preview button. |
edit |
Fires whenever the editor is opened (excluding fullscreen) via edit() or the edit button. |
fullscreenenter |
Fires whenever the editor opens in fullscreen via fullscreen() or the fullscreen button. |
fullscreenexit |
Fires whenever the editor closes in fullscreen via fullscreen() or the fullscreen button. |
save |
Fires whenever save() is called manually, or implicitly by ```importFile``` or ```open```. |
autosave |
Fires whenever the autoSave interval fires, and the file contents have been updated since the last save. |
open |
Fires whenever a file is opened or loads automatically by EpicEditor or when open() is called. |
reflow |
Fires whenever reflow() is called. Will return the new dimensions in the callback. Will also fire every time there is a resize from autogrow. |
## Themes
Theming is easy in EpicEditor. There are three different `
本源码包内暂不包含可直接显示的源代码文件,请下载源码包。