From 7cbdbf55b0f0468619bc6b0a330f7983b418196d Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Thu, 8 Jan 2015 17:06:59 +0100 Subject: [PATCH] Added prompt mode for editor autosave --- com.woltlab.wcf/templates/wysiwyg.tpl | 7 ++ .../js/3rdParty/redactor/plugins/wutil.js | 64 ++++++++++++++++--- wcfsetup/install/files/style/redactor.less | 2 +- wcfsetup/install/lang/de.xml | 3 + wcfsetup/install/lang/en.xml | 3 + 5 files changed, 70 insertions(+), 9 deletions(-) diff --git a/com.woltlab.wcf/templates/wysiwyg.tpl b/com.woltlab.wcf/templates/wysiwyg.tpl index fe5537309b..fc56a362a8 100644 --- a/com.woltlab.wcf/templates/wysiwyg.tpl +++ b/com.woltlab.wcf/templates/wysiwyg.tpl @@ -36,6 +36,9 @@ $(function() { 'wcf.bbcode.quote.title.clickToSet': '{lang}wcf.bbcode.quote.title.clickToSet{/lang}', 'wcf.bbcode.quote.title.javascript': '{lang}wcf.bbcode.quote.title.javascript{/lang}', 'wcf.global.noSelection': '{lang}wcf.global.noSelection{/lang}', + 'wcf.message.autosave.prompt': '{lang}wcf.message.autosave.prompt{/lang}', + 'wcf.message.autosave.prompt.confirm': '{lang}wcf.message.autosave.prompt.confirm{/lang}', + 'wcf.message.autosave.prompt.discard': '{lang}wcf.message.autosave.prompt.discard{/lang}', 'wcf.message.autosave.restored': '{lang}wcf.message.autosave.restored{/lang}', 'wcf.message.autosave.restored.confirm': '{lang}wcf.message.autosave.restored.confirm{/lang}', 'wcf.message.autosave.restored.revert': '{lang}wcf.message.autosave.restored.revert{/lang}', @@ -54,6 +57,8 @@ $(function() { {include file='wysiwygToolbar'} var $autosave = $textarea.data('autosave'); + var $autosaveLastEditTime = ($autosave && $textarea.data('autosaveLastEditTime')) ? (parseInt($textarea.data('autosaveLastEditTime')) || 0) : 0; + var $autosavePrompt = ($autosave && $textarea.data('autosavePrompt')) ? true : false; var $config = { autosave: false, buttons: $buttons, @@ -74,7 +79,9 @@ $(function() { autosave: { active: ($autosave) ? true : false, key: ($autosave) ? '{@$__wcf->getAutosavePrefix()}_' + $autosave : '', + lastEditTime: $autosaveLastEditTime, prefix: '{@$__wcf->getAutosavePrefix()}', + prompt: $autosavePrompt, saveOnInit: {if !$errorField|empty}true{else}false{/if} }, originalValue: $textarea.val() diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js index 329003eb2c..287a23d3d5 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js @@ -384,6 +384,19 @@ RedactorPlugins.wutil = function() { return false; } + if ($options.lastEditTime && ($options.lastEditTime * 1000) > $text.timestamp) { + // stored message is older than last edit time, consider it tainted and discard + this.wutil.autosavePurge(); + + return false; + } + + if ($options.prompt) { + this.wutil.autosaveShowNotice('prompt', $text); + + return false; + } + if (this.wutil.inWysiwygMode()) { this.wutil.setOption('woltlab.originalValue', $text.content); } @@ -392,7 +405,6 @@ RedactorPlugins.wutil = function() { } this.wutil.autosaveShowNotice('restored', { timestamp: $text.timestamp }); - WCF.DOMNodeInsertedHandler.execute(); return true; }, @@ -407,12 +419,13 @@ RedactorPlugins.wutil = function() { if ($autosaveNotice === null) { $autosaveNotice = $('
'); $autosaveNotice.appendTo(this.$box); - $autosaveNotice.on('transitionend webkitTransitionEnd', (function(event) { - if (event.originalEvent.propertyName !== 'opacity') { + + var $resetNotice = (function(event) { + if (event !== null && event.originalEvent.propertyName !== 'opacity') { return; } - if ($autosaveNotice.hasClass('open')) { + if ($autosaveNotice.hasClass('open') && event !== null) { if ($autosaveNotice.data('callbackOpen')) { $autosaveNotice.data('callbackOpen')(); } @@ -425,15 +438,47 @@ RedactorPlugins.wutil = function() { $autosaveNotice.removeData('callbackClose'); $autosaveNotice.removeData('callbackOpen'); - $autosaveNotice.removeClass('redactorAutosaveNoticeRestore'); + $autosaveNotice.removeClass('redactorAutosaveNoticeIcons'); $autosaveNotice.empty(); $('').appendTo($autosaveNotice); } - }).bind(this)); + }).bind(this); + + $autosaveNotice.on('transitionend webkitTransitionEnd', $resetNotice); } var $message = ''; switch (type) { + case 'prompt': + $('').prependTo($autosaveNotice); + var $accept = $('').appendTo($autosaveNotice); + var $discard = $('').appendTo($autosaveNotice); + console.debug(data); + $accept.click((function() { + this.wutil.replaceText(data.content); + + $resetNotice(null); + + this.wutil.autosaveShowNotice('restored', data); + }).bind(this)); + + $discard.click((function() { + this.wutil.autosavePurge(); + + $autosaveNotice.removeClass('open'); + }).bind(this)); + + $message = WCF.Language.get('wcf.message.autosave.prompt'); + $autosaveNotice.addClass('redactorAutosaveNoticeIcons'); + + var $uuid = ''; + $uuid = WCF.System.Event.addListener('com.woltlab.wcf.redactor', 'keydown_' + this.$textarea.wcfIdentify(), (function(data) { + WCF.System.Event.removeListener('com.woltlab.wcf.redactor', 'keydown_' + this.$textarea.wcfIdentify(), $uuid); + + setTimeout(function() { $autosaveNotice.removeClass('open'); }, 3000); + }).bind(this)); + break; + case 'restored': $('').prependTo($autosaveNotice); var $accept = $('').appendTo($autosaveNotice); @@ -453,8 +498,7 @@ RedactorPlugins.wutil = function() { }).bind(this)); $message = WCF.Language.get('wcf.message.autosave.restored'); - - $autosaveNotice.addClass('redactorAutosaveNoticeRestore'); + $autosaveNotice.addClass('redactorAutosaveNoticeIcons'); var $uuid = ''; $uuid = WCF.System.Event.addListener('com.woltlab.wcf.redactor', 'keydown_' + this.$textarea.wcfIdentify(), (function(data) { @@ -479,6 +523,10 @@ RedactorPlugins.wutil = function() { $autosaveNotice.children('span.redactorAutosaveMessage').text($message); $autosaveNotice.addClass('open'); + + if (type !== 'saved') { + WCF.DOMNodeInsertedHandler.execute(); + } }, /** diff --git a/wcfsetup/install/files/style/redactor.less b/wcfsetup/install/files/style/redactor.less index 4b778774fa..0d9138e8db 100644 --- a/wcfsetup/install/files/style/redactor.less +++ b/wcfsetup/install/files/style/redactor.less @@ -87,7 +87,7 @@ transition-delay: 0s; } - &.redactorAutosaveNoticeRestore > span.fa-check { + &.redactorAutosaveNoticeIcons > span.fa-check { margin-right: @wcfGapSmall; } diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index e3144de050..e4e1a8b1a5 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -2277,6 +2277,9 @@ Fehler sind beispielsweise: + + + diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index 4822b470dc..9c870d4543 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -2276,6 +2276,9 @@ Errors are: + + + -- 2.20.1