Skip to content

Commit 3399977

Browse files
authored
Merge branch 'develop' into develop
2 parents f03eb79 + 40a5fa8 commit 3399977

File tree

10 files changed

+536
-487
lines changed

10 files changed

+536
-487
lines changed

src/core/Docsify.js

+32-15
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
1-
import { initMixin } from './init';
2-
import { routerMixin } from './router';
3-
import { renderMixin } from './render';
4-
import { fetchMixin } from './fetch';
5-
import { eventMixin } from './event';
6-
import initGlobalAPI from './global-api';
1+
import { Router } from './router/index.js';
2+
import { Render } from './render/index.js';
3+
import { Fetch } from './fetch/index.js';
4+
import { Events } from './event/index.js';
5+
import initGlobalAPI from './global-api.js';
76

8-
export function Docsify() {
9-
this._init();
10-
}
7+
import config from './config.js';
8+
import { isFn } from './util/core';
9+
import { Lifecycle } from './init/lifecycle';
10+
11+
/** @typedef {new (...args: any[]) => any} Constructor */
12+
13+
// eslint-disable-next-line new-cap
14+
export class Docsify extends Fetch(Events(Render(Router(Lifecycle(Object))))) {
15+
constructor() {
16+
super();
1117

12-
const proto = Docsify.prototype;
18+
this.config = config(this);
1319

14-
initMixin(proto);
15-
routerMixin(proto);
16-
renderMixin(proto);
17-
fetchMixin(proto);
18-
eventMixin(proto);
20+
this.initLifecycle(); // Init hooks
21+
this.initPlugin(); // Install plugins
22+
this.callHook('init');
23+
this.initRouter(); // Add router
24+
this.initRender(); // Render base DOM
25+
this.initEvent(); // Bind events
26+
this.initFetch(); // Fetch data
27+
this.callHook('mounted');
28+
}
29+
30+
initPlugin() {
31+
[]
32+
.concat(this.config.plugins)
33+
.forEach(fn => isFn(fn) && fn(this._lifecycle, this));
34+
}
35+
}
1936

2037
/**
2138
* Global API

src/core/config.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { merge, hyphenate, isPrimitive, hasOwn } from './util/core';
22

33
const currentScript = document.currentScript;
44

5+
/** @param {import('./Docsify').Docsify} vm */
56
export default function(vm) {
67
const config = merge(
78
{

src/core/event/index.js

+38-30
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,47 @@ import { body, on } from '../util/dom';
33
import * as sidebar from './sidebar';
44
import { scrollIntoView, scroll2Top } from './scroll';
55

6-
export function eventMixin(proto) {
7-
proto.$resetEvents = function(source) {
8-
const { auto2top } = this.config;
6+
/** @typedef {import('../Docsify').Constructor} Constructor */
97

10-
(() => {
11-
// Rely on the browser's scroll auto-restoration when going back or forward
12-
if (source === 'history') {
13-
return;
14-
}
15-
// Scroll to ID if specified
16-
if (this.route.query.id) {
17-
scrollIntoView(this.route.path, this.route.query.id);
18-
}
19-
// Scroll to top if a link was clicked and auto2top is enabled
20-
if (source === 'navigate') {
21-
auto2top && scroll2Top(auto2top);
8+
/**
9+
* @template {!Constructor} T
10+
* @param {T} Base - The class to extend
11+
*/
12+
export function Events(Base) {
13+
return class Events extends Base {
14+
$resetEvents(source) {
15+
const { auto2top } = this.config;
16+
17+
(() => {
18+
// Rely on the browser's scroll auto-restoration when going back or forward
19+
if (source === 'history') {
20+
return;
21+
}
22+
// Scroll to ID if specified
23+
if (this.route.query.id) {
24+
scrollIntoView(this.route.path, this.route.query.id);
25+
}
26+
// Scroll to top if a link was clicked and auto2top is enabled
27+
if (source === 'navigate') {
28+
auto2top && scroll2Top(auto2top);
29+
}
30+
})();
31+
32+
if (this.config.loadNavbar) {
33+
sidebar.getAndActive(this.router, 'nav');
2234
}
23-
})();
35+
}
2436

25-
if (this.config.loadNavbar) {
26-
sidebar.getAndActive(this.router, 'nav');
37+
initEvent() {
38+
// Bind toggle button
39+
sidebar.btn('button.sidebar-toggle', this.router);
40+
sidebar.collapse('.sidebar', this.router);
41+
// Bind sticky effect
42+
if (this.config.coverpage) {
43+
!isMobile && on('scroll', sidebar.sticky);
44+
} else {
45+
body.classList.add('sticky');
46+
}
2747
}
2848
};
2949
}
30-
31-
export function initEvent(vm) {
32-
// Bind toggle button
33-
sidebar.btn('button.sidebar-toggle', vm.router);
34-
sidebar.collapse('.sidebar', vm.router);
35-
// Bind sticky effect
36-
if (vm.config.coverpage) {
37-
!isMobile && on('scroll', sidebar.sticky);
38-
} else {
39-
body.classList.add('sticky');
40-
}
41-
}

0 commit comments

Comments
 (0)