-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(RelatedPrompt): component logic to be reusable
- Loading branch information
1 parent
d2adc0d
commit b8e20e7
Showing
6 changed files
with
187 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './infinite-scroll'; | ||
export * from './typing'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import type { Directive } from 'vue'; | ||
|
||
export interface TypingOptions { | ||
/** | ||
* The text (plain or html) that will be typed into the target element. | ||
*/ | ||
text: string; | ||
/** | ||
* The typing speed in milliseconds per character. | ||
* | ||
* @default 1 | ||
*/ | ||
speed?: number; | ||
/** | ||
* The attribute of the HTML element where the typed text will be placed. | ||
* If not specified, the text will be set as content (innerHTML). | ||
* | ||
* @example 'placeholder' | ||
*/ | ||
targetAttr?: string; | ||
} | ||
|
||
interface TypingHTMLElement extends HTMLElement { | ||
__timeoutId?: number; | ||
} | ||
|
||
const typingDirective: Directive<TypingHTMLElement, TypingOptions> = { | ||
mounted(el, binding) { | ||
execute(el, binding.value); | ||
}, | ||
|
||
updated(el, binding) { | ||
if (binding.value.text !== binding.oldValue?.text) { | ||
clearTimeout(el.__timeoutId); | ||
execute(el, binding.value); | ||
} | ||
}, | ||
|
||
unmounted(el) { | ||
clearTimeout(el.__timeoutId); | ||
} | ||
}; | ||
|
||
/** | ||
* Execute a typing animation in an HTML element. | ||
* | ||
* @param el - The HTML element where the typing animation will be displayed | ||
* @param options - Options for the behavior of the animation. | ||
*/ | ||
function execute(el: TypingHTMLElement, options: TypingOptions) { | ||
const { text, speed = 1, targetAttr = '' } = options; | ||
|
||
if (!text) { | ||
console.error('v-typing: "text" is required.'); | ||
return; | ||
} | ||
|
||
let index = 0; | ||
el.__timeoutId = el.__timeoutId || undefined; | ||
|
||
const updateContent = (value: string) => { | ||
if (targetAttr) { | ||
el.setAttribute(targetAttr, value); | ||
} else { | ||
el.innerHTML = value; | ||
} | ||
}; | ||
|
||
const type = () => { | ||
if (index < text.length) { | ||
updateContent(text.slice(0, index + 1)); | ||
index++; | ||
el.__timeoutId = setTimeout(type, speed) as unknown as number; | ||
} else { | ||
updateContent(text); | ||
clearTimeout(el.__timeoutId); | ||
el.__timeoutId = undefined; | ||
} | ||
}; | ||
|
||
type(); | ||
} | ||
|
||
export default typingDirective; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.