-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Axel Boberg <[email protected]>
- Loading branch information
1 parent
81a50da
commit 6a867e1
Showing
9 changed files
with
224 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react' | ||
import './style.css' | ||
|
||
export const LogHeader = ({ data = {}, onChange = () => {} }) => { | ||
function handleDataChange (key, value) { | ||
onChange({ | ||
...data, | ||
[key]: value | ||
}) | ||
} | ||
|
||
return ( | ||
<header className='LogHeader'> | ||
<div className='LogHeader-setting'> | ||
<input type='checkbox' checked={data?.autoScroll || false} onChange={e => handleDataChange('autoScroll', e.target.checked)} /> | ||
<div className='LogHeader-settingLabel'>Auto scroll</div> | ||
</div> | ||
</header> | ||
) | ||
} |
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,14 @@ | ||
.LogHeader { | ||
width: 100%; | ||
padding: 0 10px 10px; | ||
|
||
border-bottom: 1px solid var(--base-color--shade1); | ||
} | ||
|
||
.LogHeader-setting { | ||
display: flex; | ||
} | ||
|
||
.LogHeader-settingLabel { | ||
margin-left: 10px; | ||
} |
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,24 @@ | ||
import React from 'react' | ||
import './style.css' | ||
|
||
function zeroPad (n) { | ||
if (n < 10) { | ||
return `0${n}` | ||
} | ||
return `${n}` | ||
} | ||
|
||
function formatTime (ms) { | ||
const d = new Date(ms) | ||
return `${zeroPad(d.getHours())}:${zeroPad(d.getMinutes())}:${zeroPad(d.getSeconds())}.${d.getMilliseconds()}` | ||
} | ||
|
||
export const LogItem = ({ item = {} }) => { | ||
return ( | ||
<div className='LogItem' data-direction={item?.direction}> | ||
<div className='LogItem-timestamp'>{formatTime(item?.timestamp)}</div> | ||
<div className='LogItem-direction'>| {item?.direction === 'in' ? '>>' : '<<'}</div> | ||
<div className='LogItem-address'>{item?.address}</div> | ||
</div> | ||
) | ||
} |
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,22 @@ | ||
.LogItem { | ||
display: flex; | ||
width: 100%; | ||
|
||
white-space: nowrap; | ||
} | ||
|
||
.LogItem > div { | ||
margin-left: 10px; | ||
} | ||
|
||
.LogItem[data-direction='in']{ | ||
color: var(--base-color--accent1); | ||
} | ||
|
||
.LogItem-timestamp { | ||
width: 100px; | ||
} | ||
|
||
.LogItem:nth-child(odd) { | ||
background: var(--base-color--shade1); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react' | ||
|
||
import { SharedContext } from '../sharedContext' | ||
|
||
import { LogHeader } from '../components/LogHeader' | ||
import { LogItem } from '../components/LogItem' | ||
|
||
const PLUGIN_NAME = 'bridge-plugin-osc' | ||
|
||
const INITIAL_SETTINGS = { | ||
autoScroll: true | ||
} | ||
|
||
function scrollToBottom (el) { | ||
el.scrollTo(0, el.scrollHeight) | ||
} | ||
|
||
export const WidgetLog = () => { | ||
const [state] = React.useContext(SharedContext) | ||
const [settings, setSettings] = React.useState(INITIAL_SETTINGS) | ||
|
||
const scrollRef = React.useRef() | ||
|
||
const items = React.useMemo(() => { | ||
return state?.plugins?.[PLUGIN_NAME]?.log || [] | ||
}, [state?.plugins?.[PLUGIN_NAME]?.log]) | ||
|
||
React.useEffect(() => { | ||
if (!settings?.autoScroll) { | ||
return | ||
} | ||
scrollToBottom(scrollRef.current) | ||
}, [settings?.autoScroll, items.length]) | ||
|
||
function handleSettingsChange (newSettings) { | ||
setSettings(newSettings) | ||
} | ||
|
||
return ( | ||
<> | ||
<LogHeader data={settings} onChange={newSettings => handleSettingsChange(newSettings)} /> | ||
<div ref={scrollRef} className='LogList'> | ||
{ | ||
items.map((item, i) => { | ||
return <LogItem key={i} item={item} /> | ||
}) | ||
} | ||
</div> | ||
</> | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// SPDX-FileCopyrightText: 2024 Sveriges Television AB | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
const bridge = require('bridge') | ||
const manifest = require('../package.json') | ||
|
||
/** | ||
* Define the max length of the log stack, | ||
* the oldest items will get pushed off the | ||
* stack when this limit is reached | ||
* @type { Number } | ||
*/ | ||
const STACK_MAX_LENGTH = 100 | ||
|
||
let stack = [] | ||
let didLoadStackFromState = false | ||
|
||
/** | ||
* Add a log to the stack | ||
* | ||
* @param {{ | ||
* timestamp: Number, | ||
* direction: 'in' | 'out', | ||
* address: String | ||
* }} logInit | ||
*/ | ||
function addLog (logInit) { | ||
stack.splice(0, stack.length + 1 - STACK_MAX_LENGTH) | ||
stack.push(logInit) | ||
|
||
if (didLoadStackFromState) { | ||
return bridge.state.apply({ | ||
plugins: { | ||
[manifest.name]: { | ||
log: { $replace: stack } | ||
} | ||
} | ||
}) | ||
} | ||
|
||
return bridge.state.apply({ | ||
plugins: { | ||
[manifest.name]: { | ||
log: stack | ||
} | ||
} | ||
}) | ||
} | ||
exports.addLog = addLog | ||
|
||
/* | ||
If available read the stack | ||
from the state on init | ||
*/ | ||
;(async function () { | ||
const currentStack = await bridge.state.get(`plugins.${manifest.name}.log`) | ||
if (Array.isArray(currentStack)) { | ||
didLoadStackFromState = true | ||
stack = currentStack | ||
} | ||
})() |