Refactor highlight caching & color management

This commit is contained in:
Mononaut 2022-02-18 18:19:02 -06:00
parent e845157610
commit 9a630adf3e
4 changed files with 32 additions and 28 deletions

View File

@ -10,16 +10,10 @@ import AddressIcon from '../assets/icon/cil-wallet.svg'
import TxIcon from '../assets/icon/cil-arrow-circle-right.svg' import TxIcon from '../assets/icon/cil-arrow-circle-right.svg'
import { matchQuery } from '../utils/search.js' import { matchQuery } from '../utils/search.js'
import { highlight, newHighlightQuery, highlightingFull } from '../stores.js' import { highlight, newHighlightQuery, highlightingFull } from '../stores.js'
import { hcl } from 'd3-color' import { hlToHex, highlightA, highlightB, highlightC, highlightD, highlightE } from '../utils/color.js'
const highlightColors = [ const highlightColors = [highlightA, highlightB, highlightC, highlightD, highlightE]
{ h: 0.03, l: 0.35 }, const highlightHexColors = highlightColors.map(c => hlToHex(c))
{ h: 0.40, l: 0.35 },
{ h: 0.65, l: 0.35 },
{ h: 0.85, l: 0.35 },
{ h: 0.12, l: 0.35 },
]
const highlightHexColors = highlightColors.map(c => hclToHex(c))
const usedColors = [false, false, false, false, false] const usedColors = [false, false, false, false, false]
const queryIcons = { const queryIcons = {
@ -36,6 +30,7 @@ export let tab
let query let query
let matchedQuery let matchedQuery
let queryAddress let queryAddress
let queryColorIndex
let queryColor let queryColor
let queryColorHex let queryColorHex
let watchlist = [] let watchlist = []
@ -48,8 +43,9 @@ $: {
if ($newHighlightQuery) { if ($newHighlightQuery) {
matchedQuery = matchQuery($newHighlightQuery) matchedQuery = matchQuery($newHighlightQuery)
if (matchedQuery) { if (matchedQuery) {
matchedQuery.color = queryColor matchedQuery.colorIndex = queryColorIndex
matchedQuery.colorHex = queryColorHex matchedQuery.color = highlightColors[queryColorIndex]
matchedQuery.colorHex = highlightHexColors[queryColorIndex]
add() add()
query = null query = null
} }
@ -66,8 +62,9 @@ $: {
if (query) { if (query) {
matchedQuery = matchQuery(query.trim()) matchedQuery = matchQuery(query.trim())
if (matchedQuery) { if (matchedQuery) {
matchedQuery.color = queryColor matchedQuery.colorIndex = queryColorIndex
matchedQuery.colorHex = queryColorHex matchedQuery.color = highlightColors[queryColorIndex]
matchedQuery.colorHex = highlightHexColors[queryColorIndex]
} }
} else matchedQuery = null } else matchedQuery = null
} }
@ -81,9 +78,20 @@ function init () {
try { try {
watchlist = JSON.parse(val) watchlist = JSON.parse(val)
watchlist.forEach(q => { watchlist.forEach(q => {
const i = highlightHexColors.findIndex(c => c === q.colorHex) if (q.colorIndex) {
if (i >= 0) usedColors[i] = q.colorHex usedColors[q.colorIndex] = true
else console.log('unknown color') q.color = highlightColors[q.colorIndex]
q.colorHex = highlightHexColors[q.colorIndex]
}
})
watchlist.forEach(q => {
if (!q.colorIndex){
const nextIndex = usedColors.findIndex(used => !used)
usedColors[nextIndex] = true
q.colorIndex = nextIndex
q.color = highlightColors[nextIndex]
q.colorHex = highlightHexColors[nextIndex]
}
}) })
} catch (e) { } catch (e) {
console.log('failed to parse cached highlight queries') console.log('failed to parse cached highlight queries')
@ -94,18 +102,14 @@ function init () {
function setNextColor () { function setNextColor () {
const nextIndex = usedColors.findIndex(used => !used) const nextIndex = usedColors.findIndex(used => !used)
if (nextIndex >= 0) { if (nextIndex >= 0) {
queryColorIndex = nextIndex
queryColor = highlightColors[nextIndex] queryColor = highlightColors[nextIndex]
queryColorHex = highlightHexColors[nextIndex] queryColorHex = highlightHexColors[nextIndex]
usedColors[nextIndex] = queryColorHex usedColors[nextIndex] = true
} }
} }
function clearUsedColor (hex) { function clearUsedColor (colorIndex) {
const clearIndex = usedColors.findIndex(used => used === hex) usedColors[colorIndex] = false
usedColors[clearIndex] = false
}
function hclToHex (color) {
return hcl(color.h * 360, 78.225, color.l * 150).hex()
} }
async function add () { async function add () {
@ -127,7 +131,7 @@ async function remove (index) {
const wasFull = $highlightingFull const wasFull = $highlightingFull
const removed = watchlist.splice(index,1) const removed = watchlist.splice(index,1)
if (removed.length) { if (removed.length) {
clearUsedColor(removed[0].colorHex) clearUsedColor(removed[0].colorIndex)
watchlist = watchlist watchlist = watchlist
if (tab) { if (tab) {
await tick() await tick()
@ -249,7 +253,7 @@ function searchSubmit (e) {
</div> </div>
</div> </div>
<div class="watchlist"> <div class="watchlist">
{#each watchlist as watched, index (watched.colorHex)} {#each watchlist as watched, index (watched.colorIndex)}
<div <div
class="watched" class="watched"
transition:fade={{ duration: 200 }} transition:fade={{ duration: 200 }}

View File

@ -34,7 +34,6 @@ export default class TxController {
this.applyHighlighting() this.applyHighlighting()
}) })
colorMode.subscribe(mode => { colorMode.subscribe(mode => {
console.log('color mode changed: ', mode)
this.setColorMode(mode) this.setColorMode(mode)
}) })
} }
@ -99,6 +98,7 @@ export default class TxController {
} }
dropTx (txid) { dropTx (txid) {
// don't actually need to do anything, just let the tx expire
} }
addBlock (blockData, realtime=true) { addBlock (blockData, realtime=true) {

View File

@ -127,6 +127,7 @@ class TxStream {
method: 'GET' method: 'GET'
}) })
let blockData = await response.json() let blockData = await response.json()
console.log('downloaded block', id)
window.dispatchEvent(new CustomEvent('bitcoin_block', { detail: { block: blockData, realtime: !calledOnLoad} })) window.dispatchEvent(new CustomEvent('bitcoin_block', { detail: { block: blockData, realtime: !calledOnLoad} }))
} else { } else {
console.log('already seen block ', lastBlockSeen) console.log('already seen block ', lastBlockSeen)
@ -159,7 +160,6 @@ class TxStream {
// notification of tx dropped from mempool // notification of tx dropped from mempool
case 'drop': case 'drop':
console.log('Dropping transaction ', msg.txid)
window.dispatchEvent(new CustomEvent('bitcoin_drop_tx', { detail: msg.txid })) window.dispatchEvent(new CustomEvent('bitcoin_drop_tx', { detail: msg.txid }))
break; break;

0
server/data/.gitkeep Normal file
View File