Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

操作记录,参考浏览器前进,后退 #7

Open
Liqihan opened this issue Oct 10, 2017 · 0 comments
Open

操作记录,参考浏览器前进,后退 #7

Liqihan opened this issue Oct 10, 2017 · 0 comments

Comments

@Liqihan
Copy link
Owner

Liqihan commented Oct 10, 2017

###模拟用户的行为操作,不改变浏览器历史的情况下,实现了用户行为的记录,项目中可以结合vuex,也可以单独使用单独使用

/*
 * @Author: grove.liqihan
 * @Date: 2017-10-10 10:08:14
 * @Desc: 记录下操作历史
 */

import _ from 'lodash';
// var state = {
//     data: {
//         a: 1
//     }
// }
// let historyRemember = new HistoryRemember(state, () => {
// })
// historyRemember.push({
//     selector: 'data.a',
//     value: 3
// })
// console.log(state); // { data: { a: 3 } }

// historyRemember.back();
// console.log(state); // { data: { a: 1 } }
// historyRemember.forward();
// console.log(state); // { data: { a: 3 } }
class HistoryRemember {
    constructor(state, onchange) {
        this.state = state;
        this.history = [null];
        this.index = 0;
        this.onchange = onchange || function () {}
    }
    get length () {
        return this.history.length;
    }
    push(changes) {
        if (!Array.isArray(changes)) {
            changes = [changes];
        }
        changes.forEach((change) => {
            change.oldValue = _.get(this.state, change.selector) || null;
            _.set(this.state, change.selector, _.cloneDeep(change.value));
        });
        this.index += 1;
        this.history.length = this.index;
        this.history.push(_.cloneDeep(changes));
        this.onchange();
        return true;
    }
    // 返回
    back () {
        if (this.index === 0) {
            return false;
        }
        let changes = this.history[this.index];
        changes.forEach(change => {
            _.set(this.state, change.selector, _.cloneDeep(change.oldValue))
        })
        this.index -=1;
        this.onchange();
        return true;
    }
    forward () {
        if (this.index === this.history.length - 1) {
            return false;
        }
        var changes = this.history[this.index + 1];
        changes.forEach(change => {
            if (change.value == null) {
                _.unset(this.state, change.selector)
            } else {
                _.set(this.state, change.selector, _.cloneDeep(change.value))
            }
        })
        this.index += 1;
        this.onchange();
        return true;
    }
    clear () {
        this.history.length = 1
        this.index = 0

        this.onchange()

        return true
    }
}
export default HistoryRemember
@Liqihan Liqihan changed the title 操作记录,参考浏览器前进,后退, 操作记录,参考浏览器前进,后退 Oct 10, 2017
@Liqihan Liqihan added the blog label Oct 12, 2017
@Liqihan Liqihan added the JS label Jan 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant