Skip to content

Commit

Permalink
adding copyit for stat/hyperlink
Browse files Browse the repository at this point in the history
  • Loading branch information
goldbuick committed Dec 14, 2024
1 parent 9b81638 commit e2addb3
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 2 deletions.
4 changes: 2 additions & 2 deletions zss/device/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useGadgetClient } from 'zss/gadget/data/state'
import { doasync } from 'zss/mapping/func'
import { waitfor } from 'zss/mapping/tick'
import { ispresent, isstring } from 'zss/mapping/types'
import { writeheader, writeoption, writetext } from 'zss/words/writeui'
import { writecopyit, writeheader, writeoption } from 'zss/words/writeui'

import {
api_error,
Expand Down Expand Up @@ -110,7 +110,7 @@ const register = createdevice(
case 'share':
doasync('register:share', async function () {
const shorturl = await shortenUrl(window.location.href)
writetext('share', shorturl)
writecopyit('share', shorturl, shorturl)
})
break
case 'refresh':
Expand Down
4 changes: 4 additions & 0 deletions zss/firmware/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ export const CLI_FIRMWARE = createfirmware({
'cli',
`${fg('white', '"!text"')} flag;${fg('gray', 'Input Label')}`,
)
writetext(
'cli',
`${fg('white', '"!copyit"')} flag;${fg('gray', 'Input Label')}`,
)
break
case 'helpdeveloper':
writeheader('cli', `developer commands`)
Expand Down
44 changes: 44 additions & 0 deletions zss/gadget/components/tape/elements/terminalcopyit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useCallback } from 'react'
import { ispresent } from 'zss/mapping/types'
import { tokenizeandwritetextformat } from 'zss/words/textformat'
import { writetext } from 'zss/words/writeui'

import { useWriteText } from '../../hooks'
import { inputcolor } from '../../panel/common'
import { UserInput } from '../../userinput'
import { TerminalItemInputProps, setuplogitem } from '../common'

export function TerminalCopyIt({
blink,
active,
prefix,
label,
words,
y,
}: TerminalItemInputProps) {
const context = useWriteText()

const invoke = useCallback(() => {
if (ispresent(navigator.clipboard)) {
const [, ...values] = words
const content = values.join(' ')
navigator.clipboard
.writeText(content)
.then(() => writetext('copyit', `copied!`))
.catch((err) => console.error(err))
}
}, [words])

const tcolor = inputcolor(!!active)

// render output
setuplogitem(!!blink, !!active, 0, y, context)
tokenizeandwritetextformat(
`${prefix} $purple$16 $yellowCOPYIT ${tcolor}${label}`,
context,
true,
)

context.changed()
return active && <UserInput OK_BUTTON={invoke} />
}
3 changes: 3 additions & 0 deletions zss/gadget/components/tape/elements/terminalitem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
setuplogitem,
} from '../common'

import { TerminalCopyIt } from './terminalcopyit'
import { TerminalHyperlink } from './terminalhyperlink'

export function TerminalItem({ blink, active, text, y }: TerminalItemProps) {
Expand Down Expand Up @@ -73,6 +74,8 @@ export function TerminalItem({ blink, active, text, y }: TerminalItemProps) {
case 'tx':
case 'text':
return null
case 'copyit':
return <TerminalCopyIt {...props} words={words} />
default:
case 'hyperlink':
return <TerminalHyperlink {...props} words={words} />
Expand Down
7 changes: 7 additions & 0 deletions zss/words/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ export function statformat(words: string[], first = true) {
type: STAT_TYPE.SCROLL,
values,
}
case 'copyit':
return {
type: STAT_TYPE.COPYIT,
values,
}
}
}

Expand Down Expand Up @@ -119,5 +124,7 @@ export function stattypestring(type: STAT_TYPE) {
return 'hotkey'
case STAT_TYPE.SCROLL:
return 'scroll'
case STAT_TYPE.COPYIT:
return 'copyit'
}
}
1 change: 1 addition & 0 deletions zss/words/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export enum STAT_TYPE {
LINK, // @link north - use this for passages between boards
HOTKEY, // @hotkey gorp g - we can associate hotkeys with objects
SCROLL, // @scroll, affords the user to write local code
COPYIT, // only useful in hyperlinks, used to copy into the clipboard
}

export type STAT = {
Expand Down
8 changes: 8 additions & 0 deletions zss/words/writeui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,11 @@ export function writeoption(from: string, option: string, label: string) {
export function writetext(from: string, text: string) {
write(from, `${COLOR_EDGE}$blue${text}`)
}

export function writehyperlink(from: string, hyperlink: string, label: string) {
write(from, `!${hyperlink};${label}`)
}

export function writecopyit(from: string, content: string, label: string) {
write(from, `!copyit ${content};${label}`)
}

0 comments on commit e2addb3

Please sign in to comment.