mirror of
https://github.com/slidevjs/rough-notation.git
synced 2026-01-27 07:20:26 +01:00
feat: .show() returns promise
This commit is contained in:
@@ -1,5 +1,3 @@
|
|||||||

|
|
||||||
|
|
||||||
# @slidev/rough-notation
|
# @slidev/rough-notation
|
||||||
|
|
||||||
Fork of [rough-notation](https://github.com/rough-stuff/rough-notation)
|
Fork of [rough-notation](https://github.com/rough-stuff/rough-notation)
|
||||||
|
|||||||
@@ -68,7 +68,14 @@ function parsePadding(config: RoughAnnotationConfig): FullPadding {
|
|||||||
return [5, 5, 5, 5]
|
return [5, 5, 5, 5]
|
||||||
}
|
}
|
||||||
|
|
||||||
export function renderAnnotation(svg: SVGSVGElement, rect: Rect, config: RoughAnnotationConfig, animationGroupDelay: number, animationDuration: number, seed: number) {
|
export function renderAnnotation(
|
||||||
|
svg: SVGSVGElement,
|
||||||
|
rect: Rect,
|
||||||
|
config: RoughAnnotationConfig,
|
||||||
|
animationGroupDelay: number,
|
||||||
|
animationDuration: number,
|
||||||
|
seed: number,
|
||||||
|
): Promise<void> {
|
||||||
const opList: OpSet[] = []
|
const opList: OpSet[] = []
|
||||||
let strokeWidth = config.strokeWidth || 2
|
let strokeWidth = config.strokeWidth || 2
|
||||||
const padding = parsePadding(config)
|
const padding = parsePadding(config)
|
||||||
@@ -239,8 +246,14 @@ export function renderAnnotation(svg: SVGSVGElement, rect: Rect, config: RoughAn
|
|||||||
style.animation = `rough-notation-dash ${duration}ms ease-out ${delay}ms forwards`
|
style.animation = `rough-notation-dash ${duration}ms ease-out ${delay}ms forwards`
|
||||||
durationOffset += duration
|
durationOffset += duration
|
||||||
}
|
}
|
||||||
|
return sleep(animationDuration + animationGroupDelay)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return sleep(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
function sleep(ms: number) {
|
||||||
|
return new Promise<void>(resolve => setTimeout(resolve, ms))
|
||||||
}
|
}
|
||||||
|
|
||||||
function opsToPath(opList: OpSet[]): string[] {
|
function opsToPath(opList: OpSet[]): string[] {
|
||||||
@@ -253,7 +266,6 @@ function opsToPath(opList: OpSet[]): string[] {
|
|||||||
case 'move':
|
case 'move':
|
||||||
if (path.trim())
|
if (path.trim())
|
||||||
paths.push(path.trim())
|
paths.push(path.trim())
|
||||||
|
|
||||||
path = `M${data[0]} ${data[1]} `
|
path = `M${data[0]} ${data[1]} `
|
||||||
break
|
break
|
||||||
case 'bcurveTo':
|
case 'bcurveTo':
|
||||||
|
|||||||
@@ -163,27 +163,24 @@ class RoughAnnotationImpl implements RoughAnnotation {
|
|||||||
this.pendingRefresh = Promise.resolve().then(() => {
|
this.pendingRefresh = Promise.resolve().then(() => {
|
||||||
if (this.isShowing())
|
if (this.isShowing())
|
||||||
this.show()
|
this.show()
|
||||||
|
|
||||||
delete this.pendingRefresh
|
delete this.pendingRefresh
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
show(): void {
|
async show(): Promise<void> {
|
||||||
switch (this._state) {
|
switch (this._state) {
|
||||||
case 'unattached':
|
case 'unattached':
|
||||||
break
|
break
|
||||||
case 'showing':
|
case 'showing':
|
||||||
this.hide()
|
this.hide()
|
||||||
if (this._svg)
|
if (this._svg)
|
||||||
this.render(this._svg, true)
|
await this.render(this._svg, true)
|
||||||
|
|
||||||
break
|
break
|
||||||
case 'not-showing':
|
case 'not-showing':
|
||||||
this.attach()
|
this.attach()
|
||||||
if (this._svg)
|
if (this._svg)
|
||||||
this.render(this._svg, false)
|
await this.render(this._svg, false)
|
||||||
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -205,7 +202,7 @@ class RoughAnnotationImpl implements RoughAnnotation {
|
|||||||
this.detachListeners()
|
this.detachListeners()
|
||||||
}
|
}
|
||||||
|
|
||||||
private render(svg: SVGSVGElement, ensureNoAnimation: boolean) {
|
private async render(svg: SVGSVGElement, ensureNoAnimation: boolean) {
|
||||||
let config = this._config
|
let config = this._config
|
||||||
if (ensureNoAnimation) {
|
if (ensureNoAnimation) {
|
||||||
config = JSON.parse(JSON.stringify(this._config))
|
config = JSON.parse(JSON.stringify(this._config))
|
||||||
@@ -216,14 +213,18 @@ class RoughAnnotationImpl implements RoughAnnotation {
|
|||||||
rects.forEach(rect => totalWidth += rect.w)
|
rects.forEach(rect => totalWidth += rect.w)
|
||||||
const totalDuration = (config.animationDuration || DEFAULT_ANIMATION_DURATION)
|
const totalDuration = (config.animationDuration || DEFAULT_ANIMATION_DURATION)
|
||||||
let delay = 0
|
let delay = 0
|
||||||
|
const promises: Promise<void>[] = []
|
||||||
for (let i = 0; i < rects.length; i++) {
|
for (let i = 0; i < rects.length; i++) {
|
||||||
const rect = rects[i]
|
const rect = rects[i]
|
||||||
const ad = totalDuration * (rect.w / totalWidth)
|
const ad = totalDuration * (rect.w / totalWidth)
|
||||||
renderAnnotation(svg, rects[i], config, delay + this._animationDelay, ad, this._seed)
|
promises.push(
|
||||||
|
renderAnnotation(svg, rects[i], config, delay + this._animationDelay, ad, this._seed),
|
||||||
|
)
|
||||||
delay += ad
|
delay += ad
|
||||||
}
|
}
|
||||||
this._lastSizes = rects
|
this._lastSizes = rects
|
||||||
this._state = 'showing'
|
this._state = 'showing'
|
||||||
|
return await Promise.all(promises)
|
||||||
}
|
}
|
||||||
|
|
||||||
private rects(): Rect[] {
|
private rects(): Rect[] {
|
||||||
|
|||||||
Reference in New Issue
Block a user