MediaWiki:Gadget-EditDraft.js

来自希灵百科
星臣讨论 | 贡献2017年1月18日 (三) 13:33的版本 (创建页面,内容为“eslint-disable: "use strict"; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descript...”
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索

注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:前往菜单 → 设置(Mac为Opera → Preferences),然后隐私和安全 → 清除浏览数据 → 缓存的图片和文件
/* eslint-disable */
"use strict";

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var QuickSaveDraftUI = function () {
    function QuickSaveDraftUI(buttonArea, textarea, section, timer) {
        _classCallCheck(this, QuickSaveDraftUI);

        var //vscode的缩进还是有问题
        saveButton = $('<input/>', {
            class: 'QuickSaveDraftSaveButton',
            val: '立即保存草稿',
            attr: {
                type: 'button'
            }
        }),
            recoverButton = $('<input/>', {
            class: 'QuickSaveDraftRecoverButton',
            val: '暂无草稿',
            attr: {
                type: 'button'
            }
        }).disable(),
            rollbackButton = $('<input/>', {
            class: 'QuickSaveDraftRollbackButton',
            val: '回退还原',
            attr: {
                type: 'button'
            }
        }).disable(),
            timerInput = $('<input/>', {
            class: 'QuickSaveDraftTimerInput',
            val: timer, //默认值
            maxlength: 2
        }),
            timerSave = $('<span/>', {
            id: 'QuickSaveDraftTimerSave'
        }),
            lastRun = $('<div/>', {
            class: 'QuickSaveDraftLastRunDiv',
            text: '上次草稿保存于'
        }).append($('<span/>', {
            class: 'QuickSaveDraftLastRun'
        })),
            pasueButton = $('<input/>', {
            class: 'QuickSaveDraftPauseButton',
            val: '点击暂停自动保存',
            attr: {
                type: 'button'
            }
        }),
            sectionList = $('<div/>').append('当前章节号为').append($('<span/>', {
            class: 'QuickSaveDraftSectionNow',
            css: {
                'font-weight': 'bold'
            },
            text: section
        })).append($('<span/>', {
            class: 'QuickSaveDraftSectionDraftSpan',
            text: ',草稿章节号为'
        }).append($('<span/>', {
            class: 'QuickSaveDraftSectionDraft'
        }))).append($('<span/>', {
            class: 'QuickSaveDraftSame',
            text: ',草稿内容与当前编辑内容一致'
        }));
        buttonArea.append(saveButton).append(recoverButton).append(rollbackButton).append('每隔').append(timerInput).append('分钟保存一次').append(timerSave).append(pasueButton).append(lastRun).append(sectionList);
        Object.assign(this, {
            textarea: textarea,
            saveButton: saveButton,
            recoverButton: recoverButton,
            rollbackButton: rollbackButton,
            pasueButton: pasueButton,
            timerInput: timerInput,
            lastRun: lastRun,
            sectionList: sectionList,
            timerSave: timerSave,
            timerSaveCode: null
        });
    }

    _createClass(QuickSaveDraftUI, [{
        key: 'watch',
        value: function watch(callback) {
            var self = this;
            self.timerInput.on('input', function () {
                var result = callback($(this).val()) ? ',保存成功!' : ',保存失败';
                self.timerSave.text(result);
                self.timerSave.removeClass('disapper');
                if (self.timerSaveCode) window.clearTimeout(self.timerSaveCode);
                self.timerSaveCode = window.setTimeout(function () {
                    self.timerSave.addClass('disapper');
                }, 3000);
            });
        }
    }, {
        key: 'complement',
        value: function complement(n, l) {
            n += '';
            if (n.length >= l) return n;
            var b = '<span style="speak:none;visibility:hidden;color:transparent;">',
                c = '';
            while (l > (n + c).length) {
                c += '0';
            }return b + c + '</span>' + n;
        }
    }]);

    return QuickSaveDraftUI;
}();

var QuickSaveDraft = function () {
    function QuickSaveDraft() {
        _classCallCheck(this, QuickSaveDraft);

        var pagename = mw.config.get('wgPageName'),
            user = mw.config.get('wgUserId');
        //this.clear();
        Object.assign(this, {
            pagename: pagename,
            user: user,
            version: '2.0',
            section: location.search.match(/section=(\d+)/) ? +location.search.match(/section=(\d+)/)[1] : -1,
            pause: false,
            originText: null
        });
        if (new Date().getTime() - +(this.get('timestamp') || '-1') > 86400) {
            // day => millisecond
            this.del('timestamp');
            this.del('draft');
            this.del('section');
        }
        if (['edit', 'submit'].indexOf(mw.config.get('wgAction')) !== -1) this.init();
    }

    _createClass(QuickSaveDraft, [{
        key: 'clear',
        value: function clear() {
            for (var i in localStorage) {
                if (/^AnnTools\-QuickSaveDraft/.test(i) && !/^AnnTools\-QuickSaveDraft\-\d/.test(i)) localStorage.removeItem(i);
            }
        }
    }, {
        key: 'init',
        value: function init() {
            var self = this,
                textarea = $('#wpTextbox1'),
                buttonArea = $('<div/>', {
                    css: {
                        'margin-top': '.5em'
                    }
                }).appendTo('#editform .editButtons'),
                date = self.get('timestamp') ? new Date(+self.get('timestamp')) : undefined;
            this.UI = new QuickSaveDraftUI(buttonArea, textarea, self.getSection(+self.section), self.get('timer') || (self.set('timer', 5), 5));
            self.UI.watch(self.saveTime);
            self.UI.rollbackButton.on('click', function () {
                if ($(this).is(':disabled') || self.originText === null) return;
                textarea.val(self.originText);
                self.originText = null;
                $(this).disable();
                self.check();
                return false;
            });
            self.UI.recoverButton.on('click', function () {
                if ($(this).is(':disabled')) return;
                if (self.checkCurrentSection()) return;
                self.originText = textarea.val();
                textarea.val(self.get('draft'));
                self.UI.rollbackButton.enable();
                self.check();
                return false;
            });
            self.UI.saveButton.on('click', function () {
                if ($(this).is(':disabled')) return;
                self.save(false);
                return false;
            });
            self.UI.pasueButton.on('click', function () {
                if (self.pause) {
                    $(this).val('点击暂停自动保存');
                    self.pause = false;
                } else {
                    $(this).val('点击继续自动保存');
                    self.pause = true;
                }
            });
            self.check();
            /* 下面一行太长了,换一下行 */
            if (date) self.UI.lastRun.fadeIn().find('span').html((date.getDate() === new Date().getDate() ? '今' : self.UI.complement(date.getDate(), 2)) + '日' + self.UI.complement(date.getHours(), 2) + '时' + self.UI.complement(date.getMinutes(), 2) + '分');
            self.UI.sectionList.find('.QuickSaveDraftSectionDraft').text(self.get('section') || '【暂无】');
            textarea.on('input change', function () {
                self.check();
            });
            window.setTimeout(self.loop, self.getTime());
        }
    }, {
        key: 'get',
        value: function get(t) {
            if (t === 'timer') return localStorage.getItem('AnnTools-QuickSaveDraft-' + this.user + '-timer');
            return localStorage.getItem('AnnTools-QuickSaveDraft-' + this.user + '-' + t + '-' + this.pagename);
        }
    }, {
        key: 'set',
        value: function set(t, v) {
            if (t === 'timer') return localStorage.setItem('AnnTools-QuickSaveDraft-' + this.user + '-timer', v);
            return localStorage.setItem('AnnTools-QuickSaveDraft-' + this.user + '-' + t + '-' + this.pagename, v);
        }
    }, {
        key: 'del',
        value: function del(t) {
            if (t === 'timer') return localStorage.removeItem('AnnTools-QuickSaveDraft-' + this.user + '-timer');
            return localStorage.removeItem('AnnTools-QuickSaveDraft-' + this.user + '-' + t + '-' + this.pagename);
        }
    }, {
        key: 'check',
        value: function check() {
            if (this.get('draft') === undefined) return this.UI.sectionList.find('.QuickSaveDraftSectionDraftSpan').fadeOut();
            this.UI.sectionList.find('.QuickSaveDraftSectionDraftDraft').text(this.get('section')).parent().fadeIn();
            if (this.checkCurrentSection()) //检测是否同一章节
                return this.UI.recoverButton.disable().val('暂无草稿');
            if (this.get('draft') !== this.UI.textarea.val()) {
                //检测草稿是否一致
                this.UI.recoverButton.enable().val('立即还原草稿');
                this.UI.sectionList.find('.QuickSaveDraftSame').fadeOut();
            } else {
                this.UI.recoverButton.disable().val('暂无草稿');
                this.UI.sectionList.find('.QuickSaveDraftSame').fadeIn();
            }
        }
    }, {
        key: 'checkCurrentSection',
        value: function checkCurrentSection() {
            return this.section !== (Number || Window).parseInt(this.get('section'));
        }
    }, {
        key: 'getTime',
        value: function getTime() {
            return +this.get('timer') * 60000; //min => millisecond
        }
    }, {
        key: 'saveTime',
        value: function saveTime(time) {
            if (!/\D/.test(time) && +time > 0) {
                window.AnnTools.QuickSaveDraft.set('timer', time);
                return true;
            } else {
                window.AnnTools.QuickSaveDraft.UI.timerInput.val(window.AnnTools.QuickSaveDraft.get('timer'));
                return false;
            }
        }
    }, {
        key: 'save',
        value: function save(s) {
            var date = new Date(),
                value = this.UI.textarea.val();
            this.set('draft', value);
            if (value != this.get('draft')) {
                if (s) alert('当前页面内容过长,无法保存草稿!');
                this.del('draft');
                this.UI.recoverButton.fadeOut();
                this.UI.saveButton.disable().val('无法保存草稿');
                this.UI.lastRun.fadeIn().text('当前页面内容过长,无法保存草稿!');
                return window.setTimeout(function () {
                    window.AnnTools.QuickSaveDraft.save = window.AnnTools.QuickSaveDraft.loop = function () {
                        return false;
                    };
                }, 0);
            }
            this.set('section', this.getSection(+this.section));
            this.set('timestamp', date.getTime());
            this.UI.sectionList.find('.QuickSaveDraftSectionDraftSpan').fadeIn().find('.QuickSaveDraftSectionDraft').text(this.getSection(+this.section));
            this.check();
            this.UI.lastRun.fadeIn().find('span').html('今日' + this.UI.complement(date.getHours(), 2) + '时' + this.UI.complement(date.getMinutes(), 2) + '分');
        }
    }, {
        key: 'loop',
        value: function loop() {
            if (!this.pause) window.AnnTools.QuickSaveDraft.save(true);
            return window.setTimeout(window.AnnTools.QuickSaveDraft.loop, window.AnnTools.QuickSaveDraft.getTime());
        }
    }, {
        key: 'getSection',
        value: function getSection(s) {
            return s === -1 ? '-1(全文)' : s;
        }
    }]);

    return QuickSaveDraft;
}();

jQuery.fn.extend({
    disable: function disable() {
        return this.each(function () {
            if ($(this).is('input,button')) this.disabled = true;
        });
    },
    enable: function enable() {
        return this.each(function () {
            if ($(this).is('input,button')) this.disabled = false;
        });
    }
});
window.AnnTools = window.AnnTools || {};
window.AnnTools.QuickSaveDraft = new QuickSaveDraft();