21 KiB
Developer guide
Getting started
Pre-requisites
Before debugging
-
Download development tools:
npm install -
Download dependencies:
bower install -
Serve StackEdit at
http://localhost/:(export PORT=80 && node server.js)If on Windows, use
(set PORT=80 && node server.js) -
Run StackEdit in debug mode (no application cache, serve original files instead of minified):
http://localhost/?debug
Add new dependencies
NOTE: StackEdit uses RequireJS for asynchronous module definition (AMD).
-
Install new dependencies using Bower:
bower install <library> --save -
Add the new dependency to RequireJS configuration file (
main.js):gulp bower-requirejs
Build/minify
gulp
Deploy
-
on Heroku:
heroku create my-stackedit-instance git push heroku master -
in a Docker container:
docker build -t my-stackedit-image . docker run -p 3000 my-stackedit-image
NOTE: OAuth authorizations work out of the box for address
http://localhost/except for WordPress. To allow another address, you have to add specific keys at the end ofconstants.jsand eventually to set up specific proxies with the corresponding key/secret pairs (WordPress Proxy, Tumblr Proxy and Gatekeeper).
Architecture
The modules are loaded by RequireJS in the following order:
- The 3rd party libraries (jQuery, underscore.js...)
- The
Extensionmodules - The
eventMgrmodule - The
coremodule - The
fileMgrmodule and the helpers modules - The
Providermodules - The
publisherandsynchronizermodules
This is important to notice in order to avoid circular dependencies. For instance, if an Extension is declared with the core module as a dependency, RequireJS will inject undefined instead of the actual module.
Any module though can access any dependencies by implementing the proper injection listener provided by the eventMgr.
core
The core module is responsible for:
- creating the UI Layout, the ACE editor and the PageDown editor,
- loading/saving the settings,
- running periodic tasks,
- detecting the user activity,
- checking the offline status.
Attributes:
isOffline: indicates the offline status of the application.
Methods:
onReady(callback): sets a callback to be called when all modules have been loaded and the DOM is ready.
NOTE: This is preferred over jQuery's
.ready()because it ensures that all AMD modules are loaded by RequireJS.
runPeriodically(callback): sets a callback to be called every second.
NOTE: The callback will not run if the user is inactive or in StackEdit Viewer. User is considered inactive after 5 minutes of inactivity (mouse or keyboard).
setOffline(): can be called by any other modules when a network timeout occurs for instance.
NOTE: the offline status is also set by detecting the window
offlineevent.core.isOfflineis automatically set tofalsewhen the network is recovered.
initEditor(fileDesc): creates or refreshes the PageDown editor with a givenFileDescriptorobject.
fileMgr
The fileMgr module is responsible for:
- creating and deleting local files,
- switching from one file to another.
Attributes:
currentFile: theFileDescriptorobject that is currently edited.
Methods:
createFile(title, content): creates aFileDescriptorobject, add it in thefileSystemmap and returns it.deleteFile(fileDesc): deletes aFileDescriptorobject from thefileSystemmap.selectFile(fileDesc): selects aFileDescriptorobject for editing.
FileDescriptor
The FileDescriptor class represents a local file. A FileDescriptor object has the following properties:
fileIndex: the unique string index of the file in the file system.title: the title of the document.content: the content of the document.syncLocations: a map containing all the associatedsyncAttributesobjects with theirsyncIndexas a key.publishLocations: a map containing all the associatedpublishAttributesobjects with theirpublishIndexas a key.
And the following methods:
addSyncLocation(syncAttributes): associates asyncAttributesobject with the file.removeSyncLocation(syncAttributes): unassociates asyncAttributesobject with the file.addPublishLocation(publishAttributes): associates apublishAttributesobject with the file.removePublishLocation(publishAttributes): unassociates apublishAttributesobject with the file.
fileSystem
The fileSystem module is a map containing all the FileDescriptor objects with their fileIndex as a key.
synchronizer
The synchronizer module is responsible for:
- creating a new local file from a sync location (import).
- creating a new sync location from a local file (export).
- running 2 ways synchronization (upload and download) for all sync locations.
synchronizer's providers
A provider module can be associated with the synchronizer module if it implements the following functions:
importFiles(): downloads one or multiple files and create local files associated with the sync locations.exportFile(): uploads a local file to a new sync location.syncDown(): performs a download of all the changes operated on all sync locations.syncUp(): performs an upload of a change to a sync location.
syncAttributes
A syncAttributes object is an object that describes a sync location. Attributes differ from one provider to another except for the following:
syncIndex: the unique string index of the publish location.provider: theprovidermodule that handles the sync location.
publisher
The publisher module is responsible for:
- creating new publish locations,
- updating existing publish locations.
publisher's providers
A provider module can be associated with the publisher module if it implements the following functions:
newPublishAttributes(): returns a newpublishAttributesobject in order to create a new publish location.publish(): performs publishing of one publish location.
publishAttributes
A publishAttributes object is an object that describes a publish location. Attributes differ from one provider to another except for the following:
publishIndex: the unique string index of the publish location.provider: theprovidermodule that handles the publish location.format: the publishing format for the publish location. It can be:markdownfor Markdown format.htmlfor HTML format.templatefor template format.
eventMgr
The eventMgr module is responsible for receiving and dispatching events. Below is the list of all events signatures.
Most events (those that are not triggered by the eventMgr module) can be triggered by calling methods of the same name in the eventMgr module. For example:
eventMgr.onMessage('StackEdit is awesome!');
The method addListener(eventName, callback) of the eventMgr module can be used to listen to these events (except those that can only be handled by Extension modules). For example:
eventMgr.addListener('onMessage', function(message) {
alert(message);
});
Extension modules have the possibility to listen to those events by implementing methods of the same name. For example:
myExtension.onMessage = function(message) {
alert(message);
};
Core events
-
onReady()All the modules are loaded and the DOM is ready.
Triggered by the
coremodule.This is preferred over jQuery's
.ready()because it ensures that all modules have been loaded by RequireJS. -
onMessage(message)A message destined to the user has been produced.
message: the text string of the message.
-
onError(error)An error has been thrown.
error: an error object or a string.
-
onOfflineChanged(isOffline)The off-line status has changed.
isOffline: the off-line status.
Triggered by the
coremodule. -
onUserActive()The user has just moved the mouse or pressed the keyboard.
Triggered by the
coremodule. -
onAsyncRunning(isRunning)Some asynchronous tasks have just started or stopped.
isRunning: true if started, false if stopped.
Triggered by the
AsyncTaskmodule. -
onPeriodicRun()A hook that is called periodically (every 1 second if user is active).
Triggered by the
coremodule. -
onLoadSettings()A hook that is called when the settings dialog has to be refreshed. Every
Extensionmodule that has configuration inputs in the settings dialog has to implement a listener for this event.Triggered by the
coremodule. OnlyExtensionmodules can handle this event. -
onSaveSettings(newConfig, event)A hook that is called when the settings dialog has to be validated. Every
Extensionmodule that has configuration inputs in the settings dialog has to implement a listener for this event.newConfig: the new configuration object, deduced from the settings dialog inputs.event: the submit event object.stopPropagationhas to be called in case of an error when parsing settings dialog inputs.
Triggered by the
coremodule. OnlyExtensionmodules can handle this event. -
onInit()A hook allowing enabled extensions to initialize.
Triggered by the
eventMgrmodule. OnlyExtensionmodules can handle this event.This event is triggered before
onReadyevent and just after theconfigandenabledextensions properties have been set by theeventMgr.
Module injection events
-
onFileMgrCreated(fileMgr)The
fileMgrmodule has been created.fileMgr: thefileMgrmodule.
Triggered by the
fileMgrmodule. -
onSynchronizerCreated(synchronizer)The
synchronizermodule has been created.synchronizer: thesynchronizermodule.
Triggered by the
synchronizermodule. -
onPublisherCreated(publisher)The
publishermodule has been created.publisher: thepublishermodule.
Triggered by the
publishermodule. -
onEventMgrCreated()The
eventMgrmodule has been created.eventMgr: theeventMgrmodule.
Triggered by the
eventMgrmodule.
file operation events
-
onFileCreated(fileDesc)A
FileDescriptorobject has been created.fileDesc: theFileDescriptorobject.
Triggered by the
fileMgrmodule. -
onFileDeleted(fileDesc)A
FileDescriptorobject has been removed from thefileSystemmodule.fileDesc: theFileDescriptorobject.
Triggered by the
fileMgrmodule. -
onFileSelected(fileDesc)A
FileDescriptorobject has been selected.fileDesc: theFileDescriptorobject.
Triggered by the
fileMgrmodule. This event is triggered beforeonFileClosed(if another document is open) andonFileOpenevents. -
onFileClosed(fileDesc)The current
FileDescriptorobject is about to be detached from the editor.fileDesc: theFileDescriptorobject.
Triggered by the
fileMgrmodule. This event is triggered afteronFileSelectedevent and beforeonFileClosedevent. -
onFileOpen(fileDesc)The selected
FileDescriptorobject has been attached to the editor.fileDesc: theFileDescriptorobject.
Triggered by the
fileMgrmodule. This event is triggered afteronFileSelectedandonFileClosed(if another document is open) events. -
onContentChanged(fileDesc)The content of a
FileDescriptorobject has been modified.fileDesc: theFileDescriptorobject.
-
onTitleChanged(fileDesc)The content of a
FileDescriptorobject has been modified.fileDesc: theFileDescriptorobject.
-
onFoldersChanged()The folders structure has changed.
Sync events
-
onSyncRunning(isRunning)A synchronization job has just started or stopped.
isRunning: true if started, false if stopped.
Triggered by the
synchronizermodule.A synchronization job is the action to download and upload all detected changes for all sync locations of all documents.
-
onSyncSuccess()A synchronization job has successfully finished.
Triggered by the
synchronizermodule.A synchronization job is the action to download and upload all detected changes for all sync locations of all documents.
-
onSyncImportSuccess(fileDescList, provider)The import of documents has successfully finished.
fileDescList: the list ofFileDescriptorobjects that have been created.provider: theprovidermodule that handled the import.
Triggered by the
providermodule that handled the import.An import is the action to download multiple files and to create, for each, one
FileDescriptorobjects with one sync location. -
onSyncExportSuccess(fileDesc, syncAttributes)The export of one document has successfully finished.
fileDesc: theFileDescriptorobject that has been exported.syncAttributes: the descriptor object of the new sync location.
Triggered by the
synchronizermodule.An export is the action to upload one file and to create one new sync location associated with one existing
FileDescriptor]55 object. -
onSyncRemoved(fileDesc, syncAttributes)A sync location has been removed from a
FileDescriptorobject.fileDesc: theFileDescriptorobject.syncAttributes: the descriptor object of the removed sync location.
Publish events
-
onPublishRunning(isRunning)A document publication job has just started or stopped.
isRunning: true if started, false if stopped.
Triggered by the
publishermodule.A publication job is the action to upload changes on multiple publish locations associated with one
FileDescriptor]55 object. -
onPublishSuccess(fileDesc)A document publication job has successfully finished.
fileDesc: theFileDescriptorobject that has been published.
Triggered by the
publishermodule.A publication job is the action to upload changes on multiple publish locations associated with one
FileDescriptor]55 object. -
onNewPublishSuccess(fileDesc, publishAttributes)A new publish location has been successfully created.
fileDesc: theFileDescriptorobject that has been published.publishAttributes: the descriptor object of the new publish location.
Triggered by the
publishermodule. -
onPublishRemoved(fileDesc, publishAttributes)A publish location has been removed from a
FileDescriptorobject.fileDesc: theFileDescriptorobject.publishAttributes: the descriptor object of the removed publish location.
Triggered by the
publishermodule.
UI Layout events
-
onLayoutConfigure(layoutConfig)The layout is about to be configured.
layoutConfig: the configuration object of the UI Layout library.
Triggered by the
coremodule. -
onLayoutCreated(layout)The layout has just been created.
layout: the layout object of the UI Layout library.
Triggered by the
coremodule. -
onLayoutResize(paneName)One pane of the layout has been resized.
paneName: the name of the resized layout pane.
Triggered by the
coremodule. -
onCreateButton()Allows extensions to add their own buttons in the navigation bar. Implemented listeners have to return an HTML button element. For example:
myExtension.onCreateButton = function() { var button = $('<button class="btn btn-success"><i class="icon-rocket"></i></button>'); button.click(function() { eventMgr.onMessage('Booom!'); }); return button[0]; };Triggered by the
eventMgrmodule. OnlyExtensionmodules can handle this event. -
onCreateEditorButton()Allows extensions to add their own buttons in the side bar. Implemented listeners have to return an HTML button element. See
onCreateButtonfor a concrete example.Triggered by the
eventMgrmodule. OnlyExtensionmodules can handle this event. -
onCreatePreviewButton()Allows extensions to add their own buttons over the preview. Implemented listeners have to return an HTML button element. See
onCreateButtonfor a concrete example.Triggered by the
eventMgrmodule. OnlyExtensionmodules can handle this event.
PageDown events
-
onPagedownConfigure(editor)The Pagedown editor is about to be created.
editor: the Pagedown editor object beforerunhas been called.
Triggered by the
coremodule. -
onAsyncPreview(callback)Called after Pagedown's synchronous rendering to trigger extra asynchronous rendering (such as MathJax). Implemented listeners have to call the callback parameter after processing in order other
onAsyncPreviewlisteners to run.callback: the callback to call at the end of the asynchronous processing.
Triggered by the
eventMgrmodule. OnlyExtensionmodules can handle this event. -
onPreviewFinished(html)Called after every
onAsyncPreviewlisteners have been called.html: the finally rendered HTML.
-
onSectionsCreated(sectionList)The Markdown has been split into sections before rendering.
sectionList: the list of section objects. Each section object contains:text: the markdown substring contained in the section.textWithDelimiter: the text with an added delimiter.
Triggered by the
markdownSectionParserextension. -
onMarkdownTrim(offset)The Markdown has been left trimmed by a certain number of character.
offset: the number of characters that have been removed.
Triggered by the
yamlFrontMatterParserextension.
ACE events
-
onAceCreated(aceEditor)The ACE editor has just been created.
aceEditor: the ACE editor object.
Triggered by the
coremodule.
Written with StackEdit.
