Skip to content

Commit 45770b1

Browse files
committed
fix(plugin-nprogress): fork nprogress to local
1 parent a82fc60 commit 45770b1

File tree

5 files changed

+267
-32
lines changed

5 files changed

+267
-32
lines changed

packages/@vuepress/plugin-nprogress/package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,9 @@
3232
"@vuepress/client": "workspace:*",
3333
"@vuepress/core": "workspace:*",
3434
"@vuepress/utils": "workspace:*",
35-
"nprogress": "^0.2.0",
3635
"vue": "^3.2.33",
3736
"vue-router": "^4.0.14"
3837
},
39-
"devDependencies": {
40-
"@types/nprogress": "^0.2.0"
41-
},
4238
"publishConfig": {
4339
"access": "public"
4440
}

packages/@vuepress/plugin-nprogress/src/client/composables/useNprogress.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import * as nprogress from 'nprogress'
21
import { onMounted } from 'vue'
32
import { useRouter } from 'vue-router'
3+
import { nprogress } from '../nprogress'
44

55
import '../styles/vars.css'
66
import '../styles/nprogress.css'
@@ -16,9 +16,6 @@ export const useNprogress = (): void => {
1616
// add path of current page as initial value
1717
loadedPages.add(router.currentRoute.value.path)
1818

19-
// set nprogress config
20-
nprogress.configure({ showSpinner: false })
21-
2219
// show progress bar before navigation
2320
router.beforeEach((to) => {
2421
if (!loadedPages.has(to.path)) {
Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
// Forked and modified from nprogress v0.2.0
2+
3+
interface NProgressOptions {
4+
minimum: number
5+
template: string
6+
easing: string
7+
speed: number
8+
trickle: boolean
9+
trickleRate: number
10+
trickleSpeed: number
11+
parent: string
12+
barSelector: string
13+
}
14+
15+
interface NProgress {
16+
settings: NProgressOptions
17+
status: number | null
18+
19+
set(number: number): NProgress
20+
isStarted(): boolean
21+
start(): NProgress
22+
done(force?: boolean): NProgress
23+
inc(amount?: number): NProgress
24+
trickle(): NProgress
25+
26+
/* Internal */
27+
28+
render(fromStart?: boolean): HTMLDivElement
29+
remove(): void
30+
isRendered(): boolean
31+
}
32+
33+
export const nprogress: NProgress = {
34+
settings: {
35+
minimum: 0.08,
36+
easing: 'ease',
37+
speed: 200,
38+
trickle: true,
39+
trickleRate: 0.02,
40+
trickleSpeed: 800,
41+
barSelector: '[role="bar"]',
42+
parent: 'body',
43+
template: '<div class="bar" role="bar"></div>',
44+
},
45+
status: null,
46+
set: (n) => {
47+
const started = nprogress.isStarted()
48+
n = clamp(n, nprogress.settings.minimum, 1)
49+
nprogress.status = n === 1 ? null : n
50+
const progress = nprogress.render(!started)
51+
const bar = progress.querySelector<HTMLElement>(
52+
nprogress.settings.barSelector
53+
)!
54+
const speed = nprogress.settings.speed
55+
const ease = nprogress.settings.easing
56+
57+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
58+
progress.offsetWidth /* Repaint */
59+
60+
queue((next) => {
61+
// Add transition
62+
css(bar, {
63+
transform: 'translate3d(' + toBarPerc(n) + '%,0,0)',
64+
transition: 'all ' + speed + 'ms ' + ease,
65+
})
66+
67+
if (n === 1) {
68+
// Fade out
69+
css(progress, {
70+
transition: 'none',
71+
opacity: '1',
72+
})
73+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
74+
progress.offsetWidth /* Repaint */
75+
76+
setTimeout(function () {
77+
css(progress, {
78+
transition: 'all ' + speed + 'ms linear',
79+
opacity: '0',
80+
})
81+
setTimeout(function () {
82+
nprogress.remove()
83+
next()
84+
}, speed)
85+
}, speed)
86+
} else {
87+
setTimeout(() => next(), speed)
88+
}
89+
})
90+
return nprogress
91+
},
92+
93+
isStarted: () => typeof nprogress.status === 'number',
94+
95+
start: () => {
96+
if (!nprogress.status) nprogress.set(0)
97+
98+
const work = (): void => {
99+
setTimeout(() => {
100+
if (!nprogress.status) return
101+
nprogress.trickle()
102+
work()
103+
}, nprogress.settings.trickleSpeed)
104+
}
105+
106+
if (nprogress.settings.trickle) work()
107+
108+
return nprogress
109+
},
110+
111+
done: (force) => {
112+
if (!force && !nprogress.status) return nprogress
113+
return nprogress.inc(0.3 + 0.5 * Math.random()).set(1)
114+
},
115+
116+
inc: (amount) => {
117+
let n = nprogress.status
118+
if (!n) {
119+
return nprogress.start()
120+
}
121+
122+
if (typeof amount !== 'number') {
123+
amount = (1 - n) * clamp(Math.random() * n, 0.1, 0.95)
124+
}
125+
n = clamp(n + amount, 0, 0.994)
126+
return nprogress.set(n)
127+
},
128+
129+
trickle: () => nprogress.inc(Math.random() * nprogress.settings.trickleRate),
130+
131+
render: (fromStart) => {
132+
if (nprogress.isRendered()) {
133+
return document.getElementById('nprogress') as HTMLDivElement
134+
}
135+
136+
addClass(document.documentElement, 'nprogress-busy')
137+
138+
const progress = document.createElement('div')
139+
progress.id = 'nprogress'
140+
progress.innerHTML = nprogress.settings.template
141+
142+
const bar = progress.querySelector<HTMLElement>(
143+
nprogress.settings.barSelector
144+
)!
145+
const perc = fromStart ? '-100' : toBarPerc(nprogress.status || 0)
146+
const parent = document.querySelector<HTMLElement>(
147+
nprogress.settings.parent
148+
)!
149+
150+
css(bar, {
151+
transition: 'all 0 linear',
152+
transform: 'translate3d(' + perc + '%,0,0)',
153+
})
154+
155+
if (parent !== document.body) {
156+
addClass(parent, 'nprogress-custom-parent')
157+
}
158+
159+
parent?.appendChild(progress)
160+
return progress
161+
},
162+
163+
remove: () => {
164+
removeClass(document.documentElement, 'nprogress-busy')
165+
removeClass(
166+
document.querySelector(nprogress.settings.parent)!,
167+
'nprogress-custom-parent'
168+
)
169+
const progress = document.getElementById('nprogress')
170+
progress && removeElement(progress)
171+
},
172+
173+
isRendered: () => !!document.getElementById('nprogress'),
174+
}
175+
176+
const clamp = (n: number, min: number, max: number): number => {
177+
if (n < min) return min
178+
if (n > max) return max
179+
return n
180+
}
181+
const toBarPerc = (n: number): number => (-1 + n) * 100
182+
const queue = (function () {
183+
const pending: ((next: () => void) => void)[] = []
184+
function next(): void {
185+
const fn = pending.shift()
186+
if (fn) {
187+
fn(next)
188+
}
189+
}
190+
return function (fn) {
191+
pending.push(fn)
192+
if (pending.length === 1) next()
193+
}
194+
})()
195+
const css = (function () {
196+
const cssPrefixes = ['Webkit', 'O', 'Moz', 'ms']
197+
const cssProps = {}
198+
199+
function camelCase(string: string): string {
200+
return string
201+
.replace(/^-ms-/, 'ms-')
202+
.replace(/-([\da-z])/gi, function (match, letter) {
203+
return letter.toUpperCase()
204+
})
205+
}
206+
207+
function getVendorProp(name: string): string {
208+
const style = document.body.style
209+
if (name in style) return name
210+
let i = cssPrefixes.length
211+
const capName = name.charAt(0).toUpperCase() + name.slice(1)
212+
let vendorName
213+
while (i--) {
214+
vendorName = cssPrefixes[i] + capName
215+
if (vendorName in style) return vendorName
216+
}
217+
return name
218+
}
219+
220+
function getStyleProp(name: string): string {
221+
name = camelCase(name)
222+
return cssProps[name] || (cssProps[name] = getVendorProp(name))
223+
}
224+
225+
function applyCss(element: HTMLElement, prop: string, value: string): void {
226+
prop = getStyleProp(prop)
227+
element.style[prop] = value
228+
}
229+
230+
return function (element: HTMLElement, properties: Record<string, string>) {
231+
for (const prop in properties) {
232+
const value = properties[prop]
233+
if (
234+
value !== undefined &&
235+
Object.prototype.hasOwnProperty.call(properties, prop)
236+
)
237+
applyCss(element, prop, value)
238+
}
239+
}
240+
})()
241+
242+
const hasClass = (element: Element | string, name: string): boolean => {
243+
const list = typeof element === 'string' ? element : classList(element)
244+
return list.indexOf(' ' + name + ' ') >= 0
245+
}
246+
const addClass = (element: Element, name: string): void => {
247+
const oldList = classList(element)
248+
const newList = oldList + name
249+
if (hasClass(oldList, name)) return
250+
element.className = newList.substring(1)
251+
}
252+
253+
const removeClass = (element: Element, name: string): void => {
254+
const oldList = classList(element)
255+
if (!hasClass(element, name)) return
256+
const newList = oldList.replace(' ' + name + ' ', ' ')
257+
element.className = newList.substring(1, newList.length - 1)
258+
}
259+
260+
const classList = (element: Element): string => {
261+
return (' ' + (element.className || '') + ' ').replace(/\s+/gi, ' ')
262+
}
263+
264+
const removeElement = (element: Element): void => {
265+
element && element.parentNode && element.parentNode.removeChild(element)
266+
}

packages/@vuepress/plugin-nprogress/src/client/styles/nprogress.css

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,3 @@
1111
width: 100%;
1212
height: 2px;
1313
}
14-
15-
#nprogress .peg {
16-
display: block;
17-
position: absolute;
18-
right: 0px;
19-
width: 100px;
20-
height: 100%;
21-
box-shadow: 0 0 10px var(--nprogress-color), 0 0 5px var(--nprogress-color);
22-
opacity: 1;
23-
transform: rotate(3deg) translate(0px, -4px);
24-
}

pnpm-lock.yaml

Lines changed: 0 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)