Skip to content

Commit

Permalink
type writer
Browse files Browse the repository at this point in the history
  • Loading branch information
yann300 committed Jun 27, 2023
1 parent c7c308b commit ca2c2a9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
2 changes: 1 addition & 1 deletion apps/remix-ide/src/app/plugins/openaigpt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class OpenAIGpt extends Plugin {
})).json()

console.log(result)
this.call('terminal', 'log', { type: 'typewriterlog', value: result.choices[0].message.content })
this.call('terminal', 'log', { type: 'typewritersuccess', value: result.choices[0].message.content })
return result.data
}
}
7 changes: 6 additions & 1 deletion libs/remix-ui/terminal/src/lib/reducers/terminalReducer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CLEAR_CONSOLE, CMD_HISTORY, EMPTY_BLOCK, ERROR, HTML, INFO, KNOWN_TRANSACTION, LISTEN_ON_NETWORK, LOG, NEW_TRANSACTION, SCRIPT, UNKNOWN_TRANSACTION, WARN } from '../types/terminalTypes'
import { CLEAR_CONSOLE, CMD_HISTORY, EMPTY_BLOCK, ERROR, HTML, INFO, KNOWN_TRANSACTION, LISTEN_ON_NETWORK, LOG, TYPEWRITERLOG, NEW_TRANSACTION, SCRIPT, UNKNOWN_TRANSACTION, WARN } from '../types/terminalTypes'

export const initialState = {
journalBlocks: [
Expand Down Expand Up @@ -151,6 +151,11 @@ export const registerScriptRunnerReducer = (state, action) => {
...state,
journalBlocks: initialState.journalBlocks.push({ message: action.payload.message, style: 'text-log', provider: action.payload.provider })
}
case TYPEWRITERLOG:
return {
...state,
journalBlocks: initialState.journalBlocks.push({ message: action.payload.message, typewriter: true, style: 'text-log', provider: action.payload.provider })
}
case INFO:
return {
...state,
Expand Down
47 changes: 41 additions & 6 deletions libs/remix-ui/terminal/src/lib/remix-ui-terminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const RemixUiTerminal = (props: RemixUiTerminalProps) => {
// terminal inputRef
const inputEl = useRef(null)
const messagesEndRef = useRef(null)
const typeWriterIndexes = useRef([])

// terminal dragable
const panelRef = useRef(null)
Expand Down Expand Up @@ -314,6 +315,7 @@ export const RemixUiTerminal = (props: RemixUiTerminalProps) => {

const handleClearConsole = () => {
setClearConsole(true)
typeWriterIndexes.current = []
dispatch({ type: 'clearconsole', payload: [] })
inputEl.current.focus()
}
Expand Down Expand Up @@ -606,19 +608,41 @@ export const RemixUiTerminal = (props: RemixUiTerminalProps) => {
<div className={classNameBlock} data-id="block" key={i}><span className={x.style}>{ stringified } </span></div>
)
} else {
return (
<div className={classNameBlock} data-id="block" key={i}><span className={x.style}>{msg ? msg.toString() : null}</span></div>
)
// typeWriterIndexes: we don't want to rerender using typewriter when the react component updates
if (x.typewriter && !typeWriterIndexes.current.includes(index)) {
typeWriterIndexes.current.push(index)
return (
<div className={classNameBlock} data-id="block" key={index}> <span ref={(element) => {
typewrite(element, msg ? msg.toString() : null)
}} className={x.style}></span></div>
)
} else {
return (
<div className={classNameBlock} data-id="block" key={i}><span className={x.style}>{msg ? msg.toString() : null}</span></div>
)
}
}
})
} else {
if (typeof x.message !== 'function') {
// typeWriterIndexes: we don't want to rerender using typewriter when the react component updates
if (x.typewriter && !typeWriterIndexes.current.includes(index)) {
typeWriterIndexes.current.push(index)
return (
<div className={classNameBlock} data-id="block" key={index}> <span className={x.style}> {x.message}</span></div>
<div className={classNameBlock} data-id="block" key={index}> <span ref={(element) => {
typewrite(element, x.message)
}} className={x.style}></span></div>
)
} else {
if (typeof x.message !== 'function') {
return (
<div className={classNameBlock} data-id="block" key={index}> <span className={x.style}> {x.message}</span></div>
)
}
}
}
})}
})

}
<div ref={messagesEndRef} />
</div>
{ isOpen && <div id="terminalCli" data-id="terminalCli" className="remix_ui_terminal_cli position-absolute w-100" onClick={focusinput}>
Expand Down Expand Up @@ -655,6 +679,17 @@ export const RemixUiTerminal = (props: RemixUiTerminalProps) => {
)
}

const typewrite = (elementsRef, message) => {
(() => {
let count = 0
const id = setInterval(() => {
count++
elementsRef.innerText = message.substr(0, count)
if (message === count) clearInterval(id)
}, 5)
})()
}

function isHtml (value) {
if (!value.indexOf) return false
return value.indexOf('<div') !== -1
Expand Down
1 change: 1 addition & 0 deletions libs/remix-ui/terminal/src/lib/types/terminalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const NEW_CALL = 'newCall'

export const HTML = 'html'
export const LOG = 'log'
export const TYPEWRITERLOG = 'typewriterlog'
export const INFO = 'info'
export const WARN = 'warn'
export const ERROR = 'error'
Expand Down

0 comments on commit ca2c2a9

Please sign in to comment.