var $callbackIdentifier = 'Redactor_' + $editorName;
WCF.System.Dependency.Manager.setup($callbackIdentifier, function() {
+ var $textarea = $('#' + $editorName);
+
//
// TODO: toolbar configuration / 'wysiwygToolbar.tpl'
//
plugins: [ 'wbbcode', 'wbutton', 'wfontcolor', 'wmonkeypatch', 'wutil' ]
};
+ // autosave config
+ if ($textarea.data('autosave')) {
+ $config.wautosave = {
+ active: true,
+ key: $textarea.data('autosave')
+ }
+ }
+
{event name='javascriptInit'}
- $('#' + $editorName).redactor($config);
+ $textarea.redactor($config);
});
head.load([
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*/
RedactorPlugins.wutil = {
+ /**
+ * autosave worker process
+ * @var WCF.PeriodicalExecuter
+ */
+ _autosaveWorker: null,
+
/**
* Initializes the RedactorPlugins.wutil plugin.
*/
init: function() {
// convert HTML to BBCode upon submit
this.$source.parents('form').submit($.proxy(this.submit, this));
+
+ if (this.getOption('wautosave').active) {
+ this.autosaveEnable();
+
+ if (this.getOption('wautosave').saveOnInit || this.$source.data('saveOnInit')) {
+ this._saveTextToStorage();
+ }
+ else {
+ this.autosaveRestore();
+ }
+ }
+
+ // prevent Redactor's own autosave
+ this.setOption('autosave', false);
},
/**
* @param string plainValue
*/
insertDynamic: function(html, plainValue) {
- if (plainValue === undefined || plainValue === null) {
- // shortcut if both 'html' and 'html' are the same
- plainValue = html;
- }
-
if (this.inWysiwygMode()) {
this.insertHtml(html);
}
else {
+ if (plainValue === undefined || plainValue === null) {
+ plainValue = html;
+ }
+
this.insertAtCaret(plainValue);
}
},
/**
* Sets an option value after initialization.
+ *
+ * @param string key
+ * @param mixed value
*/
setOption: function(key, value) {
this.opts[key] = value;
},
+ /**
+ * Reads an option value, returns null if key is unknown.
+ *
+ * @param string key
+ * @return mixed
+ */
+ getOption: function(key) {
+ if (this.opts[key]) {
+ return this.opts[key];
+ }
+
+ return null;
+ },
+
/**
* Returns true if editor is in source mode.
*
this._convertFromHtml();
}
+
+ this.autosavePurge();
},
/**
else {
this.$source.val('');
}
+ },
+
+ /**
+ * Enables automatic saving every minute.
+ *
+ * @param string key
+ */
+ autosaveEnable: function(key) {
+ if (!this.getOption('wautosave').active) {
+ this.setOption('wautosave', {
+ active: true,
+ key: key
+ });
+ }
+
+ if (this._autosaveWorker === null) {
+ var self = this;
+ this._autosaveWorker = new WCF.PeriodicalExecuter($.proxy(this._saveTextToStorage, this), 60 * 1000);
+ }
+
+ return true;
+ },
+
+ /**
+ * Saves current editor text to local browser storage.
+ */
+ _saveTextToStorage: function() {
+ localStorage.setItem(this.getOption('wautosave').key, this.getText());
+ },
+
+ /**
+ * Disables automatic saving.
+ */
+ autosaveDisable: function() {
+ if (!this.getOption('wautosave').active) {
+ return false;
+ }
+
+ this._autosaveWorker.stop();
+ this._autosaveWorker = null;
+
+ this.setOption('wautosave', {
+ active: false,
+ key: ''
+ });
+
+ return true;
+ },
+
+ /**
+ * Attempts to purge saved text.
+ *
+ * @param string key
+ */
+ autosavePurge: function() {
+ localStorage.removeItem(this.getOption('wautosave').key);
+ },
+
+ /**
+ * Attempts to restore a saved text.
+ */
+ autosaveRestore: function() {
+ var $options = this.getOption('wautosave');
+ var $text = localStorage.getItem($options.key);
+ if ($text !== null) {
+ if (this.inWysiwygMode()) {
+ this.toggle(false);
+ this.$source.val($text);
+ this.toggle(false);
+ this.focusEnd();
+ }
+ else {
+ this.$source.val($text);
+ }
+
+ return true;
+ }
+
+ return false;
}
};